Files
itgmania212121/stepmania/src/InputMapper.cpp
T

332 lines
7.6 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
2002-04-16 17:31:00 +00:00
/*
-----------------------------------------------------------------------------
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"
#include "RageLog.h"
2002-09-08 06:11:59 +00:00
#include "InputFilter.h"
#include "RageUtil.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()
{
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 );
}
}
}
2002-04-16 17:31:00 +00:00
void InputMapper::ReadMappingsFromDisk()
{
2002-05-27 08:23:27 +00:00
ASSERT( GAMEMAN != NULL );
2002-04-16 17:31:00 +00:00
ClearAllMappings();
2002-04-16 17:31:00 +00:00
IniFile ini;
2003-04-22 12:16:06 +00:00
ini.SetPath( "Data/Keymaps.ini" );
if( !ini.ReadFile() )
2003-01-18 03:54:42 +00:00
LOG->Warn( "could not input mapping file \"Keymaps.ini\"." );
2002-04-16 17:31:00 +00:00
const IniFile::key *Key = ini.GetKey( GAMESTATE->GetCurrentGameDef()->m_szName );
2002-04-16 17:31:00 +00:00
2003-01-03 22:37:44 +00:00
if( Key )
2002-04-16 17:31:00 +00:00
{
2003-01-03 22:37:44 +00:00
for( IniFile::key::const_iterator i = Key->begin();
i != Key->end(); ++i )
{
2002-10-24 23:08:18 +00:00
CString name = i->first;
CString value = i->second;
2002-04-16 17:31:00 +00:00
GameInput GameI;
GameI.fromString( name );
2002-04-16 17:31:00 +00:00
CStringArray sDeviceInputStrings;
split( value, ",", sDeviceInputStrings, false );
2002-04-16 17:31:00 +00:00
2002-11-16 08:55:46 +00:00
for( unsigned i=0; i<sDeviceInputStrings.size() && i<unsigned(NUM_GAME_TO_DEVICE_SLOTS); i++ )
{
DeviceInput DeviceI;
DeviceI.fromString( sDeviceInputStrings[i] );
if( DeviceI.IsValid() )
SetInputMap( DeviceI, GameI, i );
}
2002-04-16 17:31:00 +00:00
}
}
AddDefaultMappingsForCurrentGameIfUnmapped();
2002-04-16 17:31:00 +00:00
}
void InputMapper::SaveMappingsToDisk()
{
IniFile ini;
2003-04-22 12:16:06 +00:00
ini.SetPath( "Data/Keymaps.ini" );
ini.ReadFile();
// erase the key so that we overwrite everything for this game
ini.DeleteKey( GAMESTATE->GetCurrentGameDef()->m_szName );
2002-04-16 17:31:00 +00:00
// iterate over our input map and write all mappings to the ini file
2002-07-27 19:29:51 +00:00
for( int i=0; i<MAX_GAME_CONTROLLERS; i++ )
2002-04-16 17:31:00 +00:00
{
2002-07-27 19:29:51 +00:00
for( int j=0; j<MAX_GAME_BUTTONS; j++ )
2002-04-16 17:31:00 +00:00
{
CString sNameString, sValueString;
2002-07-27 19:29:51 +00:00
GameInput GameI( (GameController)i, (GameButton)j );
2002-04-16 17:31:00 +00:00
sNameString = GameI.toString();
sValueString = ssprintf( "%s,%s,%s",
m_GItoDI[i][j][0].toString().GetString(), m_GItoDI[i][j][1].toString().GetString(), m_GItoDI[i][j][2].toString().GetString() );
2002-04-16 17:31:00 +00:00
ini.SetValue( GAMESTATE->GetCurrentGameDef()->m_szName, sNameString, sValueString );
2002-04-16 17:31:00 +00:00
}
}
ini.WriteFile();
}
void InputMapper::SetInputMap( DeviceInput DeviceI, GameInput GameI, int iSlotIndex )
2002-04-16 17:31:00 +00:00
{
// remove the old input
ClearFromInputMap( DeviceI );
ClearFromInputMap( GameI, iSlotIndex );
2002-07-27 19:29:51 +00:00
m_GItoDI[GameI.controller][GameI.button][iSlotIndex] = DeviceI;
2002-04-16 17:31:00 +00:00
UpdateTempDItoGI();
}
void InputMapper::ClearFromInputMap( DeviceInput DeviceI )
{
// search for where this DeviceI maps to
2002-07-27 19:29:51 +00:00
for( int p=0; p<MAX_GAME_CONTROLLERS; p++ )
2002-04-16 17:31:00 +00:00
{
2002-07-27 19:29:51 +00:00
for( int b=0; b<MAX_GAME_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].MakeInvalid();
2002-04-16 17:31:00 +00:00
}
}
}
UpdateTempDItoGI();
}
void InputMapper::ClearFromInputMap( GameInput GameI, int iSlotIndex )
{
if( !GameI.IsValid() )
2002-04-16 17:31:00 +00:00
return;
m_GItoDI[GameI.controller][GameI.button][iSlotIndex].MakeInvalid();
2002-04-16 17:31:00 +00:00
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;
}
2002-04-16 17:31:00 +00:00
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();
2002-04-16 17:31:00 +00:00
}
}
// repopulate m_tempDItoGI
2002-07-27 19:29:51 +00:00
for( int n=0; n<MAX_GAME_CONTROLLERS; n++ )
2002-04-16 17:31:00 +00:00
{
2002-07-27 19:29:51 +00:00
for( int b=0; b<MAX_GAME_BUTTONS; b++ )
2002-04-16 17:31:00 +00:00
{
for( int s=0; s<NUM_GAME_TO_DEVICE_SLOTS; s++ )
{
2002-07-27 19:29:51 +00:00
GameInput GameI( (GameController)n, (GameButton)b );
2002-04-16 17:31:00 +00:00
DeviceInput DeviceI = m_GItoDI[n][b][s];
if( DeviceI.IsValid() )
m_tempDItoGI[DeviceI.device][DeviceI.button] = GameI;
2002-04-16 17:31:00 +00:00
}
}
}
}
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-11-16 08:45:40 +00:00
return GameI.controller != GAME_CONTROLLER_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
{
2002-07-27 19:29:51 +00:00
DeviceI = m_GItoDI[GameI.controller][GameI.button][iSoltNum];
2002-04-16 17:31:00 +00:00
return DeviceI.device != DEVICE_NONE;
}
void InputMapper::GameToStyle( GameInput GameI, StyleInput &StyleI )
{
2003-01-30 07:18:33 +00:00
if( GAMESTATE->m_CurStyle == STYLE_INVALID )
2002-06-14 22:25:22 +00:00
{
StyleI.MakeInvalid();
2002-06-14 22:25:22 +00:00
return;
}
2002-08-22 23:36:03 +00:00
const StyleDef* pStyleDef = GAMESTATE->GetCurrentStyleDef();
2002-04-16 17:31:00 +00:00
StyleI = pStyleDef->GameInputToStyleInput( GameI );
}
void InputMapper::GameToMenu( GameInput GameI, MenuInput &MenuI )
{
2002-08-22 23:36:03 +00:00
const GameDef* pGameDef = GAMESTATE->GetCurrentGameDef();
2002-04-16 17:31:00 +00:00
MenuI = pGameDef->GameInputToMenuInput( GameI );
}
void InputMapper::StyleToGame( StyleInput StyleI, GameInput &GameI )
{
2002-08-22 23:36:03 +00:00
const StyleDef* pStyleDef = GAMESTATE->GetCurrentStyleDef();
2002-04-16 17:31:00 +00:00
GameI = pStyleDef->StyleInputToGameInput( StyleI );
}
void InputMapper::MenuToGame( MenuInput MenuI, GameInput GameIout[4] )
2002-04-16 17:31:00 +00:00
{
2002-07-23 01:41:40 +00:00
GameDef* pGameDef = GAMESTATE->GetCurrentGameDef();
pGameDef->MenuInputToGameInput( MenuI, GameIout );
2002-04-16 17:31:00 +00:00
}
2002-04-16 17:31:00 +00:00
bool InputMapper::IsButtonDown( GameInput GameI )
{
for( int i=0; i<NUM_GAME_TO_DEVICE_SLOTS; i++ )
{
DeviceInput DeviceI;
if( GameToDevice( GameI, i, DeviceI ) )
2002-09-08 06:11:59 +00:00
if( INPUTFILTER->IsBeingPressed( DeviceI ) )
2002-04-16 17:31:00 +00:00
return true;
}
return false;
}
bool InputMapper::IsButtonDown( MenuInput MenuI )
{
GameInput GameI[4];
2002-04-16 17:31:00 +00:00
MenuToGame( MenuI, GameI );
for( int i=0; i<4; i++ )
if( GameI[i].IsValid() && IsButtonDown(GameI[i]) )
return true;
return false;
2002-04-16 17:31:00 +00:00
}
bool InputMapper::IsButtonDown( StyleInput StyleI )
{
GameInput GameI;
StyleToGame( StyleI, GameI );
return IsButtonDown( GameI );
}
2002-10-18 19:01:32 +00:00
float InputMapper::GetSecsHeld( GameInput GameI )
{
float fMaxSecsHeld = 0;
for( int i=0; i<NUM_GAME_TO_DEVICE_SLOTS; i++ )
{
DeviceInput DeviceI;
if( GameToDevice( GameI, i, DeviceI ) )
fMaxSecsHeld = max( fMaxSecsHeld, INPUTFILTER->GetSecsHeld(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 );
}