From 182db1222f7f7ae8e086245d64f139ee3a306cc5 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Thu, 24 Feb 2005 15:40:05 +0000 Subject: [PATCH] Have OptionRows subscribe to a Message in order to refresh --- stepmania/src/MessageManager.cpp | 11 ++ stepmania/src/MessageManager.h | 2 + stepmania/src/OptionRow.cpp | 44 +++++++ stepmania/src/OptionRow.h | 7 ++ stepmania/src/OptionRowHandler.cpp | 166 ++++++++++++-------------- stepmania/src/OptionRowHandler.h | 7 +- stepmania/src/ScreenOptions.cpp | 41 ------- stepmania/src/ScreenOptions.h | 3 +- stepmania/src/ScreenOptionsMaster.cpp | 33 +---- stepmania/src/ScreenOptionsMaster.h | 2 - 10 files changed, 148 insertions(+), 168 deletions(-) diff --git a/stepmania/src/MessageManager.cpp b/stepmania/src/MessageManager.cpp index 562c3aa5bb..5ef95a2088 100644 --- a/stepmania/src/MessageManager.cpp +++ b/stepmania/src/MessageManager.cpp @@ -31,6 +31,12 @@ void MessageManager::Subscribe( IMessageSubscriber* pSubscriber, const CString& #endif subs.insert( pSubscriber ); } + +void MessageManager::Subscribe( IMessageSubscriber* pSubscriber, Message m ) +{ + Subscribe( pSubscriber, MessageToString(m) ); +} + void MessageManager::Unsubscribe( IMessageSubscriber* pSubscriber, const CString& sMessage ) { SubscribersSet& subs = m_MessageToSubscribers[sMessage]; @@ -39,6 +45,11 @@ void MessageManager::Unsubscribe( IMessageSubscriber* pSubscriber, const CString subs.erase( iter ); } +void MessageManager::Unsubscribe( IMessageSubscriber* pSubscriber, Message m ) +{ + Unsubscribe( pSubscriber, MessageToString(m) ); +} + void MessageManager::Broadcast( const CString& sMessage ) { SubscribersSet& subs = m_MessageToSubscribers[sMessage]; diff --git a/stepmania/src/MessageManager.h b/stepmania/src/MessageManager.h index 8ae825e97c..de36960db3 100644 --- a/stepmania/src/MessageManager.h +++ b/stepmania/src/MessageManager.h @@ -38,7 +38,9 @@ public: ~MessageManager(); void Subscribe( IMessageSubscriber* pSubscriber, const CString& sMessage ); + void Subscribe( IMessageSubscriber* pSubscriber, Message m ); void Unsubscribe( IMessageSubscriber* pSubscriber, const CString& sMessage ); + void Unsubscribe( IMessageSubscriber* pSubscriber, Message m ); void Broadcast( const CString& sMessage ); private: diff --git a/stepmania/src/OptionRow.cpp b/stepmania/src/OptionRow.cpp index 58abf3ec92..435d6cbb80 100644 --- a/stepmania/src/OptionRow.cpp +++ b/stepmania/src/OptionRow.cpp @@ -3,6 +3,8 @@ #include "RageUtil.h" #include "RageLog.h" #include "Font.h" +#include "Foreach.h" +#include "OptionRowHandler.h" static const CString SelectTypeNames[NUM_SELECT_TYPES] = { "SelectOne", @@ -52,6 +54,12 @@ void OptionRow::Clear() m_Underline[p].clear(); } + if( m_pHand ) + { + FOREACH_CONST( CString, m_pHand->m_vsReloadRowMessages, m ) + MESSAGEMAN->Unsubscribe( this, *m ); + } + m_pHand = NULL; } @@ -61,6 +69,12 @@ void OptionRow::LoadNormal( const OptionRowDefinition &def, OptionRowHandler *pH m_RowType = OptionRow::ROW_NORMAL; m_pHand = pHand; + if( m_pHand ) + { + FOREACH_CONST( CString, m_pHand->m_vsReloadRowMessages, m ) + MESSAGEMAN->Subscribe( this, *m ); + } + if( !def.choices.size() ) RageException::Throw( "Screen %s menu entry \"%s\" has no choices", m_sName.c_str(), def.name.c_str() ); @@ -565,6 +579,36 @@ void OptionRow::SetExitText( CString sExitText ) bt->SetText( sExitText ); } +void OptionRow::Reload() +{ + if( m_pHand == NULL ) + return; + + OptionRowDefinition def; + m_pHand->Reload( def ); + + switch( GetRowType() ) + { + case OptionRow::ROW_NORMAL: + { + ASSERT( m_RowDef.layoutType == LAYOUT_SHOW_ONE_IN_ROW ); + m_RowDef = def; + UpdateText( true ); + } + break; + case OptionRow::ROW_EXIT: + // nothing to do + break; + default: + ASSERT(0); + } +} + +void OptionRow::HandleMessage( const CString& sMessage ) +{ + Reload(); +} + /* * (c) 2001-2004 Chris Danford diff --git a/stepmania/src/OptionRow.h b/stepmania/src/OptionRow.h index 1f28cdb4a6..1d46a3f3ba 100644 --- a/stepmania/src/OptionRow.h +++ b/stepmania/src/OptionRow.h @@ -153,6 +153,13 @@ public: void SetExitText( CString sExitText ); + void Reload(); + + // + // Messages + // + virtual void HandleMessage( const CString& sMessage ); + protected: OptionRowDefinition m_RowDef; RowType m_RowType; diff --git a/stepmania/src/OptionRowHandler.cpp b/stepmania/src/OptionRowHandler.cpp index 58f6ae8039..8ebd844b81 100644 --- a/stepmania/src/OptionRowHandler.cpp +++ b/stepmania/src/OptionRowHandler.cpp @@ -55,9 +55,9 @@ public: Default.Init(); m_bUseModNameForIcon = false; } + virtual void Load( OptionRowDefinition &defOut, CString sParam ); virtual void ImportOption( const OptionRowDefinition &def, PlayerNumber pn, vector &vbSelectedOut ) const; virtual int ExportOption( const OptionRowDefinition &def, PlayerNumber pn, const vector &vbSelected ) const; - virtual void Reload( OptionRowDefinition &defOut ); virtual CString GetIconText( const OptionRowDefinition &def, int iFirstSelection ) const { return m_bUseModNameForIcon ? @@ -80,7 +80,6 @@ public: return ""; } - void FillList( OptionRowDefinition &defOut, CString param ); void FillNoteSkins( OptionRowDefinition &defOut ); void FillSteps( OptionRowDefinition &defOut ); void FillEditsAndNull( OptionRowDefinition &defOut ); @@ -103,11 +102,9 @@ public: delete m_pLuaTable; m_pLuaTable = new LuaExpression; } + virtual void Load( OptionRowDefinition &defOut, CString sLuaFunction ); virtual void ImportOption( const OptionRowDefinition &def, PlayerNumber pn, vector &vbSelectedOut ) const; virtual int ExportOption( const OptionRowDefinition &def, PlayerNumber pn, const vector &vbSelected ) const; - virtual void Reload( OptionRowDefinition &defOut ); - - void FillLua( OptionRowDefinition &defOut, CString sLuaFunction ); }; class OptionRowHandlerConfig : public OptionRowHandler @@ -121,10 +118,9 @@ public: OptionRowHandler::Init(); opt = NULL; } + virtual void Load( OptionRowDefinition &defOut, CString sParam ); virtual void ImportOption( const OptionRowDefinition &row, PlayerNumber pn, vector &vbSelectedOut ) const; virtual int ExportOption( const OptionRowDefinition &def, PlayerNumber pn, const vector &vbSelected ) const; - - void FillConfig( OptionRowDefinition &defOut, CString param ); }; class OptionRowHandlerStepsType : public OptionRowHandler @@ -140,29 +136,8 @@ public: m_pstToFill = NULL; m_vStepsTypesToShow.clear(); } - virtual void ImportOption( const OptionRowDefinition &row, PlayerNumber pn, vector &vbSelectedOut ) const - { - if( GAMESTATE->m_pCurSteps[0] ) - { - StepsType st = GAMESTATE->m_pCurSteps[0]->m_StepsType; - vector::const_iterator iter = find( m_vStepsTypesToShow.begin(), m_vStepsTypesToShow.end(), st ); - if( iter != m_vStepsTypesToShow.end() ) - { - unsigned i = iter - m_vStepsTypesToShow.begin(); - vbSelectedOut[i] = true; - return; - } - } - vbSelectedOut[0] = true; - } - virtual int ExportOption( const OptionRowDefinition &def, PlayerNumber pn, const vector &vbSelected ) const - { - int index = GetOneSelection( vbSelected ); - *m_pstToFill = m_vStepsTypesToShow[index]; - return 0; - } - void FillStepsType( OptionRowDefinition &defOut, CString sParam ) + virtual void Load( OptionRowDefinition &defOut, CString sParam ) { Init(); defOut.Init(); @@ -206,6 +181,27 @@ public: if( *m_pstToFill == STEPS_TYPE_INVALID ) *m_pstToFill = m_vStepsTypesToShow[0]; } + virtual void ImportOption( const OptionRowDefinition &row, PlayerNumber pn, vector &vbSelectedOut ) const + { + if( GAMESTATE->m_pCurSteps[0] ) + { + StepsType st = GAMESTATE->m_pCurSteps[0]->m_StepsType; + vector::const_iterator iter = find( m_vStepsTypesToShow.begin(), m_vStepsTypesToShow.end(), st ); + if( iter != m_vStepsTypesToShow.end() ) + { + unsigned i = iter - m_vStepsTypesToShow.begin(); + vbSelectedOut[i] = true; + return; + } + } + vbSelectedOut[0] = true; + } + virtual int ExportOption( const OptionRowDefinition &def, PlayerNumber pn, const vector &vbSelected ) const + { + int index = GetOneSelection( vbSelected ); + *m_pstToFill = m_vStepsTypesToShow[index]; + return 0; + } }; @@ -222,6 +218,40 @@ public: m_ppStepsToFill = NULL; m_vSteps.clear(); } + + virtual void Load( OptionRowDefinition &defOut, CString sParam ) + { + Init(); + defOut.Init(); + + if( sParam == "EditSourceSteps" ) + m_ppStepsToFill = &GAMESTATE->m_pStepsEditSource; + else + RageException::Throw( "invalid StepsType param \"%s\"", sParam.c_str() ); + + m_sName = sParam; + defOut.name = sParam; + defOut.bOneChoiceForAllPlayers = true; + defOut.layoutType = LAYOUT_SHOW_ONE_IN_ROW; + defOut.m_bExportOnChange = true; + m_vsReloadRowMessages.push_back( MessageToString(MESSAGE_CURRENT_SONG_CHANGED) ); + + GAMESTATE->m_pCurSong->GetSteps( m_vSteps, GAMESTATE->m_stEditSource ); + StepsUtil::SortNotesArrayByDifficulty( m_vSteps ); + for( unsigned i=0; iGetDifficulty() == DIFFICULTY_EDIT ) + s = pSteps->GetDescription(); + else + s = DifficultyToThemedString( pSteps->GetDifficulty() ); + s += ssprintf( " (%d)", pSteps->GetMeter() ); + + defOut.choices.push_back( s ); + } + } virtual void ImportOption( const OptionRowDefinition &row, PlayerNumber pn, vector &vbSelectedOut ) const { vector::const_iterator iter = find( m_vSteps.begin(), m_vSteps.end(), *m_ppStepsToFill ); @@ -239,39 +269,6 @@ public: *m_ppStepsToFill = m_vSteps[index]; return 0; } - - void FillSteps( OptionRowDefinition &defOut, CString sParam ) - { - Init(); - defOut.Init(); - - if( sParam == "EditSourceSteps" ) - m_ppStepsToFill = &GAMESTATE->m_pStepsEditSource; - else - RageException::Throw( "invalid StepsType param \"%s\"", sParam.c_str() ); - - m_sName = sParam; - defOut.name = sParam; - defOut.bOneChoiceForAllPlayers = true; - defOut.layoutType = LAYOUT_SHOW_ONE_IN_ROW; - defOut.m_bExportOnChange = true; - - GAMESTATE->m_pCurSong->GetSteps( m_vSteps, GAMESTATE->m_stEditSource ); - StepsUtil::SortNotesArrayByDifficulty( m_vSteps ); - for( unsigned i=0; iGetDifficulty() == DIFFICULTY_EDIT ) - s = pSteps->GetDescription(); - else - s = DifficultyToThemedString( pSteps->GetDifficulty() ); - s += ssprintf( " (%d)", pSteps->GetMeter() ); - - defOut.choices.push_back( s ); - } - } }; @@ -337,11 +334,6 @@ int OptionRowHandlerList::ExportOption( const OptionRowDefinition &def, PlayerNu return 0; } -void OptionRowHandlerList::Reload( OptionRowDefinition &defOut ) -{ - FillList( defOut, m_sName ); -} - void OptionRowHandlerLua::ImportOption( const OptionRowDefinition &def, PlayerNumber pn, vector &vbSelectedOut ) const { @@ -435,11 +427,6 @@ int OptionRowHandlerLua::ExportOption( const OptionRowDefinition &def, PlayerNum return 0; } -void OptionRowHandlerLua::Reload( OptionRowDefinition &defOut ) -{ - FillLua( defOut, m_sName ); -} - void OptionRowHandlerConfig::ImportOption( const OptionRowDefinition &def, PlayerNumber pn, vector &vbSelectedOut ) const { @@ -471,7 +458,7 @@ int OptionRowHandlerConfig::ExportOption( const OptionRowDefinition &def, Player /* Add the list named "ListName" to the given row/handler. */ -void OptionRowHandlerList::FillList( OptionRowDefinition &defOut, CString sListName ) +void OptionRowHandlerList::Load( OptionRowDefinition &defOut, CString sListName ) { m_sName = sListName; @@ -509,10 +496,10 @@ void OptionRowHandlerList::FillList( OptionRowDefinition &defOut, CString sListN else if( sName == "selectmultiple" ) defOut.selectType = SELECT_MULTIPLE; else if( sName == "selectnone" ) defOut.selectType = SELECT_NONE; else if( sName == "showoneinrow" ) defOut.layoutType = LAYOUT_SHOW_ONE_IN_ROW; - else if( sName == "reloadrownames" ) + else if( sName == "reloadrowmessages" ) { for( unsigned a=1; aL, 1 ); /* pop ExportOnChange value */ - /* Iterate over the "RefreshRowNames" table. */ - lua_pushstring( LUA->L, "RefreshRowNames" ); + /* Iterate over the "ReloadRowMessages" table. */ + lua_pushstring( LUA->L, "ReloadRowMessages" ); lua_gettable( LUA->L, -2 ); if( !lua_isnil( LUA->L, -1 ) ) { if( !lua_istable( LUA->L, -1 ) ) - RageException::Throw( "\"%s\" \"RefreshRowNames\" is not a table", sLuaFunction.c_str() ); + RageException::Throw( "\"%s\" \"ReloadRowMessages\" is not a table", sLuaFunction.c_str() ); - m_vsRefreshRowNames.clear(); // and fill in with supplied PlayerNumbers below + m_vsReloadRowMessages.clear(); // and fill in with supplied PlayerNumbers below lua_pushnil( LUA->L ); while( lua_next(LUA->L, -2) != 0 ) @@ -675,12 +662,12 @@ void OptionRowHandlerLua::FillLua( OptionRowDefinition &defOut, CString sLuaFunc RageException::Throw( "\"%s\" Column entry is not a string", sLuaFunction.c_str() ); LOG->Trace( "'%s'", pValue); - m_vsRefreshRowNames.push_back( pValue ); + m_vsReloadRowMessages.push_back( pValue ); lua_pop( LUA->L, 1 ); /* removes `value'; keeps `key' for next iteration */ } } - lua_pop( LUA->L, 1 ); /* pop RefreshRowNames table */ + lua_pop( LUA->L, 1 ); /* pop ReloadRowMessages table */ lua_pop( LUA->L, 1 ); /* pop main table */ @@ -775,6 +762,7 @@ void OptionRowHandlerList::FillEditsAndNull( OptionRowDefinition &defOut ) defOut.bOneChoiceForAllPlayers = true; defOut.layoutType = LAYOUT_SHOW_ONE_IN_ROW; defOut.m_bExportOnChange = true; + m_vsReloadRowMessages.push_back( MessageToString(MESSAGE_CURRENT_SONG_CHANGED) ); vector vSteps; GAMESTATE->m_pCurSong->GetSteps( vSteps, GAMESTATE->m_stEdit, DIFFICULTY_EDIT ); @@ -800,7 +788,7 @@ void OptionRowHandlerList::FillEditsAndNull( OptionRowDefinition &defOut ) /* Add the given configuration value to the given row/handler. */ -void OptionRowHandlerConfig::FillConfig( OptionRowDefinition &defOut, CString param ) +void OptionRowHandlerConfig::Load( OptionRowDefinition &defOut, CString param ) { Init(); defOut.Init(); @@ -971,11 +959,13 @@ OptionRowHandler* OptionRowHandlerUtil::Make( const Command &command, OptionRowD const CString &name = command.GetName(); - if( name == "list" ) { OptionRowHandlerList *p = new OptionRowHandlerList; p->FillList( defOut, sArg(1) ); pHand = p; } - else if( name == "lua" ) { OptionRowHandlerLua *p = new OptionRowHandlerLua; p->FillLua( defOut, sArg(1) ); pHand = p; } - else if( name == "conf" ) { OptionRowHandlerConfig *p = new OptionRowHandlerConfig; p->FillConfig( defOut, sArg(1) ); pHand = p; } - else if( name == "stepstype" ) { OptionRowHandlerStepsType *p = new OptionRowHandlerStepsType; p->FillStepsType( defOut, sArg(1) ); pHand = p; } - else if( name == "steps" ) { OptionRowHandlerSteps *p = new OptionRowHandlerSteps; p->FillSteps( defOut, sArg(1) ); pHand = p; } +#define MAKE( type ) { type *p = new type; p->Load( defOut, sArg(1) ); pHand = p; } + + if( name == "list" ) MAKE( OptionRowHandlerList ) + else if( name == "lua" ) MAKE( OptionRowHandlerLua ) + else if( name == "conf" ) MAKE( OptionRowHandlerConfig ) + else if( name == "stepstype" ) MAKE( OptionRowHandlerStepsType ) + else if( name == "steps" ) MAKE( OptionRowHandlerSteps ) EndHandleArgs; diff --git a/stepmania/src/OptionRowHandler.h b/stepmania/src/OptionRowHandler.h index 7f644cb06d..ea4a82b87e 100644 --- a/stepmania/src/OptionRowHandler.h +++ b/stepmania/src/OptionRowHandler.h @@ -13,18 +13,19 @@ class OptionRowHandler { public: CString m_sName; - vector m_vsRefreshRowNames; // refresh these rows when the value of this row changes + vector m_vsReloadRowMessages; // refresh this row on on these messages OptionRowHandler::OptionRowHandler() { Init(); } virtual void Init() { m_sName = ""; - m_vsRefreshRowNames.clear(); + m_vsReloadRowMessages.clear(); } + virtual void Load( OptionRowDefinition &defOut, CString sParam ) = 0; + void Reload( OptionRowDefinition &defOut ) { this->Load(defOut,m_sName); } virtual void ImportOption( const OptionRowDefinition &row, PlayerNumber pn, vector &vbSelectedOut ) const = 0; /* Returns an OPT mask. */ virtual int ExportOption( const OptionRowDefinition &def, PlayerNumber pn, const vector &vbSelected ) const = 0; - virtual void Reload( OptionRowDefinition &defOut ) {} virtual CString GetIconText( const OptionRowDefinition &def, int iFirstSelection ) const { return ""; } virtual CString GetAndEraseScreen( int iChoice ) { return ""; } }; diff --git a/stepmania/src/ScreenOptions.cpp b/stepmania/src/ScreenOptions.cpp index 4434f0a470..9062751712 100644 --- a/stepmania/src/ScreenOptions.cpp +++ b/stepmania/src/ScreenOptions.cpp @@ -1142,47 +1142,6 @@ int ScreenOptions::GetCurrentRow( PlayerNumber pn ) const return r; } -void ScreenOptions::RefreshRowChoices( int r, const OptionRowDefinition &newdef ) -{ - OptionRow &row = *m_Rows[r]; - - switch( row.GetRowType() ) - { - case OptionRow::ROW_NORMAL: - { - Font* pFont = FONT->LoadFont( THEME->GetPathF(m_sName,"item") ); - row.Clear(); - row.LoadNormal( newdef, row.GetHandler() ); - row.AfterImportOptions( - pFont, - ITEMS_START_X, - ITEMS_GAP_X, - ITEMS_END_X, - ITEMS_LONG_ROW_SHARED_X, - ITEMS_LONG_ROW_X, - ITEMS_ZOOM, - CAPITALIZE_ALL_OPTION_NAMES, - THEME->GetPathF(m_sName,"item"), - THEME->GetPathF(m_sName,"title"), - GetExplanationTitle( r ), - THEME->GetPathG(m_sName,"bullet"), - LABELS_X, - ARROWS_X, - row.GetRowY(), - LABELS_ON_COMMAND - ); - FONT->UnloadFont( pFont ); - pFont = NULL; - UpdateEnabledDisabled( r ); - } - break; - case OptionRow::ROW_EXIT: - // nothing to do - break; - default: - ASSERT(0); - } -} /* * (c) 2001-2004 Chris Danford, Glenn Maynard diff --git a/stepmania/src/ScreenOptions.h b/stepmania/src/ScreenOptions.h index d72e38f4c7..45e6670b16 100644 --- a/stepmania/src/ScreenOptions.h +++ b/stepmania/src/ScreenOptions.h @@ -49,7 +49,6 @@ protected: void UpdateEnabledDisabled(); void UpdateEnabledDisabled( int row ); virtual void OnChange( PlayerNumber pn ); - void RefreshRowChoices( int row, const OptionRowDefinition &def ); virtual void MenuBack( PlayerNumber pn ); virtual void MenuStart( PlayerNumber pn, const InputEventType type ); @@ -59,7 +58,7 @@ protected: virtual void GoToNextScreen() = 0; virtual void GoToPrevScreen() = 0; - virtual void ChangeValueInRow( PlayerNumber pn, int iDelta, bool Repeat ); + void ChangeValueInRow( PlayerNumber pn, int iDelta, bool Repeat ); void MoveRow( PlayerNumber pn, int dir, bool Repeat ); void MenuLeft( PlayerNumber pn, const InputEventType type ) { ChangeValueInRow(pn,-1,type != IET_FIRST_PRESS); } diff --git a/stepmania/src/ScreenOptionsMaster.cpp b/stepmania/src/ScreenOptionsMaster.cpp index f3e794ba76..6d21b4b55a 100644 --- a/stepmania/src/ScreenOptionsMaster.cpp +++ b/stepmania/src/ScreenOptionsMaster.cpp @@ -105,9 +105,7 @@ void ScreenOptionsMaster::Init() ASSERT( OptionRowHandlers.size() == asLineNames.size() ); - vector vHands( m_OptionRowAlloc.size(), NULL ); - - InitMenu( im, m_OptionRowAlloc, vHands, bShowUnderlines ); + InitMenu( im, m_OptionRowAlloc, OptionRowHandlers, bShowUnderlines ); } ScreenOptionsMaster::~ScreenOptionsMaster() @@ -283,35 +281,6 @@ void ScreenOptionsMaster::RefreshIcons() } } -void ScreenOptionsMaster::ChangeValueInRow( PlayerNumber pn, int iDelta, bool Repeat ) -{ - ScreenOptions::ChangeValueInRow( pn, iDelta, Repeat ); - - int iRow = m_iCurrentRow[pn]; - - const OptionRowHandler *pHand = OptionRowHandlers[iRow]; - - FOREACH_CONST( CString, pHand->m_vsRefreshRowNames, sRowToRefreshName ) - { - for( unsigned r=0; rm_sName ) - { - pHandOther->Reload( defOther ); - ScreenOptions::RefreshRowChoices( r, defOther ); - } - } - } -} - void ScreenOptionsMaster::HandleScreenMessage( const ScreenMessage SM ) { switch( SM ) diff --git a/stepmania/src/ScreenOptionsMaster.h b/stepmania/src/ScreenOptionsMaster.h index 5ad0ad0b28..394f7badd0 100644 --- a/stepmania/src/ScreenOptionsMaster.h +++ b/stepmania/src/ScreenOptionsMaster.h @@ -24,8 +24,6 @@ protected: protected: void HandleScreenMessage( const ScreenMessage SM ); - virtual void ChangeValueInRow( PlayerNumber pn, int iDelta, bool Repeat ); - virtual void ImportOptions( int row ); virtual void ExportOptions( int row ); virtual void ImportOptionsForPlayer( PlayerNumber pn ); // used by ScreenPlayerOptions