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.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "VarEditDialog.h"
  8. #include "RemoteServer.h"
  9. #include <stdio.h>
  10. #include <vgui/IInput.h>
  11. #include <vgui_controls/Button.h>
  12. #include <vgui_controls/ComboBox.h>
  13. #include <vgui_controls/TextEntry.h>
  14. #include <KeyValues.h>
  15. using namespace vgui;
  16. //-----------------------------------------------------------------------------
  17. // Purpose: Constructor
  18. //-----------------------------------------------------------------------------
  19. CVarEditDialog::CVarEditDialog(vgui::Panel *parent, const char *name) : Frame(parent, name)
  20. {
  21. SetSize(280, 180);
  22. SetSizeable(false);
  23. m_pOKButton = new Button(this, "OKButton", "OK");
  24. m_pCancelButton = new Button(this, "CancelButton", "Cancel");
  25. m_pStringEdit = new TextEntry(this, "StringEdit");
  26. m_pComboEdit = new ComboBox(this, "ComboEdit", 12, false);
  27. m_pRules = NULL;
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Purpose: Destructor
  31. //-----------------------------------------------------------------------------
  32. CVarEditDialog::~CVarEditDialog()
  33. {
  34. // input()->ReleaseAppModalSurface();
  35. if (m_pRules)
  36. {
  37. m_pRules->deleteThis();
  38. }
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Purpose: Configures and shows the var edit dialog
  42. //-----------------------------------------------------------------------------
  43. void CVarEditDialog::Activate(vgui::Panel *actionSignalTarget, KeyValues *rules)
  44. {
  45. // configure
  46. AddActionSignalTarget(actionSignalTarget);
  47. m_pRules = rules->MakeCopy();
  48. const char *type = m_pRules->GetString("type");
  49. if (!stricmp(type, "enumeration"))
  50. {
  51. LoadControlSettings("Admin/VarEditDialog_ComboBox.res", "PLATFORM");
  52. m_pStringEdit->SetVisible(false);
  53. // fill in the combo box
  54. for (KeyValues *kv = m_pRules->FindKey("list", true)->GetFirstSubKey(); kv != NULL; kv = kv->GetNextKey())
  55. {
  56. Assert( 0 );
  57. // FIXME: This Assert doesn't compile
  58. // Assert(index++ == atoi(kv->GetName()));
  59. m_pComboEdit->AddItem(kv->GetString(), NULL);
  60. }
  61. // activate the current item
  62. m_pComboEdit->ActivateItemByRow(m_pRules->GetInt("enum"));
  63. }
  64. else if (!stricmp(type, "customlist"))
  65. {
  66. LoadControlSettings("Admin/VarEditDialog_ComboBox.res", "PLATFORM");
  67. m_pStringEdit->SetVisible(false);
  68. // fill in the combo box
  69. int index = 0;
  70. const char *currentValue = m_pRules->GetString("value");
  71. const char *parse = m_pRules->GetString("stringlist");
  72. while (*parse)
  73. {
  74. // newline-seperated map list
  75. if (*parse == '\n')
  76. {
  77. parse++;
  78. continue;
  79. }
  80. // pull out the map name
  81. const char *end = strstr(parse, "\n");
  82. if (!end)
  83. break;
  84. char customString[64];
  85. int nameSize = end - parse;
  86. if (nameSize >= sizeof(customString))
  87. {
  88. nameSize = sizeof(customString) - 1;
  89. }
  90. // copy in the name
  91. strncpy(customString, parse, nameSize);
  92. customString[nameSize] = 0;
  93. parse = end;
  94. // add to dropdown
  95. int itemID = m_pComboEdit->AddItem(customString, NULL);
  96. index++;
  97. // activate the current item
  98. if (!stricmp(customString, currentValue))
  99. {
  100. m_pComboEdit->ActivateItem(itemID);
  101. }
  102. }
  103. }
  104. else
  105. {
  106. // normal string edit
  107. LoadControlSettings("Admin/VarEditDialog_String.res", "PLATFORM");
  108. m_pComboEdit->SetVisible(false);
  109. m_pStringEdit->SelectAllOnFirstFocus(true);
  110. m_pStringEdit->SetText(m_pRules->GetString("value"));
  111. }
  112. // set value
  113. char title[256];
  114. _snprintf(title, sizeof(title) - 1, "Change %s", m_pRules->GetString("name"));
  115. SetTitle(title, false);
  116. // bring to front
  117. // input()->SetAppModalSurface(GetVPanel());
  118. MoveToCenterOfScreen();
  119. BaseClass::Activate();
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Purpose: button command handler
  123. //-----------------------------------------------------------------------------
  124. void CVarEditDialog::OnCommand(const char *command)
  125. {
  126. if (!stricmp(command, "OK"))
  127. {
  128. // change the value
  129. ApplyChanges();
  130. Close();
  131. }
  132. else if (!stricmp(command, "Cancel"))
  133. {
  134. Close();
  135. }
  136. else
  137. {
  138. BaseClass::OnCommand(command);
  139. }
  140. }
  141. //-----------------------------------------------------------------------------
  142. // Purpose: Applies changes
  143. //-----------------------------------------------------------------------------
  144. void CVarEditDialog::ApplyChanges()
  145. {
  146. const char *type = m_pRules->GetString("type");
  147. if (!stricmp(type, "enumeration"))
  148. {
  149. // get the enumeration position from the combo box
  150. int iVal = m_pComboEdit->GetActiveItem();
  151. char value[32];
  152. _snprintf(value, sizeof(value) - 1, "%d", iVal);
  153. RemoteServer().SetValue(m_pRules->GetName(), value);
  154. }
  155. else if (!stricmp(type, "customlist"))
  156. {
  157. char value[512];
  158. m_pComboEdit->GetText(value, sizeof(value));
  159. RemoteServer().SetValue(m_pRules->GetName(), value);
  160. }
  161. else
  162. {
  163. // normal string
  164. char value[512];
  165. m_pStringEdit->GetText(value, sizeof(value));
  166. RemoteServer().SetValue(m_pRules->GetName(), value);
  167. }
  168. // tell the caller the var changed
  169. PostActionSignal(new KeyValues("VarChanged", "var", m_pRules->GetName()));
  170. }
  171. //-----------------------------------------------------------------------------
  172. // Purpose: Deletes on close
  173. //-----------------------------------------------------------------------------
  174. void CVarEditDialog::OnClose()
  175. {
  176. BaseClass::OnClose();
  177. MarkForDeletion();
  178. }