Minetest  5.4.0
inputhandler.h
Go to the documentation of this file.
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 
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 
49  {
50  handler = NULL;
51  populate();
53  }
54 
55  void populate();
56 
57  // Keys that are not settings dependent
58  void populate_nonchanging();
59 
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); }
157 
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();
171 
172  mouse_wheel = 0;
173  }
174 
176  {
178  }
179 
181  {
183  }
184 
186  {
187 #ifdef HAVE_TOUCHSCREENGUI
188  m_touchscreengui = NULL;
189 #endif
190  }
191 
192  s32 mouse_wheel = 0;
193 
195 
196 #ifdef HAVE_TOUCHSCREENGUI
197  TouchScreenGUI *m_touchscreengui;
198 #endif
199 
200 private:
201  // The current state of keys
203 
204  // Like keyIsDown but only reset when that key is read
206 
207  // Whether a key has just been pressed
209 
210  // Whether a key has just been released
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.
219 };
220 
222 {
223 public:
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 bool wasKeyDown(GameKeyType k) = 0;
239  virtual bool wasKeyPressed(GameKeyType k) = 0;
240  virtual bool wasKeyReleased(GameKeyType k) = 0;
241  virtual bool cancelPressed() = 0;
242 
243  virtual void clearWasKeyPressed() {}
244  virtual void clearWasKeyReleased() {}
245 
246  virtual void listenForKey(const KeyPress &keyCode) {}
247  virtual void dontListenForKeys() {}
248 
249  virtual v2s32 getMousePos() = 0;
250  virtual void setMousePos(s32 x, s32 y) = 0;
251 
252  virtual s32 getMouseWheel() = 0;
253 
254  virtual void step(float dtime) {}
255 
256  virtual void clear() {}
257 
260 };
261 /*
262  Separated input handler
263 */
264 
266 {
267 public:
269  {
271  }
272  virtual bool isKeyDown(GameKeyType k)
273  {
275  }
276  virtual bool wasKeyDown(GameKeyType k)
277  {
279  }
280  virtual bool wasKeyPressed(GameKeyType k)
281  {
283  }
284  virtual bool wasKeyReleased(GameKeyType k)
285  {
287  }
288  virtual bool cancelPressed()
289  {
291  }
292  virtual void clearWasKeyPressed()
293  {
295  }
296  virtual void clearWasKeyReleased()
297  {
299  }
300  virtual void listenForKey(const KeyPress &keyCode)
301  {
302  m_receiver->listenForKey(keyCode);
303  }
305  virtual v2s32 getMousePos()
306  {
307  if (RenderingEngine::get_raw_device()->getCursorControl()) {
309  ->getCursorControl()
310  ->getPosition();
311  }
312 
313  return m_mousepos;
314  }
315 
316  virtual void setMousePos(s32 x, s32 y)
317  {
318  if (RenderingEngine::get_raw_device()->getCursorControl()) {
320  ->getCursorControl()
321  ->setPosition(x, y);
322  } else {
323  m_mousepos = v2s32(x, y);
324  }
325  }
326 
327  virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
328 
329  void clear()
330  {
331  joystick.clear();
333  }
334 
335 private:
338 };
339 
341 {
342 public:
343  RandomInputHandler() = default;
344 
345  bool isRandom() const
346  {
347  return true;
348  }
349 
350  virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
351  virtual bool wasKeyDown(GameKeyType k) { return false; }
352  virtual bool wasKeyPressed(GameKeyType k) { return false; }
353  virtual bool wasKeyReleased(GameKeyType k) { return false; }
354  virtual bool cancelPressed() { return false; }
355  virtual v2s32 getMousePos() { return mousepos; }
356  virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
357 
358  virtual s32 getMouseWheel() { return 0; }
359 
360  virtual void step(float dtime);
361 
362  s32 Rand(s32 min, s32 max);
363 
364 private:
368 };
Definition: inputhandler.h:222
virtual void dontListenForKeys()
Definition: inputhandler.h:247
virtual s32 getMouseWheel()=0
virtual v2s32 getMousePos()=0
virtual bool isKeyDown(GameKeyType k)=0
virtual ~InputHandler()=default
virtual void clearWasKeyReleased()
Definition: inputhandler.h:244
KeyCache keycache
Definition: inputhandler.h:259
virtual bool isRandom() const
Definition: inputhandler.h:232
virtual void listenForKey(const KeyPress &keyCode)
Definition: inputhandler.h:246
virtual bool wasKeyPressed(GameKeyType k)=0
InputHandler()
Definition: inputhandler.h:224
virtual void setMousePos(s32 x, s32 y)=0
virtual bool wasKeyDown(GameKeyType k)=0
virtual void clear()
Definition: inputhandler.h:256
virtual void step(float dtime)
Definition: inputhandler.h:254
JoystickController joystick
Definition: inputhandler.h:258
virtual bool cancelPressed()=0
virtual bool wasKeyReleased(GameKeyType k)=0
virtual void clearWasKeyPressed()
Definition: inputhandler.h:243
Definition: joystick_controller.h:102
bool isKeyDown(GameKeyType b)
Definition: joystick_controller.h:137
bool wasKeyReleased(GameKeyType b)
Definition: joystick_controller.h:119
void clear()
Definition: joystick_controller.cpp:245
bool wasKeyDown(GameKeyType b)
Definition: joystick_controller.h:112
bool wasKeyPressed(GameKeyType b)
Definition: joystick_controller.h:128
Definition: inputhandler.h:65
virtual iterator find(const KeyPress &key)
Definition: inputhandler.h:85
virtual const_iterator find(const KeyPress &key) const
Definition: inputhandler.h:70
super::iterator iterator
Definition: inputhandler.h:67
void toggle(const KeyPress &key)
Definition: inputhandler.h:117
std::list< KeyPress > super
Definition: inputhandler.h:66
super::const_iterator const_iterator
Definition: inputhandler.h:68
void set(const KeyPress &key)
Definition: inputhandler.h:103
bool operator[](const KeyPress &key) const
Definition: inputhandler.h:127
void unset(const KeyPress &key)
Definition: inputhandler.h:109
void clear()
Definition: inputhandler.h:101
Definition: keycode.h:31
T
Definition: keys.h:28
@ ESC
Definition: keys.h:41
@ INTERNAL_ENUM_COUNT
Definition: keys.h:114
Definition: inputhandler.h:131
KeyList keyWasDown
Definition: inputhandler.h:205
KeyList keyIsDown
Definition: inputhandler.h:202
s32 mouse_wheel
Definition: inputhandler.h:192
bool WasKeyDown(const KeyPress &keyCode)
Definition: inputhandler.h:139
bool WasKeyReleased(const KeyPress &keycode) const
Definition: inputhandler.h:153
virtual bool OnEvent(const SEvent &event)
Definition: inputhandler.cpp:98
void listenForKey(const KeyPress &keyCode)
Definition: inputhandler.h:155
void dontListenForKeys()
Definition: inputhandler.h:156
KeyList keysListenedFor
Definition: inputhandler.h:218
bool IsKeyDown(const KeyPress &keyCode) const
Definition: inputhandler.h:136
KeyList keyWasReleased
Definition: inputhandler.h:211
KeyList keyWasPressed
Definition: inputhandler.h:208
bool WasKeyPressed(const KeyPress &keycode) const
Definition: inputhandler.h:149
void clearWasKeyReleased()
Definition: inputhandler.h:180
s32 getMouseWheel()
Definition: inputhandler.h:158
void clearInput()
Definition: inputhandler.h:165
MyEventReceiver()
Definition: inputhandler.h:185
JoystickController * joystick
Definition: inputhandler.h:194
void clearWasKeyPressed()
Definition: inputhandler.h:175
Definition: inputhandler.h:341
v2s32 mousespeed
Definition: inputhandler.h:367
virtual v2s32 getMousePos()
Definition: inputhandler.h:355
bool isRandom() const
Definition: inputhandler.h:345
virtual bool wasKeyReleased(GameKeyType k)
Definition: inputhandler.h:353
v2s32 mousepos
Definition: inputhandler.h:366
s32 Rand(s32 min, s32 max)
Definition: inputhandler.cpp:207
virtual s32 getMouseWheel()
Definition: inputhandler.h:358
virtual void step(float dtime)
Definition: inputhandler.cpp:218
virtual bool wasKeyPressed(GameKeyType k)
Definition: inputhandler.h:352
KeyList keydown
Definition: inputhandler.h:365
virtual bool isKeyDown(GameKeyType k)
Definition: inputhandler.h:350
virtual void setMousePos(s32 x, s32 y)
Definition: inputhandler.h:356
virtual bool cancelPressed()
Definition: inputhandler.h:354
RandomInputHandler()=default
virtual bool wasKeyDown(GameKeyType k)
Definition: inputhandler.h:351
Definition: inputhandler.h:266
virtual bool cancelPressed()
Definition: inputhandler.h:288
MyEventReceiver * m_receiver
Definition: inputhandler.h:336
virtual bool wasKeyDown(GameKeyType k)
Definition: inputhandler.h:276
virtual v2s32 getMousePos()
Definition: inputhandler.h:305
v2s32 m_mousepos
Definition: inputhandler.h:337
virtual bool isKeyDown(GameKeyType k)
Definition: inputhandler.h:272
virtual void setMousePos(s32 x, s32 y)
Definition: inputhandler.h:316
virtual void clearWasKeyPressed()
Definition: inputhandler.h:292
virtual bool wasKeyReleased(GameKeyType k)
Definition: inputhandler.h:284
virtual void clearWasKeyReleased()
Definition: inputhandler.h:296
virtual void listenForKey(const KeyPress &keyCode)
Definition: inputhandler.h:300
virtual s32 getMouseWheel()
Definition: inputhandler.h:327
RealInputHandler(MyEventReceiver *receiver)
Definition: inputhandler.h:268
virtual void dontListenForKeys()
Definition: inputhandler.h:304
virtual bool wasKeyPressed(GameKeyType k)
Definition: inputhandler.h:280
void clear()
Definition: inputhandler.h:329
static irr::IrrlichtDevice * get_raw_device()
Definition: renderingengine.h:86
Definition: touchscreengui.h:166
core::vector2d< s32 > v2s32
Definition: irr_v2d.h:28
const KeyPress CancelKey("KEY_CANCEL")
Definition: inputhandler.h:46
KeyPress key[KeyType::INTERNAL_ENUM_COUNT]
Definition: inputhandler.h:60
void populate()
Definition: inputhandler.cpp:31
InputHandler * handler
Definition: inputhandler.h:61
KeyCache()
Definition: inputhandler.h:48
void populate_nonchanging()
Definition: inputhandler.cpp:26
std::string p(std::string path)
Definition: test_filepath.cpp:59