Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
8.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Dialog for selecting game configurations
  4. //
  5. //=====================================================================================//
  6. #include <windows.h>
  7. #include <vgui/IVGui.h>
  8. #include <vgui/IInput.h>
  9. #include <vgui/ISystem.h>
  10. #include <vgui_controls/ComboBox.h>
  11. #include <vgui_controls/MessageBox.h>
  12. #include <KeyValues.h>
  13. #include "vconfig_main.h"
  14. #include "VConfigDialog.h"
  15. #include "ManageGamesDialog.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include <tier0/memdbgon.h>
  18. using namespace vgui;
  19. CVConfigDialog *g_pVConfigDialog = NULL;
  20. class CConversionInfoMessageBox : public vgui::Frame
  21. {
  22. public:
  23. typedef vgui::Frame BaseClass;
  24. CConversionInfoMessageBox( Panel *pParent, const char *pPanelName )
  25. : BaseClass( pParent, pPanelName )
  26. {
  27. SetSize( 200, 200 );
  28. SetMinimumSize( 250, 100 );
  29. SetSizeable( false );
  30. LoadControlSettings( "convinfobox.res" );
  31. }
  32. virtual void OnCommand( const char *command )
  33. {
  34. BaseClass::OnCommand( command );
  35. // For some weird reason, this dialog can
  36. if ( Q_stricmp( command, "ShowFAQ" ) == 0 )
  37. {
  38. system()->ShellExecute( "open", "http://www.valve-erc.com/srcsdk/faq.html#convertINI" );
  39. }
  40. }
  41. };
  42. class CModalPreserveMessageBox : public vgui::MessageBox
  43. {
  44. public:
  45. CModalPreserveMessageBox(const char *title, const char *text, vgui::Panel *parent)
  46. : vgui::MessageBox( title, text, parent )
  47. {
  48. m_PrevAppFocusPanel = vgui::input()->GetAppModalSurface();
  49. }
  50. ~CModalPreserveMessageBox()
  51. {
  52. vgui::input()->SetAppModalSurface( m_PrevAppFocusPanel );
  53. }
  54. public:
  55. vgui::VPANEL m_PrevAppFocusPanel;
  56. };
  57. //-----------------------------------------------------------------------------
  58. // Purpose: Utility function to pop up a VGUI message box
  59. //-----------------------------------------------------------------------------
  60. void VGUIMessageBox( vgui::Panel *pParent, const char *pTitle, const char *pMsg, ... )
  61. {
  62. char msg[4096];
  63. va_list marker;
  64. va_start( marker, pMsg );
  65. Q_vsnprintf( msg, sizeof( msg ), pMsg, marker );
  66. va_end( marker );
  67. vgui::MessageBox *dlg = new CModalPreserveMessageBox( pTitle, msg, pParent );
  68. dlg->DoModal();
  69. dlg->Activate();
  70. dlg->RequestFocus();
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Constructor
  74. //-----------------------------------------------------------------------------
  75. CVConfigDialog::CVConfigDialog( Panel *parent, const char *name ) : BaseClass( parent, name ), m_bChanged( false )
  76. {
  77. Assert( !g_pVConfigDialog );
  78. g_pVConfigDialog = this;
  79. SetSize(384, 420);
  80. SetMinimumSize(200, 100);
  81. SetMinimizeButtonVisible( true );
  82. m_pConfigCombo = new ComboBox( this, "ConfigCombo", 8, false );
  83. PopulateConfigList();
  84. LoadControlSettings( "VConfigDialog.res" );
  85. // See if we converted on load and notify
  86. if ( g_ConfigManager.WasConvertedOnLoad() )
  87. {
  88. //VGUIMessageBox( this, "Update Occured", "Your game configurations have been updated.\n\nA backup file GameCfg.INI.OLD has been created.\n\nPlease visit http://www.valve-erc.com/srcsdk/faq.html#GameConfigUpdate for more information." );
  89. CConversionInfoMessageBox *pDlg = new CConversionInfoMessageBox( this, "ConversionInfo" );
  90. pDlg->RequestFocus();
  91. pDlg->SetVisible( true );
  92. pDlg->MoveToCenterOfScreen();
  93. input()->SetAppModalSurface( pDlg->GetVPanel() );
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Destructor
  98. //-----------------------------------------------------------------------------
  99. CVConfigDialog::~CVConfigDialog()
  100. {
  101. delete m_pConfigCombo;
  102. g_pVConfigDialog = NULL;
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Purpose: Populate the configuration list for selection
  106. //-----------------------------------------------------------------------------
  107. void CVConfigDialog::PopulateConfigList( bool bSelectActiveConfig /*=true*/ )
  108. {
  109. int activeItem = -1;
  110. char szKeyValue[1024] = "\0";
  111. if ( bSelectActiveConfig )
  112. {
  113. // Get the currently set game configuration
  114. if ( GetVConfigRegistrySetting( GAMEDIR_TOKEN, szKeyValue, sizeof( szKeyValue ) ) == false )
  115. {
  116. //NOTE: We may want to pop an info dialog here if there was no initial VPROJECT setting
  117. activeItem = 0;
  118. }
  119. }
  120. else
  121. {
  122. activeItem = m_pConfigCombo->GetActiveItem();
  123. }
  124. // Purge all our items
  125. m_pConfigCombo->DeleteAllItems();
  126. KeyValues *kv = new KeyValues( "Items" );
  127. // Add all configurations
  128. for ( int i = 0; i < g_Configs.Count(); i++ )
  129. {
  130. // Set the text
  131. kv->SetString( "ModDir", g_Configs[i]->m_ModDir.Base() );
  132. // Add the item into the list
  133. int index = m_pConfigCombo->AddItem( g_Configs[i]->m_Name.Base(), kv );
  134. if ( bSelectActiveConfig )
  135. {
  136. if ( !Q_stricmp( g_Configs[i]->m_ModDir.Base(), szKeyValue ) )
  137. {
  138. activeItem = index;
  139. }
  140. }
  141. }
  142. // Make sure we have an active item
  143. if ( activeItem < 0 )
  144. {
  145. // Give a warning if they have a mismatched directory!
  146. VGUIMessageBox( this, "Invalid Game Directory", "The currently selected game directory: %s is invalid.\nChoose a new directory, or select 'Cancel' to exit.\n", szKeyValue );
  147. // Default to the first config in the list
  148. activeItem = 0;
  149. }
  150. // Set us to the active config
  151. m_pConfigCombo->ActivateItem( activeItem );
  152. kv->deleteThis();
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose: Kills the whole app on close
  156. //-----------------------------------------------------------------------------
  157. void CVConfigDialog::OnClose( void )
  158. {
  159. BaseClass::OnClose();
  160. ivgui()->Stop();
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Purpose: Select the item from the list (updating the environment variable as well)
  164. // Input : index - item to select
  165. //-----------------------------------------------------------------------------
  166. void CVConfigDialog::SetGlobalConfig( const char *modDir )
  167. {
  168. // Set our environment variable
  169. SetMayaScriptSettings( );
  170. SetXSIScriptSettings( );
  171. SetPathSettings( );
  172. SetVConfigRegistrySetting( GAMEDIR_TOKEN, modDir );
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Purpose: Parse commands coming in from the VGUI dialog
  176. //-----------------------------------------------------------------------------
  177. void CVConfigDialog::OnCommand( const char *command )
  178. {
  179. if ( Q_stricmp( command, "Select" ) == 0 )
  180. {
  181. int activeID = m_pConfigCombo->GetActiveItem();
  182. SetGlobalConfig( g_Configs[activeID]->m_ModDir.Base() );
  183. // Save off our data
  184. if ( m_bChanged )
  185. {
  186. SaveConfigs();
  187. }
  188. Close();
  189. }
  190. else if ( Q_stricmp( command, "Manage" ) == 0 )
  191. {
  192. int activeID = m_pConfigCombo->GetActiveItem();
  193. // Launch the edit window
  194. CManageGamesDialog *pDialog = new CManageGamesDialog( this, "ManageGamesDialog", activeID );
  195. pDialog->AddActionSignalTarget( this );
  196. pDialog->SetGameDir( g_Configs[activeID]->m_ModDir.Base() );
  197. pDialog->SetGameName( g_Configs[activeID]->m_Name.Base() );
  198. pDialog->DoModal();
  199. }
  200. else if ( Q_stricmp( command, "AddConfig" ) == 0 )
  201. {
  202. // Launch the edit window, specifying that we're adding a config
  203. CManageGamesDialog *pDialog = new CManageGamesDialog( this, "ManageGamesDialog", NEW_CONFIG_ID );
  204. pDialog->AddActionSignalTarget( this );
  205. pDialog->DoModal();
  206. }
  207. else if ( Q_stricmp( command, "RemoveConfig" ) == 0 )
  208. {
  209. // Don't allow the list to completely vanish
  210. // NOTE: We should display the list as being empty, i.e. "<EMPTY>"
  211. if ( g_Configs.Count() <= 1 )
  212. {
  213. VGUIMessageBox( this, "Error", "Cannot remove last configuration from list!" );
  214. return;
  215. }
  216. // Remove this config from our list
  217. int activeID = m_pConfigCombo->GetActiveItem();
  218. RemoveConfig( activeID );
  219. ReloadConfigs( true );
  220. // Select the next entry
  221. m_pConfigCombo->ActivateItem( g_Configs.Count()-1 );
  222. // Mark that we changed our configs
  223. m_bChanged = true;
  224. }
  225. BaseClass::OnCommand( command );
  226. }
  227. //-----------------------------------------------------------------------------
  228. // Purpose: Manage dialog has reported a need to update
  229. //-----------------------------------------------------------------------------
  230. void CVConfigDialog::OnManageSelect( void )
  231. {
  232. // Publish the config changes to the internal data in the configuration manager
  233. UpdateConfigs();
  234. // Update the configuration list
  235. PopulateConfigList( false );
  236. m_bChanged = true;
  237. }
  238. //-----------------------------------------------------------------------------
  239. // Purpose: Manage dialog has reported that it has added a configuration
  240. //-----------------------------------------------------------------------------
  241. void CVConfigDialog::OnAddSelect( void )
  242. {
  243. // Add the last config we entered
  244. AddConfig( g_Configs.Count()-1 );
  245. // Re-populate the configuration list
  246. ReloadConfigs();
  247. // Select the last entry (which will be the new one)
  248. m_pConfigCombo->ActivateItem( g_Configs.Count()-1 );
  249. // Mark us as changed
  250. m_bChanged = true;
  251. }