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.

162 lines
5.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hudelement.h"
  8. #include "hud_numericdisplay.h"
  9. #include <vgui_controls/Panel.h>
  10. #include "hud.h"
  11. #include "hud_suitpower.h"
  12. #include "hud_macros.h"
  13. #include "iclientmode.h"
  14. #include <vgui_controls/AnimationController.h>
  15. #include <vgui/ISurface.h>
  16. #include "c_basehlplayer.h"
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Shows the flashlight icon
  21. //-----------------------------------------------------------------------------
  22. class CHudFlashlight : public CHudElement, public vgui::Panel
  23. {
  24. DECLARE_CLASS_SIMPLE( CHudFlashlight, vgui::Panel );
  25. public:
  26. CHudFlashlight( const char *pElementName );
  27. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  28. protected:
  29. virtual void Paint();
  30. private:
  31. void SetFlashlightState( bool flashlightOn );
  32. void Reset( void );
  33. bool m_bFlashlightOn;
  34. CPanelAnimationVar( vgui::HFont, m_hFont, "Font", "WeaponIconsSmall" );
  35. CPanelAnimationVarAliasType( float, m_IconX, "icon_xpos", "4", "proportional_float" );
  36. CPanelAnimationVarAliasType( float, m_IconY, "icon_ypos", "4", "proportional_float" );
  37. CPanelAnimationVarAliasType( float, m_flBarInsetX, "BarInsetX", "2", "proportional_float" );
  38. CPanelAnimationVarAliasType( float, m_flBarInsetY, "BarInsetY", "18", "proportional_float" );
  39. CPanelAnimationVarAliasType( float, m_flBarWidth, "BarWidth", "28", "proportional_float" );
  40. CPanelAnimationVarAliasType( float, m_flBarHeight, "BarHeight", "2", "proportional_float" );
  41. CPanelAnimationVarAliasType( float, m_flBarChunkWidth, "BarChunkWidth", "2", "proportional_float" );
  42. CPanelAnimationVarAliasType( float, m_flBarChunkGap, "BarChunkGap", "2", "proportional_float" );
  43. };
  44. using namespace vgui;
  45. #ifdef HL2_EPISODIC
  46. DECLARE_HUDELEMENT( CHudFlashlight );
  47. #endif // HL2_EPISODIC
  48. //-----------------------------------------------------------------------------
  49. // Purpose: Constructor
  50. //-----------------------------------------------------------------------------
  51. CHudFlashlight::CHudFlashlight( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudFlashlight" )
  52. {
  53. vgui::Panel *pParent = g_pClientMode->GetViewport();
  54. SetParent( pParent );
  55. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Purpose:
  59. // Input : *pScheme -
  60. //-----------------------------------------------------------------------------
  61. void CHudFlashlight::ApplySchemeSettings( vgui::IScheme *pScheme )
  62. {
  63. BaseClass::ApplySchemeSettings(pScheme);
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Purpose: Start with our background off
  67. //-----------------------------------------------------------------------------
  68. void CHudFlashlight::Reset( void )
  69. {
  70. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "SuitFlashlightOn" );
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose: data accessor
  74. //-----------------------------------------------------------------------------
  75. void CHudFlashlight::SetFlashlightState( bool flashlightOn )
  76. {
  77. if ( m_bFlashlightOn == flashlightOn )
  78. return;
  79. m_bFlashlightOn = flashlightOn;
  80. }
  81. #define WCHAR_FLASHLIGHT_ON 169
  82. #define WCHAR_FLASHLIGHT_OFF 174
  83. //-----------------------------------------------------------------------------
  84. // Purpose: draws the flashlight icon
  85. //-----------------------------------------------------------------------------
  86. void CHudFlashlight::Paint()
  87. {
  88. #ifdef HL2_EPISODIC
  89. C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
  90. if ( !pPlayer )
  91. return;
  92. // Only paint if we're using the new flashlight code
  93. if ( pPlayer->m_HL2Local.m_flFlashBattery < 0.0f )
  94. {
  95. SetPaintBackgroundEnabled( false );
  96. return;
  97. }
  98. bool bIsOn = pPlayer->IsEffectActive( EF_DIMLIGHT );
  99. SetFlashlightState( bIsOn );
  100. // get bar chunks
  101. int chunkCount = m_flBarWidth / (m_flBarChunkWidth + m_flBarChunkGap);
  102. int enabledChunks = (int)((float)chunkCount * (pPlayer->m_HL2Local.m_flFlashBattery * 1.0f/100.0f) + 0.5f );
  103. Color clrFlashlight;
  104. clrFlashlight = ( enabledChunks < ( chunkCount / 4 ) ) ? gHUD.m_clrCaution : gHUD.m_clrNormal;
  105. clrFlashlight[3] = ( bIsOn ) ? 255: 32;
  106. // Pick the right character given our current state
  107. wchar_t pState = ( bIsOn ) ? WCHAR_FLASHLIGHT_ON : WCHAR_FLASHLIGHT_OFF;
  108. surface()->DrawSetTextFont( m_hFont );
  109. surface()->DrawSetTextColor( clrFlashlight );
  110. surface()->DrawSetTextPos( m_IconX, m_IconY );
  111. surface()->DrawUnicodeChar( pState );
  112. // Don't draw the progress bar is we're fully charged
  113. if ( bIsOn == false && chunkCount == enabledChunks )
  114. return;
  115. // draw the suit power bar
  116. surface()->DrawSetColor( clrFlashlight );
  117. int xpos = m_flBarInsetX, ypos = m_flBarInsetY;
  118. for (int i = 0; i < enabledChunks; i++)
  119. {
  120. surface()->DrawFilledRect( xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight );
  121. xpos += (m_flBarChunkWidth + m_flBarChunkGap);
  122. }
  123. // Be even less transparent than we already are
  124. clrFlashlight[3] = clrFlashlight[3] / 8;
  125. // draw the exhausted portion of the bar.
  126. surface()->DrawSetColor( clrFlashlight );
  127. for (int i = enabledChunks; i < chunkCount; i++)
  128. {
  129. surface()->DrawFilledRect( xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight );
  130. xpos += (m_flBarChunkWidth + m_flBarChunkGap);
  131. }
  132. #endif // HL2_EPISODIC
  133. }