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.

181 lines
5.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: This is a panel which is rendered image on top of an entity
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "vgui_imagehealthpanel.h"
  10. #include "commanderoverlay.h"
  11. #include <KeyValues.h>
  12. #include "mapdata.h"
  13. bool IsLocalPlayerInTactical( void );
  14. //-----------------------------------------------------------------------------
  15. // Class factory
  16. //-----------------------------------------------------------------------------
  17. DECLARE_OVERLAY_FACTORY( CEntityImageHealthPanel, "image_health" );
  18. //-----------------------------------------------------------------------------
  19. // Constructor
  20. //-----------------------------------------------------------------------------
  21. CEntityImageHealthPanel::CEntityImageHealthPanel( vgui::Panel *parent, const char *panelName )
  22. : BaseClass( parent, "CEntityImageHealthPanel" )
  23. {
  24. SetPaintBackgroundEnabled( false );
  25. }
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. //-----------------------------------------------------------------------------
  29. CEntityImageHealthPanel::~CEntityImageHealthPanel()
  30. {
  31. if ( m_pImagePanel )
  32. {
  33. delete m_pImagePanel;
  34. m_pImagePanel = NULL;
  35. }
  36. }
  37. //-----------------------------------------------------------------------------
  38. // initialization
  39. //-----------------------------------------------------------------------------
  40. bool CEntityImageHealthPanel::Init( KeyValues* pInitData, C_BaseEntity* pEntity )
  41. {
  42. if ( !pInitData )
  43. return false;
  44. if ( !BaseClass::Init( pInitData, pEntity ) )
  45. return false;
  46. m_CommanderHealthBar = NULL;
  47. m_NormalHealthBar = NULL;
  48. m_ResourceLevelBar = NULL;
  49. m_pImagePanel = NULL;
  50. // Health bar when we're in commander view
  51. KeyValues *pHealth = pInitData->FindKey("health_commander");
  52. if ( pHealth )
  53. {
  54. m_CommanderHealthBar = new CHealthBarPanel();
  55. if (!m_CommanderHealthBar->Init( pHealth ))
  56. return false;
  57. m_CommanderHealthBar->SetParent( this );
  58. }
  59. // Health bar when we're in normal view
  60. pHealth = pInitData->FindKey("health_normal");
  61. if ( pHealth )
  62. {
  63. m_NormalHealthBar = new CHealthBarPanel();
  64. if (!m_NormalHealthBar->Init( pHealth ))
  65. return false;
  66. m_NormalHealthBar->SetParent( this );
  67. }
  68. // Resource bar for collectors
  69. KeyValues *pResources = pInitData->FindKey("resource_level");
  70. if ( pResources )
  71. {
  72. m_ResourceLevelBar = new CHealthBarPanel();
  73. if (!m_ResourceLevelBar->Init( pResources ))
  74. return false;
  75. m_ResourceLevelBar->SetParent( this );
  76. }
  77. KeyValues *pImage = pInitData->FindKey("Image");
  78. if ( pImage )
  79. {
  80. m_pImagePanel = new CEntityTeamImagePanel( this, "CEntityTeamImagePanel" );
  81. if ( !m_pImagePanel->Init( pImage, GetEntity() ) )
  82. return false;
  83. }
  84. return true;
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Should we draw?.
  88. //-----------------------------------------------------------------------------
  89. bool CEntityImageHealthPanel::ShouldDraw( void )
  90. {
  91. // Always draw health
  92. return true;
  93. }
  94. //-----------------------------------------------------------------------------
  95. // called when we're ticked...
  96. //-----------------------------------------------------------------------------
  97. void CEntityImageHealthPanel::OnTick()
  98. {
  99. // tick the entity panel
  100. BaseClass::OnTick();
  101. C_BaseEntity* pBaseEntity = GetEntity();
  102. if (!pBaseEntity)
  103. return;
  104. // Don't draw if I'm not visible in the tactical map
  105. if ( MapData().IsEntityVisibleToTactical( pBaseEntity ) == false )
  106. return;
  107. if ( m_CommanderHealthBar )
  108. m_CommanderHealthBar->SetHealth( (float)pBaseEntity->GetHealth() / (float)pBaseEntity->GetMaxHealth() );
  109. if ( m_NormalHealthBar )
  110. m_NormalHealthBar->SetHealth( (float)pBaseEntity->GetHealth() / (float)pBaseEntity->GetMaxHealth() );
  111. // Hide the health bar we don't want to see
  112. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  113. if ( pPlayer && (pBaseEntity->GetTeamNumber() != pPlayer->GetTeamNumber()) )
  114. {
  115. if ( m_CommanderHealthBar )
  116. m_CommanderHealthBar->SetVisible( false );
  117. if ( m_NormalHealthBar )
  118. m_NormalHealthBar->SetVisible( false );
  119. if ( m_ResourceLevelBar )
  120. m_ResourceLevelBar->SetVisible( false );
  121. if ( m_pImagePanel )
  122. m_pImagePanel->SetVisible( false );
  123. }
  124. else if ( IsLocalPlayerInTactical() )
  125. {
  126. if ( m_CommanderHealthBar )
  127. m_CommanderHealthBar->SetVisible( true );
  128. if ( m_NormalHealthBar )
  129. m_NormalHealthBar->SetVisible( false );
  130. if ( m_ResourceLevelBar )
  131. m_ResourceLevelBar->SetVisible( true );
  132. if ( m_pImagePanel )
  133. m_pImagePanel->SetVisible( true );
  134. }
  135. else
  136. {
  137. if ( m_CommanderHealthBar )
  138. m_CommanderHealthBar->SetVisible( false );
  139. if ( m_NormalHealthBar )
  140. m_NormalHealthBar->SetVisible( true );
  141. if ( m_ResourceLevelBar )
  142. m_ResourceLevelBar->SetVisible( true );
  143. if ( m_pImagePanel )
  144. m_pImagePanel->SetVisible( false );
  145. }
  146. }
  147. //-----------------------------------------------------------------------------
  148. // Purpose: Compute the size of the panel based upon the commander's zoom level
  149. //-----------------------------------------------------------------------------
  150. void CEntityImageHealthPanel::ComputeAndSetSize( void )
  151. {
  152. BaseClass::ComputeAndSetSize();
  153. // Now update the bars
  154. if ( m_CommanderHealthBar )
  155. m_CommanderHealthBar->SetSize( GetWide(), m_CommanderHealthBar->GetTall() );
  156. if ( m_NormalHealthBar )
  157. m_NormalHealthBar->SetSize( GetWide(), m_CommanderHealthBar->GetTall() );
  158. if ( m_ResourceLevelBar )
  159. m_ResourceLevelBar->SetSize( GetWide(), m_CommanderHealthBar->GetTall() );
  160. }