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.

186 lines
5.6 KiB

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