|
| 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 | + |
0 commit comments