]> git.lizzy.rs Git - minetest.git/blob - src/client/inputhandler.h
bab.cpp: code modernization
[minetest.git] / src / client / inputhandler.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef INPUT_HANDLER_H
21 #define INPUT_HANDLER_H
22
23 #include "irrlichttypes_extrabloated.h"
24 #include "joystick_controller.h"
25 #include <list>
26 #include "keycode.h"
27 #include "renderingengine.h"
28
29 #ifdef HAVE_TOUCHSCREENGUI
30 #include "touchscreengui.h"
31 #endif
32
33 class KeyList : private std::list<KeyPress>
34 {
35         typedef std::list<KeyPress> super;
36         typedef super::iterator iterator;
37         typedef super::const_iterator const_iterator;
38
39         virtual const_iterator find(const KeyPress &key) const
40         {
41                 const_iterator f(begin());
42                 const_iterator e(end());
43
44                 while (f != e) {
45                         if (*f == key)
46                                 return f;
47
48                         ++f;
49                 }
50
51                 return e;
52         }
53
54         virtual iterator find(const KeyPress &key)
55         {
56                 iterator f(begin());
57                 iterator e(end());
58
59                 while (f != e) {
60                         if (*f == key)
61                                 return f;
62
63                         ++f;
64                 }
65
66                 return e;
67         }
68
69 public:
70         void clear() { super::clear(); }
71
72         void set(const KeyPress &key)
73         {
74                 if (find(key) == end())
75                         push_back(key);
76         }
77
78         void unset(const KeyPress &key)
79         {
80                 iterator p(find(key));
81
82                 if (p != end())
83                         erase(p);
84         }
85
86         void toggle(const KeyPress &key)
87         {
88                 iterator p(this->find(key));
89
90                 if (p != end())
91                         erase(p);
92                 else
93                         push_back(key);
94         }
95
96         bool operator[](const KeyPress &key) const { return find(key) != end(); }
97 };
98
99 class MyEventReceiver : public IEventReceiver
100 {
101 public:
102         // This is the one method that we have to implement
103         virtual bool OnEvent(const SEvent &event);
104
105         bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
106
107         // Checks whether a key was down and resets the state
108         bool WasKeyDown(const KeyPress &keyCode)
109         {
110                 bool b = keyWasDown[keyCode];
111                 if (b)
112                         keyWasDown.unset(keyCode);
113                 return b;
114         }
115
116         void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
117         void dontListenForKeys() { keysListenedFor.clear(); }
118
119         s32 getMouseWheel()
120         {
121                 s32 a = mouse_wheel;
122                 mouse_wheel = 0;
123                 return a;
124         }
125
126         void clearInput()
127         {
128                 keyIsDown.clear();
129                 keyWasDown.clear();
130
131                 leftclicked = false;
132                 rightclicked = false;
133                 leftreleased = false;
134                 rightreleased = false;
135
136                 left_active = false;
137                 middle_active = false;
138                 right_active = false;
139
140                 mouse_wheel = 0;
141         }
142
143         MyEventReceiver()
144         {
145 #ifdef HAVE_TOUCHSCREENGUI
146                 m_touchscreengui = NULL;
147 #endif
148         }
149
150         bool leftclicked = false;
151         bool rightclicked = false;
152         bool leftreleased = false;
153         bool rightreleased = false;
154
155         bool left_active = false;
156         bool middle_active = false;
157         bool right_active = false;
158
159         s32 mouse_wheel = 0;
160
161         JoystickController *joystick = nullptr;
162
163 #ifdef HAVE_TOUCHSCREENGUI
164         TouchScreenGUI *m_touchscreengui;
165 #endif
166
167 private:
168         // The current state of keys
169         KeyList keyIsDown;
170         // Whether a key has been pressed or not
171         KeyList keyWasDown;
172         // List of keys we listen for
173         // TODO perhaps the type of this is not really
174         // performant as KeyList is designed for few but
175         // often changing keys, and keysListenedFor is expected
176         // to change seldomly but contain lots of keys.
177         KeyList keysListenedFor;
178 };
179
180 class InputHandler
181 {
182 public:
183         InputHandler() {}
184         virtual ~InputHandler() {}
185
186         virtual bool isKeyDown(const KeyPress &keyCode) = 0;
187         virtual bool wasKeyDown(const KeyPress &keyCode) = 0;
188
189         virtual void listenForKey(const KeyPress &keyCode) {}
190         virtual void dontListenForKeys() {}
191
192         virtual v2s32 getMousePos() = 0;
193         virtual void setMousePos(s32 x, s32 y) = 0;
194
195         virtual bool getLeftState() = 0;
196         virtual bool getRightState() = 0;
197
198         virtual bool getLeftClicked() = 0;
199         virtual bool getRightClicked() = 0;
200         virtual void resetLeftClicked() = 0;
201         virtual void resetRightClicked() = 0;
202
203         virtual bool getLeftReleased() = 0;
204         virtual bool getRightReleased() = 0;
205         virtual void resetLeftReleased() = 0;
206         virtual void resetRightReleased() = 0;
207
208         virtual s32 getMouseWheel() = 0;
209
210         virtual void step(float dtime) {}
211
212         virtual void clear() {}
213
214         JoystickController joystick;
215 };
216 /*
217         Separated input handler
218 */
219
220 class RealInputHandler : public InputHandler
221 {
222 public:
223         RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
224         {
225                 m_receiver->joystick = &joystick;
226         }
227         virtual bool isKeyDown(const KeyPress &keyCode)
228         {
229                 return m_receiver->IsKeyDown(keyCode);
230         }
231         virtual bool wasKeyDown(const KeyPress &keyCode)
232         {
233                 return m_receiver->WasKeyDown(keyCode);
234         }
235         virtual void listenForKey(const KeyPress &keyCode)
236         {
237                 m_receiver->listenForKey(keyCode);
238         }
239         virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
240         virtual v2s32 getMousePos()
241         {
242                 if (RenderingEngine::get_raw_device()->getCursorControl()) {
243                         return RenderingEngine::get_raw_device()
244                                         ->getCursorControl()
245                                         ->getPosition();
246                 } else {
247                         return m_mousepos;
248                 }
249         }
250         virtual void setMousePos(s32 x, s32 y)
251         {
252                 if (RenderingEngine::get_raw_device()->getCursorControl()) {
253                         RenderingEngine::get_raw_device()
254                                         ->getCursorControl()
255                                         ->setPosition(x, y);
256                 } else {
257                         m_mousepos = v2s32(x, y);
258                 }
259         }
260
261         virtual bool getLeftState() { return m_receiver->left_active; }
262         virtual bool getRightState() { return m_receiver->right_active; }
263
264         virtual bool getLeftClicked() { return m_receiver->leftclicked; }
265         virtual bool getRightClicked() { return m_receiver->rightclicked; }
266         virtual void resetLeftClicked() { m_receiver->leftclicked = false; }
267         virtual void resetRightClicked() { m_receiver->rightclicked = false; }
268
269         virtual bool getLeftReleased() { return m_receiver->leftreleased; }
270         virtual bool getRightReleased() { return m_receiver->rightreleased; }
271         virtual void resetLeftReleased() { m_receiver->leftreleased = false; }
272         virtual void resetRightReleased() { m_receiver->rightreleased = false; }
273
274         virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
275
276         void clear()
277         {
278                 joystick.clear();
279                 m_receiver->clearInput();
280         }
281
282 private:
283         MyEventReceiver *m_receiver = nullptr;
284         v2s32 m_mousepos;
285 };
286
287 class RandomInputHandler : public InputHandler
288 {
289 public:
290         RandomInputHandler() {}
291         virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; }
292         virtual bool wasKeyDown(const KeyPress &keyCode) { return false; }
293         virtual v2s32 getMousePos() { return mousepos; }
294         virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
295
296         virtual bool getLeftState() { return leftdown; }
297         virtual bool getRightState() { return rightdown; }
298
299         virtual bool getLeftClicked() { return leftclicked; }
300         virtual bool getRightClicked() { return rightclicked; }
301         virtual void resetLeftClicked() { leftclicked = false; }
302         virtual void resetRightClicked() { rightclicked = false; }
303
304         virtual bool getLeftReleased() { return leftreleased; }
305         virtual bool getRightReleased() { return rightreleased; }
306         virtual void resetLeftReleased() { leftreleased = false; }
307         virtual void resetRightReleased() { rightreleased = false; }
308
309         virtual s32 getMouseWheel() { return 0; }
310
311         virtual void step(float dtime)
312         {
313                 {
314                         static float counter1 = 0;
315                         counter1 -= dtime;
316                         if (counter1 < 0.0) {
317                                 counter1 = 0.1 * Rand(1, 40);
318                                 keydown.toggle(getKeySetting("keymap_jump"));
319                         }
320                 }
321                 {
322                         static float counter1 = 0;
323                         counter1 -= dtime;
324                         if (counter1 < 0.0) {
325                                 counter1 = 0.1 * Rand(1, 40);
326                                 keydown.toggle(getKeySetting("keymap_special1"));
327                         }
328                 }
329                 {
330                         static float counter1 = 0;
331                         counter1 -= dtime;
332                         if (counter1 < 0.0) {
333                                 counter1 = 0.1 * Rand(1, 40);
334                                 keydown.toggle(getKeySetting("keymap_forward"));
335                         }
336                 }
337                 {
338                         static float counter1 = 0;
339                         counter1 -= dtime;
340                         if (counter1 < 0.0) {
341                                 counter1 = 0.1 * Rand(1, 40);
342                                 keydown.toggle(getKeySetting("keymap_left"));
343                         }
344                 }
345                 {
346                         static float counter1 = 0;
347                         counter1 -= dtime;
348                         if (counter1 < 0.0) {
349                                 counter1 = 0.1 * Rand(1, 20);
350                                 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
351                         }
352                 }
353                 {
354                         static float counter1 = 0;
355                         counter1 -= dtime;
356                         if (counter1 < 0.0) {
357                                 counter1 = 0.1 * Rand(1, 30);
358                                 leftdown = !leftdown;
359                                 if (leftdown)
360                                         leftclicked = true;
361                                 if (!leftdown)
362                                         leftreleased = true;
363                         }
364                 }
365                 {
366                         static float counter1 = 0;
367                         counter1 -= dtime;
368                         if (counter1 < 0.0) {
369                                 counter1 = 0.1 * Rand(1, 15);
370                                 rightdown = !rightdown;
371                                 if (rightdown)
372                                         rightclicked = true;
373                                 if (!rightdown)
374                                         rightreleased = true;
375                         }
376                 }
377                 mousepos += mousespeed;
378         }
379
380         s32 Rand(s32 min, s32 max);
381
382 private:
383         KeyList keydown;
384         v2s32 mousepos;
385         v2s32 mousespeed;
386         bool leftdown = false;
387         bool rightdown = false;
388         bool leftclicked = false;
389         bool rightclicked = false;
390         bool leftreleased = false;
391         bool rightreleased = false;
392 };
393
394 #endif