Files
itgmania212121/stepmania/src/InputMapper.cpp
T

296 lines
6.9 KiB
C++
Raw Normal View History

2002-04-16 17:31:00 +00:00
#include "stdafx.h"
/*
-----------------------------------------------------------------------------
Class: InputMapper
Desc: See Header.
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
2002-04-16 17:31:00 +00:00
Chris Danford
-----------------------------------------------------------------------------
*/
#include "InputMapper.h"
#include "IniFile.h"
#include "GameManager.h"
2002-07-23 01:41:40 +00:00
#include "GameState.h"
2002-04-16 17:31:00 +00:00
2002-05-19 01:59:48 +00:00
InputMapper* INPUTMAPPER = NULL; // global and accessable from anywhere in our program
2002-04-16 17:31:00 +00:00
InputMapper::InputMapper()
{
}
InputMapper::~InputMapper()
{
// don't worry about releasing the Song array. Let the OS do it :-)
SaveMappingsToDisk();
}
void InputMapper::ReadMappingsFromDisk()
{
2002-05-27 08:23:27 +00:00
ASSERT( GAMEMAN != NULL );
2002-04-16 17:31:00 +00:00
IniFile ini;
2002-07-23 01:41:40 +00:00
ini.SetPath( GAMESTATE->GetCurrentGameDef()->m_szName + CString("Map.ini") );
2002-04-16 17:31:00 +00:00
if( !ini.ReadFile() ) {
return; // load nothing
2002-06-14 22:25:22 +00:00
//throw RageException( "could not read config file" );
2002-04-16 17:31:00 +00:00
}
2002-07-23 01:41:40 +00:00
IniFile::key* pKey = ini.GetKey( "Input" );
2002-04-16 17:31:00 +00:00
2002-07-23 01:41:40 +00:00
if( pKey == NULL )
return;
for( int i=0; i<pKey->names.GetSize(); i++ )
2002-04-16 17:31:00 +00:00
{
2002-07-23 01:41:40 +00:00
CString name = pKey->names[i];
CString value = pKey->values[i];
2002-04-16 17:31:00 +00:00
2002-07-23 01:41:40 +00:00
GameInput GameI;
GameI.fromString( name );
2002-04-16 17:31:00 +00:00
2002-07-23 01:41:40 +00:00
CStringArray sDeviceInputStrings;
split( value, ",", sDeviceInputStrings, false );
2002-04-16 17:31:00 +00:00
2002-07-23 01:41:40 +00:00
for( int i=0; i<sDeviceInputStrings.GetSize() && i<NUM_GAME_TO_DEVICE_SLOTS; i++ )
{
DeviceInput DeviceI;
DeviceI.fromString( sDeviceInputStrings[i] );
if( !DeviceI.IsBlank() )
SetInputMap( DeviceI, GameI, i );
2002-04-16 17:31:00 +00:00
}
}
}
void InputMapper::SaveMappingsToDisk()
{
IniFile ini;
2002-07-23 01:41:40 +00:00
ini.SetPath( GAMESTATE->GetCurrentGameDef()->m_szName + CString("Map.ini") );
2002-04-16 17:31:00 +00:00
// 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
2002-04-28 20:42:32 +00:00
for( int i=0; i<MAX_INSTRUMENTS; i++ )
2002-04-16 17:31:00 +00:00
{
2002-04-28 20:42:32 +00:00
for( int j=0; j<MAX_INSTRUMENT_BUTTONS; j++ )
2002-04-16 17:31:00 +00:00
{
CString sNameString, sValueString;
GameInput GameI( (InstrumentNumber)i, (InstrumentButton)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();
}
///////////////////////////////////////
// Input mapping stuff
///////////////////////////////////////
void InputMapper::SetInputMap( DeviceInput DeviceI, GameInput GameI, int iSlotIndex, bool bOverrideHardCoded )
{
// remove the old input
ClearFromInputMap( DeviceI );
ClearFromInputMap( GameI, iSlotIndex );
m_GItoDI[GameI.number][GameI.button][iSlotIndex] = DeviceI;
UpdateTempDItoGI();
}
void InputMapper::ClearFromInputMap( DeviceInput DeviceI )
{
// search for where this DeviceI maps to
2002-04-28 20:42:32 +00:00
for( int p=0; p<MAX_INSTRUMENTS; p++ )
2002-04-16 17:31:00 +00:00
{
2002-04-28 20:42:32 +00:00
for( int b=0; b<MAX_INSTRUMENT_BUTTONS; b++ )
2002-04-16 17:31:00 +00:00
{
for( int s=0; s<NUM_GAME_TO_DEVICE_SLOTS; s++ )
{
if( m_GItoDI[p][b][s] == DeviceI )
m_GItoDI[p][b][s].MakeBlank();
}
}
}
UpdateTempDItoGI();
}
void InputMapper::ClearFromInputMap( GameInput GameI, int iSlotIndex )
{
if( GameI.IsBlank() )
return;
m_GItoDI[GameI.number][GameI.button][iSlotIndex].MakeBlank();
UpdateTempDItoGI();
}
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].MakeBlank();
}
}
// repopulate m_tempDItoGI
2002-04-28 20:42:32 +00:00
for( int n=0; n<MAX_INSTRUMENTS; n++ )
2002-04-16 17:31:00 +00:00
{
2002-04-28 20:42:32 +00:00
for( int b=0; b<MAX_INSTRUMENT_BUTTONS; b++ )
2002-04-16 17:31:00 +00:00
{
for( int s=0; s<NUM_GAME_TO_DEVICE_SLOTS; s++ )
{
GameInput GameI( (InstrumentNumber)n, (InstrumentButton)b );
DeviceInput DeviceI = m_GItoDI[n][b][s];
if( DeviceI.IsBlank() )
continue;
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];
2002-06-24 22:04:31 +00:00
return GameI.number != PLAYER_INVALID;
2002-04-16 17:31:00 +00:00
}
bool InputMapper::GameToDevice( GameInput GameI, int iSoltNum, DeviceInput& DeviceI ) // return true if there is a mapping from pad to device
{
DeviceI = m_GItoDI[GameI.number][GameI.button][iSoltNum];
return DeviceI.device != DEVICE_NONE;
}
struct HardCodedMenuKey
{
InputDevice device;
int device_button;
PlayerNumber player_no;
MenuButton menu_button;
};
const HardCodedMenuKey g_HardCodedMenuKeys[] =
{
2002-05-28 20:01:22 +00:00
{ DEVICE_KEYBOARD, DIK_UP, PLAYER_1, MENU_BUTTON_UP },
{ DEVICE_KEYBOARD, DIK_DOWN, PLAYER_1, MENU_BUTTON_DOWN },
{ DEVICE_KEYBOARD, DIK_LEFT, PLAYER_1, MENU_BUTTON_LEFT },
{ DEVICE_KEYBOARD, DIK_RIGHT, PLAYER_1, MENU_BUTTON_RIGHT },
{ DEVICE_KEYBOARD, DIK_RETURN, PLAYER_1, MENU_BUTTON_START },
{ DEVICE_KEYBOARD, DIK_ESCAPE, PLAYER_1, MENU_BUTTON_BACK },
2002-04-16 17:31:00 +00:00
};
const int NUM_HARD_CODED_MENU_KEYS = sizeof(g_HardCodedMenuKeys) / sizeof(HardCodedMenuKey);
MenuInput InputMapper::DeviceToMenu( DeviceInput DeviceI )
{
for( int i=0; i<NUM_HARD_CODED_MENU_KEYS; i++ )
{
if( g_HardCodedMenuKeys[i].device == DeviceI.device &&
g_HardCodedMenuKeys[i].device_button == DeviceI.button )
{
return MenuInput( g_HardCodedMenuKeys[i].player_no, g_HardCodedMenuKeys[i].menu_button );
}
}
2002-06-24 22:04:31 +00:00
return MenuInput( PLAYER_INVALID, MENU_BUTTON_INVALID );
2002-04-16 17:31:00 +00:00
}
DeviceInput InputMapper::MenuToDevice( MenuInput MenuI )
{
for( int i=0; i<NUM_HARD_CODED_MENU_KEYS; i++ )
{
if( g_HardCodedMenuKeys[i].player_no == MenuI.player &&
g_HardCodedMenuKeys[i].menu_button == MenuI.button )
{
return DeviceInput( g_HardCodedMenuKeys[i].device, g_HardCodedMenuKeys[i].device_button );
}
}
return DeviceInput( DEVICE_NONE, -1 );
}
void InputMapper::GameToStyle( GameInput GameI, StyleInput &StyleI )
{
2002-07-23 01:41:40 +00:00
if( GAMESTATE->m_CurStyle == STYLE_NONE )
2002-06-14 22:25:22 +00:00
{
StyleI.MakeBlank();
return;
}
2002-07-23 01:41:40 +00:00
StyleDef* pStyleDef = GAMESTATE->GetCurrentStyleDef();
2002-04-16 17:31:00 +00:00
StyleI = pStyleDef->GameInputToStyleInput( GameI );
}
void InputMapper::GameToMenu( GameInput GameI, MenuInput &MenuI )
{
2002-07-23 01:41:40 +00:00
GameDef* pGameDef = GAMESTATE->GetCurrentGameDef();
2002-04-16 17:31:00 +00:00
MenuI = pGameDef->GameInputToMenuInput( GameI );
}
void InputMapper::StyleToGame( StyleInput StyleI, GameInput &GameI )
{
2002-07-23 01:41:40 +00:00
StyleDef* pStyleDef = GAMESTATE->GetCurrentStyleDef();
2002-04-16 17:31:00 +00:00
GameI = pStyleDef->StyleInputToGameInput( StyleI );
}
void InputMapper::MenuToGame( MenuInput MenuI, GameInput &GameI )
{
2002-07-23 01:41:40 +00:00
GameDef* pGameDef = GAMESTATE->GetCurrentGameDef();
2002-04-16 17:31:00 +00:00
GameI = pGameDef->MenuInputToGameInput( MenuI );
}
bool InputMapper::IsButtonDown( GameInput GameI )
{
for( int i=0; i<NUM_GAME_TO_DEVICE_SLOTS; i++ )
{
DeviceInput DeviceI;
if( GameToDevice( GameI, i, DeviceI ) )
{
2002-05-19 01:59:48 +00:00
if( INPUTMAN->IsBeingPressed( DeviceI ) )
2002-04-16 17:31:00 +00:00
return true;
}
}
return false;
}
bool InputMapper::IsButtonDown( MenuInput MenuI )
{
GameInput GameI;
MenuToGame( MenuI, GameI );
return IsButtonDown( GameI );
}
bool InputMapper::IsButtonDown( StyleInput StyleI )
{
GameInput GameI;
StyleToGame( StyleI, GameI );
return IsButtonDown( GameI );
}