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.

124 lines
2.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hudelement.h"
  8. #include <vgui_controls/Panel.h>
  9. #include <vgui/ISurface.h>
  10. #include "c_cs_player.h"
  11. #include "clientmode_csnormal.h"
  12. #include "weapon_c4.h"
  13. ConVar cl_c4progressbar( "cl_c4progressbar", "1", 0, "Draw progress bar when defusing the C4" );
  14. class CHudProgressBar : public CHudElement, public vgui::Panel
  15. {
  16. public:
  17. DECLARE_CLASS_SIMPLE( CHudProgressBar, vgui::Panel );
  18. CHudProgressBar( const char *name );
  19. // vgui overrides
  20. virtual void Paint();
  21. virtual bool ShouldDraw();
  22. CPanelAnimationVar( Color, m_clrProgress, "ProgressBarFg", "ProgressBar.FgColor" );
  23. };
  24. DECLARE_HUDELEMENT( CHudProgressBar );
  25. CHudProgressBar::CHudProgressBar( const char *name ) :
  26. vgui::Panel( NULL, "HudProgressBar" ), CHudElement( name )
  27. {
  28. vgui::Panel *pParent = g_pClientMode->GetViewport();
  29. SetParent( pParent );
  30. SetPaintBorderEnabled( false );
  31. SetPaintBackgroundEnabled( false );
  32. SetHiddenBits( HIDEHUD_PLAYERDEAD | HIDEHUD_WEAPONSELECTION );
  33. }
  34. void CHudProgressBar::Paint()
  35. {
  36. C_CSPlayer *pPlayer = C_CSPlayer::GetLocalCSPlayer();
  37. if( pPlayer && pPlayer->GetObserverMode() == OBS_MODE_IN_EYE )
  38. {
  39. C_BaseEntity *pTarget = pPlayer->GetObserverTarget();
  40. if( pTarget && pTarget->IsPlayer() )
  41. {
  42. pPlayer = ToCSPlayer( pTarget );
  43. if( !pPlayer->IsAlive() )
  44. return;
  45. }
  46. else
  47. return;
  48. }
  49. if ( !pPlayer )
  50. return;
  51. int x, y, wide, tall;
  52. GetBounds( x, y, wide, tall );
  53. tall = 10;
  54. int xOffset=0;
  55. int yOffset=0;
  56. Color clr = m_clrProgress;
  57. clr[3] = 160;
  58. vgui::surface()->DrawSetColor( clr );
  59. vgui::surface()->DrawOutlinedRect( xOffset, yOffset, xOffset+wide, yOffset+tall );
  60. if( pPlayer->m_iProgressBarDuration > 0 )
  61. {
  62. // ProgressBarStartTime is now with respect to m_flSimulationTime rather than local time
  63. float percent = (pPlayer->m_flSimulationTime - pPlayer->m_flProgressBarStartTime) / (float)pPlayer->m_iProgressBarDuration;
  64. percent = clamp( percent, 0, 1 );
  65. clr[3] = 240;
  66. vgui::surface()->DrawSetColor( clr );
  67. vgui::surface()->DrawFilledRect( xOffset+2, yOffset+2, xOffset+(int)(percent*wide)-2, yOffset+tall-2 );
  68. }
  69. }
  70. bool CHudProgressBar::ShouldDraw()
  71. {
  72. C_CSPlayer *pPlayer = C_CSPlayer::GetLocalCSPlayer();
  73. if( pPlayer && pPlayer->GetObserverMode() == OBS_MODE_IN_EYE )
  74. {
  75. C_BaseEntity *pTarget = pPlayer->GetObserverTarget();
  76. if( pTarget && pTarget->IsPlayer() )
  77. {
  78. pPlayer = ToCSPlayer( pTarget );
  79. if( !pPlayer->IsAlive() )
  80. return false;
  81. }
  82. else
  83. return false;
  84. }
  85. if( !pPlayer || pPlayer->m_iProgressBarDuration == 0 || pPlayer->m_lifeState == LIFE_DEAD )
  86. {
  87. return false;
  88. }
  89. return cl_c4progressbar.GetBool();
  90. }