Luanti 5.19.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
54 // Returns whole mouse wheel revolutions since the last call and resets it.
55 // Incomplete cycles are kept to not break high resolution mouse wheels.
57 {
58 s32 r = static_cast<s32>(mouse_wheel);
59 mouse_wheel -= r;
60 return r;
61 }
62
63 // Returns the mouse movement since the last call and resets it.
64 // Remains valid even when the cursor is hidden and locked to the
65 // center of the window.
67 {
69 mouse_movement = v2s32(0, 0);
70 return a;
71 }
72
74 {
75 physicalKeyDown.clear();
76 axisValues.fill(0);
77 keyWasDown.reset();
78 keyWasPressed.reset();
79 keyWasReleased.reset();
80
81 mouse_wheel = 0.f;
82 mouse_movement = v2s32(0, 0);
83 }
84
86 {
87 physicalKeyDown.clear();
88 for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++)
90 axisValues.fill(0);
91 }
92
94 {
95 keyWasPressed.reset();
96 }
97
99 {
100 keyWasReleased.reset();
101 }
102
104
105private:
106 void listenForKey(KeyPress keyCode, GameKeyType action)
107 {
108 if (keyCode)
109 keysListenedFor[keyCode] = action;
110 }
111
112 bool setKeyDown(KeyPress keyCode, float value);
113 void setKeyDown(GameKeyType action, std::pair<float, bool> new_state);
114 std::pair<float, bool> checkKeyDown(GameKeyType action) const;
115
116 struct Keybinding {
117 std::vector<KeyPress> keys;
118 struct {
119 float keyboard_mouse = 1.0f;
120 float joystick = 1.0f;
122
123 Keybinding() = default;
124 Keybinding(const std::vector<KeyPress> &in_keys): keys(in_keys) {}
125
126 inline float getScale(KeyPress::InputType input_type) const
127 {
128 switch (input_type) {
131 return scale.keyboard_mouse;
135 return scale.joystick;
136 default:
137 return 1.0f;
138 }
139 }
140 };
141
144 f64 last_binary_update; // The last time the binary state ("is this pressed?") is updated
145 };
146
147 /* This is faster than using getKeySetting with the tradeoff that functions
148 * using it must make sure that it's initialised before using it and there is
149 * no error handling (for example bounds checking). This is useful here as the
150 * faster (up to 10x faster) key lookup is an asset.
151 */
152 std::array<Keybinding, KeyType::INTERNAL_ENUM_COUNT> keybindings;
153
154 // Repetition interval for joystick input
156
157 f32 mouse_wheel = 0.f;
158
159 // The current state of physical keys.
160 std::map<KeyPress, PhysicalKeyState> physicalKeyDown;
161
162 // The current state of keys
163 std::array<float, GameKeyType::INTERNAL_ENUM_COUNT> axisValues;
164
165 // Like axisValues but only reset when that key is read
166 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasDown;
167
168 // Whether a key has just been pressed
169 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasPressed;
170
171 // Whether a key has just been released
172 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasReleased;
173
174 // List of keys we listen for
175 std::unordered_map<KeyPress, GameKeyType> keysListenedFor;
176
177 // Intentionally not reset by clearInput/releaseAllKeys.
178 bool fullscreen_is_down = false;
179
180 bool close_world_down = false;
181 bool esc_down = false;
182
184
185 // Relative mouse movement accumulated from mouse events
187};
188
190{
191public:
192 InputHandler();
193
194 virtual ~InputHandler() = default;
195
196 virtual bool isRandom() const
197 {
198 return false;
199 }
200
201 static inline s16 analogToInt(float value)
202 {
203 return value > 0 ? std::min(1.0f, value) * INT16_MAX : 0;
204 }
205
206 static inline float intToAnalog(s16 value)
207 {
208 return value > 0 ? value / (f32)INT16_MAX : 0.0f;
209 }
210
211 virtual float getAxisValue(GameKeyType k) = 0;
212 virtual bool isKeyDown(GameKeyType k)
213 {
214 return getAxisValue(k) > 0;
215 }
216 virtual bool wasKeyDown(GameKeyType k) = 0;
217 virtual bool wasKeyPressed(GameKeyType k) = 0;
218 virtual bool wasKeyReleased(GameKeyType k) = 0;
219 virtual bool cancelPressed() = 0;
220
221 virtual void clearWasKeyPressed() {}
222 virtual void clearWasKeyReleased() {}
223
224 virtual void reloadKeybindings() {}
225
226 virtual v2s32 getMousePos() = 0;
227 virtual void setMousePos(s32 x, s32 y) = 0;
228
229 virtual v2s32 getMouseMovement() = 0;
230
231 virtual s32 getMouseWheel() = 0;
232
233 virtual void step(float dtime) {}
234
235 virtual void clear() {}
236 virtual void releaseAllKeys() {}
237
238 static void settingChangedCallback(const std::string &name, void *data)
239 {
240 static_cast<InputHandler *>(data)->reloadKeybindings();
241 }
242};
243
244/*
245 Separated input handler implementations
246*/
247
248class RealInputHandler final : public InputHandler
249{
250public:
252 {
254 }
255
256 virtual float getAxisValue(GameKeyType k)
257 {
258 return m_receiver->GetAxisValue(k);
259 }
260 virtual bool wasKeyDown(GameKeyType k)
261 {
262 return m_receiver->WasKeyDown(k);
263 }
265 {
266 return m_receiver->WasKeyPressed(k);
267 }
269 {
270 return m_receiver->WasKeyReleased(k);
271 }
272
273 virtual bool cancelPressed()
274 {
275 return wasKeyDown(KeyType::ESC);
276 }
277
278 virtual void clearWasKeyPressed()
279 {
281 }
282 virtual void clearWasKeyReleased()
283 {
285 }
286
287 virtual void reloadKeybindings()
288 {
290 }
291
292 virtual v2s32 getMousePos();
293 virtual void setMousePos(s32 x, s32 y);
294
296 {
298 }
299
300 virtual s32 getMouseWheel()
301 {
302 return m_receiver->getMouseWheel();
303 }
304
305 void clear()
306 {
308 }
309
311 {
313 }
314
315private:
318};
319
321{
322public:
324
325 bool isRandom() const
326 {
327 return true;
328 }
329
330 virtual float getAxisValue(GameKeyType k) { return keydown[k]; }
331 virtual bool wasKeyDown(GameKeyType k) { return false; }
332 virtual bool wasKeyPressed(GameKeyType k) { return false; }
333 virtual bool wasKeyReleased(GameKeyType k) { return false; }
334 virtual bool cancelPressed() { return false; }
335 virtual v2s32 getMousePos() { return mousepos; }
336 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
337
339 {
341 mousemovement = v2s32(0, 0);
342 return a;
343 }
344
345 virtual s32 getMouseWheel() { return 0; }
346
347 virtual void step(float dtime);
348
349 s32 Rand(s32 min, s32 max);
350
351private:
352 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keydown;
356};
Definition inputhandler.h:190
virtual s32 getMouseWheel()=0
virtual v2s32 getMousePos()=0
virtual v2s32 getMouseMovement()=0
virtual ~InputHandler()=default
virtual void clearWasKeyReleased()
Definition inputhandler.h:222
virtual bool isRandom() const
Definition inputhandler.h:196
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:212
virtual bool wasKeyDown(GameKeyType k)=0
virtual void clear()
Definition inputhandler.h:235
virtual void reloadKeybindings()
Definition inputhandler.h:224
virtual float getAxisValue(GameKeyType k)=0
virtual void step(float dtime)
Definition inputhandler.h:233
static s16 analogToInt(float value)
Definition inputhandler.h:201
static float intToAnalog(s16 value)
Definition inputhandler.h:206
static void settingChangedCallback(const std::string &name, void *data)
Definition inputhandler.h:238
virtual bool cancelPressed()=0
virtual void releaseAllKeys()
Definition inputhandler.h:236
virtual bool wasKeyReleased(GameKeyType k)=0
virtual void clearWasKeyPressed()
Definition inputhandler.h:221
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:152
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:106
bool WasKeyPressed(GameKeyType key) const
Definition inputhandler.h:46
void releaseAllKeys()
Definition inputhandler.h:85
std::unordered_map< KeyPress, GameKeyType > keysListenedFor
Definition inputhandler.h:175
v2s32 mouse_movement
Definition inputhandler.h:186
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:163
float repeat_joystick_button_time
Definition inputhandler.h:155
std::map< KeyPress, PhysicalKeyState > physicalKeyDown
Definition inputhandler.h:160
PointerType getLastPointerType()
Definition inputhandler.h:103
bool fullscreen_is_down
Definition inputhandler.h:178
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasReleased
Definition inputhandler.h:172
bool IsKeyDown(GameKeyType key) const
Definition inputhandler.h:36
bool esc_down
Definition inputhandler.h:181
float GetAxisValue(GameKeyType key) const
Definition inputhandler.h:33
void clearWasKeyReleased()
Definition inputhandler.h:98
s32 getMouseWheel()
Definition inputhandler.h:56
void clearInput()
Definition inputhandler.h:73
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasDown
Definition inputhandler.h:166
PointerType last_pointer_type
Definition inputhandler.h:183
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasPressed
Definition inputhandler.h:169
v2s32 getMouseMovement()
Definition inputhandler.h:66
f32 mouse_wheel
Definition inputhandler.h:157
bool setKeyDown(KeyPress keyCode, float value)
Definition inputhandler.cpp:149
bool close_world_down
Definition inputhandler.h:180
void clearWasKeyPressed()
Definition inputhandler.h:93
bool WasKeyDown(GameKeyType key)
Definition inputhandler.cpp:124
v2s32 mousespeed
Definition inputhandler.h:354
virtual v2s32 getMousePos()
Definition inputhandler.h:335
bool isRandom() const
Definition inputhandler.h:325
virtual bool wasKeyReleased(GameKeyType k)
Definition inputhandler.h:333
v2s32 mousepos
Definition inputhandler.h:353
s32 Rand(s32 min, s32 max)
Definition inputhandler.cpp:323
virtual s32 getMouseWheel()
Definition inputhandler.h:345
virtual void step(float dtime)
Definition inputhandler.cpp:334
virtual bool wasKeyPressed(GameKeyType k)
Definition inputhandler.h:332
virtual v2s32 getMouseMovement()
Definition inputhandler.h:338
virtual void setMousePos(s32 x, s32 y)
Definition inputhandler.h:336
virtual bool cancelPressed()
Definition inputhandler.h:334
v2s32 mousemovement
Definition inputhandler.h:355
RandomInputHandler()=default
virtual float getAxisValue(GameKeyType k)
Definition inputhandler.h:330
virtual bool wasKeyDown(GameKeyType k)
Definition inputhandler.h:331
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keydown
Definition inputhandler.h:352
virtual bool cancelPressed()
Definition inputhandler.h:273
MyEventReceiver * m_receiver
Definition inputhandler.h:316
virtual v2s32 getMousePos()
Definition inputhandler.cpp:300
virtual bool wasKeyDown(GameKeyType k)
Definition inputhandler.h:260
v2s32 m_mousepos
Definition inputhandler.h:317
virtual void clearWasKeyPressed()
Definition inputhandler.h:278
virtual v2s32 getMouseMovement()
Definition inputhandler.h:295
void releaseAllKeys()
Definition inputhandler.h:310
virtual bool wasKeyReleased(GameKeyType k)
Definition inputhandler.h:268
virtual void clearWasKeyReleased()
Definition inputhandler.h:282
virtual void setMousePos(s32 x, s32 y)
Definition inputhandler.cpp:310
virtual s32 getMouseWheel()
Definition inputhandler.h:300
RealInputHandler(MyEventReceiver *receiver)
Definition inputhandler.h:251
virtual bool wasKeyPressed(GameKeyType k)
Definition inputhandler.h:264
virtual void reloadKeybindings()
Definition inputhandler.h:287
void clear()
Definition inputhandler.h:305
virtual float getAxisValue(GameKeyType k)
Definition inputhandler.h:256
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:124
float getScale(KeyPress::InputType input_type) const
Definition inputhandler.h:126
std::vector< KeyPress > keys
Definition inputhandler.h:117
Definition inputhandler.h:142
f64 last_binary_update
Definition inputhandler.h:144
float analog_value
Definition inputhandler.h:143
constexpr v3f x
Definition test_irr_matrix4.cpp:18
constexpr v3f y
Definition test_irr_matrix4.cpp:19