Pioneer
Loading...
Searching...
No Matches
Input.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#include <iostream>
5#include <algorithm>
6
7#ifndef INPUT_H
8#define INPUT_H
9
10#include "InputBindings.h"
11
12#include "SDL_joystick.h"
13
14#include <array>
15#include <vector>
16#include <map>
17#include <string>
18
19class IniConfig;
20
21// Macro to simplify registering input bindings in the codebase
22// TODO: evaluate if registering key bindings via lua / json file works better
23#define REGISTER_INPUT_BINDING(name) \
24 namespace name##Input \
25 { \
26 void Register(Input::Manager *input); \
27 bool name##Registered = Input::AddBindingRegistrar(&Register); \
28 } \
29 void name##Input::Register(Input::Manager *input)
30
31namespace Input {
32 class Manager;
33
34 // The Page->Group->Binding system serves as a thin veneer for the UI to make
35 // sane reasonings about how to structure the Options dialog.
36 struct BindingGroup {
37 enum EntryType : uint8_t {
40 };
41
42 std::map<std::string, EntryType> bindings;
43 };
44
45 struct BindingPage {
46 BindingGroup *GetBindingGroup(std::string id) { return &groups[id]; }
47
48 std::map<std::string, BindingGroup> groups;
49 };
50
51 struct InputFrame {
54
55 InputFrame(Input::Manager *man, bool modal = false) :
56 manager(man),
58 {}
59
60 std::vector<Action *> actions;
61 std::vector<Axis *> axes;
62
63 // Must set this to a valid Input::Manager instance before using AddAction / AddAxis
64 Manager *manager = nullptr;
65 bool active = false;
66 bool modal = false;
67
68 // Call this at startup to register all the bindings associated with the frame.
69 virtual void RegisterBindings(){};
70
71 // Called when the frame is added to the stack.
72 sigc::signal<void, InputFrame *> onFrameAdded;
73
74 // Called when the frame is removed from the stack.
75 sigc::signal<void, InputFrame *> onFrameRemoved;
76
77 Action *AddAction(std::string id);
78 Axis *AddAxis(std::string id);
79 };
80
81 struct JoystickInfo {
82 struct Axis {
83 float value = 0.0;
84 float deadzone = 0.1;
85 float curve = 1.0;
86 bool zeroToOne = false;
87 };
88
89 SDL_Joystick *joystick;
90 SDL_JoystickGUID guid;
91 std::string name;
92
93 std::vector<bool> buttons;
94 std::vector<int> hats;
95
96 std::vector<Axis> axes;
97 };
98
99 void InitJoysticks(IniConfig *config);
100 std::vector<JoystickInfo> &GetJoysticks();
101
102 // User display name for the joystick from the API/OS.
103 std::string JoystickName(int joystick);
104 // fetch the GUID for the named joystick
105 SDL_JoystickGUID JoystickGUID(int joystick);
106 std::string JoystickGUIDString(int joystick);
107 // update the joystick's saved configuration
108 void SaveJoystickConfig(uint32_t joystick, IniConfig *config);
109
110 // reverse map a JoystickGUID to the actual internal ID.
111 int JoystickFromGUIDString(const std::string &guid);
112 int JoystickFromGUIDString(const char *guid);
113 int JoystickFromGUID(SDL_JoystickGUID guid);
114
115 int JoystickFromID(SDL_JoystickID id);
116 // map SDL joystick instance ID to internal ID
117 void AddJoystickID(SDL_JoystickID sdl_id, uint32_t internal_id);
118
119 // An adapter to decouple input frame creation from input binding registration.
120 // The functions registered via AddBindingRegistrar should be thread-safe and
121 // should not depend on anything but the manager object being passed in.
122 // The registrars are guaranteed to be called after static initialization has finished.
123 std::vector<sigc::slot<void, Input::Manager *>> &GetBindingRegistration();
124 bool AddBindingRegistrar(sigc::slot<void, Input::Manager *> &&fn);
125} // namespace Input
126
128public:
129 Manager(IniConfig *config, SDL_Window *window);
130 void InitGame();
131
132 // Call this at the start of a frame, before passing SDL events in
133 void NewFrame();
134
135 // Call once per SDL event, handles updating all internal state
136 void HandleSDLEvent(SDL_Event &ev);
137
138 // Call immediately after processing events, dispatches events to Action / Axis bindings.
139 void DispatchEvents();
140
141 // When enable is false, this prevents the input system from writing to the config file.
142 void EnableConfigSaving(bool enable) { m_enableConfigSaving = enable; }
143
144 BindingPage *GetBindingPage(std::string id) { return &bindingPages[id]; }
145 std::map<std::string, BindingPage> GetBindingPages() { return bindingPages; }
146
147 // Pushes an InputFrame onto the input stack.
148 bool AddInputFrame(InputFrame *frame);
149
150 // Get a read-only list of input frames.
151 const std::vector<InputFrame *> &GetInputFrames() { return m_inputFrames; }
152
153 // Check if a specific input frame is currently on the stack.
154 bool HasInputFrame(InputFrame *frame);
155
156 // Remove an arbitrary input frame from the input stack.
157 void RemoveInputFrame(InputFrame *frame);
158
159 // Inform the input system that a binding or frame was changed this frame.
160 void MarkBindingsDirty() { m_frameListChanged = true; }
161
162 // Creates a new action binding, copying the provided binding.
163 // The returned binding pointer points to the actual binding.
166
167 // Creates a new axis binding, copying the provided binding.
168 // The returned binding pointer points to the actual binding.
169 InputBindings::Axis *AddAxisBinding(std::string id, BindingGroup *group, InputBindings::Axis &&binding);
170 InputBindings::Axis *GetAxisBinding(std::string id);
171
172 // Call EnableBindings() to temporarily disable handling input bindings while
173 // you're recording a new input binding or are in a modal window.
174 void EnableBindings(bool enabled) { m_enableBindings = enabled; }
175
176 bool KeyState(SDL_Keycode k) { return IsKeyDown(k); }
177
178 // returns true if key K is currently pressed
179 bool IsKeyDown(SDL_Keycode k) { return keyState[k] & 0x3; }
180
181 // returns true if key K was pressed this frame
182 bool IsKeyPressed(SDL_Keycode k) { return keyState[k] == 1; }
183
184 // returns true if key K was released this frame
185 bool IsKeyReleased(SDL_Keycode k) { return keyState[k] == 4; }
186
187 int KeyModState() { return keyModState; }
188
189 int JoystickButtonState(int joystick, int button);
190 int JoystickHatState(int joystick, int hat);
191 float JoystickAxisState(int joystick, int axis);
192
193 bool IsJoystickEnabled() { return joystickEnabled; }
194 void SetJoystickEnabled(bool state);
195
196 bool IsMouseYInvert() { return mouseYInvert; }
197 void SetMouseYInvert(bool state);
198
199 bool IsMouseButtonPressed(int button) { return mouseButton[button] == 1; }
200 bool IsMouseButtonReleased(int button) { return mouseButton[button] == 4; }
201
202 bool MouseButtonState(int button) { return mouseButton[button] & 3; }
203 void SetMouseButtonState(int button, bool state) { mouseButton[button] = state; }
204
205 void GetMousePosition(int position[2]);
206
207 void GetMouseMotion(int motion[2])
208 {
209 std::copy_n(mouseMotion.data(), mouseMotion.size(), motion);
210 }
211
212 int GetMouseWheel() { return mouseWheel; }
213
214 // Capturing the mouse hides the cursor, puts the mouse into relative mode,
215 // and passes all mouse inputs to the input system, regardless of whether
216 // ImGui is using them or not.
217 bool IsCapturingMouse() const { return m_capturingMouse; }
218
219 // Set whether the application would like to capture the mouse.
220 // To avoid contention between different classes, please only call this when the state
221 // has actually changed.
222 void SetCapturingMouse(bool enabled);
223 void ClearMouse();
224
225 // Get the default speed modifier to apply to movement (scrolling, zooming...), depending on the "shift" keys.
226 // This is a default value only, centralized here to promote uniform user expericience.
228
229 sigc::signal<void, SDL_Keysym *> onKeyPress;
230 sigc::signal<void, SDL_Keysym *> onKeyRelease;
231 sigc::signal<void, int, int, int> onMouseButtonUp;
232 sigc::signal<void, int, int, int> onMouseButtonDown;
233 sigc::signal<void, bool> onMouseWheel;
234
235private:
236 void RebuildInputFrames();
237 bool GetModifierState(InputBindings::KeyChord *key);
238 bool GetBindingState(InputBindings::KeyBinding &key);
239 float GetAxisState(InputBindings::JoyAxis &axis);
240
241 SDL_Window *m_window;
242 IniConfig *m_config;
243 bool m_enableConfigSaving;
244
245 std::map<SDL_Keycode, uint8_t> keyState;
246 int keyModState;
247 std::array<char, 6> mouseButton;
248 std::array<int, 2> mouseMotion;
249 int mouseWheel;
250 bool m_capturingMouse;
251
252 bool joystickEnabled;
253 bool mouseYInvert;
254
255 std::map<std::string, BindingPage> bindingPages;
256 std::map<std::string, InputBindings::Action> actionBindings;
257 std::map<std::string, InputBindings::Axis> axisBindings;
258 bool m_enableBindings;
259
260 std::vector<InputFrame *> m_inputFrames;
261 bool m_frameListChanged;
262
263 std::vector<InputBindings::Action *> m_activeActions;
264 std::vector<InputBindings::Axis *> m_activeAxes;
265
266 std::map<InputBindings::KeyBinding, bool> m_modifiers;
267 std::vector<InputBindings::KeyChord *> m_chords;
268};
269
270#endif
Definition IniConfig.h:16
Definition Input.h:127
InputBindings::Action * AddActionBinding(std::string id, BindingGroup *group, InputBindings::Action &&binding)
Definition Input.cpp:371
void DispatchEvents()
Definition Input.cpp:787
bool HasInputFrame(InputFrame *frame)
Definition Input.cpp:353
bool KeyState(SDL_Keycode k)
Definition Input.h:176
sigc::signal< void, int, int, int > onMouseButtonUp
Definition Input.h:231
bool AddInputFrame(InputFrame *frame)
Definition Input.cpp:308
void ClearMouse()
Definition Input.cpp:852
void GetMousePosition(int position[2])
Definition Input.cpp:512
bool IsMouseButtonReleased(int button)
Definition Input.h:200
bool IsKeyPressed(SDL_Keycode k)
Definition Input.h:182
void SetCapturingMouse(bool enabled)
Definition Input.cpp:841
bool IsMouseYInvert()
Definition Input.h:196
int KeyModState()
Definition Input.h:187
bool IsMouseButtonPressed(int button)
Definition Input.h:199
float JoystickAxisState(int joystick, int axis)
Definition Input.cpp:482
sigc::signal< void, bool > onMouseWheel
Definition Input.h:233
void MarkBindingsDirty()
Definition Input.h:160
void NewFrame()
Definition Input.cpp:523
void SetJoystickEnabled(bool state)
Definition Input.cpp:494
bool IsKeyDown(SDL_Keycode k)
Definition Input.h:179
bool MouseButtonState(int button)
Definition Input.h:202
std::map< std::string, BindingPage > GetBindingPages()
Definition Input.h:145
void SetMouseYInvert(bool state)
Definition Input.cpp:503
bool IsJoystickEnabled()
Definition Input.h:193
sigc::signal< void, SDL_Keysym * > onKeyPress
Definition Input.h:229
void GetMouseMotion(int motion[2])
Definition Input.h:207
int GetMouseWheel()
Definition Input.h:212
InputBindings::Axis * AddAxisBinding(std::string id, BindingGroup *group, InputBindings::Axis &&binding)
Definition Input.cpp:389
float GetMoveSpeedShiftModifier()
Definition Input.cpp:858
void RemoveInputFrame(InputFrame *frame)
Definition Input.cpp:358
void EnableBindings(bool enabled)
Definition Input.h:174
InputBindings::Axis * GetAxisBinding(std::string id)
Definition Input.cpp:412
bool IsCapturingMouse() const
Definition Input.h:217
void EnableConfigSaving(bool enable)
Definition Input.h:142
void HandleSDLEvent(SDL_Event &ev)
Definition Input.cpp:635
BindingPage * GetBindingPage(std::string id)
Definition Input.h:144
sigc::signal< void, SDL_Keysym * > onKeyRelease
Definition Input.h:230
bool IsKeyReleased(SDL_Keycode k)
Definition Input.h:185
int JoystickButtonState(int joystick, int button)
Definition Input.cpp:458
InputBindings::Action * GetActionBinding(std::string id)
Definition Input.cpp:407
void InitGame()
Definition Input.cpp:263
const std::vector< InputFrame * > & GetInputFrames()
Definition Input.h:151
void SetMouseButtonState(int button, bool state)
Definition Input.h:203
sigc::signal< void, int, int, int > onMouseButtonDown
Definition Input.h:232
int JoystickHatState(int joystick, int hat)
Definition Input.cpp:470
Definition GuiApplication.h:14
std::string JoystickName(int joystick)
Definition Input.cpp:57
SDL_JoystickGUID JoystickGUID(int joystick)
Definition Input.cpp:104
void InitJoysticks(IniConfig *config)
Definition Input.cpp:185
int JoystickFromGUIDString(const std::string &guid)
Definition Input.cpp:72
bool AddBindingRegistrar(sigc::slot< void, Input::Manager * > &&fn)
Definition Input.cpp:32
int JoystickFromGUID(SDL_JoystickGUID guid)
Definition Input.cpp:85
std::vector< JoystickInfo > & GetJoysticks()
Definition Input.cpp:234
int JoystickFromID(SDL_JoystickID id)
Definition Input.cpp:97
std::vector< sigc::slot< void, Input::Manager * > > & GetBindingRegistration()
Definition Input.cpp:27
void AddJoystickID(SDL_JoystickID sdl_id, uint32_t internal_id)
Definition Input.cpp:176
std::string JoystickGUIDString(int joystick)
Definition Input.cpp:62
void SaveJoystickConfig(uint32_t joystick, IniConfig *config)
Definition Input.cpp:157
Definition InputBindings.h:144
Definition InputBindings.h:168
Definition InputBindings.h:89
Definition InputBindings.h:23
Definition InputBindings.h:109
Definition Input.h:36
std::map< std::string, EntryType > bindings
Definition Input.h:42
EntryType
Definition Input.h:37
@ ENTRY_ACTION
Definition Input.h:38
@ ENTRY_AXIS
Definition Input.h:39
Definition Input.h:45
BindingGroup * GetBindingGroup(std::string id)
Definition Input.h:46
std::map< std::string, BindingGroup > groups
Definition Input.h:48
Definition Input.h:51
virtual void RegisterBindings()
Definition Input.h:69
InputFrame(Input::Manager *man, bool modal=false)
Definition Input.h:55
Manager * manager
Definition Input.h:64
bool active
Definition Input.h:65
std::vector< Action * > actions
Definition Input.h:60
Action * AddAction(std::string id)
Definition Input.cpp:288
sigc::signal< void, InputFrame * > onFrameRemoved
Definition Input.h:75
Axis * AddAxis(std::string id)
Definition Input.cpp:298
bool modal
Definition Input.h:66
std::vector< Axis * > axes
Definition Input.h:61
sigc::signal< void, InputFrame * > onFrameAdded
Definition Input.h:72
Definition Input.h:82
float deadzone
Definition Input.h:84
bool zeroToOne
Definition Input.h:86
float value
Definition Input.h:83
float curve
Definition Input.h:85
Definition Input.h:81
SDL_Joystick * joystick
Definition Input.h:89
std::vector< bool > buttons
Definition Input.h:93
std::string name
Definition Input.h:91
std::vector< Axis > axes
Definition Input.h:96
SDL_JoystickGUID guid
Definition Input.h:90
std::vector< int > hats
Definition Input.h:94