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.

188 lines
4.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "idebugoverlaypanel.h"
  10. #include "overlaytext.h"
  11. #include <vgui/IVGui.h>
  12. #include "engine/ivdebugoverlay.h"
  13. #include "VGuiMatSurface/IMatSystemSurface.h"
  14. #include <vgui_controls/Panel.h>
  15. #include <vgui_controls/Controls.h>
  16. #include <vgui/IScheme.h>
  17. #include "ienginevgui.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. class CDebugOverlay : public vgui::Panel
  21. {
  22. typedef vgui::Panel BaseClass;
  23. public:
  24. CDebugOverlay( vgui::VPANEL parent );
  25. virtual ~CDebugOverlay( void );
  26. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  27. virtual void Paint();
  28. virtual void OnTick( void );
  29. virtual bool ShouldDraw( void );
  30. private:
  31. vgui::HFont m_hFont;
  32. };
  33. //-----------------------------------------------------------------------------
  34. // Purpose: Instances the overlay object
  35. // Input : *parent -
  36. //-----------------------------------------------------------------------------
  37. CDebugOverlay::CDebugOverlay( vgui::VPANEL parent ) :
  38. BaseClass( NULL, "CDebugOverlay" )
  39. {
  40. int w, h;
  41. vgui::surface()->GetScreenSize( w, h );
  42. SetParent( parent );
  43. SetSize( w, h );
  44. SetPos( 0, 0 );
  45. SetVisible( false );
  46. SetCursor( null );
  47. m_hFont = 0;
  48. SetFgColor( Color( 0, 0, 0, 0 ) );
  49. SetPaintBackgroundEnabled( false );
  50. // set the scheme before any child control is created
  51. vgui::HScheme scheme = vgui::scheme()->LoadSchemeFromFileEx( enginevgui->GetPanel( PANEL_CLIENTDLL_TOOLS ), "resource/ClientScheme.res", "Client");
  52. SetScheme( scheme );
  53. vgui::ivgui()->AddTickSignal( GetVPanel(), 250 );
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Purpose:
  57. //-----------------------------------------------------------------------------
  58. CDebugOverlay::~CDebugOverlay( void )
  59. {
  60. }
  61. void CDebugOverlay::ApplySchemeSettings(vgui::IScheme *pScheme)
  62. {
  63. BaseClass::ApplySchemeSettings(pScheme);
  64. // Use a large font
  65. // m_hFont = pScheme->GetFont( "Default" );
  66. m_hFont = pScheme->GetFont( "DebugOverlay" );
  67. assert( m_hFont );
  68. int w, h;
  69. vgui::surface()->GetScreenSize( w, h );
  70. SetSize( w, h );
  71. SetPos( 0, 0 );
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose:
  75. //-----------------------------------------------------------------------------
  76. void CDebugOverlay::OnTick( void )
  77. {
  78. bool bVisible = ShouldDraw();
  79. if ( IsVisible() != bVisible )
  80. {
  81. SetVisible( bVisible );
  82. }
  83. }
  84. bool CDebugOverlay::ShouldDraw( void )
  85. {
  86. if ( debugoverlay && debugoverlay->GetFirst() )
  87. return true;
  88. return false;
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Purpose: Paints the 2D overlay items to the screen
  92. //-----------------------------------------------------------------------------
  93. void CDebugOverlay::Paint()
  94. {
  95. if (!debugoverlay)
  96. return;
  97. OverlayText_t* pCurrText = debugoverlay->GetFirst();
  98. while (pCurrText)
  99. {
  100. if ( pCurrText->text != NULL )
  101. {
  102. // --------------
  103. // Draw the text
  104. // --------------
  105. int r = pCurrText->r;
  106. int g = pCurrText->g;
  107. int b = pCurrText->b;
  108. int a = pCurrText->a;
  109. Vector screenPos;
  110. if (pCurrText->bUseOrigin)
  111. {
  112. if (!debugoverlay->ScreenPosition( pCurrText->origin, screenPos ))
  113. {
  114. float xPos = screenPos[0];
  115. float yPos = screenPos[1]+ (pCurrText->lineOffset*13); // Line spacing;
  116. g_pMatSystemSurface->DrawColoredText( m_hFont, xPos, yPos, r, g, b, a, "%s", pCurrText->text );
  117. }
  118. }
  119. else
  120. {
  121. if (!debugoverlay->ScreenPosition( pCurrText->flXPos,pCurrText->flYPos, screenPos ))
  122. {
  123. float xPos = screenPos[0];
  124. float yPos = screenPos[1]+ (pCurrText->lineOffset*13); // Line spacing;
  125. g_pMatSystemSurface->DrawColoredText( m_hFont, xPos, yPos, r, g, b, a, "%s", pCurrText->text );
  126. }
  127. }
  128. }
  129. pCurrText = debugoverlay->GetNext( pCurrText );
  130. }
  131. debugoverlay->ClearDeadOverlays();
  132. }
  133. class CDebugOverlayPanel : public IDebugOverlayPanel
  134. {
  135. private:
  136. CDebugOverlay *debugOverlayPanel;
  137. public:
  138. CDebugOverlayPanel( void )
  139. {
  140. debugOverlayPanel = NULL;
  141. }
  142. void Create( vgui::VPANEL parent )
  143. {
  144. debugOverlayPanel = new CDebugOverlay( parent );
  145. }
  146. void Destroy( void )
  147. {
  148. if ( debugOverlayPanel )
  149. {
  150. debugOverlayPanel->SetParent( (vgui::Panel *)NULL );
  151. debugOverlayPanel->MarkForDeletion();
  152. debugOverlayPanel = NULL;
  153. }
  154. }
  155. };
  156. static CDebugOverlayPanel g_DebugOverlay;
  157. IDebugOverlayPanel *debugoverlaypanel = ( IDebugOverlayPanel * )&g_DebugOverlay;
  158. void DebugDrawLine( const Vector& vecAbsStart, const Vector& vecAbsEnd, int r, int g, int b, bool test, float duration )
  159. {
  160. if ( debugoverlay )
  161. {
  162. debugoverlay->AddLineOverlay( vecAbsStart + Vector( 0,0,0.1), vecAbsEnd + Vector( 0,0,0.1), r,g,b, test, duration );
  163. }
  164. }