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.

263 lines
7.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "MapCycleEditDialog.h"
  8. #include <vgui/KeyCode.h>
  9. #include <KeyValues.h>
  10. #include <vgui_controls/Button.h>
  11. #include <vgui_controls/ListPanel.h>
  12. #include "RemoteServer.h"
  13. #include "tier1/utlbuffer.h"
  14. using namespace vgui;
  15. //-----------------------------------------------------------------------------
  16. // Purpose: Constructor
  17. //-----------------------------------------------------------------------------
  18. CMapCycleEditDialog::CMapCycleEditDialog(vgui::Panel *parent, const char *name) : BaseClass(parent, name)
  19. {
  20. SetSize(480, 320);
  21. SetSizeable(false);
  22. m_pAvailableMapList = new ListPanel(this, "AvailableMapList");
  23. m_pAvailableMapList->AddColumnHeader(0, "Map", "#Available_Maps", 128);
  24. m_pAvailableMapList->SetColumnSortable(0, false);
  25. m_pMapCycleList = new ListPanel(this, "MapCycleList");
  26. m_pMapCycleList->AddColumnHeader(0, "Map", "#Map_Cycle", 128);
  27. m_pMapCycleList->SetColumnSortable(0, false);
  28. m_RightArrow = new Button(this, "RightButton", "");
  29. m_LeftArrow = new Button(this, "LeftButton", "");
  30. m_UpArrow = new Button(this, "UpButton", "");
  31. m_DownArrow = new Button(this, "DownButton", "");
  32. LoadControlSettings("Admin/MapCycleEditDialog.res", "PLATFORM");
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Purpose: Destructor
  36. //-----------------------------------------------------------------------------
  37. CMapCycleEditDialog::~CMapCycleEditDialog()
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Purpose: Shows the dialog, building the lists from the params
  42. //-----------------------------------------------------------------------------
  43. void CMapCycleEditDialog::Activate(vgui::Panel *updateTarget, CUtlVector<CUtlSymbol> &availableMaps, CUtlVector<CUtlSymbol> &mapCycle)
  44. {
  45. // set the action signal target
  46. AddActionSignalTarget(updateTarget);
  47. // clear lists
  48. m_pAvailableMapList->DeleteAllItems();
  49. m_pMapCycleList->DeleteAllItems();
  50. // build lists
  51. for (int i = 0; i < availableMaps.Count(); i++)
  52. {
  53. // only add to the available maps list if it's not in mapCycle
  54. bool inMapCycle = false;
  55. for (int j = 0; j < mapCycle.Count(); j++)
  56. {
  57. if (!stricmp(mapCycle[j].String(), availableMaps[i].String()))
  58. {
  59. inMapCycle = true;
  60. break;
  61. }
  62. }
  63. if (!inMapCycle)
  64. {
  65. m_pAvailableMapList->AddItem(new KeyValues("MapItem", "Map", availableMaps[i].String()), 0, false, false);
  66. }
  67. }
  68. for (int i = 0; i < mapCycle.Count(); i++)
  69. {
  70. m_pMapCycleList->AddItem(new KeyValues("MapItem", "Map", mapCycle[i].String()), 0, false, false);
  71. }
  72. // show window
  73. SetTitle("Change Map Cycle", false);
  74. MoveToCenterOfScreen();
  75. BaseClass::Activate();
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Purpose: Sets up button state
  79. //-----------------------------------------------------------------------------
  80. void CMapCycleEditDialog::PerformLayout()
  81. {
  82. m_LeftArrow->SetEnabled(false);
  83. m_RightArrow->SetEnabled(false);
  84. m_UpArrow->SetEnabled(false);
  85. m_DownArrow->SetEnabled(false);
  86. if (m_pMapCycleList->GetSelectedItemsCount() > 0)
  87. {
  88. m_LeftArrow->SetEnabled(true);
  89. m_LeftArrow->SetAsDefaultButton(true);
  90. if (m_pMapCycleList->GetSelectedItemsCount() == 1)
  91. {
  92. int row = m_pMapCycleList->GetSelectedItem(0);
  93. if (row > 0)
  94. {
  95. m_UpArrow->SetEnabled(true);
  96. }
  97. if (row + 1 < m_pMapCycleList->GetItemCount())
  98. {
  99. m_DownArrow->SetEnabled(true);
  100. }
  101. }
  102. }
  103. else if (m_pAvailableMapList->GetSelectedItemsCount() > 0)
  104. {
  105. m_RightArrow->SetEnabled(true);
  106. m_RightArrow->SetAsDefaultButton(true);
  107. }
  108. BaseClass::PerformLayout();
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Purpose: Updates UI based on which listpanel got selection
  112. //-----------------------------------------------------------------------------
  113. void CMapCycleEditDialog::OnItemSelected(vgui::Panel *panel)
  114. {
  115. if (panel == m_pAvailableMapList && m_pAvailableMapList->GetSelectedItemsCount() > 0)
  116. {
  117. m_pMapCycleList->ClearSelectedItems();
  118. }
  119. else if (panel == m_pMapCycleList && m_pMapCycleList->GetSelectedItemsCount() > 0)
  120. {
  121. m_pAvailableMapList->ClearSelectedItems();
  122. }
  123. InvalidateLayout();
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Purpose: Button command handler
  127. //-----------------------------------------------------------------------------
  128. void CMapCycleEditDialog::OnCommand(const char *command)
  129. {
  130. if (!stricmp(command, "ArrowLeft"))
  131. {
  132. // move map from mapcycle to available list
  133. while (m_pMapCycleList->GetSelectedItemsCount() > 0)
  134. {
  135. int itemID = m_pMapCycleList->GetSelectedItem(0);
  136. KeyValues *data = m_pMapCycleList->GetItem(itemID);
  137. if (!data)
  138. return;
  139. const char *map = data->GetString("Map");
  140. m_pAvailableMapList->AddItem(new KeyValues("MapItem", "Map", map), 0, true, false);
  141. m_pMapCycleList->RemoveItem(itemID);
  142. }
  143. }
  144. else if (!stricmp(command, "ArrowRight"))
  145. {
  146. // move map from available list to mapcycle
  147. while (m_pAvailableMapList->GetSelectedItemsCount() > 0)
  148. {
  149. int itemID = m_pAvailableMapList->GetSelectedItem(0);
  150. KeyValues *data = m_pAvailableMapList->GetItem(itemID);
  151. if (!data)
  152. return;
  153. const char *map = data->GetString("Map");
  154. m_pMapCycleList->AddItem(new KeyValues("MapItem", "Map", map), 0, true, false);
  155. m_pAvailableMapList->RemoveItem(itemID);
  156. }
  157. }
  158. else if (!stricmp(command, "ArrowUp"))
  159. {
  160. int itemID = m_pMapCycleList->GetSelectedItem(0);
  161. int row = m_pMapCycleList->GetItemCurrentRow(itemID);
  162. int prevRow = row - 1;
  163. if (prevRow < 0)
  164. return;
  165. int prevItemID = m_pMapCycleList->GetItemIDFromRow(prevRow);
  166. // get the data
  167. KeyValues *d1 = m_pMapCycleList->GetItem(itemID);
  168. KeyValues *d2 = m_pMapCycleList->GetItem(prevItemID);
  169. // swap the strings
  170. CUtlSymbol tempString = d1->GetString("Map");
  171. d1->SetString("Map", d2->GetString("Map"));
  172. d2->SetString("Map", tempString.String());
  173. // update the list
  174. m_pMapCycleList->ApplyItemChanges(itemID);
  175. m_pMapCycleList->ApplyItemChanges(prevItemID);
  176. PostMessage(m_pMapCycleList, new KeyValues("KeyCodePressed", "code", KEY_UP));
  177. }
  178. else if (!stricmp(command, "ArrowDown"))
  179. {
  180. int itemID = m_pMapCycleList->GetSelectedItem(0);
  181. int row = m_pMapCycleList->GetItemCurrentRow(itemID);
  182. int nextRow = row + 1;
  183. if (nextRow + 1 > m_pMapCycleList->GetItemCount())
  184. return;
  185. int nextItemID = m_pMapCycleList->GetItemIDFromRow(nextRow);
  186. // get the data
  187. KeyValues *d1 = m_pMapCycleList->GetItem(itemID);
  188. KeyValues *d2 = m_pMapCycleList->GetItem(nextItemID);
  189. // swap the strings
  190. CUtlSymbol tempString = d1->GetString("Map");
  191. d1->SetString("Map", d2->GetString("Map"));
  192. d2->SetString("Map", tempString.String());
  193. // update the list
  194. m_pMapCycleList->ApplyItemChanges(itemID);
  195. m_pMapCycleList->ApplyItemChanges(nextItemID);
  196. PostMessage(m_pMapCycleList, new KeyValues("KeyCodePressed", "code", KEY_DOWN));
  197. }
  198. else if (!stricmp(command, "Cancel"))
  199. {
  200. Close();
  201. }
  202. else if (!stricmp(command, "OK"))
  203. {
  204. // write out the data
  205. CUtlBuffer msg(0, 1024, CUtlBuffer::TEXT_BUFFER);
  206. for (int i = 0; i < m_pMapCycleList->GetItemCount(); i++)
  207. {
  208. int itemID = m_pMapCycleList->GetItemIDFromRow(i);
  209. KeyValues *kv = m_pMapCycleList->GetItem(itemID);
  210. if ( kv )
  211. {
  212. msg.PutString(kv->GetString("Map"));
  213. msg.PutChar('\n');
  214. }
  215. }
  216. msg.PutChar(0);
  217. RemoteServer().SetValue("mapcycle", (const char *)msg.Base());
  218. // post message to tell varlist update
  219. PostActionSignal(new KeyValues("VarChanged", "var", "mapcycle"));
  220. // close
  221. Close();
  222. }
  223. else
  224. {
  225. BaseClass::OnCommand(command);
  226. }
  227. }