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
15
namespace
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
,
50
MOVE_DOWN
,
51
MOVE_LEFT
,
52
MOVE_RIGHT
,
53
54
// Combat
55
SHOOT
,
56
57
// UI/System
58
PAUSE_MENU
,
59
CHAT_OPEN
,
60
61
// Menu Navigation
62
MENU_NEXT
,
63
MENU_PREVIOUS
,
64
MENU_CONFIRM
,
65
MENU_BACK
,
66
67
COUNT
// Keep last for iteration
68
};
69
79
class
KeyBindings
{
80
public
:
85
static
KeyBindings
&
getInstance
();
86
87
// Delete copy/move constructors
88
KeyBindings
(
const
KeyBindings
&) =
delete
;
89
KeyBindings
&
operator=
(
const
KeyBindings
&) =
delete
;
90
KeyBindings
(
KeyBindings
&&) =
delete
;
91
KeyBindings
&
operator=
(
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
200
void
NotifyBindingsChanged
();
201
};
202
203
}
// namespace Input
Input::KeyBindings
Centralized key binding manager (Singleton)
Definition
KeyBindings.hpp:79
Input::KeyBindings::SetPrimaryKey
void SetPrimaryKey(GameAction action, int key)
Set the primary key for an action.
Definition
KeyBindings.cpp:67
Input::KeyBindings::getInstance
static KeyBindings & getInstance()
Get the singleton instance.
Definition
KeyBindings.cpp:15
Input::KeyBindings::SetSecondaryKey
void SetSecondaryKey(GameAction action, int key)
Set the secondary key for an action.
Definition
KeyBindings.cpp:72
Input::KeyBindings::GetActionName
static std::string GetActionName(GameAction action)
Get human-readable name of an action.
Definition
KeyBindings.cpp:238
Input::KeyBindings::GetGamepadButtonName
static std::string GetGamepadButtonName(int button)
Get human-readable name of a gamepad button.
Definition
KeyBindings.cpp:199
Input::KeyBindings::ClearSecondaryKey
void ClearSecondaryKey(GameAction action)
Clear the secondary key for an action.
Definition
KeyBindings.cpp:77
Input::KeyBindings::IsKeyBoundToAction
bool IsKeyBoundToAction(GameAction action, int key) const
Check if a key is bound to an action (primary or secondary)
Definition
KeyBindings.cpp:82
Input::KeyBindings::~KeyBindings
~KeyBindings()=default
Input::KeyBindings::KeyBindings
KeyBindings(const KeyBindings &)=delete
Input::KeyBindings::GetPrimaryKey
int GetPrimaryKey(GameAction action) const
Get the primary key for an action.
Definition
KeyBindings.cpp:51
Input::KeyBindings::SaveToFile
bool SaveToFile(const std::string &filepath) const
Save bindings to a file.
Definition
KeyBindings.cpp:270
Input::KeyBindings::GetBindingName
static std::string GetBindingName(int binding)
Get human-readable name of a binding (keyboard key or gamepad button)
Definition
KeyBindings.cpp:228
Input::KeyBindings::GetSecondaryKey
int GetSecondaryKey(GameAction action) const
Get the secondary key for an action.
Definition
KeyBindings.cpp:59
Input::KeyBindings::KeyBindings
KeyBindings(KeyBindings &&)=delete
Input::KeyBindings::LoadFromFile
bool LoadFromFile(const std::string &filepath)
Load bindings from a file.
Definition
KeyBindings.cpp:285
Input::KeyBindings::_onBindingsChanged
std::function< void()> _onBindingsChanged
Definition
KeyBindings.hpp:198
Input::KeyBindings::operator=
KeyBindings & operator=(KeyBindings &&)=delete
Input::KeyBindings::NotifyBindingsChanged
void NotifyBindingsChanged()
Definition
KeyBindings.cpp:264
Input::KeyBindings::operator=
KeyBindings & operator=(const KeyBindings &)=delete
Input::KeyBindings::SetOnBindingsChanged
void SetOnBindingsChanged(std::function< void()> callback)
Set callback for when bindings change.
Definition
KeyBindings.cpp:260
Input::KeyBindings::GetKeyName
static std::string GetKeyName(int key)
Get human-readable name of a key.
Definition
KeyBindings.cpp:90
Input::KeyBindings::ResetToDefaults
void ResetToDefaults()
Reset all bindings to defaults.
Definition
KeyBindings.cpp:24
Input::KeyBindings::KeyBindings
KeyBindings()
Definition
KeyBindings.cpp:20
Input::KeyBindings::_bindings
std::unordered_map< GameAction, KeyBinding > _bindings
Definition
KeyBindings.hpp:197
Input
Definition
KeyBindings.cpp:13
Input::GameAction
GameAction
Enumeration of all bindable game actions.
Definition
KeyBindings.hpp:47
Input::GameAction::SHOOT
@ SHOOT
Input::GameAction::CHAT_OPEN
@ CHAT_OPEN
Input::GameAction::MENU_CONFIRM
@ MENU_CONFIRM
Input::GameAction::MOVE_RIGHT
@ MOVE_RIGHT
Input::GameAction::COUNT
@ COUNT
Input::GameAction::PAUSE_MENU
@ PAUSE_MENU
Input::GameAction::MOVE_DOWN
@ MOVE_DOWN
Input::GameAction::MENU_PREVIOUS
@ MENU_PREVIOUS
Input::GameAction::MOVE_LEFT
@ MOVE_LEFT
Input::GameAction::MOVE_UP
@ MOVE_UP
Input::GameAction::MENU_BACK
@ MENU_BACK
Input::GameAction::MENU_NEXT
@ MENU_NEXT
Input::GAMEPAD_BUTTON_OFFSET
constexpr int GAMEPAD_BUTTON_OFFSET
Special constant for gamepad button bindings Gamepad buttons are stored as (GAMEPAD_BUTTON_OFFSET + b...
Definition
KeyBindings.hpp:21
Input::BindingToGamepadButton
int BindingToGamepadButton(int binding)
Extract gamepad button from a binding value.
Definition
KeyBindings.hpp:40
Input::IsGamepadBinding
bool IsGamepadBinding(int binding)
Check if a binding value represents a gamepad button.
Definition
KeyBindings.hpp:26
Input::GamepadButtonToBinding
int GamepadButtonToBinding(int button)
Convert a gamepad button to a binding value.
Definition
KeyBindings.hpp:33
Input::KeyBindings::KeyBinding
Definition
KeyBindings.hpp:192
Input::KeyBindings::KeyBinding::secondary
int secondary
Definition
KeyBindings.hpp:194
Input::KeyBindings::KeyBinding::primary
int primary
Definition
KeyBindings.hpp:193
client
Input
KeyBindings.hpp
Generated by
1.9.8