Counter Strike : Global Offensive Source Code
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.

446 lines
11 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <time.h>
  8. #include "multiplayeradvanceddialog.h"
  9. #include <vgui/ILocalize.h>
  10. #include <vgui/ISurface.h>
  11. #include <vgui_controls/ListPanel.h>
  12. #include <keyvalues.h>
  13. #include <vgui_controls/Label.h>
  14. #include <vgui_controls/Button.h>
  15. #include <vgui_controls/MessageBox.h>
  16. #include <vgui_controls/CheckButton.h>
  17. #include <vgui_controls/ComboBox.h>
  18. #include <vgui_controls/TextEntry.h>
  19. #include "panellistpanel.h"
  20. #include <vgui/IInput.h>
  21. #include "filesystem.h"
  22. // memdbgon must be the last include file in a .cpp file!!!
  23. #include "tier0/memdbgon.h"
  24. using namespace vgui;
  25. #define OPTIONS_DIR "cfg"
  26. #define DEFAULT_OPTIONS_FILE OPTIONS_DIR "/user_default.scr"
  27. #define OPTIONS_FILE OPTIONS_DIR "/user.scr"
  28. //-----------------------------------------------------------------------------
  29. // Purpose: Constructor
  30. //-----------------------------------------------------------------------------
  31. CMultiplayerAdvancedDialog::CMultiplayerAdvancedDialog(vgui::Panel *parent) : BaseClass(NULL, "MultiplayerAdvancedDialog")
  32. {
  33. SetBounds(0, 0, 372, 160);
  34. SetSizeable( false );
  35. SetTitle("#GameUI_MultiplayerAdvanced", true);
  36. Button *cancel = new Button( this, "Cancel", "#GameUI_Cancel" );
  37. cancel->SetCommand( "Close" );
  38. Button *ok = new Button( this, "OK", "#GameUI_OK" );
  39. ok->SetCommand( "Ok" );
  40. m_pListPanel = new CPanelListPanel( this, "PanelListPanel" );
  41. m_pList = NULL;
  42. m_pDescription = new CInfoDescription( m_pListPanel );
  43. m_pDescription->InitFromFile( DEFAULT_OPTIONS_FILE );
  44. m_pDescription->InitFromFile( OPTIONS_FILE );
  45. m_pDescription->TransferCurrentValues( NULL );
  46. LoadControlSettings("Resource\\MultiplayerAdvancedDialog.res");
  47. CreateControls();
  48. MoveToCenterOfScreen();
  49. SetSizeable( false );
  50. SetDeleteSelfOnClose( true );
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Purpose: Destructor
  54. //-----------------------------------------------------------------------------
  55. CMultiplayerAdvancedDialog::~CMultiplayerAdvancedDialog()
  56. {
  57. delete m_pDescription;
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose:
  61. //-----------------------------------------------------------------------------
  62. void CMultiplayerAdvancedDialog::Activate()
  63. {
  64. BaseClass::Activate();
  65. input()->SetAppModalSurface(GetVPanel());
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose:
  69. //-----------------------------------------------------------------------------
  70. void CMultiplayerAdvancedDialog::OnClose()
  71. {
  72. BaseClass::OnClose();
  73. MarkForDeletion();
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Purpose:
  77. // Input : *command -
  78. //-----------------------------------------------------------------------------
  79. void CMultiplayerAdvancedDialog::OnCommand( const char *command )
  80. {
  81. if ( !stricmp( command, "Ok" ) )
  82. {
  83. // OnApplyChanges();
  84. SaveValues();
  85. OnClose();
  86. return;
  87. }
  88. BaseClass::OnCommand( command );
  89. }
  90. void CMultiplayerAdvancedDialog::OnKeyCodeTyped(KeyCode code)
  91. {
  92. // force ourselves to be closed if the escape key it pressed
  93. if (code == KEY_ESCAPE)
  94. {
  95. Close();
  96. }
  97. else
  98. {
  99. BaseClass::OnKeyCodeTyped(code);
  100. }
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose:
  104. //-----------------------------------------------------------------------------
  105. void CMultiplayerAdvancedDialog::GatherCurrentValues()
  106. {
  107. if ( !m_pDescription )
  108. return;
  109. // OK
  110. CheckButton *pBox;
  111. TextEntry *pEdit;
  112. ComboBox *pCombo;
  113. mpcontrol_t *pList;
  114. CScriptObject *pObj;
  115. CScriptListItem *pItem;
  116. char szValue[256];
  117. char strValue[ 256 ];
  118. pList = m_pList;
  119. while ( pList )
  120. {
  121. pObj = pList->pScrObj;
  122. if ( !pList->pControl )
  123. {
  124. pObj->SetCurValue( pObj->defValue );
  125. pList = pList->next;
  126. continue;
  127. }
  128. switch ( pObj->type )
  129. {
  130. case O_BOOL:
  131. pBox = (CheckButton *)pList->pControl;
  132. Q_snprintf( szValue, 256, "%s", pBox->IsSelected() ? "1" : "0" );
  133. break;
  134. case O_NUMBER:
  135. pEdit = ( TextEntry * )pList->pControl;
  136. pEdit->GetText( strValue, sizeof( strValue ) );
  137. Q_snprintf( szValue, 256, "%s", strValue );
  138. break;
  139. case O_STRING:
  140. pEdit = ( TextEntry * )pList->pControl;
  141. pEdit->GetText( strValue, sizeof( strValue ) );
  142. Q_snprintf( szValue, 256, "%s", strValue );
  143. break;
  144. case O_LIST:
  145. pCombo = (ComboBox *)pList->pControl;
  146. // pCombo->GetText( strValue, sizeof( strValue ) );
  147. int activeItem = pCombo->GetActiveItem();
  148. pItem = pObj->pListItems;
  149. // int n = (int)pObj->fdefValue;
  150. while ( pItem )
  151. {
  152. if (!activeItem--)
  153. break;
  154. pItem = pItem->pNext;
  155. }
  156. if ( pItem )
  157. {
  158. Q_snprintf( szValue, 256, "%s", pItem->szValue );
  159. }
  160. else // Couln't find index
  161. {
  162. //assert(!("Couldn't find string in list, using default value"));
  163. Q_snprintf( szValue, 256, "%s", pObj->defValue );
  164. }
  165. break;
  166. }
  167. // Remove double quotes and % characters
  168. UTIL_StripInvalidCharacters( szValue, sizeof(szValue) );
  169. strcpy( strValue, szValue );
  170. pObj->SetCurValue( strValue );
  171. pList = pList->next;
  172. }
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Purpose:
  176. //-----------------------------------------------------------------------------
  177. void CMultiplayerAdvancedDialog::CreateControls()
  178. {
  179. DestroyControls();
  180. // Go through desciption creating controls
  181. CScriptObject *pObj;
  182. pObj = m_pDescription->pObjList;
  183. mpcontrol_t *pCtrl;
  184. CheckButton *pBox;
  185. TextEntry *pEdit;
  186. ComboBox *pCombo;
  187. CScriptListItem *pListItem;
  188. Panel *objParent = m_pListPanel;
  189. while ( pObj )
  190. {
  191. if ( pObj->type == O_OBSOLETE )
  192. {
  193. pObj = pObj->pNext;
  194. continue;
  195. }
  196. pCtrl = new mpcontrol_t( objParent, "mpcontrol_t" );
  197. pCtrl->type = pObj->type;
  198. switch ( pCtrl->type )
  199. {
  200. case O_BOOL:
  201. pBox = new CheckButton( pCtrl, "DescCheckButton", pObj->prompt );
  202. pBox->SetSelected( pObj->fdefValue != 0.0f ? true : false );
  203. pCtrl->pControl = (Panel *)pBox;
  204. break;
  205. case O_STRING:
  206. case O_NUMBER:
  207. pEdit = new TextEntry( pCtrl, "DescTextEntry");
  208. pEdit->InsertString(pObj->defValue);
  209. pCtrl->pControl = (Panel *)pEdit;
  210. break;
  211. case O_LIST:
  212. pCombo = new ComboBox( pCtrl, "DescComboBox", 5, false );
  213. pListItem = pObj->pListItems;
  214. while ( pListItem )
  215. {
  216. pCombo->AddItem( pListItem->szItemText, NULL );
  217. pListItem = pListItem->pNext;
  218. }
  219. pCombo->ActivateItemByRow((int)pObj->fdefValue);
  220. pCtrl->pControl = (Panel *)pCombo;
  221. break;
  222. default:
  223. break;
  224. }
  225. if ( pCtrl->type != O_BOOL )
  226. {
  227. pCtrl->pPrompt = new vgui::Label( pCtrl, "DescLabel", "" );
  228. pCtrl->pPrompt->SetContentAlignment( vgui::Label::a_west );
  229. pCtrl->pPrompt->SetTextInset( 5, 0 );
  230. pCtrl->pPrompt->SetText( pObj->prompt );
  231. }
  232. pCtrl->pScrObj = pObj;
  233. pCtrl->SetSize( 100, 28 );
  234. //pCtrl->SetBorder( scheme()->GetBorder(1, "DepressedButtonBorder") );
  235. m_pListPanel->AddItem( pCtrl );
  236. // Link it in
  237. if ( !m_pList )
  238. {
  239. m_pList = pCtrl;
  240. pCtrl->next = NULL;
  241. }
  242. else
  243. {
  244. mpcontrol_t *p;
  245. p = m_pList;
  246. while ( p )
  247. {
  248. if ( !p->next )
  249. {
  250. p->next = pCtrl;
  251. pCtrl->next = NULL;
  252. break;
  253. }
  254. p = p->next;
  255. }
  256. }
  257. pObj = pObj->pNext;
  258. }
  259. }
  260. //-----------------------------------------------------------------------------
  261. // Purpose:
  262. //-----------------------------------------------------------------------------
  263. void CMultiplayerAdvancedDialog::DestroyControls()
  264. {
  265. mpcontrol_t *p, *n;
  266. p = m_pList;
  267. while ( p )
  268. {
  269. n = p->next;
  270. //
  271. delete p->pControl;
  272. delete p->pPrompt;
  273. delete p;
  274. p = n;
  275. }
  276. m_pList = NULL;
  277. }
  278. //-----------------------------------------------------------------------------
  279. // Purpose:
  280. //-----------------------------------------------------------------------------
  281. void CMultiplayerAdvancedDialog::SaveValues()
  282. {
  283. // Get the values from the controls:
  284. GatherCurrentValues();
  285. // Create the game.cfg file
  286. if ( m_pDescription )
  287. {
  288. FileHandle_t fp;
  289. // Add settings to config.cfg
  290. m_pDescription->WriteToConfig();
  291. g_pFullFileSystem->CreateDirHierarchy( OPTIONS_DIR );
  292. fp = g_pFullFileSystem->Open( OPTIONS_FILE, "wb" );
  293. if ( fp )
  294. {
  295. m_pDescription->WriteToScriptFile( fp );
  296. g_pFullFileSystem->Close( fp );
  297. }
  298. }
  299. }
  300. //-----------------------------------------------------------------------------
  301. // Purpose: Constructor, load/save client settings object
  302. //-----------------------------------------------------------------------------
  303. CInfoDescription::CInfoDescription( CPanelListPanel *panel )
  304. : CDescription( panel )
  305. {
  306. setHint( "// NOTE: THIS FILE IS AUTOMATICALLY REGENERATED, \r\n\
  307. //DO NOT EDIT THIS HEADER, YOUR COMMENTS WILL BE LOST IF YOU DO\r\n\
  308. // User options script\r\n\
  309. //\r\n\
  310. // Format:\r\n\
  311. // Version [float]\r\n\
  312. // Options description followed by \r\n\
  313. // Options defaults\r\n\
  314. //\r\n\
  315. // Option description syntax:\r\n\
  316. //\r\n\
  317. // \"cvar\" { \"Prompt\" { type [ type info ] } { default } }\r\n\
  318. //\r\n\
  319. // type = \r\n\
  320. // BOOL (a yes/no toggle)\r\n\
  321. // STRING\r\n\
  322. // NUMBER\r\n\
  323. // LIST\r\n\
  324. //\r\n\
  325. // type info:\r\n\
  326. // BOOL no type info\r\n\
  327. // NUMBER min max range, use -1 -1 for no limits\r\n\
  328. // STRING no type info\r\n\
  329. // LIST "" delimited list of options value pairs\r\n\
  330. //\r\n\
  331. //\r\n\
  332. // default depends on type\r\n\
  333. // BOOL is \"0\" or \"1\"\r\n\
  334. // NUMBER is \"value\"\r\n\
  335. // STRING is \"value\"\r\n\
  336. // LIST is \"index\", where index \"0\" is the first element of the list\r\n\r\n\r\n" );
  337. setDescription( "INFO_OPTIONS" );
  338. }
  339. //-----------------------------------------------------------------------------
  340. // Purpose:
  341. //-----------------------------------------------------------------------------
  342. void CInfoDescription::WriteScriptHeader( FileHandle_t fp )
  343. {
  344. char am_pm[] = "AM";
  345. tm newtime;
  346. Plat_GetLocalTime( &newtime );
  347. g_pFullFileSystem->FPrintf( fp, "%s", (char *)getHint() );
  348. char timeString[64];
  349. Plat_GetTimeString( &newtime, timeString, sizeof( timeString ) );
  350. // Write out the comment and Cvar Info:
  351. g_pFullFileSystem->FPrintf( fp, "// Half-Life User Info Configuration Layout Script (stores last settings chosen, too)\r\n" );
  352. g_pFullFileSystem->FPrintf( fp, "// File generated: %.19s %s\r\n", timeString, am_pm );
  353. g_pFullFileSystem->FPrintf( fp, "//\r\n//\r\n// Cvar\t-\tSetting\r\n\r\n" );
  354. g_pFullFileSystem->FPrintf( fp, "VERSION %.1f\r\n\r\n", SCRIPT_VERSION );
  355. g_pFullFileSystem->FPrintf( fp, "DESCRIPTION INFO_OPTIONS\r\n{\r\n" );
  356. }
  357. //-----------------------------------------------------------------------------
  358. // Purpose:
  359. //-----------------------------------------------------------------------------
  360. void CInfoDescription::WriteFileHeader( FileHandle_t fp )
  361. {
  362. char am_pm[] = "AM";
  363. tm newtime;
  364. Plat_GetLocalTime( &newtime );
  365. char timeString[64];
  366. Plat_GetTimeString( &newtime, timeString, sizeof( timeString ) );
  367. g_pFullFileSystem->FPrintf( fp, "// Half-Life User Info Configuration Settings\r\n" );
  368. g_pFullFileSystem->FPrintf( fp, "// DO NOT EDIT, GENERATED BY HALF-LIFE\r\n" );
  369. g_pFullFileSystem->FPrintf( fp, "// File generated: %.19s %s\r\n", timeString, am_pm );
  370. g_pFullFileSystem->FPrintf( fp, "//\r\n//\r\n// Cvar\t-\tSetting\r\n\r\n" );
  371. }
  372. //-----------------------------------------------------------------------------