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.

235 lines
5.6 KiB

  1. //========= Copyright � 1996-2005, 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. explicit 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 = GetClientMode()->GetViewport();
  51. SetParent( pParent );
  52. }
  53. void CHudRoundTimer::ApplySchemeSettings(vgui::IScheme *pScheme)
  54. {
  55. m_pTimerIcon = HudIcons().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(timer > 30)
  87. {
  88. SetFgColor(m_TextColor);
  89. return;
  90. }
  91. if(timer <= 0)
  92. {
  93. timer = 0;
  94. SetFgColor(m_FlashColor);
  95. return;
  96. }
  97. if(gpGlobals->curtime > m_flNextToggle)
  98. {
  99. if( timer <= 0)
  100. {
  101. m_bFlash = true;
  102. }
  103. else if( timer <= 2)
  104. {
  105. m_flToggleTime = gpGlobals->curtime;
  106. m_flNextToggle = gpGlobals->curtime + 0.05;
  107. m_bFlash = !m_bFlash;
  108. }
  109. else if( timer <= 5)
  110. {
  111. m_flToggleTime = gpGlobals->curtime;
  112. m_flNextToggle = gpGlobals->curtime + 0.1;
  113. m_bFlash = !m_bFlash;
  114. }
  115. else if( timer <= 10)
  116. {
  117. m_flToggleTime = gpGlobals->curtime;
  118. m_flNextToggle = gpGlobals->curtime + 0.2;
  119. m_bFlash = !m_bFlash;
  120. }
  121. else if( timer <= 20)
  122. {
  123. m_flToggleTime = gpGlobals->curtime;
  124. m_flNextToggle = gpGlobals->curtime + 0.4;
  125. m_bFlash = !m_bFlash;
  126. }
  127. else if( timer <= 30)
  128. {
  129. m_flToggleTime = gpGlobals->curtime;
  130. m_flNextToggle = gpGlobals->curtime + 0.8;
  131. m_bFlash = !m_bFlash;
  132. }
  133. else
  134. m_bFlash = false;
  135. }
  136. Color startValue, endValue;
  137. Color interp_color;
  138. if( m_bFlash )
  139. {
  140. startValue = m_FlashColor;
  141. endValue = m_TextColor;
  142. }
  143. else
  144. {
  145. startValue = m_TextColor;
  146. endValue = m_FlashColor;
  147. }
  148. float pos = (gpGlobals->curtime - m_flToggleTime) / (m_flNextToggle - m_flToggleTime);
  149. pos = clamp( SimpleSpline( pos ), 0, 1 );
  150. interp_color[0] = ((endValue[0] - startValue[0]) * pos) + startValue[0];
  151. interp_color[1] = ((endValue[1] - startValue[1]) * pos) + startValue[1];
  152. interp_color[2] = ((endValue[2] - startValue[2]) * pos) + startValue[2];
  153. interp_color[3] = ((endValue[3] - startValue[3]) * pos) + startValue[3];
  154. SetFgColor(interp_color);
  155. }
  156. void CHudRoundTimer::Paint()
  157. {
  158. // Update the time.
  159. C_CSGameRules *pRules = CSGameRules();
  160. if ( !pRules )
  161. return;
  162. int timer = (int)ceil( pRules->GetRoundRemainingTime() );
  163. //If the bomb is planted and the timer is 0, don't draw
  164. // EDIT: In CZ the timer is turned off as soon as the bomb is planted, so emulate that behavior here.
  165. if( g_PlantedC4s.Count() > 0 )
  166. return;
  167. if ( pRules->IsFreezePeriod() )
  168. {
  169. // in freeze period countdown to round start time
  170. timer = (int)ceil(pRules->GetRoundStartTime()-gpGlobals->curtime);
  171. }
  172. if(timer < 0)
  173. timer = 0;
  174. int minutes = timer / 60;
  175. int seconds = timer % 60;
  176. //Draw Timer icon
  177. if( m_pTimerIcon )
  178. {
  179. m_pTimerIcon->DrawSelf( icon_xpos, icon_ypos, icon_wide, icon_tall, GetFgColor() );
  180. }
  181. PaintTime( m_hNumberFont, digit_xpos, digit_ypos, minutes, seconds );
  182. }
  183. void CHudRoundTimer::PaintTime(HFont font, int xpos, int ypos, int mins, int secs)
  184. {
  185. surface()->DrawSetTextFont(font);
  186. wchar_t unicode[6];
  187. V_snwprintf(unicode, ARRAYSIZE(unicode), L"%d:%.2d", mins, secs);
  188. surface()->DrawSetTextPos(xpos, ypos);
  189. surface()->DrawUnicodeString( unicode );
  190. }