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.

254 lines
7.1 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 "hud_numericdisplay.h"
  11. #include <KeyValues.h>
  12. #include <vgui/IScheme.h>
  13. #include <vgui/ISurface.h>
  14. #include <vgui/ISystem.h>
  15. #include <vgui_controls/AnimationController.h>
  16. #include "iclientmode.h"
  17. #include "tf_shareddefs.h"
  18. #include <vgui_controls/EditablePanel.h>
  19. #include <vgui_controls/Label.h>
  20. #include <vgui_controls/ImagePanel.h>
  21. #include <vgui/ISurface.h>
  22. #include <vgui/IImage.h>
  23. #include <vgui_controls/Label.h>
  24. #include "VGuiMatSurface/IMatSystemSurface.h"
  25. #include "c_tf_player.h"
  26. // memdbgon must be the last include file in a .cpp file!!!
  27. #include "tier0/memdbgon.h"
  28. static ConVar cl_showcrit( "cl_showcrit", "0", FCVAR_DEVELOPMENTONLY, "Debug! Draw crit values above the ammo count." );
  29. //-----------------------------------------------------------------------------
  30. // Purpose: Critical meter panel.
  31. //-----------------------------------------------------------------------------
  32. class CCriticalPanel : public CHudElement, public vgui::EditablePanel
  33. {
  34. public:
  35. DECLARE_CLASS_SIMPLE( CCriticalPanel, vgui::EditablePanel );
  36. CCriticalPanel( const char *pElementName );
  37. virtual ~CCriticalPanel( void );
  38. virtual void Reset();
  39. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  40. virtual bool ShouldDraw( void );
  41. protected:
  42. virtual void OnThink();
  43. private:
  44. void InitCritData( void );
  45. // vgui
  46. vgui::HFont m_hFont;
  47. vgui::Label *m_pTextLabel;
  48. float m_flNextThink;
  49. // Critical Data.
  50. struct CriticalData_t
  51. {
  52. float m_flCurrent;
  53. float m_flAverage;
  54. float m_flLow;
  55. float m_flHigh;
  56. };
  57. bool m_bInitData;
  58. CriticalData_t m_CritData;
  59. };
  60. DECLARE_HUDELEMENT( CCriticalPanel );
  61. #define CRITICAL_PANEL_WIDTH 300
  62. #define CRITICAL_BLEND_WEIGHT 0.1f
  63. //-----------------------------------------------------------------------------
  64. // Purpose: Constructor.
  65. // Input : *parent - parent VGUI window
  66. //-----------------------------------------------------------------------------
  67. CCriticalPanel::CCriticalPanel( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "CriticalPanel" )
  68. {
  69. Panel *pParent = g_pClientMode->GetViewport();
  70. SetParent( pParent );
  71. SetScheme( "ClientScheme" );
  72. SetVisible( false );
  73. SetCursor( null );
  74. SetFgColor( Color( 0, 0, 0, 255 ) );
  75. SetPaintBackgroundEnabled( false );
  76. m_pTextLabel = new vgui::Label( this, "CriticalPanelLabel", "" );
  77. m_hFont = 0;
  78. // Have we initialize the criticl data yet?
  79. m_bInitData = false;
  80. m_flNextThink = 0.0f;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: Destructor.
  84. //-----------------------------------------------------------------------------
  85. CCriticalPanel::~CCriticalPanel( void )
  86. {
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Purpose:
  90. //-----------------------------------------------------------------------------
  91. void CCriticalPanel::Reset()
  92. {
  93. m_flNextThink = gpGlobals->curtime + 0.05f;
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Purpose:
  97. //-----------------------------------------------------------------------------
  98. void CCriticalPanel::InitCritData( void )
  99. {
  100. m_CritData.m_flCurrent = -1.0f;
  101. m_CritData.m_flAverage = -1.0f;
  102. m_CritData.m_flLow = -1.0f;
  103. m_CritData.m_flHigh = -1.0f;
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Purpose:
  107. //-----------------------------------------------------------------------------
  108. void CCriticalPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  109. {
  110. BaseClass::ApplySchemeSettings( pScheme );
  111. m_pTextLabel->SetBounds( 0.0f, 0.0f, GetWide(), GetTall() );
  112. m_pTextLabel->SetFont( pScheme->GetFont( "DefaultSmall" ) );
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Purpose:
  116. // Output : Returns true on success, false on failure.
  117. //-----------------------------------------------------------------------------
  118. bool CCriticalPanel::ShouldDraw( void )
  119. {
  120. return false;
  121. if ( ( !cl_showcrit.GetInt() || ( gpGlobals->absoluteframetime <= 0 ) ) &&
  122. ( !cl_showcrit.GetInt() ) )
  123. {
  124. m_bInitData = false;
  125. return false;
  126. }
  127. if ( !m_bInitData )
  128. {
  129. m_bInitData = true;
  130. InitCritData();
  131. }
  132. return true;
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Purpose:
  136. //-----------------------------------------------------------------------------
  137. void GetCritColor( float flCritMult, unsigned char ucColor[3] )
  138. {
  139. ucColor[0] = 255; ucColor[1] = 255; ucColor[2] = 0;
  140. if ( flCritMult < 5.0f )
  141. {
  142. ucColor[1] = 0;
  143. }
  144. else if ( flCritMult > 5.0f )
  145. {
  146. ucColor[0] = 0;
  147. }
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Purpose:
  151. //-----------------------------------------------------------------------------
  152. void CCriticalPanel::OnThink()
  153. {
  154. // Get the local TF player.
  155. C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();
  156. if ( !pPlayer )
  157. return;
  158. if ( cl_showcrit.GetInt() )
  159. {
  160. // Get the current critical multiplier.
  161. float flCritMult = pPlayer->GetCritMult() * TF_DAMAGE_CRIT_CHANCE;
  162. flCritMult *= 100.0f;
  163. if ( m_CritData.m_flAverage < 0.0f )
  164. {
  165. // Initial data.
  166. m_CritData.m_flCurrent = flCritMult;
  167. m_CritData.m_flAverage = flCritMult;
  168. m_CritData.m_flLow = m_CritData.m_flAverage;
  169. m_CritData.m_flHigh = m_CritData.m_flAverage;
  170. }
  171. else
  172. {
  173. // Average over time.
  174. m_CritData.m_flCurrent = flCritMult;
  175. m_CritData.m_flAverage *= ( 1.0f - CRITICAL_BLEND_WEIGHT ) ;
  176. m_CritData.m_flAverage += ( flCritMult * CRITICAL_BLEND_WEIGHT );
  177. }
  178. // Adjust for highs and lows.
  179. m_CritData.m_flLow = MIN( m_CritData.m_flLow, flCritMult );
  180. m_CritData.m_flHigh = MAX( m_CritData.m_flHigh, flCritMult );
  181. unsigned char ucColor[3];
  182. GetCritColor( flCritMult, ucColor );
  183. m_pTextLabel->SetFgColor( Color( ucColor[0], ucColor[1], ucColor[2], 255 ) );
  184. char szCriticalText[256];
  185. //Q_snprintf( szCriticalText, sizeof( szCriticalText ), "Crit: %3.2f (A:%3.2f, L:%3.2f, H:%3.2f)",
  186. // m_CritData.m_flCurrent, m_CritData.m_flAverage, m_CritData.m_flLow, m_CritData.m_flHigh );
  187. Q_snprintf( szCriticalText, sizeof( szCriticalText ), "Crit Chance: %3.2f", m_CritData.m_flCurrent );
  188. m_pTextLabel->SetText( szCriticalText );
  189. m_flNextThink = gpGlobals->curtime + 0.01f;
  190. }
  191. else
  192. {
  193. m_flNextThink = gpGlobals->curtime + 0.1f;
  194. }
  195. }
  196. #if 0
  197. //-----------------------------------------------------------------------------
  198. // Purpose:
  199. // Input :
  200. //-----------------------------------------------------------------------------
  201. void CCriticalPanel::Paint()
  202. {
  203. if ( cl_showcrit.GetInt() )
  204. {
  205. unsigned char ucColor[3];
  206. GetCritColor( m_CritData.m_flCurrent, ucColor );
  207. g_pMatSystemSurface->DrawColoredText( m_hFont, 0, 2, ucColor[0], ucColor[1], ucColor[2], 255,
  208. "%2.2f (Avg:%2.2f, Low:%2.2f, High:%2.2f)",
  209. m_CritData.m_flCurrent, m_CritData.m_flAverage, m_CritData.m_flLow, m_CritData.m_flHigh );
  210. }
  211. }
  212. #endif