Luanti 5.16.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 bool checkKeyDown(GameKeyType action) const;
100
101 /* This is faster than using getKeySetting with the tradeoff that functions
102 * using it must make sure that it's initialised before using it and there is
103 * no error handling (for example bounds checking). This is useful here as the
104 * faster (up to 10x faster) key lookup is an asset.
105 */
106 std::array<std::vector<KeyPress>, KeyType::INTERNAL_ENUM_COUNT> keybindings;
107
108 s32 mouse_wheel = 0;
109
110 // The current state of physical keys.
111 std::set<KeyPress> physicalKeyDown;
112
113 // The current state of keys
114 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyIsDown;
115
116 // Like keyIsDown but only reset when that key is read
117 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasDown;
118
119 // Whether a key has just been pressed
120 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasPressed;
121
122 // Whether a key has just been released
123 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keyWasReleased;
124
125 // List of keys we listen for
126 std::unordered_map<KeyPress, GameKeyType> keysListenedFor;
127
128 // Intentionally not reset by clearInput/releaseAllKeys.
129 bool fullscreen_is_down = false;
130
131 bool close_world_down = false;
132 bool esc_down = false;
133
135};
136
138{
139public:
141 {
142 for (const auto &name: Settings::getLayer(SL_DEFAULTS)->getNames())
143 if (str_starts_with(name, "keymap_"))
145 }
146
147 virtual ~InputHandler() = default;
148
149 virtual bool isRandom() const
150 {
151 return false;
152 }
153
154 virtual bool isKeyDown(GameKeyType k) = 0;
155 virtual bool wasKeyDown(GameKeyType k) = 0;
156 virtual bool wasKeyPressed(GameKeyType k) = 0;
157 virtual bool wasKeyReleased(GameKeyType k) = 0;
158 virtual bool cancelPressed() = 0;
159
160 virtual float getJoystickSpeed() = 0;
161 virtual float getJoystickDirection() = 0;
162
163 virtual void clearWasKeyPressed() {}
164 virtual void clearWasKeyReleased() {}
165
166 virtual void reloadKeybindings() {}
167
168 virtual v2s32 getMousePos() = 0;
169 virtual void setMousePos(s32 x, s32 y) = 0;
170
171 virtual s32 getMouseWheel() = 0;
172
173 virtual void step(float dtime) {}
174
175 virtual void clear() {}
176 virtual void releaseAllKeys() {}
177
178 static void settingChangedCallback(const std::string &name, void *data)
179 {
180 static_cast<InputHandler *>(data)->reloadKeybindings();
181 }
182
184};
185
186/*
187 Separated input handler implementations
188*/
189
190class RealInputHandler final : public InputHandler
191{
192public:
198
200 {
201 m_receiver->joystick = nullptr;
202 }
203
204 virtual bool isKeyDown(GameKeyType k)
205 {
206 return m_receiver->IsKeyDown(k) || joystick.isKeyDown(k);
207 }
208 virtual bool wasKeyDown(GameKeyType k)
209 {
210 return m_receiver->WasKeyDown(k) || joystick.wasKeyDown(k);
211 }
213 {
215 }
217 {
219 }
220
221 virtual float getJoystickSpeed();
222
223 virtual float getJoystickDirection();
224
225 virtual bool cancelPressed()
226 {
227 return wasKeyDown(KeyType::ESC);
228 }
229
230 virtual void clearWasKeyPressed()
231 {
233 }
234 virtual void clearWasKeyReleased()
235 {
237 }
238
239 virtual void reloadKeybindings()
240 {
242 }
243
244 virtual v2s32 getMousePos();
245 virtual void setMousePos(s32 x, s32 y);
246
247 virtual s32 getMouseWheel()
248 {
249 return m_receiver->getMouseWheel();
250 }
251
252 void clear()
253 {
254 joystick.clear();
256 }
257
263
264private:
267};
268
270{
271public:
273
274 bool isRandom() const
275 {
276 return true;
277 }
278
279 virtual bool isKeyDown(GameKeyType k) { return keydown[k]; }
280 virtual bool wasKeyDown(GameKeyType k) { return false; }
281 virtual bool wasKeyPressed(GameKeyType k) { return false; }
282 virtual bool wasKeyReleased(GameKeyType k) { return false; }
283 virtual bool cancelPressed() { return false; }
284 virtual float getJoystickSpeed() { return joystickSpeed; }
285 virtual float getJoystickDirection() { return joystickDirection; }
286 virtual v2s32 getMousePos() { return mousepos; }
287 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
288
289 virtual s32 getMouseWheel() { return 0; }
290
291 virtual void step(float dtime);
292
293 s32 Rand(s32 min, s32 max);
294
295private:
296 std::bitset<GameKeyType::INTERNAL_ENUM_COUNT> keydown;
301};
Definition inputhandler.h:138
virtual s32 getMouseWheel()=0
virtual v2s32 getMousePos()=0
virtual bool isKeyDown(GameKeyType k)=0
virtual ~InputHandler()=default
virtual void clearWasKeyReleased()
Definition inputhandler.h:164
virtual float getJoystickSpeed()=0
virtual bool isRandom() const
Definition inputhandler.h:149
virtual bool wasKeyPressed(GameKeyType k)=0
InputHandler()
Definition inputhandler.h:140
virtual void setMousePos(s32 x, s32 y)=0
virtual bool wasKeyDown(GameKeyType k)=0
virtual void clear()
Definition inputhandler.h:175
virtual void reloadKeybindings()
Definition inputhandler.h:166
virtual void step(float dtime)
Definition inputhandler.h:173
static void settingChangedCallback(const std::string &name, void *data)
Definition inputhandler.h:178
JoystickController joystick
Definition inputhandler.h:183
virtual bool cancelPressed()=0
virtual void releaseAllKeys()
Definition inputhandler.h:176
virtual bool wasKeyReleased(GameKeyType k)=0
virtual float getJoystickDirection()=0
virtual void clearWasKeyPressed()
Definition inputhandler.h:163
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:331
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:18
T
Definition keys.h:11
@ ESC
Definition keys.h:24
@ INTERNAL_ENUM_COUNT
Definition keys.h:98
Definition inputhandler.h:25
bool checkKeyDown(GameKeyType action) const
Definition inputhandler.cpp:112
void reloadKeybindings()
Definition inputhandler.cpp:15
std::set< KeyPress > physicalKeyDown
Definition inputhandler.h:111
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:108
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyIsDown
Definition inputhandler.h:114
std::unordered_map< KeyPress, GameKeyType > keysListenedFor
Definition inputhandler.h:126
std::array< std::vector< KeyPress >, KeyType::INTERNAL_ENUM_COUNT > keybindings
Definition inputhandler.h:106
bool WasKeyReleased(GameKeyType key) const
Definition inputhandler.h:47
virtual bool OnEvent(const SEvent &event)
Definition inputhandler.cpp:121
bool setKeyDown(KeyPress keyCode, bool is_down)
Definition inputhandler.cpp:85
PointerType getLastPointerType()
Definition inputhandler.h:88
bool fullscreen_is_down
Definition inputhandler.h:129
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasReleased
Definition inputhandler.h:123
bool IsKeyDown(GameKeyType key) const
Definition inputhandler.h:30
bool esc_down
Definition inputhandler.h:132
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:117
PointerType last_pointer_type
Definition inputhandler.h:134
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keyWasPressed
Definition inputhandler.h:120
JoystickController * joystick
Definition inputhandler.h:86
bool close_world_down
Definition inputhandler.h:131
void clearWasKeyPressed()
Definition inputhandler.h:76
bool WasKeyDown(GameKeyType key)
Definition inputhandler.h:33
Definition inputhandler.h:270
v2s32 mousespeed
Definition inputhandler.h:298
virtual v2s32 getMousePos()
Definition inputhandler.h:286
bool isRandom() const
Definition inputhandler.h:274
virtual float getJoystickDirection()
Definition inputhandler.h:285
virtual bool wasKeyReleased(GameKeyType k)
Definition inputhandler.h:282
v2s32 mousepos
Definition inputhandler.h:297
s32 Rand(s32 min, s32 max)
Definition inputhandler.cpp:275
virtual s32 getMouseWheel()
Definition inputhandler.h:289
virtual float getJoystickSpeed()
Definition inputhandler.h:284
virtual void step(float dtime)
Definition inputhandler.cpp:286
virtual bool wasKeyPressed(GameKeyType k)
Definition inputhandler.h:281
float joystickSpeed
Definition inputhandler.h:299
float joystickDirection
Definition inputhandler.h:300
virtual bool isKeyDown(GameKeyType k)
Definition inputhandler.h:279
virtual void setMousePos(s32 x, s32 y)
Definition inputhandler.h:287
virtual bool cancelPressed()
Definition inputhandler.h:283
RandomInputHandler()=default
virtual bool wasKeyDown(GameKeyType k)
Definition inputhandler.h:280
std::bitset< GameKeyType::INTERNAL_ENUM_COUNT > keydown
Definition inputhandler.h:296
Definition inputhandler.h:191
virtual bool cancelPressed()
Definition inputhandler.h:225
MyEventReceiver * m_receiver
Definition inputhandler.h:265
virtual v2s32 getMousePos()
Definition inputhandler.cpp:252
virtual bool wasKeyDown(GameKeyType k)
Definition inputhandler.h:208
virtual ~RealInputHandler()
Definition inputhandler.h:199
v2s32 m_mousepos
Definition inputhandler.h:266
virtual bool isKeyDown(GameKeyType k)
Definition inputhandler.h:204
virtual float getJoystickDirection()
Definition inputhandler.cpp:243
virtual float getJoystickSpeed()
Definition inputhandler.cpp:236
virtual void clearWasKeyPressed()
Definition inputhandler.h:230
void releaseAllKeys()
Definition inputhandler.h:258
virtual bool wasKeyReleased(GameKeyType k)
Definition inputhandler.h:216
virtual void clearWasKeyReleased()
Definition inputhandler.h:234
virtual void setMousePos(s32 x, s32 y)
Definition inputhandler.cpp:262
virtual s32 getMouseWheel()
Definition inputhandler.h:247
RealInputHandler(MyEventReceiver *receiver)
Definition inputhandler.h:193
virtual bool wasKeyPressed(GameKeyType k)
Definition inputhandler.h:212
virtual void reloadKeybindings()
Definition inputhandler.h:239
void clear()
Definition inputhandler.h:252
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