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.

285 lines
7.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "dodteammenu.h"
  9. #include <convar.h>
  10. #include "hud.h" // for gEngfuncs
  11. #include "c_dod_player.h"
  12. #include "dod_gamerules.h"
  13. #include <vgui/ILocalize.h>
  14. #include <vgui/IVGui.h>
  15. #include <vgui_controls/RichText.h>
  16. #include "c_dod_team.h"
  17. #include "IGameUIFuncs.h" // for key bindings
  18. extern IGameUIFuncs *gameuifuncs; // for key binding details
  19. using namespace vgui;
  20. //-----------------------------------------------------------------------------
  21. // Purpose: Constructor
  22. //-----------------------------------------------------------------------------
  23. CDODTeamMenu::CDODTeamMenu(IViewPort *pViewPort) : CTeamMenu(pViewPort)
  24. {
  25. m_pBackground = SETUP_PANEL( new CDODMenuBackground( this ) );
  26. m_pPanel = new EditablePanel( this, "TeamImagePanel" );// team image panel
  27. m_pFirstButton = NULL;
  28. LoadControlSettings("Resource/UI/TeamMenu.res"); // reload this to catch DODButtons
  29. vgui::ivgui()->AddTickSignal( GetVPanel() );
  30. m_iActiveTeam = TEAM_UNASSIGNED;
  31. m_iLastPlayerCount = -1;
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Purpose: Destructor
  35. //-----------------------------------------------------------------------------
  36. CDODTeamMenu::~CDODTeamMenu()
  37. {
  38. }
  39. void CDODTeamMenu::ShowPanel(bool bShow)
  40. {
  41. if ( bShow )
  42. {
  43. engine->CheckPoint( "TeamMenu" ); //MATTTODO what is this?
  44. m_iTeamMenuKey = gameuifuncs->GetButtonCodeForBind( "changeteam" );
  45. }
  46. BaseClass::ShowPanel( bShow );
  47. }
  48. //-----------------------------------------------------------------------------
  49. // Purpose: Make the first buttons page get displayed when the menu becomes visible
  50. //-----------------------------------------------------------------------------
  51. void CDODTeamMenu::SetVisible( bool state )
  52. {
  53. BaseClass::SetVisible( state );
  54. for( int i = 0; i< GetChildCount(); i++ ) // get all the buy buttons to performlayout
  55. {
  56. CDODMouseOverButton<EditablePanel> *button = dynamic_cast<CDODMouseOverButton<EditablePanel> *>(GetChild(i));
  57. if ( button )
  58. {
  59. if( button == m_pFirstButton && state == true )
  60. button->ShowPage();
  61. else
  62. button->HidePage();
  63. button->InvalidateLayout();
  64. }
  65. }
  66. if ( state )
  67. {
  68. Panel *pAutoButton = FindChildByName( "autobutton" );
  69. if ( pAutoButton )
  70. {
  71. pAutoButton->RequestFocus();
  72. }
  73. }
  74. }
  75. void CDODTeamMenu::OnTick( void )
  76. {
  77. C_DODTeam *pAllies = dynamic_cast<C_DODTeam *>( GetGlobalTeam(TEAM_ALLIES) );
  78. C_DODTeam *pAxis = dynamic_cast<C_DODTeam *>( GetGlobalTeam(TEAM_AXIS) );
  79. if ( !pAllies || !pAxis )
  80. return;
  81. static int iLastAlliesCount = -1;
  82. static int iLastAxisCount = -1;
  83. int iNumAllies = pAllies->Get_Number_Players();
  84. int iNumAxis = pAxis->Get_Number_Players();
  85. if ( iNumAllies != iLastAlliesCount )
  86. {
  87. iLastAlliesCount = iNumAllies;
  88. wchar_t wbuf[128];
  89. if ( iNumAllies == 1 )
  90. {
  91. g_pVGuiLocalize->ConstructString( wbuf, sizeof(wbuf), g_pVGuiLocalize->Find("#teammenu_numAllies_1"), 0 );
  92. }
  93. else
  94. {
  95. wchar_t wnum[6];
  96. _snwprintf( wnum, ARRAYSIZE(wnum), L"%d", iNumAllies );
  97. g_pVGuiLocalize->ConstructString( wbuf, sizeof(wbuf), g_pVGuiLocalize->Find("#teammenu_numAllies"), 1, wnum );
  98. }
  99. Label *pLabel = dynamic_cast<Label *>( FindChildByName("num_allies") );
  100. if ( pLabel )
  101. pLabel->SetText( wbuf );
  102. }
  103. if ( iNumAxis != iLastAxisCount )
  104. {
  105. iLastAxisCount = iNumAxis;
  106. wchar_t wbuf[128];
  107. if ( iNumAxis == 1 )
  108. {
  109. g_pVGuiLocalize->ConstructString( wbuf, sizeof(wbuf), g_pVGuiLocalize->Find("#teammenu_numAxis_1"), 0 );
  110. }
  111. else
  112. {
  113. wchar_t wnum[6];
  114. _snwprintf( wnum, ARRAYSIZE(wnum), L"%d", iNumAxis );
  115. g_pVGuiLocalize->ConstructString( wbuf, sizeof(wbuf), g_pVGuiLocalize->Find("#teammenu_numAxis"), 1, wnum );
  116. }
  117. Label *pLabel = dynamic_cast<Label *>( FindChildByName("num_axis") );
  118. if ( pLabel )
  119. pLabel->SetText( wbuf );
  120. }
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose: called to update the menu with new information
  124. //-----------------------------------------------------------------------------
  125. void CDODTeamMenu::Update( void )
  126. {
  127. BaseClass::Update();
  128. C_DODPlayer *pPlayer = C_DODPlayer::GetLocalDODPlayer();
  129. Assert( pPlayer );
  130. const ConVar *allowspecs = cvar->FindVar( "mp_allowspectators" );
  131. if ( allowspecs && allowspecs->GetBool() )
  132. {
  133. if ( !pPlayer || !DODGameRules() )
  134. return;
  135. SetVisibleButton("specbutton", true);
  136. }
  137. else
  138. {
  139. SetVisibleButton("specbutton", false );
  140. }
  141. if( pPlayer->GetTeamNumber() == TEAM_UNASSIGNED ) // we aren't on a team yet
  142. {
  143. SetVisibleButton("CancelButton", false);
  144. }
  145. else
  146. {
  147. SetVisibleButton("CancelButton", true);
  148. }
  149. }
  150. //-----------------------------------------------------------------------------
  151. // Purpose: When a team button is pressed it triggers this function to
  152. // cause the player to join a team
  153. //-----------------------------------------------------------------------------
  154. void CDODTeamMenu::OnCommand( const char *command )
  155. {
  156. if ( !FStrEq( command, "vguicancel" ) )
  157. {
  158. engine->ClientCmd( command );
  159. }
  160. BaseClass::OnCommand( command );
  161. gViewPortInterface->ShowBackGround( false );
  162. OnClose();
  163. }
  164. //-----------------------------------------------------------------------------
  165. // Purpose: Sets the visibility of a button by name
  166. //-----------------------------------------------------------------------------
  167. void CDODTeamMenu::SetVisibleButton(const char *textEntryName, bool state)
  168. {
  169. Button *entry = dynamic_cast<Button *>(FindChildByName(textEntryName));
  170. if (entry)
  171. {
  172. entry->SetVisible(state);
  173. }
  174. }
  175. void CDODTeamMenu::ApplySchemeSettings( IScheme *pScheme )
  176. {
  177. BaseClass::ApplySchemeSettings(pScheme);
  178. }
  179. //-----------------------------------------------------------------------------
  180. // Draw nothing
  181. //-----------------------------------------------------------------------------
  182. void CDODTeamMenu::PaintBackground( void )
  183. {
  184. }
  185. //-----------------------------------------------------------------------------
  186. // Purpose:
  187. //-----------------------------------------------------------------------------
  188. Panel *CDODTeamMenu::CreateControlByName( const char *controlName )
  189. {
  190. if( !Q_stricmp( "DODMouseOverPanelButton", controlName ) )
  191. {
  192. CDODMouseOverButton<EditablePanel> *newButton = new CDODMouseOverButton<EditablePanel>( this, NULL, m_pPanel );
  193. if( !m_pFirstButton )
  194. {
  195. m_pFirstButton = newButton;
  196. }
  197. return newButton;
  198. }
  199. else if( !Q_stricmp( "DODButton", controlName ) )
  200. {
  201. return new CDODButton(this);
  202. }
  203. else if ( !Q_stricmp( "CIconPanel", controlName ) )
  204. {
  205. return new CIconPanel(this, "icon_panel");
  206. }
  207. else
  208. {
  209. return BaseClass::CreateControlByName( controlName );
  210. }
  211. }
  212. void CDODTeamMenu::OnShowPage( char const *pagename )
  213. {
  214. if ( !pagename || !pagename[ 0 ] )
  215. return;
  216. if ( !Q_stricmp( pagename, "allies") )
  217. {
  218. m_iActiveTeam = TEAM_ALLIES;
  219. }
  220. else if ( !Q_stricmp( pagename, "axis" ) )
  221. {
  222. m_iActiveTeam = TEAM_AXIS;
  223. }
  224. }
  225. void CDODTeamMenu::OnKeyCodePressed(KeyCode code)
  226. {
  227. if ( m_iTeamMenuKey != BUTTON_CODE_INVALID && m_iTeamMenuKey == code )
  228. {
  229. ShowPanel( false );
  230. }
  231. else
  232. {
  233. BaseClass::OnKeyCodePressed( code );
  234. }
  235. }