Translated using Weblate (Chinese (Simplified))
[oweals/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 <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         void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
148         void dontListenForKeys() { keysListenedFor.clear(); }
149
150         s32 getMouseWheel()
151         {
152                 s32 a = mouse_wheel;
153                 mouse_wheel = 0;
154                 return a;
155         }
156
157         void clearInput()
158         {
159                 keyIsDown.clear();
160                 keyWasDown.clear();
161
162                 leftclicked = false;
163                 rightclicked = false;
164                 leftreleased = false;
165                 rightreleased = false;
166
167                 left_active = false;
168                 middle_active = false;
169                 right_active = false;
170
171                 mouse_wheel = 0;
172         }
173
174         MyEventReceiver()
175         {
176 #ifdef HAVE_TOUCHSCREENGUI
177                 m_touchscreengui = NULL;
178 #endif
179         }
180
181         bool leftclicked = false;
182         bool rightclicked = false;
183         bool leftreleased = false;
184         bool rightreleased = false;
185
186         bool left_active = false;
187         bool middle_active = false;
188         bool right_active = false;
189
190         s32 mouse_wheel = 0;
191
192         JoystickController *joystick = nullptr;
193
194 #ifdef HAVE_TOUCHSCREENGUI
195         TouchScreenGUI *m_touchscreengui;
196 #endif
197
198 private:
199         // The current state of keys
200         KeyList keyIsDown;
201         // Whether a key has been pressed or not
202         KeyList keyWasDown;
203         // List of keys we listen for
204         // TODO perhaps the type of this is not really
205         // performant as KeyList is designed for few but
206         // often changing keys, and keysListenedFor is expected
207         // to change seldomly but contain lots of keys.
208         KeyList keysListenedFor;
209 };
210
211 class InputHandler
212 {
213 public:
214         InputHandler()
215         {
216                 keycache.handler = this;
217                 keycache.populate();
218         }
219
220         virtual ~InputHandler() = default;
221
222         virtual bool isKeyDown(GameKeyType k) = 0;
223         virtual bool wasKeyDown(GameKeyType k) = 0;
224         virtual bool cancelPressed() = 0;
225
226         virtual void listenForKey(const KeyPress &keyCode) {}
227         virtual void dontListenForKeys() {}
228
229         virtual v2s32 getMousePos() = 0;
230         virtual void setMousePos(s32 x, s32 y) = 0;
231
232         virtual bool getLeftState() = 0;
233         virtual bool getRightState() = 0;
234
235         virtual bool getLeftClicked() = 0;
236         virtual bool getRightClicked() = 0;
237         virtual void resetLeftClicked() = 0;
238         virtual void resetRightClicked() = 0;
239
240         virtual bool getLeftReleased() = 0;
241         virtual bool getRightReleased() = 0;
242         virtual void resetLeftReleased() = 0;
243         virtual void resetRightReleased() = 0;
244
245         virtual s32 getMouseWheel() = 0;
246
247         virtual void step(float dtime) {}
248
249         virtual void clear() {}
250
251         JoystickController joystick;
252         KeyCache keycache;
253 };
254 /*
255         Separated input handler
256 */
257
258 class RealInputHandler : public InputHandler
259 {
260 public:
261         RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
262         {
263                 m_receiver->joystick = &joystick;
264         }
265         virtual bool isKeyDown(GameKeyType k)
266         {
267                 return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
268         }
269         virtual bool wasKeyDown(GameKeyType k)
270         {
271                 return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
272         }
273         virtual bool cancelPressed()
274         {
275                 return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
276         }
277         virtual void listenForKey(const KeyPress &keyCode)
278         {
279                 m_receiver->listenForKey(keyCode);
280         }
281         virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
282         virtual v2s32 getMousePos()
283         {
284                 if (RenderingEngine::get_raw_device()->getCursorControl()) {
285                         return RenderingEngine::get_raw_device()
286                                         ->getCursorControl()
287                                         ->getPosition();
288                 }
289
290                 return m_mousepos;
291         }
292
293         virtual void setMousePos(s32 x, s32 y)
294         {
295                 if (RenderingEngine::get_raw_device()->getCursorControl()) {
296                         RenderingEngine::get_raw_device()
297                                         ->getCursorControl()
298                                         ->setPosition(x, y);
299                 } else {
300                         m_mousepos = v2s32(x, y);
301                 }
302         }
303
304         virtual bool getLeftState()
305         {
306                 return m_receiver->left_active || joystick.isKeyDown(KeyType::MOUSE_L);
307         }
308         virtual bool getRightState()
309         {
310                 return m_receiver->right_active || joystick.isKeyDown(KeyType::MOUSE_R);
311         }
312
313         virtual bool getLeftClicked()
314         {
315                 return m_receiver->leftclicked ||
316                        joystick.getWasKeyDown(KeyType::MOUSE_L);
317         }
318         virtual bool getRightClicked()
319         {
320                 return m_receiver->rightclicked ||
321                        joystick.getWasKeyDown(KeyType::MOUSE_R);
322         }
323
324         virtual void resetLeftClicked()
325         {
326                 m_receiver->leftclicked = false;
327                 joystick.clearWasKeyDown(KeyType::MOUSE_L);
328         }
329         virtual void resetRightClicked()
330         {
331                 m_receiver->rightclicked = false;
332                 joystick.clearWasKeyDown(KeyType::MOUSE_R);
333         }
334
335         virtual bool getLeftReleased()
336         {
337                 return m_receiver->leftreleased ||
338                        joystick.wasKeyReleased(KeyType::MOUSE_L);
339         }
340         virtual bool getRightReleased()
341         {
342                 return m_receiver->rightreleased ||
343                        joystick.wasKeyReleased(KeyType::MOUSE_R);
344         }
345
346         virtual void resetLeftReleased()
347         {
348                 m_receiver->leftreleased = false;
349                 joystick.clearWasKeyReleased(KeyType::MOUSE_L);
350         }
351         virtual void resetRightReleased()
352         {
353                 m_receiver->rightreleased = false;
354                 joystick.clearWasKeyReleased(KeyType::MOUSE_R);
355         }
356
357         virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
358
359         void clear()
360         {
361                 joystick.clear();
362                 m_receiver->clearInput();
363         }
364
365 private:
366         MyEventReceiver *m_receiver = nullptr;
367         v2s32 m_mousepos;
368 };
369
370 class RandomInputHandler : public InputHandler
371 {
372 public:
373         RandomInputHandler() = default;
374
375         virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
376         virtual bool wasKeyDown(GameKeyType k) { return false; }
377         virtual bool cancelPressed() { return false; }
378         virtual v2s32 getMousePos() { return mousepos; }
379         virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
380
381         virtual bool getLeftState() { return leftdown; }
382         virtual bool getRightState() { return rightdown; }
383
384         virtual bool getLeftClicked() { return leftclicked; }
385         virtual bool getRightClicked() { return rightclicked; }
386         virtual void resetLeftClicked() { leftclicked = false; }
387         virtual void resetRightClicked() { rightclicked = false; }
388
389         virtual bool getLeftReleased() { return leftreleased; }
390         virtual bool getRightReleased() { return rightreleased; }
391         virtual void resetLeftReleased() { leftreleased = false; }
392         virtual void resetRightReleased() { rightreleased = false; }
393
394         virtual s32 getMouseWheel() { return 0; }
395
396         virtual void step(float dtime);
397
398         s32 Rand(s32 min, s32 max);
399
400 private:
401         KeyList keydown;
402         v2s32 mousepos;
403         v2s32 mousespeed;
404         bool leftdown = false;
405         bool rightdown = false;
406         bool leftclicked = false;
407         bool rightclicked = false;
408         bool leftreleased = false;
409         bool rightreleased = false;
410 };