From 928e9500064073e39eb6f998391dcd2b5094abf7 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 30 Sep 2006 08:18:36 +0000 Subject: [PATCH] Most of Game is generic, but not all. Split the InputMapper- specific stuff apart into InputScheme. --- stepmania/src/Game.cpp | 8 +++--- stepmania/src/Game.h | 12 ++------- stepmania/src/InputMapper.cpp | 49 ++++++++++++++++++++--------------- stepmania/src/InputMapper.h | 19 ++++++++++++++ 4 files changed, 53 insertions(+), 35 deletions(-) diff --git a/stepmania/src/Game.cpp b/stepmania/src/Game.cpp index 51ce8e59aa..03b6ff5622 100644 --- a/stepmania/src/Game.cpp +++ b/stepmania/src/Game.cpp @@ -4,14 +4,14 @@ #include "RageUtil.h" #include "PrefsManager.h" -int Game::GetNumGameplayButtons() const +int InputScheme::GetNumGameplayButtons() const { int iIndexOfStart = ButtonNameToIndex( "Start" ); ASSERT( iIndexOfStart != GameButton_Invalid ); return iIndexOfStart; } -GameButton Game::ButtonNameToIndex( const RString &sButtonName ) const +GameButton InputScheme::ButtonNameToIndex( const RString &sButtonName ) const { for( int i=0; iGetCurrentGame(); FOREACH_GameController( c ) { - for( int b=0; bm_iButtonsPerController; b++ ) + for( int b=0; bm_iButtonsPerController; b++ ) { - DeviceButton key = pGame->m_iDefaultKeyboardKey[c][b]; + DeviceButton key = m_pInputScheme->m_iDefaultKeyboardKey[c][b]; if( key == NO_DEFAULT_KEY ) continue; DeviceInput DeviceI( DEVICE_KEYBOARD, key ); @@ -498,8 +496,6 @@ void InputMapper::AutoMapJoysticksForCurrentGame() int iNumJoysticksMapped = 0; - const Game* pGame = GAMESTATE->m_pCurGame; - for( unsigned i=0; iStringToGameType(mapping.szGame) ) + if( RString(m_pInputScheme->m_szName).CompareNoCase(mapping.szGame) ) continue; // games don't match RString sDriverRegex = mapping.szDriverRegex; @@ -534,6 +530,18 @@ void InputMapper::AutoMapJoysticksForCurrentGame() } } +void InputMapper::SetInputScheme( const InputScheme *pInputScheme ) +{ + m_pInputScheme = pInputScheme; + + ReadMappingsFromDisk(); +} + +const InputScheme *InputMapper::GetInputScheme() const +{ + return m_pInputScheme; +} + static const RString DEVICE_INPUT_SEPARATOR = ":"; // this isn't used in any key names void InputMapper::ReadMappingsFromDisk() @@ -547,9 +555,7 @@ void InputMapper::ReadMappingsFromDisk() LOG->Trace( "Couldn't open mapping file \"%s\": %s.", SpecialFiles::KEYMAPS_PATH.c_str(), ini.GetError().c_str() ); - const Game *pGame = GAMESTATE->GetCurrentGame(); - - const XNode *Key = ini.GetChild( pGame->m_szName ); + const XNode *Key = ini.GetChild( m_pInputScheme->m_szName ); if( Key ) { @@ -559,7 +565,7 @@ void InputMapper::ReadMappingsFromDisk() const RString &value = i->second; GameInput GameI; - GameI.FromString( pGame, name ); + GameI.FromString( m_pInputScheme, name ); vector sDeviceInputStrings; split( value, DEVICE_INPUT_SEPARATOR, sDeviceInputStrings, false ); @@ -582,18 +588,21 @@ void InputMapper::SaveMappingsToDisk() IniFile ini; ini.ReadFile( SpecialFiles::KEYMAPS_PATH ); - const Game* pGame = GAMESTATE->GetCurrentGame(); - // erase the key so that we overwrite everything for this game - ini.DeleteKey( pGame->m_szName ); + ini.DeleteKey( m_pInputScheme->m_szName ); + + XNode *pKey = ini.GetChild( m_pInputScheme->m_szName ); + if( pKey != NULL ) + ini.RemoveChild( pKey ); + pKey = ini.AppendChild( m_pInputScheme->m_szName ); // iterate over our input map and write all mappings to the ini file FOREACH_GameController( i ) { - for( int j=0; jm_iButtonsPerController; j++ ) + for( int j=0; jm_iButtonsPerController; j++ ) { GameInput GameI( i, (GameButton)j ); - RString sNameString = GameI.ToString( pGame ); + RString sNameString = GameI.ToString( m_pInputScheme ); vector asValues; for( int slot = 0; slot < NUM_USER_GAME_TO_DEVICE_SLOTS; ++slot ) // don't save data from the last (keyboard automap) slot @@ -604,7 +613,7 @@ void InputMapper::SaveMappingsToDisk() RString sValueString = join( DEVICE_INPUT_SEPARATOR, asValues ); - ini.SetValue( pGame->m_szName, sNameString, sValueString ); + pKey->AppendAttr( sNameString, sValueString ); } } @@ -771,8 +780,7 @@ PlayerNumber InputMapper::ControllerToPlayerNumber( GameController controller ) MenuButton InputMapper::GameToMenu( const GameInput &GameI ) { - const Game* pGame = GAMESTATE->GetCurrentGame(); - return pGame->GameInputToMenuButton( GameI ); + return m_pInputScheme->GameInputToMenuButton( GameI ); } void InputMapper::MenuToGame( MenuButton MenuI, PlayerNumber pn, GameInput GameIout[4] ) @@ -783,8 +791,7 @@ void InputMapper::MenuToGame( MenuButton MenuI, PlayerNumber pn, GameInput GameI pn = PLAYER_INVALID; } - const Game* pGame = GAMESTATE->GetCurrentGame(); - pGame->MenuButtonToGameInputs( MenuI, pn, GameIout ); + m_pInputScheme->MenuButtonToGameInputs( MenuI, pn, GameIout ); } diff --git a/stepmania/src/InputMapper.h b/stepmania/src/InputMapper.h index 4be8f59558..8287f199b5 100644 --- a/stepmania/src/InputMapper.h +++ b/stepmania/src/InputMapper.h @@ -12,12 +12,30 @@ const int NUM_GAME_TO_DEVICE_SLOTS = 5; // five device inputs may map to one gam const int NUM_SHOWN_GAME_TO_DEVICE_SLOTS = 3; const int NUM_USER_GAME_TO_DEVICE_SLOTS = 2; +class InputScheme +{ +public: + const char *m_szName; + int m_iButtonsPerController; + char m_szButtonNames[NUM_GameButton][60]; // The name used by the button graphics system. e.g. "left", "right", "middle C", "snare" + GameButton m_DedicatedMenuButton[NUM_MenuButton]; + GameButton m_SecondaryMenuButton[NUM_MenuButton]; + DeviceButton m_iDefaultKeyboardKey[NUM_GameController][NUM_GameButton]; // default keyboard keys only have an effect the current game is this game + + int GetNumGameplayButtons() const; + GameButton ButtonNameToIndex( const RString &sButtonName ) const; + MenuButton GameInputToMenuButton( GameInput GameI ) const; + void MenuButtonToGameInputs( MenuButton MenuI, PlayerNumber pn, GameInput GameIout[4] ) const; +}; + class InputMapper { public: InputMapper(); ~InputMapper(); + void SetInputScheme( const InputScheme *pInputScheme ); + const InputScheme *GetInputScheme() const; void ReadMappingsFromDisk(); void SaveMappingsToDisk(); @@ -82,6 +100,7 @@ protected: DeviceInput m_GItoDI[NUM_GameController][NUM_GameButton][NUM_GAME_TO_DEVICE_SLOTS]; void UpdateTempDItoGI(); + const InputScheme *m_pInputScheme; };