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.

148 lines
4.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "cbase.h"
  8. #include "hud.h"
  9. #include "hudelement.h"
  10. #include "c_tf_player.h"
  11. #include "iclientmode.h"
  12. #include "ienginevgui.h"
  13. #include <vgui/ILocalize.h>
  14. #include <vgui/ISurface.h>
  15. #include <vgui/IVGui.h>
  16. #include <vgui_controls/EditablePanel.h>
  17. #include <vgui_controls/ProgressBar.h>
  18. #include "tf_weaponbase.h"
  19. //#include "c_tf_projectile_arrow.h"
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. using namespace vgui;
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. //-----------------------------------------------------------------------------
  26. class CHudSapperChargeMeter : public CHudElement, public EditablePanel
  27. {
  28. DECLARE_CLASS_SIMPLE( CHudSapperChargeMeter, EditablePanel );
  29. public:
  30. CHudSapperChargeMeter( const char *pElementName );
  31. virtual void ApplySchemeSettings( IScheme *scheme );
  32. virtual bool ShouldDraw( void );
  33. virtual void OnTick( void );
  34. private:
  35. vgui::ContinuousProgressBar *m_pChargeMeter;
  36. };
  37. #ifdef STAGING_ONLY
  38. DECLARE_HUDELEMENT( CHudSapperChargeMeter );
  39. #endif
  40. //-----------------------------------------------------------------------------
  41. // Purpose:
  42. //-----------------------------------------------------------------------------
  43. CHudSapperChargeMeter::CHudSapperChargeMeter( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudSapperCharge" )
  44. {
  45. Panel *pParent = g_pClientMode->GetViewport();
  46. SetParent( pParent );
  47. m_pChargeMeter = new ContinuousProgressBar( this, "ChargeMeter" );
  48. SetHiddenBits( HIDEHUD_MISCSTATUS );
  49. vgui::ivgui()->AddTickSignal( GetVPanel() );
  50. RegisterForRenderGroup( "inspect_panel" );
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Purpose:
  54. //-----------------------------------------------------------------------------
  55. void CHudSapperChargeMeter::ApplySchemeSettings( IScheme *pScheme )
  56. {
  57. // load control settings...
  58. LoadControlSettings( "resource/UI/HudSapperCharge.res" );
  59. BaseClass::ApplySchemeSettings( pScheme );
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. //-----------------------------------------------------------------------------
  64. bool CHudSapperChargeMeter::ShouldDraw( void )
  65. {
  66. C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();
  67. if ( !pPlayer || !pPlayer->IsPlayerClass( TF_CLASS_SPY ) || !pPlayer->IsAlive() )
  68. return false;
  69. CTFWeaponBase *pWpn = pPlayer->GetActiveTFWeapon();
  70. if ( !pWpn )
  71. return false;
  72. int iWeaponID = pWpn->GetWeaponID();
  73. if ( iWeaponID != TF_WEAPON_BUILDER )
  74. return false;
  75. int iCustomHUD = 0;
  76. CALL_ATTRIB_HOOK_INT_ON_OTHER( pWpn, iCustomHUD, custom_charge_meter );
  77. if ( !iCustomHUD )
  78. return false;
  79. ITFChargeUpWeapon *pChargeupWeapon = dynamic_cast<ITFChargeUpWeapon *>( pWpn );
  80. if ( !pChargeupWeapon || !pChargeupWeapon->CanCharge() )
  81. return false;
  82. // Only draw if we have any charge
  83. if ( pChargeupWeapon->GetChargeBeginTime() <= 0 )
  84. return false;
  85. return CHudElement::ShouldDraw();
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose:
  89. //-----------------------------------------------------------------------------
  90. void CHudSapperChargeMeter::OnTick( void )
  91. {
  92. C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();
  93. if ( !pPlayer )
  94. return;
  95. CTFWeaponBase *pWpn = pPlayer->GetActiveTFWeapon();
  96. ITFChargeUpWeapon *pChargeupWeapon = dynamic_cast< ITFChargeUpWeapon *>( pWpn );
  97. if ( !pWpn || !pChargeupWeapon )
  98. return;
  99. if ( m_pChargeMeter )
  100. {
  101. float flChargeMaxTime = pChargeupWeapon->GetChargeMaxTime();
  102. if ( flChargeMaxTime != 0 )
  103. {
  104. float flChargeBeginTime = pChargeupWeapon->GetChargeBeginTime();
  105. if ( flChargeBeginTime > 0 )
  106. {
  107. float flTimeCharged = MAX( 0, gpGlobals->curtime - flChargeBeginTime );
  108. flTimeCharged = MIN( flTimeCharged, flChargeMaxTime );
  109. float flPercentCharged = MIN( 1.0, flTimeCharged / flChargeMaxTime );
  110. m_pChargeMeter->SetProgress( flPercentCharged );
  111. }
  112. else
  113. {
  114. m_pChargeMeter->SetProgress( 0.0f );
  115. }
  116. }
  117. }
  118. }