From 5144e29421be28193a902954b6935df5b72dbf03 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 25 Jan 2004 02:29:18 +0000 Subject: [PATCH] generalize --- stepmania/src/CodeDetector.cpp | 106 +++++++++++++++------------------ stepmania/src/CodeDetector.h | 19 +++++- 2 files changed, 65 insertions(+), 60 deletions(-) diff --git a/stepmania/src/CodeDetector.cpp b/stepmania/src/CodeDetector.cpp index e80a1fbcd8..94fbfe19d9 100644 --- a/stepmania/src/CodeDetector.cpp +++ b/stepmania/src/CodeDetector.cpp @@ -64,59 +64,44 @@ const CString g_sCodeNames[CodeDetector::NUM_CODES] = { "NextBannerGroup2" }; -const unsigned MAX_CODE_LENGTH = 10; - -struct CodeCacheItem { - int iNumButtons; - GameButton buttons[MAX_CODE_LENGTH]; - enum Type { - sequence, // press the buttons in sequence - hold_and_press, // hold the first iNumButtons-1 buttons, then press the last - tap // press all buttons simultaneously - }; - Type m_Type; - float fMaxSecondsBack; -}; -CodeCacheItem g_CodeCacheItems[CodeDetector::NUM_CODES]; +CodeItem g_CodeItems[CodeDetector::NUM_CODES]; -bool CodeDetector::EnteredCode( GameController controller, Code code ) +bool CodeItem::EnteredCode( GameController controller ) const { - if( g_CodeCacheItems[code].iNumButtons == 0 ) + if( buttons.size() == 0 ) return false; - const CodeCacheItem& item = g_CodeCacheItems[code]; - switch( item.m_Type ) + switch( m_Type ) { - case CodeCacheItem::sequence: - return INPUTQUEUE->MatchesSequence(controller, item.buttons, item.iNumButtons, item.fMaxSecondsBack); - case CodeCacheItem::hold_and_press: + case sequence: + return INPUTQUEUE->MatchesSequence( controller, &buttons[0], buttons.size(), fMaxSecondsBack ); + case hold_and_press: { // check that all but the last are being held - for( int i=0; iIsButtonDown(gi) ) return false; } // just pressed the last button - return INPUTQUEUE->MatchesSequence(controller, &item.buttons[item.iNumButtons-1], 1, 0.05f); + return INPUTQUEUE->MatchesSequence( controller, &buttons[buttons.size()-1], 1, 0.05f ); } break; - case CodeCacheItem::tap: - return INPUTQUEUE->AllWerePressedRecently(controller, item.buttons, item.iNumButtons, item.fMaxSecondsBack); + case tap: + return INPUTQUEUE->AllWerePressedRecently( controller, &buttons[0], buttons.size(), fMaxSecondsBack ); default: ASSERT(0); return false; } } -void RefreshCacheItem( int iIndex ) +void CodeItem::Load( CString sButtonsNames ) { - CodeCacheItem& item = g_CodeCacheItems[iIndex]; + buttons.clear(); + const GameDef* pGameDef = GAMESTATE->GetCurrentGameDef(); - CString sCodeName = g_sCodeNames[iIndex]; - CString sButtonsNames = THEME->GetMetric("CodeDetector",sCodeName); CStringArray asButtonNames; bool bHasAPlus = sButtonsNames.Find( '+' ) != -1; @@ -124,62 +109,53 @@ void RefreshCacheItem( int iIndex ) if( bHasAPlus ) { - item.m_Type = CodeCacheItem::tap; + m_Type = tap; split( sButtonsNames, "+", asButtonNames, false ); } else if( bHasADash ) { - item.m_Type = CodeCacheItem::hold_and_press; + m_Type = hold_and_press; split( sButtonsNames, "-", asButtonNames, false ); } else { - item.m_Type = CodeCacheItem::sequence; + m_Type = sequence; split( sButtonsNames, ",", asButtonNames, false ); } if( asButtonNames.size() < 2 ) { if( sButtonsNames != "" ) - LOG->Trace( "The code '%s' is less than 2 buttons, so it will be ignored.", sCodeName.c_str() ); - item.iNumButtons = 0; + LOG->Trace( "The code '%s' is less than 2 buttons, so it will be ignored.", sButtonsNames.c_str() ); return; } - for( unsigned i=0; im_iButtonsPerController; j++ ) + const GameButton gb = pGameDef->ButtonNameToIndex( sButtonName ); + if( gb == GAME_BUTTON_INVALID ) { - if( 0==stricmp(sButtonName,pGameDef->m_szButtonNames[j]) ) - { - gb = j; - item.iNumButtons = i+1; - break; // found it. Don't keep searching - } - } - if( gb == -1 ) // didn't find it - { - LOG->Trace( "The code '%s' contains an unrecognized button '%s'.", sCodeName.c_str(), sButtonName.c_str() ); - item.iNumButtons = 0; + LOG->Trace( "The code '%s' contains an unrecognized button '%s'.", sButtonsNames.c_str(), sButtonName.c_str() ); + buttons.clear(); return; } + + buttons.push_back( gb ); } - switch( item.m_Type ) + switch( m_Type ) { - case CodeCacheItem::sequence: - item.fMaxSecondsBack = item.iNumButtons*0.4f; + case sequence: + fMaxSecondsBack = buttons.size()*0.4f; break; - case CodeCacheItem::hold_and_press: - item.fMaxSecondsBack = -1.f; // not applicable + case hold_and_press: + fMaxSecondsBack = -1.f; // not applicable break; - case CodeCacheItem::tap: - item.fMaxSecondsBack = 0.05f; // simultaneous + case tap: + fMaxSecondsBack = 0.05f; // simultaneous break; default: ASSERT(0); @@ -188,10 +164,22 @@ void RefreshCacheItem( int iIndex ) // if we make it here, we found all the buttons in the code } +bool CodeDetector::EnteredCode( GameController controller, Code code ) +{ + return g_CodeItems[code].EnteredCode( controller ); +} + + void CodeDetector::RefreshCacheItems() { for( int i=0; iGetMetric("CodeDetector",sCodeName); + + item.Load( sButtonsNames ); + } } bool CodeDetector::EnteredNextBannerGroup( GameController controller ) diff --git a/stepmania/src/CodeDetector.h b/stepmania/src/CodeDetector.h index 0f893f1d55..0441c1f842 100644 --- a/stepmania/src/CodeDetector.h +++ b/stepmania/src/CodeDetector.h @@ -13,6 +13,24 @@ #include "GameInput.h" +struct CodeItem +{ +public: + void Load( CString sButtonsNames ); + bool EnteredCode( GameController controller ) const; + +private: + vector buttons; + enum Type + { + sequence, // press the buttons in sequence + hold_and_press, // hold the first iNumButtons-1 buttons, then press the last + tap // press all buttons simultaneously + }; + Type m_Type; + float fMaxSecondsBack; +}; + class CodeDetector { public: @@ -68,7 +86,6 @@ public: static bool DetectAndAdjustMusicOptions( GameController controller ); static bool EnteredCode( GameController controller, Code code ); static bool EnteredNextBannerGroup( GameController controller ); - };