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.

254 lines
7.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //
  9. #include "cbase.h"
  10. #include "view.h"
  11. #include "iclientmode.h"
  12. #include <KeyValues.h>
  13. #include <vgui/ISurface.h>
  14. #include <vgui/ISystem.h>
  15. #include <vgui_controls/AnimationController.h>
  16. #include <vgui_controls/EditablePanel.h>
  17. #include <vgui_controls/ImagePanel.h>
  18. #include "convar.h"
  19. #include "c_dod_team.h"
  20. #include "c_dod_playerresource.h"
  21. #include "c_dod_player.h"
  22. #include "dod_hud_playerstatus_health.h"
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. //-----------------------------------------------------------------------------
  26. CDoDHudHealthBar::CDoDHudHealthBar( vgui::Panel *parent, const char *name ) : vgui::ImagePanel( parent, name )
  27. {
  28. m_flPercentage = 1.0f;
  29. m_iMaterialIndex = vgui::surface()->DrawGetTextureId( "vgui/white" );
  30. if ( m_iMaterialIndex == -1 ) // we didn't find it, so create a new one
  31. {
  32. m_iMaterialIndex = vgui::surface()->CreateNewTextureID();
  33. }
  34. vgui::surface()->DrawSetTextureFile( m_iMaterialIndex, "vgui/white", true, false );
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose:
  38. //-----------------------------------------------------------------------------
  39. void CDoDHudHealthBar::OnThink()
  40. {
  41. BaseClass::OnThink();
  42. C_DODPlayer *pPlayer = GetHealthDelegatePlayer();
  43. if ( pPlayer )
  44. {
  45. // m_nHealth >= 0
  46. int nHealth = MAX( pPlayer->GetHealth(), 0 );
  47. m_flPercentage = nHealth / 100.0f;
  48. }
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose:
  52. //-----------------------------------------------------------------------------
  53. void CDoDHudHealthBar::ApplySchemeSettings( vgui::IScheme *pScheme )
  54. {
  55. BaseClass::ApplySchemeSettings( pScheme );
  56. m_clrHealthHigh = pScheme->GetColor( "HudHealthGreen", GetFgColor() );
  57. m_clrHealthMed = pScheme->GetColor( "HudHealthYellow", GetFgColor() );
  58. m_clrHealthLow = pScheme->GetColor( "HudHealthRed", GetFgColor() );
  59. m_clrBackground = pScheme->GetColor( "HudHealthBG", GetBgColor() );
  60. m_clrBorder = pScheme->GetColor( "HudHealthBorder", GetBgColor() );
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose:
  64. //-----------------------------------------------------------------------------
  65. void CDoDHudHealthBar::Paint( void )
  66. {
  67. BaseClass::Paint();
  68. int x, y, w, h;
  69. GetBounds( x, y, w, h );
  70. int xpos = 0, ypos = 0;
  71. float flDamageY = h * ( 1.0f - m_flPercentage );
  72. Color *pclrHealth;
  73. if ( m_flPercentage > m_flFirstWarningLevel )
  74. {
  75. pclrHealth = &m_clrHealthHigh;
  76. }
  77. else if ( m_flPercentage > m_flSecondWarningLevel )
  78. {
  79. pclrHealth = &m_clrHealthMed;
  80. }
  81. else
  82. {
  83. pclrHealth = &m_clrHealthLow;
  84. }
  85. // blend in the red "damage" part
  86. float uv1 = 0.0f;
  87. float uv2 = 1.0f;
  88. vgui::surface()->DrawSetTexture( m_iMaterialIndex );
  89. Vector2D uv11( uv1, uv1 );
  90. Vector2D uv21( uv2, uv1 );
  91. Vector2D uv22( uv2, uv2 );
  92. Vector2D uv12( uv1, uv2 );
  93. vgui::Vertex_t vert[4];
  94. // background
  95. vert[0].Init( Vector2D( xpos, ypos ), uv11 );
  96. vert[1].Init( Vector2D( xpos + w, ypos ), uv21 );
  97. vert[2].Init( Vector2D( xpos + w, ypos + h ), uv22 );
  98. vert[3].Init( Vector2D( xpos, ypos + h ), uv12 );
  99. if ( m_flPercentage <= 0.01 )
  100. {
  101. vgui::surface()->DrawSetColor( m_clrHealthLow );
  102. }
  103. else
  104. {
  105. vgui::surface()->DrawSetColor( m_clrBackground );
  106. }
  107. vgui::surface()->DrawTexturedPolygon( 4, vert );
  108. // damage part
  109. vert[0].Init( Vector2D( xpos, flDamageY ), uv11 );
  110. vert[1].Init( Vector2D( xpos + w, flDamageY ), uv21 );
  111. vert[2].Init( Vector2D( xpos + w, ypos + h ), uv22 );
  112. vert[3].Init( Vector2D( xpos, ypos + h ), uv12 );
  113. vgui::surface()->DrawSetColor( *pclrHealth );
  114. vgui::surface()->DrawTexturedPolygon( 4, vert );
  115. // outline
  116. vert[0].Init( Vector2D( xpos, ypos ), uv11 );
  117. vert[1].Init( Vector2D( xpos + w - 1, ypos ), uv21 );
  118. vert[2].Init( Vector2D( xpos + w - 1, ypos + h - 1 ), uv22 );
  119. vert[3].Init( Vector2D( xpos, ypos + h - 1 ), uv12 );
  120. vgui::surface()->DrawSetColor( m_clrBorder );
  121. vgui::surface()->DrawTexturedPolyLine( vert, 4 );
  122. }
  123. // Show the health / class for a player other than the local player
  124. void CDoDHudHealthBar::SetHealthDelegatePlayer( C_DODPlayer *pPlayer )
  125. {
  126. m_hHealthDelegatePlayer = pPlayer;
  127. }
  128. C_DODPlayer *CDoDHudHealthBar::GetHealthDelegatePlayer( void )
  129. {
  130. if ( m_hHealthDelegatePlayer.Get() )
  131. {
  132. C_DODPlayer *pPlayer = dynamic_cast<C_DODPlayer *>( m_hHealthDelegatePlayer.Get() );
  133. if ( pPlayer )
  134. return pPlayer;
  135. }
  136. return C_DODPlayer::GetLocalDODPlayer();
  137. }
  138. DECLARE_BUILD_FACTORY( CDoDHudHealth );
  139. //-----------------------------------------------------------------------------
  140. // Purpose: Constructor
  141. //-----------------------------------------------------------------------------
  142. CDoDHudHealth::CDoDHudHealth( vgui::Panel *parent, const char *name ) : vgui::EditablePanel( parent, name )
  143. {
  144. SetProportional( true );
  145. m_pHealthBar = new CDoDHudHealthBar( this, "HealthBar" );
  146. m_pClassImageBG = new vgui::ImagePanel( this, "HealthClassImageBG" );
  147. m_pClassImage = new vgui::ImagePanel( this, "HealthClassImage" );
  148. m_nPrevClass = PLAYERCLASS_UNDEFINED;
  149. m_nPrevTeam = TEAM_INVALID;
  150. // load control settings...
  151. LoadControlSettings( "resource/UI/HudPlayerStatusHealth.res" );
  152. m_hHealthDelegatePlayer = NULL;
  153. }
  154. void CDoDHudHealth::OnScreenSizeChanged(int iOldWide, int iOldTall)
  155. {
  156. LoadControlSettings( "resource/UI/HudPlayerStatusHealth.res" );
  157. }
  158. //-----------------------------------------------------------------------------
  159. // Purpose:
  160. //-----------------------------------------------------------------------------
  161. void CDoDHudHealth::OnThink()
  162. {
  163. BaseClass::OnThink();
  164. C_DODPlayer *pPlayer = GetHealthDelegatePlayer();
  165. if ( pPlayer )
  166. {
  167. int nTeam = pPlayer->GetTeamNumber();
  168. if ( nTeam == TEAM_ALLIES || nTeam == TEAM_AXIS )
  169. {
  170. C_DODTeam *pTeam = dynamic_cast<C_DODTeam *>( GetGlobalTeam( nTeam ) );
  171. C_DOD_PlayerResource *dod_PR = dynamic_cast<C_DOD_PlayerResource *>( g_PR );
  172. int nClass = dod_PR->GetPlayerClass( pPlayer->entindex() );
  173. if ( nClass != PLAYERCLASS_UNDEFINED )
  174. {
  175. if ( ( nClass != m_nPrevClass ) ||
  176. ( nTeam != TEAM_INVALID && ( nTeam == TEAM_AXIS || nTeam == TEAM_ALLIES ) && nTeam != m_nPrevTeam ) )
  177. {
  178. m_nPrevClass = nClass;
  179. m_nPrevTeam = nTeam;
  180. if ( m_pClassImage )
  181. {
  182. m_pClassImage->SetImage( ( pTeam->GetPlayerClassInfo( nClass ) ).m_szClassHealthImage );
  183. }
  184. if ( m_pClassImageBG )
  185. {
  186. m_pClassImageBG->SetImage( ( pTeam->GetPlayerClassInfo( nClass ) ).m_szClassHealthImageBG );
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. // Show the health / class for a player other than the local player
  194. void CDoDHudHealth::SetHealthDelegatePlayer( C_DODPlayer *pPlayer )
  195. {
  196. m_hHealthDelegatePlayer = pPlayer;
  197. m_pHealthBar->SetHealthDelegatePlayer( pPlayer );
  198. }
  199. C_DODPlayer *CDoDHudHealth::GetHealthDelegatePlayer( void )
  200. {
  201. if ( m_hHealthDelegatePlayer.Get() )
  202. {
  203. C_DODPlayer *pPlayer = dynamic_cast<C_DODPlayer *>( m_hHealthDelegatePlayer.Get() );
  204. if ( pPlayer )
  205. return pPlayer;
  206. }
  207. return C_DODPlayer::GetLocalDODPlayer();
  208. }