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

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

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