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.

324 lines
8.1 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 "fgdlib/HelperInfo.h"
  10. #include "MapAlignedBox.h"
  11. #include "MapEntity.h"
  12. #include "Options.h"
  13. #include "Render2D.h"
  14. #include "Render3D.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include <tier0/memdbgon.h>
  17. IMPLEMENT_MAPCLASS(CMapAlignedBox)
  18. //-----------------------------------------------------------------------------
  19. // Purpose: Factory function. Used for creating a CMapAlignedBox from a set
  20. // of string parameters from the FGD file.
  21. // Input : *pInfo - Pointer to helper info class which gives us information
  22. // about how to create the class.
  23. // Output : Returns a pointer to the class, NULL if an error occurs.
  24. //-----------------------------------------------------------------------------
  25. CMapClass *CMapAlignedBox::Create(CHelperInfo *pHelperInfo, CMapEntity *pParent)
  26. {
  27. if (stricmp(pHelperInfo->GetName(), "wirebox") == 0)
  28. {
  29. const char *pMinsKeyName, *pMaxsKeyName;
  30. if (((pMinsKeyName = pHelperInfo->GetParameter(0)) != NULL) &&
  31. ((pMaxsKeyName = pHelperInfo->GetParameter(1)) != NULL))
  32. {
  33. CMapAlignedBox *pBox = new CMapAlignedBox(pMinsKeyName, pMaxsKeyName);
  34. pBox->m_bWireframe = true;
  35. return pBox;
  36. }
  37. else
  38. {
  39. return NULL;
  40. }
  41. }
  42. else
  43. {
  44. //
  45. // Extract the box mins and maxs from the input parameter list.
  46. //
  47. Vector Mins;
  48. Vector Maxs;
  49. int nParam = 0;
  50. int nCount = pHelperInfo->GetParameterCount();
  51. for (int i = 0; i < nCount; i++)
  52. {
  53. const char *pszParam = pHelperInfo->GetParameter(i);
  54. if (nParam < 3)
  55. {
  56. Mins[nParam] = atof(pszParam);
  57. }
  58. else if (nParam < 6)
  59. {
  60. Maxs[nParam % 3] = atof(pszParam);
  61. }
  62. else
  63. {
  64. break;
  65. }
  66. nParam++;
  67. }
  68. CMapAlignedBox *pBox = NULL;
  69. //
  70. // If we got enough parameters to define the box, create it.
  71. //
  72. if (nParam >= 6)
  73. {
  74. pBox = new CMapAlignedBox(Mins, Maxs);
  75. }
  76. return(pBox);
  77. }
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose:
  81. //-----------------------------------------------------------------------------
  82. CMapAlignedBox::CMapAlignedBox(void)
  83. {
  84. m_bUseKeyName = false;
  85. m_bWireframe = false;
  86. m_Mins.Init();
  87. m_Maxs.Init();
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose:
  91. // Input : pfMins -
  92. // pfMaxs -
  93. //-----------------------------------------------------------------------------
  94. CMapAlignedBox::CMapAlignedBox(Vector &Mins, Vector &Maxs)
  95. {
  96. m_bUseKeyName = false;
  97. m_bWireframe = false;
  98. m_Mins = Mins;
  99. m_Maxs = Maxs;
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Purpose:
  103. // Input : *pMinsKeyName -
  104. // *pMaxsKeyName -
  105. //-----------------------------------------------------------------------------
  106. CMapAlignedBox::CMapAlignedBox(const char *pMinsKeyName, const char *pMaxsKeyName)
  107. {
  108. m_bUseKeyName = true;
  109. m_bWireframe = false;
  110. strncpy(m_MinsKeyName, pMinsKeyName, sizeof(m_MinsKeyName)-1);
  111. strncpy(m_MaxsKeyName, pMaxsKeyName, sizeof(m_MaxsKeyName)-1);
  112. m_MinsKeyName[sizeof(m_MinsKeyName)-1] = 0;
  113. m_MaxsKeyName[sizeof(m_MaxsKeyName)-1] = 0;
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Purpose:
  117. //-----------------------------------------------------------------------------
  118. CMapAlignedBox::~CMapAlignedBox(void)
  119. {
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Purpose:
  123. // Input : bFullUpdate -
  124. //-----------------------------------------------------------------------------
  125. void CMapAlignedBox::CalcBounds(BOOL bFullUpdate)
  126. {
  127. Vector AbsMins = m_Origin + m_Mins;
  128. Vector AbsMaxs = m_Origin + m_Maxs;
  129. m_Render2DBox.ResetBounds();
  130. m_Render2DBox.UpdateBounds(AbsMins, AbsMaxs);
  131. m_CullBox = m_Render2DBox;
  132. m_BoundingBox = m_Render2DBox;
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Purpose:
  136. // Output : CMapClass
  137. //-----------------------------------------------------------------------------
  138. CMapClass *CMapAlignedBox::Copy(bool bUpdateDependencies)
  139. {
  140. CMapAlignedBox *pCopy = new CMapAlignedBox;
  141. if (pCopy != NULL)
  142. {
  143. pCopy->CopyFrom(this, bUpdateDependencies);
  144. }
  145. return(pCopy);
  146. }
  147. //-----------------------------------------------------------------------------
  148. // Purpose:
  149. // Input : pObject -
  150. // Output : CMapClass
  151. //-----------------------------------------------------------------------------
  152. CMapClass *CMapAlignedBox::CopyFrom(CMapClass *pObject, bool bUpdateDependencies)
  153. {
  154. Assert(pObject->IsMapClass(MAPCLASS_TYPE(CMapAlignedBox)));
  155. CMapAlignedBox *pFrom = (CMapAlignedBox *)pObject;
  156. CMapClass::CopyFrom(pObject, bUpdateDependencies);
  157. m_bUseKeyName = pFrom->m_bUseKeyName;
  158. m_bWireframe = pFrom->m_bWireframe;
  159. strncpy(m_MinsKeyName, pFrom->m_MinsKeyName, sizeof(m_MinsKeyName)-1);
  160. strncpy(m_MaxsKeyName, pFrom->m_MaxsKeyName, sizeof(m_MaxsKeyName)-1);
  161. m_MinsKeyName[sizeof(pFrom->m_MinsKeyName)-1] = 0;
  162. m_MaxsKeyName[sizeof(pFrom->m_MaxsKeyName)-1] = 0;
  163. m_Mins = pFrom->m_Mins;
  164. m_Maxs = pFrom->m_Maxs;
  165. return(this);
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Purpose:
  169. // Input : pRender -
  170. //-----------------------------------------------------------------------------
  171. void CMapAlignedBox::Render2D(CRender2D *pRender)
  172. {
  173. Vector vecMins, vecMaxs;
  174. GetRender2DBox(vecMins, vecMaxs);
  175. if (!IsSelected())
  176. {
  177. pRender->SetDrawColor( r, g, b);
  178. pRender->SetHandleColor( r, g, b);
  179. }
  180. else
  181. {
  182. pRender->SetDrawColor( GetRValue(Options.colors.clrSelection), GetGValue(Options.colors.clrSelection), GetBValue(Options.colors.clrSelection) );
  183. pRender->SetHandleColor( GetRValue(Options.colors.clrSelection), GetGValue(Options.colors.clrSelection), GetBValue(Options.colors.clrSelection) );
  184. }
  185. // Draw the bounding box.
  186. pRender->DrawBox( vecMins, vecMaxs );
  187. //
  188. // Draw center handle.
  189. //
  190. if ( pRender->IsActiveView() )
  191. {
  192. Vector2D pt, pt2;
  193. pRender->TransformPoint(pt, vecMins);
  194. pRender->TransformPoint(pt2, vecMaxs);
  195. int sizex = abs(pt.x - pt2.x)+1;
  196. int sizey = abs(pt.y - pt2.y)+1;
  197. // dont draw handle if object is too small
  198. if ( sizex > 6 && sizey > 6 )
  199. {
  200. pRender->SetHandleStyle( HANDLE_RADIUS, CRender::HANDLE_CROSS );
  201. pRender->DrawHandle( (vecMins+vecMaxs)/2 );
  202. }
  203. }
  204. }
  205. //-----------------------------------------------------------------------------
  206. // Purpose:
  207. // Input : pRender -
  208. //-----------------------------------------------------------------------------
  209. void CMapAlignedBox::Render3D(CRender3D *pRender)
  210. {
  211. if (!m_bWireframe)
  212. {
  213. pRender->BeginRenderHitTarget(this);
  214. pRender->RenderBox(m_CullBox.bmins, m_CullBox.bmaxs, r, g, b, GetSelectionState());
  215. pRender->EndRenderHitTarget();
  216. }
  217. else if (GetSelectionState() != SELECT_NONE)
  218. {
  219. pRender->RenderWireframeBox(m_CullBox.bmins, m_CullBox.bmaxs, 255, 255, 0);
  220. }
  221. }
  222. //-----------------------------------------------------------------------------
  223. // Purpose:
  224. // Input : File -
  225. // bRMF -
  226. // Output : int
  227. //-----------------------------------------------------------------------------
  228. int CMapAlignedBox::SerializeRMF(std::fstream &File, BOOL bRMF)
  229. {
  230. return(0);
  231. }
  232. //-----------------------------------------------------------------------------
  233. // Purpose:
  234. // Input : File -
  235. // bRMF -
  236. // Output : int
  237. //-----------------------------------------------------------------------------
  238. int CMapAlignedBox::SerializeMAP(std::fstream &File, BOOL bRMF)
  239. {
  240. return(0);
  241. }
  242. //-----------------------------------------------------------------------------
  243. // Purpose:
  244. // Input : key -
  245. // value -
  246. //-----------------------------------------------------------------------------
  247. void CMapAlignedBox::OnParentKeyChanged( const char* key, const char* value )
  248. {
  249. if (!m_bUseKeyName)
  250. {
  251. return;
  252. }
  253. if (stricmp(key, m_MinsKeyName) == 0)
  254. {
  255. sscanf(value, "%f %f %f", &m_Mins[0], &m_Mins[1], &m_Mins[2]);
  256. }
  257. else if (stricmp(key, m_MaxsKeyName) == 0)
  258. {
  259. sscanf(value, "%f %f %f", &m_Maxs[0], &m_Maxs[1], &m_Maxs[2]);
  260. }
  261. PostUpdate(Notify_Changed);
  262. }