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.

203 lines
5.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include <windows.h>
  7. #include <vgui/IVGui.h>
  8. #include <vgui_controls/DirectorySelectDialog.h>
  9. #include <vgui/IInput.h>
  10. #include <KeyValues.h>
  11. #include "vconfig_main.h"
  12. #include "ManageGamesDialog.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include <tier0/memdbgon.h>
  15. using namespace vgui;
  16. CManageGamesDialog *g_pManageGamesDialog = NULL;
  17. class CModalDirectorySelectDialog : public vgui::DirectorySelectDialog
  18. {
  19. public:
  20. CModalDirectorySelectDialog( vgui::Panel *parent, const char *title )
  21. : vgui::DirectorySelectDialog( parent, title )
  22. {
  23. m_PrevAppFocusPanel = vgui::input()->GetAppModalSurface();
  24. }
  25. ~CModalDirectorySelectDialog( void )
  26. {
  27. vgui::input()->SetAppModalSurface( m_PrevAppFocusPanel );
  28. }
  29. public:
  30. vgui::VPANEL m_PrevAppFocusPanel;
  31. };
  32. //-----------------------------------------------------------------------------
  33. // Constructor
  34. //-----------------------------------------------------------------------------
  35. CManageGamesDialog::CManageGamesDialog( Panel *parent, const char *name, int configID ) : BaseClass( parent, name ), m_nConfigID( configID )
  36. {
  37. Assert( !g_pManageGamesDialog );
  38. g_pManageGamesDialog = this;
  39. SetSize(384, 420);
  40. SetMinimumSize(200, 50);
  41. SetMinimizeButtonVisible( false );
  42. m_pGameNameEntry= new vgui::TextEntry( this, "GameName" );
  43. m_pGameDirEntry = new vgui::TextEntry( this, "GamePath" );
  44. LoadControlSettings( "ManageGamesDialog.res" );
  45. SetDeleteSelfOnClose( true );
  46. SetSizeable( false );
  47. MoveToCenterOfScreen();
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Destructor
  51. //-----------------------------------------------------------------------------
  52. CManageGamesDialog::~CManageGamesDialog( void )
  53. {
  54. g_pManageGamesDialog = NULL;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose: Sets the game directory
  58. //-----------------------------------------------------------------------------
  59. void CManageGamesDialog::SetGameDir( const char *szDir )
  60. {
  61. // Strip any trailing slashes
  62. char szGameDir[MAX_PATH];
  63. Q_strncpy( szGameDir, szDir, MAX_PATH );
  64. Q_StripTrailingSlash( szGameDir );
  65. m_pGameDirEntry->SetText( szGameDir );
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose: Set the game name
  69. //-----------------------------------------------------------------------------
  70. void CManageGamesDialog::SetGameName( const char *szDir )
  71. {
  72. m_pGameNameEntry->SetText( szDir );
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose: Ensures the parameter name is unique
  76. // Input : *name - name to test
  77. // Output : Returns true on success, false on failure.
  78. //-----------------------------------------------------------------------------
  79. bool CManageGamesDialog::IsGameNameUnique( const char *name )
  80. {
  81. // Check all names
  82. for ( int i = 0; i < g_Configs.Count(); i++ )
  83. {
  84. // Skip ourself
  85. if ( i == m_nConfigID )
  86. continue;
  87. // Test it
  88. if ( Q_stricmp( name, g_Configs[i]->m_Name.Base() ) == 0 )
  89. return false;
  90. }
  91. return true;
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Purpose: Handles dialog commands
  95. //-----------------------------------------------------------------------------
  96. void CManageGamesDialog::OnCommand( const char *command )
  97. {
  98. // Handle "OK" button
  99. if ( Q_stricmp( command, "Select" ) == 0 )
  100. {
  101. char textBuffer[1024];
  102. // Save out the data
  103. m_pGameNameEntry->GetText( textBuffer, sizeof( textBuffer ) );
  104. // Make sure we're not setting this to a duplicate
  105. if ( IsGameNameUnique( textBuffer ) == false )
  106. {
  107. // Select the text
  108. m_pGameNameEntry->SelectAllText( true );
  109. // Pop a message box and refuse to close
  110. VGUIMessageBox( this, "Error", "Game name %s already exists! Please enter a unique name.", textBuffer );
  111. BaseClass::OnCommand( command );
  112. return;
  113. }
  114. KeyValues *actionSignal = new KeyValues("ManageSelect");
  115. if ( actionSignal == NULL )
  116. {
  117. Assert( 0 );
  118. return;
  119. }
  120. // See if we need to add a new config
  121. if ( m_nConfigID == NEW_CONFIG_ID )
  122. {
  123. // Create a new data container and point to it
  124. m_nConfigID = g_Configs.AddToTail( new CGameConfig() );
  125. // Send an overidden action signal to notify that we've added, not edited a field
  126. actionSignal->SetName("AddSelect");
  127. }
  128. // Otherwise take the name
  129. UtlStrcpy( g_Configs[m_nConfigID]->m_Name, textBuffer );
  130. // Take the game directory
  131. m_pGameDirEntry->GetText( textBuffer, sizeof( textBuffer ) );
  132. // Strip off the trailing slash always
  133. Q_StripTrailingSlash( textBuffer );
  134. UtlStrcpy( g_Configs[m_nConfigID]->m_ModDir, textBuffer );
  135. // Tell the parent we altered its data so it can refresh
  136. PostActionSignal( actionSignal );
  137. Close();
  138. }
  139. // Modified to allow more than one browse button
  140. else if ( Q_stricmp( command, "BrowseDir" ) == 0 )
  141. {
  142. // Create a new dialog
  143. CModalDirectorySelectDialog *pDlg = vgui::SETUP_PANEL( new CModalDirectorySelectDialog( this, "Select Game Directory" ) );
  144. char textBuffer[1024];
  145. m_pGameDirEntry->GetText( textBuffer, sizeof( textBuffer ) );
  146. // Get the currently set dir and use that as the start
  147. pDlg->ExpandTreeToPath( textBuffer );
  148. pDlg->MoveToCenterOfScreen();
  149. pDlg->AddActionSignalTarget( this );
  150. pDlg->SetDeleteSelfOnClose( true );
  151. pDlg->DoModal();
  152. }
  153. BaseClass::OnCommand( command );
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Purpose: Notify us that the directory dialog has returned a new entry
  157. //-----------------------------------------------------------------------------
  158. void CManageGamesDialog::OnChooseDirectory( const char *dir )
  159. {
  160. SetGameDir( dir );
  161. }