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