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.

451 lines
13 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A helper that represents the axis of rotation for a rotating entity.
  4. // When selected, it exposes handles for the endpoints of the axis.
  5. //
  6. // It writes the axis as a keyvalue of the form:
  7. //
  8. // "x0 y0 z0, x1 y1 z1"
  9. //
  10. //=============================================================================//
  11. #include "stdafx.h"
  12. #include "Box3D.h"
  13. #include "GlobalFunctions.h"
  14. #include "fgdlib/HelperInfo.h"
  15. #include "materialsystem/imaterialsystem.h"
  16. #include "materialsystem/imesh.h"
  17. #include "MainFrm.h" // For refreshing the object properties dialog
  18. #include "MapDoc.h"
  19. #include "MapAxisHandle.h"
  20. #include "MapPointHandle.h"
  21. #include "MapView2D.h"
  22. #include "Material.h"
  23. #include "Options.h"
  24. #include "Render2D.h"
  25. #include "Render3D.h"
  26. #include "ToolManager.h"
  27. #include "ToolAxisHandle.h"
  28. // memdbgon must be the last include file in a .cpp file!!!
  29. #include <tier0/memdbgon.h>
  30. IMPLEMENT_MAPCLASS(CMapAxisHandle);
  31. //-----------------------------------------------------------------------------
  32. // Purpose: Factory function. Used for creating a CMapAxisHandle from a set
  33. // of string parameters from the FGD file.
  34. // Input : pInfo - Pointer to helper info class which gives us information
  35. // about how to create the class.
  36. // Output : Returns a pointer to the class, NULL if an error occurs.
  37. //-----------------------------------------------------------------------------
  38. CMapClass *CMapAxisHandle::Create(CHelperInfo *pHelperInfo, CMapEntity *pParent)
  39. {
  40. static char *pszDefaultKeyName = "axis";
  41. const char *pszKey = pHelperInfo->GetParameter(0);
  42. if (pszKey == NULL)
  43. {
  44. pszKey = pszDefaultKeyName;
  45. }
  46. CMapAxisHandle *pBox = new CMapAxisHandle(pszKey);
  47. pBox->SetRenderColor(255, 255, 255);
  48. return(pBox);
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose:
  52. // Input : pfMins -
  53. // pfMaxs -
  54. //-----------------------------------------------------------------------------
  55. CMapAxisHandle::CMapAxisHandle(void)
  56. {
  57. Initialize();
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose:
  61. // Input : pfMins -
  62. // pfMaxs -
  63. //-----------------------------------------------------------------------------
  64. CMapAxisHandle::CMapAxisHandle(const char *pszKey)
  65. {
  66. Initialize();
  67. strcpy(m_szKeyName, pszKey);
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose:
  71. //-----------------------------------------------------------------------------
  72. void CMapAxisHandle::Initialize(void)
  73. {
  74. m_szKeyName[0] = '\0';
  75. r = 255;
  76. g = 255;
  77. b = 255;
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose:
  81. //-----------------------------------------------------------------------------
  82. CMapAxisHandle::~CMapAxisHandle(void)
  83. {
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose:
  87. // Input : bFullUpdate -
  88. //-----------------------------------------------------------------------------
  89. void CMapAxisHandle::CalcBounds(BOOL bFullUpdate)
  90. {
  91. CMapClass::CalcBounds(bFullUpdate);
  92. //
  93. // We don't affect our parent's 2D render bounds.
  94. //
  95. m_Render2DBox.ResetBounds();
  96. //
  97. // Calculate 3D culling box.
  98. //
  99. m_CullBox.ResetBounds();
  100. for (int i = 0; i < 2; i++)
  101. {
  102. m_Point[i].CalcBounds(bFullUpdate);
  103. Vector vecMins;
  104. Vector vecMaxs;
  105. m_Point[i].GetCullBox(vecMins, vecMaxs);
  106. m_CullBox.UpdateBounds(vecMins, vecMaxs);
  107. }
  108. m_BoundingBox = m_CullBox;
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Purpose:
  112. // Output : CMapClass
  113. //-----------------------------------------------------------------------------
  114. CMapClass *CMapAxisHandle::Copy(bool bUpdateDependencies)
  115. {
  116. CMapAxisHandle *pCopy = new CMapAxisHandle;
  117. if (pCopy != NULL)
  118. {
  119. pCopy->CopyFrom(this, bUpdateDependencies);
  120. }
  121. return(pCopy);
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Purpose:
  125. // Input : pObject -
  126. // Output : CMapClass
  127. //-----------------------------------------------------------------------------
  128. CMapClass *CMapAxisHandle::CopyFrom(CMapClass *pObject, bool bUpdateDependencies)
  129. {
  130. Assert(pObject->IsMapClass(MAPCLASS_TYPE(CMapAxisHandle)));
  131. CMapAxisHandle *pFrom = (CMapAxisHandle *)pObject;
  132. CMapClass::CopyFrom(pObject, bUpdateDependencies);
  133. m_Point[0].CopyFrom(&pFrom->m_Point[0], bUpdateDependencies);
  134. m_Point[1].CopyFrom(&pFrom->m_Point[1], bUpdateDependencies);
  135. strcpy(m_szKeyName, pFrom->m_szKeyName);
  136. return(this);
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Purpose: Gets the tool object for a given context data from HitTest2D.
  140. //-----------------------------------------------------------------------------
  141. CBaseTool *CMapAxisHandle::GetToolObject(int nHitData, bool bAttachObject)
  142. {
  143. // FIXME: ideally, we could use CToolPointHandle here, because all it does is move
  144. // points around, but that would require some way for the CMapAxisHandle to know
  145. // when the CMapPointHandle's position changes. This way the CToolAxisHandle can
  146. // handle the notification. In general, we need a better system for building complex
  147. // objects from simple ones and handling changes to the simple objects in the complex one.
  148. //
  149. // If we DID use a CToolPointHandle, we'd need to reconcile the status bar updates that
  150. // are done in OnMouseMove2D, because points and axes cause different status bar text
  151. // to be displayed as they are dragged around.
  152. CToolAxisHandle *pTool = (CToolAxisHandle *)ToolManager()->GetToolForID(TOOL_AXIS_HANDLE);
  153. if ( bAttachObject )
  154. {
  155. pTool->Attach(this, nHitData);
  156. }
  157. return pTool;
  158. }
  159. //-----------------------------------------------------------------------------
  160. // Purpose:
  161. // Input : pView -
  162. // point -
  163. // nData -
  164. // Output : Returns true on success, false on failure.
  165. //-----------------------------------------------------------------------------
  166. bool CMapAxisHandle::HitTest2D(CMapView2D *pView, const Vector2D &point, HitInfo_t &HitData)
  167. {
  168. if ( IsVisible() )
  169. {
  170. for (int i = 0; i < 2; i++)
  171. {
  172. if ( m_Point[i].HitTest2D(pView, point, HitData) )
  173. {
  174. HitData.pObject = this;
  175. HitData.uData = i;
  176. HitData.nDepth = 0; // map helpers have no real depth
  177. return true;
  178. }
  179. }
  180. }
  181. return false;
  182. }
  183. //-----------------------------------------------------------------------------
  184. // Purpose:
  185. // Input : pRender -
  186. //-----------------------------------------------------------------------------
  187. void CMapAxisHandle::Render2D(CRender2D *pRender)
  188. {
  189. SelectionState_t eState = GetSelectionState();
  190. if (eState == SELECT_NONE)
  191. return;
  192. m_Point[0].Render2D(pRender);
  193. m_Point[1].Render2D(pRender);
  194. if (eState == SELECT_MODIFY)
  195. {
  196. pRender->PushRenderMode( RENDER_MODE_DOTTED );
  197. pRender->SetDrawColor( GetRValue(Options.colors.clrSelection), GetGValue(Options.colors.clrSelection), GetBValue(Options.colors.clrSelection) );
  198. }
  199. else
  200. {
  201. pRender->PushRenderMode( RENDER_MODE_FLAT );
  202. pRender->SetDrawColor( GetRValue(Options.colors.clrToolHandle), GetGValue(Options.colors.clrToolHandle), GetBValue(Options.colors.clrToolHandle) );
  203. }
  204. Vector vecOrigin1;
  205. Vector vecOrigin2;
  206. m_Point[0].GetOrigin(vecOrigin1);
  207. m_Point[1].GetOrigin(vecOrigin2);
  208. pRender->DrawLine(vecOrigin1, vecOrigin2);
  209. pRender->PopRenderMode();
  210. }
  211. //-----------------------------------------------------------------------------
  212. // Purpose:
  213. // Input : pRender -
  214. //-----------------------------------------------------------------------------
  215. void CMapAxisHandle::Render3D(CRender3D *pRender)
  216. {
  217. if (GetSelectionState() != SELECT_NONE)
  218. {
  219. for (int i = 0; i < 2; i++)
  220. {
  221. m_Point[i].Render3D(pRender);
  222. }
  223. Vector vec1;
  224. Vector vec2;
  225. m_Point[0].GetOrigin(vec1);
  226. m_Point[1].GetOrigin(vec2);
  227. pRender->SetDrawColor( 255, 255, 255 );
  228. pRender->DrawLine(vec1, vec2);
  229. }
  230. }
  231. //-----------------------------------------------------------------------------
  232. // Purpose:
  233. //-----------------------------------------------------------------------------
  234. int CMapAxisHandle::SerializeRMF(std::fstream &File, BOOL bRMF)
  235. {
  236. return(0);
  237. }
  238. //-----------------------------------------------------------------------------
  239. // Purpose:
  240. //-----------------------------------------------------------------------------
  241. int CMapAxisHandle::SerializeMAP(std::fstream &File, BOOL bRMF)
  242. {
  243. return(0);
  244. }
  245. //-----------------------------------------------------------------------------
  246. // Purpose: Overridden to chain down to our endpoints, which are not children.
  247. //-----------------------------------------------------------------------------
  248. void CMapAxisHandle::SetOrigin(Vector &vecOrigin)
  249. {
  250. BaseClass::SetOrigin(vecOrigin);
  251. m_Point[0].SetOrigin(vecOrigin);
  252. m_Point[1].SetOrigin(vecOrigin);
  253. }
  254. //-----------------------------------------------------------------------------
  255. // Purpose: Overridden to chain down to our endpoints, which are not children.
  256. //-----------------------------------------------------------------------------
  257. SelectionState_t CMapAxisHandle::SetSelectionState(SelectionState_t eSelectionState)
  258. {
  259. SelectionState_t eState = BaseClass::SetSelectionState(eSelectionState);
  260. m_Point[0].SetSelectionState(eSelectionState);
  261. m_Point[1].SetSelectionState(eSelectionState);
  262. return eState;
  263. }
  264. //-----------------------------------------------------------------------------
  265. // Purpose: Special version of set SelectionState to set the state in only one
  266. // endpoint handle for dragging that handle.
  267. //-----------------------------------------------------------------------------
  268. SelectionState_t CMapAxisHandle::SetSelectionState(SelectionState_t eSelectionState, int nHandle)
  269. {
  270. SelectionState_t eState = BaseClass::SetSelectionState(eSelectionState);
  271. m_Point[nHandle].SetSelectionState(eSelectionState);
  272. return eState;
  273. }
  274. //-----------------------------------------------------------------------------
  275. // Purpose: Overridden because origin helpers don't take the color of their
  276. // parent entity.
  277. // Input : red, green, blue -
  278. //-----------------------------------------------------------------------------
  279. void CMapAxisHandle::SetRenderColor(unsigned char red, unsigned char green, unsigned char blue)
  280. {
  281. }
  282. //-----------------------------------------------------------------------------
  283. // Purpose: Overridden because origin helpers don't take the color of their
  284. // parent entity.
  285. // Input : red, green, blue -
  286. //-----------------------------------------------------------------------------
  287. void CMapAxisHandle::SetRenderColor(color32 rgbColor)
  288. {
  289. }
  290. //-----------------------------------------------------------------------------
  291. // Purpose:
  292. // Input : szKey -
  293. // szValue -
  294. //-----------------------------------------------------------------------------
  295. void CMapAxisHandle::OnParentKeyChanged(const char *szKey, const char *szValue)
  296. {
  297. if (!stricmp(szKey, m_szKeyName))
  298. {
  299. Vector vecOrigin1;
  300. Vector vecOrigin2;
  301. sscanf(szValue, "%f %f %f, %f %f %f", &vecOrigin1.x, &vecOrigin1.y, &vecOrigin1.z, &vecOrigin2.x, &vecOrigin2.y, &vecOrigin2.z);
  302. m_Point[0].SetOrigin(vecOrigin1);
  303. m_Point[1].SetOrigin(vecOrigin2);
  304. CalcBounds();
  305. }
  306. }
  307. //-----------------------------------------------------------------------------
  308. // Purpose: Called by the axis tool to update the position of the endpoint.
  309. //-----------------------------------------------------------------------------
  310. void CMapAxisHandle::UpdateEndPoint(Vector &vecPos, int nPointIndex)
  311. {
  312. m_Point[nPointIndex].m_Origin = vecPos;
  313. CalcBounds();
  314. UpdateParentKey();
  315. }
  316. //-----------------------------------------------------------------------------
  317. // Purpose: Overridden to transform our endpoints.
  318. //-----------------------------------------------------------------------------
  319. void CMapAxisHandle::DoTransform(const VMatrix &matrix)
  320. {
  321. BaseClass::DoTransform(matrix);
  322. m_Point[0].Transform(matrix);
  323. m_Point[1].Transform(matrix);
  324. UpdateParentKey();
  325. }
  326. //-----------------------------------------------------------------------------
  327. // Purpose:
  328. //-----------------------------------------------------------------------------
  329. void CMapAxisHandle::UpdateParentKey(void)
  330. {
  331. if (m_szKeyName[0])
  332. {
  333. CMapEntity *pEntity = dynamic_cast <CMapEntity *> (m_pParent);
  334. if (pEntity != NULL)
  335. {
  336. Vector vecOrigin1;
  337. Vector vecOrigin2;
  338. m_Point[0].GetOrigin(vecOrigin1);
  339. m_Point[1].GetOrigin(vecOrigin2);
  340. CalcBounds();
  341. char szValue[KEYVALUE_MAX_VALUE_LENGTH];
  342. sprintf(szValue, "%g %g %g, %g %g %g", (double)vecOrigin1.x, (double)vecOrigin1.y, (double)vecOrigin1.z, (double)vecOrigin2.x, (double)vecOrigin2.y, (double)vecOrigin2.z);
  343. pEntity->NotifyChildKeyChanged(this, m_szKeyName, szValue);
  344. }
  345. }
  346. }
  347. //-----------------------------------------------------------------------------
  348. // Purpose: Sets the keyvalue in our parent when we are added to the world.
  349. // Input : pWorld -
  350. //-----------------------------------------------------------------------------
  351. void CMapAxisHandle::OnAddToWorld(CMapWorld *pWorld)
  352. {
  353. BaseClass::OnAddToWorld(pWorld);
  354. UpdateParentKey();
  355. }
  356. //-----------------------------------------------------------------------------
  357. // Purpose: Sets the keyvalue in our parent after the map is loaded.
  358. // Input : pWorld -
  359. //-----------------------------------------------------------------------------
  360. void CMapAxisHandle::PostloadWorld(CMapWorld *pWorld)
  361. {
  362. BaseClass::PostloadWorld(pWorld);
  363. UpdateParentKey();
  364. }