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.

246 lines
6.4 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. // 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 CHudTeamGoal : public CHudElement, public EditablePanel
  28. {
  29. DECLARE_CLASS_SIMPLE( CHudTeamGoal, EditablePanel );
  30. public:
  31. CHudTeamGoal( const char *pElementName );
  32. virtual void LevelInit( void );
  33. virtual void ApplySchemeSettings( IScheme *scheme );
  34. virtual bool ShouldDraw( void );
  35. void SetupGoalPanel( const char *pszGoal );
  36. private:
  37. Label *m_pSwitchLabel;
  38. Label *m_pGoalLabel;
  39. CTFImagePanel *m_pGoalImage;
  40. float m_flHideAt;
  41. int m_iGoalLabelOrgY;
  42. };
  43. DECLARE_HUDELEMENT( CHudTeamGoal );
  44. //-----------------------------------------------------------------------------
  45. // Purpose:
  46. //-----------------------------------------------------------------------------
  47. CHudTeamGoal::CHudTeamGoal( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudTeamGoal" )
  48. {
  49. Panel *pParent = g_pClientMode->GetViewport();
  50. SetParent( pParent );
  51. SetHiddenBits( HIDEHUD_MISCSTATUS );
  52. m_flHideAt = 0;
  53. m_iGoalLabelOrgY = 0;
  54. RegisterForRenderGroup( "commentary" );
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose:
  58. //-----------------------------------------------------------------------------
  59. void CHudTeamGoal::LevelInit( void )
  60. {
  61. m_flHideAt = 0;
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose:
  65. //-----------------------------------------------------------------------------
  66. void CHudTeamGoal::ApplySchemeSettings( IScheme *pScheme )
  67. {
  68. // load control settings...
  69. LoadControlSettings( "resource/UI/HudTeamGoal.res" );
  70. BaseClass::ApplySchemeSettings( pScheme );
  71. m_pSwitchLabel = dynamic_cast<Label *>( FindChildByName("SwitchLabel") );
  72. m_pGoalLabel = dynamic_cast<Label *>( FindChildByName("GoalLabel") );
  73. m_pGoalImage = dynamic_cast<CTFImagePanel *>( FindChildByName("GoalImage") );
  74. if ( m_pGoalLabel )
  75. {
  76. int iIgnored;
  77. m_pGoalLabel->GetPos( iIgnored, m_iGoalLabelOrgY );
  78. }
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose:
  82. //-----------------------------------------------------------------------------
  83. bool CHudTeamGoal::ShouldDraw( void )
  84. {
  85. if ( !TFGameRules() )
  86. return false;
  87. bool bCouldSee = TFGameRules()->ShouldShowTeamGoal();
  88. if ( TFGameRules()->IsInTournamentMode() )
  89. {
  90. bCouldSee = false;
  91. }
  92. if ( m_flHideAt && m_flHideAt < gpGlobals->curtime )
  93. {
  94. if ( !bCouldSee )
  95. {
  96. m_flHideAt = 0;
  97. }
  98. return false;
  99. }
  100. if ( bCouldSee )
  101. {
  102. C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();
  103. if ( pPlayer && pPlayer->IsAlive() && pPlayer->GetTeamNumber() >= FIRST_GAME_TEAM )
  104. {
  105. const char *pszGoal = TFGameRules()->GetTeamGoalString( pPlayer->GetTeamNumber() );
  106. if ( pszGoal && pszGoal[0] && CHudElement::ShouldDraw() )
  107. {
  108. if ( !IsVisible() )
  109. {
  110. // Once we've played a map 15 times, don't show team goals anymore.
  111. if ( UTIL_GetMapKeyCount( "viewed" ) > 15 )
  112. {
  113. m_flHideAt = -1; // To prevent it rechecking until next level load
  114. return false;
  115. }
  116. SetupGoalPanel( pszGoal );
  117. // Show for 15 seconds
  118. m_flHideAt = gpGlobals->curtime + 15.0;
  119. }
  120. // Don't appear if the team switch alert is there
  121. CHudElement *pHudSwitch = gHUD.FindElement( "CHudTeamSwitch" );
  122. if ( pHudSwitch && pHudSwitch->ShouldDraw() )
  123. return false;
  124. return true;
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130. const char *pszTeamRoleIcons[NUM_TEAM_ROLES] =
  131. {
  132. "../hud/hud_icon_capture", // TEAM_ROLE_NONE = 0,
  133. "../hud/hud_icon_defend", // TEAM_ROLE_DEFENDERS,
  134. "../hud/hud_icon_attack", // TEAM_ROLE_ATTACKERS,
  135. };
  136. const char *pszTeamRoleSwitch[NUM_TEAM_ROLES] =
  137. {
  138. " ", // TEAM_ROLE_NONE = 0,
  139. "#TF_teamswitch_defenders", // TEAM_ROLE_DEFENDERS,
  140. "#TF_teamswitch_attackers", // TEAM_ROLE_ATTACKERS,
  141. };
  142. //-----------------------------------------------------------------------------
  143. // Purpose:
  144. //-----------------------------------------------------------------------------
  145. void CHudTeamGoal::SetupGoalPanel( const char *pszGoal )
  146. {
  147. if ( m_pGoalLabel )
  148. {
  149. wchar_t *pszLocalizedGoal = g_pVGuiLocalize->Find( pszGoal );
  150. if ( pszLocalizedGoal )
  151. {
  152. m_pGoalLabel->SetText( pszLocalizedGoal );
  153. }
  154. else
  155. {
  156. m_pGoalLabel->SetText( pszGoal );
  157. }
  158. }
  159. if ( m_pSwitchLabel )
  160. {
  161. m_pSwitchLabel->SetVisible( false );
  162. }
  163. C_TFTeam *pLocalTeam = GetGlobalTFTeam( GetLocalPlayerTeam() );
  164. if ( pLocalTeam )
  165. {
  166. //=============================================================================
  167. // HPE_BEGIN:
  168. // [msmith] If we're in training, we want to use a different icon here.
  169. //=============================================================================
  170. if ( TFGameRules()->IsInTraining() )
  171. {
  172. m_pGoalImage->SetImage( "../hud/hud_icon_training" );
  173. }
  174. //=============================================================================
  175. // HPE_END
  176. //=============================================================================
  177. else
  178. {
  179. int iRole = pLocalTeam->GetRole();
  180. if ( iRole >= 0 && iRole < NUM_TEAM_ROLES )
  181. {
  182. m_pGoalImage->SetImage( pszTeamRoleIcons[iRole] );
  183. if ( m_pSwitchLabel )
  184. {
  185. if ( TFGameRules() && TFGameRules()->SwitchedTeamsThisRound() )
  186. {
  187. m_pSwitchLabel->SetText( g_pVGuiLocalize->Find( pszTeamRoleSwitch[iRole] ) );
  188. m_pSwitchLabel->SetVisible( true );
  189. }
  190. }
  191. }
  192. }
  193. }
  194. if ( m_pGoalLabel && m_pSwitchLabel )
  195. {
  196. // If the switch label is invisible, move the goal label up to where it is.
  197. int iX, iY, iSwitchY, iIgnored;
  198. m_pGoalLabel->GetPos( iX, iY );
  199. m_pSwitchLabel->GetPos( iIgnored, iSwitchY );
  200. if ( m_pSwitchLabel->IsVisible() )
  201. {
  202. m_pGoalLabel->SetPos( iX, m_iGoalLabelOrgY );
  203. }
  204. else
  205. {
  206. m_pGoalLabel->SetPos( iX, iSwitchY );
  207. }
  208. }
  209. }