Player12
An open source football management simulator
Loading...
Searching...
No Matches
input_actions.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <SDL3/SDL.h>
12#include <imgui.h>
13
14#include <array>
15#include <cstddef>
16#include <cstdint>
17#include <functional>
18#include <map>
19#include <optional>
20#include <string>
21#include <string_view>
22#include <unordered_map>
23#include <vector>
24
42namespace Input
43{
44
46enum class Context : std::uint8_t
47{
48 GLOBAL,
50 MATCH,
51 PLAY,
53};
54
56enum class Category : std::uint8_t
57{
59 CAREER,
60 MATCH,
61 CAMERA,
62 SHOUTS,
63 PLAY,
64 COUNT
65};
66
68const char* categoryKey(Category category);
69
71using ActionId = std::size_t;
72
74inline constexpr std::size_t BINDING_SLOTS = 2;
75
78{
79 std::string id;
80 std::string label_key;
83 ImGuiKeyChord primary = ImGuiKey_None;
84 ImGuiKeyChord alternate = ImGuiKey_None;
85 bool rebindable = true;
86};
87
89struct Action
90{
92 std::array<ImGuiKeyChord, BINDING_SLOTS> chords{};
93};
94
97{
98 bool applied = false;
100 std::optional<ActionId> conflict;
101 std::size_t conflict_slot = 0;
102};
103
105bool contextsOverlap(Context first, Context second);
106
108std::string chordToString(ImGuiKeyChord chord);
109
111std::optional<ImGuiKeyChord> chordFromString(std::string_view text);
112
114std::string chordLabel(ImGuiKeyChord chord);
115
117bool isBindableKey(ImGuiKey key);
118
124std::optional<ImGuiKeyChord> capturePressedChord();
125
131std::array<ImGuiKeyChord, 2> chordsOfEvent(const SDL_KeyboardEvent& event);
132
142{
143 public:
145
148
150 [[nodiscard]] std::optional<ActionId> find(std::string_view id) const;
151
152 [[nodiscard]] const Action& action(ActionId id) const;
153 [[nodiscard]] std::size_t size() const { return actions.size(); }
154
156 [[nodiscard]] ImGuiKeyChord chord(ActionId id, std::size_t slot = 0) const;
157
159 [[nodiscard]] std::string label(ActionId id) const;
160
166 [[nodiscard]] bool pressed(
167 ActionId id, ImGuiInputFlags flags = ImGuiInputFlags_RouteGlobal) const;
169 [[nodiscard]] bool pressed(
170 std::string_view id,
171 ImGuiInputFlags flags = ImGuiInputFlags_RouteGlobal) const;
172
174 [[nodiscard]] bool matches(ActionId id, const SDL_KeyboardEvent& event) const;
175 [[nodiscard]] bool matches(std::string_view id,
176 const SDL_KeyboardEvent& event) const;
181 [[nodiscard]] bool matchesKey(ActionId id,
182 const SDL_KeyboardEvent& event) const;
183 [[nodiscard]] bool matchesKey(std::string_view id,
184 const SDL_KeyboardEvent& event) const;
185
190 [[nodiscard]] std::optional<std::pair<ActionId, std::size_t>> conflictFor(
191 ActionId id, ImGuiKeyChord chord, std::size_t slot) const;
192
199 RebindResult rebind(ActionId id, std::size_t slot, ImGuiKeyChord chord,
200 bool replace = false);
201
203 void resetToDefault(ActionId id);
205 void resetAll();
207 [[nodiscard]] bool isDefault(ActionId id) const;
208
210 void reloadFromSettings();
211
213 [[nodiscard]] std::uint32_t revision() const { return revision_; }
214
216 void forEach(Category category,
217 const std::function<void(ActionId, const Action&)>& visit) const;
218
219 private:
221 {
222 using is_transparent = void;
223 std::size_t operator()(std::string_view text) const
224 {
225 return std::hash<std::string_view>{}(text);
226 }
227 };
228
229 void applyStored(ActionId id);
230 void store(ActionId id) const;
231
232 std::vector<Action> actions;
233 std::uint32_t revision_ = 0;
234 std::unordered_map<std::string, ActionId, StringHash, std::equal_to<>> by_id;
235};
236
239
241namespace Ids
242{
243inline constexpr std::string_view NAV_HOME = "nav.home";
244inline constexpr std::string_view NAV_INBOX = "nav.inbox";
245inline constexpr std::string_view NAV_SQUAD = "nav.squad";
246inline constexpr std::string_view NAV_TRAINING = "nav.training";
247inline constexpr std::string_view NAV_MATCHES = "nav.matches";
248inline constexpr std::string_view NAV_RECRUITMENT = "nav.recruitment";
249inline constexpr std::string_view NAV_CLUB = "nav.club";
250inline constexpr std::string_view NAV_PALETTE = "nav.palette";
251inline constexpr std::string_view NAV_BACK = "nav.back";
252inline constexpr std::string_view NAV_FORWARD = "nav.forward";
253inline constexpr std::string_view NAV_CLOSE = "nav.close";
254inline constexpr std::string_view CAREER_CONTINUE = "career.continue";
255inline constexpr std::string_view CAREER_SAVE = "career.save";
256inline constexpr std::string_view CAREER_HELP = "career.help";
257inline constexpr std::string_view MATCH_PAUSE = "match.pause";
258inline constexpr std::string_view MATCH_FASTER = "match.faster";
259inline constexpr std::string_view MATCH_SLOWER = "match.slower";
260inline constexpr std::string_view MATCH_SUBSTITUTIONS = "match.substitutions";
261inline constexpr std::string_view MATCH_TACTICS = "match.tactics";
262inline constexpr std::string_view MATCH_MUTE = "match.mute";
263inline constexpr std::string_view MATCH_PITCH_FOCUS = "match.pitch_focus";
264inline constexpr std::string_view MATCH_BACK = "match.back";
265inline constexpr std::string_view MATCH_FULLSCREEN = "match.fullscreen";
266inline constexpr std::string_view CAMERA_VIEW_TOGGLE = "camera.view_toggle";
267inline constexpr std::string_view CAMERA_BROADCAST = "camera.broadcast";
268inline constexpr std::string_view CAMERA_TACTICAL = "camera.tactical";
269inline constexpr std::string_view CAMERA_END = "camera.end";
270inline constexpr std::string_view CAMERA_PLAYER = "camera.player";
271inline constexpr std::string_view CAMERA_FREE = "camera.free";
272inline constexpr std::string_view CAMERA_DIRECTOR = "camera.director";
273inline constexpr std::string_view CAMERA_FOLLOW_BALL = "camera.follow_ball";
274inline constexpr std::string_view CAMERA_RESET = "camera.reset";
275inline constexpr std::string_view SCREENSHOT = "general.screenshot";
277inline constexpr std::string_view PLAY_UP = "play.up";
278inline constexpr std::string_view PLAY_DOWN = "play.down";
279inline constexpr std::string_view PLAY_LEFT = "play.left";
280inline constexpr std::string_view PLAY_RIGHT = "play.right";
281inline constexpr std::string_view PLAY_PASS = "play.pass";
282inline constexpr std::string_view PLAY_SHOOT = "play.shoot";
283inline constexpr std::string_view PLAY_THROUGH = "play.through";
284inline constexpr std::string_view PLAY_LOB = "play.lob";
285inline constexpr std::string_view PLAY_SWITCH = "play.switch";
286inline constexpr std::string_view PLAY_JOCKEY = "play.jockey";
287inline constexpr std::string_view PLAY_PAUSE = "play.pause";
289inline constexpr std::size_t SHOUT_COUNT = 11;
290std::string shout(std::size_t index);
291} // namespace Ids
292
293} // namespace Input
Registry of every action. One instance per run (registry()).
Definition input_actions.h:142
bool matchesKey(ActionId id, const SDL_KeyboardEvent &event) const
Like matches() but ignoring the modifiers held (key down or up): for held game controls,...
Definition input_actions.cpp:482
bool matches(ActionId id, const SDL_KeyboardEvent &event) const
True when an SDL key-down event is one of the action's chords.
Definition input_actions.cpp:462
const Action & action(ActionId id) const
Definition input_actions.cpp:430
void applyStored(ActionId id)
Definition input_actions.cpp:593
void forEach(Category category, const std::function< void(ActionId, const Action &)> &visit) const
Calls visit for each action of a category, in order.
Definition input_actions.cpp:585
bool isDefault(ActionId id) const
True when an action uses its default chords.
Definition input_actions.cpp:567
RebindResult rebind(ActionId id, std::size_t slot, ImGuiKeyChord chord, bool replace=false)
Binds a chord (ImGuiKey_None clears the slot). A clash is refused unless replace is true,...
Definition input_actions.cpp:522
ImGuiKeyChord chord(ActionId id, std::size_t slot=0) const
Current chord in a slot (ImGuiKey_None when unbound).
Definition input_actions.cpp:435
std::optional< std::pair< ActionId, std::size_t > > conflictFor(ActionId id, ImGuiKeyChord chord, std::size_t slot) const
Another action that would clash with chord in slot of id (same chord, overlapping context).
Definition input_actions.cpp:504
std::vector< Action > actions
Definition input_actions.h:232
std::uint32_t revision_
Definition input_actions.h:233
std::optional< ActionId > find(std::string_view id) const
Handle of an action by id.
Definition input_actions.cpp:423
ActionId registerAction(const ActionDef &def)
Registers an action (or returns the existing one).
Definition input_actions.cpp:411
std::unordered_map< std::string, ActionId, StringHash, std::equal_to<> > by_id
Definition input_actions.h:234
void reloadFromSettings()
Re-applies the bindings stored in the settings.
Definition input_actions.cpp:575
void resetAll()
Restores every action's default chords.
Definition input_actions.cpp:558
bool pressed(ActionId id, ImGuiInputFlags flags=ImGuiInputFlags_RouteGlobal) const
Pressed this frame through ImGui's shortcut router (any slot). Pass ImGuiInputFlags_Repeat for held k...
Definition input_actions.cpp:447
std::uint32_t revision() const
Bumped by every binding change (for cached labels).
Definition input_actions.h:213
void store(ActionId id) const
Definition input_actions.cpp:609
void resetToDefault(ActionId id)
Restores one action's default chords.
Definition input_actions.cpp:549
std::size_t size() const
Definition input_actions.h:153
ImGuiKey key
Definition input_actions.cpp:25
const char * label
Definition input_actions.cpp:27
constexpr std::string_view NAV_PALETTE
Definition input_actions.h:250
constexpr std::string_view SCREENSHOT
Definition input_actions.h:275
constexpr std::string_view PLAY_LOB
Definition input_actions.h:284
constexpr std::size_t SHOUT_COUNT
Definition input_actions.h:289
constexpr std::string_view CAMERA_FREE
Definition input_actions.h:271
std::string shout(std::size_t index)
Definition input_actions.cpp:312
constexpr std::string_view NAV_TRAINING
Definition input_actions.h:246
constexpr std::string_view MATCH_TACTICS
Definition input_actions.h:261
constexpr std::string_view CAREER_SAVE
Definition input_actions.h:255
constexpr std::string_view PLAY_PASS
Definition input_actions.h:281
constexpr std::string_view NAV_FORWARD
Definition input_actions.h:252
constexpr std::string_view MATCH_FULLSCREEN
Definition input_actions.h:265
constexpr std::string_view PLAY_RIGHT
Definition input_actions.h:280
constexpr std::string_view NAV_CLOSE
Definition input_actions.h:253
constexpr std::string_view NAV_SQUAD
Definition input_actions.h:245
constexpr std::string_view MATCH_BACK
Definition input_actions.h:264
constexpr std::string_view MATCH_FASTER
Definition input_actions.h:258
constexpr std::string_view CAREER_HELP
Definition input_actions.h:256
constexpr std::string_view CAMERA_DIRECTOR
Definition input_actions.h:272
constexpr std::string_view MATCH_SLOWER
Definition input_actions.h:259
constexpr std::string_view CAMERA_END
Definition input_actions.h:269
constexpr std::string_view CAMERA_PLAYER
Definition input_actions.h:270
constexpr std::string_view NAV_RECRUITMENT
Definition input_actions.h:248
constexpr std::string_view PLAY_PAUSE
Definition input_actions.h:287
constexpr std::string_view NAV_CLUB
Definition input_actions.h:249
constexpr std::string_view NAV_MATCHES
Definition input_actions.h:247
constexpr std::string_view PLAY_UP
Definition input_actions.h:277
constexpr std::string_view PLAY_SHOOT
Definition input_actions.h:282
constexpr std::string_view MATCH_MUTE
Definition input_actions.h:262
constexpr std::string_view MATCH_PAUSE
Definition input_actions.h:257
constexpr std::string_view CAMERA_TACTICAL
Definition input_actions.h:268
constexpr std::string_view CAMERA_BROADCAST
Definition input_actions.h:267
constexpr std::string_view CAMERA_VIEW_TOGGLE
Definition input_actions.h:266
constexpr std::string_view NAV_HOME
Definition input_actions.h:243
constexpr std::string_view MATCH_PITCH_FOCUS
Definition input_actions.h:263
constexpr std::string_view CAMERA_RESET
Definition input_actions.h:274
constexpr std::string_view PLAY_SWITCH
Definition input_actions.h:285
constexpr std::string_view PLAY_LEFT
Definition input_actions.h:279
constexpr std::string_view NAV_INBOX
Definition input_actions.h:244
constexpr std::string_view CAREER_CONTINUE
Definition input_actions.h:254
constexpr std::string_view MATCH_SUBSTITUTIONS
Definition input_actions.h:260
constexpr std::string_view CAMERA_FOLLOW_BALL
Definition input_actions.h:273
constexpr std::string_view PLAY_THROUGH
Definition input_actions.h:283
constexpr std::string_view PLAY_DOWN
Definition input_actions.h:278
constexpr std::string_view PLAY_JOCKEY
Definition input_actions.h:286
constexpr std::string_view NAV_BACK
Definition input_actions.h:251
Rebindable keyboard shortcuts.
Definition input_actions.cpp:19
std::array< ImGuiKeyChord, 2 > chordsOfEvent(const SDL_KeyboardEvent &event)
The chord of an SDL key-down event. Letters and digits match both the character and the physical key,...
Definition input_actions.cpp:402
Context
Where an action is listened to (decides what conflicts).
Definition input_actions.h:47
bool isBindableKey(ImGuiKey key)
True for keys that can be bound (letters, digits, F-keys...).
Definition input_actions.cpp:349
std::string chordLabel(ImGuiKeyChord chord)
Name shown to the player ("Ctrl+K", "Alt+Left", "-").
Definition input_actions.cpp:383
ActionRegistry & registry()
The registry of the running game, with the built-in actions.
Definition input_actions.cpp:625
std::string chordToString(ImGuiKeyChord chord)
Stable name of a chord for the settings file ("Ctrl+K").
Definition input_actions.cpp:351
std::optional< ImGuiKeyChord > chordFromString(std::string_view text)
Parses chordToString() output; nullopt when invalid.
Definition input_actions.cpp:361
std::optional< ImGuiKeyChord > capturePressedChord()
The chord pressed this frame, for "press a key" capture: the first bindable key pressed with the modi...
Definition input_actions.cpp:393
Category
Group an action is listed under in Settings > Controls.
Definition input_actions.h:57
const char * categoryKey(Category category)
Language key of a category's heading.
Definition input_actions.cpp:317
bool contextsOverlap(Context first, Context second)
True when two contexts can be live at once.
Definition input_actions.cpp:340
constexpr std::size_t BINDING_SLOTS
Number of chords an action can have (primary, alternate).
Definition input_actions.h:74
std::size_t ActionId
Handle of a registered action (index, stable for the run).
Definition input_actions.h:71
int index
Definition runtime_editor.cpp:94
Description of an action, given once at registration.
Definition input_actions.h:78
bool rebindable
Definition input_actions.h:85
std::string label_key
Definition input_actions.h:80
Context context
Definition input_actions.h:82
ImGuiKeyChord primary
Definition input_actions.h:83
ImGuiKeyChord alternate
Definition input_actions.h:84
std::string id
Definition input_actions.h:79
Category category
Definition input_actions.h:81
Definition input_actions.h:221
void is_transparent
Definition input_actions.h:222
std::size_t operator()(std::string_view text) const
Definition input_actions.h:223
A registered action with its current chords.
Definition input_actions.h:90
std::array< ImGuiKeyChord, BINDING_SLOTS > chords
Definition input_actions.h:92
ActionDef def
Definition input_actions.h:91
Outcome of a rebind request.
Definition input_actions.h:97
bool applied
Definition input_actions.h:98
std::size_t conflict_slot
Definition input_actions.h:101
std::optional< ActionId > conflict
Definition input_actions.h:100