ManaPlus
touchactionmodel.cpp
Go to the documentation of this file.
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 
26 
27 #include "utils/foreach.h"
28 
29 #include <algorithm>
30 
31 #include "debug.h"
32 
33 const int touchActionDataSize = 5;
34 
36 {
37  public:
39 
40  bool operator() (const SetupActionData *const data1,
41  const SetupActionData *const data2) const
42  {
43  if ((data1 == nullptr) || (data2 == nullptr))
44  return false;
45  return data1->name < data2->name;
46  }
48 
50  NamesModel(),
51  mActionId(),
52  mActionToSelection()
53 {
54  STD_VECTOR<SetupActionData*> data;
55 
56  for (int f = 0, fsz = touchActionDataSize; f < fsz; f ++)
57  {
58  int k = 0;
59  while (!touchActionData[f][k].name.empty())
60  {
61  data.push_back(&touchActionData[f][k]);
62  k ++;
63  }
64  }
65 
66  std::sort(data.begin(), data.end(), touchActionSorter);
67  int cnt = 0;
68  FOR_EACH (STD_VECTOR<SetupActionData*>::iterator, it, data)
69  {
70  const SetupActionData *const data1 = *it;
71  mNames.push_back(data1->name);
72  mActionId.push_back(data1->actionId);
73  mActionToSelection[data1->actionId] = cnt;
74  cnt ++;
75  }
76 }
77 
79 {
80  if (sel < 0 || sel > static_cast<signed int>(mActionId.size()))
81  return InputAction::NO_VALUE;
82  return mActionId[sel];
83 }
84 
86 {
87  const std::map<InputActionT, int>::const_iterator it
88  = mActionToSelection.find(action);
89  if (it == mActionToSelection.end())
90  return 0;
91  return (*it).second;
92 }
StringVect mNames
Definition: namesmodel.h:62
std::vector< InputActionT > mActionId
InputActionT getActionFromSelection(const int sel) const
int getSelectionFromAction(const InputActionT action) const
std::map< InputActionT, int > mActionToSelection
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
InputAction ::T InputActionT
Definition: inputaction.h:717
#define final
Definition: localconsts.h:46
#define A_DEFAULT_COPY(func)
Definition: localconsts.h:41
uint32_t data
std::string name
const InputActionT actionId
SetupActionData *const touchActionData[]
const int touchActionDataSize
static class SortTouchActionFunctor touchActionSorter