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.

171 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: To display the player's health with the use of one graphic over another. A cross in this case
  4. // Currently this is only used on the freeze cam panel
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "cs_hud_freezepanel.h"
  10. #include "vgui_controls/AnimationController.h"
  11. #include "iclientmode.h"
  12. #include "c_cs_player.h"
  13. #include "c_cs_playerresource.h"
  14. #include <vgui_controls/Label.h>
  15. #include <vgui/ILocalize.h>
  16. #include <vgui/ISurface.h>
  17. #include "cs_gamerules.h"
  18. DECLARE_BUILD_FACTORY( CCSHudPlayerHealth );
  19. //-----------------------------------------------------------------------------
  20. // Purpose:
  21. //-----------------------------------------------------------------------------
  22. CCSHudPlayerHealth::CCSHudPlayerHealth( Panel *parent, const char *name ) : EditablePanel( parent, name )
  23. {
  24. m_pHealthImage = new CCSHealthPanel( this, "PlayerStatusHealthImage" );
  25. m_pHealthImageBG = new ImagePanel( this, "PlayerStatusHealthImageBG" );
  26. m_flNextThink = 0.0f;
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Purpose:
  30. //-----------------------------------------------------------------------------
  31. void CCSHudPlayerHealth::Reset()
  32. {
  33. //m_flNextThink = gpGlobals->curtime + 0.05f;
  34. m_nHealth = -1;
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose:
  38. //-----------------------------------------------------------------------------
  39. void CCSHudPlayerHealth::ApplySchemeSettings( IScheme *pScheme )
  40. {
  41. // load control settings...
  42. LoadControlSettings( GetResFilename() );
  43. m_flNextThink = 0.0f;
  44. BaseClass::ApplySchemeSettings( pScheme );
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose:
  48. //-----------------------------------------------------------------------------
  49. void CCSHudPlayerHealth::SetHealth( int iNewHealth, int iMaxHealth, int iMaxBuffedHealth )
  50. {
  51. int nPrevHealth = m_nHealth;
  52. // set our health
  53. m_nHealth = iNewHealth;
  54. m_nMaxHealth = iMaxHealth;
  55. m_pHealthImage->SetHealth( (float)(m_nHealth) / (float)(m_nMaxHealth) );
  56. if ( m_pHealthImage )
  57. {
  58. m_pHealthImage->SetFgColor( Color( 255, 255, 255, 255 ) );
  59. }
  60. if ( m_nHealth <= 0 )
  61. {
  62. if ( m_pHealthImageBG->IsVisible() )
  63. {
  64. m_pHealthImageBG->SetVisible( false );
  65. }
  66. }
  67. else
  68. {
  69. if ( !m_pHealthImageBG->IsVisible() )
  70. {
  71. m_pHealthImageBG->SetVisible( true );
  72. }
  73. }
  74. // set our health display value
  75. if ( nPrevHealth != m_nHealth )
  76. {
  77. if ( m_nHealth > 0 )
  78. {
  79. SetDialogVariable( "Health", m_nHealth );
  80. }
  81. else
  82. {
  83. SetDialogVariable( "Health", "" );
  84. }
  85. }
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose:
  89. //-----------------------------------------------------------------------------
  90. CCSHealthPanel::CCSHealthPanel( Panel *parent, const char *name ) : vgui::Panel( parent, name )
  91. {
  92. m_flHealth = 1.0f;
  93. m_iMaterialIndex = surface()->DrawGetTextureId( "hud/health_color" );
  94. if ( m_iMaterialIndex == -1 ) // we didn't find it, so create a new one
  95. {
  96. m_iMaterialIndex = surface()->CreateNewTextureID();
  97. }
  98. surface()->DrawSetTextureFile( m_iMaterialIndex, "hud/health_color", true, false );
  99. m_iDeadMaterialIndex = surface()->DrawGetTextureId( "hud/health_dead" );
  100. if ( m_iDeadMaterialIndex == -1 ) // we didn't find it, so create a new one
  101. {
  102. m_iDeadMaterialIndex = surface()->CreateNewTextureID();
  103. }
  104. surface()->DrawSetTextureFile( m_iDeadMaterialIndex, "hud/health_dead", true, false );
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose:
  108. //-----------------------------------------------------------------------------
  109. void CCSHealthPanel::Paint()
  110. {
  111. BaseClass::Paint();
  112. int x, y, w, h;
  113. GetBounds( x, y, w, h );
  114. Vertex_t vert[4];
  115. float uv1 = 0.0f;
  116. float uv2 = 1.0f;
  117. int xpos = 0, ypos = 0;
  118. if ( m_flHealth <= 0 )
  119. {
  120. // Draw the dead material
  121. surface()->DrawSetTexture( m_iDeadMaterialIndex );
  122. vert[0].Init( Vector2D( xpos, ypos ), Vector2D( uv1, uv1 ) );
  123. vert[1].Init( Vector2D( xpos + w, ypos ), Vector2D( uv2, uv1 ) );
  124. vert[2].Init( Vector2D( xpos + w, ypos + h ), Vector2D( uv2, uv2 ) );
  125. vert[3].Init( Vector2D( xpos, ypos + h ), Vector2D( uv1, uv2 ) );
  126. surface()->DrawSetColor( Color(255,255,255,255) );
  127. }
  128. else
  129. {
  130. float flDamageY = h * ( 1.0f - m_flHealth );
  131. // blend in the red "damage" part
  132. surface()->DrawSetTexture( m_iMaterialIndex );
  133. Vector2D uv11( uv1, uv2 - m_flHealth );
  134. Vector2D uv21( uv2, uv2 - m_flHealth );
  135. Vector2D uv22( uv2, uv2 );
  136. Vector2D uv12( uv1, uv2 );
  137. vert[0].Init( Vector2D( xpos, flDamageY ), uv11 );
  138. vert[1].Init( Vector2D( xpos + w, flDamageY ), uv21 );
  139. vert[2].Init( Vector2D( xpos + w, ypos + h ), uv22 );
  140. vert[3].Init( Vector2D( xpos, ypos + h ), uv12 );
  141. surface()->DrawSetColor( GetFgColor() );
  142. }
  143. surface()->DrawTexturedPolygon( 4, vert );
  144. }