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.

233 lines
5.4 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 "clientmode_csnormal.h"
  11. #include "cs_gamerules.h"
  12. #include "hud_basetimer.h"
  13. #include "hud_bitmapnumericdisplay.h"
  14. #include "c_plantedc4.h"
  15. #include <vgui_controls/AnimationController.h>
  16. class CHudRoundTimer : public CHudElement, public vgui::Panel
  17. {
  18. public:
  19. DECLARE_CLASS_SIMPLE( CHudRoundTimer, vgui::Panel );
  20. CHudRoundTimer( const char *name );
  21. protected:
  22. virtual void Paint();
  23. virtual void Think();
  24. virtual bool ShouldDraw();
  25. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  26. void PaintTime(HFont font, int xpos, int ypos, int mins, int secs);
  27. private:
  28. float m_flToggleTime;
  29. float m_flNextToggle;
  30. CHudTexture *m_pTimerIcon;
  31. bool m_bFlash;
  32. int m_iAdditiveWhiteID;
  33. CPanelAnimationVar( Color, m_FlashColor, "FlashColor", "Panel.FgColor" );
  34. CPanelAnimationVarAliasType( float, icon_xpos, "icon_xpos", "0", "proportional_float" );
  35. CPanelAnimationVarAliasType( float, icon_ypos, "icon_ypos", "0", "proportional_float" );
  36. CPanelAnimationVar( Color, m_TextColor, "TextColor", "Panel.FgColor" );
  37. CPanelAnimationVar( vgui::HFont, m_hNumberFont, "NumberFont", "HudNumbers" );
  38. CPanelAnimationVarAliasType( float, digit_xpos, "digit_xpos", "50", "proportional_float" );
  39. CPanelAnimationVarAliasType( float, digit_ypos, "digit_ypos", "2", "proportional_float" );
  40. float icon_tall;
  41. float icon_wide;
  42. };
  43. DECLARE_HUDELEMENT( CHudRoundTimer );
  44. CHudRoundTimer::CHudRoundTimer( const char *pName ) :
  45. BaseClass( NULL, "HudRoundTimer" ), CHudElement( pName )
  46. {
  47. m_iAdditiveWhiteID = vgui::surface()->CreateNewTextureID();
  48. vgui::surface()->DrawSetTextureFile( m_iAdditiveWhiteID, "vgui/white_additive" , true, false);
  49. SetHiddenBits( HIDEHUD_PLAYERDEAD );
  50. vgui::Panel *pParent = g_pClientMode->GetViewport();
  51. SetParent( pParent );
  52. }
  53. void CHudRoundTimer::ApplySchemeSettings(vgui::IScheme *pScheme)
  54. {
  55. m_pTimerIcon = gHUD.GetIcon( "timer_icon" );
  56. if( m_pTimerIcon )
  57. {
  58. icon_tall = GetTall() - YRES(2);
  59. float scale = icon_tall / (float)m_pTimerIcon->Height();
  60. icon_wide = ( scale ) * (float)m_pTimerIcon->Width();
  61. }
  62. SetFgColor( m_TextColor );
  63. BaseClass::ApplySchemeSettings( pScheme );
  64. }
  65. bool CHudRoundTimer::ShouldDraw()
  66. {
  67. //necessary?
  68. C_CSPlayer *pPlayer = C_CSPlayer::GetLocalCSPlayer();
  69. if ( pPlayer )
  70. {
  71. return !pPlayer->IsObserver();
  72. }
  73. return false;
  74. }
  75. void CHudRoundTimer::Think()
  76. {
  77. C_CSGameRules *pRules = CSGameRules();
  78. if ( !pRules )
  79. return;
  80. int timer = (int)ceil( pRules->GetRoundRemainingTime() );
  81. if ( pRules->IsFreezePeriod() )
  82. {
  83. // in freeze period countdown to round start time
  84. timer = (int)ceil(pRules->GetRoundStartTime()-gpGlobals->curtime);
  85. }
  86. //If the bomb is planted don't draw -- the timer is irrelevant
  87. SetVisible(g_PlantedC4s.Count() == 0);
  88. if(timer > 30)
  89. {
  90. SetFgColor(m_TextColor);
  91. return;
  92. }
  93. if(timer <= 0)
  94. {
  95. timer = 0;
  96. SetFgColor(m_FlashColor);
  97. return;
  98. }
  99. if(gpGlobals->curtime > m_flNextToggle)
  100. {
  101. if( timer <= 0)
  102. {
  103. m_bFlash = true;
  104. }
  105. else if( timer <= 2)
  106. {
  107. m_flToggleTime = gpGlobals->curtime;
  108. m_flNextToggle = gpGlobals->curtime + 0.05;
  109. m_bFlash = !m_bFlash;
  110. }
  111. else if( timer <= 5)
  112. {
  113. m_flToggleTime = gpGlobals->curtime;
  114. m_flNextToggle = gpGlobals->curtime + 0.1;
  115. m_bFlash = !m_bFlash;
  116. }
  117. else if( timer <= 10)
  118. {
  119. m_flToggleTime = gpGlobals->curtime;
  120. m_flNextToggle = gpGlobals->curtime + 0.2;
  121. m_bFlash = !m_bFlash;
  122. }
  123. else if( timer <= 20)
  124. {
  125. m_flToggleTime = gpGlobals->curtime;
  126. m_flNextToggle = gpGlobals->curtime + 0.4;
  127. m_bFlash = !m_bFlash;
  128. }
  129. else if( timer <= 30)
  130. {
  131. m_flToggleTime = gpGlobals->curtime;
  132. m_flNextToggle = gpGlobals->curtime + 0.8;
  133. m_bFlash = !m_bFlash;
  134. }
  135. else
  136. m_bFlash = false;
  137. }
  138. Color startValue, endValue;
  139. Color interp_color;
  140. if( m_bFlash )
  141. {
  142. startValue = m_FlashColor;
  143. endValue = m_TextColor;
  144. }
  145. else
  146. {
  147. startValue = m_TextColor;
  148. endValue = m_FlashColor;
  149. }
  150. float pos = (gpGlobals->curtime - m_flToggleTime) / (m_flNextToggle - m_flToggleTime);
  151. pos = clamp( SimpleSpline( pos ), 0, 1 );
  152. interp_color[0] = ((endValue[0] - startValue[0]) * pos) + startValue[0];
  153. interp_color[1] = ((endValue[1] - startValue[1]) * pos) + startValue[1];
  154. interp_color[2] = ((endValue[2] - startValue[2]) * pos) + startValue[2];
  155. interp_color[3] = ((endValue[3] - startValue[3]) * pos) + startValue[3];
  156. SetFgColor(interp_color);
  157. }
  158. void CHudRoundTimer::Paint()
  159. {
  160. // Update the time.
  161. C_CSGameRules *pRules = CSGameRules();
  162. if ( !pRules )
  163. return;
  164. int timer = (int)ceil( pRules->GetRoundRemainingTime() );
  165. if ( pRules->IsFreezePeriod() )
  166. {
  167. // in freeze period countdown to round start time
  168. timer = (int)ceil(pRules->GetRoundStartTime()-gpGlobals->curtime);
  169. }
  170. if(timer < 0)
  171. timer = 0;
  172. int minutes = timer / 60;
  173. int seconds = timer % 60;
  174. //Draw Timer icon
  175. if( m_pTimerIcon )
  176. {
  177. m_pTimerIcon->DrawSelf( icon_xpos, icon_ypos, icon_wide, icon_tall, GetFgColor() );
  178. }
  179. PaintTime( m_hNumberFont, digit_xpos, digit_ypos, minutes, seconds );
  180. }
  181. void CHudRoundTimer::PaintTime(HFont font, int xpos, int ypos, int mins, int secs)
  182. {
  183. surface()->DrawSetTextFont(font);
  184. wchar_t unicode[6];
  185. V_snwprintf(unicode, ARRAYSIZE(unicode), L"%d:%.2d", mins, secs);
  186. surface()->DrawSetTextPos(xpos, ypos);
  187. surface()->DrawUnicodeString( unicode );
  188. }