From 4ebfc1dcac3e36a38a052b08e0ec35a89fe5cec4 Mon Sep 17 00:00:00 2001 From: Garux Date: Sat, 7 Sep 2019 20:55:55 +0300 Subject: [PATCH] refactor template class LatchedValue use make m_sGameFile LatchedValue fix odd prompt to restart after game change via global preferences dialog + OK in local preferences sort games list alphabetically by .game file name --- libs/stringio.h | 30 ++++++++++++++++ radiant/brushmodule.cpp | 1 - radiant/entity.cpp | 6 ++-- radiant/mainframe.cpp | 37 ++++++++----------- radiant/multimon.cpp | 6 ++-- radiant/preferences.cpp | 79 ++++++++++++++++------------------------- radiant/preferences.h | 36 ++++++++++--------- 7 files changed, 98 insertions(+), 97 deletions(-) diff --git a/libs/stringio.h b/libs/stringio.h index c7a99d25..72bfc1ee 100644 --- a/libs/stringio.h +++ b/libs/stringio.h @@ -378,6 +378,36 @@ static void thunk( void* environment, FirstArgument firstArgument ){ }; +class CopiedStringFromString +{ +CopiedString m_value; +public: +CopiedStringFromString( const char* string ){ + CopiedString_importString( m_value, string ); +} +operator CopiedString() const +{ + return m_value; +} +}; + +inline void CopiedString_toString( const StringImportCallback& self, CopiedString value ){ + CopiedString_exportString( value, self ); +} +typedef ConstReferenceCaller1 CopiedStringToString; + + +template +inline StringImportCallback makeCopiedStringStringImportCallback( const Caller& caller ){ + return StringImportCallback( caller.getEnvironment(), ImportConvert1::thunk ); +} + +template +inline StringExportCallback makeCopiedStringStringExportCallback( const Caller& caller ){ + return StringExportCallback( caller.getEnvironment(), ImportConvert1::thunk ); +} + + class BoolFromString { bool m_value; diff --git a/radiant/brushmodule.cpp b/radiant/brushmodule.cpp index fbd6ae2c..c71b38ef 100644 --- a/radiant/brushmodule.cpp +++ b/radiant/brushmodule.cpp @@ -36,7 +36,6 @@ #include "clippertool.h" -LatchedBool g_useAlternativeTextureProjection( false, "Use alternative texture-projection (\"brush primitives\")" ); bool g_multipleBrushTypes = false; EBrushType g_brushTypes[3]; int g_brushType; diff --git a/radiant/entity.cpp b/radiant/entity.cpp index 410531ce..2d5e3da4 100644 --- a/radiant/entity.cpp +++ b/radiant/entity.cpp @@ -651,11 +651,9 @@ typedef ReferenceCaller1 ShowTargetNamesImportCaller; diff --git a/radiant/mainframe.cpp b/radiant/mainframe.cpp index da59a050..368681d8 100644 --- a/radiant/mainframe.cpp +++ b/radiant/mainframe.cpp @@ -3510,40 +3510,40 @@ void Layout_constructPreferences( PreferencesPage& page ){ page.appendRadioIcons( "Window Layout", STRING_ARRAY_RANGE( layouts ), - LatchedIntImportCaller( g_Layout_viewStyle ), + LatchedImportCaller( g_Layout_viewStyle ), IntExportCaller( g_Layout_viewStyle.m_latched ) ); } page.appendCheckBox( "", "Detachable Menus", - LatchedBoolImportCaller( g_Layout_enableDetachableMenus ), + LatchedImportCaller( g_Layout_enableDetachableMenus ), BoolExportCaller( g_Layout_enableDetachableMenus.m_latched ) ); page.appendCheckBox( "", "Main Toolbar", - LatchedBoolImportCaller( g_Layout_enableMainToolbar ), + LatchedImportCaller( g_Layout_enableMainToolbar ), BoolExportCaller( g_Layout_enableMainToolbar.m_latched ) ); if ( !string_empty( g_pGameDescription->getKeyValue( "no_patch" ) ) ) { page.appendCheckBox( "", "Patch Toolbar", - LatchedBoolImportCaller( g_Layout_enablePatchToolbar ), + LatchedImportCaller( g_Layout_enablePatchToolbar ), BoolExportCaller( g_Layout_enablePatchToolbar.m_latched ) ); } page.appendCheckBox( "", "Plugin Toolbar", - LatchedBoolImportCaller( g_Layout_enablePluginToolbar ), + LatchedImportCaller( g_Layout_enablePluginToolbar ), BoolExportCaller( g_Layout_enablePluginToolbar.m_latched ) ); page.appendCheckBox( "", "Filter Toolbar", - LatchedBoolImportCaller( g_Layout_enableFilterToolbar ), + LatchedImportCaller( g_Layout_enableFilterToolbar ), BoolExportCaller( g_Layout_enableFilterToolbar.m_latched ) ); page.appendCheckBox( "", "Single Scrollable Toolbar", - LatchedBoolImportCaller( g_Layout_SingleToolbar ), + LatchedImportCaller( g_Layout_SingleToolbar ), BoolExportCaller( g_Layout_SingleToolbar.m_latched ) ); } @@ -3699,13 +3699,13 @@ void MainFrame_Construct(){ typedef FreeCaller1 ComponentModeSelectionChangedCaller; GlobalSelectionSystem().addSelectionChangeCallback( ComponentModeSelectionChangedCaller() ); - GlobalPreferenceSystem().registerPreference( "DetachableMenus", BoolImportStringCaller( g_Layout_enableDetachableMenus.m_latched ), BoolExportStringCaller( g_Layout_enableDetachableMenus.m_latched ) ); - GlobalPreferenceSystem().registerPreference( "MainToolBar", BoolImportStringCaller( g_Layout_enableMainToolbar.m_latched ), BoolExportStringCaller( g_Layout_enableMainToolbar.m_latched ) ); - GlobalPreferenceSystem().registerPreference( "PatchToolBar", BoolImportStringCaller( g_Layout_enablePatchToolbar.m_latched ), BoolExportStringCaller( g_Layout_enablePatchToolbar.m_latched ) ); - GlobalPreferenceSystem().registerPreference( "PluginToolBar", BoolImportStringCaller( g_Layout_enablePluginToolbar.m_latched ), BoolExportStringCaller( g_Layout_enablePluginToolbar.m_latched ) ); - GlobalPreferenceSystem().registerPreference( "FilterToolBar", BoolImportStringCaller( g_Layout_enableFilterToolbar.m_latched ), BoolExportStringCaller( g_Layout_enableFilterToolbar.m_latched ) ); - GlobalPreferenceSystem().registerPreference( "SingleToolBar", BoolImportStringCaller( g_Layout_SingleToolbar.m_latched ), BoolExportStringCaller( g_Layout_SingleToolbar.m_latched ) ); - GlobalPreferenceSystem().registerPreference( "QE4StyleWindows", IntImportStringCaller( g_Layout_viewStyle.m_latched ), IntExportStringCaller( g_Layout_viewStyle.m_latched ) ); + GlobalPreferenceSystem().registerPreference( "DetachableMenus", makeBoolStringImportCallback( LatchedAssignCaller( g_Layout_enableDetachableMenus ) ), BoolExportStringCaller( g_Layout_enableDetachableMenus.m_latched ) ); + GlobalPreferenceSystem().registerPreference( "MainToolBar", makeBoolStringImportCallback( LatchedAssignCaller( g_Layout_enableMainToolbar ) ), BoolExportStringCaller( g_Layout_enableMainToolbar.m_latched ) ); + GlobalPreferenceSystem().registerPreference( "PatchToolBar", makeBoolStringImportCallback( LatchedAssignCaller( g_Layout_enablePatchToolbar ) ), BoolExportStringCaller( g_Layout_enablePatchToolbar.m_latched ) ); + GlobalPreferenceSystem().registerPreference( "PluginToolBar", makeBoolStringImportCallback( LatchedAssignCaller( g_Layout_enablePluginToolbar ) ), BoolExportStringCaller( g_Layout_enablePluginToolbar.m_latched ) ); + GlobalPreferenceSystem().registerPreference( "FilterToolBar", makeBoolStringImportCallback( LatchedAssignCaller( g_Layout_enableFilterToolbar ) ), BoolExportStringCaller( g_Layout_enableFilterToolbar.m_latched ) ); + GlobalPreferenceSystem().registerPreference( "SingleToolBar", makeBoolStringImportCallback( LatchedAssignCaller( g_Layout_SingleToolbar ) ), BoolExportStringCaller( g_Layout_SingleToolbar.m_latched ) ); + GlobalPreferenceSystem().registerPreference( "QE4StyleWindows", makeIntStringImportCallback( LatchedAssignCaller( g_Layout_viewStyle ) ), IntExportStringCaller( g_Layout_viewStyle.m_latched ) ); GlobalPreferenceSystem().registerPreference( "XYHeight", IntImportStringCaller( g_layout_globals.nXYHeight ), IntExportStringCaller( g_layout_globals.nXYHeight ) ); GlobalPreferenceSystem().registerPreference( "XYWidth", IntImportStringCaller( g_layout_globals.nXYWidth ), IntExportStringCaller( g_layout_globals.nXYWidth ) ); GlobalPreferenceSystem().registerPreference( "CamWidth", IntImportStringCaller( g_layout_globals.nCamWidth ), IntExportStringCaller( g_layout_globals.nCamWidth ) ); @@ -3747,15 +3747,6 @@ void MainFrame_Construct(){ } - - g_Layout_viewStyle.useLatched(); - g_Layout_enableDetachableMenus.useLatched(); - g_Layout_enableMainToolbar.useLatched(); - g_Layout_enablePatchToolbar.useLatched(); - g_Layout_enablePluginToolbar.useLatched(); - g_Layout_enableFilterToolbar.useLatched(); - g_Layout_SingleToolbar.useLatched(); - Layout_registerPreferencesPage(); Paths_registerPreferencesPage(); PreferencesDialog_addSettingsPreferences( FreeCaller1() ); diff --git a/radiant/multimon.cpp b/radiant/multimon.cpp index cab6d7ab..f3c4f670 100644 --- a/radiant/multimon.cpp +++ b/radiant/multimon.cpp @@ -36,7 +36,7 @@ void MultiMonitor_constructPreferences( PreferencesPage& page ){ page.appendCheckBox( "Multi Monitor", "Start on Primary Monitor", g_multimon_globals.m_bStartOnPrimMon ); // GtkWidget* popup = page.appendCheckBox( // "", "Disable system menu on popup windows", -// LatchedBoolImportCaller( g_Multimon_enableSysMenuPopups ), +// LatchedImportCaller( g_Multimon_enableSysMenuPopups ), // BoolExportCaller( g_Multimon_enableSysMenuPopups.m_latched ) // ); // Widget_connectToggleDependency( popup, primary_monitor ); @@ -89,9 +89,7 @@ void MultiMon_Construct(){ } GlobalPreferenceSystem().registerPreference( "StartOnPrimMon", BoolImportStringCaller( g_multimon_globals.m_bStartOnPrimMon ), BoolExportStringCaller( g_multimon_globals.m_bStartOnPrimMon ) ); -// GlobalPreferenceSystem().registerPreference( "NoSysMenuPopups", BoolImportStringCaller( g_Multimon_enableSysMenuPopups.m_latched ), BoolExportStringCaller( g_Multimon_enableSysMenuPopups.m_latched ) ); - -// g_Multimon_enableSysMenuPopups.useLatched(); +// GlobalPreferenceSystem().registerPreference( "NoSysMenuPopups", makeBoolStringImportCallback( LatchedAssignCaller( g_Multimon_enableSysMenuPopups ) ), BoolExportStringCaller( g_Multimon_enableSysMenuPopups.m_latched ) ); PreferencesDialog_addInterfacePreferences( FreeCaller1() ); } diff --git a/radiant/preferences.cpp b/radiant/preferences.cpp index f0c1079f..197a7596 100644 --- a/radiant/preferences.cpp +++ b/radiant/preferences.cpp @@ -150,7 +150,7 @@ void CGameDescription::Dump(){ } } -CGameDescription *g_pGameDescription; ///< shortcut to g_GamesDialog.m_pCurrentDescription +CGameDescription *g_pGameDescription; #include "warnings.h" @@ -224,7 +224,7 @@ typedef FreeCaller1 LogConsoleImportString void RegisterGlobalPreferences( PreferenceSystem& preferences ){ - preferences.registerPreference( "gamefile", CopiedStringImportStringCaller( g_GamesDialog.m_sGameFile ), CopiedStringExportStringCaller( g_GamesDialog.m_sGameFile ) ); + preferences.registerPreference( "gamefile", makeCopiedStringStringImportCallback( LatchedAssignCaller( g_GamesDialog.m_sGameFile ) ), CopiedStringExportStringCaller( g_GamesDialog.m_sGameFile.m_latched ) ); preferences.registerPreference( "gamePrompt", BoolImportStringCaller( g_GamesDialog.m_bGamePrompt ), BoolExportStringCaller( g_GamesDialog.m_bGamePrompt ) ); preferences.registerPreference( "log console", LogConsoleImportStringCaller(), BoolExportStringCaller( g_Console_enableLogging ) ); } @@ -267,15 +267,24 @@ void CGameDialog::DoGameDialog(){ SavePrefs(); } +CGameDescription* CGameDialog::GameDescriptionForComboItem(){ + return ( m_nComboSelect >= 0 && m_nComboSelect < mGames.size() )? + *std::next( mGames.begin(), m_nComboSelect ) + : 0; // not found +} + +void CGameDialog::GameFileAssign( int value ){ + m_nComboSelect = value; + // use value to set m_sGameFile + if( CGameDescription* iGame = GameDescriptionForComboItem() ) + m_sGameFile.assign( iGame->mGameFile ); +} + void CGameDialog::GameFileImport( int value ){ m_nComboSelect = value; // use value to set m_sGameFile - std::list::const_iterator iGame = std::next( mGames.begin(), value ); - - if ( ( *iGame )->mGameFile != m_sGameFile ) { - m_sGameFile = ( *iGame )->mGameFile; - PreferencesDialog_restartRequired( "Selected Game" ); - } + if( CGameDescription* iGame = GameDescriptionForComboItem() ) + m_sGameFile.import( iGame->mGameFile ); } void CGameDialog::GameFileExport( const IntImportCallback& importCallback ) const { @@ -284,7 +293,7 @@ void CGameDialog::GameFileExport( const IntImportCallback& importCallback ) cons int i = 0; for ( iGame = mGames.begin(); iGame != mGames.end(); ++iGame ) { - if ( ( *iGame )->mGameFile == m_sGameFile ) { + if ( ( *iGame )->mGameFile == m_sGameFile.m_latched ) { m_nComboSelect = i; break; } @@ -293,15 +302,7 @@ void CGameDialog::GameFileExport( const IntImportCallback& importCallback ) cons importCallback( m_nComboSelect ); } -void CGameDialog_GameFileImport( CGameDialog& self, int value ){ - self.GameFileImport( value ); -} - -void CGameDialog_GameFileExport( CGameDialog& self, const IntImportCallback& importCallback ){ - self.GameFileExport( importCallback ); -} - -void CGameDialog::CreateGlobalFrame( PreferencesPage& page ){ +void CGameDialog::CreateGlobalFrame( PreferencesPage& page, bool global ){ std::vector games; games.reserve( mGames.size() ); for ( std::list::iterator i = mGames.begin(); i != mGames.end(); ++i ) @@ -311,8 +312,10 @@ void CGameDialog::CreateGlobalFrame( PreferencesPage& page ){ page.appendCombo( "Select the game", StringArrayRange( &( *games.begin() ), &( *games.end() ) ), - ReferenceCaller1( *this ), - ReferenceCaller1( *this ) + global? + IntImportCallback( MemberCaller1( *this ) ): + IntImportCallback( MemberCaller1( *this ) ), + ConstMemberCaller1( *this ) ); page.appendCheckBox( "Startup", "Show Global Preferences", m_bGamePrompt ); } @@ -326,7 +329,7 @@ GtkWindow* CGameDialog::BuildDialog(){ { PreferencesPage preferencesPage( *this, GTK_WIDGET( vbox2 ) ); Global_constructPreferences( preferencesPage ); - CreateGlobalFrame( preferencesPage ); + CreateGlobalFrame( preferencesPage, true ); } return create_simple_modal_dialog_window( "Global Preferences", m_modal, GTK_WIDGET( frame ) ); @@ -349,7 +352,7 @@ void operator()( const char* name ) const { xmlDocPtr pDoc = xmlParseFile( strPath.c_str() ); if ( pDoc ) { - mGames.push_front( new CGameDescription( pDoc, name ) ); + mGames.push_back( new CGameDescription( pDoc, name ) ); xmlFreeDoc( pDoc ); } else @@ -378,18 +381,6 @@ void CGameDialog::ScanForGames(){ Directory_forEach( path, LoadGameFile( mGames, path ) ); } -CGameDescription* CGameDialog::GameDescriptionForComboItem(){ - std::list::iterator iGame; - int i = 0; - for ( iGame = mGames.begin(); iGame != mGames.end(); ++iGame,i++ ) - { - if ( i == m_nComboSelect ) { - return ( *iGame ); - } - } - return 0; // not found -} - void CGameDialog::InitGlobalPrefPath(){ g_Preferences.m_global_rc_path = g_string_new( SettingsPath_get() ); } @@ -412,17 +403,9 @@ void CGameDialog::Init(){ } else { - std::list::iterator iGame, iPrevGame; - for ( iGame = mGames.begin(), iPrevGame = mGames.end(); iGame != mGames.end(); iPrevGame = iGame, ++iGame ) - { - if ( iPrevGame != mGames.end() ) { - if ( strcmp( ( *iGame )->getRequiredKeyValue( "name" ), ( *iPrevGame )->getRequiredKeyValue( "name" ) ) < 0 ) { - CGameDescription *h = *iGame; - *iGame = *iPrevGame; - *iPrevGame = h; - } - } - } + mGames.sort( []( const CGameDescription* one, const CGameDescription* another ){ + return one->mGameFile < another->mGameFile; + } ); } CGameDescription* currentGameDescription = 0; @@ -432,7 +415,7 @@ void CGameDialog::Init(){ std::list::iterator iGame; for ( iGame = mGames.begin(); iGame != mGames.end(); ++iGame ) { - if ( ( *iGame )->mGameFile == m_sGameFile ) { + if ( ( *iGame )->mGameFile == m_sGameFile.m_value ) { currentGameDescription = ( *iGame ); break; } @@ -792,7 +775,7 @@ GtkWindow* PrefsDlg::BuildDialog(){ { GtkWidget* game = PreferencePages_addPage( m_notebook, "Game" ); PreferencesPage preferencesPage( *this, getVBox( game ) ); - g_GamesDialog.CreateGlobalFrame( preferencesPage ); + g_GamesDialog.CreateGlobalFrame( preferencesPage, false ); PreferenceTree_appendPage( store, &group, "Game", game ); } @@ -910,7 +893,7 @@ void Preferences_Load(){ globalOutputStream() << "loading local preferences from " << g_Preferences.m_inipath->str << "\n"; - if ( !Preferences_Load( g_preferences, g_Preferences.m_inipath->str, g_GamesDialog.m_sGameFile.c_str() ) ) { + if ( !Preferences_Load( g_preferences, g_Preferences.m_inipath->str, g_GamesDialog.m_sGameFile.m_value.c_str() ) ) { globalWarningStream() << "failed to load local preferences from " << g_Preferences.m_inipath->str << "\n"; } } diff --git a/radiant/preferences.h b/radiant/preferences.h index 33857a1e..96a96931 100644 --- a/radiant/preferences.h +++ b/radiant/preferences.h @@ -137,12 +137,12 @@ Value m_value; Value m_latched; const char* m_description; -LatchedValue( Value value, const char* description ) : m_latched( value ), m_description( description ){ +LatchedValue( Value value, const char* description ) : m_value( value ), m_latched( value ), m_description( description ){ } -void useLatched(){ - m_value = m_latched; +void assign( Value value ){ // assign during initialization + m_value = m_latched = value; } -void import( Value value ){ +void import( Value value ){ // import during runtime m_latched = value; if ( m_latched != m_value ) { PreferencesDialog_restartRequired( m_description ); @@ -151,10 +151,17 @@ void import( Value value ){ }; typedef LatchedValue LatchedBool; -typedef MemberCaller1 LatchedBoolImportCaller; - typedef LatchedValue LatchedInt; -typedef MemberCaller1 LatchedIntImportCaller; + +template, T, &LatchedValue::assign>> +inline R LatchedAssignCaller( LatchedValue& latchedValue ){ + return R( latchedValue ); +} + +template, T, &LatchedValue::import>> +inline R LatchedImportCaller( LatchedValue& latchedValue ){ + return R( latchedValue ); +} /*! holds information for a given game @@ -203,9 +210,6 @@ void Dump(); extern CGameDescription *g_pGameDescription; typedef struct _GtkWidget GtkWidget; -class PrefsDlg; - -class PreferencesPage; class StringOutputStream; @@ -231,7 +235,7 @@ public: what game has been selected this is the name of the .game file */ -CopiedString m_sGameFile; +LatchedValue m_sGameFile; /*! prompt which game to load on startup */ @@ -249,7 +253,7 @@ bool m_bForceLogConsole; std::list mGames; CGameDialog() : - m_sGameFile( "" ), + m_sGameFile( "", "Selected Game" ), m_bGamePrompt( false ), m_bForceLogConsole( false ){ } @@ -279,6 +283,7 @@ void DoGameDialog(); */ GtkWindow* BuildDialog(); +void GameFileAssign( int value ); void GameFileImport( int value ); void GameFileExport( const IntImportCallback& importCallback ) const; @@ -289,7 +294,8 @@ void GameFileExport( const IntImportCallback& importCallback ) const; for prefs, we hook the frame in the main notebook build the frame on-demand (only once) */ -void CreateGlobalFrame( PreferencesPage& page ); +/// \brief \p global controls if to use LatchedValue assignment callback (global settings dialog) or import (preferences dialog). +void CreateGlobalFrame( PreferencesPage& page, bool global ); /*! global preferences subsystem @@ -328,10 +334,6 @@ extern CGameDialog g_GamesDialog; class PrefsDlg : public Dialog { -public: -protected: -std::list mGames; - public: GtkWidget *m_notebook;