@@ -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>
25
24
#include " keycode.h"
26
25
#include " renderingengine.h"
26
+ #include < unordered_set>
27
27
28
28
#ifdef HAVE_TOUCHSCREENGUI
29
29
#include " gui/touchscreengui.h"
@@ -61,98 +61,32 @@ 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
-
130
64
class MyEventReceiver : public IEventReceiver
131
65
{
132
66
public:
133
67
// This is the one method that we have to implement
134
68
virtual bool OnEvent (const SEvent &event);
135
69
136
- bool IsKeyDown (const KeyPress &keyCode) const { return keyIsDown[ keyCode] ; }
70
+ bool IsKeyDown (const KeyPress &keyCode) const { return keyIsDown. count ( keyCode) ; }
137
71
138
72
// Checks whether a key was down and resets the state
139
73
bool WasKeyDown (const KeyPress &keyCode)
140
74
{
141
- bool b = keyWasDown[ keyCode] ;
75
+ bool b = keyWasDown. count ( keyCode) ;
142
76
if (b)
143
- keyWasDown.unset (keyCode);
77
+ keyWasDown.erase (keyCode);
144
78
return b;
145
79
}
146
80
147
81
// Checks whether a key was just pressed. State will be cleared
148
82
// in the subsequent iteration of Game::processPlayerInteraction
149
- bool WasKeyPressed (const KeyPress &keycode) const { return keyWasPressed[ keycode] ; }
83
+ bool WasKeyPressed (const KeyPress &keycode) const { return keyWasPressed. count ( keycode) ; }
150
84
151
85
// Checks whether a key was just released. State will be cleared
152
86
// in the subsequent iteration of Game::processPlayerInteraction
153
- bool WasKeyReleased (const KeyPress &keycode) const { return keyWasReleased[ keycode] ; }
87
+ bool WasKeyReleased (const KeyPress &keycode) const { return keyWasReleased. count ( keycode) ; }
154
88
155
- void listenForKey (const KeyPress &keyCode) { keysListenedFor.set (keyCode); }
89
+ void listenForKey (const KeyPress &keyCode) { keysListenedFor.insert (keyCode); }
156
90
void dontListenForKeys () { keysListenedFor.clear (); }
157
91
158
92
s32 getMouseWheel ()
@@ -198,24 +132,20 @@ class MyEventReceiver : public IEventReceiver
198
132
#endif
199
133
200
134
private:
201
- // The current state of keys
202
- KeyList keyIsDown;
135
+ // ! The current state of keys
136
+ std::unordered_set<KeyPress> keyIsDown;
203
137
204
- // Whether a key was down
205
- KeyList keyWasDown;
138
+ // ! Whether a key was down
139
+ std::unordered_set<KeyPress> keyWasDown;
206
140
207
- // Whether a key has just been pressed
208
- KeyList keyWasPressed;
141
+ // ! Whether a key has just been pressed
142
+ std::unordered_set<KeyPress> keyWasPressed;
209
143
210
- // Whether a key has just been released
211
- KeyList keyWasReleased;
144
+ // ! Whether a key has just been released
145
+ std::unordered_set<KeyPress> keyWasReleased;
212
146
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;
147
+ // ! List of keys we listen for
148
+ std::unordered_set<KeyPress> keysListenedFor;
219
149
};
220
150
221
151
class InputHandler
@@ -347,7 +277,7 @@ class RandomInputHandler : public InputHandler
347
277
return true ;
348
278
}
349
279
350
- virtual bool isKeyDown (GameKeyType k) { return keydown[ keycache.key [k]] ; }
280
+ virtual bool isKeyDown (GameKeyType k) { return keydown. count ( keycache.key [k]) ; }
351
281
virtual bool wasKeyDown (GameKeyType k) { return false ; }
352
282
virtual bool wasKeyPressed (GameKeyType k) { return false ; }
353
283
virtual bool wasKeyReleased (GameKeyType k) { return false ; }
@@ -362,7 +292,7 @@ class RandomInputHandler : public InputHandler
362
292
s32 Rand (s32 min, s32 max);
363
293
364
294
private:
365
- KeyList keydown;
295
+ std::unordered_set<KeyPress> keydown;
366
296
v2s32 mousepos;
367
297
v2s32 mousespeed;
368
298
};
0 commit comments