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.

412 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "stdafx.h"
  7. #include "Box3D.h"
  8. #include "GlobalFunctions.h"
  9. #include "fgdlib/HelperInfo.h"
  10. #include "materialsystem/imaterialsystem.h"
  11. #include "MainFrm.h" // For refreshing the object properties dialog
  12. #include "MapDoc.h"
  13. #include "MapPlayerHullHandle.h"
  14. #include "MapSweptPlayerHull.h"
  15. #include "MapView2D.h"
  16. #include "Material.h"
  17. #include "Options.h"
  18. #include "ObjectProperties.h" // For refreshing the object properties dialog
  19. #include "Render2D.h"
  20. #include "Render3D.h"
  21. #include "StatusBarIDs.h" // For updating status bar text
  22. #include "ToolManager.h"
  23. #include "vgui/Cursor.h"
  24. // memdbgon must be the last include file in a .cpp file!!!
  25. #include <tier0/memdbgon.h>
  26. IMPLEMENT_MAPCLASS(CMapPlayerHullHandle);
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Factory function. Used for creating a CMapPlayerHullHandle from a set
  29. // of string parameters from the FGD file.
  30. // Input : pInfo - Pointer to helper info class which gives us information
  31. // about how to create the class.
  32. // Output : Returns a pointer to the class, NULL if an error occurs.
  33. //-----------------------------------------------------------------------------
  34. CMapClass *CMapPlayerHullHandle::Create(CHelperInfo *pHelperInfo, CMapEntity *pParent)
  35. {
  36. static char *pszDefaultKeyName = "origin";
  37. bool bDrawLineToParent = !stricmp(pHelperInfo->GetName(), "vecline");
  38. const char *pszKey = pHelperInfo->GetParameter(0);
  39. if (pszKey == NULL)
  40. {
  41. pszKey = pszDefaultKeyName;
  42. }
  43. CMapPlayerHullHandle *pBox = new CMapPlayerHullHandle(pszKey, bDrawLineToParent);
  44. pBox->SetRenderColor(255, 255, 255);
  45. return(pBox);
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose:
  49. // Input : pfMins -
  50. // pfMaxs -
  51. //-----------------------------------------------------------------------------
  52. CMapPlayerHullHandle::CMapPlayerHullHandle(void)
  53. {
  54. Initialize();
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose:
  58. // Input : pszKey -
  59. // bDrawLineToParent -
  60. //-----------------------------------------------------------------------------
  61. CMapPlayerHullHandle::CMapPlayerHullHandle(const char *pszKey, bool bDrawLineToParent)
  62. {
  63. Initialize();
  64. strcpy(m_szKeyName, pszKey);
  65. m_bDrawLineToParent = bDrawLineToParent;
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose:
  69. //-----------------------------------------------------------------------------
  70. void CMapPlayerHullHandle::Initialize(void)
  71. {
  72. m_szKeyName[0] = '\0';
  73. m_bDrawLineToParent = 0;
  74. r = 255;
  75. g = 255;
  76. b = 255;
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose:
  80. //-----------------------------------------------------------------------------
  81. CMapPlayerHullHandle::~CMapPlayerHullHandle(void)
  82. {
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose: Sets a backlink to our owner so we can call them in PrepareSelection.
  86. //-----------------------------------------------------------------------------
  87. void CMapPlayerHullHandle::Attach( CMapSweptPlayerHull *pOwner )
  88. {
  89. m_pOwner = pOwner;
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Purpose:
  93. // Input : bFullUpdate -
  94. //-----------------------------------------------------------------------------
  95. void CMapPlayerHullHandle::CalcBounds(BOOL bFullUpdate)
  96. {
  97. // We don't affect our parent's 2D render bounds.
  98. m_Render2DBox.ResetBounds();
  99. // Calculate 3D culling box.
  100. Vector Mins = m_Origin + Vector(-16, -16, -36);
  101. Vector Maxs = m_Origin + Vector(16, 16, 36);
  102. m_Render2DBox.UpdateBounds(Mins, Maxs);
  103. m_CullBox = m_Render2DBox;
  104. m_BoundingBox = m_CullBox;
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose:
  108. // Output :
  109. //-----------------------------------------------------------------------------
  110. CMapClass *CMapPlayerHullHandle::Copy(bool bUpdateDependencies)
  111. {
  112. CMapPlayerHullHandle *pCopy = new CMapPlayerHullHandle;
  113. if (pCopy != NULL)
  114. {
  115. pCopy->CopyFrom(this, bUpdateDependencies);
  116. }
  117. return(pCopy);
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose:
  121. // Input : pObject -
  122. // Output :
  123. //-----------------------------------------------------------------------------
  124. CMapClass *CMapPlayerHullHandle::CopyFrom(CMapClass *pObject, bool bUpdateDependencies)
  125. {
  126. Assert(pObject->IsMapClass(MAPCLASS_TYPE(CMapPlayerHullHandle)));
  127. CMapPlayerHullHandle *pFrom = (CMapPlayerHullHandle *)pObject;
  128. CMapClass::CopyFrom(pObject, bUpdateDependencies);
  129. strcpy(m_szKeyName, pFrom->m_szKeyName);
  130. return(this);
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose:
  134. // Input : pView -
  135. // point -
  136. // nData -
  137. // Output :
  138. //-----------------------------------------------------------------------------
  139. bool CMapPlayerHullHandle::HitTest2D(CMapView2D *pView, const Vector2D &point, HitInfo_t &HitData)
  140. {
  141. if ( IsVisible() )
  142. {
  143. Vector2D vecClient;
  144. pView->WorldToClient(vecClient, m_Origin);
  145. if (pView->CheckDistance(point, vecClient, HANDLE_RADIUS))
  146. {
  147. HitData.pObject = this;
  148. HitData.uData = 0;
  149. HitData.nDepth = 0; // // handles have no depth
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Purpose:
  157. // Input : pRender -
  158. //-----------------------------------------------------------------------------
  159. void CMapPlayerHullHandle::Render2D(CRender2D *pRender)
  160. {
  161. SelectionState_t eState = GetSelectionState();
  162. if (eState == SELECT_MODIFY)
  163. {
  164. pRender->PushRenderMode( RENDER_MODE_DOTTED );
  165. pRender->SetDrawColor( GetRValue(Options.colors.clrSelection), GetGValue(Options.colors.clrSelection), GetBValue(Options.colors.clrSelection) );
  166. }
  167. else
  168. {
  169. pRender->PushRenderMode( RENDER_MODE_FLAT );
  170. pRender->SetDrawColor( GetRValue(Options.colors.clrToolHandle), GetGValue(Options.colors.clrToolHandle), GetBValue(Options.colors.clrToolHandle) );
  171. }
  172. Vector vecMins, vecMaxs;
  173. GetRender2DBox(vecMins, vecMaxs);
  174. pRender->DrawBox( vecMins, vecMaxs );
  175. pRender->PopRenderMode();
  176. // Draw center handle.
  177. color32 rgbColor = GetRenderColor();
  178. pRender->SetDrawColor( rgbColor.r, rgbColor.g, rgbColor.b );
  179. if (eState == SELECT_NONE)
  180. {
  181. pRender->SetHandleStyle( HANDLE_RADIUS, CRender::HANDLE_CROSS );
  182. }
  183. else
  184. {
  185. pRender->SetHandleStyle( HANDLE_RADIUS, CRender::HANDLE_CIRCLE );
  186. }
  187. pRender->DrawHandle( (vecMins+vecMaxs)/2 );
  188. }
  189. //-----------------------------------------------------------------------------
  190. // Purpose:
  191. // Input : pRender -
  192. //-----------------------------------------------------------------------------
  193. void CMapPlayerHullHandle::Render3D(CRender3D *pRender)
  194. {
  195. if (GetSelectionState() == SELECT_NONE)
  196. {
  197. pRender->SetDrawColor( 200,180,0 );
  198. }
  199. else
  200. {
  201. pRender->SetDrawColor( 255,0,0 );
  202. }
  203. Vector Mins, Maxs;
  204. m_Render2DBox.GetBounds( Mins, Maxs );
  205. pRender->BeginRenderHitTarget(this);
  206. pRender->RenderBox( Mins, Maxs, 200, 180, 0, SELECT_NONE );
  207. pRender->EndRenderHitTarget();
  208. if ((m_pParent != NULL) && (m_bDrawLineToParent))
  209. {
  210. Vector vecOrigin;
  211. GetParent()->GetOrigin(vecOrigin);
  212. pRender->DrawLine( m_Origin, vecOrigin );
  213. }
  214. }
  215. //-----------------------------------------------------------------------------
  216. // Purpose:
  217. //-----------------------------------------------------------------------------
  218. int CMapPlayerHullHandle::SerializeRMF(std::fstream &File, BOOL bRMF)
  219. {
  220. return(0);
  221. }
  222. //-----------------------------------------------------------------------------
  223. // Purpose:
  224. //-----------------------------------------------------------------------------
  225. int CMapPlayerHullHandle::SerializeMAP(std::fstream &File, BOOL bRMF)
  226. {
  227. return(0);
  228. }
  229. //-----------------------------------------------------------------------------
  230. // Purpose: Overridden because origin helpers don't take the color of their
  231. // parent entity.
  232. // Input : red, green, blue -
  233. //-----------------------------------------------------------------------------
  234. void CMapPlayerHullHandle::SetRenderColor(unsigned char red, unsigned char green, unsigned char blue)
  235. {
  236. }
  237. //-----------------------------------------------------------------------------
  238. // Purpose: Overridden because origin helpers don't take the color of their
  239. // parent entity.
  240. // Input : red, green, blue -
  241. //-----------------------------------------------------------------------------
  242. void CMapPlayerHullHandle::SetRenderColor(color32 rgbColor)
  243. {
  244. }
  245. //-----------------------------------------------------------------------------
  246. // Purpose:
  247. // Input : szKey -
  248. // szValue -
  249. //-----------------------------------------------------------------------------
  250. void CMapPlayerHullHandle::OnParentKeyChanged(const char *szKey, const char *szValue)
  251. {
  252. if (stricmp(szKey, m_szKeyName) == 0)
  253. {
  254. sscanf(szValue, "%f %f %f", &m_Origin.x, &m_Origin.y, &m_Origin.z);
  255. CalcBounds();
  256. }
  257. }
  258. //-----------------------------------------------------------------------------
  259. // Purpose:
  260. // Input : vecOrigin -
  261. //-----------------------------------------------------------------------------
  262. void CMapPlayerHullHandle::UpdateOrigin(const Vector &vecOrigin)
  263. {
  264. m_Origin = vecOrigin;
  265. CalcBounds();
  266. UpdateParentKey();
  267. }
  268. //-----------------------------------------------------------------------------
  269. // Purpose:
  270. //-----------------------------------------------------------------------------
  271. void CMapPlayerHullHandle::UpdateParentKey(void)
  272. {
  273. // Snap to prevent error creep.
  274. for (int i = 0; i < 3; i++)
  275. {
  276. m_Origin[i] = V_rint(m_Origin[i] / 0.01f) * 0.01f;
  277. }
  278. if (m_szKeyName[0])
  279. {
  280. CMapEntity *pEntity = dynamic_cast <CMapEntity *> (m_pParent);
  281. if (pEntity != NULL)
  282. {
  283. char szValue[KEYVALUE_MAX_VALUE_LENGTH];
  284. sprintf(szValue, "%g %g %g", (double)m_Origin.x, (double)m_Origin.y, (double)m_Origin.z);
  285. pEntity->NotifyChildKeyChanged(this, m_szKeyName, szValue);
  286. }
  287. }
  288. }
  289. //-----------------------------------------------------------------------------
  290. // Purpose:
  291. // Input : pTransBox -
  292. //-----------------------------------------------------------------------------
  293. void CMapPlayerHullHandle::DoTransform(const VMatrix &matrix)
  294. {
  295. BaseClass::DoTransform(matrix);
  296. UpdateParentKey();
  297. }
  298. //-----------------------------------------------------------------------------
  299. // Purpose: Sets the keyvalue in our parent when we are added to the world.
  300. // Input : pWorld -
  301. //-----------------------------------------------------------------------------
  302. void CMapPlayerHullHandle::OnAddToWorld(CMapWorld *pWorld)
  303. {
  304. BaseClass::OnAddToWorld(pWorld);
  305. UpdateParentKey();
  306. }
  307. //-----------------------------------------------------------------------------
  308. // Purpose: Called when we change because of an Undo or Redo.
  309. //-----------------------------------------------------------------------------
  310. void CMapPlayerHullHandle::OnUndoRedo(void)
  311. {
  312. // We've changed but our parent entity may not have. Update our parent.
  313. UpdateParentKey();
  314. }
  315. //-----------------------------------------------------------------------------
  316. // Purpose: Sets the keyvalue in our parent after the map is loaded.
  317. // Input : pWorld -
  318. //-----------------------------------------------------------------------------
  319. void CMapPlayerHullHandle::PostloadWorld(CMapWorld *pWorld)
  320. {
  321. BaseClass::PostloadWorld(pWorld);
  322. UpdateParentKey();
  323. }
  324. //-----------------------------------------------------------------------------
  325. // Purpose: Returns the appropriate object to the selection code.
  326. // Input : dwFlags - selectPicky or selectNormal
  327. // Output : Returns a pointer to the object that should be selected, based on
  328. // the selection mode.
  329. //-----------------------------------------------------------------------------
  330. CMapClass *CMapPlayerHullHandle::PrepareSelection(SelectMode_t eSelectMode)
  331. {
  332. Assert(m_pOwner);
  333. return m_pOwner->PrepareSelection(eSelectMode);
  334. }