move CodeItem into InputQueue

rename to InputQueueCode
This commit is contained in:
Glenn Maynard
2007-03-15 20:44:18 +00:00
parent ca8f64a18f
commit dc8a6ee910
6 changed files with 202 additions and 202 deletions
+2 -180
View File
@@ -3,14 +3,12 @@
#include "PlayerOptions.h"
#include "GameState.h"
#include "InputQueue.h"
#include "InputMapper.h"
#include "ThemeManager.h"
#include "RageLog.h"
#include "Game.h"
#include "RageUtil.h"
#include "PlayerState.h"
#include "InputEventPlus.h"
#include "Foreach.h"
const char *CodeNames[] = {
"Easier1",
@@ -55,183 +53,7 @@ const char *CodeNames[] = {
};
XToString( Code );
static CodeItem g_CodeItems[NUM_Code];
static const float g_fSimultaneousThreshold = 0.05f;
bool CodeItem::EnteredCode( GameController controller ) const
{
if( controller == GameController_Invalid )
return false;
if( m_aPresses.size() == 0 )
return false;
RageTimer OldestTimeAllowed;
OldestTimeAllowed += -fMaxSecondsBack;
// iterate newest to oldest
int iSequenceIndex = m_aPresses.size()-1; // count down
const vector<InputEventPlus> &aQueue = INPUTQUEUE->GetQueue( controller );
int iQueueIndex = aQueue.size()-1;
while( iQueueIndex >= 0 )
{
{
const InputEventPlus &iep = aQueue[iQueueIndex];
if( iep.DeviceI.ts < OldestTimeAllowed )
return false;
}
/* Search backwards for all of Press.m_aButtonsToPress pressed within g_fTapThreshold seconds
* with m_aButtonsToHold pressed. */
const ButtonPress &Press = m_aPresses[iSequenceIndex];
bool bMatched = false;
int iMinSearchIndexUsed = iQueueIndex;
for( int b=0; b<(int) Press.m_aButtonsToPress.size(); ++b )
{
/* Search backwards for the buttons in this tap, within the tap threshold since iQueueIndex. */
RageTimer OldestTimeAllowedForTap( aQueue[iQueueIndex].DeviceI.ts );
OldestTimeAllowedForTap += -g_fSimultaneousThreshold;
const InputEventPlus *pIEP = NULL;
int iQueueSearchIndex = iQueueIndex;
for( ; iQueueSearchIndex>=0; --iQueueSearchIndex ) // iterate newest to oldest
{
const InputEventPlus &iep = aQueue[iQueueSearchIndex];
if( iep.DeviceI.ts < OldestTimeAllowedForTap ) // buttons are too old. Stop searching because we're not going to find a match
break;
if( iep.GameI.button == Press.m_aButtonsToPress[b] )
{
pIEP = &iep;
break;
}
}
if( pIEP == NULL )
break; // didn't find the button
// Check that m_aButtonsToHold were being held when the buttons were pressed.
bool bAllButtonsPressed = true;
for( unsigned i=0; i<Press.m_aButtonsToHold.size(); i++ )
{
GameInput gi( controller, Press.m_aButtonsToHold[i] );
if( !INPUTMAPPER->IsBeingPressed(gi, MultiPlayer_Invalid, &pIEP->InputList) )
bAllButtonsPressed = false;
}
if( !bAllButtonsPressed )
continue;
if( b == (int) Press.m_aButtonsToPress.size()-1 )
{
bMatched = true;
iMinSearchIndexUsed = min( iMinSearchIndexUsed, iQueueSearchIndex );
}
}
if( !bMatched )
{
/* The press wasn't matched. If m_bAllowIntermediatePresses is true,
* skip the last press, and look again. */
if( !Press.m_bAllowIntermediatePresses )
return false;
--iQueueIndex;
continue;
}
if( iSequenceIndex == 0 )
{
// we matched the whole pattern. Empty the queue so we don't match on it again.
INPUTQUEUE->ClearQueue( controller );
return true;
}
/* The press was matched. */
iQueueIndex = iMinSearchIndexUsed - 1;
--iSequenceIndex;
}
return false;
}
bool CodeItem::Load( RString sButtonsNames )
{
m_aPresses.clear();
vector<RString> asButtonNames;
bool bHasAPlus = sButtonsNames.find( '+' ) != string::npos;
bool bHasADash = sButtonsNames.find( '-' ) != string::npos;
if( bHasAPlus )
{
// press all buttons simultaneously
split( sButtonsNames, "+", asButtonNames, false );
}
else if( bHasADash )
{
// hold the first iNumButtons-1 buttons, then press the last
split( sButtonsNames, "-", asButtonNames, false );
}
else
{
// press the buttons in sequence
split( sButtonsNames, ",", asButtonNames, false );
}
if( asButtonNames.size() < 1 )
{
if( sButtonsNames != "" )
LOG->Trace( "Ignoring empty code \"%s\".", sButtonsNames.c_str() );
return false;
}
vector<GameButton> buttons;
for( unsigned i=0; i<asButtonNames.size(); i++ ) // for each button in this code
{
const RString sButtonName = asButtonNames[i];
// Search for the corresponding GameButton
const GameButton gb = INPUTMAPPER->GetInputScheme()->ButtonNameToIndex( sButtonName );
if( gb == GameButton_Invalid )
{
LOG->Trace( "The code \"%s\" contains an unrecognized button \"%s\".", sButtonsNames.c_str(), sButtonName.c_str() );
m_aPresses.clear();
return false;
}
buttons.push_back( gb );
}
if( bHasAPlus )
{
m_aPresses.push_back( ButtonPress() );
FOREACH( GameButton, buttons, gb )
{
m_aPresses.back().m_aButtonsToPress.push_back( *gb );
}
}
else if( bHasADash )
{
m_aPresses.push_back( ButtonPress() );
m_aPresses.back().m_aButtonsToHold.insert( m_aPresses.back().m_aButtonsToHold.begin(), buttons.begin(), buttons.end()-1 );
m_aPresses.back().m_aButtonsToPress.push_back( buttons.back() );
}
else
{
FOREACH( GameButton, buttons, gb )
{
m_aPresses.push_back( ButtonPress() );
m_aPresses.back().m_aButtonsToPress.push_back( *gb );
m_aPresses.back().m_bAllowIntermediatePresses = false;
}
}
if( m_aPresses.size() == 1 )
fMaxSecondsBack = 0.55f;
else
fMaxSecondsBack = (m_aPresses.size()-1)*0.6f;
// if we make it here, we found all the buttons in the code
return true;
}
static InputQueueCode g_CodeItems[NUM_Code];
bool CodeDetector::EnteredCode( GameController controller, Code code )
{
@@ -245,7 +67,7 @@ void CodeDetector::RefreshCacheItems( RString sClass )
sClass = "CodeDetector";
FOREACH_ENUM( Code, c )
{
CodeItem& item = g_CodeItems[c];
InputQueueCode& item = g_CodeItems[c];
const RString sCodeName = CodeToString(c);
const RString sButtonsNames = THEME->GetMetric(sClass,sCodeName);