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.

184 lines
4.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "hud.h"
  10. #include "hud_macros.h"
  11. #include "view.h"
  12. #include "iclientmode.h"
  13. #include <KeyValues.h>
  14. #include <vgui/ISurface.h>
  15. #include <vgui/ISystem.h>
  16. #include <vgui_controls/Panel.h>
  17. using namespace vgui;
  18. #include "hudelement.h"
  19. #include "hl1_hud_numbers.h"
  20. #include "convar.h"
  21. #define FADE_TIME 100
  22. #define MIN_ALPHA 100
  23. //-----------------------------------------------------------------------------
  24. // Purpose: Health panel
  25. //-----------------------------------------------------------------------------
  26. class CHudBattery : public CHudElement, public CHL1HudNumbers
  27. {
  28. DECLARE_CLASS_SIMPLE( CHudBattery, CHL1HudNumbers );
  29. public:
  30. CHudBattery( const char *pElementName );
  31. void Init( void );
  32. void Reset( void );
  33. void VidInit( void );
  34. void MsgFunc_Battery(bf_read &msg);
  35. private:
  36. void Paint( void );
  37. void ApplySchemeSettings(vgui::IScheme *pScheme);
  38. private:
  39. CHudTexture *icon_suit_empty;
  40. CHudTexture *icon_suit_full;
  41. int m_iBattery;
  42. float m_flFade;
  43. };
  44. DECLARE_HUDELEMENT( CHudBattery );
  45. DECLARE_HUD_MESSAGE( CHudBattery, Battery );
  46. //-----------------------------------------------------------------------------
  47. // Purpose: Constructor
  48. //-----------------------------------------------------------------------------
  49. CHudBattery::CHudBattery( const char *pElementName ) : CHudElement( pElementName ), BaseClass(NULL, "HudSuit")
  50. {
  51. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_NEEDSUIT );
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose:
  55. //-----------------------------------------------------------------------------
  56. void CHudBattery::Init()
  57. {
  58. HOOK_HUD_MESSAGE( CHudBattery, Battery );
  59. Reset();
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. //-----------------------------------------------------------------------------
  64. void CHudBattery::Reset()
  65. {
  66. m_iBattery = 0;
  67. m_flFade = 0;
  68. }
  69. void CHudBattery::VidInit()
  70. {
  71. Reset();
  72. BaseClass::VidInit();
  73. }
  74. void CHudBattery::Paint()
  75. {
  76. Color clrHealth;
  77. int a;
  78. int x;
  79. int y;
  80. BaseClass::Paint();
  81. if ( !icon_suit_empty )
  82. {
  83. icon_suit_empty = gHUD.GetIcon( "suit_empty" );
  84. }
  85. if ( !icon_suit_full )
  86. {
  87. icon_suit_full = gHUD.GetIcon( "suit_full" );
  88. }
  89. if ( !icon_suit_empty || !icon_suit_full )
  90. {
  91. return;
  92. }
  93. // Has health changed? Flash the health #
  94. if ( m_flFade )
  95. {
  96. if (m_flFade > FADE_TIME)
  97. m_flFade = FADE_TIME;
  98. m_flFade -= ( gpGlobals->frametime * 20 );
  99. if ( m_flFade <= 0 )
  100. {
  101. a = 128;
  102. m_flFade = 0;
  103. }
  104. else
  105. {
  106. // Fade the health number back to dim
  107. a = MIN_ALPHA + ( m_flFade / FADE_TIME ) * 128;
  108. }
  109. }
  110. else
  111. {
  112. a = MIN_ALPHA;
  113. }
  114. int r, g, b, nUnused;
  115. (gHUD.m_clrYellowish).GetColor( r, g, b, nUnused );
  116. clrHealth.SetColor( r, g, b, a );
  117. int nFontHeight = GetNumberFontHeight();
  118. int nHudElemWidth, nHudElemHeight;
  119. GetSize( nHudElemWidth, nHudElemHeight );
  120. int iOffset = icon_suit_empty->Height() / 6;
  121. x = nHudElemWidth / 5;
  122. y = nHudElemHeight - ( nFontHeight * 1.5 );
  123. icon_suit_empty->DrawSelf( x, y - iOffset, clrHealth );
  124. if ( m_iBattery > 0 )
  125. {
  126. int nSuitOffset = icon_suit_full->Height() * ((float)(100-(MIN(100,m_iBattery))) * 0.01); // battery can go from 0 to 100 so * 0.01 goes from 0 to 1
  127. icon_suit_full->DrawSelfCropped( x, y - iOffset + nSuitOffset, 0, nSuitOffset, icon_suit_full->Width(), icon_suit_full->Height() - nSuitOffset, clrHealth );
  128. }
  129. x += icon_suit_empty->Width();
  130. DrawHudNumber( x, y, m_iBattery, clrHealth );
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose:
  134. //-----------------------------------------------------------------------------
  135. void CHudBattery::MsgFunc_Battery( bf_read &msg )
  136. {
  137. int x = msg.ReadShort();
  138. if ( x != m_iBattery )
  139. {
  140. m_flFade = FADE_TIME;
  141. m_iBattery = x;
  142. }
  143. }
  144. void CHudBattery::ApplySchemeSettings(vgui::IScheme *pScheme)
  145. {
  146. BaseClass::ApplySchemeSettings(pScheme);
  147. SetPaintBackgroundEnabled(false);
  148. }