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.

319 lines
7.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include <stdio.h>
  9. #include <cdll_client_int.h>
  10. #include "classmenu.h"
  11. #include <vgui/IScheme.h>
  12. #include <vgui/ILocalize.h>
  13. #include <vgui/ISurface.h>
  14. #include <KeyValues.h>
  15. #include <vgui_controls/ImageList.h>
  16. #include <filesystem.h>
  17. #include <vgui_controls/TextEntry.h>
  18. #include <vgui_controls/Button.h>
  19. #include <vgui_controls/Panel.h>
  20. #include "inputsystem/iinputsystem.h"
  21. #include "cdll_util.h"
  22. #include "IGameUIFuncs.h" // for key bindings
  23. #ifndef _XBOX
  24. extern IGameUIFuncs *gameuifuncs; // for key binding details
  25. #endif
  26. #include <game/client/iviewport.h>
  27. #include <stdlib.h> // MAX_PATH define
  28. // memdbgon must be the last include file in a .cpp file!!!
  29. #include "tier0/memdbgon.h"
  30. using namespace vgui;
  31. #ifdef TF_CLIENT_DLL
  32. #define HUD_CLASSAUTOKILL_FLAGS ( FCVAR_CLIENTDLL | FCVAR_ARCHIVE | FCVAR_USERINFO )
  33. #else
  34. #define HUD_CLASSAUTOKILL_FLAGS ( FCVAR_CLIENTDLL | FCVAR_ARCHIVE )
  35. #endif // !TF_CLIENT_DLL
  36. ConVar hud_classautokill( "hud_classautokill", "1", HUD_CLASSAUTOKILL_FLAGS, "Automatically kill player after choosing a new playerclass." );
  37. //-----------------------------------------------------------------------------
  38. // Purpose: Constructor
  39. //-----------------------------------------------------------------------------
  40. CClassMenu::CClassMenu(IViewPort *pViewPort) : Frame(NULL, PANEL_CLASS)
  41. {
  42. m_pViewPort = pViewPort;
  43. m_iScoreBoardKey = BUTTON_CODE_INVALID; // this is looked up in Activate()
  44. m_iTeam = 0;
  45. // initialize dialog
  46. SetTitle("", true);
  47. // load the new scheme early!!
  48. SetScheme("ClientScheme");
  49. SetMoveable(false);
  50. SetSizeable(false);
  51. // hide the system buttons
  52. SetTitleBarVisible( false );
  53. SetProportional(true);
  54. // info window about this class
  55. m_pPanel = new EditablePanel( this, "ClassInfo" );
  56. LoadControlSettings( "Resource/UI/ClassMenu.res" );
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Purpose: Constructor
  60. //-----------------------------------------------------------------------------
  61. CClassMenu::CClassMenu(IViewPort *pViewPort, const char *panelName) : Frame(NULL, panelName)
  62. {
  63. m_pViewPort = pViewPort;
  64. m_iScoreBoardKey = BUTTON_CODE_INVALID; // this is looked up in Activate()
  65. m_iTeam = 0;
  66. // initialize dialog
  67. SetTitle("", true);
  68. // load the new scheme early!!
  69. SetScheme("ClientScheme");
  70. SetMoveable(false);
  71. SetSizeable(false);
  72. // hide the system buttons
  73. SetTitleBarVisible( false );
  74. SetProportional(true);
  75. // info window about this class
  76. m_pPanel = new EditablePanel( this, "ClassInfo" );
  77. // Inheriting classes are responsible for calling LoadControlSettings()!
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose: Destructor
  81. //-----------------------------------------------------------------------------
  82. CClassMenu::~CClassMenu()
  83. {
  84. }
  85. MouseOverPanelButton* CClassMenu::CreateNewMouseOverPanelButton(EditablePanel *panel)
  86. {
  87. return new MouseOverPanelButton(this, "MouseOverPanelButton", panel);
  88. }
  89. Panel *CClassMenu::CreateControlByName(const char *controlName)
  90. {
  91. if( !Q_stricmp( "MouseOverPanelButton", controlName ) )
  92. {
  93. MouseOverPanelButton *newButton = CreateNewMouseOverPanelButton( m_pPanel );
  94. m_mouseoverButtons.AddToTail( newButton );
  95. return newButton;
  96. }
  97. else
  98. {
  99. return BaseClass::CreateControlByName( controlName );
  100. }
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose:
  104. //-----------------------------------------------------------------------------
  105. void CClassMenu::Reset()
  106. {
  107. for ( int i = 0 ; i < GetChildCount() ; ++i )
  108. {
  109. // Hide the subpanel for the MouseOverPanelButtons
  110. MouseOverPanelButton *pPanel = dynamic_cast<MouseOverPanelButton *>( GetChild( i ) );
  111. if ( pPanel )
  112. {
  113. pPanel->HidePage();
  114. }
  115. }
  116. // Turn the first button back on again (so we have a default description shown)
  117. Assert( m_mouseoverButtons.Count() );
  118. for ( int i=0; i<m_mouseoverButtons.Count(); ++i )
  119. {
  120. if ( i == 0 )
  121. {
  122. m_mouseoverButtons[i]->ShowPage(); // Show the first page
  123. }
  124. else
  125. {
  126. m_mouseoverButtons[i]->HidePage(); // Hide the rest
  127. }
  128. }
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Purpose: Called when the user picks a class
  132. //-----------------------------------------------------------------------------
  133. void CClassMenu::OnCommand( const char *command )
  134. {
  135. if ( Q_stricmp( command, "vguicancel" ) )
  136. {
  137. engine->ClientCmd( const_cast<char *>( command ) );
  138. #if !defined( CSTRIKE_DLL ) && !defined( TF_CLIENT_DLL )
  139. // They entered a command to change their class, kill them so they spawn with
  140. // the new class right away
  141. if ( hud_classautokill.GetBool() )
  142. {
  143. engine->ClientCmd( "kill" );
  144. }
  145. #endif // !CSTRIKE_DLL && !TF_CLIENT_DLL
  146. }
  147. Close();
  148. gViewPortInterface->ShowBackGround( false );
  149. BaseClass::OnCommand( command );
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Purpose: shows the class menu
  153. //-----------------------------------------------------------------------------
  154. void CClassMenu::ShowPanel(bool bShow)
  155. {
  156. if ( bShow )
  157. {
  158. Activate();
  159. SetMouseInputEnabled( true );
  160. // load a default class page
  161. for ( int i=0; i<m_mouseoverButtons.Count(); ++i )
  162. {
  163. if ( i == 0 )
  164. {
  165. m_mouseoverButtons[i]->ShowPage(); // Show the first page
  166. }
  167. else
  168. {
  169. m_mouseoverButtons[i]->HidePage(); // Hide the rest
  170. }
  171. }
  172. if ( m_iScoreBoardKey == BUTTON_CODE_INVALID )
  173. {
  174. m_iScoreBoardKey = gameuifuncs->GetButtonCodeForBind( "showscores" );
  175. }
  176. }
  177. else
  178. {
  179. SetVisible( false );
  180. SetMouseInputEnabled( false );
  181. }
  182. m_pViewPort->ShowBackGround( bShow );
  183. }
  184. void CClassMenu::SetData(KeyValues *data)
  185. {
  186. m_iTeam = data->GetInt( "team" );
  187. }
  188. //-----------------------------------------------------------------------------
  189. // Purpose: Sets the text of a control by name
  190. //-----------------------------------------------------------------------------
  191. void CClassMenu::SetLabelText(const char *textEntryName, const char *text)
  192. {
  193. Label *entry = dynamic_cast<Label *>(FindChildByName(textEntryName));
  194. if (entry)
  195. {
  196. entry->SetText(text);
  197. }
  198. }
  199. //-----------------------------------------------------------------------------
  200. // Purpose: Sets the visibility of a button by name
  201. //-----------------------------------------------------------------------------
  202. void CClassMenu::SetVisibleButton(const char *textEntryName, bool state)
  203. {
  204. Button *entry = dynamic_cast<Button *>(FindChildByName(textEntryName));
  205. if (entry)
  206. {
  207. entry->SetVisible(state);
  208. }
  209. }
  210. void CClassMenu::OnKeyCodePressed(KeyCode code)
  211. {
  212. int nDir = 0;
  213. switch ( code )
  214. {
  215. case KEY_XBUTTON_UP:
  216. case KEY_XSTICK1_UP:
  217. case KEY_XSTICK2_UP:
  218. case KEY_UP:
  219. case KEY_XBUTTON_LEFT:
  220. case KEY_XSTICK1_LEFT:
  221. case KEY_XSTICK2_LEFT:
  222. case KEY_LEFT:
  223. case STEAMCONTROLLER_DPAD_LEFT:
  224. nDir = -1;
  225. break;
  226. case KEY_XBUTTON_DOWN:
  227. case KEY_XSTICK1_DOWN:
  228. case KEY_XSTICK2_DOWN:
  229. case KEY_DOWN:
  230. case KEY_XBUTTON_RIGHT:
  231. case KEY_XSTICK1_RIGHT:
  232. case KEY_XSTICK2_RIGHT:
  233. case KEY_RIGHT:
  234. case STEAMCONTROLLER_DPAD_RIGHT:
  235. nDir = 1;
  236. break;
  237. }
  238. if ( m_iScoreBoardKey != BUTTON_CODE_INVALID && m_iScoreBoardKey == code )
  239. {
  240. gViewPortInterface->ShowPanel( PANEL_SCOREBOARD, true );
  241. gViewPortInterface->PostMessageToPanel( PANEL_SCOREBOARD, new KeyValues( "PollHideCode", "code", code ) );
  242. }
  243. else if ( nDir != 0 )
  244. {
  245. CUtlSortVector< SortedPanel_t, CSortedPanelYLess > vecSortedButtons;
  246. VguiPanelGetSortedChildButtonList( this, (void*)&vecSortedButtons, "&", 0 );
  247. int nNewArmed = VguiPanelNavigateSortedChildButtonList( (void*)&vecSortedButtons, nDir );
  248. if ( nNewArmed != -1 )
  249. {
  250. // Handled!
  251. if ( nNewArmed < m_mouseoverButtons.Count() )
  252. {
  253. m_mouseoverButtons[ nNewArmed ]->OnCursorEntered();
  254. }
  255. return;
  256. }
  257. }
  258. else
  259. {
  260. BaseClass::OnKeyCodePressed( code );
  261. }
  262. }