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