]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/inputhandler.h
Add spider
[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) { keysListenedFor.set(keyCode); }
157         void dontListenForKeys() { keysListenedFor.clear(); }
158
159         s32 getMouseWheel()
160         {
161                 s32 a = mouse_wheel;
162                 mouse_wheel = 0;
163                 return a;
164         }
165
166         void clearInput()
167         {
168                 keyIsDown.clear();
169                 keyWasDown.clear();
170                 keyWasPressed.clear();
171                 keyWasReleased.clear();
172
173                 mouse_wheel = 0;
174         }
175
176         void clearWasKeyPressed()
177         {
178                 keyWasPressed.clear();
179         }
180
181         void clearWasKeyReleased()
182         {
183                 keyWasReleased.clear();
184         }
185
186         MyEventReceiver()
187         {
188 #ifdef HAVE_TOUCHSCREENGUI
189                 m_touchscreengui = NULL;
190 #endif
191         }
192
193         s32 mouse_wheel = 0;
194
195         JoystickController *joystick = nullptr;
196
197 #ifdef HAVE_TOUCHSCREENGUI
198         TouchScreenGUI *m_touchscreengui;
199 #endif
200
201         // The current state of keys
202         KeyList keyIsDown;
203
204         // Like keyIsDown but only reset when that key is read
205         KeyList keyWasDown;
206
207         // Whether a key has just been pressed
208         KeyList keyWasPressed;
209
210         // Whether a key has just been released
211         KeyList keyWasReleased;
212
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;
219 };
220
221 class InputHandler
222 {
223 public:
224         InputHandler()
225         {
226                 keycache.handler = this;
227                 keycache.populate();
228         }
229
230         virtual ~InputHandler() = default;
231
232         virtual bool isRandom() const
233         {
234                 return false;
235         }
236
237         virtual bool isKeyDown(GameKeyType k) = 0;
238         virtual void setKeypress(const KeyPress &keyCode) = 0;
239         virtual void unsetKeypress(const KeyPress &keyCode) = 0;
240         virtual bool wasKeyDown(GameKeyType k) = 0;
241         virtual bool wasKeyPressed(GameKeyType k) = 0;
242         virtual bool wasKeyReleased(GameKeyType k) = 0;
243         virtual bool cancelPressed() = 0;
244
245         virtual float getMovementSpeed() = 0;
246         virtual float getMovementDirection() = 0;
247
248         virtual void clearWasKeyPressed() {}
249         virtual void clearWasKeyReleased() {}
250
251         virtual void listenForKey(const KeyPress &keyCode) {}
252         virtual void dontListenForKeys() {}
253
254         virtual v2s32 getMousePos() = 0;
255         virtual void setMousePos(s32 x, s32 y) = 0;
256
257         virtual s32 getMouseWheel() = 0;
258
259         virtual void step(float dtime) {}
260
261         virtual void clear() {}
262
263         JoystickController joystick;
264         KeyCache keycache;
265 };
266 /*
267         Separated input handler
268 */
269
270 class RealInputHandler : public InputHandler
271 {
272 public:
273         RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
274         {
275                 m_receiver->joystick = &joystick;
276         }
277         virtual bool isKeyDown(GameKeyType k)
278         {
279                 return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
280         }
281         virtual void setKeypress(const KeyPress &keyCode)
282         {
283                 m_receiver->keyIsDown.set(keyCode);
284                 m_receiver->keyWasDown.set(keyCode);
285         }
286         virtual void unsetKeypress(const KeyPress &keyCode)
287         {
288                 m_receiver->keyIsDown.unset(keyCode);
289         }
290         virtual bool wasKeyDown(GameKeyType k)
291         {
292                 return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
293         }
294         virtual bool wasKeyPressed(GameKeyType k)
295         {
296                 return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyPressed(k);
297         }
298         virtual bool wasKeyReleased(GameKeyType k)
299         {
300                 return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
301         }
302         virtual float getMovementSpeed()
303         {
304                 bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
305                         b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]),
306                         l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]),
307                         r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]);
308                 if (f || b || l || r)
309                 {
310                         // if contradictory keys pressed, stay still
311                         if (f && b && l && r)
312                                 return 0.0f;
313                         else if (f && b && !l && !r)
314                                 return 0.0f;
315                         else if (!f && !b && l && r)
316                                 return 0.0f;
317                         return 1.0f; // If there is a keyboard event, assume maximum speed
318                 }
319                 return joystick.getMovementSpeed();
320         }
321         virtual float getMovementDirection()
322         {
323                 float x = 0, z = 0;
324
325                 /* Check keyboard for input */
326                 if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]))
327                         z += 1;
328                 if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]))
329                         z -= 1;
330                 if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]))
331                         x += 1;
332                 if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]))
333                         x -= 1;
334
335                 if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
336                         return atan2(x, z);
337                 else
338                         return joystick.getMovementDirection();
339         }
340         virtual bool cancelPressed()
341         {
342                 return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
343         }
344         virtual void clearWasKeyPressed()
345         {
346                 m_receiver->clearWasKeyPressed();
347         }
348         virtual void clearWasKeyReleased()
349         {
350                 m_receiver->clearWasKeyReleased();
351         }
352         virtual void listenForKey(const KeyPress &keyCode)
353         {
354                 m_receiver->listenForKey(keyCode);
355         }
356         virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
357         virtual v2s32 getMousePos()
358         {
359                 if (RenderingEngine::get_raw_device()->getCursorControl()) {
360                         return RenderingEngine::get_raw_device()
361                                         ->getCursorControl()
362                                         ->getPosition();
363                 }
364
365                 return m_mousepos;
366         }
367
368         virtual void setMousePos(s32 x, s32 y)
369         {
370                 if (RenderingEngine::get_raw_device()->getCursorControl()) {
371                         RenderingEngine::get_raw_device()
372                                         ->getCursorControl()
373                                         ->setPosition(x, y);
374                 } else {
375                         m_mousepos = v2s32(x, y);
376                 }
377         }
378
379         virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
380
381         void clear()
382         {
383                 joystick.clear();
384                 m_receiver->clearInput();
385         }
386
387         private:
388         MyEventReceiver *m_receiver = nullptr;
389         v2s32 m_mousepos;
390 };
391
392 class RandomInputHandler : public InputHandler
393 {
394 public:
395         RandomInputHandler() = default;
396
397         bool isRandom() const
398         {
399                 return true;
400         }
401
402         virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
403         virtual void setKeypress(const KeyPress &keyCode)
404         {
405                 keydown.set(keyCode);
406         }
407         virtual void unsetKeypress(const KeyPress &keyCode)
408         {
409                 keydown.unset(keyCode);
410         }
411         virtual bool wasKeyDown(GameKeyType k) { return false; }
412         virtual bool wasKeyPressed(GameKeyType k) { return false; }
413         virtual bool wasKeyReleased(GameKeyType k) { return false; }
414         virtual bool cancelPressed() { return false; }
415         virtual float getMovementSpeed() { return movementSpeed; }
416         virtual float getMovementDirection() { return movementDirection; }
417         virtual v2s32 getMousePos() { return mousepos; }
418         virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
419
420         virtual s32 getMouseWheel() { return 0; }
421
422         virtual void step(float dtime);
423
424         s32 Rand(s32 min, s32 max);
425
426 private:
427         KeyList keydown;
428         v2s32 mousepos;
429         v2s32 mousespeed;
430         float movementSpeed;
431         float movementDirection;
432 };