Pioneer
Loading...
Searching...
No Matches
InputBindings.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#pragma once
5
6#include "DeleteEmitter.h"
7
8#include <SDL_events.h>
9#include <SDL_keycode.h>
10#include <sigc++/sigc++.h>
11
12#include <cstdint>
13#include <iosfwd>
14#include <string_view>
15
16namespace InputBindings {
17 enum class Response {
18 Ignored = 0,
19 Pressed,
21 };
22
23 struct KeyBinding {
24 enum class Type : uint8_t {
30 };
31
33 union {
34 SDL_Keycode keycode = 0;
35 struct {
36 // 65536 possible IDs should be more than plenty, even with lots of hot-plug noise.
37 uint16_t id;
38 // if type = JoystickHat, this is the hat direction; otherwise it's the button index
39 uint8_t button;
40 uint8_t hat;
42 struct {
43 uint8_t button;
45 };
46
47 KeyBinding() = default;
48
49 KeyBinding(SDL_Keycode k) :
51
52 static KeyBinding JoystickButton(uint16_t joystickID, uint8_t button)
53 {
54 KeyBinding t;
56 t.joystick = { joystickID, button, 0 };
57 return t;
58 }
59
60 static KeyBinding JoystickHat(uint16_t joystickID, uint8_t hat, uint8_t direction)
61 {
62 KeyBinding t;
64 t.joystick = { joystickID, direction, hat };
65 return t;
66 }
67
68 static KeyBinding MouseButton(uint8_t mouseButton)
69 {
70 KeyBinding t;
72 t.mouse.button = mouseButton;
73 return t;
74 }
75
76 static KeyBinding FromEvent(SDL_Event &event);
77
78 bool Enabled() const { return type != Type::Disabled; }
79 Response Matches(const SDL_Event &ev) const;
80
81 bool operator==(const KeyBinding &rhs) const;
82 bool operator<(const KeyBinding &rhs) const;
83
84 // serialization
85 friend std::string_view &operator>>(std::string_view &, KeyBinding &);
86 friend std::ostream &operator<<(std::ostream &, const KeyBinding &);
87 };
88
89 struct JoyAxis {
90 uint16_t joystickId;
91 uint8_t axis;
92 int8_t direction; // if 0, the axis is disabled
93
94 bool Enabled() const { return direction != 0; }
95
96 bool operator==(const JoyAxis &rhs) const
97 {
98 if (!direction && !rhs.direction)
99 return true;
100
101 return joystickId == rhs.joystickId && axis == rhs.axis && direction == rhs.direction;
102 }
103
104 // serialization
105 friend std::string_view &operator>>(std::string_view &, JoyAxis &);
106 friend std::ostream &operator<<(std::ostream &, const JoyAxis &);
107 };
108
109 struct KeyChord {
113
114 KeyChord() = default;
116 activator(a),
117 modifier1(m1),
118 modifier2(m2)
119 {}
120
121 bool IsActive() const { return Enabled() && m_active; }
122 bool Enabled() const { return activator.Enabled(); }
123
124 bool operator==(const KeyChord &rhs) const
125 {
126 return activator == rhs.activator && modifier1 == rhs.modifier1 && modifier2 == rhs.modifier2;
127 }
128 bool operator!=(const KeyChord &rhs) const { return !(*this == rhs); }
129
130 // Groups chords by number of modifiers in descending order
131 bool operator<(const KeyChord &rhs) const
132 {
133 return (modifier2.Enabled() && !rhs.modifier2.Enabled()) || (modifier1.Enabled() && !rhs.modifier1.Enabled());
134 }
135
136 bool m_active = false;
137 uint8_t m_queuedEvents = 0;
138
139 // serialization
140 friend std::string_view &operator>>(std::string_view &, KeyChord &);
141 friend std::ostream &operator<<(std::ostream &, const KeyChord &);
142 };
143
144 struct Action : public DeleteEmitter {
147 bool m_active = false;
148 sigc::signal<void> onPressed;
149 sigc::signal<void> onReleased;
150
151 Action() = default;
152 Action(KeyChord b1, KeyChord b2 = {}) :
153 binding(b1),
154 binding2(b2)
155 {}
156
157 // NOTE: sigc::signals cannot be copied, this function is for convenience to copy bindings only
158 Action &operator=(const Action &rhs);
159
160 bool IsActive() { return m_active; }
161 bool Enabled() { return binding.Enabled() || binding2.Enabled(); }
162
163 // serialization
164 friend std::string_view &operator>>(std::string_view &, Action &);
165 friend std::ostream &operator<<(std::ostream &, const Action &);
166 };
167
168 struct Axis : public DeleteEmitter {
172 float m_value;
174 sigc::signal<void, float> onAxisValue;
175
176 Axis() = default;
177 Axis(JoyAxis a, KeyChord p = {}, KeyChord n = {}) :
178 axis(a),
179 positive(p),
180 negative(n)
181 {}
182
183 Axis(KeyChord p, KeyChord n = {}) :
184 axis{},
185 positive(p),
186 negative(n)
187 {}
188
189 // NOTE: sigc::signals cannot be copied, this function is for convenience to copy bindings only
190 Axis &operator=(const Axis &rhs);
191
192 bool IsActive() { return m_value != 0.0 || positive.IsActive() || negative.IsActive(); }
193 float GetValue() { return m_value; }
194 // if we want to set the value of the axis, for example from the UI slider
195 // must be remembered separately, because m_value is overwritten by data from the joystick
196 void SetValue(float value) { m_manualValue = value; }
197 bool Enabled() { return axis.Enabled() || positive.Enabled() || negative.Enabled(); }
198
199 // serialization
200 friend std::string_view &operator>>(std::string_view &, Axis &);
201 friend std::ostream &operator<<(std::ostream &, const Axis &);
202 };
203
204 std::string_view &operator>>(std::string_view &, KeyBinding &);
205 std::ostream &operator<<(std::ostream &, const KeyBinding &);
206
207 std::string_view &operator>>(std::string_view &, JoyAxis &);
208 std::ostream &operator<<(std::ostream &, const JoyAxis &);
209
210 std::string_view &operator>>(std::string_view &, KeyChord &);
211 std::ostream &operator<<(std::ostream &, const KeyChord &);
212
213 std::string_view &operator>>(std::string_view &, Action &);
214 std::ostream &operator<<(std::ostream &, const Action &);
215
216 std::string_view &operator>>(std::string_view &, Axis &);
217 std::ostream &operator<<(std::ostream &, const Axis &);
218}; // namespace InputBindings
Definition: DeleteEmitter.h:16
Definition: InputBindings.h:16
Response
Definition: InputBindings.h:17
std::string_view & operator>>(std::string_view &, KeyBinding &)
Definition: InputBindings.cpp:159
std::ostream & operator<<(std::ostream &, const KeyBinding &)
Definition: InputBindings.cpp:206
Definition: InputBindings.h:144
friend std::ostream & operator<<(std::ostream &, const Action &)
sigc::signal< void > onPressed
Definition: InputBindings.h:148
bool Enabled()
Definition: InputBindings.h:161
Action(KeyChord b1, KeyChord b2={})
Definition: InputBindings.h:152
KeyChord binding
Definition: InputBindings.h:145
KeyChord binding2
Definition: InputBindings.h:146
Action & operator=(const Action &rhs)
Definition: InputBindings.cpp:121
friend std::string_view & operator>>(std::string_view &, Action &)
sigc::signal< void > onReleased
Definition: InputBindings.h:149
bool m_active
Definition: InputBindings.h:147
bool IsActive()
Definition: InputBindings.h:160
Definition: InputBindings.h:168
bool Enabled()
Definition: InputBindings.h:197
Axis(JoyAxis a, KeyChord p={}, KeyChord n={})
Definition: InputBindings.h:177
Axis & operator=(const Axis &rhs)
Definition: InputBindings.cpp:128
KeyChord negative
Definition: InputBindings.h:171
friend std::ostream & operator<<(std::ostream &, const Axis &)
float GetValue()
Definition: InputBindings.h:193
float m_manualValue
Definition: InputBindings.h:173
float m_value
Definition: InputBindings.h:172
friend std::string_view & operator>>(std::string_view &, Axis &)
bool IsActive()
Definition: InputBindings.h:192
KeyChord positive
Definition: InputBindings.h:170
void SetValue(float value)
Definition: InputBindings.h:196
sigc::signal< void, float > onAxisValue
Definition: InputBindings.h:174
JoyAxis axis
Definition: InputBindings.h:169
Axis(KeyChord p, KeyChord n={})
Definition: InputBindings.h:183
Definition: InputBindings.h:89
uint8_t axis
Definition: InputBindings.h:91
bool operator==(const JoyAxis &rhs) const
Definition: InputBindings.h:96
int8_t direction
Definition: InputBindings.h:92
friend std::ostream & operator<<(std::ostream &, const JoyAxis &)
bool Enabled() const
Definition: InputBindings.h:94
friend std::string_view & operator>>(std::string_view &, JoyAxis &)
uint16_t joystickId
Definition: InputBindings.h:90
Definition: InputBindings.h:23
bool Enabled() const
Definition: InputBindings.h:78
struct InputBindings::KeyBinding::@4::@6 joystick
KeyBinding(SDL_Keycode k)
Definition: InputBindings.h:49
Type
Definition: InputBindings.h:24
static KeyBinding JoystickButton(uint16_t joystickID, uint8_t button)
Definition: InputBindings.h:52
bool operator==(const KeyBinding &rhs) const
Definition: InputBindings.cpp:74
uint8_t hat
Definition: InputBindings.h:40
SDL_Keycode keycode
Definition: InputBindings.h:34
Response Matches(const SDL_Event &ev) const
Definition: InputBindings.cpp:53
struct InputBindings::KeyBinding::@4::@7 mouse
friend std::ostream & operator<<(std::ostream &, const KeyBinding &)
friend std::string_view & operator>>(std::string_view &, KeyBinding &)
bool operator<(const KeyBinding &rhs) const
Definition: InputBindings.cpp:93
static KeyBinding MouseButton(uint8_t mouseButton)
Definition: InputBindings.h:68
uint8_t button
Definition: InputBindings.h:39
Type type
Definition: InputBindings.h:32
static KeyBinding FromEvent(SDL_Event &event)
uint16_t id
Definition: InputBindings.h:37
static KeyBinding JoystickHat(uint16_t joystickID, uint8_t hat, uint8_t direction)
Definition: InputBindings.h:60
Definition: InputBindings.h:109
bool operator<(const KeyChord &rhs) const
Definition: InputBindings.h:131
bool operator!=(const KeyChord &rhs) const
Definition: InputBindings.h:128
friend std::string_view & operator>>(std::string_view &, KeyChord &)
KeyChord(KeyBinding a, KeyBinding m1={}, KeyBinding m2={})
Definition: InputBindings.h:115
bool IsActive() const
Definition: InputBindings.h:121
friend std::ostream & operator<<(std::ostream &, const KeyChord &)
KeyBinding activator
Definition: InputBindings.h:110
KeyBinding modifier2
Definition: InputBindings.h:112
bool Enabled() const
Definition: InputBindings.h:122
bool operator==(const KeyChord &rhs) const
Definition: InputBindings.h:124
bool m_active
Definition: InputBindings.h:136
uint8_t m_queuedEvents
Definition: InputBindings.h:137
KeyBinding modifier1
Definition: InputBindings.h:111