#include "global.h" /* ----------------------------------------------------------------------------- Class: InputMapper Desc: See Header. Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved. Chris Danford ----------------------------------------------------------------------------- */ #include "InputMapper.h" #include "IniFile.h" #include "GameManager.h" #include "GameState.h" #include "RageLog.h" #include "InputFilter.h" #include "RageUtil.h" InputMapper* INPUTMAPPER = NULL; // global and accessable from anywhere in our program InputMapper::InputMapper() { } InputMapper::~InputMapper() { SaveMappingsToDisk(); } void InputMapper::ClearAllMappings() { for( int i=0; iGetCurrentGameDef(); for( int c=0; cm_iButtonsPerController; b++ ) { int key = pGameDef->m_iDefaultKeyboardKey[c][b]; if( key == -1 ) // "no key" marker" continue; DeviceInput DeviceI( DEVICE_KEYBOARD, key ); GameInput GameI( (GameController)c, (GameButton)b ); if( !IsMapped(DeviceI) ) // if this key isn't already being used by another user-made mapping SetInputMap( DeviceI, GameI, 2 ); } } } void InputMapper::ReadMappingsFromDisk() { ASSERT( GAMEMAN != NULL ); ClearAllMappings(); IniFile ini; ini.SetPath( "Data/Keymaps.ini" ); if( !ini.ReadFile() ) LOG->Warn( "could not input mapping file \"Keymaps.ini\"." ); const IniFile::key *Key = ini.GetKey( GAMESTATE->GetCurrentGameDef()->m_szName ); if( Key ) { for( IniFile::key::const_iterator i = Key->begin(); i != Key->end(); ++i ) { CString name = i->first; CString value = i->second; GameInput GameI; GameI.fromString( name ); CStringArray sDeviceInputStrings; split( value, ",", sDeviceInputStrings, false ); for( unsigned i=0; iGetCurrentGameDef()->m_szName ); // iterate over our input map and write all mappings to the ini file for( int i=0; iGetCurrentGameDef()->m_szName, sNameString, sValueString ); } } ini.WriteFile(); } void InputMapper::SetInputMap( DeviceInput DeviceI, GameInput GameI, int iSlotIndex ) { // remove the old input ClearFromInputMap( DeviceI ); ClearFromInputMap( GameI, iSlotIndex ); m_GItoDI[GameI.controller][GameI.button][iSlotIndex] = DeviceI; UpdateTempDItoGI(); } void InputMapper::ClearFromInputMap( DeviceInput DeviceI ) { // search for where this DeviceI maps to for( int p=0; pm_CurStyle == STYLE_INVALID ) { StyleI.MakeInvalid(); return; } const StyleDef* pStyleDef = GAMESTATE->GetCurrentStyleDef(); StyleI = pStyleDef->GameInputToStyleInput( GameI ); } void InputMapper::GameToMenu( GameInput GameI, MenuInput &MenuI ) { const GameDef* pGameDef = GAMESTATE->GetCurrentGameDef(); MenuI = pGameDef->GameInputToMenuInput( GameI ); } void InputMapper::StyleToGame( StyleInput StyleI, GameInput &GameI ) { const StyleDef* pStyleDef = GAMESTATE->GetCurrentStyleDef(); GameI = pStyleDef->StyleInputToGameInput( StyleI ); } void InputMapper::MenuToGame( MenuInput MenuI, GameInput GameIout[4] ) { GameDef* pGameDef = GAMESTATE->GetCurrentGameDef(); pGameDef->MenuInputToGameInput( MenuI, GameIout ); } bool InputMapper::IsButtonDown( GameInput GameI ) { for( int i=0; iIsBeingPressed( DeviceI ) ) return true; } return false; } bool InputMapper::IsButtonDown( MenuInput MenuI ) { GameInput GameI[4]; MenuToGame( MenuI, GameI ); for( int i=0; i<4; i++ ) if( GameI[i].IsValid() && IsButtonDown(GameI[i]) ) return true; return false; } bool InputMapper::IsButtonDown( StyleInput StyleI ) { GameInput GameI; StyleToGame( StyleI, GameI ); return IsButtonDown( GameI ); } float InputMapper::GetSecsHeld( GameInput GameI ) { float fMaxSecsHeld = 0; for( int i=0; iGetSecsHeld(DeviceI) ); } return fMaxSecsHeld; } float InputMapper::GetSecsHeld( MenuInput MenuI ) { float fMaxSecsHeld = 0; GameInput GameI[4]; MenuToGame( MenuI, GameI ); for( int i=0; i<4; i++ ) if( GameI[i].IsValid() ) fMaxSecsHeld = max( fMaxSecsHeld, GetSecsHeld(GameI[i]) ); return fMaxSecsHeld; } float InputMapper::GetSecsHeld( StyleInput StyleI ) { GameInput GameI; StyleToGame( StyleI, GameI ); return GetSecsHeld( GameI ); }