R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
KeyBindings.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2026
3** r-type
4** File description:
5** KeyBindings - Centralized key binding management
6*/
7
8#pragma once
9
10#include <raylib.h>
11#include <functional>
12#include <string>
13#include <unordered_map>
14
15namespace Input {
16
21 constexpr int GAMEPAD_BUTTON_OFFSET = 1000;
22
26 inline bool IsGamepadBinding(int binding) {
27 return binding >= GAMEPAD_BUTTON_OFFSET;
28 }
29
33 inline int GamepadButtonToBinding(int button) {
34 return GAMEPAD_BUTTON_OFFSET + button;
35 }
36
40 inline int BindingToGamepadButton(int binding) {
41 return binding - GAMEPAD_BUTTON_OFFSET;
42 }
43
47 enum class GameAction {
48 // Movement
49 MOVE_UP,
53
54 // Combat
55 SHOOT,
56
57 // UI/System
60
61 // Menu Navigation
66
67 COUNT // Keep last for iteration
68 };
69
80 public:
85 static KeyBindings &getInstance();
86
87 // Delete copy/move constructors
88 KeyBindings(const KeyBindings &) = delete;
89 KeyBindings &operator=(const KeyBindings &) = delete;
92
96 void ResetToDefaults();
97
103 [[nodiscard]] int GetPrimaryKey(GameAction action) const;
104
110 [[nodiscard]] int GetSecondaryKey(GameAction action) const;
111
117 void SetPrimaryKey(GameAction action, int key);
118
124 void SetSecondaryKey(GameAction action, int key);
125
130 void ClearSecondaryKey(GameAction action);
131
138 [[nodiscard]] bool IsKeyBoundToAction(GameAction action, int key) const;
139
145 [[nodiscard]] static std::string GetKeyName(int key);
146
152 [[nodiscard]] static std::string GetGamepadButtonName(int button);
153
159 [[nodiscard]] static std::string GetBindingName(int binding);
160
166 [[nodiscard]] static std::string GetActionName(GameAction action);
167
172 void SetOnBindingsChanged(std::function<void()> callback);
173
179 bool SaveToFile(const std::string &filepath) const;
180
186 bool LoadFromFile(const std::string &filepath);
187
188 private:
189 KeyBindings();
190 ~KeyBindings() = default;
191
192 struct KeyBinding {
193 int primary{KEY_NULL};
194 int secondary{KEY_NULL};
195 };
196
197 std::unordered_map<GameAction, KeyBinding> _bindings;
198 std::function<void()> _onBindingsChanged;
199
201 };
202
203} // namespace Input
Centralized key binding manager (Singleton)
void SetPrimaryKey(GameAction action, int key)
Set the primary key for an action.
static KeyBindings & getInstance()
Get the singleton instance.
void SetSecondaryKey(GameAction action, int key)
Set the secondary key for an action.
static std::string GetActionName(GameAction action)
Get human-readable name of an action.
static std::string GetGamepadButtonName(int button)
Get human-readable name of a gamepad button.
void ClearSecondaryKey(GameAction action)
Clear the secondary key for an action.
bool IsKeyBoundToAction(GameAction action, int key) const
Check if a key is bound to an action (primary or secondary)
~KeyBindings()=default
KeyBindings(const KeyBindings &)=delete
int GetPrimaryKey(GameAction action) const
Get the primary key for an action.
bool SaveToFile(const std::string &filepath) const
Save bindings to a file.
static std::string GetBindingName(int binding)
Get human-readable name of a binding (keyboard key or gamepad button)
int GetSecondaryKey(GameAction action) const
Get the secondary key for an action.
KeyBindings(KeyBindings &&)=delete
bool LoadFromFile(const std::string &filepath)
Load bindings from a file.
std::function< void()> _onBindingsChanged
KeyBindings & operator=(KeyBindings &&)=delete
KeyBindings & operator=(const KeyBindings &)=delete
void SetOnBindingsChanged(std::function< void()> callback)
Set callback for when bindings change.
static std::string GetKeyName(int key)
Get human-readable name of a key.
void ResetToDefaults()
Reset all bindings to defaults.
std::unordered_map< GameAction, KeyBinding > _bindings
GameAction
Enumeration of all bindable game actions.
constexpr int GAMEPAD_BUTTON_OFFSET
Special constant for gamepad button bindings Gamepad buttons are stored as (GAMEPAD_BUTTON_OFFSET + b...
int BindingToGamepadButton(int binding)
Extract gamepad button from a binding value.
bool IsGamepadBinding(int binding)
Check if a binding value represents a gamepad button.
int GamepadButtonToBinding(int button)
Convert a gamepad button to a binding value.