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.

177 lines
4.9 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 "c_tf_team.h"
  21. #include "tf_hud_freezepanel.h"
  22. // memdbgon must be the last include file in a .cpp file!!!
  23. #include "tier0/memdbgon.h"
  24. using namespace vgui;
  25. //-----------------------------------------------------------------------------
  26. // Purpose:
  27. //-----------------------------------------------------------------------------
  28. class CHudTeamSwitch : public CHudElement, public EditablePanel
  29. {
  30. DECLARE_CLASS_SIMPLE( CHudTeamSwitch, EditablePanel );
  31. public:
  32. CHudTeamSwitch( const char *pElementName );
  33. virtual void Init( void );
  34. virtual void OnTick( void );
  35. virtual void LevelInit( void );
  36. virtual void ApplySchemeSettings( IScheme *scheme );
  37. virtual bool ShouldDraw( void );
  38. virtual void FireGameEvent( IGameEvent * event );
  39. void SetupSwitchPanel( int iNewTeam );
  40. private:
  41. Label *m_pBalanceLabel;
  42. float m_flHideAt;
  43. };
  44. DECLARE_HUDELEMENT( CHudTeamSwitch );
  45. //-----------------------------------------------------------------------------
  46. // Purpose:
  47. //-----------------------------------------------------------------------------
  48. CHudTeamSwitch::CHudTeamSwitch( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudTeamSwitch" )
  49. {
  50. Panel *pParent = g_pClientMode->GetViewport();
  51. SetParent( pParent );
  52. SetHiddenBits( HIDEHUD_MISCSTATUS );
  53. m_flHideAt = 0;
  54. vgui::ivgui()->AddTickSignal( GetVPanel() );
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose:
  58. //-----------------------------------------------------------------------------
  59. void CHudTeamSwitch::Init( void )
  60. {
  61. // listen for events
  62. ListenForGameEvent( "teamplay_teambalanced_player" );
  63. SetVisible( false );
  64. CHudElement::Init();
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose:
  68. //-----------------------------------------------------------------------------
  69. void CHudTeamSwitch::FireGameEvent( IGameEvent * event )
  70. {
  71. const char *pEventName = event->GetName();
  72. if ( Q_strcmp( "teamplay_teambalanced_player", pEventName ) == 0 )
  73. {
  74. int iPlayer = event->GetInt( "player" );
  75. int iNewTeam = event->GetInt( "team" );
  76. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  77. if ( pPlayer && iPlayer == pPlayer->entindex() )
  78. {
  79. SetupSwitchPanel( iNewTeam );
  80. m_flHideAt = gpGlobals->curtime + 10.0;
  81. SetVisible( true );
  82. }
  83. }
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose:
  87. // Input : -
  88. //-----------------------------------------------------------------------------
  89. void CHudTeamSwitch::OnTick( void )
  90. {
  91. if ( m_flHideAt && m_flHideAt < gpGlobals->curtime )
  92. {
  93. SetVisible( false );
  94. m_flHideAt = 0;
  95. }
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Purpose:
  99. //-----------------------------------------------------------------------------
  100. void CHudTeamSwitch::LevelInit( void )
  101. {
  102. m_flHideAt = 0;
  103. SetVisible( false );
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Purpose:
  107. //-----------------------------------------------------------------------------
  108. bool CHudTeamSwitch::ShouldDraw( void )
  109. {
  110. if ( IsTakingAFreezecamScreenshot() )
  111. return false;
  112. return ( IsVisible() );
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Purpose:
  116. //-----------------------------------------------------------------------------
  117. void CHudTeamSwitch::ApplySchemeSettings( IScheme *pScheme )
  118. {
  119. // load control settings...
  120. LoadControlSettings( "resource/UI/HudTeamSwitch.res" );
  121. BaseClass::ApplySchemeSettings( pScheme );
  122. m_pBalanceLabel = dynamic_cast<Label *>( FindChildByName("BalanceLabel") );
  123. }
  124. extern const char *pszTeamRoleSwitch[NUM_TEAM_ROLES];
  125. //-----------------------------------------------------------------------------
  126. // Purpose:
  127. //-----------------------------------------------------------------------------
  128. void CHudTeamSwitch::SetupSwitchPanel( int iNewTeam )
  129. {
  130. if ( m_pBalanceLabel )
  131. {
  132. C_TFTeam *pNewTeam = GetGlobalTFTeam( iNewTeam );
  133. if ( pNewTeam )
  134. {
  135. int iRole = pNewTeam->GetRole();
  136. if ( iRole > 0 && iRole < NUM_TEAM_ROLES )
  137. {
  138. m_pBalanceLabel->SetText( g_pVGuiLocalize->Find( pszTeamRoleSwitch[iRole] ) );
  139. }
  140. else if ( iRole == 0 )
  141. {
  142. if ( iNewTeam == TF_TEAM_RED )
  143. {
  144. m_pBalanceLabel->SetText( g_pVGuiLocalize->Find( "#TF_teamswitch_red" ) );
  145. }
  146. else
  147. {
  148. m_pBalanceLabel->SetText( g_pVGuiLocalize->Find( "#TF_teamswitch_blue" ) );
  149. }
  150. }
  151. }
  152. }
  153. }