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.

710 lines
22 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Rendering and mouse handling in the logical view.
  4. //
  5. //===========================================================================//
  6. #include "stdafx.h"
  7. #include "MapViewLogical.h"
  8. #include "Render2D.h"
  9. #include "MapWorld.h"
  10. #include "TitleWnd.h"
  11. #include "MapDoc.h"
  12. #include "ToolManager.h"
  13. #include "history.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include <tier0/memdbgon.h>
  16. IMPLEMENT_DYNCREATE(CMapViewLogical, CMapView2DBase)
  17. BEGIN_MESSAGE_MAP(CMapViewLogical, CMapView2DBase)
  18. //{{AFX_MSG_MAP(CMapViewLogical)
  19. ON_WM_TIMER()
  20. //}}AFX_MSG_MAP
  21. END_MESSAGE_MAP()
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Logical View Look and feel constants
  24. //-----------------------------------------------------------------------------
  25. #define LOGICAL_CONN_VERT_SPACING 100
  26. #define LOGICAL_CONN_HORIZ_SPACING 20
  27. #define LOGICAL_CONN_SPREAD_DIST 50
  28. #define LOGICAL_CONN_TEXT_LENGTH 450
  29. #define LOGICAL_CONN_CROSS_SIZE 30
  30. #define LOGICAL_CONN_MULTI_CIRCLE_RADIUS 15
  31. // Broken connection blinking interval (in ms)
  32. #define TIMER_BLINK_INTERVAL 512
  33. // Unselected and selected color values for the connections palette against the background color
  34. #define DARK 112
  35. #define MID 144
  36. #define BACKGROUND 168
  37. #define LITE 224
  38. #define BRITE 255
  39. #define LOGICAL_CONN_COLOR_COUNT 7
  40. #define LOGICAL_CONN_SELECTION_STATES 2
  41. static color32 s_pWireColors[LOGICAL_CONN_COLOR_COUNT][LOGICAL_CONN_SELECTION_STATES] =
  42. {
  43. { { MID, MID, 0, 255 }, /* Mid Yellow */ { BRITE, BRITE, 0, 255 }, /* Bright Yellow */ },
  44. { { MID, DARK, 0, 255 }, /* Dark Orange */ { BRITE, MID, 0, 255 }, /* Bright Orange */ },
  45. { { 0, DARK, 0, 255 }, /* Dark Green */ { 0, BRITE, 0, 255 }, /* Bright Green */ },
  46. { { 0, MID, MID, 255 }, /* Mid Cyan */ { 0, BRITE, BRITE, 255 }, /* Bright Cyan */ },
  47. { { 0, 0, MID, 255 }, /* Mid Blue */ { 0, MID, BRITE, 255 }, /* Bright Baby Blue */ },
  48. { { MID, 0, MID, 255 }, /* Mid Magenta */ { BRITE, 0, BRITE, 255 }, /* Bright Magenta */ },
  49. { { DARK, DARK, DARK, 255 }, /* Dark Gray */ { BRITE, BRITE, BRITE, 255 }, /* Bright White */ },
  50. };
  51. static color32 s_pBrokenWireColor[LOGICAL_CONN_SELECTION_STATES] =
  52. {
  53. { DARK, 0, 0, 255 }, /* Dark Red */ { BRITE, 0, 0, 255 }, /* Bright Red */
  54. };
  55. //-----------------------------------------------------------------------------
  56. // Purpose: Constructor. Initializes data members.
  57. // ---------------------------------------------------------------------------
  58. CMapViewLogical::CMapViewLogical(void) : m_RenderDict( 0, 1024, DefLessFunc( CMapClass* ) )
  59. {
  60. m_bUpdateRenderObjects = true;
  61. SetAxes(AXIS_X, FALSE, AXIS_Y, TRUE);
  62. SetDrawType( VIEW_LOGICAL );
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose: Destructor. Frees dynamically allocated resources.
  66. //-----------------------------------------------------------------------------
  67. CMapViewLogical::~CMapViewLogical(void)
  68. {
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose: First-time initialization of this view.
  72. //-----------------------------------------------------------------------------
  73. void CMapViewLogical::OnInitialUpdate(void)
  74. {
  75. CreateTitleWindow();
  76. GetTitleWnd()->SetTitle("Logical");
  77. SetZoom(0); // Zoom out as far as possible.
  78. UpdateClientView();
  79. CMapView2DBase::OnInitialUpdate();
  80. // FIXME: Hardcoded light gray background - should be from a new "Logical View" options settings dialog
  81. m_ClearColor.SetColor( BACKGROUND, BACKGROUND, BACKGROUND, 255 );
  82. m_clrGrid.SetColor( MID, MID, MID, 255 );
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose:
  86. // Input : point - Point in client coordinates.
  87. // bMakeFirst -
  88. // Output : Returns true on success, false on failure.
  89. //-----------------------------------------------------------------------------
  90. bool CMapViewLogical::SelectAtCascading( const Vector2D &ptClient, bool bMakeFirst )
  91. {
  92. CMapDoc *pDoc = GetMapDoc();
  93. CSelection *pSelection = pDoc->GetSelection();
  94. pSelection->ClearHitList();
  95. GetHistory()->MarkUndoPosition(pSelection->GetList(), "Selection");
  96. //
  97. // Check all the objects in the world for a hit at this point.
  98. //
  99. HitInfo_t HitData[MAX_PICK_HITS];
  100. int nHits = ObjectsAt(ptClient, HitData, MAX_PICK_HITS);
  101. // If there were no hits at the given point, clear selection.
  102. if ( nHits == 0 )
  103. {
  104. if (bMakeFirst)
  105. {
  106. pDoc->SelectFace(NULL, 0, scClear|scSaveChanges);
  107. pDoc->SelectObject(NULL, scClear|scSaveChanges);
  108. }
  109. return false;
  110. }
  111. SelectMode_t eSelectMode = pSelection->GetMode();
  112. for ( int i=0; i<nHits; ++i )
  113. {
  114. CMapClass *pSelObject = HitData[i].pObject->PrepareSelection( eSelectMode );
  115. if ( !pSelObject )
  116. continue;
  117. pSelection->AddHit( pSelObject );
  118. }
  119. //
  120. // Select a single object.
  121. //
  122. if ( bMakeFirst )
  123. {
  124. pDoc->SelectFace( NULL, 0, scClear|scSaveChanges );
  125. pDoc->SelectObject( NULL, scClear|scSaveChanges );
  126. }
  127. pSelection->SetCurrentHit( hitFirst, true );
  128. return true;
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Base class calls this when render lists need rebuilding
  132. //-----------------------------------------------------------------------------
  133. void CMapViewLogical::OnRenderListDirty()
  134. {
  135. m_bUpdateRenderObjects = true;
  136. }
  137. //-----------------------------------------------------------------------------
  138. // Purpose: Builds up list of mapclasses to render
  139. //-----------------------------------------------------------------------------
  140. void CMapViewLogical::AddToRenderLists( CMapClass *pObject )
  141. {
  142. #if _DEBUG && 0
  143. CMapEntity *pEntity = dynamic_cast<CMapEntity *>(pObject);
  144. if (pEntity)
  145. {
  146. LPCTSTR pszTargetName = pEntity->GetKeyValue("targetname");
  147. if ( pszTargetName && !strcmp(pszTargetName, "relay_cancelVCDs") )
  148. {
  149. // Set breakpoint here for debugging this entity's visiblity
  150. int foo = 0;
  151. }
  152. }
  153. #endif
  154. if ( !pObject->IsVisibleLogical() )
  155. return;
  156. // Don't render groups, render their children instead.
  157. if ( !pObject->IsGroup() && pObject->IsLogical() )
  158. {
  159. Vector2D vecMins, vecMaxs;
  160. pObject->GetRenderLogicalBox( vecMins, vecMaxs );
  161. // Always paint all the entities to ensure that any inter-entity connections are visible
  162. // if ( !IsValidBox( vecMins, vecMaxs ) || IsInClientView( vecMins, vecMaxs ) )
  163. {
  164. // Make sure the object is in the update region.
  165. m_RenderList.AddToTail( pObject );
  166. m_ConnectionList.AddToTail( pObject );
  167. m_RenderDict.Insert( pObject );
  168. }
  169. }
  170. // Recurse into children and add them.
  171. const CMapObjectList *pChildren = pObject->GetChildren();
  172. FOR_EACH_OBJ( *pChildren, pos )
  173. {
  174. AddToRenderLists( pChildren->Element(pos) );
  175. }
  176. }
  177. //-----------------------------------------------------------------------------
  178. // Purpose: Builds up list of mapclasses to render
  179. //-----------------------------------------------------------------------------
  180. void CMapViewLogical::PopulateConnectionList( )
  181. {
  182. while ( m_ConnectionUpdate.Count() )
  183. {
  184. CMapClass *pObject;
  185. m_ConnectionUpdate.Pop( pObject );
  186. if ( !pObject->IsVisibleLogical() )
  187. continue;
  188. // Don't render groups, render their children instead.
  189. if ( !pObject->IsGroup() && pObject->IsLogical() )
  190. {
  191. // Don't add it if it's visible already
  192. if ( m_RenderDict.Find( pObject ) == m_RenderDict.InvalidIndex() )
  193. {
  194. CEditGameClass *pClass = dynamic_cast< CEditGameClass * >( pObject );
  195. if ( pClass )
  196. {
  197. int nCount = pClass->Connections_GetCount();
  198. for ( int i = 0; i < nCount; ++i )
  199. {
  200. CEntityConnection *pConn = pClass->Connections_Get( i );
  201. // Find the input entity associated with this connection
  202. CMapEntityList *pEntityList = pConn->GetTargetEntityList();
  203. int j;
  204. int nInputCount = pEntityList->Count();
  205. for ( j = 0; j < nInputCount; ++j )
  206. {
  207. CMapEntity *pEntity = pEntityList->Element(j);
  208. if ( m_RenderDict.Find( pEntity ) != m_RenderDict.InvalidIndex() )
  209. {
  210. m_ConnectionList.AddToTail( pObject );
  211. break;
  212. }
  213. }
  214. if ( j != nInputCount )
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. // Recurse into children and add them.
  221. const CMapObjectList *pChildren = pObject->GetChildren();
  222. FOR_EACH_OBJ( *pChildren, pos )
  223. {
  224. m_ConnectionUpdate.Push( pChildren->Element(pos) );
  225. }
  226. }
  227. }
  228. //-----------------------------------------------------------------------------
  229. // Purpose:
  230. // Input : nIDEvent -
  231. //-----------------------------------------------------------------------------
  232. void CMapViewLogical::OnTimer(UINT nIDEvent)
  233. {
  234. if ( nIDEvent == TIMER_CONNECTIONUPDATE )
  235. {
  236. // Make sure we don't blink too fast
  237. static unsigned int nLastUpdate = 0;
  238. if ( GetTickCount() >= (nLastUpdate+TIMER_BLINK_INTERVAL/2) )
  239. {
  240. nLastUpdate = GetTickCount();
  241. UpdateView( 0 ); // Force the view to redraw for blinking errors
  242. }
  243. }
  244. else
  245. CView::OnTimer(nIDEvent);
  246. }
  247. const color32 & CMapViewLogical::GetWireColor(const char *pszName, const bool bSelected, const bool bError, const bool bAnySelected)
  248. {
  249. // Select the connecting color based on the string passed in
  250. // (using OutputName for instance, gives varying "wire colors" by entity output type).
  251. Assert( LOGICAL_CONN_COLOR_COUNT == (sizeof(s_pWireColors) / sizeof(color32) ) / LOGICAL_CONN_SELECTION_STATES );
  252. if ( !bError )
  253. {
  254. int nIndex = 0;
  255. // Has the named passed in
  256. while ( *pszName )
  257. nIndex += *pszName++;
  258. // Index based on the number of colors available
  259. nIndex %= LOGICAL_CONN_COLOR_COUNT;
  260. // Only blink non-errors if the drawing object is selected
  261. // bool bBlinkingState = bSelected ? (GetTickCount() / TIMER_BLINK_INTERVAL) & 1 : 0;
  262. return s_pWireColors[nIndex][bSelected];
  263. }
  264. else
  265. {
  266. // Only blink errors if nothing is selected, or this is selected
  267. bool bBlinkingState = bSelected || !bAnySelected ? (GetTickCount() / TIMER_BLINK_INTERVAL) & 1 : bSelected;
  268. return s_pBrokenWireColor[bBlinkingState];
  269. }
  270. }
  271. //-----------------------------------------------------------------------------
  272. // Draws a wire from a particular point to a target
  273. //-----------------------------------------------------------------------------
  274. #define BACKWARD_WIRE_OVERSHOOT 50
  275. #define BACKWARD_WIRE_Y_DISTANCE 150
  276. void CMapViewLogical::DrawConnectingWire( float x, float y, CMapEntity *pSource, CEntityConnection *pConnection, CMapEntity *pTarget )
  277. {
  278. CRender2D *pRender = GetRender();
  279. // FIXME: Deal with bad input type
  280. Vector2D vecEndPosition, vecConnector;
  281. pTarget->GetLogicalConnectionPosition( LOGICAL_CONNECTION_INPUT, vecConnector );
  282. vecEndPosition = vecConnector;
  283. int nInputs = pTarget->Upstream_GetCount();
  284. // Compensate for multiple inputs -- fan-in the connections from the various pSource entities
  285. BOOL bFound = false;
  286. if ( nInputs )
  287. {
  288. int nInput;
  289. for ( nInput = 0; nInput < nInputs; nInput++ )
  290. {
  291. CEntityConnection *pInputConnection = pTarget->Upstream_Get(nInput);
  292. if ( pInputConnection )
  293. {
  294. if ( pInputConnection == pConnection )
  295. {
  296. bFound = true;
  297. break;
  298. }
  299. }
  300. }
  301. if (bFound)
  302. {
  303. vecEndPosition.x -= LOGICAL_CONN_SPREAD_DIST;
  304. vecEndPosition.y += ( (nInputs - 1) * LOGICAL_CONN_VERT_SPACING / 2 ) / 2 - (nInput * LOGICAL_CONN_VERT_SPACING / 2);
  305. pRender->MoveTo( Vector( vecEndPosition.x, vecEndPosition.y, 0.0f) );
  306. pRender->DrawLineTo( Vector( vecConnector.x, vecConnector.y, 0.0f) );
  307. }
  308. else
  309. {
  310. Assert(0);
  311. }
  312. }
  313. pRender->MoveTo( Vector( x, y, 0.0f ) );
  314. if ( x < vecEndPosition.x )
  315. {
  316. // Do direct connection
  317. pRender->DrawLineTo( Vector( x, vecEndPosition.y, 0.0f ) );
  318. pRender->DrawLineTo( Vector( vecEndPosition.x, vecEndPosition.y, 0.0f ) );
  319. return;
  320. }
  321. Vector2D vecTargetMins, vecTargetMaxs;
  322. pTarget->GetRenderLogicalBox( vecTargetMins, vecTargetMaxs );
  323. vecTargetMins.y -= BACKWARD_WIRE_Y_DISTANCE;
  324. vecTargetMaxs.y += BACKWARD_WIRE_Y_DISTANCE;
  325. float flHalfY = ( y + vecEndPosition.y ) * 0.5f;
  326. if ( flHalfY > vecTargetMins.y && flHalfY < vecTargetMaxs.y )
  327. {
  328. flHalfY = ( flHalfY < vecEndPosition.y ) ? vecTargetMins.y : vecTargetMaxs.y;
  329. }
  330. pRender->DrawLineTo( Vector( x, flHalfY, 0.0f ) );
  331. pRender->DrawLineTo( Vector( vecEndPosition.x - BACKWARD_WIRE_OVERSHOOT, flHalfY, 0.0f ) );
  332. pRender->DrawLineTo( Vector( vecEndPosition.x - BACKWARD_WIRE_OVERSHOOT, vecEndPosition.y, 0.0f ) );
  333. pRender->DrawLineTo( Vector( vecEndPosition.x, vecEndPosition.y, 0.0f ) );
  334. }
  335. void CMapViewLogical::RenderConnections(const bool bDrawSelected, const bool bAnySelected)
  336. {
  337. Vector2D pt, pt2;
  338. WorldToClient( pt, Vector( 0.0f, 0.0f, 0.0f ) );
  339. WorldToClient( pt2, Vector( 0.0f, LOGICAL_CONN_VERT_SPACING, 0.0f ) );
  340. int nCount = m_ConnectionList.Count();
  341. for ( int i = 0; i < nCount ; ++i )
  342. {
  343. CMapEntity *pMapClass = dynamic_cast<CMapEntity*>( m_ConnectionList[i] );
  344. if ( !pMapClass )
  345. continue;
  346. CEditGameClass *pEditClass = dynamic_cast<CEditGameClass*>( pMapClass );
  347. if ( !pEditClass )
  348. continue;
  349. int nConnectionCount = pEditClass->Connections_GetCount();
  350. if ( nConnectionCount == 0 )
  351. continue;
  352. Vector2D vecStartPosition;
  353. pMapClass->GetLogicalConnectionPosition( LOGICAL_CONNECTION_OUTPUT, vecStartPosition );
  354. CRender2D *pRender = GetRender();
  355. float x = vecStartPosition.x + LOGICAL_CONN_SPREAD_DIST + LOGICAL_CONN_TEXT_LENGTH;
  356. float y = vecStartPosition.y + ( nConnectionCount - 1 ) * LOGICAL_CONN_VERT_SPACING / 2;
  357. for ( int j = 0; j < nConnectionCount; ++j )
  358. {
  359. CEntityConnection *pConn = pEditClass->Connections_Get( j );
  360. // Find the input entity associated with this connection
  361. CMapEntityList *pEntityList = pConn->GetTargetEntityList();
  362. int nInputCount = pEntityList->Count();
  363. bool bBadInput = !MapEntityList_HasInput( pEntityList, pConn->GetInputName() );
  364. bool bBadConnection = (nInputCount == 0);
  365. // Stop drawing entity connection text once the entity itself gets too small.
  366. bool bDrawOutput = ( fabs( pt.y - pt2.y ) >= 16 );
  367. bool bDrawDelay = ( fabs( pt.y - pt2.y ) >= 20 );
  368. bool bDrawInput = ( fabs( pt.y - pt2.y ) >= 24 );
  369. bool bDrawTarget = ( fabs( pt.y - pt2.y ) >= 28 );
  370. bool bEntitySelected = ( pMapClass->GetSelectionState() != SELECT_NONE );
  371. bool bInputSelected = false;
  372. for ( int k = 0; k < nInputCount; ++k )
  373. {
  374. if ( pEntityList->Element( k )->GetSelectionState() != SELECT_NONE )
  375. bInputSelected = true;
  376. // Make sure all the connected entities are all visible
  377. if ( pEntityList->Element( k )->IsVisibleLogical() == false )
  378. bBadConnection = true;
  379. }
  380. // Make sure we only draw the selected OR unselected connections as requested
  381. if ( bDrawSelected == (bEntitySelected || bInputSelected) )
  382. {
  383. color32 c = GetWireColor( pConn->GetOutputName(),
  384. bEntitySelected || bInputSelected,
  385. bBadConnection || bBadInput,
  386. bAnySelected );
  387. if ( bDrawDelay || bDrawOutput || bDrawInput || bDrawTarget )
  388. {
  389. char pBuf[1024];
  390. pRender->SetTextColor( c.r, c.g, c.b );
  391. int nChars = 0;
  392. if ( bDrawOutput )
  393. nChars += Q_snprintf( pBuf+nChars, sizeof(pBuf), "%s", pConn->GetOutputName() );
  394. if ( bDrawDelay )
  395. nChars += Q_snprintf( pBuf+nChars, sizeof(pBuf), "(%.2f)", pConn->GetDelay() );
  396. if ( nChars )
  397. pRender->DrawText( pBuf, Vector2D( vecStartPosition.x + LOGICAL_CONN_SPREAD_DIST, y ), 2, 1, CRender2D::TEXT_JUSTIFY_TOP | CRender2D::TEXT_JUSTIFY_RIGHT );
  398. nChars = 0;
  399. if ( bDrawInput )
  400. nChars += Q_snprintf( pBuf+nChars, sizeof(pBuf), "%s", pConn->GetInputName() );
  401. if ( bDrawTarget )
  402. nChars += Q_snprintf( pBuf+nChars, sizeof(pBuf), "[%s] ", pConn->GetTargetName() );
  403. if ( nChars )
  404. pRender->DrawText( pBuf, Vector2D( vecStartPosition.x + LOGICAL_CONN_SPREAD_DIST, y ), 2, -1, CRender2D::TEXT_JUSTIFY_BOTTOM | CRender2D::TEXT_JUSTIFY_RIGHT );
  405. }
  406. pRender->SetDrawColor( c.r, c.g, c.b );
  407. pRender->MoveTo( Vector( vecStartPosition.x, vecStartPosition.y, 0.0f ) );
  408. pRender->DrawLineTo( Vector( vecStartPosition.x + LOGICAL_CONN_SPREAD_DIST, y, 0.0f ) );
  409. pRender->DrawLineTo( Vector( x, y, 0.0f ) );
  410. if ( bBadConnection )
  411. {
  412. // Draw an X for a bogus connection.
  413. pRender->MoveTo( Vector( x - LOGICAL_CONN_CROSS_SIZE, y - LOGICAL_CONN_CROSS_SIZE, 0.0f ) );
  414. pRender->DrawLineTo( Vector( x + LOGICAL_CONN_CROSS_SIZE, y + LOGICAL_CONN_CROSS_SIZE, 0.0f ) );
  415. pRender->MoveTo( Vector( x - LOGICAL_CONN_CROSS_SIZE, y + LOGICAL_CONN_CROSS_SIZE, 0.0f ) );
  416. pRender->DrawLineTo( Vector( x + LOGICAL_CONN_CROSS_SIZE, y - LOGICAL_CONN_CROSS_SIZE, 0.0f ) );
  417. }
  418. else if ( nInputCount == 1 )
  419. {
  420. DrawConnectingWire( x, y, pMapClass, pConn, pEntityList->Element( 0 ) );
  421. }
  422. else
  423. {
  424. // Draw a circle for multiple connections
  425. pRender->DrawCircle( Vector( x + LOGICAL_CONN_MULTI_CIRCLE_RADIUS, y, 0.0f ), LOGICAL_CONN_MULTI_CIRCLE_RADIUS );
  426. float mx = x + LOGICAL_CONN_SPREAD_DIST;
  427. float my = y + ( nInputCount / 2 ) * LOGICAL_CONN_VERT_SPACING/2;
  428. Vector vecStart( x + LOGICAL_CONN_MULTI_CIRCLE_RADIUS, y, 0.0f );
  429. for ( int k = 0; k < nInputCount; ++k )
  430. {
  431. // bBadInput = false; // This should be based on whether downstream entity has the specificied named input
  432. bInputSelected = ( pEntityList->Element( k )->GetSelectionState() != SELECT_NONE );
  433. color32 col = GetWireColor( pConn->GetOutputName(),
  434. bEntitySelected || bInputSelected,
  435. bBadInput,
  436. bAnySelected );
  437. pRender->SetDrawColor( col.r, col.g, col.b );
  438. Vector vecEnd( mx, my, 0.0f );
  439. Vector vecDelta;
  440. VectorSubtract( vecEnd, vecStart, vecDelta );
  441. VectorNormalize( vecDelta );
  442. vecDelta *= LOGICAL_CONN_MULTI_CIRCLE_RADIUS;
  443. vecDelta += vecStart;
  444. pRender->MoveTo( vecDelta );
  445. pRender->DrawLineTo( Vector( mx, my, 0.0f ) );
  446. DrawConnectingWire( mx, my, pMapClass, pConn, pEntityList->Element( k ) );
  447. mx += LOGICAL_CONN_HORIZ_SPACING;
  448. my -= LOGICAL_CONN_VERT_SPACING/2;
  449. }
  450. }
  451. }
  452. x += LOGICAL_CONN_HORIZ_SPACING * (nInputCount+1) + 2*LOGICAL_CONN_MULTI_CIRCLE_RADIUS;
  453. y -= LOGICAL_CONN_VERT_SPACING;
  454. }
  455. }
  456. }
  457. //-----------------------------------------------------------------------------
  458. // Purpose:
  459. //-----------------------------------------------------------------------------
  460. void CMapViewLogical::Render()
  461. {
  462. CMapDoc *pDoc = GetMapDoc();
  463. CMapWorld *pWorld = pDoc->GetMapWorld();
  464. GetRender()->StartRenderFrame();
  465. // Draw grid if enabled.
  466. if ( pDoc->m_bShowLogicalGrid )
  467. {
  468. DrawGridLogical( GetRender() );
  469. }
  470. // Draw the world if we have one.
  471. if (pWorld == NULL)
  472. return;
  473. // Traverse the entire world, sorting visible elements into two arrays:
  474. // Normal objects and selected objects, so that we can render the selected
  475. // objects last.
  476. if ( m_bUpdateRenderObjects )
  477. {
  478. m_bUpdateRenderObjects = false;
  479. m_RenderList.RemoveAll();
  480. m_RenderDict.RemoveAll();
  481. m_ConnectionList.RemoveAll();
  482. m_ConnectionUpdate.Clear();
  483. // fill render lists with visible objects
  484. AddToRenderLists( pWorld );
  485. // Add the connections too
  486. m_ConnectionUpdate.Push( pWorld );
  487. PopulateConnectionList();
  488. // Make sure we have a timer running to drive error animations
  489. SetTimer( TIMER_CONNECTIONUPDATE, TIMER_BLINK_INTERVAL, NULL);
  490. }
  491. // Assume we are blinking, unless something is selected.
  492. bool bAnySelected = FALSE;
  493. CUtlVector<CMapClass *> selectedObjects;
  494. CUtlVector<CMapClass *> helperObjects;
  495. CUtlVector<CMapClass *> unselectedObjects;
  496. // Render normal (nonselected) objects first
  497. for (int i = 0; i < m_RenderList.Count(); i++)
  498. {
  499. CMapClass *pObject = m_RenderList[i];
  500. if ( pObject->IsSelected() )
  501. {
  502. bAnySelected = TRUE;
  503. // render later
  504. if ( pObject->GetToolObject( 0, false ) )
  505. {
  506. helperObjects.AddToTail( pObject );
  507. }
  508. else
  509. {
  510. selectedObjects.AddToTail( pObject );
  511. }
  512. }
  513. else
  514. {
  515. unselectedObjects.AddToTail( pObject );
  516. }
  517. }
  518. // Render unselected connections first
  519. RenderConnections(false, bAnySelected);
  520. // render unselected objects next, on top of the connections
  521. for ( int j = 0; j < unselectedObjects.Count(); j++ )
  522. {
  523. unselectedObjects[j]->RenderLogical( GetRender() );
  524. }
  525. if ( bAnySelected )
  526. {
  527. // Render selected objects in second batch, so they overdraw normal object
  528. for (int i = 0; i < selectedObjects.Count(); i++)
  529. {
  530. selectedObjects[i]->RenderLogical( GetRender() );
  531. }
  532. // Render selected connections on top of everything else
  533. RenderConnections(true, bAnySelected);
  534. }
  535. // render all tools
  536. CBaseTool *pCurTool = pDoc->GetTools()->GetActiveTool();
  537. int nToolCount = pDoc->GetTools()->GetToolCount();
  538. for (int i = 0; i < nToolCount; i++)
  539. {
  540. CBaseTool *pTool = pDoc->GetTools()->GetTool(i);
  541. if ((pTool != NULL) && (pTool != pCurTool))
  542. {
  543. pTool->RenderToolLogical( GetRender() );
  544. }
  545. }
  546. // render active tool over all other tools
  547. if ( pCurTool )
  548. {
  549. pCurTool->RenderToolLogical( GetRender() );
  550. }
  551. // render map helpers at last
  552. for (int i = 0; i < helperObjects.Count(); i++)
  553. {
  554. helperObjects[i]->RenderLogical( GetRender() );
  555. }
  556. GetRender()->EndRenderFrame();
  557. }
  558. //-----------------------------------------------------------------------------
  559. // convert client view space to map world coordinates (2D versions for convenience)
  560. //-----------------------------------------------------------------------------
  561. void CMapViewLogical::WorldToClient( Vector2D &ptClient, const Vector2D &vWorld )
  562. {
  563. Vector vWorld3D( vWorld.x, vWorld.y, 0.0f );
  564. CMapView2DBase::WorldToClient( ptClient, vWorld3D );
  565. }
  566. void CMapViewLogical::ClientToWorld( Vector2D &vWorld, const Vector2D &vClient )
  567. {
  568. Vector vWorld3D;
  569. CMapView2DBase::ClientToWorld( vWorld3D, vClient );
  570. vWorld.x = vWorld3D.x;
  571. vWorld.y = vWorld3D.y;
  572. }
  573. void CMapViewLogical::WorldToClient( Vector2D &ptClient, const Vector &vWorld )
  574. {
  575. CMapView2DBase::WorldToClient( ptClient, vWorld );
  576. }
  577. void CMapViewLogical::ClientToWorld( Vector &vWorld, const Vector2D &vClient )
  578. {
  579. CMapView2DBase::ClientToWorld( vWorld, vClient );
  580. }