From dc8a6ee9107e6581ef6e95b337bb420a004766a7 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 15 Mar 2007 20:44:18 +0000 Subject: [PATCH] move CodeItem into InputQueue rename to InputQueueCode --- stepmania/src/CodeDetector.cpp | 182 +-------------------------------- stepmania/src/CodeDetector.h | 19 ---- stepmania/src/InputQueue.cpp | 179 ++++++++++++++++++++++++++++++++ stepmania/src/InputQueue.h | 18 ++++ stepmania/src/ScreenSelect.cpp | 2 +- stepmania/src/ScreenSelect.h | 4 +- 6 files changed, 202 insertions(+), 202 deletions(-) diff --git a/stepmania/src/CodeDetector.cpp b/stepmania/src/CodeDetector.cpp index 7649dede25..dffa3da208 100644 --- a/stepmania/src/CodeDetector.cpp +++ b/stepmania/src/CodeDetector.cpp @@ -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 &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; iIsBeingPressed(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 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 buttons; - for( unsigned i=0; iGetInputScheme()->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); diff --git a/stepmania/src/CodeDetector.h b/stepmania/src/CodeDetector.h index 3c61f9f108..1e8e8353f6 100644 --- a/stepmania/src/CodeDetector.h +++ b/stepmania/src/CodeDetector.h @@ -48,25 +48,6 @@ enum Code { NUM_Code // leave this at the end }; -struct CodeItem -{ -public: - bool Load( RString sButtonsNames ); - bool EnteredCode( GameController controller ) const; - -private: - struct ButtonPress - { - ButtonPress() { m_bAllowIntermediatePresses = true; } - vector m_aButtonsToHold; - vector m_aButtonsToPress; - bool m_bAllowIntermediatePresses; - }; - vector m_aPresses; - - float fMaxSecondsBack; -}; - class CodeDetector { public: diff --git a/stepmania/src/InputQueue.cpp b/stepmania/src/InputQueue.cpp index 60b2821006..6ed15807c4 100644 --- a/stepmania/src/InputQueue.cpp +++ b/stepmania/src/InputQueue.cpp @@ -1,7 +1,10 @@ #include "global.h" #include "InputQueue.h" #include "RageTimer.h" +#include "RageLog.h" #include "InputEventPlus.h" +#include "Foreach.h" +#include "InputMapper.h" InputQueue* INPUTQUEUE = NULL; // global and accessable from anywhere in our program @@ -91,6 +94,182 @@ void InputQueue::ClearQueue( GameController c ) m_aQueue[c].clear(); } +static const float g_fSimultaneousThreshold = 0.05f; + +bool InputQueueCode::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 &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; iIsBeingPressed(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 InputQueueCode::Load( RString sButtonsNames ) +{ + m_aPresses.clear(); + + vector 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 buttons; + for( unsigned i=0; iGetInputScheme()->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; +} + /* * (c) 2001-2003 Chris Danford * All rights reserved. diff --git a/stepmania/src/InputQueue.h b/stepmania/src/InputQueue.h index 5058a61aa8..18db7c5dde 100644 --- a/stepmania/src/InputQueue.h +++ b/stepmania/src/InputQueue.h @@ -25,6 +25,24 @@ protected: vector m_aQueue[NUM_GameController]; }; +struct InputQueueCode +{ +public: + bool Load( RString sButtonsNames ); + bool EnteredCode( GameController controller ) const; + +private: + struct ButtonPress + { + ButtonPress() { m_bAllowIntermediatePresses = true; } + vector m_aButtonsToHold; + vector m_aButtonsToPress; + bool m_bAllowIntermediatePresses; + }; + vector m_aPresses; + + float fMaxSecondsBack; +}; extern InputQueue* INPUTQUEUE; // global and accessable from anywhere in our program diff --git a/stepmania/src/ScreenSelect.cpp b/stepmania/src/ScreenSelect.cpp index 93535f45dc..aa3cd84a95 100644 --- a/stepmania/src/ScreenSelect.cpp +++ b/stepmania/src/ScreenSelect.cpp @@ -68,7 +68,7 @@ void ScreenSelect::Init() { RString sCodeName = vsCodeNames[c]; - CodeItem code; + InputQueueCode code; if( !code.Load( CODE(sCodeName) ) ) continue; diff --git a/stepmania/src/ScreenSelect.h b/stepmania/src/ScreenSelect.h index 701e41c677..29d07aab71 100644 --- a/stepmania/src/ScreenSelect.h +++ b/stepmania/src/ScreenSelect.h @@ -5,7 +5,7 @@ #include "ScreenWithMenuElements.h" #include "GameCommand.h" -#include "CodeDetector.h" +#include "InputQueue.h" #include "ThemeMetric.h" class ScreenSelect : public ScreenWithMenuElements @@ -28,7 +28,7 @@ protected: vector m_aGameCommands; // derived classes should look here for what choices are available - vector m_aCodes; + vector m_aCodes; vector m_aCodeChoices; vector m_asSubscribedMessages;