Counter Strike : Global Offensive Source Code
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.

195 lines
5.2 KiB

  1. //========= Copyright � 1996-2005, 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 "hud_numericdisplay.h"
  11. #include "hud_macros.h"
  12. #include "iclientmode.h"
  13. #include <vgui_controls/AnimationController.h>
  14. #include <vgui/ISurface.h>
  15. #include <vgui/ILocalize.h>
  16. #include <vgui_controls/Panel.h>
  17. #include <vgui/IVgui.h>
  18. using namespace vgui;
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. #define HUD_POSTURE_UPDATES_PER_SECOND 10
  22. #define HUD_POSTURE_FADE_TIME 0.4f
  23. #define CROUCHING_CHARACTER_INDEX 92 // index of the crouching dude in the TTF font
  24. //-----------------------------------------------------------------------------
  25. // Purpose: Shows the sprint power bar
  26. //-----------------------------------------------------------------------------
  27. class CHudPosture : public CHudElement, public vgui::Panel
  28. {
  29. DECLARE_CLASS_SIMPLE( CHudPosture, vgui::Panel );
  30. public:
  31. explicit CHudPosture( const char *pElementName );
  32. bool ShouldDraw( void );
  33. #ifdef _GAMECONSOLE // if not xbox 360, don't waste code space on this
  34. virtual void Init( void );
  35. virtual void Reset( void );
  36. virtual void OnTick( void );
  37. protected:
  38. virtual void Paint();
  39. float m_duckTimeout; /// HUD_POSTURE_FADE_TIME after the last known time the player was ducking
  40. private:
  41. CPanelAnimationVar( vgui::HFont, m_hFont, "Font", "WeaponIconsSmall" );
  42. CPanelAnimationVarAliasType( float, m_IconX, "icon_xpos", "4", "proportional_float" );
  43. CPanelAnimationVarAliasType( float, m_IconY, "icon_ypos", "4", "proportional_float" );
  44. enum { NOT_FADING,
  45. FADING_UP,
  46. FADING_DOWN
  47. } m_kIsFading;
  48. #endif
  49. };
  50. DECLARE_HUDELEMENT( CHudPosture );
  51. namespace
  52. {
  53. // Don't pass a null pPlayer. Doesn't check for it.
  54. inline bool PlayerIsDucking(C_BasePlayer *pPlayer)
  55. {
  56. return pPlayer->m_Local.m_bDucked && // crouching
  57. pPlayer->GetGroundEntity() != NULL ; // but not jumping
  58. }
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose: Constructor
  62. //-----------------------------------------------------------------------------
  63. CHudPosture::CHudPosture( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudPosture" )
  64. {
  65. vgui::Panel *pParent = GetClientMode()->GetViewport();
  66. SetParent( pParent );
  67. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
  68. if( IsGameConsole() )
  69. {
  70. vgui::ivgui()->AddTickSignal( GetVPanel(), (1000/HUD_POSTURE_UPDATES_PER_SECOND) );
  71. }
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose: Save CPU cycles by letting the HUD system early cull
  75. // costly traversal. Called per frame, return true if thinking and
  76. // painting need to occur.
  77. //-----------------------------------------------------------------------------
  78. bool CHudPosture::ShouldDraw()
  79. {
  80. #ifdef _GAMECONSOLE
  81. return ( m_duckTimeout >= gpGlobals->curtime &&
  82. CHudElement::ShouldDraw() );
  83. #else
  84. return false;
  85. #endif
  86. }
  87. #ifdef _GAMECONSOLE
  88. //-----------------------------------------------------------------------------
  89. // Purpose:
  90. //-----------------------------------------------------------------------------
  91. void CHudPosture::Init( void )
  92. {
  93. m_duckTimeout = 0.0f;
  94. m_kIsFading = NOT_FADING;
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Purpose:
  98. //-----------------------------------------------------------------------------
  99. void CHudPosture::Reset( void )
  100. {
  101. Init();
  102. }
  103. void CHudPosture::OnTick( void )
  104. {
  105. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  106. if (!pPlayer)
  107. return;
  108. if ( PlayerIsDucking(pPlayer) )
  109. {
  110. m_duckTimeout = gpGlobals->curtime + HUD_POSTURE_FADE_TIME; // kick the timer forward
  111. if (GetAlpha() < 255)
  112. {
  113. // if not fully faded in, and not fading in, start fading in.
  114. if (m_kIsFading != FADING_UP)
  115. {
  116. m_kIsFading = FADING_UP;
  117. GetAnimationController()->RunAnimationCommand( this, "alpha", 255, 0, HUD_POSTURE_FADE_TIME, vgui::AnimationController::INTERPOLATOR_SIMPLESPLINE );
  118. }
  119. }
  120. else
  121. {
  122. m_kIsFading = NOT_FADING;
  123. }
  124. }
  125. else // player is not ducking
  126. {
  127. if (GetAlpha() > 0)
  128. {
  129. // if not faded out or fading out, fade out.
  130. if (m_kIsFading != FADING_DOWN)
  131. {
  132. m_kIsFading = FADING_DOWN;
  133. GetAnimationController()->RunAnimationCommand( this, "alpha", 0, 0, HUD_POSTURE_FADE_TIME, vgui::AnimationController::INTERPOLATOR_SIMPLESPLINE );
  134. }
  135. }
  136. else
  137. {
  138. m_kIsFading = NOT_FADING;
  139. }
  140. }
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Purpose: draws the posture elements we want.
  144. //-----------------------------------------------------------------------------
  145. void CHudPosture::Paint()
  146. {
  147. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  148. if ( !pPlayer )
  149. return;
  150. SetPaintBackgroundEnabled( true );
  151. Color clr;
  152. clr = GetHud().m_clrNormal;
  153. clr[3] = 255;
  154. // Pick the duck character
  155. wchar_t duck_char = CROUCHING_CHARACTER_INDEX;
  156. surface()->DrawSetTextFont( m_hFont );
  157. surface()->DrawSetTextColor( clr );
  158. surface()->DrawSetTextPos( m_IconX, m_IconY );
  159. surface()->DrawUnicodeChar( duck_char );
  160. }
  161. #endif