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.

283 lines
8.3 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. #include "tf_hud_teamswitch.h"
  23. #include "hud_chat.h"
  24. // memdbgon must be the last include file in a .cpp file!!!
  25. #include "tier0/memdbgon.h"
  26. using namespace vgui;
  27. extern ConVar tf_arena_preround_time;
  28. #define TF_ARENA_NOTIFICATION_HIDETIME tf_arena_preround_time.GetFloat();
  29. //-----------------------------------------------------------------------------
  30. // Purpose:
  31. //-----------------------------------------------------------------------------
  32. class CHudArenaNotification : public CHudElement, public EditablePanel
  33. {
  34. DECLARE_CLASS_SIMPLE( CHudArenaNotification, EditablePanel );
  35. public:
  36. CHudArenaNotification( const char *pElementName );
  37. virtual void Init( void );
  38. virtual void OnTick( void );
  39. virtual void LevelInit( void );
  40. virtual void ApplySchemeSettings( IScheme *scheme );
  41. virtual bool ShouldDraw( void );
  42. virtual void SetVisible( bool state );
  43. virtual void FireGameEvent( IGameEvent * event );
  44. void SetupSwitchPanel( int iNotification );
  45. private:
  46. Label *m_pBalanceLabel;
  47. float m_flHideAt;
  48. CHudTeamSwitch *m_pTeamSwitchPanel;
  49. };
  50. DECLARE_HUDELEMENT( CHudArenaNotification );
  51. //-----------------------------------------------------------------------------
  52. // Purpose:
  53. //-----------------------------------------------------------------------------
  54. CHudArenaNotification::CHudArenaNotification( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudArenaNotification" )
  55. {
  56. Panel *pParent = g_pClientMode->GetViewport();
  57. SetParent( pParent );
  58. SetHiddenBits( HIDEHUD_MISCSTATUS );
  59. m_flHideAt = 0;
  60. vgui::ivgui()->AddTickSignal( GetVPanel(), 100 );
  61. m_pTeamSwitchPanel = NULL;
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose:
  65. //-----------------------------------------------------------------------------
  66. void CHudArenaNotification::Init( void )
  67. {
  68. // listen for events
  69. ListenForGameEvent( "arena_player_notification" );
  70. ListenForGameEvent( "arena_match_maxstreak" );
  71. ListenForGameEvent( "arena_round_start" );
  72. SetVisible( false );
  73. CHudElement::Init();
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Purpose:
  77. //-----------------------------------------------------------------------------
  78. void CHudArenaNotification::FireGameEvent( IGameEvent * event )
  79. {
  80. const char *pEventName = event->GetName();
  81. if ( Q_strcmp( "arena_player_notification", pEventName ) == 0 )
  82. {
  83. int iPlayer = event->GetInt( "player" );
  84. int iNotification = event->GetInt( "message" );
  85. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  86. if ( pPlayer && iPlayer == pPlayer->entindex() )
  87. {
  88. SetupSwitchPanel( iNotification );
  89. m_flHideAt = gpGlobals->curtime + TF_ARENA_NOTIFICATION_HIDETIME;
  90. SetVisible( true );
  91. if ( m_pTeamSwitchPanel == NULL )
  92. {
  93. m_pTeamSwitchPanel = GET_HUDELEMENT( CHudTeamSwitch );
  94. }
  95. }
  96. }
  97. else if ( Q_strcmp( "arena_match_maxstreak", pEventName ) == 0 )
  98. {
  99. int iTeam = event->GetInt( "team" );
  100. int iStreak = event->GetInt( "streak" );
  101. C_TFTeam *pTeam = GetGlobalTFTeam( iTeam );
  102. if ( !pTeam )
  103. return;
  104. CBaseHudChat *hudChat = (CBaseHudChat *)GET_HUDELEMENT( CHudChat );
  105. wchar_t wStreak[6];
  106. _snwprintf( wStreak, ARRAYSIZE( wStreak ), L"%i", iStreak );
  107. wchar_t wszLocalized[100];
  108. g_pVGuiLocalize->ConstructString_safe( wszLocalized, g_pVGuiLocalize->Find( "#TF_Arena_MaxStreak" ), 2, pTeam->Get_Localized_Name(), wStreak );
  109. char szLocalized[100];
  110. g_pVGuiLocalize->ConvertUnicodeToANSI( wszLocalized, szLocalized, sizeof(szLocalized) );
  111. hudChat->ChatPrintf( 0, CHAT_FILTER_NONE, "%c%s", COLOR_NORMAL, szLocalized );
  112. }
  113. else if ( Q_strcmp( "arena_round_start", pEventName ) == 0 )
  114. {
  115. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  116. if ( pPlayer )
  117. {
  118. pPlayer->EmitSound( "Ambient.Siren" );
  119. }
  120. }
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose:
  124. // Input : -
  125. //-----------------------------------------------------------------------------
  126. void CHudArenaNotification::OnTick( void )
  127. {
  128. if ( IsVisible() == true )
  129. {
  130. if ( m_flHideAt == 9999 )
  131. {
  132. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  133. if ( pPlayer && pPlayer->GetTeamNumber() > LAST_SHARED_TEAM )
  134. {
  135. if ( tf_arena_use_queue.GetBool() || TFGameRules()->State_Get() != GR_STATE_PREGAME )
  136. {
  137. m_flHideAt = 0.1f;
  138. }
  139. }
  140. }
  141. if ( ( m_flHideAt && m_flHideAt < gpGlobals->curtime ) || ( m_pTeamSwitchPanel && m_pTeamSwitchPanel->ShouldDraw() == true ) || TFGameRules()->InStalemate() == true || TFGameRules()->IsInArenaMode() == false )
  142. {
  143. SetVisible( false );
  144. m_flHideAt = 0;
  145. }
  146. }
  147. else
  148. {
  149. if ( TFGameRules() && TFGameRules()->IsInArenaMode() == true )
  150. {
  151. if ( TFGameRules()->State_Get() == GR_STATE_PREGAME )
  152. {
  153. SetupSwitchPanel( TF_ARENA_NOTIFICATION_NOPLAYERS );
  154. m_flHideAt = 9999;
  155. SetVisible( true );
  156. }
  157. }
  158. }
  159. }
  160. //-----------------------------------------------------------------------------
  161. // Purpose:
  162. //-----------------------------------------------------------------------------
  163. void CHudArenaNotification::SetVisible( bool state )
  164. {
  165. int iRenderGroup = gHUD.LookupRenderGroupIndexByName( "arena_target_id" );
  166. if ( state == true )
  167. {
  168. gHUD.LockRenderGroup( iRenderGroup );
  169. }
  170. else
  171. {
  172. gHUD.UnlockRenderGroup( iRenderGroup );
  173. }
  174. BaseClass::SetVisible( state );
  175. }
  176. //-----------------------------------------------------------------------------
  177. // Purpose:
  178. //-----------------------------------------------------------------------------
  179. void CHudArenaNotification::LevelInit( void )
  180. {
  181. m_flHideAt = 0;
  182. SetVisible( false );
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Purpose:
  186. //-----------------------------------------------------------------------------
  187. bool CHudArenaNotification::ShouldDraw( void )
  188. {
  189. return ( IsVisible() );
  190. }
  191. //-----------------------------------------------------------------------------
  192. // Purpose:
  193. //-----------------------------------------------------------------------------
  194. void CHudArenaNotification::ApplySchemeSettings( IScheme *pScheme )
  195. {
  196. // load control settings...
  197. LoadControlSettings( "resource/UI/HudArenaNotification.res" );
  198. BaseClass::ApplySchemeSettings( pScheme );
  199. m_pBalanceLabel = dynamic_cast<Label *>( FindChildByName("BalanceLabel") );
  200. SetVisible( false );
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Purpose:
  204. //-----------------------------------------------------------------------------
  205. void CHudArenaNotification::SetupSwitchPanel( int iNotification )
  206. {
  207. if ( m_pBalanceLabel )
  208. {
  209. SetDialogVariable( "notificationtip", g_pVGuiLocalize->Find( "#TF_Arena_ProTip" ) );
  210. if ( iNotification == TF_ARENA_NOTIFICATION_CAREFUL )
  211. {
  212. m_pBalanceLabel->SetText( g_pVGuiLocalize->Find( "#TF_Arena_Careful" ) );
  213. }
  214. else if ( iNotification == TF_ARENA_NOTIFICATION_SITOUT )
  215. {
  216. m_pBalanceLabel->SetText( g_pVGuiLocalize->Find( "#TF_Arena_SitOut" ) );
  217. }
  218. else if ( iNotification == TF_ARENA_NOTIFICATION_NOPLAYERS )
  219. {
  220. m_pBalanceLabel->SetText( g_pVGuiLocalize->Find( "#TF_Arena_Welcome" ) );
  221. SetDialogVariable( "notificationtip", g_pVGuiLocalize->Find( "#TF_Arena_NoPlayers" ) );
  222. }
  223. }
  224. }
  225. //----------------------------------------------------------------------------------------------------------------
  226. // Receive the PlayerIgnitedInv user message and send out a clientside event for achievements to hook.
  227. void __MsgFunc_HudArenaNotify( bf_read &msg )
  228. {
  229. int iPlayerIndex = (int) msg.ReadByte();
  230. int iNotification = (int) msg.ReadByte();
  231. IGameEvent *event = gameeventmanager->CreateEvent( "arena_player_notification" );
  232. if ( event )
  233. {
  234. event->SetInt( "player", iPlayerIndex );
  235. event->SetInt( "message", iNotification );
  236. gameeventmanager->FireEventClientSide( event );
  237. }
  238. }