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.

305 lines
7.9 KiB

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