R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
Input::KeyBindings Class Reference

Centralized key binding manager (Singleton) More...

#include <KeyBindings.hpp>

Collaboration diagram for Input::KeyBindings:
Collaboration graph

Classes

struct  KeyBinding
 

Public Member Functions

 KeyBindings (const KeyBindings &)=delete
 
KeyBindingsoperator= (const KeyBindings &)=delete
 
 KeyBindings (KeyBindings &&)=delete
 
KeyBindingsoperator= (KeyBindings &&)=delete
 
void ResetToDefaults ()
 Reset all bindings to defaults.
 
int GetPrimaryKey (GameAction action) const
 Get the primary key for an action.
 
int GetSecondaryKey (GameAction action) const
 Get the secondary key for an action.
 
void SetPrimaryKey (GameAction action, int key)
 Set the primary key for an action.
 
void SetSecondaryKey (GameAction action, int key)
 Set the secondary key for an action.
 
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)
 
void SetOnBindingsChanged (std::function< void()> callback)
 Set callback for when bindings change.
 
bool SaveToFile (const std::string &filepath) const
 Save bindings to a file.
 
bool LoadFromFile (const std::string &filepath)
 Load bindings from a file.
 

Static Public Member Functions

static KeyBindingsgetInstance ()
 Get the singleton instance.
 
static std::string GetKeyName (int key)
 Get human-readable name of a key.
 
static std::string GetGamepadButtonName (int button)
 Get human-readable name of a gamepad button.
 
static std::string GetBindingName (int binding)
 Get human-readable name of a binding (keyboard key or gamepad button)
 
static std::string GetActionName (GameAction action)
 Get human-readable name of an action.
 

Private Member Functions

 KeyBindings ()
 
 ~KeyBindings ()=default
 
void NotifyBindingsChanged ()
 

Private Attributes

std::unordered_map< GameAction, KeyBinding_bindings
 
std::function< void()> _onBindingsChanged
 

Detailed Description

Centralized key binding manager (Singleton)

Manages all game key bindings with support for:

  • Multiple keys per action (primary/secondary)
  • Runtime remapping
  • Persistence (save/load)
  • Human-readable key names

Definition at line 79 of file KeyBindings.hpp.

Constructor & Destructor Documentation

◆ KeyBindings() [1/3]

Input::KeyBindings::KeyBindings ( const KeyBindings )
delete

◆ KeyBindings() [2/3]

Input::KeyBindings::KeyBindings ( KeyBindings &&  )
delete

◆ KeyBindings() [3/3]

Input::KeyBindings::KeyBindings ( )
private

Definition at line 20 of file KeyBindings.cpp.

References ResetToDefaults().

Here is the call graph for this function:

◆ ~KeyBindings()

Input::KeyBindings::~KeyBindings ( )
privatedefault

Member Function Documentation

◆ ClearSecondaryKey()

void Input::KeyBindings::ClearSecondaryKey ( GameAction  action)

Clear the secondary key for an action.

Parameters
actionThe game action

Definition at line 77 of file KeyBindings.cpp.

References _bindings, and NotifyBindingsChanged().

Referenced by Game::KeyBindingsMenu::CreateBindingRow().

Here is the call graph for this function:

◆ GetActionName()

std::string Input::KeyBindings::GetActionName ( GameAction  action)
static

◆ GetBindingName()

std::string Input::KeyBindings::GetBindingName ( int  binding)
static

Get human-readable name of a binding (keyboard key or gamepad button)

Parameters
bindingThe binding value (may be key or GAMEPAD_BUTTON_OFFSET + button)
Returns
String representation of the binding

Definition at line 228 of file KeyBindings.cpp.

References Input::BindingToGamepadButton(), GetGamepadButtonName(), GetKeyName(), and Input::IsGamepadBinding().

Referenced by Game::KeyBindingsMenu::CreateBindingRow(), and Game::KeyBindingsMenu::UpdateBindingButtonText().

Here is the call graph for this function:

◆ GetGamepadButtonName()

std::string Input::KeyBindings::GetGamepadButtonName ( int  button)
static

Get human-readable name of a gamepad button.

Parameters
buttonThe button code (GAMEPAD_BUTTON_* constant)
Returns
String representation of the button

Definition at line 199 of file KeyBindings.cpp.

Referenced by GetBindingName().

◆ getInstance()

◆ GetKeyName()

std::string Input::KeyBindings::GetKeyName ( int  key)
static

Get human-readable name of a key.

Parameters
keyThe key code
Returns
String representation of the key

Definition at line 90 of file KeyBindings.cpp.

Referenced by GetBindingName(), and Game::KeyBindingsMenu::HandleCapturedKey().

◆ GetPrimaryKey()

int Input::KeyBindings::GetPrimaryKey ( GameAction  action) const

Get the primary key for an action.

Parameters
actionThe game action
Returns
The key code (raylib KEY_* constant)

Definition at line 51 of file KeyBindings.cpp.

References _bindings.

◆ GetSecondaryKey()

int Input::KeyBindings::GetSecondaryKey ( GameAction  action) const

Get the secondary key for an action.

Parameters
actionThe game action
Returns
The key code, or KEY_NULL if not set

Definition at line 59 of file KeyBindings.cpp.

References _bindings.

◆ IsKeyBoundToAction()

bool Input::KeyBindings::IsKeyBoundToAction ( GameAction  action,
int  key 
) const

Check if a key is bound to an action (primary or secondary)

Parameters
actionThe game action to check
keyThe key code to check
Returns
true if the key is bound to this action

Definition at line 82 of file KeyBindings.cpp.

References _bindings.

◆ LoadFromFile()

bool Input::KeyBindings::LoadFromFile ( const std::string &  filepath)

Load bindings from a file.

Parameters
filepathPath to load file
Returns
true if successful

Definition at line 285 of file KeyBindings.cpp.

References _bindings, LOG_INFO, LOG_WARNING, and NotifyBindingsChanged().

Referenced by Rendering::InitializeMenus().

Here is the call graph for this function:

◆ NotifyBindingsChanged()

void Input::KeyBindings::NotifyBindingsChanged ( )
private

◆ operator=() [1/2]

KeyBindings & Input::KeyBindings::operator= ( const KeyBindings )
delete

◆ operator=() [2/2]

KeyBindings & Input::KeyBindings::operator= ( KeyBindings &&  )
delete

◆ ResetToDefaults()

void Input::KeyBindings::ResetToDefaults ( )

◆ SaveToFile()

bool Input::KeyBindings::SaveToFile ( const std::string &  filepath) const

Save bindings to a file.

Parameters
filepathPath to save file
Returns
true if successful

Definition at line 270 of file KeyBindings.cpp.

References _bindings, LOG_ERROR, and LOG_INFO.

Referenced by Rendering::InitializeKeyBindingsMenu().

◆ SetOnBindingsChanged()

void Input::KeyBindings::SetOnBindingsChanged ( std::function< void()>  callback)

Set callback for when bindings change.

Parameters
callbackFunction to call when any binding changes

Definition at line 260 of file KeyBindings.cpp.

References _onBindingsChanged.

◆ SetPrimaryKey()

void Input::KeyBindings::SetPrimaryKey ( GameAction  action,
int  key 
)

Set the primary key for an action.

Parameters
actionThe game action
keyThe key code to bind

Definition at line 67 of file KeyBindings.cpp.

References _bindings, and NotifyBindingsChanged().

Here is the call graph for this function:

◆ SetSecondaryKey()

void Input::KeyBindings::SetSecondaryKey ( GameAction  action,
int  key 
)

Set the secondary key for an action.

Parameters
actionThe game action
keyThe key code to bind

Definition at line 72 of file KeyBindings.cpp.

References _bindings, and NotifyBindingsChanged().

Here is the call graph for this function:

Member Data Documentation

◆ _bindings

std::unordered_map<GameAction, KeyBinding> Input::KeyBindings::_bindings
private

◆ _onBindingsChanged

std::function<void()> Input::KeyBindings::_onBindingsChanged
private

Definition at line 198 of file KeyBindings.hpp.

Referenced by NotifyBindingsChanged(), and SetOnBindingsChanged().


The documentation for this class was generated from the following files: