Files
itgmania212121/stepmania/src/InputMapper.cpp
T
2002-08-22 23:36:03 +00:00

295 lines
6.8 KiB
C++

#include "stdafx.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"
InputMapper* INPUTMAPPER = NULL; // global and accessable from anywhere in our program
InputMapper::InputMapper()
{
}
InputMapper::~InputMapper()
{
SaveMappingsToDisk();
}
void InputMapper::ClearAllMappings()
{
for( int i=0; i<MAX_GAME_CONTROLLERS; i++ )
for( int j=0; j<MAX_GAME_BUTTONS; j++ )
for( int k=0; k<NUM_GAME_TO_DEVICE_SLOTS; k++ )
m_GItoDI[i][j][k].MakeInvalid();
}
void InputMapper::AddDefaultMappingsForCurrentGameIfUnmapped()
{
// Clear default mappings. Default mappings are in the third slot.
for( int i=0; i<MAX_GAME_CONTROLLERS; i++ )
for( int j=0; j<MAX_GAME_BUTTONS; j++ )
ClearFromInputMap( GameInput((GameController)i,(GameButton)j), 2 );
GameDef* pGameDef = GAMESTATE->GetCurrentGameDef();
for( int c=0; c<MAX_GAME_CONTROLLERS; c++ )
{
for( int b=0; b<pGameDef->m_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();
CString sPath = GAMESTATE->GetCurrentGameDef()->m_szName + CString("Map.ini");
IniFile ini;
ini.SetPath( sPath );
if( !ini.ReadFile() )
LOG->Warn( "could not input mapping file '%s'.", sPath );
IniFile::key* pKey = ini.GetKey( "Input" );
if( pKey != NULL )
{
for( int i=0; i<pKey->names.GetSize(); i++ )
{
CString name = pKey->names[i];
CString value = pKey->values[i];
GameInput GameI;
GameI.fromString( name );
CStringArray sDeviceInputStrings;
split( value, ",", sDeviceInputStrings, false );
for( int i=0; i<sDeviceInputStrings.GetSize() && i<NUM_GAME_TO_DEVICE_SLOTS; i++ )
{
DeviceInput DeviceI;
DeviceI.fromString( sDeviceInputStrings[i] );
if( DeviceI.IsValid() )
SetInputMap( DeviceI, GameI, i );
}
}
}
AddDefaultMappingsForCurrentGameIfUnmapped();
}
void InputMapper::SaveMappingsToDisk()
{
IniFile ini;
ini.SetPath( GAMESTATE->GetCurrentGameDef()->m_szName + CString("Map.ini") );
// ini.ReadFile(); // don't read the file so that we overwrite everything there
// iterate over our input map and write all mappings to the ini file
for( int i=0; i<MAX_GAME_CONTROLLERS; i++ )
{
for( int j=0; j<MAX_GAME_BUTTONS; j++ )
{
CString sNameString, sValueString;
GameInput GameI( (GameController)i, (GameButton)j );
sNameString = GameI.toString();
sValueString = ssprintf( "%s,%s,%s",
m_GItoDI[i][j][0].toString(), m_GItoDI[i][j][1].toString(), m_GItoDI[i][j][2].toString() );
ini.SetValue( "Input", 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; p<MAX_GAME_CONTROLLERS; p++ )
{
for( int b=0; b<MAX_GAME_BUTTONS; b++ )
{
for( int s=0; s<NUM_GAME_TO_DEVICE_SLOTS; s++ )
{
if( m_GItoDI[p][b][s] == DeviceI )
m_GItoDI[p][b][s].MakeInvalid();
}
}
}
UpdateTempDItoGI();
}
void InputMapper::ClearFromInputMap( GameInput GameI, int iSlotIndex )
{
if( !GameI.IsValid() )
return;
m_GItoDI[GameI.controller][GameI.button][iSlotIndex].MakeInvalid();
UpdateTempDItoGI();
}
bool InputMapper::IsMapped( DeviceInput DeviceI )
{
return m_tempDItoGI[DeviceI.device][DeviceI.button].IsValid();
}
bool InputMapper::IsMapped( GameInput GameI )
{
for( int i=0; i<NUM_GAME_TO_DEVICE_SLOTS; i++ )
if( m_GItoDI[GameI.controller][GameI.button][i].IsValid() )
return true;
return false;
}
void InputMapper::UpdateTempDItoGI()
{
// clear out m_tempDItoGI
for( int d=0; d<NUM_INPUT_DEVICES; d++ )
{
for( int b=0; b<NUM_DEVICE_BUTTONS; b++ )
{
m_tempDItoGI[d][b].MakeInvalid();
}
}
// repopulate m_tempDItoGI
for( int n=0; n<MAX_GAME_CONTROLLERS; n++ )
{
for( int b=0; b<MAX_GAME_BUTTONS; b++ )
{
for( int s=0; s<NUM_GAME_TO_DEVICE_SLOTS; s++ )
{
GameInput GameI( (GameController)n, (GameButton)b );
DeviceInput DeviceI = m_GItoDI[n][b][s];
if( DeviceI.IsValid() )
m_tempDItoGI[DeviceI.device][DeviceI.button] = GameI;
}
}
}
}
bool InputMapper::DeviceToGame( DeviceInput DeviceI, GameInput& GameI ) // return true if there is a mapping from device to pad
{
GameI = m_tempDItoGI[DeviceI.device][DeviceI.button];
return GameI.controller != PLAYER_INVALID;
}
bool InputMapper::GameToDevice( GameInput GameI, int iSoltNum, DeviceInput& DeviceI ) // return true if there is a mapping from pad to device
{
DeviceI = m_GItoDI[GameI.controller][GameI.button][iSoltNum];
return DeviceI.device != DEVICE_NONE;
}
void InputMapper::GameToStyle( GameInput GameI, StyleInput &StyleI )
{
if( GAMESTATE->m_CurStyle == STYLE_NONE )
{
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; i<NUM_GAME_TO_DEVICE_SLOTS; i++ )
{
DeviceInput DeviceI;
if( GameToDevice( GameI, i, DeviceI ) )
{
if( INPUTMAN->IsBeingPressed( 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 );
}