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.

132 lines
3.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hud.h"
  8. #include "hudelement.h"
  9. #include "hud_macros.h"
  10. #include "iclientmode.h"
  11. #include "hl1_c_player.h"
  12. #include <vgui/ISurface.h>
  13. #include <vgui_controls/Panel.h>
  14. #define MIN_ALPHA 100
  15. class CHudFlashlight : public CHudElement, public vgui::Panel
  16. {
  17. DECLARE_CLASS_SIMPLE( CHudFlashlight, vgui::Panel );
  18. public:
  19. CHudFlashlight( const char *pElementName );
  20. private:
  21. void Paint( void );
  22. void ApplySchemeSettings(vgui::IScheme *pScheme);
  23. private:
  24. CHudTexture *icon_flash_empty;
  25. CHudTexture *icon_flash_full;
  26. CHudTexture *icon_flash_beam;
  27. Color m_clrReddish;
  28. };
  29. DECLARE_HUDELEMENT( CHudFlashlight );
  30. //-----------------------------------------------------------------------------
  31. // Purpose: Constructor
  32. //-----------------------------------------------------------------------------
  33. CHudFlashlight::CHudFlashlight( const char *pElementName ) : CHudElement( pElementName ), BaseClass(NULL, "HudFlashlight")
  34. {
  35. vgui::Panel *pParent = g_pClientMode->GetViewport();
  36. SetParent( pParent );
  37. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
  38. }
  39. void CHudFlashlight::Paint( void )
  40. {
  41. int r, g, b, a, nUnused;
  42. int x, y;
  43. bool bIsOn;
  44. Color clrFlash;
  45. C_HL1_Player *pPlayer = ToHL1Player( C_HL1_Player::GetLocalPlayer() );
  46. if ( !pPlayer )
  47. return;
  48. if ( !icon_flash_empty )
  49. {
  50. icon_flash_empty = gHUD.GetIcon( "flash_empty" );
  51. }
  52. if ( !icon_flash_full )
  53. {
  54. icon_flash_full = gHUD.GetIcon( "flash_full" );
  55. }
  56. if ( !icon_flash_beam )
  57. {
  58. icon_flash_beam = gHUD.GetIcon( "flash_beam" );
  59. }
  60. if ( !icon_flash_empty || !icon_flash_full || !icon_flash_beam )
  61. {
  62. return;
  63. }
  64. bIsOn = pPlayer->IsEffectActive( EF_DIMLIGHT );
  65. if ( bIsOn )
  66. a = 225;
  67. else
  68. a = MIN_ALPHA;
  69. if ( pPlayer->m_nFlashBattery < 20 )
  70. {
  71. m_clrReddish.GetColor( r, g, b, nUnused );
  72. }
  73. else
  74. {
  75. (gHUD.m_clrYellowish).GetColor( r, g, b, nUnused );
  76. }
  77. clrFlash.SetColor( r, g, b, a );
  78. y = icon_flash_empty->Height() / 2;
  79. x = GetWide() - ( icon_flash_empty->Width() * 1.5 );
  80. // Draw the flashlight casing
  81. icon_flash_empty->DrawSelf( x, y, clrFlash );
  82. if ( bIsOn )
  83. { // draw the flashlight beam
  84. x = GetWide() - icon_flash_empty->Width() / 2;
  85. icon_flash_beam->DrawSelf( x, y, clrFlash );
  86. }
  87. // draw the flashlight energy level
  88. x = GetWide() - ( icon_flash_empty->Width() * 1.5 );
  89. int nOffset = icon_flash_empty->Width() * ( 1.0 - ( (float)pPlayer->m_nFlashBattery / 100.0 ) );
  90. if ( nOffset < icon_flash_empty->Width() )
  91. {
  92. icon_flash_full->DrawSelfCropped( x + nOffset, y, nOffset, 0, icon_flash_full->Width() - nOffset, icon_flash_full->Height(), clrFlash );
  93. }
  94. }
  95. void CHudFlashlight::ApplySchemeSettings(vgui::IScheme *pScheme)
  96. {
  97. BaseClass::ApplySchemeSettings(pScheme);
  98. SetPaintBackgroundEnabled(false);
  99. m_clrReddish = pScheme->GetColor( "Reddish", Color( 255, 16, 16, 255 ) );
  100. }