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.

169 lines
5.3 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 "iclientmode.h"
  11. #include "vgui_controls/AnimationController.h"
  12. #include "vgui_controls/Label.h"
  13. #include "vgui/ILocalize.h"
  14. #include "vgui/ISurface.h"
  15. #include "text_message.h"
  16. #include "dod_gamerules.h"
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Displays current ammunition level
  21. //-----------------------------------------------------------------------------
  22. class CDODRestartRoundLabel : public vgui::Panel, public CHudElement
  23. {
  24. DECLARE_CLASS_SIMPLE( CDODRestartRoundLabel, vgui::Panel );
  25. public:
  26. CDODRestartRoundLabel( const char *pElementName );
  27. virtual void Reset();
  28. virtual void PerformLayout();
  29. protected:
  30. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  31. virtual void OnThink();
  32. private:
  33. vgui::HFont m_hFont;
  34. Color m_bgColor;
  35. vgui::Label *m_pRestartLabel; // "Round will restart in 0:00"
  36. vgui::Label *m_pBackground;
  37. CPanelAnimationVarAliasType( int, m_iTextX, "text_xpos", "8", "proportional_int" );
  38. CPanelAnimationVarAliasType( int, m_iTextY, "text_ypos", "8", "proportional_int" );
  39. float m_flLastRestartTime;
  40. };
  41. DECLARE_HUDELEMENT( CDODRestartRoundLabel );
  42. //-----------------------------------------------------------------------------
  43. // Purpose: Constructor
  44. //-----------------------------------------------------------------------------
  45. CDODRestartRoundLabel::CDODRestartRoundLabel( const char *pElementName ) : BaseClass(NULL, "RestartRoundLabel"), CHudElement( pElementName )
  46. {
  47. vgui::Panel *pParent = g_pClientMode->GetViewport();
  48. SetParent( pParent );
  49. SetVisible( false );
  50. SetAlpha( 0 );
  51. m_pBackground = new vgui::Label( this, "Background", "" );
  52. wchar_t labelText[128];
  53. g_pVGuiLocalize->ConstructString( labelText, sizeof(labelText), g_pVGuiLocalize->Find( "#clan_game_restart" ), 2, L"0", L"00" );
  54. m_pRestartLabel = new vgui::Label( this, "restartroundlabel", labelText );
  55. m_pRestartLabel->SetPaintBackgroundEnabled( false );
  56. m_pRestartLabel->SetPaintBorderEnabled( false );
  57. m_pRestartLabel->SizeToContents();
  58. m_pRestartLabel->SetContentAlignment( vgui::Label::a_west );
  59. m_pRestartLabel->SetFgColor( GetFgColor() );
  60. m_flLastRestartTime = 0;
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose:
  64. //-----------------------------------------------------------------------------
  65. void CDODRestartRoundLabel::Reset()
  66. {
  67. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "RestartRoundLabelHide" );
  68. m_flLastRestartTime = 0;
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose:
  72. //-----------------------------------------------------------------------------
  73. void CDODRestartRoundLabel::ApplySchemeSettings( vgui::IScheme *pScheme )
  74. {
  75. BaseClass::ApplySchemeSettings( pScheme );
  76. SetFgColor( Color(0,0,0,0) );
  77. m_hFont = pScheme->GetFont( "HudHintText", true );
  78. m_pBackground->SetBgColor( GetSchemeColor("HintMessageBg", pScheme) );
  79. m_pBackground->SetPaintBackgroundType( 2 );
  80. SetAlpha( 0 );
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: Resizes the label
  84. //-----------------------------------------------------------------------------
  85. void CDODRestartRoundLabel::PerformLayout()
  86. {
  87. BaseClass::PerformLayout();
  88. int wide, tall;
  89. GetSize( wide, tall );
  90. // find the widest line
  91. int labelWide = m_pRestartLabel->GetWide();
  92. // find the total height
  93. int fontTall = vgui::surface()->GetFontTall( m_hFont );
  94. int labelTall = fontTall;
  95. labelWide += m_iTextX*2;
  96. labelTall += m_iTextY*2;
  97. m_pBackground->SetBounds( 0, 0, labelWide, labelTall );
  98. int xOffset = (labelWide - m_pRestartLabel->GetWide())/2;
  99. m_pRestartLabel->SetPos( 0 + xOffset, 0 + m_iTextY );
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Purpose: Updates the label color each frame
  103. //-----------------------------------------------------------------------------
  104. void CDODRestartRoundLabel::OnThink()
  105. {
  106. float flRoundRestartTime = DODGameRules()->GetRoundRestartTime();
  107. if( flRoundRestartTime != m_flLastRestartTime )
  108. {
  109. if( flRoundRestartTime > 0 )
  110. {
  111. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "RestartRoundLabelShow" );
  112. }
  113. m_flLastRestartTime = flRoundRestartTime;
  114. }
  115. if( GetAlpha() > 0 )
  116. {
  117. wchar_t minutes[4];
  118. wchar_t seconds[4];
  119. int iSecondsRemaining = (int)( flRoundRestartTime - gpGlobals->curtime );
  120. iSecondsRemaining = MAX( 0, iSecondsRemaining );
  121. _snwprintf ( minutes, sizeof(minutes)/sizeof(wchar_t), L"%d", (iSecondsRemaining / 60) );
  122. _snwprintf ( seconds, sizeof(seconds)/sizeof(wchar_t), L"%02d", (iSecondsRemaining % 60) );
  123. wchar_t labelText[128];
  124. g_pVGuiLocalize->ConstructString( labelText, sizeof(labelText), g_pVGuiLocalize->Find( "#clan_game_restart" ), 2, minutes, seconds );
  125. m_pRestartLabel->SetText( labelText );
  126. m_pRestartLabel->SizeToContents();
  127. //InvalidateLayout();
  128. }
  129. m_pBackground->SetFgColor(GetFgColor());
  130. m_pRestartLabel->SetFgColor(GetFgColor());
  131. }