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