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.

415 lines
12 KiB

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