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.

306 lines
8.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // TODO: add an autoregistration system for tools a la LINK_ENTITY_TO_CLASS
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include "MapDoc.h"
  9. #include "MainFrm.h"
  10. #include "MapView2D.h" // FIXME: for MapView2D::updTool
  11. #include "ToolAxisHandle.h"
  12. #include "ToolDecal.h"
  13. #include "ToolDisplace.h"
  14. #include "ToolManager.h"
  15. #include "ToolMagnify.h"
  16. #include "ToolMaterial.h"
  17. #include "ToolPickFace.h"
  18. #include "ToolPickAngles.h"
  19. #include "ToolPickEntity.h"
  20. #include "ToolPointHandle.h"
  21. #include "ToolSphere.h"
  22. #include "ToolSweptHull.h"
  23. #include "ToolBlock.h"
  24. #include "ToolCamera.h"
  25. #include "ToolClipper.h"
  26. #include "ToolCordon.h"
  27. #include "ToolEntity.h"
  28. #include "ToolMorph.h"
  29. #include "ToolOverlay.h"
  30. #include "ToolSelection.h"
  31. #include "ToolMagnify.h"
  32. #include "ToolMaterial.h"
  33. #include "toolsprinkle.h"
  34. #include "ChunkFile.h"
  35. // memdbgon must be the last include file in a .cpp file!!!
  36. #include <tier0/memdbgon.h>
  37. static CToolManager s_DummyToolmanager;
  38. CToolManager* ToolManager()
  39. {
  40. CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
  41. if ( pDoc )
  42. return pDoc->GetTools();
  43. return &s_DummyToolmanager;
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Purpose: Prepares the tool manager for use.
  47. // Output : Returns true on success, false on failure.
  48. //-----------------------------------------------------------------------------
  49. bool CToolManager::Init( CMapDoc *pDocument )
  50. {
  51. // add default tools
  52. //
  53. // Create the tools that are held by the tool manager and add them
  54. // to the internal tools list.
  55. //
  56. RemoveAllTools();
  57. m_pDocument = pDocument;
  58. AddTool( new CToolDisplace );
  59. AddTool( new CToolMagnify );
  60. AddTool( new CToolDecal );
  61. AddTool( new CToolMaterial );
  62. AddTool( new CToolAxisHandle );
  63. AddTool( new CToolPointHandle );
  64. AddTool( new CToolSphere );
  65. AddTool( new CToolPickAngles );
  66. AddTool( new CToolPickEntity );
  67. AddTool( new CToolPickFace );
  68. AddTool( new CToolSweptPlayerHull );
  69. AddTool( new Selection3D );
  70. AddTool( new CToolBlock );
  71. AddTool( new CToolEntity );
  72. AddTool( new Camera3D );
  73. AddTool( new Morph3D );
  74. AddTool( new Clipper3D );
  75. AddTool( new Cordon3D );
  76. AddTool( new CToolOverlay );
  77. AddTool( new CToolEntitySprinkle );
  78. return true;
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose: Shuts down the tool manager - called on app exit.
  82. //-----------------------------------------------------------------------------
  83. void CToolManager::Shutdown()
  84. {
  85. m_pActiveTool = NULL;
  86. m_pDocument = NULL;
  87. RemoveAllTools();
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose: Constructor. Allocates the tools.
  91. //-----------------------------------------------------------------------------
  92. CToolManager::CToolManager()
  93. {
  94. m_pActiveTool = NULL;
  95. m_pDocument = NULL;
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Purpose: Destructor. Deletes the tools.
  99. //-----------------------------------------------------------------------------
  100. CToolManager::~CToolManager()
  101. {
  102. Shutdown();
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Purpose:
  106. //-----------------------------------------------------------------------------
  107. void CToolManager::AddTool(CBaseTool *pTool)
  108. {
  109. if ( GetToolForID( pTool->GetToolID() ) )
  110. {
  111. Assert( !pTool );
  112. Msg("CToolManager::AddTool: Tool %i already registered.\n");
  113. return;
  114. }
  115. pTool->Init( m_pDocument );
  116. m_Tools.AddToTail(pTool);
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. //-----------------------------------------------------------------------------
  121. CBaseTool *CToolManager::GetActiveTool()
  122. {
  123. return m_pActiveTool;
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Purpose: Returns a tool pointer for a given tool ID, NULL if there is no
  127. // corresponding tool.
  128. //-----------------------------------------------------------------------------
  129. CBaseTool *CToolManager::GetToolForID(ToolID_t eToolID)
  130. {
  131. int nToolCount = GetToolCount();
  132. for (int i = 0; i < nToolCount; i++)
  133. {
  134. CBaseTool *pTool = GetTool(i);
  135. if (pTool->GetToolID() == eToolID)
  136. {
  137. return pTool;
  138. }
  139. }
  140. return NULL;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Purpose: Returns the ID of the active tool.
  144. //-----------------------------------------------------------------------------
  145. ToolID_t CToolManager::GetActiveToolID()
  146. {
  147. if ( m_pActiveTool )
  148. return m_pActiveTool->GetToolID();
  149. else
  150. return TOOL_NONE;
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Purpose: Pushes a new tool onto the tool stack and activates it. The active
  154. // tool will be deactivated and reactivated when PopTool is called.
  155. //-----------------------------------------------------------------------------
  156. void CToolManager::PushTool(ToolID_t eToolID)
  157. {
  158. //
  159. // Add the new tool to the top of the tool stack.
  160. //
  161. if (eToolID != GetActiveToolID())
  162. {
  163. m_ToolIDStack.AddToHead(GetActiveToolID());
  164. }
  165. SetTool(eToolID);
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Purpose: Restores the active tool to what it was when PushTool was called.
  169. // If the stack is underflowed somehow, the pointer is restored.
  170. //-----------------------------------------------------------------------------
  171. void CToolManager::PopTool()
  172. {
  173. int nCount = m_ToolIDStack.Count();
  174. if (nCount > 0)
  175. {
  176. ToolID_t eNewTool = m_ToolIDStack.Element(0);
  177. m_ToolIDStack.Remove(0);
  178. SetTool(eNewTool);
  179. }
  180. }
  181. //-----------------------------------------------------------------------------
  182. // Purpose: Sets the current active tool by ID.
  183. // Input : iTool - ID of the tool to activate.
  184. //-----------------------------------------------------------------------------
  185. void CToolManager::SetTool(ToolID_t eNewTool)
  186. {
  187. CBaseTool *pNewTool = GetToolForID(eNewTool);
  188. CBaseTool *pOldTool = m_pActiveTool;
  189. // Check to see that we can deactive the current tool
  190. if ( pOldTool && (pOldTool != pNewTool) )
  191. {
  192. // Deactivate the current tool unless we are just 'reactivating' it.
  193. if( !pOldTool->CanDeactivate() )
  194. return;
  195. }
  196. // set active tool to new tool already so old tool can peek whats coming next
  197. m_pActiveTool = pNewTool;
  198. // deactivate the old tool if different.
  199. if ( pOldTool && (pOldTool != pNewTool) )
  200. {
  201. pOldTool->Deactivate();
  202. }
  203. // always activate the new tool
  204. if ( pNewTool )
  205. {
  206. pNewTool->Activate();
  207. }
  208. // FIXME: When we start up, we end up here before the main window is created because
  209. // CFaceEditDispPage::OnSetActive() calls SetTool(TOOL_FACEEDIT_DISP). This
  210. // behavior is rather nonsensical during startup.
  211. CMainFrame *pwndMain = GetMainWnd();
  212. if (pwndMain != NULL)
  213. {
  214. pwndMain->m_ObjectBar.UpdateListForTool(eNewTool);
  215. }
  216. if ( m_pDocument )
  217. m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
  218. }
  219. ChunkFileResult_t CToolManager::SaveVMF(CChunkFile *pFile, CSaveInfo *pSaveInfo)
  220. {
  221. for (int i=0;i<m_Tools.Count(); i++)
  222. {
  223. if ( m_Tools[i]->GetVMFChunkName() != NULL )
  224. {
  225. m_Tools[i]->SaveVMF( pFile, pSaveInfo );
  226. }
  227. }
  228. return ChunkFile_Ok;
  229. }
  230. ChunkFileResult_t CToolManager::LoadCallback(CChunkFile *pFile, CBaseTool *pTool)
  231. {
  232. return pTool->LoadVMF( pFile );
  233. }
  234. void CToolManager::AddToolHandlers( CChunkHandlerMap *pHandlersMap )
  235. {
  236. for (int i=0;i<m_Tools.Count(); i++)
  237. {
  238. if ( m_Tools[i]->GetVMFChunkName() != NULL )
  239. {
  240. pHandlersMap->AddHandler( m_Tools[i]->GetVMFChunkName(), (ChunkHandler_t)LoadCallback, m_Tools[i] );
  241. }
  242. }
  243. }
  244. //-----------------------------------------------------------------------------
  245. // Purpose: Removes all the document-created tools from the tools list.
  246. //-----------------------------------------------------------------------------
  247. void CToolManager::RemoveAllTools()
  248. {
  249. m_pActiveTool = NULL;
  250. m_Tools.PurgeAndDeleteElements();
  251. m_ToolIDStack.RemoveAll();
  252. }