Luanti 5.17.0-dev
Loading...
Searching...
No Matches
keycode.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 "keys.h"
9#include <Keycodes.h>
10#include <IEventReceiver.h>
11#include <string>
12#include <variant>
13#include <vector>
14
15/* A key press, consisting of a scancode or a keycode.
16 * This fits into 64 bits, so prefer passing this by value.
17*/
19{
20public:
35
44
45 KeyPress() = default;
46
47 KeyPress(const std::string &name);
48
49 KeyPress(const SEvent::SKeyInput &in);
50 KeyPress(const SEvent::SMouseInput &in);
51 KeyPress(const SEvent::SGamepadButtonEvent &in);
52 KeyPress(const SEvent::SGamepadAxisEvent &in);
53 KeyPress(GameKeyType key) : value(key) {}
54
55 // Get a string representation that is suitable for use in minetest.conf
56 std::string sym() const;
57
58 // Get a human-readable string representation
59 std::string name() const;
60
61 // Get the scancode or 0 is one is not available
62 u32 getScancode() const
63 {
64 if (auto pv = getIf<InputType::KEYBOARD>())
65 return *pv;
66 return 0;
67 }
68
69 bool operator==(KeyPress o) const {
70 return value == o.value;
71 }
72 bool operator!=(KeyPress o) const {
73 return !(*this == o);
74 }
75
76 // Used for e.g. std::set
77 bool operator<(KeyPress o) const {
78 return value < o.value;
79 }
80
81 // Get the type of input
83 return static_cast<InputType>(value.index());
84 }
85
88
91
92 // Check whether the keypress is valid
93 operator bool() const;
94
95 static KeyPress getSpecialKey(const std::string &name);
96
97private:
98 // The same data type may be used for different variants, so this should be indexed using InputType.
99 // The get, getIf, and emplace methods are wrappers for their std::variant counterparts. This allows using
100 // InputType enum values instead of numeric indices.
101 using value_type = std::variant<u32, u32, GameKeyType, GamepadButton, GamepadAxis, GamepadAxis>;
102
103 template<InputType I>
104 using value_alternative_t = std::variant_alternative_t<static_cast<size_t>(I), value_type>;
105
106 template<InputType I>
107 bool loadUnsignedFromPrefix(const std::string &name, const std::string &prefix);
108 bool loadFromScancode(const std::string &name);
109 void loadFromKey(EKEY_CODE keycode, wchar_t keychar);
110
112
113 template<InputType I>
115 return std::get<static_cast<size_t>(I)>(value);
116 }
117
118 template<InputType I, typename T>
119 T getCast() const {
120 return static_cast<T>(get<I>());
121 }
122
123 template<InputType I>
124 std::add_pointer_t<const value_alternative_t<I>> getIf() const {
125 return std::get_if<static_cast<size_t>(I)>(&value);
126 }
127
128 template<InputType I>
130 value.emplace<static_cast<size_t>(I)>(newValue);
131 }
132
133 friend std::hash<KeyPress>;
134};
135
136template <>
137struct std::hash<KeyPress>
138{
139 size_t operator()(KeyPress kp) const noexcept {
140 return std::hash<KeyPress::value_type>{}(kp.value);
141 }
142};
143
144// Global defines for convenience
145// This implementation defers creation of the objects to make sure that the
146// IrrlichtDevice is initialized.
147#define EscapeKey KeyPress::getSpecialKey("KEY_ESCAPE")
148
149// Key configuration getter
150// Note that the reference may be invalidated by a next call to getKeySetting
151// or a related function, so the value should either be used immediately or
152// copied elsewhere before calling this again.
153const std::vector<KeyPress> &getKeySetting(const std::string &settingname);
154
155// Check whether the key setting includes a key.
156bool keySettingHasMatch(const std::string &settingname, KeyPress kp);
157
158// Clear fast lookup cache
159void clearKeyCache();
160
164 float analog_value = 0;
165
167 KeyPressEvent(const SEvent &event);
168
169 operator bool() const {
170 return key;
171 }
172
173 bool isPressed() const {
174 return analog_value > 0;
175 }
176};
Definition keycode.h:19
void loadFromKey(EKEY_CODE keycode, wchar_t keychar)
Definition keycode.cpp:278
KeyPress getOppositeAxisDirection() const
Get the joystick axis in the opposite direction (if available).
Definition keycode.cpp:511
u32 getScancode() const
Definition keycode.h:62
KeyPress(GameKeyType key)
Definition keycode.h:53
value_alternative_t< I > get() const
Definition keycode.h:114
std::add_pointer_t< const value_alternative_t< I > > getIf() const
Definition keycode.h:124
bool loadFromScancode(const std::string &name)
static KeyPress getSpecialKey(const std::string &name)
Definition keycode.cpp:568
std::string sym() const
Definition keycode.cpp:339
std::string name() const
Definition keycode.cpp:357
std::variant_alternative_t< static_cast< size_t >(I), value_type > value_alternative_t
Definition keycode.h:104
std::variant< u32, u32, GameKeyType, GamepadButton, GamepadAxis, GamepadAxis > value_type
Definition keycode.h:101
bool operator!=(KeyPress o) const
Definition keycode.h:72
bool operator==(KeyPress o) const
Definition keycode.h:69
bool operator<(KeyPress o) const
Definition keycode.h:77
InputType getType() const
Definition keycode.h:82
InputSourceType getSourceType() const
Get the source type of input.
Definition keycode.cpp:547
value_type value
Definition keycode.h:111
T getCast() const
Definition keycode.h:119
bool loadUnsignedFromPrefix(const std::string &name, const std::string &prefix)
Definition keycode.cpp:499
InputSourceType
Type of the input device.
Definition keycode.h:37
@ GAMEPAD
Definition keycode.h:42
@ TOUCHSCREEN
Definition keycode.h:41
@ MOUSE
Definition keycode.h:40
@ INVALID
Definition keycode.h:38
InputType
The type of the input value.
Definition keycode.h:27
@ GAME_ACTION
GameKeyType input passed by touchscreen buttons.
Definition keycode.h:30
@ 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
KeyPress()=default
void emplace(value_alternative_t< I > newValue)
Definition keycode.h:129
const std::vector< KeyPress > & getKeySetting(const std::string &settingname)
Definition keycode.cpp:583
bool keySettingHasMatch(const std::string &settingname, KeyPress kp)
Definition keycode.cpp:601
void clearKeyCache()
Definition keycode.cpp:607
KeyType::T GameKeyType
Definition keys.h:109
KeyPressEvent(const SEvent &event)
Construct a KeyPressEvent from an Irrlicht event. User events (e.g. touchscreen input) is not handled...
Definition keycode.cpp:612
KeyPress key
Definition keycode.h:163
bool isPressed() const
Definition keycode.h:173
float analog_value
Definition keycode.h:164
size_t operator()(KeyPress kp) const noexcept
Definition keycode.h:139