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.

538 lines
16 KiB

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