From ab22e629d45cf82e5ba7fb992b416efe27facec4 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Thu, 24 Feb 2005 12:27:30 +0000 Subject: [PATCH] enforce that changed message is broadcast whenever value changes --- stepmania/src/GameCommand.cpp | 4 ++-- stepmania/src/GameState.cpp | 7 +++---- stepmania/src/GameState.h | 4 +++- stepmania/src/MessageManager.cpp | 7 +++++++ stepmania/src/MessageManager.h | 19 +++++++++++++++++++ stepmania/src/MusicWheel.cpp | 5 +++-- stepmania/src/OptionRowHandler.cpp | 2 +- stepmania/src/ScreenEditMenu.cpp | 2 +- stepmania/src/ScreenEnding.cpp | 2 +- stepmania/src/ScreenEvaluation.cpp | 2 +- stepmania/src/ScreenEz2SelectMusic.cpp | 2 +- stepmania/src/ScreenGameplay.cpp | 6 +++--- stepmania/src/ScreenGameplayMultiplayer.cpp | 2 +- stepmania/src/ScreenHowToPlay.cpp | 2 +- stepmania/src/ScreenJukebox.cpp | 2 +- stepmania/src/ScreenNetSelectMusic.cpp | 6 +++--- stepmania/src/ScreenSelectGroup.cpp | 2 +- stepmania/src/ScreenSelectMusic.cpp | 2 +- stepmania/src/SongManager.cpp | 2 +- 19 files changed, 54 insertions(+), 26 deletions(-) diff --git a/stepmania/src/GameCommand.cpp b/stepmania/src/GameCommand.cpp index 3222b30fbe..7bc59649d5 100644 --- a/stepmania/src/GameCommand.cpp +++ b/stepmania/src/GameCommand.cpp @@ -111,7 +111,7 @@ bool GameCommand::DescribesCurrentMode( PlayerNumber pn ) const return false; } - if( m_pSong && GAMESTATE->m_pCurSong != m_pSong ) + if( m_pSong && GAMESTATE->m_pCurSong.Get() != m_pSong ) return false; if( m_pSteps && GAMESTATE->m_pCurSteps[pn] != m_pSteps ) return false; @@ -636,7 +636,7 @@ void GameCommand::Apply( const vector &vpns ) const SCREENMAN->SetNewScreen( m_sScreen ); if( m_pSong ) { - GAMESTATE->m_pCurSong = m_pSong; + GAMESTATE->m_pCurSong.Set( m_pSong ); GAMESTATE->m_pPreferredSong = m_pSong; } if( m_pSteps ) diff --git a/stepmania/src/GameState.cpp b/stepmania/src/GameState.cpp index 3bab894c11..12975529bb 100644 --- a/stepmania/src/GameState.cpp +++ b/stepmania/src/GameState.cpp @@ -151,7 +151,7 @@ void GameState::Reset() m_iGameSeed = rand(); m_iRoundSeed = rand(); - m_pCurSong = NULL; + m_pCurSong.Set( NULL ); m_pPreferredSong = NULL; FOREACH_PlayerNumber( p ) m_pCurSteps[p] = NULL; @@ -1826,9 +1826,8 @@ public: static int GetCurrentSong( T* p, lua_State *L ) { if(p->m_pCurSong) p->m_pCurSong->PushSelf(L); else lua_pushnil(L); return 1; } static int SetCurrentSong( T* p, lua_State *L ) { - if( lua_isnil(L,1) ) { p->m_pCurSong = NULL; } - else { Song *pS = Luna::check(L,1); p->m_pCurSong = pS; } - MESSAGEMAN->Broadcast( "CurrentSongChanged" ); + if( lua_isnil(L,1) ) { p->m_pCurSong.Set( NULL ); } + else { Song *pS = Luna::check(L,1); p->m_pCurSong.Set( pS ); } return 0; } static int GetCurrentSteps( T* p, lua_State *L ) diff --git a/stepmania/src/GameState.h b/stepmania/src/GameState.h index 09475239fa..5b08d87a27 100644 --- a/stepmania/src/GameState.h +++ b/stepmania/src/GameState.h @@ -9,6 +9,7 @@ #include "Attack.h" #include "RageTimer.h" #include "Difficulty.h" +#include "MessageManager.h" #include #include @@ -26,6 +27,7 @@ struct StageStats; struct PlayerState; struct lua_State; + class GameState { public: @@ -127,7 +129,7 @@ public: // // NULL on ScreenSelectMusic if the currently selected wheel item isn't a Song. - Song* m_pCurSong; + BroadcastOnChangePtr m_pCurSong; // The last Song that the user manually changed to. Song* m_pPreferredSong; Steps* m_pCurSteps[NUM_PLAYERS]; diff --git a/stepmania/src/MessageManager.cpp b/stepmania/src/MessageManager.cpp index d9a392b4a8..562c3aa5bb 100644 --- a/stepmania/src/MessageManager.cpp +++ b/stepmania/src/MessageManager.cpp @@ -3,10 +3,17 @@ #include "Foreach.h" #include "Actor.h" #include "RageUtil.h" +#include "EnumHelper.h" MessageManager* MESSAGEMAN = NULL; // global and accessable from anywhere in our program +static const CString MessageNames[NUM_MESSAGES] = { + "CurrentSongChanged", +}; +XToString( Message ); + + MessageManager::MessageManager() { } diff --git a/stepmania/src/MessageManager.h b/stepmania/src/MessageManager.h index e6a8592c1e..8ae825e97c 100644 --- a/stepmania/src/MessageManager.h +++ b/stepmania/src/MessageManager.h @@ -12,6 +12,25 @@ public: virtual void HandleMessage( const CString& sMessage ) = 0; }; +enum Message +{ + MESSAGE_CURRENT_SONG_CHANGED, + NUM_MESSAGES +}; +const CString& MessageToString( Message m ); + +template +class BroadcastOnChangePtr +{ +private: + T *val; +public: + const T* Get() { return val; } + void Set( T* t ) { val = t; MESSAGEMAN->Broadcast( MessageToString(M) ); } + operator T* () const { return val; } + T* operator->() const { return val; } +}; + class MessageManager { public: diff --git a/stepmania/src/MusicWheel.cpp b/stepmania/src/MusicWheel.cpp index ebfd64f1b8..534d04c35a 100644 --- a/stepmania/src/MusicWheel.cpp +++ b/stepmania/src/MusicWheel.cpp @@ -171,7 +171,8 @@ void MusicWheel::Load() pSteps, po, so ); - GAMESTATE->m_pCurSong = GAMESTATE->m_pPreferredSong = pSong; + GAMESTATE->m_pCurSong.Set( pSong ); + GAMESTATE->m_pPreferredSong = pSong; FOREACH_HumanPlayer( p ) { GAMESTATE->m_pCurSteps[p] = pSteps; @@ -235,7 +236,7 @@ void MusicWheel::Load() && !GAMESTATE->GetEventMode() && !GAMESTATE->IsExtraStage() && !GAMESTATE->IsExtraStage2() ) { - GAMESTATE->m_pCurSong = NULL; + GAMESTATE->m_pCurSong.Set( NULL ); } // Select the the previously selected song (if any) diff --git a/stepmania/src/OptionRowHandler.cpp b/stepmania/src/OptionRowHandler.cpp index c1daad4cfa..f97de63d5a 100644 --- a/stepmania/src/OptionRowHandler.cpp +++ b/stepmania/src/OptionRowHandler.cpp @@ -946,7 +946,7 @@ void OptionRowHandlerList::FillSongsInCurrentSongGroup( OptionRowDefinition &def SONGMAN->GetSongs( vpSongs, GAMESTATE->m_sPreferredSongGroup ); if( GAMESTATE->m_pCurSong == NULL ) - GAMESTATE->m_pCurSong = vpSongs[0]; + GAMESTATE->m_pCurSong.Set( vpSongs[0] ); defOut.name = "SongsInCurrentSongGroup"; defOut.bOneChoiceForAllPlayers = true; diff --git a/stepmania/src/ScreenEditMenu.cpp b/stepmania/src/ScreenEditMenu.cpp index d8418f5f2c..f48761dd63 100644 --- a/stepmania/src/ScreenEditMenu.cpp +++ b/stepmania/src/ScreenEditMenu.cpp @@ -110,7 +110,7 @@ void ScreenEditMenu::MenuStart( PlayerNumber pn ) Steps* pSourceNotes = m_Selector.GetSelectedSourceNotes(); EditMenu::Action action = m_Selector.GetSelectedAction(); - GAMESTATE->m_pCurSong = pSong; + GAMESTATE->m_pCurSong.Set( pSong ); GAMESTATE->m_pCurStyle = GAMEMAN->GetEditorStyleForStepsType( st ); GAMESTATE->m_pCurSteps[PLAYER_1] = pSteps; diff --git a/stepmania/src/ScreenEnding.cpp b/stepmania/src/ScreenEnding.cpp index eb20c9e1fb..233c72883a 100644 --- a/stepmania/src/ScreenEnding.cpp +++ b/stepmania/src/ScreenEnding.cpp @@ -148,7 +148,7 @@ ScreenEnding::ScreenEnding( CString sClassName ) : ScreenAttract( sClassName, fa GAMESTATE->m_bSideIsJoined[PLAYER_1] = true; GAMESTATE->m_bSideIsJoined[PLAYER_2] = true; GAMESTATE->m_MasterPlayerNumber = PLAYER_1; - GAMESTATE->m_pCurSong = SONGMAN->GetRandomSong(); + GAMESTATE->m_pCurSong.Set( SONGMAN->GetRandomSong() ); GAMESTATE->m_pCurCourse = SONGMAN->GetRandomCourse(); GAMESTATE->m_pCurSteps[PLAYER_1] = GAMESTATE->m_pCurSong->GetAllSteps()[0]; GAMESTATE->m_pCurSteps[PLAYER_2] = GAMESTATE->m_pCurSong->GetAllSteps()[0]; diff --git a/stepmania/src/ScreenEvaluation.cpp b/stepmania/src/ScreenEvaluation.cpp index 69e254f6dc..5f4ffdd082 100644 --- a/stepmania/src/ScreenEvaluation.cpp +++ b/stepmania/src/ScreenEvaluation.cpp @@ -108,7 +108,7 @@ ScreenEvaluation::ScreenEvaluation( CString sClassName ) : ScreenWithMenuElement GAMESTATE->m_bSideIsJoined[PLAYER_1] = true; GAMESTATE->m_bSideIsJoined[PLAYER_2] = true; GAMESTATE->m_MasterPlayerNumber = PLAYER_1; - GAMESTATE->m_pCurSong = SONGMAN->GetRandomSong(); + GAMESTATE->m_pCurSong.Set( SONGMAN->GetRandomSong() ); STATSMAN->m_CurStageStats.vpSongs.push_back( GAMESTATE->m_pCurSong ); GAMESTATE->m_pCurCourse = SONGMAN->GetRandomCourse(); GAMESTATE->m_pCurSteps[PLAYER_1] = GAMESTATE->m_pCurSong->GetAllSteps()[0]; diff --git a/stepmania/src/ScreenEz2SelectMusic.cpp b/stepmania/src/ScreenEz2SelectMusic.cpp index e70b2148fa..bf71917b8a 100644 --- a/stepmania/src/ScreenEz2SelectMusic.cpp +++ b/stepmania/src/ScreenEz2SelectMusic.cpp @@ -604,7 +604,7 @@ void ScreenEz2SelectMusic::HarderDifficulty( PlayerNumber pn ) void ScreenEz2SelectMusic::MusicChanged() { Song* pSong = m_MusicBannerWheel.GetSelectedSong(); - GAMESTATE->m_pCurSong = pSong; + GAMESTATE->m_pCurSong.Set( pSong ); m_CurrentGroup.SetText( SONGMAN->ShortenGroupName( pSong->m_sGroupName ) , ""); m_CurrentGroup.SetDiffuse( SONGMAN->GetGroupColor(pSong->m_sGroupName) ); diff --git a/stepmania/src/ScreenGameplay.cpp b/stepmania/src/ScreenGameplay.cpp index 7ff32ffeca..4ec93542cb 100644 --- a/stepmania/src/ScreenGameplay.cpp +++ b/stepmania/src/ScreenGameplay.cpp @@ -903,7 +903,7 @@ void ScreenGameplay::LoadNextSong() int iPlaySongIndex = GAMESTATE->GetCourseSongIndex(); iPlaySongIndex %= m_apSongsQueue.size(); - GAMESTATE->m_pCurSong = m_apSongsQueue[iPlaySongIndex]; + GAMESTATE->m_pCurSong.Set( m_apSongsQueue[iPlaySongIndex] ); STATSMAN->m_CurStageStats.vpSongs.push_back( GAMESTATE->m_pCurSong ); // No need to do this here. We do it in SongFinished(). @@ -2162,10 +2162,10 @@ void ScreenGameplay::HandleScreenMessage( const ScreenMessage SM ) int iPlaySongIndex = GAMESTATE->GetCourseSongIndex()+1; iPlaySongIndex %= m_apSongsQueue.size(); - GAMESTATE->m_pCurSong = m_apSongsQueue[iPlaySongIndex]; + GAMESTATE->m_pCurSong.Set( m_apSongsQueue[iPlaySongIndex] ); m_NextSongOut.Load( THEME->GetPathB(m_sName,"next song out") ); - GAMESTATE->m_pCurSong = pCurSong; + GAMESTATE->m_pCurSong.Set( pCurSong ); m_NextSongOut.StartTransitioning( SM_LoadNextSong ); LoadCourseSongNumber( GetMaxSongsPlayed()+1 ); diff --git a/stepmania/src/ScreenGameplayMultiplayer.cpp b/stepmania/src/ScreenGameplayMultiplayer.cpp index db28615fc5..6e4ce8d028 100644 --- a/stepmania/src/ScreenGameplayMultiplayer.cpp +++ b/stepmania/src/ScreenGameplayMultiplayer.cpp @@ -348,7 +348,7 @@ void ScreenGameplayMultiplayer::LoadNextSong() int iPlaySongIndex = GAMESTATE->GetCourseSongIndex(); iPlaySongIndex %= m_vpSongsQueue.size(); - GAMESTATE->m_pCurSong = m_vpSongsQueue[iPlaySongIndex]; + GAMESTATE->m_pCurSong.Set( m_vpSongsQueue[iPlaySongIndex] ); STATSMAN->m_CurStageStats.vpSongs.push_back( GAMESTATE->m_pCurSong ); // No need to do this here. We do it in SongFinished(). diff --git a/stepmania/src/ScreenHowToPlay.cpp b/stepmania/src/ScreenHowToPlay.cpp index 1034efe794..69c1123eeb 100644 --- a/stepmania/src/ScreenHowToPlay.cpp +++ b/stepmania/src/ScreenHowToPlay.cpp @@ -154,7 +154,7 @@ void ScreenHowToPlay::Init() pSteps->GetNoteData( tempNoteData ); pStyle->GetTransformedNoteDataForStyle( PLAYER_1, tempNoteData, m_NoteData ); - GAMESTATE->m_pCurSong = &m_Song; + GAMESTATE->m_pCurSong.Set( &m_Song ); GAMESTATE->m_bPastHereWeGo = true; GAMESTATE->m_pPlayerState[PLAYER_1]->m_PlayerController = PC_AUTOPLAY; diff --git a/stepmania/src/ScreenJukebox.cpp b/stepmania/src/ScreenJukebox.cpp index 56654c0efa..3d70e3e451 100644 --- a/stepmania/src/ScreenJukebox.cpp +++ b/stepmania/src/ScreenJukebox.cpp @@ -99,7 +99,7 @@ bool ScreenJukebox::SetSong( bool bDemonstration ) continue; // skip // Found something we can use! - GAMESTATE->m_pCurSong = pSong; + GAMESTATE->m_pCurSong.Set( pSong ); FOREACH_PlayerNumber( p ) GAMESTATE->m_pCurSteps[p] = pSteps; diff --git a/stepmania/src/ScreenNetSelectMusic.cpp b/stepmania/src/ScreenNetSelectMusic.cpp index 81078be9ca..1f5614722b 100644 --- a/stepmania/src/ScreenNetSelectMusic.cpp +++ b/stepmania/src/ScreenNetSelectMusic.cpp @@ -271,7 +271,7 @@ void ScreenNetSelectMusic::HandleScreenMessage( const ScreenMessage SM ) } break; case SM_SongChanged: - GAMESTATE->m_pCurSong = m_MusicWheel.GetSelectedSong(); + GAMESTATE->m_pCurSong.Set( m_MusicWheel.GetSelectedSong() ); MusicChanged(); break; case SM_SMOnlinePack: @@ -389,7 +389,7 @@ void ScreenNetSelectMusic::MenuStart( PlayerNumber pn ) if ( pSong == NULL ) return; - GAMESTATE->m_pCurSong = pSong; + GAMESTATE->m_pCurSong.Set( pSong ); if ( NSMAN->useSMserver ) { @@ -439,7 +439,7 @@ void ScreenNetSelectMusic::TweenOffScreen() void ScreenNetSelectMusic::StartSelectedSong() { Song * pSong = m_MusicWheel.GetSelectedSong(); - GAMESTATE->m_pCurSong = pSong; + GAMESTATE->m_pCurSong.Set( pSong ); StepsType st = GAMESTATE->GetCurrentStyle()->m_StepsType; //STEPS_TYPE_DANCE_SINGLE; FOREACH_EnabledPlayer (pn) { diff --git a/stepmania/src/ScreenSelectGroup.cpp b/stepmania/src/ScreenSelectGroup.cpp index 6cbb98d407..43b4229d53 100644 --- a/stepmania/src/ScreenSelectGroup.cpp +++ b/stepmania/src/ScreenSelectGroup.cpp @@ -223,7 +223,7 @@ void ScreenSelectGroup::MenuStart( PlayerNumber pn ) m_MenuTimer->Stop(); m_bChosen = true; - GAMESTATE->m_pCurSong = NULL; + GAMESTATE->m_pCurSong.Set( NULL ); GAMESTATE->m_sPreferredSongGroup = (m_GroupList.GetSelectionName()=="ALL MUSIC" ? GROUP_ALL_MUSIC : m_GroupList.GetSelectionName() ); if( GAMESTATE->m_sPreferredSongGroup == GROUP_ALL_MUSIC ) diff --git a/stepmania/src/ScreenSelectMusic.cpp b/stepmania/src/ScreenSelectMusic.cpp index 74ebc20202..14c2dd957d 100644 --- a/stepmania/src/ScreenSelectMusic.cpp +++ b/stepmania/src/ScreenSelectMusic.cpp @@ -1384,7 +1384,7 @@ void ScreenSelectMusic::AfterMusicChange() } Song* pSong = m_MusicWheel.GetSelectedSong(); - GAMESTATE->m_pCurSong = pSong; + GAMESTATE->m_pCurSong.Set( pSong ); if( pSong ) GAMESTATE->m_pPreferredSong = pSong; diff --git a/stepmania/src/SongManager.cpp b/stepmania/src/SongManager.cpp index 49ef2304bb..be15f5fba8 100644 --- a/stepmania/src/SongManager.cpp +++ b/stepmania/src/SongManager.cpp @@ -917,7 +917,7 @@ void SongManager::GetExtraStageInfo( bool bExtra2, const Style *sd, { /* This normally shouldn't happen, but it's helpful to permit it for testing. */ LOG->Warn( "GetExtraStageInfo() called in GROUP_ALL_MUSIC, but GAMESTATE->m_pCurSong == NULL" ); - GAMESTATE->m_pCurSong = SONGMAN->GetRandomSong(); + GAMESTATE->m_pCurSong.Set( SONGMAN->GetRandomSong() ); } sGroup = GAMESTATE->m_pCurSong->m_sGroupName; }