@@ -21,9 +21,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
21
21
22
22
#include " irrlichttypes_extrabloated.h"
23
23
#include " joystick_controller.h"
24
+ #include < list>
24
25
#include " keycode.h"
25
26
#include " renderingengine.h"
26
- #include < unordered_set>
27
27
28
28
#ifdef HAVE_TOUCHSCREENGUI
29
29
#include " gui/touchscreengui.h"
@@ -61,32 +61,98 @@ struct KeyCache
61
61
InputHandler *handler;
62
62
};
63
63
64
+ class KeyList : private std ::list<KeyPress>
65
+ {
66
+ typedef std::list<KeyPress> super;
67
+ typedef super::iterator iterator;
68
+ typedef super::const_iterator const_iterator;
69
+
70
+ virtual const_iterator find (const KeyPress &key) const
71
+ {
72
+ const_iterator f (begin ());
73
+ const_iterator e (end ());
74
+
75
+ while (f != e) {
76
+ if (*f == key)
77
+ return f;
78
+
79
+ ++f;
80
+ }
81
+
82
+ return e;
83
+ }
84
+
85
+ virtual iterator find (const KeyPress &key)
86
+ {
87
+ iterator f (begin ());
88
+ iterator e (end ());
89
+
90
+ while (f != e) {
91
+ if (*f == key)
92
+ return f;
93
+
94
+ ++f;
95
+ }
96
+
97
+ return e;
98
+ }
99
+
100
+ public:
101
+ void clear () { super::clear (); }
102
+
103
+ void set (const KeyPress &key)
104
+ {
105
+ if (find (key) == end ())
106
+ push_back (key);
107
+ }
108
+
109
+ void unset (const KeyPress &key)
110
+ {
111
+ iterator p (find (key));
112
+
113
+ if (p != end ())
114
+ erase (p);
115
+ }
116
+
117
+ void toggle (const KeyPress &key)
118
+ {
119
+ iterator p (this ->find (key));
120
+
121
+ if (p != end ())
122
+ erase (p);
123
+ else
124
+ push_back (key);
125
+ }
126
+
127
+ bool operator [](const KeyPress &key) const { return find (key) != end (); }
128
+ };
129
+
64
130
class MyEventReceiver : public IEventReceiver
65
131
{
66
132
public:
67
133
// This is the one method that we have to implement
68
134
virtual bool OnEvent (const SEvent &event);
69
135
70
- bool IsKeyDown (const KeyPress &keyCode) const { return keyIsDown. count ( keyCode) ; }
136
+ bool IsKeyDown (const KeyPress &keyCode) const { return keyIsDown[ keyCode] ; }
71
137
72
138
// Checks whether a key was down and resets the state
73
139
bool WasKeyDown (const KeyPress &keyCode)
74
140
{
75
- bool b = keyWasDown. count ( keyCode) ;
141
+ bool b = keyWasDown[ keyCode] ;
76
142
if (b)
77
- keyWasDown.erase (keyCode);
143
+ keyWasDown.unset (keyCode);
78
144
return b;
79
145
}
80
146
81
147
// Checks whether a key was just pressed. State will be cleared
82
148
// in the subsequent iteration of Game::processPlayerInteraction
83
- bool WasKeyPressed (const KeyPress &keycode) const { return keyWasPressed. count ( keycode) ; }
149
+ bool WasKeyPressed (const KeyPress &keycode) const { return keyWasPressed[ keycode] ; }
84
150
85
151
// Checks whether a key was just released. State will be cleared
86
152
// in the subsequent iteration of Game::processPlayerInteraction
87
- bool WasKeyReleased (const KeyPress &keycode) const { return keyWasReleased. count ( keycode) ; }
153
+ bool WasKeyReleased (const KeyPress &keycode) const { return keyWasReleased[ keycode] ; }
88
154
89
- void listenForKey (const KeyPress &keyCode) { keysListenedFor.insert (keyCode); }
155
+ void listenForKey (const KeyPress &keyCode) { keysListenedFor.set (keyCode); }
90
156
void dontListenForKeys () { keysListenedFor.clear (); }
91
157
92
158
s32 getMouseWheel ()
@@ -132,20 +198,24 @@ class MyEventReceiver : public IEventReceiver
132
198
#endif
133
199
134
200
private:
135
- // ! The current state of keys
136
- std::unordered_set<KeyPress> keyIsDown;
201
+ // The current state of keys
202
+ KeyList keyIsDown;
137
203
138
- // ! Whether a key was down
139
- std::unordered_set<KeyPress> keyWasDown;
204
+ // Whether a key was down
205
+ KeyList keyWasDown;
140
206
141
- // ! Whether a key has just been pressed
142
- std::unordered_set<KeyPress> keyWasPressed;
207
+ // Whether a key has just been pressed
208
+ KeyList keyWasPressed;
143
209
144
- // ! Whether a key has just been released
145
- std::unordered_set<KeyPress> keyWasReleased;
210
+ // Whether a key has just been released
211
+ KeyList keyWasReleased;
146
212
147
- // ! List of keys we listen for
148
- std::unordered_set<KeyPress> keysListenedFor;
213
+ // List of keys we listen for
214
+ // TODO perhaps the type of this is not really
215
+ // performant as KeyList is designed for few but
216
+ // often changing keys, and keysListenedFor is expected
217
+ // to change seldomly but contain lots of keys.
218
+ KeyList keysListenedFor;
149
219
};
150
220
151
221
class InputHandler
@@ -277,7 +347,7 @@ class RandomInputHandler : public InputHandler
277
347
return true ;
278
348
}
279
349
280
- virtual bool isKeyDown (GameKeyType k) { return keydown. count ( keycache.key [k]) ; }
350
+ virtual bool isKeyDown (GameKeyType k) { return keydown[ keycache.key [k]] ; }
281
351
virtual bool wasKeyDown (GameKeyType k) { return false ; }
282
352
virtual bool wasKeyPressed (GameKeyType k) { return false ; }
283
353
virtual bool wasKeyReleased (GameKeyType k) { return false ; }
@@ -292,7 +362,7 @@ class RandomInputHandler : public InputHandler
292
362
s32 Rand (s32 min, s32 max);
293
363
294
364
private:
295
- std::unordered_set<KeyPress> keydown;
365
+ KeyList keydown;
296
366
v2s32 mousepos;
297
367
v2s32 mousespeed;
298
368
};
0 commit comments