Luanti 5.18.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"
9#include <array>
10#include <bitset>
11#include <map>
12#include <set>
13#include <unordered_map>
14#include <utility>
15#include "keycode.h"
16#include "settings.h"
17#include "util/string.h"
18
19class InputHandler;
20
21enum class PointerType {
24};
25
26class MyEventReceiver : public IEventReceiver
27{
28public:
29 // This is the one method that we have to implement
30 virtual bool OnEvent(const SEvent &event);
31
32 // Gets the analog value corresponding to a key
33 float GetAxisValue(GameKeyType key) const { return axisValues[key]; }
34
35 // Checks whether a key is held down
36 bool IsKeyDown(GameKeyType key) const
37 {
38 return GetAxisValue(key) > 0;
39 }
40
41 // Checks whether a key was down and resets the state
42 bool WasKeyDown(GameKeyType key);
43
44 // Checks whether a key was just pressed. State will be cleared
45 // in the subsequent iteration of Game::processPlayerInteraction
46 bool WasKeyPressed(GameKeyType key) const { return keyWasPressed[key]; }
47
48 // Checks whether a key was just released. State will be cleared
49 // in the subsequent iteration of Game::processPlayerInteraction
50 bool WasKeyReleased(GameKeyType key) const { return keyWasReleased[key]; }
51
52 void reloadKeybindings();
53
55 {
56 s32 a = mouse_wheel;
57 mouse_wheel = 0;
58 return a;
59 }
60
61 // Returns the mouse movement since the last call and resets it.
62 // Remains valid even when the cursor is hidden and locked to the
63 // center of the window.
65 {
67 mouse_movement = v2s32(0, 0);
68 return a;
69 }
70
72 {
73 physicalKeyDown.clear();
74 axisValues.fill(0);
75 keyWasDown.reset();
76 keyWasPressed.reset();
77 keyWasReleased.reset();
78
79 mouse_wheel = 0;
80 mouse_movement = v2s32(0, 0);
81 }
82
84 {
85 physicalKeyDown.clear();
86 for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++)
88 axisValues.fill(0);
89 }
90
92 {
93 keyWasPressed.reset();
94 }
95
97 {
98 keyWasReleased.reset();
99 }
100
102
103private:
104 void listenForKey(KeyPress keyCode, GameKeyType action)
105 {
106 if (keyCode)
107 keysListenedFor[keyCode] = action;
108 }
109
110 bool setKeyDown(KeyPress keyCode, float value);
111 void setKeyDown(GameKeyType action, std::pair<float, bool> new_state);
112 std::pair<float, bool> checkKeyDown(GameKeyType action) const;
113
114 struct Keybinding {
115 std::vector<KeyPress> keys;
116 struct {
117 float keyboard_mouse = 1.0f;
118 float joystick = 1.0f;
120
121 Keybinding() = default;
122 Keybinding(const std::vector<KeyPress> &in_keys): keys(in_keys) {}
123
124 inline float getScale(KeyPress::InputType input_type) const
125 {
126 switch (input_type) {
129 return scale.keyboard_mouse;
133 return scale.joystick;
134 default:
135 return 1.0f;
136 }
137 }
138 };
139
142 f64 last_binary_update; // The last time the binary state ("is this pressed?") is updated
143 };
144
145 /* This is faster than using getKeySetting with the tradeoff that functions
146 * using it must make sure that it's initialised before using it and there is
147 * no error handling (for example bounds checking). This is useful here as the
148 * faster (up to 10x faster) key lookup is an asset.
149 */
150 std::array<Keybinding, KeyType::INTERNAL_ENUM_COUNT> keybindings;
151
152 // Repetition interval for joystick input
154
155 s32 mouse_wheel = 0;
156
157 // The current state of physical keys.
158 std::map<KeyPress, PhysicalKeyState> physicalKeyDown;
159
160 // The current state of keys
161 std::array<float, GameKeyType::INTERNAL_ENUM_COUNT> axisValues;
162
163 // Like axisValues but only reset when that key is read
164 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasDown;
165
166 // Whether a key has just been pressed
167 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasPressed;
168
169 // Whether a key has just been released
170 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasReleased;
171
172 // List of keys we listen for
173 std::unordered_map<KeyPress, GameKeyType> keysListenedFor;
174
175 // Intentionally not reset by clearInput/releaseAllKeys.
176 bool fullscreen_is_down = false;
177
178 bool close_world_down = false;
179 bool esc_down = false;
180
182
183 // Relative mouse movement accumulated from mouse events
185};
186
188{
189public:
190 InputHandler();
191
192 virtual ~InputHandler() = default;
193
194 virtual bool isRandom() const
195 {
196 return false;
197 }
198
199 static inline s16 analogToInt(float value)
200 {
201 return value > 0 ? std::min(1.0f, value) * INT16_MAX : 0;
202 }
203
204 static inline float intToAnalog(s16 value)
205 {
206 return value > 0 ? value / (f32)INT16_MAX : 0.0f;
207 }
208
209 virtual float getAxisValue(GameKeyType k) = 0;
210 virtual bool isKeyDown(GameKeyType k)
211 {
212 return getAxisValue(k) > 0;
213 }
214 virtual bool wasKeyDown(GameKeyType k) = 0;
215 virtual bool wasKeyPressed(GameKeyType k) = 0;
216 virtual bool wasKeyReleased(GameKeyType k) = 0;
217 virtual bool cancelPressed() = 0;
218
219 virtual void clearWasKeyPressed() {}
220 virtual void clearWasKeyReleased() {}
221
222 virtual void reloadKeybindings() {}
223
224 virtual v2s32 getMousePos() = 0;
225 virtual void setMousePos(s32 x, s32 y) = 0;
226
227 virtual v2s32 getMouseMovement() = 0;
228
229 virtual s32 getMouseWheel() = 0;
230
231 virtual void step(float dtime) {}
232
233 virtual void clear() {}
234 virtual void releaseAllKeys() {}
235
236 static void settingChangedCallback(const std::string &name, void *data)
237 {
238 static_cast<InputHandler *>(data)->reloadKeybindings();
239 }
240};
241
242/*
243 Separated input handler implementations
244*/
245
246class RealInputHandler final : public InputHandler
247{
248public:
250 {
251 m_receiver->reloadKeybindings();
252 }
253
254 virtual float getAxisValue(GameKeyType k)
255 {
256 return m_receiver->GetAxisValue(k);
257 }
258 virtual bool wasKeyDown(GameKeyType k)
259 {
260 return m_receiver->WasKeyDown(k);
261 }
263 {
264 return m_receiver->WasKeyPressed(k);
265 }
267 {
268 return m_receiver->WasKeyReleased(k);
269 }
270
271 virtual bool cancelPressed()
272 {
273 return wasKeyDown(KeyType::ESC);
274 }
275
276 virtual void clearWasKeyPressed()
277 {
278 m_receiver->clearWasKeyPressed();
279 }
280 virtual void clearWasKeyReleased()
281 {
282 m_receiver->clearWasKeyReleased();
283 }
284
285 virtual void reloadKeybindings()
286 {
287 m_receiver->reloadKeybindings();
288 }
289
290 virtual v2s32 getMousePos();
291 virtual void setMousePos(s32 x, s32 y);
292
294 {
295 return m_receiver->getMouseMovement();
296 }
297
298 virtual s32 getMouseWheel()
299 {
300 return m_receiver->getMouseWheel();
301 }
302
303 void clear()
304 {
305 m_receiver->clearInput();
306 }
307
309 {
310 m_receiver->releaseAllKeys();
311 }
312
313private:
316};
317
319{
320public:
322
323 bool isRandom() const
324 {
325 return true;
326 }
327
328 virtual float getAxisValue(GameKeyType k) { return keydown[k]; }
329 virtual bool wasKeyDown(GameKeyType k) { return false; }
330 virtual bool wasKeyPressed(GameKeyType k) { return false; }
331 virtual bool wasKeyReleased(GameKeyType k) { return false; }
332 virtual bool cancelPressed() { return false; }
333 virtual v2s32 getMousePos() { return mousepos; }
334 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
335
337 {
339 mousemovement = v2s32(0, 0);
340 return a;
341 }
342
343 virtual s32 getMouseWheel() { return 0; }
344
345 virtual void step(float dtime);
346
347 s32 Rand(s32 min, s32 max);
348
349private:
350 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keydown;
354};
Definition inputhandler.h:188
virtual s32 getMouseWheel()=0
virtual v2s32 getMousePos()=0
virtual v2s32 getMouseMovement()=0
virtual ~InputHandler()=default
virtual void clearWasKeyReleased()
Definition inputhandler.h:220
virtual bool isRandom() const
Definition inputhandler.h:194
virtual bool wasKeyPressed(GameKeyType k)=0
InputHandler()
Definition inputhandler.cpp:22
virtual void setMousePos(s32 x, s32 y)=0
virtual bool isKeyDown(GameKeyType k)
Definition inputhandler.h:210
virtual bool wasKeyDown(GameKeyType k)=0
virtual void clear()
Definition inputhandler.h:233
virtual void reloadKeybindings()
Definition inputhandler.h:222
virtual float getAxisValue(GameKeyType k)=0
virtual void step(float dtime)
Definition inputhandler.h:231
static s16 analogToInt(float value)
Definition inputhandler.h:199
static float intToAnalog(s16 value)
Definition inputhandler.h:204
static void settingChangedCallback(const std::string &name, void *data)
Definition inputhandler.h:236
virtual bool cancelPressed()=0
virtual void releaseAllKeys()
Definition inputhandler.h:234
virtual bool wasKeyReleased(GameKeyType k)=0
virtual void clearWasKeyPressed()
Definition inputhandler.h:219
Definition keycode.h:19
InputType
The type of the input value.
Definition keycode.h:27
@ GAMEPAD_AXIS_PLUS
Gamepad axis in the positive direction.
Definition keycode.h:32
@ MOUSE_BUTTON
Mouse button input.
Definition keycode.h:29
@ GAMEPAD_AXIS_MINUS
Gamepad axis in the negative direction.
Definition keycode.h:33
@ GAMEPAD_BUTTON
Gamepad button.
Definition keycode.h:31
@ KEYBOARD
Keyboard input (scancodes).
Definition keycode.h:28
@ ESC
Definition keys.h:24
@ INTERNAL_ENUM_COUNT
Definition keys.h:104
Definition inputhandler.h:27
std::array< Keybinding, KeyType::INTERNAL_ENUM_COUNT > keybindings
Definition inputhandler.h:150
void reloadKeybindings()
Definition inputhandler.cpp:31
std::pair< float, bool > checkKeyDown(GameKeyType action) const
Definition inputhandler.cpp:189
void listenForKey(KeyPress keyCode, GameKeyType action)
Definition inputhandler.h:104
bool WasKeyPressed(GameKeyType key) const
Definition inputhandler.h:46
void releaseAllKeys()
Definition inputhandler.h:83
s32 mouse_wheel
Definition inputhandler.h:155
std::unordered_map< KeyPress, GameKeyType > keysListenedFor
Definition inputhandler.h:173
v2s32 mouse_movement
Definition inputhandler.h:184
bool WasKeyReleased(GameKeyType key) const
Definition inputhandler.h:50
virtual bool OnEvent(const SEvent &event)
Definition inputhandler.cpp:203
std::array< float, GameKeyType::INTERNAL_ENUM_COUNT > axisValues
Definition inputhandler.h:161
float repeat_joystick_button_time
Definition inputhandler.h:153
std::map< KeyPress, PhysicalKeyState > physicalKeyDown
Definition inputhandler.h:158
PointerType getLastPointerType()
Definition inputhandler.h:101
bool fullscreen_is_down
Definition inputhandler.h:176
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasReleased
Definition inputhandler.h:170
bool IsKeyDown(GameKeyType key) const
Definition inputhandler.h:36
bool esc_down
Definition inputhandler.h:179
float GetAxisValue(GameKeyType key) const
Definition inputhandler.h:33
void clearWasKeyReleased()
Definition inputhandler.h:96
s32 getMouseWheel()
Definition inputhandler.h:54
void clearInput()
Definition inputhandler.h:71
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasDown
Definition inputhandler.h:164
PointerType last_pointer_type
Definition inputhandler.h:181
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasPressed
Definition inputhandler.h:167
v2s32 getMouseMovement()
Definition inputhandler.h:64
bool setKeyDown(KeyPress keyCode, float value)
Definition inputhandler.cpp:149
bool close_world_down
Definition inputhandler.h:178
void clearWasKeyPressed()
Definition inputhandler.h:91
bool WasKeyDown(GameKeyType key)
Definition inputhandler.cpp:124
v2s32 mousespeed
Definition inputhandler.h:352
virtual v2s32 getMousePos()
Definition inputhandler.h:333
bool isRandom() const
Definition inputhandler.h:323
virtual bool wasKeyReleased(GameKeyType k)
Definition inputhandler.h:331
v2s32 mousepos
Definition inputhandler.h:351
s32 Rand(s32 min, s32 max)
Definition inputhandler.cpp:323
virtual s32 getMouseWheel()
Definition inputhandler.h:343
virtual void step(float dtime)
Definition inputhandler.cpp:334
virtual bool wasKeyPressed(GameKeyType k)
Definition inputhandler.h:330
virtual v2s32 getMouseMovement()
Definition inputhandler.h:336
virtual void setMousePos(s32 x, s32 y)
Definition inputhandler.h:334
virtual bool cancelPressed()
Definition inputhandler.h:332
v2s32 mousemovement
Definition inputhandler.h:353
RandomInputHandler()=default
virtual float getAxisValue(GameKeyType k)
Definition inputhandler.h:328
virtual bool wasKeyDown(GameKeyType k)
Definition inputhandler.h:329
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keydown
Definition inputhandler.h:350
virtual bool cancelPressed()
Definition inputhandler.h:271
MyEventReceiver * m_receiver
Definition inputhandler.h:314
virtual v2s32 getMousePos()
Definition inputhandler.cpp:300
virtual bool wasKeyDown(GameKeyType k)
Definition inputhandler.h:258
v2s32 m_mousepos
Definition inputhandler.h:315
virtual void clearWasKeyPressed()
Definition inputhandler.h:276
virtual v2s32 getMouseMovement()
Definition inputhandler.h:293
void releaseAllKeys()
Definition inputhandler.h:308
virtual bool wasKeyReleased(GameKeyType k)
Definition inputhandler.h:266
virtual void clearWasKeyReleased()
Definition inputhandler.h:280
virtual void setMousePos(s32 x, s32 y)
Definition inputhandler.cpp:310
virtual s32 getMouseWheel()
Definition inputhandler.h:298
RealInputHandler(MyEventReceiver *receiver)
Definition inputhandler.h:249
virtual bool wasKeyPressed(GameKeyType k)
Definition inputhandler.h:262
virtual void reloadKeybindings()
Definition inputhandler.h:285
void clear()
Definition inputhandler.h:303
virtual float getAxisValue(GameKeyType k)
Definition inputhandler.h:254
PointerType
Definition inputhandler.h:21
@ Touch
Definition inputhandler.h:23
@ Mouse
Definition inputhandler.h:22
core::vector2d< s32 > v2s32
Definition irr_v2d.h:13
KeyType::T GameKeyType
Definition keys.h:109
struct MyEventReceiver::Keybinding::@224352171233173203231251207143015257330236066165 scale
Keybinding(const std::vector< KeyPress > &in_keys)
Definition inputhandler.h:122
float getScale(KeyPress::InputType input_type) const
Definition inputhandler.h:124
std::vector< KeyPress > keys
Definition inputhandler.h:115
float keyboard_mouse
Definition inputhandler.h:117
float joystick
Definition inputhandler.h:118
Definition inputhandler.h:140
f64 last_binary_update
Definition inputhandler.h:142
float analog_value
Definition inputhandler.h:141
constexpr v3f x
Definition test_irr_matrix4.cpp:18
constexpr v3f y
Definition test_irr_matrix4.cpp:19