]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/inputhandler.h
Merged Minetest
[dragonfireclient.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 #pragma once
21
22 #include "irrlichttypes_extrabloated.h"
23 #include "joystick_controller.h"
24 #include <list>
25 #include "keycode.h"
26 #include "renderingengine.h"
27
28 #ifdef HAVE_TOUCHSCREENGUI
29 #include "gui/touchscreengui.h"
30 #endif
31
32 class InputHandler;
33
34 /****************************************************************************
35  Fast key cache for main game loop
36  ****************************************************************************/
37
38 /* This is faster than using getKeySetting with the tradeoff that functions
39  * using it must make sure that it's initialised before using it and there is
40  * no error handling (for example bounds checking). This is really intended for
41  * use only in the main running loop of the client (the_game()) where the faster
42  * (up to 10x faster) key lookup is an asset. Other parts of the codebase
43  * (e.g. formspecs) should continue using getKeySetting().
44  */
45 struct KeyCache
46 {
47
48         KeyCache()
49         {
50                 handler = NULL;
51                 populate();
52                 populate_nonchanging();
53         }
54
55         void populate();
56
57         // Keys that are not settings dependent
58         void populate_nonchanging();
59
60         KeyPress key[KeyType::INTERNAL_ENUM_COUNT];
61         InputHandler *handler;
62 };
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 class MyEventReceiver : public IEventReceiver
131 {
132 public:
133         // This is the one method that we have to implement
134         virtual bool OnEvent(const SEvent &event);
135
136         bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
137
138         // Checks whether a key was down and resets the state
139         bool WasKeyDown(const KeyPress &keyCode)
140         {
141                 bool b = keyWasDown[keyCode];
142                 if (b)
143                         keyWasDown.unset(keyCode);
144                 return b;
145         }
146
147         // Checks whether a key was just pressed. State will be cleared
148         // in the subsequent iteration of Game::processPlayerInteraction
149         bool WasKeyPressed(const KeyPress &keycode) const { return keyWasPressed[keycode]; }
150
151         // Checks whether a key was just released. State will be cleared
152         // in the subsequent iteration of Game::processPlayerInteraction
153         bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; }
154
155         void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
156         void dontListenForKeys() { keysListenedFor.clear(); }
157
158         s32 getMouseWheel()
159         {
160                 s32 a = mouse_wheel;
161                 mouse_wheel = 0;
162                 return a;
163         }
164
165         void clearInput()
166         {
167                 keyIsDown.clear();
168                 keyWasDown.clear();
169                 keyWasPressed.clear();
170                 keyWasReleased.clear();
171
172                 mouse_wheel = 0;
173         }
174
175         void clearWasKeyPressed()
176         {
177                 keyWasPressed.clear();
178         }
179
180         void clearWasKeyReleased()
181         {
182                 keyWasReleased.clear();
183         }
184
185         MyEventReceiver()
186         {
187 #ifdef HAVE_TOUCHSCREENGUI
188                 m_touchscreengui = NULL;
189 #endif
190         }
191
192         s32 mouse_wheel = 0;
193
194         JoystickController *joystick = nullptr;
195
196 #ifdef HAVE_TOUCHSCREENGUI
197         TouchScreenGUI *m_touchscreengui;
198 #endif
199
200         // The current state of keys
201         KeyList keyIsDown;
202
203         // Whether a key was down
204         KeyList keyWasDown;
205
206         // Whether a key has just been pressed
207         KeyList keyWasPressed;
208
209         // Whether a key has just been released
210         KeyList keyWasReleased;
211
212         // List of keys we listen for
213         // TODO perhaps the type of this is not really
214         // performant as KeyList is designed for few but
215         // often changing keys, and keysListenedFor is expected
216         // to change seldomly but contain lots of keys.
217         KeyList keysListenedFor;
218 };
219
220 class InputHandler
221 {
222 public:
223         InputHandler()
224         {
225                 keycache.handler = this;
226                 keycache.populate();
227         }
228
229         virtual ~InputHandler() = default;
230
231         virtual bool isRandom() const
232         {
233                 return false;
234         }
235
236         virtual bool isKeyDown(GameKeyType k) = 0;
237         virtual void setKeypress(const KeyPress &keyCode) = 0;
238         virtual void unsetKeypress(const KeyPress &keyCode) = 0;
239         virtual bool wasKeyDown(GameKeyType k) = 0;
240         virtual bool wasKeyPressed(GameKeyType k) = 0;
241         virtual bool wasKeyReleased(GameKeyType k) = 0;
242         virtual bool cancelPressed() = 0;
243
244         virtual void clearWasKeyPressed() {}
245         virtual void clearWasKeyReleased() {}
246
247         virtual void listenForKey(const KeyPress &keyCode) {}
248         virtual void dontListenForKeys() {}
249
250         virtual v2s32 getMousePos() = 0;
251         virtual void setMousePos(s32 x, s32 y) = 0;
252
253         virtual s32 getMouseWheel() = 0;
254
255         virtual void step(float dtime) {}
256
257         virtual void clear() {}
258
259         JoystickController joystick;
260         KeyCache keycache;
261 };
262 /*
263         Separated input handler
264 */
265
266 class RealInputHandler : public InputHandler
267 {
268 public:
269         RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
270         {
271                 m_receiver->joystick = &joystick;
272         }
273         virtual bool isKeyDown(GameKeyType k)
274         {
275                 return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
276         }
277         virtual void setKeypress(const KeyPress &keyCode)
278         {
279                 m_receiver->keyIsDown.set(keyCode);
280                 m_receiver->keyWasDown.set(keyCode);
281         }
282         virtual void unsetKeypress(const KeyPress &keyCode)
283         {
284                 m_receiver->keyIsDown.unset(keyCode);
285         }
286         virtual bool wasKeyDown(GameKeyType k)
287         {
288                 return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
289         }
290         virtual bool wasKeyPressed(GameKeyType k)
291         {
292                 return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyReleased(k);
293         }
294         virtual bool wasKeyReleased(GameKeyType k)
295         {
296                 return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
297         }
298         virtual bool cancelPressed()
299         {
300                 return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
301         }
302         virtual void clearWasKeyPressed()
303         {
304                 m_receiver->clearWasKeyPressed();
305         }
306         virtual void clearWasKeyReleased()
307         {
308                 m_receiver->clearWasKeyReleased();
309         }
310         virtual void listenForKey(const KeyPress &keyCode)
311         {
312                 m_receiver->listenForKey(keyCode);
313         }
314         virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
315         virtual v2s32 getMousePos()
316         {
317                 if (RenderingEngine::get_raw_device()->getCursorControl()) {
318                         return RenderingEngine::get_raw_device()
319                                         ->getCursorControl()
320                                         ->getPosition();
321                 }
322
323                 return m_mousepos;
324         }
325
326         virtual void setMousePos(s32 x, s32 y)
327         {
328                 if (RenderingEngine::get_raw_device()->getCursorControl()) {
329                         RenderingEngine::get_raw_device()
330                                         ->getCursorControl()
331                                         ->setPosition(x, y);
332                 } else {
333                         m_mousepos = v2s32(x, y);
334                 }
335         }
336
337         virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
338
339         void clear()
340         {
341                 joystick.clear();
342                 m_receiver->clearInput();
343         }
344
345         private:
346         MyEventReceiver *m_receiver = nullptr;
347         v2s32 m_mousepos;
348 };
349
350 class RandomInputHandler : public InputHandler
351 {
352 public:
353         RandomInputHandler() = default;
354
355         bool isRandom() const
356         {
357                 return true;
358         }
359
360         virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
361         virtual void setKeypress(const KeyPress &keyCode)
362         {
363                 keydown.set(keyCode);
364         }
365         virtual void unsetKeypress(const KeyPress &keyCode)
366         {
367                 keydown.unset(keyCode);
368         }
369         virtual bool wasKeyDown(GameKeyType k) { return false; }
370         virtual bool wasKeyPressed(GameKeyType k) { return false; }
371         virtual bool wasKeyReleased(GameKeyType k) { return false; }
372         virtual bool cancelPressed() { return false; }
373         virtual v2s32 getMousePos() { return mousepos; }
374         virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
375
376         virtual s32 getMouseWheel() { return 0; }
377
378         virtual void step(float dtime);
379
380         s32 Rand(s32 min, s32 max);
381
382 private:
383         KeyList keydown;
384         v2s32 mousepos;
385         v2s32 mousespeed;
386 };