Skip to content

Commit 0b8cbcf

Browse files
committed
Created contexts
1 parent 92eb077 commit 0b8cbcf

File tree

6 files changed

+287
-154
lines changed

6 files changed

+287
-154
lines changed

BindContext.cpp

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include "BindContext.h"
2+
#include <SDL2/SDL_keyboard.h>
3+
#include <utility/Config.h>
4+
#include "LogInput.h"
5+
6+
BindContext::BindContext( const pString& name )
7+
: m_Name( name ) {
8+
}
9+
10+
void BindContext::LoadFromConfig( Config& cfg, const pVector<pString> descriptions ) {
11+
for ( auto& title : m_ActionTitleToAction ) {
12+
pString keyName = cfg.GetString( m_Name + ".primary." + title.first, SDL_GetScancodeName( title.second.DefaultScancode ),
13+
descriptions.at( static_cast<int>( title.second.Action ) ) );
14+
if ( keyName != "" ) {
15+
SDL_Scancode scanCode = SDL_GetScancodeFromName( keyName.c_str() );
16+
if ( scanCode == SDL_SCANCODE_UNKNOWN ) {
17+
LogInput( "Failed to interpret " + keyName + " as a scancode", "KeyBindings", LogSeverity::WARNING_MSG );
18+
} else {
19+
m_KeyBindingCollection.AddMappingWithScancode( scanCode, title.second.Action );
20+
}
21+
}
22+
}
23+
for ( auto& title : m_ActionTitleToAction ) {
24+
pString keyName = cfg.GetString( m_Name + ".secondary." + title.first, "", descriptions.at( static_cast<int>( title.second.Action ) ) );
25+
if ( keyName != "" ) {
26+
SDL_Scancode scanCode = SDL_GetScancodeFromName( keyName.c_str() );
27+
if ( scanCode == SDL_SCANCODE_UNKNOWN ) {
28+
LogInput( "Failed to interpret " + keyName + " as a scancode", "KeyBindings", LogSeverity::WARNING_MSG );
29+
} else {
30+
m_KeyBindingCollection.AddMappingWithScancode( scanCode, title.second.Action );
31+
}
32+
}
33+
}
34+
for ( auto& title : m_ActionTitleToAction ) {
35+
pString buttonName =
36+
cfg.GetString( m_Name + ".gamepad." + title.first, title.second.DefaultButton != SDL_CONTROLLER_BUTTON_INVALID ? SDL_GameControllerGetStringForButton(
37+
title.second.DefaultButton ) : "",
38+
descriptions.at( static_cast<int>( title.second.Action ) ) );
39+
if ( buttonName != "" ) {
40+
SDL_GameControllerButton button = SDL_GameControllerGetButtonFromString( buttonName.c_str() );
41+
if ( button == SDL_CONTROLLER_BUTTON_INVALID ) {
42+
LogInput( "Failed to interpret " + buttonName + " as a button", "KeyBindings", LogSeverity::WARNING_MSG );
43+
} else {
44+
m_GamepadBindingCollection.AddMappingWithButton( button, title.second.Action );
45+
}
46+
}
47+
}
48+
}
49+
50+
void BindContext::SaveToConfig( Config& cfg ) const {
51+
for ( auto& keybinding : m_ActionTitleToAction ) {
52+
SDL_Scancode primary = m_KeyBindingCollection.GetPrimaryScancodeFromAction( keybinding.second.Action );
53+
cfg.SetString( m_Name + ".primary." + keybinding.first, SDL_GetScancodeName( primary ) );
54+
}
55+
for ( auto& keybinding : m_ActionTitleToAction ) {
56+
SDL_Scancode secondary = m_KeyBindingCollection.GetSecondaryScancodeFromAction( keybinding.second.Action );
57+
cfg.SetString( m_Name + ".secondary." + keybinding.first, SDL_GetScancodeName( secondary ) );
58+
}
59+
for ( auto& buttonBinding : m_ActionTitleToAction ) {
60+
SDL_GameControllerButton button = m_GamepadBindingCollection.GetButtonFromAction( buttonBinding.second.Action );
61+
cfg.SetString( m_Name + ".gamepad." + buttonBinding.first, SDL_GameControllerGetStringForButton( button ) );
62+
}
63+
}
64+
65+
void BindContext::ClearBindings() {
66+
m_KeyBindingCollection = KeyBindingCollection();
67+
m_GamepadBindingCollection = GamepadBindingCollection();
68+
}
69+
70+
void BindContext::ClearActions() {
71+
ClearBindings();
72+
m_ActionTitleToAction.clear();
73+
}
74+
75+
void BindContext::AddAction( ActionIdentifier actionIdentifier, const pString& name, SDL_Scancode defaultScancode, SDL_GameControllerButton defaultButton ) {
76+
m_ActionTitleToAction[name] = ActionTitleMapping {
77+
actionIdentifier, defaultScancode, defaultButton
78+
};
79+
}
80+
81+
void BindContext::GetDefaultKeyBindings( KeyBindingCollection& collection ) const {
82+
collection = KeyBindingCollection();
83+
84+
for ( auto& title : m_ActionTitleToAction ) {
85+
collection.AddMappingWithScancode( title.second.DefaultScancode, title.second.Action );
86+
}
87+
}
88+
89+
void BindContext::GetDefaultGamepadBindings( GamepadBindingCollection& collection ) const {
90+
collection = GamepadBindingCollection();
91+
92+
for ( auto& title : m_ActionTitleToAction ) {
93+
collection.AddMappingWithButton( title.second.DefaultButton, title.second.Action );
94+
}
95+
}
96+
97+
const rMap<rString, BindContext::ActionTitleMapping>& BindContext::GetActionTitleToAction() const {
98+
return m_ActionTitleToAction;
99+
}
100+
101+
const KeyBindingCollection& BindContext::GetKeyBindCollection( ) const {
102+
return m_KeyBindingCollection;
103+
}
104+
105+
KeyBindingCollection& BindContext::GetEditableKeyBindCollection( ) {
106+
return m_KeyBindingCollection;
107+
}
108+
109+
void BindContext::SetKeyBindingCollection( const KeyBindingCollection& collection ) {
110+
m_KeyBindingCollection = collection;
111+
}
112+
113+
const GamepadBindingCollection& BindContext::GetGamepadBindCollection( ) const {
114+
return m_GamepadBindingCollection;
115+
}
116+
117+
GamepadBindingCollection& BindContext::GetEditableGamepadBindCollection( ) {
118+
return m_GamepadBindingCollection;
119+
}
120+
121+
void BindContext::SetGamepadBindingCollection( const GamepadBindingCollection& collection ) {
122+
m_GamepadBindingCollection = collection;
123+
}
124+

BindContext.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
#include <SDL2/SDL_scancode.h>
3+
#include <SDL2/SDL_gamecontroller.h>
4+
#include "Types.h"
5+
#include "KeyBindingCollection.h"
6+
#include "GamepadBindingCollection.h"
7+
8+
class Config;
9+
10+
class BindContext {
11+
public:
12+
struct ActionTitleMapping;
13+
14+
INPUT_API BindContext( const pString& name );
15+
16+
INPUT_API void LoadFromConfig( Config& cfg, const pVector<pString> descriptions );
17+
INPUT_API void SaveToConfig( Config& cfg ) const;
18+
INPUT_API void ClearBindings();
19+
INPUT_API void ClearActions();
20+
INPUT_API void AddAction( ActionIdentifier actionIdentifier, const pString &name, SDL_Scancode scancode, SDL_GameControllerButton = SDL_CONTROLLER_BUTTON_INVALID );
21+
22+
INPUT_API void GetDefaultKeyBindings ( KeyBindingCollection& collection ) const;
23+
INPUT_API void GetDefaultGamepadBindings ( GamepadBindingCollection& collection ) const;
24+
INPUT_API const rMap<rString, ActionTitleMapping>& GetActionTitleToAction () const;
25+
INPUT_API const KeyBindingCollection& GetKeyBindCollection () const;
26+
INPUT_API KeyBindingCollection& GetEditableKeyBindCollection ();
27+
INPUT_API void SetKeyBindingCollection ( const KeyBindingCollection& collection );
28+
INPUT_API const GamepadBindingCollection& GetGamepadBindCollection () const;
29+
INPUT_API GamepadBindingCollection& GetEditableGamepadBindCollection ();
30+
INPUT_API void SetGamepadBindingCollection ( const GamepadBindingCollection& collection );
31+
32+
struct ActionTitleMapping {
33+
ActionIdentifier Action;
34+
SDL_Scancode DefaultScancode;
35+
SDL_GameControllerButton DefaultButton;
36+
};
37+
38+
private:
39+
pString m_Name;
40+
pMap<pString, ActionTitleMapping> m_ActionTitleToAction;
41+
KeyBindingCollection m_KeyBindingCollection;
42+
GamepadBindingCollection m_GamepadBindingCollection;
43+
};

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ file(GLOB_RECURSE InputSources
1515
"KeyBindingCollection.cpp"
1616
"GamepadBindingCollection.h"
1717
"GamepadBindingCollection.cpp"
18+
"BindContext.h"
19+
"BindContext.cpp"
1820
"Typedefs.h"
1921
"LogInput.h"
2022
)

0 commit comments

Comments
 (0)