ScummVM API documentation
keymapper-defaults.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
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 3 of the License, or
10  * (at your option) 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 
22 #ifndef KEYMAPPER_DEFAULTS_H
23 #define KEYMAPPER_DEFAULTS_H
24 
25 #include "common/scummsys.h"
26 #include "common/hashmap.h"
27 #include "common/str.h"
28 #include "common/hash-str.h"
29 
30 namespace Common {
31 
32 class KeymapperDefaultBindings : public HashMap<String, StringArray> {
33 public:
40  void setDefaultBinding(String keymapId, String actionId, String hwInputId) {
41  setVal(keymapId + "_" + actionId, hwInputId.empty() ? StringArray() : StringArray(1, hwInputId));
42  }
43 
50  void addDefaultBinding(String keymapId, String actionId, String hwInputId) {
51  // NOTE: addDefaultBinding() cannot be used to remove bindings;
52  // use setDefaultBinding() with a nullptr or empty string as hwInputId instead.
53  if (hwInputId.empty()) {
54  return;
55  }
56 
57  KeymapperDefaultBindings::iterator it = findDefaultBinding(keymapId, actionId);
58  if (it != end()) {
59  // Don't allow an input to map to the same action multiple times
60  StringArray &itv = it->_value;
61 
62  Array<String>::const_iterator found = Common::find(itv.begin(), itv.end(), hwInputId);
63  if (found == itv.end()) {
64  itv.push_back(hwInputId);
65  }
66  } else {
67  setDefaultBinding(keymapId, actionId, hwInputId);
68  }
69  }
70 
77  const_iterator findDefaultBinding(String keymapId, String actionId) const {
78  return find(keymapId + "_" + actionId);
79  }
80 };
81 
82 } //namespace Common
83 
84 #endif // #ifndef KEYMAPPER_DEFAULTS_H
Definition: str.h:59
iterator end()
Definition: array.h:339
iterator begin()
Definition: array.h:334
In find(In first, In last, const T &v)
Definition: algorithm.h:168
void push_back(const T &element)
Definition: array.h:142
Definition: hashmap.h:85
Definition: algorithm.h:29
Array< String > StringArray
Definition: str-array.h:43
Definition: keymapper-defaults.h:32
void setDefaultBinding(String keymapId, String actionId, String hwInputId)
Definition: keymapper-defaults.h:40
void addDefaultBinding(String keymapId, String actionId, String hwInputId)
Definition: keymapper-defaults.h:50
const_iterator findDefaultBinding(String keymapId, String actionId) const
Definition: keymapper-defaults.h:77