Luanti 5.15.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 <array>
11#include <set>
12#include <unordered_map>
13#include "keycode.h"
14#include "settings.h"
15#include "util/string.h"
16
17class InputHandler;
18
19enum class PointerType {
20 Mouse,
21 Touch,
22};
23
24class MyEventReceiver : public IEventReceiver
25{
26public:
27 // This is the one method that we have to implement
28 virtual bool OnEvent(const SEvent &event);
29
30 bool IsKeyDown(GameKeyType key) const { return keyIsDown[key]; }
31
32 // Checks whether a key was down and resets the state
34 {
35 bool b = keyWasDown[key];
36 if (b)
37 keyWasDown.reset(key);
38 return b;
39 }
40
41 // Checks whether a key was just pressed. State will be cleared
42 // in the subsequent iteration of Game::processPlayerInteraction
43 bool WasKeyPressed(GameKeyType key) const { return keyWasPressed[key]; }
44
45 // Checks whether a key was just released. State will be cleared
46 // in the subsequent iteration of Game::processPlayerInteraction
47 bool WasKeyReleased(GameKeyType key) const { return keyWasReleased[key]; }
48
49 void reloadKeybindings();
50
52 {
53 s32 a = mouse_wheel;
54 mouse_wheel = 0;
55 return a;
56 }
57
59 {
60 physicalKeyDown.clear();
61 keyIsDown.reset();
62 keyWasDown.reset();
63 keyWasPressed.reset();
64 keyWasReleased.reset();
65
66 mouse_wheel = 0;
67 }
68
70 {
71 physicalKeyDown.clear();
73 keyIsDown.reset();
74 }
75
77 {
78 keyWasPressed.reset();
79 }
80
82 {
83 keyWasReleased.reset();
84 }
85
87
89
90private:
91 void listenForKey(KeyPress keyCode, GameKeyType action)
92 {
93 if (keyCode)
94 keysListenedFor[keyCode] = action;
95 }
96
97 bool setKeyDown(KeyPress keyCode, bool is_down);
98 void setKeyDown(GameKeyType action, bool is_down);
99
100 /* This is faster than using getKeySetting with the tradeoff that functions
101 * using it must make sure that it's initialised before using it and there is
102 * no error handling (for example bounds checking). This is useful here as the
103 * faster (up to 10x faster) key lookup is an asset.
104 */
105 std::array<KeyPress, KeyType::INTERNAL_ENUM_COUNT> keybindings;
106
107 s32 mouse_wheel = 0;
108
109 // The current state of physical keys.
110 std::set<KeyPress> physicalKeyDown;
111
112 // The current state of keys
113 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyIsDown;
114
115 // Like keyIsDown but only reset when that key is read
116 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasDown;
117
118 // Whether a key has just been pressed
119 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasPressed;
120
121 // Whether a key has just been released
122 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasReleased;
123
124 // List of keys we listen for
125 std::unordered_map<KeyPress, GameKeyType> keysListenedFor;
126
127 // Intentionally not reset by clearInput/releaseAllKeys.
128 bool fullscreen_is_down = false;
129
130 bool close_world_down = false;
131 bool esc_down = false;
132
134};
135
137{
138public:
140 {
141 for (const auto &name: Settings::getLayer(SL_DEFAULTS)->getNames())
142 if (str_starts_with(name, "keymap_"))
144 }
145
146 virtual ~InputHandler() = default;
147
148 virtual bool isRandom() const
149 {
150 return false;
151 }
152
153 virtual bool isKeyDown(GameKeyType k) = 0;
154 virtual bool wasKeyDown(GameKeyType k) = 0;
155 virtual bool wasKeyPressed(GameKeyType k) = 0;
156 virtual bool wasKeyReleased(GameKeyType k) = 0;
157 virtual bool cancelPressed() = 0;
158
159 virtual float getJoystickSpeed() = 0;
160 virtual float getJoystickDirection() = 0;
161
162 virtual void clearWasKeyPressed() {}
163 virtual void clearWasKeyReleased() {}
164
165 virtual void reloadKeybindings() {}
166
167 virtual v2s32 getMousePos() = 0;
168 virtual void setMousePos(s32 x, s32 y) = 0;
169
170 virtual s32 getMouseWheel() = 0;
171
172 virtual void step(float dtime) {}
173
174 virtual void clear() {}
175 virtual void releaseAllKeys() {}
176
177 static void settingChangedCallback(const std::string &name, void *data)
178 {
179 static_cast<InputHandler *>(data)->reloadKeybindings();
180 }
181
183};
184
185/*
186 Separated input handler implementations
187*/
188
189class RealInputHandler final : public InputHandler
190{
191public:
197
199 {
200 m_receiver->joystick = nullptr;
201 }
202
203 virtual bool isKeyDown(GameKeyType k)
204 {
205 return m_receiver->IsKeyDown(k) || joystick.isKeyDown(k);
206 }
207 virtual bool wasKeyDown(GameKeyType k)
208 {
209 return m_receiver->WasKeyDown(k) || joystick.wasKeyDown(k);
210 }
212 {
214 }
216 {
218 }
219
220 virtual float getJoystickSpeed();
221
222 virtual float getJoystickDirection();
223
224 virtual bool cancelPressed()
225 {
226 return wasKeyDown(KeyType::ESC);
227 }
228
229 virtual void clearWasKeyPressed()
230 {
232 }
233 virtual void clearWasKeyReleased()
234 {
236 }
237
238 virtual void reloadKeybindings()
239 {
241 }
242
243 virtual v2s32 getMousePos();
244 virtual void setMousePos(s32 x, s32 y);
245
246 virtual s32 getMouseWheel()
247 {
248 return m_receiver->getMouseWheel();
249 }
250
251 void clear()
252 {
253 joystick.clear();
255 }
256
262
263private:
266};
267
269{
270public:
272
273 bool isRandom() const
274 {
275 return true;
276 }
277
278 virtual bool isKeyDown(GameKeyType k) { return keydown[k]; }
279 virtual bool wasKeyDown(GameKeyType k) { return false; }
280 virtual bool wasKeyPressed(GameKeyType k) { return false; }
281 virtual bool wasKeyReleased(GameKeyType k) { return false; }
282 virtual bool cancelPressed() { return false; }
283 virtual float getJoystickSpeed() { return joystickSpeed; }
284 virtual float getJoystickDirection() { return joystickDirection; }
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
294private:
295 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keydown;
300};
Definition inputhandler.h:137
virtual s32 getMouseWheel()=0
virtual v2s32 getMousePos()=0
virtual bool isKeyDown(GameKeyType k)=0
virtual ~InputHandler()=default
virtual void clearWasKeyReleased()
Definition inputhandler.h:163
virtual float getJoystickSpeed()=0
virtual bool isRandom() const
Definition inputhandler.h:148
virtual bool wasKeyPressed(GameKeyType k)=0
InputHandler()
Definition inputhandler.h:139
virtual void setMousePos(s32 x, s32 y)=0
virtual bool wasKeyDown(GameKeyType k)=0
virtual void clear()
Definition inputhandler.h:174
virtual void reloadKeybindings()
Definition inputhandler.h:165
virtual void step(float dtime)
Definition inputhandler.h:172
static void settingChangedCallback(const std::string &name, void *data)
Definition inputhandler.h:177
JoystickController joystick
Definition inputhandler.h:182
virtual bool cancelPressed()=0
virtual void releaseAllKeys()
Definition inputhandler.h:175
virtual bool wasKeyReleased(GameKeyType k)=0
virtual float getJoystickDirection()=0
virtual void clearWasKeyPressed()
Definition inputhandler.h:162
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:281
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 keycode.h:17
T
Definition keys.h:11
@ ESC
Definition keys.h:24
Definition inputhandler.h:25
void reloadKeybindings()
Definition inputhandler.cpp:15
std::set< KeyPress > physicalKeyDown
Definition inputhandler.h:110
void listenForKey(KeyPress keyCode, GameKeyType action)
Definition inputhandler.h:91
bool WasKeyPressed(GameKeyType key) const
Definition inputhandler.h:43
void releaseAllKeys()
Definition inputhandler.h:69
s32 mouse_wheel
Definition inputhandler.h:107
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyIsDown
Definition inputhandler.h:113
std::unordered_map< KeyPress, GameKeyType > keysListenedFor
Definition inputhandler.h:125
bool WasKeyReleased(GameKeyType key) const
Definition inputhandler.h:47
virtual bool OnEvent(const SEvent &event)
Definition inputhandler.cpp:112
bool setKeyDown(KeyPress keyCode, bool is_down)
Definition inputhandler.cpp:83
PointerType getLastPointerType()
Definition inputhandler.h:88
bool fullscreen_is_down
Definition inputhandler.h:128
std::array< KeyPress, KeyType::INTERNAL_ENUM_COUNT > keybindings
Definition inputhandler.h:105
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasReleased
Definition inputhandler.h:122
bool IsKeyDown(GameKeyType key) const
Definition inputhandler.h:30
bool esc_down
Definition inputhandler.h:131
void clearWasKeyReleased()
Definition inputhandler.h:81
s32 getMouseWheel()
Definition inputhandler.h:51
void clearInput()
Definition inputhandler.h:58
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasDown
Definition inputhandler.h:116
PointerType last_pointer_type
Definition inputhandler.h:133
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasPressed
Definition inputhandler.h:119
JoystickController * joystick
Definition inputhandler.h:86
bool close_world_down
Definition inputhandler.h:130
void clearWasKeyPressed()
Definition inputhandler.h:76
bool WasKeyDown(GameKeyType key)
Definition inputhandler.h:33
Definition inputhandler.h:269
v2s32 mousespeed
Definition inputhandler.h:297
virtual v2s32 getMousePos()
Definition inputhandler.h:285
bool isRandom() const
Definition inputhandler.h:273
virtual float getJoystickDirection()
Definition inputhandler.h:284
virtual bool wasKeyReleased(GameKeyType k)
Definition inputhandler.h:281
v2s32 mousepos
Definition inputhandler.h:296
s32 Rand(s32 min, s32 max)
Definition inputhandler.cpp:266
virtual s32 getMouseWheel()
Definition inputhandler.h:288
virtual float getJoystickSpeed()
Definition inputhandler.h:283
virtual void step(float dtime)
Definition inputhandler.cpp:277
virtual bool wasKeyPressed(GameKeyType k)
Definition inputhandler.h:280
float joystickSpeed
Definition inputhandler.h:298
float joystickDirection
Definition inputhandler.h:299
virtual bool isKeyDown(GameKeyType k)
Definition inputhandler.h:278
virtual void setMousePos(s32 x, s32 y)
Definition inputhandler.h:286
virtual bool cancelPressed()
Definition inputhandler.h:282
RandomInputHandler()=default
virtual bool wasKeyDown(GameKeyType k)
Definition inputhandler.h:279
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keydown
Definition inputhandler.h:295
Definition inputhandler.h:190
virtual bool cancelPressed()
Definition inputhandler.h:224
MyEventReceiver * m_receiver
Definition inputhandler.h:264
virtual v2s32 getMousePos()
Definition inputhandler.cpp:243
virtual bool wasKeyDown(GameKeyType k)
Definition inputhandler.h:207
virtual ~RealInputHandler()
Definition inputhandler.h:198
v2s32 m_mousepos
Definition inputhandler.h:265
virtual bool isKeyDown(GameKeyType k)
Definition inputhandler.h:203
virtual float getJoystickDirection()
Definition inputhandler.cpp:234
virtual float getJoystickSpeed()
Definition inputhandler.cpp:227
virtual void clearWasKeyPressed()
Definition inputhandler.h:229
void releaseAllKeys()
Definition inputhandler.h:257
virtual bool wasKeyReleased(GameKeyType k)
Definition inputhandler.h:215
virtual void clearWasKeyReleased()
Definition inputhandler.h:233
virtual void setMousePos(s32 x, s32 y)
Definition inputhandler.cpp:253
virtual s32 getMouseWheel()
Definition inputhandler.h:246
RealInputHandler(MyEventReceiver *receiver)
Definition inputhandler.h:192
virtual bool wasKeyPressed(GameKeyType k)
Definition inputhandler.h:211
virtual void reloadKeybindings()
Definition inputhandler.h:238
void clear()
Definition inputhandler.h:251
int getLayer() const
Definition settings.h:233
void registerChangedCallback(const std::string &name, SettingsChangedCallback cbf, void *userdata=NULL)
Definition settings.cpp:1034
PointerType
Definition inputhandler.h:19
core::vector2d< s32 > v2s32
Definition irr_v2d.h:13
Settings * g_settings
Definition settings.cpp:22
@ SL_DEFAULTS
Definition settings.h:52
bool str_starts_with(std::basic_string_view< T > str, std::basic_string_view< T > prefix, bool case_insensitive=false)
Check whether str begins with the string prefix.
Definition string.h:210
constexpr v3f x
Definition test_irr_matrix4.cpp:18
constexpr v3f y
Definition test_irr_matrix4.cpp:19