Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
inputhandler.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irrlichttypes.h"
9#include <list>
10#include "keycode.h"
11
12class InputHandler;
13
14enum class PointerType {
15 Mouse,
16 Touch,
17};
18
19/****************************************************************************
20 Fast key cache for main game loop
21 ****************************************************************************/
22
23/* This is faster than using getKeySetting with the tradeoff that functions
24 * using it must make sure that it's initialised before using it and there is
25 * no error handling (for example bounds checking). This is really intended for
26 * use only in the main running loop of the client (the_game()) where the faster
27 * (up to 10x faster) key lookup is an asset. Other parts of the codebase
28 * (e.g. formspecs) should continue using getKeySetting().
29 */
31{
32
34 {
35 handler = NULL;
36 populate();
38 }
39
40 void populate();
41
42 // Keys that are not settings dependent
44
47};
48
49class KeyList : private std::list<KeyPress>
50{
51 typedef std::list<KeyPress> super;
52 typedef super::iterator iterator;
53 typedef super::const_iterator const_iterator;
54
55 virtual const_iterator find(const KeyPress &key) const
56 {
57 const_iterator f(begin());
58 const_iterator e(end());
59
60 while (f != e) {
61 if (*f == key)
62 return f;
63
64 ++f;
65 }
66
67 return e;
68 }
69
70 virtual iterator find(const KeyPress &key)
71 {
72 iterator f(begin());
73 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
85public:
86 void clear() { super::clear(); }
87
88 void set(const KeyPress &key)
89 {
90 if (find(key) == end())
91 push_back(key);
92 }
93
94 void unset(const KeyPress &key)
95 {
96 iterator p(find(key));
97
98 if (p != end())
99 erase(p);
100 }
101
102 void toggle(const KeyPress &key)
103 {
104 iterator p(this->find(key));
105
106 if (p != end())
107 erase(p);
108 else
109 push_back(key);
110 }
111
112 void append(const KeyList &other)
113 {
114 for (const KeyPress &key : other) {
115 set(key);
116 }
117 }
118
119 bool operator[](const KeyPress &key) const { return find(key) != end(); }
120};
121
122class MyEventReceiver : public IEventReceiver
123{
124public:
125 // This is the one method that we have to implement
126 virtual bool OnEvent(const SEvent &event);
127
128 bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
129
130 // Checks whether a key was down and resets the state
131 bool WasKeyDown(const KeyPress &keyCode)
132 {
133 bool b = keyWasDown[keyCode];
134 if (b)
135 keyWasDown.unset(keyCode);
136 return b;
137 }
138
139 // Checks whether a key was just pressed. State will be cleared
140 // in the subsequent iteration of Game::processPlayerInteraction
141 bool WasKeyPressed(const KeyPress &keycode) const { return keyWasPressed[keycode]; }
142
143 // Checks whether a key was just released. State will be cleared
144 // in the subsequent iteration of Game::processPlayerInteraction
145 bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; }
146
147 void listenForKey(const KeyPress &keyCode)
148 {
149 keysListenedFor.set(keyCode);
150 }
152 {
154 }
155
157 {
158 s32 a = mouse_wheel;
159 mouse_wheel = 0;
160 return a;
161 }
162
164 {
169
170 mouse_wheel = 0;
171 }
172
178
180 {
182 }
183
185 {
187 }
188
190
192
193private:
194 s32 mouse_wheel = 0;
195
196 // The current state of keys
198
199 // Like keyIsDown but only reset when that key is read
201
202 // Whether a key has just been pressed
204
205 // Whether a key has just been released
207
208 // List of keys we listen for
209 // TODO perhaps the type of this is not really
210 // performant as KeyList is designed for few but
211 // often changing keys, and keysListenedFor is expected
212 // to change seldomly but contain lots of keys.
214
215 // Intentionally not reset by clearInput/releaseAllKeys.
216 bool fullscreen_is_down = false;
217
219};
220
222{
223public:
225 {
226 keycache.handler = this;
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 float getJoystickSpeed() = 0;
244 virtual float getJoystickDirection() = 0;
245
246 virtual void clearWasKeyPressed() {}
247 virtual void clearWasKeyReleased() {}
248
249 virtual void listenForKey(const KeyPress &keyCode) {}
250 virtual void dontListenForKeys() {}
251
252 virtual v2s32 getMousePos() = 0;
253 virtual void setMousePos(s32 x, s32 y) = 0;
254
255 virtual s32 getMouseWheel() = 0;
256
257 virtual void step(float dtime) {}
258
259 virtual void clear() {}
260 virtual void releaseAllKeys() {}
261
264};
265
266/*
267 Separated input handler implementations
268*/
269
270class RealInputHandler final : public InputHandler
271{
272public:
274 {
276 }
277
279 {
280 m_receiver->joystick = nullptr;
281 }
282
283 virtual bool isKeyDown(GameKeyType k)
284 {
286 }
287 virtual bool wasKeyDown(GameKeyType k)
288 {
290 }
292 {
294 }
296 {
298 }
299
300 virtual float getJoystickSpeed();
301
302 virtual float getJoystickDirection();
303
304 virtual bool cancelPressed()
305 {
306 return wasKeyDown(KeyType::ESC);
307 }
308
309 virtual void clearWasKeyPressed()
310 {
312 }
313 virtual void clearWasKeyReleased()
314 {
316 }
317
318 virtual void listenForKey(const KeyPress &keyCode)
319 {
320 m_receiver->listenForKey(keyCode);
321 }
322 virtual void dontListenForKeys()
323 {
325 }
326
327 virtual v2s32 getMousePos();
328 virtual void setMousePos(s32 x, s32 y);
329
330 virtual s32 getMouseWheel()
331 {
332 return m_receiver->getMouseWheel();
333 }
334
335 void clear()
336 {
337 joystick.clear();
339 }
340
346
347private:
350};
351
353{
354public:
356
357 bool isRandom() const
358 {
359 return true;
360 }
361
362 virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
363 virtual bool wasKeyDown(GameKeyType k) { return false; }
364 virtual bool wasKeyPressed(GameKeyType k) { return false; }
365 virtual bool wasKeyReleased(GameKeyType k) { return false; }
366 virtual bool cancelPressed() { return false; }
367 virtual float getJoystickSpeed() { return joystickSpeed; }
368 virtual float getJoystickDirection() { return joystickDirection; }
369 virtual v2s32 getMousePos() { return mousepos; }
370 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
371
372 virtual s32 getMouseWheel() { return 0; }
373
374 virtual void step(float dtime);
375
376 s32 Rand(s32 min, s32 max);
377
378private:
384};
Definition inputhandler.h:222
virtual void dontListenForKeys()
Definition inputhandler.h:250
virtual s32 getMouseWheel()=0
virtual v2s32 getMousePos()=0
virtual bool isKeyDown(GameKeyType k)=0
virtual ~InputHandler()=default
virtual void clearWasKeyReleased()
Definition inputhandler.h:247
virtual float getJoystickSpeed()=0
KeyCache keycache
Definition inputhandler.h:263
virtual bool isRandom() const
Definition inputhandler.h:232
virtual void listenForKey(const KeyPress &keyCode)
Definition inputhandler.h:249
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:259
virtual void step(float dtime)
Definition inputhandler.h:257
JoystickController joystick
Definition inputhandler.h:262
virtual bool cancelPressed()=0
virtual void releaseAllKeys()
Definition inputhandler.h:260
virtual bool wasKeyReleased(GameKeyType k)=0
virtual float getJoystickDirection()=0
virtual void clearWasKeyPressed()
Definition inputhandler.h:246
Definition joystick_controller.h:87
bool isKeyDown(GameKeyType b)
Definition joystick_controller.h:128
bool wasKeyReleased(GameKeyType b)
Definition joystick_controller.h:110
void clear()
Definition joystick_controller.cpp:283
bool wasKeyDown(GameKeyType b)
Definition joystick_controller.h:103
bool wasKeyPressed(GameKeyType b)
Definition joystick_controller.h:119
void releaseAllKeys()
Definition joystick_controller.h:97
Definition inputhandler.h:50
virtual iterator find(const KeyPress &key)
Definition inputhandler.h:70
virtual const_iterator find(const KeyPress &key) const
Definition inputhandler.h:55
void append(const KeyList &other)
Definition inputhandler.h:112
super::iterator iterator
Definition inputhandler.h:52
void toggle(const KeyPress &key)
Definition inputhandler.h:102
std::list< KeyPress > super
Definition inputhandler.h:51
super::const_iterator const_iterator
Definition inputhandler.h:53
void set(const KeyPress &key)
Definition inputhandler.h:88
bool operator[](const KeyPress &key) const
Definition inputhandler.h:119
void unset(const KeyPress &key)
Definition inputhandler.h:94
void clear()
Definition inputhandler.h:86
Definition keycode.h:24
T
Definition keys.h:13
@ ESC
Definition keys.h:26
@ INTERNAL_ENUM_COUNT
Definition keys.h:100
Definition inputhandler.h:123
KeyList keyWasDown
Definition inputhandler.h:200
KeyList keyIsDown
Definition inputhandler.h:197
void releaseAllKeys()
Definition inputhandler.h:173
s32 mouse_wheel
Definition inputhandler.h:194
bool WasKeyDown(const KeyPress &keyCode)
Definition inputhandler.h:131
bool WasKeyReleased(const KeyPress &keycode) const
Definition inputhandler.h:145
virtual bool OnEvent(const SEvent &event)
Definition inputhandler.cpp:87
void listenForKey(const KeyPress &keyCode)
Definition inputhandler.h:147
void dontListenForKeys()
Definition inputhandler.h:151
KeyList keysListenedFor
Definition inputhandler.h:213
bool IsKeyDown(const KeyPress &keyCode) const
Definition inputhandler.h:128
PointerType getLastPointerType()
Definition inputhandler.h:191
KeyList keyWasReleased
Definition inputhandler.h:206
bool fullscreen_is_down
Definition inputhandler.h:216
KeyList keyWasPressed
Definition inputhandler.h:203
bool WasKeyPressed(const KeyPress &keycode) const
Definition inputhandler.h:141
void clearWasKeyReleased()
Definition inputhandler.h:184
s32 getMouseWheel()
Definition inputhandler.h:156
void clearInput()
Definition inputhandler.h:163
PointerType last_pointer_type
Definition inputhandler.h:218
JoystickController * joystick
Definition inputhandler.h:189
void clearWasKeyPressed()
Definition inputhandler.h:179
Definition inputhandler.h:353
v2s32 mousespeed
Definition inputhandler.h:381
virtual v2s32 getMousePos()
Definition inputhandler.h:369
bool isRandom() const
Definition inputhandler.h:357
virtual float getJoystickDirection()
Definition inputhandler.h:368
virtual bool wasKeyReleased(GameKeyType k)
Definition inputhandler.h:365
v2s32 mousepos
Definition inputhandler.h:380
s32 Rand(s32 min, s32 max)
Definition inputhandler.cpp:254
virtual s32 getMouseWheel()
Definition inputhandler.h:372
virtual float getJoystickSpeed()
Definition inputhandler.h:367
virtual void step(float dtime)
Definition inputhandler.cpp:265
virtual bool wasKeyPressed(GameKeyType k)
Definition inputhandler.h:364
float joystickSpeed
Definition inputhandler.h:382
KeyList keydown
Definition inputhandler.h:379
float joystickDirection
Definition inputhandler.h:383
virtual bool isKeyDown(GameKeyType k)
Definition inputhandler.h:362
virtual void setMousePos(s32 x, s32 y)
Definition inputhandler.h:370
virtual bool cancelPressed()
Definition inputhandler.h:366
RandomInputHandler()=default
virtual bool wasKeyDown(GameKeyType k)
Definition inputhandler.h:363
Definition inputhandler.h:271
virtual bool cancelPressed()
Definition inputhandler.h:304
MyEventReceiver * m_receiver
Definition inputhandler.h:348
virtual v2s32 getMousePos()
Definition inputhandler.cpp:231
virtual bool wasKeyDown(GameKeyType k)
Definition inputhandler.h:287
virtual ~RealInputHandler()
Definition inputhandler.h:278
v2s32 m_mousepos
Definition inputhandler.h:349
virtual bool isKeyDown(GameKeyType k)
Definition inputhandler.h:283
virtual float getJoystickDirection()
Definition inputhandler.cpp:222
virtual float getJoystickSpeed()
Definition inputhandler.cpp:215
virtual void clearWasKeyPressed()
Definition inputhandler.h:309
void releaseAllKeys()
Definition inputhandler.h:341
virtual bool wasKeyReleased(GameKeyType k)
Definition inputhandler.h:295
virtual void clearWasKeyReleased()
Definition inputhandler.h:313
virtual void listenForKey(const KeyPress &keyCode)
Definition inputhandler.h:318
virtual void setMousePos(s32 x, s32 y)
Definition inputhandler.cpp:241
virtual s32 getMouseWheel()
Definition inputhandler.h:330
RealInputHandler(MyEventReceiver *receiver)
Definition inputhandler.h:273
virtual void dontListenForKeys()
Definition inputhandler.h:322
virtual bool wasKeyPressed(GameKeyType k)
Definition inputhandler.h:291
void clear()
Definition inputhandler.h:335
PointerType
Definition inputhandler.h:14
core::vector2d< s32 > v2s32
Definition irr_v2d.h:13
Definition inputhandler.h:31
KeyPress key[KeyType::INTERNAL_ENUM_COUNT]
Definition inputhandler.h:45
void populate()
Definition inputhandler.cpp:20
InputHandler * handler
Definition inputhandler.h:46
KeyCache()
Definition inputhandler.h:33
void populate_nonchanging()
Definition inputhandler.cpp:15
static std::string p(std::string path)
Definition test_filesys.cpp:53