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.

203 lines
4.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Default implementation for interface common to 2D and 3D views.
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #include "stdafx.h"
  14. #include "MapDoc.h"
  15. #include "MapView.h"
  16. #include "History.h"
  17. #include "mapsolid.h"
  18. #include "camera.h"
  19. #define MMNOMIXER
  20. #include <mmsystem.h>
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include <tier0/memdbgon.h>
  23. #pragma warning(disable:4244 4305)
  24. //-----------------------------------------------------------------------------
  25. // Purpose: Constructor, sets to inactive.
  26. //-----------------------------------------------------------------------------
  27. CMapView::CMapView(void)
  28. {
  29. m_bActive = false;
  30. m_bUpdateView = false;
  31. m_eDrawType = VIEW_INVALID;
  32. m_pCamera = NULL;
  33. m_dwTimeLastRender = 0;
  34. m_nRenderedFrames = 0;
  35. m_pToolManager = NULL;
  36. }
  37. bool CMapView::IsOrthographic()
  38. {
  39. return m_pCamera->IsOrthographic();
  40. }
  41. void CMapView::UpdateView( int nFlags )
  42. {
  43. m_bUpdateView = true;
  44. if ( nFlags & MAPVIEW_RENDER_NOW )
  45. {
  46. RenderView();
  47. }
  48. }
  49. void CMapView::ActivateView(bool bActivate)
  50. {
  51. if ( bActivate != m_bActive )
  52. {
  53. m_bActive = bActivate;
  54. m_bUpdateView = true;
  55. }
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Purpose: Recalculates framerate since the last call to this function and
  59. // determines whether or not we are exceeding the framerate limit.
  60. // Output : Returns true if we are beyond the framerate limit, false if not.
  61. //-----------------------------------------------------------------------------
  62. bool CMapView::ShouldRender()
  63. {
  64. DWORD dwTimeNow = timeGetTime();
  65. if (m_dwTimeLastRender != 0)
  66. {
  67. DWORD dwTimeElapsed = dwTimeNow - m_dwTimeLastRender;
  68. if ( dwTimeElapsed <= 0 )
  69. return false;
  70. float flFrameRate = (1000.0f / dwTimeElapsed);
  71. if (flFrameRate > 100.0f)
  72. {
  73. // never update view faster then 100Hz
  74. return false;
  75. }
  76. }
  77. // update view if needed
  78. if ( m_bUpdateView )
  79. {
  80. m_dwTimeLastRender = dwTimeNow;
  81. m_nRenderedFrames++;
  82. return true;
  83. }
  84. return false;
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose:
  88. // Input : point - Point in client coordinates.
  89. // bMakeFirst -
  90. // Output : Returns true on success, false on failure.
  91. //-----------------------------------------------------------------------------
  92. bool CMapView::SelectAt(const Vector2D &ptClient, bool bMakeFirst, bool bFace)
  93. {
  94. CMapDoc *pDoc = GetMapDoc();
  95. CSelection *pSelection = pDoc->GetSelection();
  96. const CMapObjectList *pSelList = pSelection->GetList();
  97. pSelection->ClearHitList();
  98. GetHistory()->MarkUndoPosition(pSelList, "Selection");
  99. //
  100. // Check all the objects in the world for a hit at this point.
  101. //
  102. HitInfo_t HitData[MAX_PICK_HITS];
  103. int nHits = ObjectsAt(ptClient, HitData, MAX_PICK_HITS);
  104. //
  105. // If there were no hits at the given point, clear selection.
  106. //
  107. if ( nHits == 0 )
  108. {
  109. if (bMakeFirst)
  110. {
  111. pDoc->SelectFace(NULL, 0, scClear|scSaveChanges );
  112. pDoc->SelectObject(NULL, scClear|scSaveChanges );
  113. }
  114. return false;
  115. }
  116. SelectMode_t eSelectMode = pSelection->GetMode();
  117. for ( int i=0; i<nHits; i++ )
  118. {
  119. if ( HitData[i].pObject )
  120. {
  121. CMapClass *pSelObject = HitData[i].pObject->PrepareSelection( eSelectMode );
  122. if (pSelObject)
  123. {
  124. pSelection->AddHit(pSelObject);
  125. }
  126. }
  127. }
  128. //
  129. // If we're selecting faces, mark all faces on the first solid we hit.
  130. //
  131. if ( bFace )
  132. {
  133. const CMapObjectList *pList = pSelection->GetHitList();
  134. FOR_EACH_OBJ( *pList, pos )
  135. {
  136. CMapClass *pObject = pList->Element(pos);
  137. if (pObject->IsMapClass(MAPCLASS_TYPE(CMapSolid)))
  138. {
  139. pDoc->SelectFace((CMapSolid*)pObject, -1, scSelect | (bMakeFirst ? scClear : 0));
  140. break;
  141. }
  142. }
  143. return true;
  144. }
  145. //
  146. // Select a single object.
  147. //
  148. if ( bMakeFirst )
  149. {
  150. pDoc->SelectFace(NULL, 0, scClear|scSaveChanges);
  151. pDoc->SelectObject(NULL, scClear|scSaveChanges);
  152. }
  153. pSelection->SetCurrentHit(hitFirst);
  154. return true;
  155. }
  156. void CMapView::BuildRay( const Vector2D &vView, Vector& vStart, Vector& vEnd )
  157. {
  158. m_pCamera->BuildRay( vView, vStart, vEnd );
  159. }
  160. const Vector &CMapView::GetViewAxis()
  161. {
  162. static Vector vForward;
  163. m_pCamera->GetViewForward(vForward);
  164. return vForward;
  165. }