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.

170 lines
4.5 KiB

  1. //========= Copyright 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 "c_tf_player.h"
  11. #include "iclientmode.h"
  12. #include "ienginevgui.h"
  13. #include <vgui/ILocalize.h>
  14. #include <vgui/ISurface.h>
  15. #include <vgui/IVGui.h>
  16. #include <vgui_controls/Label.h>
  17. #include <vgui_controls/EditablePanel.h>
  18. #include "tf_imagepanel.h"
  19. #include "tf_gamerules.h"
  20. #include "tf_hud_freezepanel.h"
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include "tier0/memdbgon.h"
  23. using namespace vgui;
  24. //-----------------------------------------------------------------------------
  25. // Purpose:
  26. //-----------------------------------------------------------------------------
  27. class CHudAlert : public CHudElement, public EditablePanel
  28. {
  29. DECLARE_CLASS_SIMPLE( CHudAlert, EditablePanel );
  30. public:
  31. CHudAlert( const char *pElementName );
  32. virtual void Init( void );
  33. virtual void OnTick( void );
  34. virtual void LevelInit( void );
  35. virtual void ApplySchemeSettings( IScheme *scheme );
  36. virtual bool ShouldDraw( void );
  37. virtual void FireGameEvent( IGameEvent * event );
  38. void SetupAlertPanel( int iAlertType );
  39. private:
  40. Label *m_pAlertLabel;
  41. float m_flHideAt;
  42. };
  43. DECLARE_HUDELEMENT( CHudAlert );
  44. //-----------------------------------------------------------------------------
  45. // Purpose:
  46. //-----------------------------------------------------------------------------
  47. CHudAlert::CHudAlert( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudAlert" )
  48. {
  49. Panel *pParent = g_pClientMode->GetViewport();
  50. SetParent( pParent );
  51. m_flHideAt = 0;
  52. vgui::ivgui()->AddTickSignal( GetVPanel(), 200 );
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Purpose:
  56. //-----------------------------------------------------------------------------
  57. void CHudAlert::Init( void )
  58. {
  59. ListenForGameEvent( "teamplay_alert" );
  60. SetVisible( false );
  61. CHudElement::Init();
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose:
  65. //-----------------------------------------------------------------------------
  66. void CHudAlert::FireGameEvent( IGameEvent * event )
  67. {
  68. const char *pEventName = event->GetName();
  69. if ( Q_strcmp( "teamplay_alert", pEventName ) == 0 )
  70. {
  71. int iAlertType = event->GetInt( "alert_type" );
  72. SetupAlertPanel( iAlertType );
  73. SetVisible( true );
  74. }
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Purpose:
  78. // Input : -
  79. //-----------------------------------------------------------------------------
  80. void CHudAlert::OnTick( void )
  81. {
  82. if ( m_flHideAt && m_flHideAt < gpGlobals->curtime )
  83. {
  84. SetVisible( false );
  85. m_flHideAt = 0;
  86. }
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Purpose:
  90. //-----------------------------------------------------------------------------
  91. void CHudAlert::LevelInit( void )
  92. {
  93. m_flHideAt = 0;
  94. SetVisible( false );
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Purpose:
  98. //-----------------------------------------------------------------------------
  99. bool CHudAlert::ShouldDraw( void )
  100. {
  101. if ( IsTakingAFreezecamScreenshot() )
  102. return false;
  103. if ( engine->IsPlayingDemo() )
  104. return false;
  105. return ( IsVisible() );
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Purpose:
  109. //-----------------------------------------------------------------------------
  110. void CHudAlert::ApplySchemeSettings( IScheme *pScheme )
  111. {
  112. // load control settings...
  113. LoadControlSettings( "resource/UI/HudAlert.res" );
  114. BaseClass::ApplySchemeSettings( pScheme );
  115. m_pAlertLabel = dynamic_cast<Label *>( FindChildByName("AlertLabel") );
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Purpose:
  119. //-----------------------------------------------------------------------------
  120. void CHudAlert::SetupAlertPanel( int iAlertType )
  121. {
  122. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  123. if ( !pPlayer )
  124. return;
  125. if ( !m_pAlertLabel )
  126. return;
  127. // Alert case handling
  128. const char *pszResult = NULL;
  129. switch ( iAlertType )
  130. {
  131. case HUD_ALERT_SCRAMBLE_TEAMS:
  132. pszResult = "#game_scramble_onrestart";
  133. m_flHideAt = gpGlobals->curtime + 5.0;
  134. break;
  135. default:
  136. m_flHideAt = gpGlobals->curtime + 10.0;
  137. break;
  138. }
  139. if ( pszResult )
  140. {
  141. m_pAlertLabel->SetText( g_pVGuiLocalize->Find( pszResult ) );
  142. }
  143. }