GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/touchactionmodel.cpp Lines: 22 25 88.0 %
Date: 2017-11-29 Branches: 12 24 50.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "gui/setupactiondata.h"
22
#include "gui/touchactiondata.h"
23
24
#include "gui/models/touchactionmodel.h"
25
26
#include "utils/foreach.h"
27
28
#include <algorithm>
29
30
#include "debug.h"
31
32
const int touchActionDataSize = 5;
33
34
static class SortTouchActionFunctor final
35
{
36
    public:
37
        A_DEFAULT_COPY(SortTouchActionFunctor)
38
39
        bool operator() (const SetupActionData *const data1,
40
                         const SetupActionData *const data2) const
41
        {
42

7512
            if ((data1 == nullptr) || (data2 == nullptr))
43
                return false;
44
15024
            return data1->name < data2->name;
45
        }
46
} touchActionSorter;
47
48
4
TouchActionsModel::TouchActionsModel() :
49
    NamesModel(),
50
    mActionId(),
51
12
    mActionToSelection()
52
{
53
8
    STD_VECTOR<SetupActionData*> data;
54
55
24
    for (int f = 0, fsz = touchActionDataSize; f < fsz; f ++)
56
    {
57
        int k = 0;
58
2548
        while (!touchActionData[f][k].name.empty())
59
        {
60
1672
            data.push_back(&touchActionData[f][k]);
61
836
            k ++;
62
        }
63
    }
64
65
8
    std::sort(data.begin(), data.end(), touchActionSorter);
66
4
    int cnt = 0;
67
848
    FOR_EACH (STD_VECTOR<SetupActionData*>::iterator, it, data)
68
    {
69
836
        const SetupActionData *const data1 = *it;
70
836
        mNames.push_back(data1->name);
71
836
        mActionId.push_back(data1->actionId);
72
836
        mActionToSelection[data1->actionId] = cnt;
73
836
        cnt ++;
74
    }
75
4
}
76
77
InputActionT TouchActionsModel::getActionFromSelection(const int sel) const
78
{
79
    if (sel < 0 || sel > static_cast<signed int>(mActionId.size()))
80
        return InputAction::NO_VALUE;
81
    return mActionId[sel];
82
}
83
84
52
int TouchActionsModel::getSelectionFromAction(const InputActionT action) const
85
{
86
    const std::map<InputActionT, int>::const_iterator it
87
104
        = mActionToSelection.find(action);
88
104
    if (it == mActionToSelection.end())
89
        return 0;
90
52
    return (*it).second;
91
}