GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/touchactionmodel.cpp Lines: 22 25 88.0 %
Date: 2018-09-20 Branches: 12 26 46.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2018  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

3662
            if ((data1 == nullptr) || (data2 == nullptr))
43
                return false;
44
7324
            return data1->name < data2->name;
45
        }
46
} touchActionSorter;
47
48
2
TouchActionsModel::TouchActionsModel() :
49
    NamesModel(),
50
    mActionId(),
51
6
    mActionToSelection()
52
{
53
4
    STD_VECTOR<SetupActionData*> data;
54
55
12
    for (int f = 0, fsz = touchActionDataSize; f < fsz; f ++)
56
    {
57
        int k = 0;
58
1280
        while (!touchActionData[f][k].name.empty())
59
        {
60
840
            data.push_back(&touchActionData[f][k]);
61
420
            k ++;
62
        }
63
    }
64
65
4
    std::sort(data.begin(), data.end(), touchActionSorter);
66
2
    int cnt = 0;
67
426
    FOR_EACH (STD_VECTOR<SetupActionData*>::iterator, it, data)
68
    {
69
420
        const SetupActionData *const data1 = *it;
70
420
        mNames.push_back(data1->name);
71
420
        mActionId.push_back(data1->actionId);
72
420
        mActionToSelection[data1->actionId] = cnt;
73
420
        cnt ++;
74
    }
75
2
}
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
26
int TouchActionsModel::getSelectionFromAction(const InputActionT action) const
85
{
86
    const std::map<InputActionT, int>::const_iterator it
87
52
        = mActionToSelection.find(action);
88
52
    if (it == mActionToSelection.end())
89
        return 0;
90
26
    return (*it).second;
91
}