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.

316 lines
9.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A dialog for adding, deleting, and renaming visgroups.
  4. //
  5. //=============================================================================//
  6. #include "stdafx.h"
  7. #include "hammer.h"
  8. #include "EditGroups.h"
  9. #include "MainFrm.h"
  10. #include "MapWorld.h"
  11. #include "CustomMessages.h"
  12. #include "GlobalFunctions.h"
  13. #include "VisGroup.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include <tier0/memdbgon.h>
  16. static const unsigned int g_uSelChangeMsg = ::RegisterWindowMessage(TREELIST_MSG_SEL_CHANGE);
  17. BEGIN_MESSAGE_MAP(CEditGroups, CDialog)
  18. //{{AFX_MSG_MAP(CEditGroups)
  19. ON_BN_CLICKED(IDC_COLOR, OnColor)
  20. ON_EN_CHANGE(IDC_NAME, OnChangeName)
  21. ON_BN_CLICKED(IDC_NEW, OnNew)
  22. ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  23. ON_WM_CLOSE()
  24. ON_REGISTERED_MESSAGE(g_uSelChangeMsg, OnSelChangeGroupList)
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Constructor.
  29. // Input : pParent - Parent window.
  30. //-----------------------------------------------------------------------------
  31. CEditGroups::CEditGroups(CWnd* pParent /*=NULL*/)
  32. : CDialog(CEditGroups::IDD, pParent)
  33. {
  34. //{{AFX_DATA_INIT(CEditGroups)
  35. // NOTE: the ClassWizard will add member initialization here
  36. //}}AFX_DATA_INIT
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Purpose: Exchanges data between controls and data members.
  40. // Input : pDX -
  41. //-----------------------------------------------------------------------------
  42. void CEditGroups::DoDataExchange(CDataExchange* pDX)
  43. {
  44. CDialog::DoDataExchange(pDX);
  45. //{{AFX_DATA_MAP(CEditGroups)
  46. DDX_Control(pDX, IDC_NAME, m_cName);
  47. //}}AFX_DATA_MAP
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose: Sets the object's color as the visgroup color if the object belongs
  51. // to the given visgroup.
  52. // Input : pObject - Object to evaluate.
  53. // pGroup - Visgroup to check against.
  54. // Output : Returns TRUE to continue enumerating.
  55. //-----------------------------------------------------------------------------
  56. static BOOL UpdateObjectColor(CMapClass *pObject, CVisGroup *pGroup)
  57. {
  58. pObject->UpdateObjectColor();
  59. return(TRUE);
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose: Invokes the color picker dialog to modify the selected visgroup.
  63. //-----------------------------------------------------------------------------
  64. void CEditGroups::OnColor(void)
  65. {
  66. CVisGroup *pGroup = m_cGroupList.GetSelectedVisGroup();
  67. if (pGroup != NULL)
  68. {
  69. color32 rgbColor = pGroup->GetColor();
  70. CColorDialog dlg(RGB(rgbColor.r, rgbColor.g, rgbColor.b), CC_FULLOPEN);
  71. if (dlg.DoModal() == IDOK)
  72. {
  73. // change group color
  74. pGroup->SetColor(GetRValue(dlg.m_cc.rgbResult), GetGValue(dlg.m_cc.rgbResult), GetBValue(dlg.m_cc.rgbResult));
  75. m_cColorBox.SetColor(dlg.m_cc.rgbResult, TRUE);
  76. // change all object colors
  77. GetActiveWorld()->EnumChildren(ENUMMAPCHILDRENPROC(UpdateObjectColor), DWORD(pGroup));
  78. CMapDoc::GetActiveMapDoc()->UpdateAllViews( MAPVIEW_UPDATE_COLOR );
  79. }
  80. }
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: Called when the contents of the name edit control changes. Renames
  84. // the currently selected group with the new edit control contents.
  85. //-----------------------------------------------------------------------------
  86. void CEditGroups::OnChangeName(void)
  87. {
  88. CVisGroup *pGroup = m_cGroupList.GetSelectedVisGroup();
  89. CString szName;
  90. m_cName.GetWindowText(szName);
  91. pGroup->SetName(szName);
  92. m_cGroupList.UpdateVisGroup(pGroup);
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Purpose: Creates a new visgroup and adds it to the list.
  96. //-----------------------------------------------------------------------------
  97. void CEditGroups::OnNew(void)
  98. {
  99. CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
  100. CVisGroup *pGroup = pDoc->VisGroups_AddGroup("new group");
  101. pGroup->SetVisible(VISGROUP_SHOWN);
  102. UpdateGroupList();
  103. m_cGroupList.SelectItem(pGroup);
  104. m_cName.EnableWindow(TRUE);
  105. m_cName.SetActiveWindow();
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Purpose: Called when the remove button is pressed from the visgroup editor.
  109. // Deletes the selected visgroup and removes all references to it.
  110. //-----------------------------------------------------------------------------
  111. void CEditGroups::OnRemove(void)
  112. {
  113. CVisGroup *pGroup = m_cGroupList.GetSelectedVisGroup();
  114. if (!pGroup)
  115. return;
  116. //Don't allow user to delete autovisgroups.
  117. if ( pGroup->IsAutoVisGroup() )
  118. return;
  119. CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
  120. if (pDoc != NULL)
  121. {
  122. pDoc->VisGroups_RemoveGroup(pGroup);
  123. if (pGroup->GetVisible() != VISGROUP_SHOWN)
  124. {
  125. pDoc->VisGroups_UpdateAll();
  126. pDoc->UpdateVisibilityAll();
  127. pDoc->UpdateAllViews( MAPVIEW_UPDATE_OBJECTS );
  128. }
  129. else
  130. {
  131. pDoc->UpdateAllViews( MAPVIEW_UPDATE_COLOR );
  132. }
  133. }
  134. UpdateGroupList();
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Purpose: Handles selection change in the visgroup list. Updates the static
  138. // text controls with the name and colot of the selected visgroup.
  139. //-----------------------------------------------------------------------------
  140. LRESULT CEditGroups::OnSelChangeGroupList(WPARAM wParam, LPARAM lParam)
  141. {
  142. CVisGroup *pVisGroup = m_cGroupList.GetSelectedVisGroup();
  143. if (!pVisGroup)
  144. return 0;
  145. UpdateControlsForVisGroup(pVisGroup);
  146. return 0;
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Purpose:
  150. //-----------------------------------------------------------------------------
  151. void CEditGroups::UpdateControlsForVisGroup(CVisGroup *pVisGroup)
  152. {
  153. if (!pVisGroup)
  154. return;
  155. //
  156. // Update the name and color controls.
  157. //
  158. m_cName.SetWindowText(pVisGroup->GetName());
  159. color32 rgbColor = pVisGroup->GetColor();
  160. m_cColorBox.SetColor(RGB(rgbColor.r, rgbColor.g, rgbColor.b), TRUE);
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Purpose: Sets up initial state of dialog.
  164. //-----------------------------------------------------------------------------
  165. BOOL CEditGroups::OnInitDialog(void)
  166. {
  167. CDialog::OnInitDialog();
  168. m_cGroupList.SubclassDlgItem(IDC_GROUPS, this);
  169. m_cColorBox.SubclassDlgItem(IDC_COLORBOX, this);
  170. //
  171. // Fill the listbox with the visgroup names.
  172. //
  173. UpdateGroupList();
  174. //
  175. // Disable the edit name window if there are no visgroups in the list.
  176. //
  177. if (m_cGroupList.GetVisGroupCount())
  178. {
  179. CVisGroup *pVisGroup = m_cGroupList.GetVisGroup(0);
  180. m_cGroupList.SelectItem(pVisGroup);
  181. UpdateControlsForVisGroup(pVisGroup);
  182. }
  183. else
  184. {
  185. m_cName.EnableWindow(FALSE);
  186. }
  187. return(TRUE);
  188. }
  189. //-----------------------------------------------------------------------------
  190. // Purpose:
  191. //-----------------------------------------------------------------------------
  192. void CEditGroups::UpdateGroupList()
  193. {
  194. if (!IsWindow(m_hWnd))
  195. {
  196. return;
  197. }
  198. m_cGroupList.SetRedraw(false);
  199. m_cGroupList.DeleteAllItems();
  200. CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
  201. if (pDoc != NULL)
  202. {
  203. int nCount = pDoc->VisGroups_GetCount();
  204. for (int i = 0; i < nCount; i++)
  205. {
  206. CVisGroup *pGroup = pDoc->VisGroups_GetVisGroup(i);
  207. if (!pGroup->GetParent())
  208. {
  209. m_cGroupList.AddVisGroup(pGroup);
  210. }
  211. }
  212. }
  213. m_cGroupList.ExpandAll();
  214. m_cGroupList.SetRedraw(true);
  215. m_cGroupList.Invalidate();
  216. }
  217. //-----------------------------------------------------------------------------
  218. // Purpose: Called when the dialog is closing. Sends a message to update the
  219. // visgroup dialog bar in case any visgroup changes were made.
  220. //-----------------------------------------------------------------------------
  221. void CEditGroups::OnClose(void)
  222. {
  223. GetMainWnd()->GlobalNotify(WM_MAPDOC_CHANGED);
  224. CDialog::OnClose();
  225. }
  226. //-----------------------------------------------------------------------------
  227. // Purpose: Called when the dialog is closing. Sends a message to update the
  228. // visgroup dialog bar in case any visgroup changes were made.
  229. //-----------------------------------------------------------------------------
  230. BOOL CEditGroups::DestroyWindow(void)
  231. {
  232. GetMainWnd()->GlobalNotify(WM_MAPDOC_CHANGED);
  233. return(CDialog::DestroyWindow());
  234. }
  235. BEGIN_MESSAGE_MAP(CColorBox, CStatic)
  236. ON_WM_PAINT()
  237. END_MESSAGE_MAP()
  238. //-----------------------------------------------------------------------------
  239. // Purpose: Sets the color of the color box.
  240. // Input : c - RGB color to set.
  241. // bRedraw - TRUE repaints, FALSE does now.
  242. //-----------------------------------------------------------------------------
  243. void CColorBox::SetColor(COLORREF c, BOOL bRedraw)
  244. {
  245. m_c = c;
  246. if (bRedraw)
  247. {
  248. RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
  249. }
  250. }
  251. //-----------------------------------------------------------------------------
  252. // Purpose: Fills the colorbox window with the current color.
  253. //-----------------------------------------------------------------------------
  254. void CColorBox::OnPaint(void)
  255. {
  256. CPaintDC dc(this);
  257. CRect r;
  258. GetClientRect(r);
  259. CBrush brush(m_c);
  260. dc.FillRect(r, &brush);
  261. }