]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/inputhandler.h
Merge branch 'master' of https://github.com/minetest/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 class TouchScreenGUI;
34
35 /****************************************************************************
36  Fast key cache for main game loop
37  ****************************************************************************/
38
39 /* This is faster than using getKeySetting with the tradeoff that functions
40  * using it must make sure that it's initialised before using it and there is
41  * no error handling (for example bounds checking). This is really intended for
42  * use only in the main running loop of the client (the_game()) where the faster
43  * (up to 10x faster) key lookup is an asset. Other parts of the codebase
44  * (e.g. formspecs) should continue using getKeySetting().
45  */
46 struct KeyCache
47 {
48
49         KeyCache()
50         {
51                 handler = NULL;
52                 populate();
53                 populate_nonchanging();
54         }
55
56         void populate();
57
58         // Keys that are not settings dependent
59         void populate_nonchanging();
60
61         KeyPress key[KeyType::INTERNAL_ENUM_COUNT];
62         InputHandler *handler;
63 };
64
65 class KeyList : private std::list<KeyPress>
66 {
67         typedef std::list<KeyPress> super;
68         typedef super::iterator iterator;
69         typedef super::const_iterator const_iterator;
70
71         virtual const_iterator find(const KeyPress &key) const
72         {
73                 const_iterator f(begin());
74                 const_iterator e(end());
75
76                 while (f != e) {
77                         if (*f == key)
78                                 return f;
79
80                         ++f;
81                 }
82
83                 return e;
84         }
85
86         virtual iterator find(const KeyPress &key)
87         {
88                 iterator f(begin());
89                 iterator e(end());
90
91                 while (f != e) {
92                         if (*f == key)
93                                 return f;
94
95                         ++f;
96                 }
97
98                 return e;
99         }
100
101 public:
102         void clear() { super::clear(); }
103
104         void set(const KeyPress &key)
105         {
106                 if (find(key) == end())
107                         push_back(key);
108         }
109
110         void unset(const KeyPress &key)
111         {
112                 iterator p(find(key));
113
114                 if (p != end())
115                         erase(p);
116         }
117
118         void toggle(const KeyPress &key)
119         {
120                 iterator p(this->find(key));
121
122                 if (p != end())
123                         erase(p);
124                 else
125                         push_back(key);
126         }
127
128         bool operator[](const KeyPress &key) const { return find(key) != end(); }
129 };
130
131 class MyEventReceiver : public IEventReceiver
132 {
133 public:
134         // This is the one method that we have to implement
135         virtual bool OnEvent(const SEvent &event);
136
137         bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
138
139         // Checks whether a key was down and resets the state
140         bool WasKeyDown(const KeyPress &keyCode)
141         {
142                 bool b = keyWasDown[keyCode];
143                 if (b)
144                         keyWasDown.unset(keyCode);
145                 return b;
146         }
147
148         // Checks whether a key was just pressed. State will be cleared
149         // in the subsequent iteration of Game::processPlayerInteraction
150         bool WasKeyPressed(const KeyPress &keycode) const { return keyWasPressed[keycode]; }
151
152         // Checks whether a key was just released. State will be cleared
153         // in the subsequent iteration of Game::processPlayerInteraction
154         bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; }
155
156         void listenForKey(const KeyPress &keyCode)
157         {
158                 keysListenedFor.set(keyCode);
159         }
160         void dontListenForKeys()
161         {
162                 keysListenedFor.clear();
163         }
164
165         s32 getMouseWheel()
166         {
167                 s32 a = mouse_wheel;
168                 mouse_wheel = 0;
169                 return a;
170         }
171
172         void clearInput()
173         {
174                 keyIsDown.clear();
175                 keyWasDown.clear();
176                 keyWasPressed.clear();
177                 keyWasReleased.clear();
178
179                 mouse_wheel = 0;
180         }
181
182         void clearWasKeyPressed()
183         {
184                 keyWasPressed.clear();
185         }
186
187         void clearWasKeyReleased()
188         {
189                 keyWasReleased.clear();
190         }
191
192         MyEventReceiver()
193         {
194 #ifdef HAVE_TOUCHSCREENGUI
195                 m_touchscreengui = NULL;
196 #endif
197         }
198
199         JoystickController *joystick = nullptr;
200
201 #ifdef HAVE_TOUCHSCREENGUI
202         TouchScreenGUI *m_touchscreengui;
203 #endif
204
205         s32 mouse_wheel = 0;
206
207         // The current state of keys
208         KeyList keyIsDown;
209
210         // Like keyIsDown but only reset when that key is read
211         KeyList keyWasDown;
212
213         // Whether a key has just been pressed
214         KeyList keyWasPressed;
215
216         // Whether a key has just been released
217         KeyList keyWasReleased;
218
219         // List of keys we listen for
220         // TODO perhaps the type of this is not really
221         // performant as KeyList is designed for few but
222         // often changing keys, and keysListenedFor is expected
223         // to change seldomly but contain lots of keys.
224         KeyList keysListenedFor;
225 };
226
227 class InputHandler
228 {
229 public:
230         InputHandler()
231         {
232                 keycache.handler = this;
233                 keycache.populate();
234         }
235
236         virtual ~InputHandler() = default;
237
238         virtual bool isRandom() const
239         {
240                 return false;
241         }
242
243         virtual bool isKeyDown(GameKeyType k) = 0;
244         virtual void setKeypress(const KeyPress &keyCode) = 0;
245         virtual void unsetKeypress(const KeyPress &keyCode) = 0;
246         virtual bool wasKeyDown(GameKeyType k) = 0;
247         virtual bool wasKeyPressed(GameKeyType k) = 0;
248         virtual bool wasKeyReleased(GameKeyType k) = 0;
249         virtual bool cancelPressed() = 0;
250
251         virtual float getMovementSpeed() = 0;
252         virtual float getMovementDirection() = 0;
253
254         virtual void clearWasKeyPressed() {}
255         virtual void clearWasKeyReleased() {}
256
257         virtual void listenForKey(const KeyPress &keyCode) {}
258         virtual void dontListenForKeys() {}
259
260         virtual v2s32 getMousePos() = 0;
261         virtual void setMousePos(s32 x, s32 y) = 0;
262
263         virtual s32 getMouseWheel() = 0;
264
265         virtual void step(float dtime) {}
266
267         virtual void clear() {}
268
269         JoystickController joystick;
270         KeyCache keycache;
271 };
272 /*
273         Separated input handler
274 */
275
276 class RealInputHandler : public InputHandler
277 {
278 public:
279         RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
280         {
281                 m_receiver->joystick = &joystick;
282         }
283
284         virtual ~RealInputHandler()
285         {
286                 m_receiver->joystick = nullptr;
287         }
288
289         virtual bool isKeyDown(GameKeyType k)
290         {
291                 return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
292         }
293         virtual void setKeypress(const KeyPress &keyCode)
294         {
295                 m_receiver->keyIsDown.set(keyCode);
296                 m_receiver->keyWasDown.set(keyCode);
297         }
298         virtual void unsetKeypress(const KeyPress &keyCode)
299         {
300                 m_receiver->keyIsDown.unset(keyCode);
301         }
302         virtual bool wasKeyDown(GameKeyType k)
303         {
304                 return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
305         }
306         virtual bool wasKeyPressed(GameKeyType k)
307         {
308                 return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyPressed(k);
309         }
310         virtual bool wasKeyReleased(GameKeyType k)
311         {
312                 return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
313         }
314
315         virtual float getMovementSpeed()
316         {
317                 bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
318                         b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]),
319                         l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]),
320                         r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]);
321                 if (f || b || l || r)
322                 {
323                         // if contradictory keys pressed, stay still
324                         if (f && b && l && r)
325                                 return 0.0f;
326                         else if (f && b && !l && !r)
327                                 return 0.0f;
328                         else if (!f && !b && l && r)
329                                 return 0.0f;
330                         return 1.0f; // If there is a keyboard event, assume maximum speed
331                 }
332                 return joystick.getMovementSpeed();
333         }
334
335         virtual float getMovementDirection()
336         {
337                 float x = 0, z = 0;
338
339                 /* Check keyboard for input */
340                 if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]))
341                         z += 1;
342                 if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]))
343                         z -= 1;
344                 if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]))
345                         x += 1;
346                 if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]))
347                         x -= 1;
348
349                 if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
350                         return atan2(x, z);
351                 else
352                         return joystick.getMovementDirection();
353         }
354
355         virtual bool cancelPressed()
356         {
357                 return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
358         }
359
360         virtual void clearWasKeyPressed()
361         {
362                 m_receiver->clearWasKeyPressed();
363         }
364         virtual void clearWasKeyReleased()
365         {
366                 m_receiver->clearWasKeyReleased();
367         }
368
369         virtual void listenForKey(const KeyPress &keyCode)
370         {
371                 m_receiver->listenForKey(keyCode);
372         }
373         virtual void dontListenForKeys()
374         {
375                 m_receiver->dontListenForKeys();
376         }
377
378         virtual v2s32 getMousePos()
379         {
380                 auto control = RenderingEngine::get_raw_device()->getCursorControl();
381                 if (control) {
382                         return control->getPosition();
383                 }
384
385                 return m_mousepos;
386         }
387
388         virtual void setMousePos(s32 x, s32 y)
389         {
390                 auto control = RenderingEngine::get_raw_device()->getCursorControl();
391                 if (control) {
392                         control->setPosition(x, y);
393                 } else {
394                         m_mousepos = v2s32(x, y);
395                 }
396         }
397
398         virtual s32 getMouseWheel()
399         {
400                 return m_receiver->getMouseWheel();
401         }
402
403         void clear()
404         {
405                 joystick.clear();
406                 m_receiver->clearInput();
407         }
408
409 private:
410         MyEventReceiver *m_receiver = nullptr;
411         v2s32 m_mousepos;
412 };
413
414 class RandomInputHandler : public InputHandler
415 {
416 public:
417         RandomInputHandler() = default;
418
419         bool isRandom() const
420         {
421                 return true;
422         }
423
424         virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
425         virtual void setKeypress(const KeyPress &keyCode)
426         {
427                 keydown.set(keyCode);
428         }
429         virtual void unsetKeypress(const KeyPress &keyCode)
430         {
431                 keydown.unset(keyCode);
432         }
433         virtual bool wasKeyDown(GameKeyType k) { return false; }
434         virtual bool wasKeyPressed(GameKeyType k) { return false; }
435         virtual bool wasKeyReleased(GameKeyType k) { return false; }
436         virtual bool cancelPressed() { return false; }
437         virtual float getMovementSpeed() { return movementSpeed; }
438         virtual float getMovementDirection() { return movementDirection; }
439         virtual v2s32 getMousePos() { return mousepos; }
440         virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
441
442         virtual s32 getMouseWheel() { return 0; }
443
444         virtual void step(float dtime);
445
446         s32 Rand(s32 min, s32 max);
447
448 private:
449         KeyList keydown;
450         v2s32 mousepos;
451         v2s32 mousespeed;
452         float movementSpeed;
453         float movementDirection;
454 };