]> git.lizzy.rs Git - minetest.git/blob - src/client/inputhandler.h
885f34e05dfd9f402e7022290488a452c8c3133e
[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 #pragma once
21
22 #include "irrlichttypes_extrabloated.h"
23 #include "joystick_controller.h"
24 #include "keycode.h"
25 #include "renderingengine.h"
26 #include <unordered_set>
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 MyEventReceiver : public IEventReceiver
65 {
66 public:
67         // This is the one method that we have to implement
68         virtual bool OnEvent(const SEvent &event);
69
70         bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown.count(keyCode); }
71
72         // Checks whether a key was down and resets the state
73         bool WasKeyDown(const KeyPress &keyCode)
74         {
75                 bool b = keyWasDown.count(keyCode);
76                 if (b)
77                         keyWasDown.erase(keyCode);
78                 return b;
79         }
80
81         // Checks whether a key was just pressed. State will be cleared
82         // in the subsequent iteration of Game::processPlayerInteraction
83         bool WasKeyPressed(const KeyPress &keycode) const { return keyWasPressed.count(keycode); }
84
85         // Checks whether a key was just released. State will be cleared
86         // in the subsequent iteration of Game::processPlayerInteraction
87         bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased.count(keycode); }
88
89         void listenForKey(const KeyPress &keyCode) { keysListenedFor.insert(keyCode); }
90         void dontListenForKeys() { keysListenedFor.clear(); }
91
92         s32 getMouseWheel()
93         {
94                 s32 a = mouse_wheel;
95                 mouse_wheel = 0;
96                 return a;
97         }
98
99         void clearInput()
100         {
101                 keyIsDown.clear();
102                 keyWasDown.clear();
103                 keyWasPressed.clear();
104                 keyWasReleased.clear();
105
106                 mouse_wheel = 0;
107         }
108
109         void clearWasKeyPressed()
110         {
111                 keyWasPressed.clear();
112         }
113
114         void clearWasKeyReleased()
115         {
116                 keyWasReleased.clear();
117         }
118
119         MyEventReceiver()
120         {
121 #ifdef HAVE_TOUCHSCREENGUI
122                 m_touchscreengui = NULL;
123 #endif
124         }
125
126         s32 mouse_wheel = 0;
127
128         JoystickController *joystick = nullptr;
129
130 #ifdef HAVE_TOUCHSCREENGUI
131         TouchScreenGUI *m_touchscreengui;
132 #endif
133
134 private:
135         //! The current state of keys
136         std::unordered_set<KeyPress> keyIsDown;
137
138         //! Whether a key was down
139         std::unordered_set<KeyPress> keyWasDown;
140
141         //! Whether a key has just been pressed
142         std::unordered_set<KeyPress> keyWasPressed;
143
144         //! Whether a key has just been released
145         std::unordered_set<KeyPress> keyWasReleased;
146
147         //! List of keys we listen for
148         std::unordered_set<KeyPress> keysListenedFor;
149 };
150
151 class InputHandler
152 {
153 public:
154         InputHandler()
155         {
156                 keycache.handler = this;
157                 keycache.populate();
158         }
159
160         virtual ~InputHandler() = default;
161
162         virtual bool isRandom() const
163         {
164                 return false;
165         }
166
167         virtual bool isKeyDown(GameKeyType k) = 0;
168         virtual bool wasKeyDown(GameKeyType k) = 0;
169         virtual bool wasKeyPressed(GameKeyType k) = 0;
170         virtual bool wasKeyReleased(GameKeyType k) = 0;
171         virtual bool cancelPressed() = 0;
172
173         virtual void clearWasKeyPressed() {}
174         virtual void clearWasKeyReleased() {}
175
176         virtual void listenForKey(const KeyPress &keyCode) {}
177         virtual void dontListenForKeys() {}
178
179         virtual v2s32 getMousePos() = 0;
180         virtual void setMousePos(s32 x, s32 y) = 0;
181
182         virtual s32 getMouseWheel() = 0;
183
184         virtual void step(float dtime) {}
185
186         virtual void clear() {}
187
188         JoystickController joystick;
189         KeyCache keycache;
190 };
191 /*
192         Separated input handler
193 */
194
195 class RealInputHandler : public InputHandler
196 {
197 public:
198         RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
199         {
200                 m_receiver->joystick = &joystick;
201         }
202         virtual bool isKeyDown(GameKeyType k)
203         {
204                 return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
205         }
206         virtual bool wasKeyDown(GameKeyType k)
207         {
208                 return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
209         }
210         virtual bool wasKeyPressed(GameKeyType k)
211         {
212                 return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyReleased(k);
213         }
214         virtual bool wasKeyReleased(GameKeyType k)
215         {
216                 return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
217         }
218         virtual bool cancelPressed()
219         {
220                 return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
221         }
222         virtual void clearWasKeyPressed()
223         {
224                 m_receiver->clearWasKeyPressed();
225         }
226         virtual void clearWasKeyReleased()
227         {
228                 m_receiver->clearWasKeyReleased();
229         }
230         virtual void listenForKey(const KeyPress &keyCode)
231         {
232                 m_receiver->listenForKey(keyCode);
233         }
234         virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
235         virtual v2s32 getMousePos()
236         {
237                 if (RenderingEngine::get_raw_device()->getCursorControl()) {
238                         return RenderingEngine::get_raw_device()
239                                         ->getCursorControl()
240                                         ->getPosition();
241                 }
242
243                 return m_mousepos;
244         }
245
246         virtual void setMousePos(s32 x, s32 y)
247         {
248                 if (RenderingEngine::get_raw_device()->getCursorControl()) {
249                         RenderingEngine::get_raw_device()
250                                         ->getCursorControl()
251                                         ->setPosition(x, y);
252                 } else {
253                         m_mousepos = v2s32(x, y);
254                 }
255         }
256
257         virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
258
259         void clear()
260         {
261                 joystick.clear();
262                 m_receiver->clearInput();
263         }
264
265 private:
266         MyEventReceiver *m_receiver = nullptr;
267         v2s32 m_mousepos;
268 };
269
270 class RandomInputHandler : public InputHandler
271 {
272 public:
273         RandomInputHandler() = default;
274
275         bool isRandom() const
276         {
277                 return true;
278         }
279
280         virtual bool isKeyDown(GameKeyType k) { return keydown.count(keycache.key[k]); }
281         virtual bool wasKeyDown(GameKeyType k) { return false; }
282         virtual bool wasKeyPressed(GameKeyType k) { return false; }
283         virtual bool wasKeyReleased(GameKeyType k) { return false; }
284         virtual bool cancelPressed() { return false; }
285         virtual v2s32 getMousePos() { return mousepos; }
286         virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
287
288         virtual s32 getMouseWheel() { return 0; }
289
290         virtual void step(float dtime);
291
292         s32 Rand(s32 min, s32 max);
293
294 private:
295         std::unordered_set<KeyPress> keydown;
296         v2s32 mousepos;
297         v2s32 mousespeed;
298 };