Counter Strike : Global Offensive Source Code
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.

1081 lines
33 KiB

  1. //===== Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: Implements all the functions exported by the GameUI dll
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #if defined( _WIN32 ) && !defined( _X360 )
  8. #include <windows.h>
  9. #endif
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <stdio.h>
  13. #if defined( WIN32 )
  14. #include <io.h>
  15. #include <direct.h>
  16. #endif
  17. #include <tier0/dbg.h>
  18. #ifdef SendMessage
  19. #undef SendMessage
  20. #endif
  21. #include "FileSystem.h"
  22. #include "GameUI_Interface.h"
  23. #include "Sys_Utils.h"
  24. #include "string.h"
  25. #include "tier0/icommandline.h"
  26. // interface to engine
  27. #include "EngineInterface.h"
  28. #include "VGuiSystemModuleLoader.h"
  29. #include "bitmap/TGALoader.h"
  30. #include "GameConsole.h"
  31. #include "LoadingDialog.h"
  32. #include "CDKeyEntryDialog.h"
  33. #include "ModInfo.h"
  34. #include "game/client/IGameClientExports.h"
  35. #include "materialsystem/imaterialsystem.h"
  36. #include "matchmaking/imatchframework.h"
  37. #include "ixboxsystem.h"
  38. #include "IGameUIFuncs.h"
  39. #include "IEngineVGUI.h"
  40. // vgui2 interface
  41. // note that GameUI project uses ..\vgui2\include, not ..\utils\vgui\include
  42. #include "vgui/Cursor.h"
  43. #include "tier1/KeyValues.h"
  44. #include "vgui/ILocalize.h"
  45. #include "vgui/IPanel.h"
  46. #include "vgui/IScheme.h"
  47. #include "vgui/IVGui.h"
  48. #include "vgui/ISystem.h"
  49. #include "vgui/ISurface.h"
  50. #include "vgui_controls/Menu.h"
  51. #include "vgui_controls/PHandle.h"
  52. #include "tier3/tier3.h"
  53. #include "matsys_controls/matsyscontrols.h"
  54. #ifndef NO_STEAM
  55. #include "steam/steam_api.h"
  56. #endif
  57. #include "protocol.h"
  58. #if !defined( NO_STEAM ) && !defined( NO_STEAM_GAMECOORDINATOR )
  59. #include "econ_ui.h"
  60. #endif
  61. #if defined( SWARM_DLL )
  62. #include "swarm/basemodpanel.h"
  63. #include "swarm/basemodui.h"
  64. typedef BaseModUI::CBaseModPanel UI_BASEMOD_PANEL_CLASS;
  65. inline UI_BASEMOD_PANEL_CLASS & GetUiBaseModPanelClass() { return UI_BASEMOD_PANEL_CLASS::GetSingleton(); }
  66. inline UI_BASEMOD_PANEL_CLASS & ConstructUiBaseModPanelClass() { return * new UI_BASEMOD_PANEL_CLASS(); }
  67. class IMatchExtSwarm *g_pMatchExt = NULL;
  68. #elif defined( PORTAL2 )
  69. #include "portal2/basemodpanel.h"
  70. #include "portal2/basemodui.h"
  71. typedef BaseModUI::CBaseModPanel UI_BASEMOD_PANEL_CLASS;
  72. inline UI_BASEMOD_PANEL_CLASS & GetUiBaseModPanelClass() { return UI_BASEMOD_PANEL_CLASS::GetSingleton(); }
  73. inline UI_BASEMOD_PANEL_CLASS & ConstructUiBaseModPanelClass() { return * new UI_BASEMOD_PANEL_CLASS(); }
  74. class IMatchExtPortal2 *g_pMatchExt = NULL;
  75. #elif defined( PORTAL2_UITEST_DLL )
  76. #include "portal2uitest/basemodpanel.h"
  77. #include "portal2uitest/basemodui.h"
  78. typedef BaseModUI::CBaseModPanel UI_BASEMOD_PANEL_CLASS;
  79. inline UI_BASEMOD_PANEL_CLASS & GetUiBaseModPanelClass() { return UI_BASEMOD_PANEL_CLASS::GetSingleton(); }
  80. inline UI_BASEMOD_PANEL_CLASS & ConstructUiBaseModPanelClass() { return * new UI_BASEMOD_PANEL_CLASS(); }
  81. IMatchExtPortal2 g_MatchExtPortal2;
  82. class IMatchExtPortal2 *g_pMatchExtPortal2 = &g_MatchExtPortal2;
  83. #else
  84. #include "BasePanel.h"
  85. typedef CBasePanel UI_BASEMOD_PANEL_CLASS;
  86. inline UI_BASEMOD_PANEL_CLASS & GetUiBaseModPanelClass() { return *BasePanel(); }
  87. inline UI_BASEMOD_PANEL_CLASS & ConstructUiBaseModPanelClass() { return *BasePanelSingleton(); }
  88. #endif
  89. #ifdef _X360
  90. #include "xbox/xbox_win32stubs.h"
  91. #endif // _GAMECONSOLE
  92. #ifdef _PS3
  93. #include "ps3/ps3_core.h"
  94. #include "ps3/ps3_win32stubs.h"
  95. #include "ps3/saverestore_ps3_api_ui.h"
  96. #endif // _GAMECONSOLE
  97. #include "tier0/dbg.h"
  98. #include "engine/IEngineSound.h"
  99. #include "gameui_util.h"
  100. // memdbgon must be the last include file in a .cpp file!!!
  101. #include "tier0/memdbgon.h"
  102. IEngineVGui *enginevguifuncs = NULL;
  103. #ifdef _X360
  104. IXOnline *xonline = NULL; // 360 only
  105. #elif defined( _PS3 )
  106. IPS3SaveRestoreToUI *ps3saveuiapi = NULL;
  107. #endif
  108. vgui::ISurface *enginesurfacefuncs = NULL;
  109. class CGameUI;
  110. CGameUI *g_pGameUI = NULL;
  111. class CLoadingDialog;
  112. vgui::DHANDLE<CLoadingDialog> g_hLoadingDialog;
  113. vgui::VPANEL g_hLoadingBackgroundDialog = NULL;
  114. static CGameUI g_GameUI;
  115. extern void VGui_ClearTransitionVideoPanels();
  116. extern bool VGui_IsPlayingFullScreenVideo();
  117. static IGameClientExports *g_pGameClientExports = NULL;
  118. IGameClientExports *GameClientExports()
  119. {
  120. return g_pGameClientExports;
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose: singleton accessor
  124. //-----------------------------------------------------------------------------
  125. CGameUI &GameUI()
  126. {
  127. return g_GameUI;
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose: hack function to give the module loader access to the main panel handle
  131. // only used in VguiSystemModuleLoader
  132. //-----------------------------------------------------------------------------
  133. vgui::VPANEL GetGameUIBasePanel()
  134. {
  135. return GetUiBaseModPanelClass().GetVPanel();
  136. }
  137. EXPOSE_SINGLE_INTERFACE_GLOBALVAR(CGameUI, IGameUI, GAMEUI_INTERFACE_VERSION, g_GameUI);
  138. //-----------------------------------------------------------------------------
  139. // Purpose: Constructor
  140. //-----------------------------------------------------------------------------
  141. CGameUI::CGameUI()
  142. {
  143. g_pGameUI = this;
  144. m_bTryingToLoadFriends = false;
  145. m_iFriendsLoadPauseFrames = 0;
  146. m_iGameIP = 0;
  147. m_iGameConnectionPort = 0;
  148. m_iGameQueryPort = 0;
  149. m_bActivatedUI = false;
  150. m_szPreviousStatusText[0] = 0;
  151. m_bIsConsoleUI = false;
  152. m_bHasSavedThisMenuSession = false;
  153. m_bOpenProgressOnStart = false;
  154. m_iPlayGameStartupSound = 0;
  155. }
  156. //-----------------------------------------------------------------------------
  157. // Purpose: Destructor
  158. //-----------------------------------------------------------------------------
  159. CGameUI::~CGameUI()
  160. {
  161. g_pGameUI = NULL;
  162. }
  163. //-----------------------------------------------------------------------------
  164. // Purpose: Initialization
  165. //-----------------------------------------------------------------------------
  166. void CGameUI::Initialize( CreateInterfaceFn factory )
  167. {
  168. MEM_ALLOC_CREDIT();
  169. ConnectTier1Libraries( &factory, 1 );
  170. ConnectTier2Libraries( &factory, 1 );
  171. ConVar_Register( FCVAR_CLIENTDLL );
  172. ConnectTier3Libraries( &factory, 1 );
  173. enginesound = (IEngineSound *)factory(IENGINESOUND_CLIENT_INTERFACE_VERSION, NULL);
  174. engine = (IVEngineClient *)factory( VENGINE_CLIENT_INTERFACE_VERSION, NULL );
  175. bik = (IBik*)factory( BIK_INTERFACE_VERSION, NULL );
  176. #ifdef _PS3
  177. ps3saveuiapi = (IPS3SaveRestoreToUI*)factory( IPS3SAVEUIAPI_VERSION_STRING, NULL );
  178. #endif
  179. #ifndef NO_STEAM
  180. #ifndef _PS3
  181. SteamAPI_InitSafe();
  182. #endif
  183. steamapicontext->Init();
  184. #endif
  185. CGameUIConVarRef var( "gameui_xbox" );
  186. m_bIsConsoleUI = var.IsValid() && var.GetBool();
  187. vgui::VGui_InitInterfacesList( "GameUI", &factory, 1 );
  188. vgui::VGui_InitMatSysInterfacesList( "GameUI", &factory, 1 );
  189. // load localization file
  190. g_pVGuiLocalize->AddFile( "Resource/gameui_%language%.txt", "GAME", true );
  191. // load mod info
  192. ModInfo().LoadCurrentGameInfo();
  193. // load localization file for kb_act.lst
  194. g_pVGuiLocalize->AddFile( "Resource/valve_%language%.txt", "GAME", true );
  195. bool bFailed = false;
  196. enginevguifuncs = (IEngineVGui *)factory( VENGINE_VGUI_VERSION, NULL );
  197. enginesurfacefuncs = (vgui::ISurface *)factory(VGUI_SURFACE_INTERFACE_VERSION, NULL);
  198. gameuifuncs = (IGameUIFuncs *)factory( VENGINE_GAMEUIFUNCS_VERSION, NULL );
  199. xboxsystem = (IXboxSystem *)factory( XBOXSYSTEM_INTERFACE_VERSION, NULL );
  200. #ifdef _X360
  201. xonline = (IXOnline *)factory( XONLINE_INTERFACE_VERSION, NULL );
  202. #endif
  203. #ifdef SWARM_DLL
  204. g_pMatchExt = ( IMatchExtSwarm * ) factory( IMATCHEXT_SWARM_INTERFACE, NULL );
  205. #endif
  206. bFailed = !enginesurfacefuncs || !gameuifuncs || !enginevguifuncs ||
  207. !xboxsystem ||
  208. #ifdef _X360
  209. !xonline ||
  210. #endif
  211. #ifdef SWARM_DLL
  212. !g_pMatchExt ||
  213. #endif
  214. !g_pMatchFramework;
  215. if ( bFailed )
  216. {
  217. Error( "CGameUI::Initialize() failed to get necessary interfaces\n" );
  218. }
  219. // setup base panel
  220. UI_BASEMOD_PANEL_CLASS& factoryBasePanel = ConstructUiBaseModPanelClass(); // explicit singleton instantiation
  221. factoryBasePanel.SetBounds( 0, 0, 640, 480 );
  222. factoryBasePanel.SetPaintBorderEnabled( false );
  223. factoryBasePanel.SetPaintBackgroundEnabled( true );
  224. factoryBasePanel.SetPaintEnabled( true );
  225. factoryBasePanel.SetVisible( true );
  226. factoryBasePanel.SetMouseInputEnabled( IsPC() );
  227. // factoryBasePanel.SetKeyBoardInputEnabled( IsPC() );
  228. factoryBasePanel.SetKeyBoardInputEnabled( true );
  229. vgui::VPANEL rootpanel = enginevguifuncs->GetPanel( PANEL_GAMEUIDLL );
  230. factoryBasePanel.SetParent( rootpanel );
  231. }
  232. void CGameUI::PostInit()
  233. {
  234. if ( IsGameConsole() )
  235. {
  236. enginesound->PrecacheSound( "UI/buttonrollover.wav", true, true );
  237. enginesound->PrecacheSound( "UI/buttonclick.wav", true, true );
  238. enginesound->PrecacheSound( "UI/buttonclickrelease.wav", true, true );
  239. enginesound->PrecacheSound( "UI/menu_accept.wav", true, true );
  240. enginesound->PrecacheSound( "UI/menu_focus.wav", true, true );
  241. enginesound->PrecacheSound( "UI/menu_invalid.wav", true, true );
  242. enginesound->PrecacheSound( "UI/menu_back.wav", true, true );
  243. enginesound->PrecacheSound( "UI/menu_countdown.wav", true, true );
  244. enginesound->PrecacheSound( "UI/ui_menu_flip_single_01.wav", true, true );
  245. enginesound->PrecacheSound( "UI/ui_menu_flip_single_02.wav", true, true );
  246. }
  247. #ifdef GAMEUI_BASEMODPANEL_VGUI
  248. // to know once client dlls have been loaded
  249. BaseModUI::CUIGameData::Get()->OnGameUIPostInit();
  250. #endif
  251. }
  252. //-----------------------------------------------------------------------------
  253. // Purpose: Sets the specified panel as the background panel for the loading
  254. // dialog. If NULL, default background is used. If you set a panel,
  255. // it should be full-screen with an opaque background, and must be a VGUI popup.
  256. //-----------------------------------------------------------------------------
  257. void CGameUI::SetLoadingBackgroundDialog( vgui::VPANEL panel )
  258. {
  259. g_hLoadingBackgroundDialog = panel;
  260. }
  261. //-----------------------------------------------------------------------------
  262. // Purpose: connects to client interfaces
  263. //-----------------------------------------------------------------------------
  264. void CGameUI::Connect( CreateInterfaceFn gameFactory )
  265. {
  266. g_pGameClientExports = (IGameClientExports *)gameFactory(GAMECLIENTEXPORTS_INTERFACE_VERSION, NULL);
  267. if (!g_pGameClientExports)
  268. {
  269. Error("CGameUI::Initialize() failed to get necessary interfaces\n");
  270. }
  271. m_GameFactory = gameFactory;
  272. }
  273. //-----------------------------------------------------------------------------
  274. // Purpose: Callback function; sends platform Shutdown message to specified window
  275. //-----------------------------------------------------------------------------
  276. int __stdcall SendShutdownMsgFunc(WHANDLE hwnd, int lparam)
  277. {
  278. Sys_PostMessage(hwnd, Sys_RegisterWindowMessage("ShutdownValvePlatform"), 0, 1);
  279. return 1;
  280. }
  281. //-----------------------------------------------------------------------------
  282. // Purpose: Searches for GameStartup*.mp3 files in the sound/ui folder and plays one
  283. //-----------------------------------------------------------------------------
  284. void CGameUI::PlayGameStartupSound()
  285. {
  286. #if defined( LEFT4DEAD )
  287. // L4D not using this path, L4D UI now handling with background menu movies
  288. return;
  289. #endif
  290. if ( IsGameConsole() )
  291. return;
  292. if ( CommandLine()->FindParm( "-nostartupsound" ) )
  293. return;
  294. FileFindHandle_t fh;
  295. CUtlVector<char *> fileNames;
  296. char path[ 512 ];
  297. Q_snprintf( path, sizeof( path ), "sound/ui/gamestartup*.mp3" );
  298. Q_FixSlashes( path );
  299. char const *fn = g_pFullFileSystem->FindFirstEx( path, "MOD", &fh );
  300. if ( fn )
  301. {
  302. do
  303. {
  304. char ext[ 10 ];
  305. Q_ExtractFileExtension( fn, ext, sizeof( ext ) );
  306. if ( !Q_stricmp( ext, "mp3" ) )
  307. {
  308. char temp[ 512 ];
  309. Q_snprintf( temp, sizeof( temp ), "ui/%s", fn );
  310. char *found = new char[ strlen( temp ) + 1 ];
  311. Q_strncpy( found, temp, strlen( temp ) + 1 );
  312. Q_FixSlashes( found );
  313. fileNames.AddToTail( found );
  314. }
  315. fn = g_pFullFileSystem->FindNext( fh );
  316. } while ( fn );
  317. g_pFullFileSystem->FindClose( fh );
  318. }
  319. // did we find any?
  320. if ( fileNames.Count() > 0 )
  321. {
  322. int index = Plat_MSTime() % fileNames.Count();
  323. if ( fileNames.IsValidIndex( index ) && fileNames[index] )
  324. {
  325. char found[ 512 ];
  326. // escape chars "*#" make it stream, and be affected by snd_musicvolume
  327. Q_snprintf( found, sizeof( found ), "play *#%s", fileNames[index] );
  328. engine->ClientCmd_Unrestricted( found );
  329. }
  330. fileNames.PurgeAndDeleteElements();
  331. }
  332. }
  333. //-----------------------------------------------------------------------------
  334. // Purpose: Called to setup the game UI
  335. //-----------------------------------------------------------------------------
  336. void CGameUI::Start()
  337. {
  338. // determine Steam location for configuration
  339. if ( !FindPlatformDirectory( m_szPlatformDir, sizeof( m_szPlatformDir ) ) )
  340. return;
  341. if ( IsPC() )
  342. {
  343. // setup config file directory
  344. char szConfigDir[512];
  345. Q_strncpy( szConfigDir, m_szPlatformDir, sizeof( szConfigDir ) );
  346. Q_strncat( szConfigDir, "config", sizeof( szConfigDir ), COPY_ALL_CHARACTERS );
  347. Msg( "Steam config directory: %s\n", szConfigDir );
  348. g_pFullFileSystem->AddSearchPath(szConfigDir, "CONFIG");
  349. g_pFullFileSystem->CreateDirHierarchy("", "CONFIG");
  350. // user dialog configuration
  351. vgui::system()->SetUserConfigFile("InGameDialogConfig.vdf", "CONFIG");
  352. g_pFullFileSystem->AddSearchPath( "platform", "PLATFORM" );
  353. }
  354. // localization
  355. g_pVGuiLocalize->AddFile( "Resource/platform_%language%.txt");
  356. g_pVGuiLocalize->AddFile( "Resource/vgui_%language%.txt");
  357. Sys_SetLastError( SYS_NO_ERROR );
  358. if ( IsPC() )
  359. {
  360. // Delay playing the startup music until two frames
  361. // this allows cbuf commands that occur on the first frame that may start a map
  362. m_iPlayGameStartupSound = 2;
  363. // now we are set up to check every frame to see if we can friends/server browser
  364. m_bTryingToLoadFriends = true;
  365. m_iFriendsLoadPauseFrames = 1;
  366. }
  367. }
  368. //-----------------------------------------------------------------------------
  369. // Purpose: Validates the user has a cdkey in the registry
  370. //-----------------------------------------------------------------------------
  371. void CGameUI::ValidateCDKey()
  372. {
  373. }
  374. //-----------------------------------------------------------------------------
  375. // Purpose: Finds which directory the platform resides in
  376. // Output : Returns true on success, false on failure.
  377. //-----------------------------------------------------------------------------
  378. bool CGameUI::FindPlatformDirectory(char *platformDir, int bufferSize)
  379. {
  380. platformDir[0] = '\0';
  381. if ( platformDir[0] == '\0' )
  382. {
  383. // we're not under steam, so setup using path relative to game
  384. if ( IsPC() )
  385. {
  386. #ifdef IS_WINDOWS_PC
  387. if ( ::GetModuleFileName( ( HINSTANCE )GetModuleHandle( NULL ), platformDir, bufferSize ) )
  388. #else
  389. if ( getcwd( platformDir, bufferSize ) )
  390. #endif
  391. {
  392. V_AppendSlash( platformDir, bufferSize );
  393. Q_strncat(platformDir, "platform", bufferSize, COPY_ALL_CHARACTERS );
  394. V_AppendSlash( platformDir, bufferSize );
  395. return true;
  396. }
  397. }
  398. else
  399. {
  400. // xbox fetches the platform path from exisiting platform search path
  401. // path to executeable is not correct for xbox remote configuration
  402. if ( g_pFullFileSystem->GetSearchPath( "PLATFORM", false, platformDir, bufferSize ) )
  403. {
  404. char *pSeperator = strchr( platformDir, ';' );
  405. if ( pSeperator )
  406. *pSeperator = '\0';
  407. return true;
  408. }
  409. }
  410. Error( "Unable to determine platform directory\n" );
  411. return false;
  412. }
  413. return (platformDir[0] != 0);
  414. }
  415. //-----------------------------------------------------------------------------
  416. // Purpose: Called to Shutdown the game UI system
  417. //-----------------------------------------------------------------------------
  418. void CGameUI::Shutdown()
  419. {
  420. #ifdef GAMEUI_BASEMODPANEL_VGUI
  421. BaseModUI::CUIGameData::Shutdown();
  422. #endif
  423. // notify all the modules of Shutdown
  424. g_VModuleLoader.ShutdownPlatformModules();
  425. // unload the modules them from memory
  426. g_VModuleLoader.UnloadPlatformModules();
  427. ModInfo().FreeModInfo();
  428. #ifndef NO_STEAM
  429. steamapicontext->Clear();
  430. #endif
  431. ConVar_Unregister();
  432. DisconnectTier3Libraries();
  433. DisconnectTier2Libraries();
  434. DisconnectTier1Libraries();
  435. }
  436. //-----------------------------------------------------------------------------
  437. // Purpose: just wraps an engine call to activate the gameUI
  438. //-----------------------------------------------------------------------------
  439. void CGameUI::ActivateGameUI()
  440. {
  441. engine->ExecuteClientCmd("gameui_activate");
  442. // Lock the UI to a particular player
  443. SetGameUIActiveSplitScreenPlayerSlot( engine->GetActiveSplitScreenPlayerSlot() );
  444. }
  445. //-----------------------------------------------------------------------------
  446. // Purpose: just wraps an engine call to hide the gameUI
  447. //-----------------------------------------------------------------------------
  448. void CGameUI::HideGameUI()
  449. {
  450. engine->ExecuteClientCmd("gameui_hide");
  451. }
  452. //-----------------------------------------------------------------------------
  453. // Purpose: Toggle allowing the engine to hide the game UI with the escape key
  454. //-----------------------------------------------------------------------------
  455. void CGameUI::PreventEngineHideGameUI()
  456. {
  457. engine->ExecuteClientCmd("gameui_preventescape");
  458. }
  459. //-----------------------------------------------------------------------------
  460. // Purpose: Toggle allowing the engine to hide the game UI with the escape key
  461. //-----------------------------------------------------------------------------
  462. void CGameUI::AllowEngineHideGameUI()
  463. {
  464. engine->ExecuteClientCmd("gameui_allowescape");
  465. }
  466. //-----------------------------------------------------------------------------
  467. // Purpose: Activate the game UI
  468. //-----------------------------------------------------------------------------
  469. void CGameUI::OnGameUIActivated()
  470. {
  471. bool bWasActive = m_bActivatedUI;
  472. m_bActivatedUI = true;
  473. materials->OnDebugEvent( "CGameUI::OnGameUIActivated" );
  474. // Lock the UI to a particular player
  475. if ( !bWasActive )
  476. {
  477. SetGameUIActiveSplitScreenPlayerSlot( engine->GetActiveSplitScreenPlayerSlot() );
  478. }
  479. // pause the server in case it is pausable
  480. engine->ClientCmd_Unrestricted( "setpause nomsg" );
  481. SetSavedThisMenuSession( false );
  482. UI_BASEMOD_PANEL_CLASS &ui = GetUiBaseModPanelClass();
  483. bool bNeedActivation = true;
  484. if ( ui.IsVisible() )
  485. {
  486. // Already visible, maybe don't need activation
  487. if ( ( !IsInLevel() && IsInBackgroundLevel() ) || ( !IsGameConsole() && !IsInLevel() ) )
  488. {
  489. bNeedActivation = false;
  490. }
  491. }
  492. if ( bNeedActivation )
  493. {
  494. GetUiBaseModPanelClass().OnGameUIActivated();
  495. }
  496. }
  497. //-----------------------------------------------------------------------------
  498. // Purpose: Hides the game ui, in whatever state it's in
  499. //-----------------------------------------------------------------------------
  500. void CGameUI::OnGameUIHidden()
  501. {
  502. bool bWasActive = m_bActivatedUI;
  503. m_bActivatedUI = false;
  504. // unpause the game when leaving the UI
  505. engine->ClientCmd_Unrestricted( "unpause nomsg" );
  506. GetUiBaseModPanelClass().OnGameUIHidden();
  507. #if !defined( NO_STEAM ) && !defined( NO_STEAM_GAMECOORDINATOR )
  508. // Tell the econ UI to close
  509. EconUI()->CloseEconUI();
  510. #endif
  511. // Restore to default
  512. if ( bWasActive )
  513. {
  514. SetGameUIActiveSplitScreenPlayerSlot( 0 );
  515. }
  516. }
  517. //-----------------------------------------------------------------------------
  518. // Purpose: paints all the vgui elements
  519. //-----------------------------------------------------------------------------
  520. void CGameUI::RunFrame()
  521. {
  522. if ( IsGameConsole() && m_bOpenProgressOnStart )
  523. {
  524. StartProgressBar();
  525. m_bOpenProgressOnStart = false;
  526. }
  527. int wide, tall;
  528. #if defined( TOOLFRAMEWORK_VGUI_REFACTOR )
  529. // resize the background panel to the screen size
  530. vgui::VPANEL clientDllPanel = enginevguifuncs->GetPanel( PANEL_ROOT );
  531. int x, y;
  532. vgui::ipanel()->GetPos( clientDllPanel, x, y );
  533. vgui::ipanel()->GetSize( clientDllPanel, wide, tall );
  534. staticPanel->SetBounds( x, y, wide,tall );
  535. #else
  536. vgui::surface()->GetScreenSize(wide, tall);
  537. GetUiBaseModPanelClass().SetSize(wide, tall);
  538. #endif
  539. // Run frames
  540. g_VModuleLoader.RunFrame();
  541. GetUiBaseModPanelClass().RunFrame();
  542. // Play the start-up music the first time we run frame
  543. if ( IsPC() && m_iPlayGameStartupSound > 0 )
  544. {
  545. m_iPlayGameStartupSound--;
  546. if ( !m_iPlayGameStartupSound )
  547. {
  548. PlayGameStartupSound();
  549. }
  550. }
  551. if ( IsPC() && m_bTryingToLoadFriends && m_iFriendsLoadPauseFrames-- < 1 )
  552. {
  553. // we got the mutex, so load Friends/Serverbrowser
  554. // clear the loading flag
  555. m_bTryingToLoadFriends = false;
  556. g_VModuleLoader.LoadPlatformModules(&m_GameFactory, 1, false);
  557. // notify the game of our game name
  558. const char *fullGamePath = engine->GetGameDirectory();
  559. const char *pathSep = strrchr( fullGamePath, '/' );
  560. if ( !pathSep )
  561. {
  562. pathSep = strrchr( fullGamePath, '\\' );
  563. }
  564. if ( pathSep )
  565. {
  566. KeyValues *pKV = new KeyValues("ActiveGameName" );
  567. pKV->SetString( "name", pathSep + 1 );
  568. pKV->SetInt( "appid", engine->GetAppID() );
  569. KeyValues *modinfo = new KeyValues("ModInfo");
  570. if ( modinfo->LoadFromFile( g_pFullFileSystem, "gameinfo.txt" ) )
  571. {
  572. pKV->SetString( "game", modinfo->GetString( "game", "" ) );
  573. }
  574. modinfo->deleteThis();
  575. g_VModuleLoader.PostMessageToAllModules( pKV );
  576. }
  577. // notify the ui of a game connect if we're already in a game
  578. if (m_iGameIP)
  579. {
  580. SendConnectedToGameMessage();
  581. }
  582. }
  583. }
  584. //-----------------------------------------------------------------------------
  585. // Purpose: Called when the game connects to a server
  586. //-----------------------------------------------------------------------------
  587. void CGameUI::OLD_OnConnectToServer(const char *game, int IP, int port)
  588. {
  589. // Nobody should use this anymore because the query port and the connection port can be different.
  590. // Use OnConnectToServer2 instead.
  591. Assert( false );
  592. OnConnectToServer2( game, IP, port, port );
  593. }
  594. //-----------------------------------------------------------------------------
  595. // Purpose: Called when the game connects to a server
  596. //-----------------------------------------------------------------------------
  597. void CGameUI::OnConnectToServer2(const char *game, int IP, int connectionPort, int queryPort)
  598. {
  599. m_iGameIP = IP;
  600. m_iGameConnectionPort = connectionPort;
  601. m_iGameQueryPort = queryPort;
  602. SendConnectedToGameMessage();
  603. }
  604. void CGameUI::SendConnectedToGameMessage()
  605. {
  606. MEM_ALLOC_CREDIT();
  607. KeyValues *kv = new KeyValues( "ConnectedToGame" );
  608. kv->SetInt( "ip", m_iGameIP );
  609. kv->SetInt( "connectionport", m_iGameConnectionPort );
  610. kv->SetInt( "queryport", m_iGameQueryPort );
  611. g_VModuleLoader.PostMessageToAllModules( kv );
  612. }
  613. //-----------------------------------------------------------------------------
  614. // Purpose: Called when the game disconnects from a server
  615. //-----------------------------------------------------------------------------
  616. void CGameUI::OnDisconnectFromServer( uint8 eSteamLoginFailure )
  617. {
  618. m_iGameIP = 0;
  619. m_iGameConnectionPort = 0;
  620. m_iGameQueryPort = 0;
  621. VGui_ClearTransitionVideoPanels();
  622. if ( g_hLoadingBackgroundDialog )
  623. {
  624. vgui::ivgui()->PostMessage( g_hLoadingBackgroundDialog, new KeyValues("DisconnectedFromGame"), NULL );
  625. }
  626. g_VModuleLoader.PostMessageToAllModules(new KeyValues("DisconnectedFromGame"));
  627. if ( eSteamLoginFailure == STEAMLOGINFAILURE_NOSTEAMLOGIN )
  628. {
  629. if ( g_hLoadingDialog )
  630. {
  631. g_hLoadingDialog->DisplayNoSteamConnectionError();
  632. }
  633. }
  634. else if ( eSteamLoginFailure == STEAMLOGINFAILURE_VACBANNED )
  635. {
  636. if ( g_hLoadingDialog )
  637. {
  638. g_hLoadingDialog->DisplayVACBannedError();
  639. }
  640. }
  641. else if ( eSteamLoginFailure == STEAMLOGINFAILURE_LOGGED_IN_ELSEWHERE )
  642. {
  643. if ( g_hLoadingDialog )
  644. {
  645. g_hLoadingDialog->DisplayLoggedInElsewhereError();
  646. }
  647. }
  648. }
  649. //-----------------------------------------------------------------------------
  650. // Purpose: activates the loading dialog on level load start
  651. //-----------------------------------------------------------------------------
  652. void CGameUI::OnLevelLoadingStarted( const char *levelName, bool bShowProgressDialog )
  653. {
  654. g_VModuleLoader.PostMessageToAllModules( new KeyValues( "LoadingStarted" ) );
  655. GetUiBaseModPanelClass().OnLevelLoadingStarted( levelName, bShowProgressDialog );
  656. ShowLoadingBackgroundDialog();
  657. if ( bShowProgressDialog )
  658. {
  659. StartProgressBar();
  660. }
  661. // Don't play the start game sound if this happens before we get to the first frame
  662. m_iPlayGameStartupSound = 0;
  663. }
  664. //-----------------------------------------------------------------------------
  665. // Purpose: closes any level load dialog
  666. //-----------------------------------------------------------------------------
  667. void CGameUI::OnLevelLoadingFinished(bool bError, const char *failureReason, const char *extendedReason)
  668. {
  669. StopProgressBar( bError, failureReason, extendedReason );
  670. // notify all the modules
  671. g_VModuleLoader.PostMessageToAllModules( new KeyValues( "LoadingFinished" ) );
  672. HideLoadingBackgroundDialog();
  673. }
  674. //-----------------------------------------------------------------------------
  675. // Purpose: Updates progress bar
  676. // Output : Returns true if screen should be redrawn
  677. //-----------------------------------------------------------------------------
  678. bool CGameUI::UpdateProgressBar(float progress, const char *statusText)
  679. {
  680. return GetUiBaseModPanelClass().UpdateProgressBar(progress, statusText);
  681. }
  682. //-----------------------------------------------------------------------------
  683. // Purpose:
  684. //-----------------------------------------------------------------------------
  685. void CGameUI::SetProgressLevelName( const char *levelName )
  686. {
  687. MEM_ALLOC_CREDIT();
  688. if ( g_hLoadingBackgroundDialog )
  689. {
  690. KeyValues *pKV = new KeyValues( "ProgressLevelName" );
  691. pKV->SetString( "levelName", levelName );
  692. vgui::ivgui()->PostMessage( g_hLoadingBackgroundDialog, pKV, NULL );
  693. }
  694. if ( g_hLoadingDialog.Get() )
  695. {
  696. // TODO: g_hLoadingDialog->SetLevelName( levelName );
  697. }
  698. }
  699. //-----------------------------------------------------------------------------
  700. // Purpose:
  701. //-----------------------------------------------------------------------------
  702. void CGameUI::StartProgressBar()
  703. {
  704. }
  705. //-----------------------------------------------------------------------------
  706. // Purpose: returns true if the screen should be updated
  707. //-----------------------------------------------------------------------------
  708. bool CGameUI::ContinueProgressBar( float progressFraction )
  709. {
  710. if (!g_hLoadingDialog.Get())
  711. return false;
  712. g_hLoadingDialog->Activate();
  713. return g_hLoadingDialog->SetProgressPoint(progressFraction);
  714. }
  715. //-----------------------------------------------------------------------------
  716. // Purpose: stops progress bar, displays error if necessary
  717. //-----------------------------------------------------------------------------
  718. void CGameUI::StopProgressBar(bool bError, const char *failureReason, const char *extendedReason)
  719. {
  720. if (!g_hLoadingDialog.Get())
  721. return;
  722. if ( !IsGameConsole() && bError )
  723. {
  724. // turn the dialog to error display mode
  725. g_hLoadingDialog->DisplayGenericError(failureReason, extendedReason);
  726. }
  727. else
  728. {
  729. // close loading dialog
  730. g_hLoadingDialog->Close();
  731. g_hLoadingDialog = NULL;
  732. }
  733. // should update the background to be in a transition here
  734. }
  735. //-----------------------------------------------------------------------------
  736. // Purpose: sets loading info text
  737. //-----------------------------------------------------------------------------
  738. bool CGameUI::SetProgressBarStatusText(const char *statusText)
  739. {
  740. if (!g_hLoadingDialog.Get())
  741. return false;
  742. if (!statusText)
  743. return false;
  744. if (!stricmp(statusText, m_szPreviousStatusText))
  745. return false;
  746. g_hLoadingDialog->SetStatusText(statusText);
  747. Q_strncpy(m_szPreviousStatusText, statusText, sizeof(m_szPreviousStatusText));
  748. return true;
  749. }
  750. //-----------------------------------------------------------------------------
  751. // Purpose:
  752. //-----------------------------------------------------------------------------
  753. void CGameUI::SetSecondaryProgressBar(float progress /* range [0..1] */)
  754. {
  755. if (!g_hLoadingDialog.Get())
  756. return;
  757. g_hLoadingDialog->SetSecondaryProgress(progress);
  758. }
  759. //-----------------------------------------------------------------------------
  760. // Purpose:
  761. //-----------------------------------------------------------------------------
  762. void CGameUI::SetSecondaryProgressBarText(const char *statusText)
  763. {
  764. if (!g_hLoadingDialog.Get())
  765. return;
  766. g_hLoadingDialog->SetSecondaryProgressText(statusText);
  767. }
  768. //-----------------------------------------------------------------------------
  769. // Purpose: Returns prev settings
  770. //-----------------------------------------------------------------------------
  771. bool CGameUI::SetShowProgressText( bool show )
  772. {
  773. if (!g_hLoadingDialog.Get())
  774. return false;
  775. return g_hLoadingDialog->SetShowProgressText( show );
  776. }
  777. //-----------------------------------------------------------------------------
  778. // Purpose: returns true if we're currently playing the game
  779. //-----------------------------------------------------------------------------
  780. bool CGameUI::IsInLevel()
  781. {
  782. const char *levelName = engine->GetLevelName();
  783. if (levelName && levelName[0] && !engine->IsLevelMainMenuBackground())
  784. {
  785. return true;
  786. }
  787. return false;
  788. }
  789. //-----------------------------------------------------------------------------
  790. // Purpose: returns true if we're at the main menu and a background level is loaded
  791. //-----------------------------------------------------------------------------
  792. bool CGameUI::IsInBackgroundLevel()
  793. {
  794. const char *levelName = engine->GetLevelName();
  795. if (levelName && levelName[0] && engine->IsLevelMainMenuBackground())
  796. {
  797. return true;
  798. }
  799. return false;
  800. }
  801. //-----------------------------------------------------------------------------
  802. // Purpose: returns true if we're in a multiplayer game
  803. //-----------------------------------------------------------------------------
  804. bool CGameUI::IsInMultiplayer()
  805. {
  806. return (IsInLevel() && engine->GetMaxClients() > 1);
  807. }
  808. //-----------------------------------------------------------------------------
  809. // Purpose: returns true if we're console ui
  810. //-----------------------------------------------------------------------------
  811. bool CGameUI::IsConsoleUI()
  812. {
  813. return m_bIsConsoleUI;
  814. }
  815. //-----------------------------------------------------------------------------
  816. // Purpose: returns true if we've saved without closing the menu
  817. //-----------------------------------------------------------------------------
  818. bool CGameUI::HasSavedThisMenuSession()
  819. {
  820. return m_bHasSavedThisMenuSession;
  821. }
  822. void CGameUI::SetSavedThisMenuSession( bool bState )
  823. {
  824. m_bHasSavedThisMenuSession = bState;
  825. }
  826. //-----------------------------------------------------------------------------
  827. // Purpose: Makes the loading background dialog visible, if one has been set
  828. //-----------------------------------------------------------------------------
  829. void CGameUI::ShowLoadingBackgroundDialog()
  830. {
  831. if ( g_hLoadingBackgroundDialog )
  832. {
  833. vgui::VPANEL panel = GetUiBaseModPanelClass().GetVPanel();
  834. vgui::ipanel()->SetParent( g_hLoadingBackgroundDialog, panel );
  835. vgui::ipanel()->MoveToFront( g_hLoadingBackgroundDialog );
  836. }
  837. }
  838. //-----------------------------------------------------------------------------
  839. // Purpose: Hides the loading background dialog, if one has been set
  840. //-----------------------------------------------------------------------------
  841. void CGameUI::HideLoadingBackgroundDialog()
  842. {
  843. if ( g_hLoadingBackgroundDialog )
  844. {
  845. if ( engine->IsInGame() )
  846. {
  847. vgui::ivgui()->PostMessage( g_hLoadingBackgroundDialog, new KeyValues( "LoadedIntoGame" ), NULL );
  848. }
  849. else
  850. {
  851. vgui::ipanel()->SetVisible( g_hLoadingBackgroundDialog, false );
  852. vgui::ipanel()->MoveToBack( g_hLoadingBackgroundDialog );
  853. }
  854. vgui::ivgui()->PostMessage( g_hLoadingBackgroundDialog, new KeyValues("HideAsLoadingPanel"), NULL );
  855. }
  856. }
  857. //-----------------------------------------------------------------------------
  858. // Purpose: Returns whether a loading background dialog has been set
  859. //-----------------------------------------------------------------------------
  860. bool CGameUI::HasLoadingBackgroundDialog()
  861. {
  862. return ( NULL != g_hLoadingBackgroundDialog );
  863. }
  864. //-----------------------------------------------------------------------------
  865. void CGameUI::NeedConnectionProblemWaitScreen()
  866. {
  867. #ifdef GAMEUI_BASEMODPANEL_VGUI
  868. BaseModUI::CUIGameData::Get()->NeedConnectionProblemWaitScreen();
  869. #endif
  870. }
  871. void CGameUI::ShowPasswordUI( char const *pchCurrentPW )
  872. {
  873. #ifdef GAMEUI_BASEMODPANEL_VGUI
  874. BaseModUI::CUIGameData::Get()->ShowPasswordUI( pchCurrentPW );
  875. #endif
  876. }
  877. //-----------------------------------------------------------------------------
  878. void CGameUI::SetProgressOnStart()
  879. {
  880. m_bOpenProgressOnStart = true;
  881. }
  882. #if defined( _GAMECONSOLE ) && defined( _DEMO )
  883. void CGameUI::OnDemoTimeout()
  884. {
  885. GetUiBaseModPanelClass().OnDemoTimeout();
  886. }
  887. #endif
  888. bool CGameUI::LoadingProgressWantsIsolatedRender( bool bContextValid )
  889. {
  890. return GetUiBaseModPanelClass().LoadingProgressWantsIsolatedRender( bContextValid );
  891. }
  892. bool CGameUI::IsPlayingFullScreenVideo()
  893. {
  894. return VGui_IsPlayingFullScreenVideo();
  895. }
  896. bool CGameUI::IsTransitionEffectEnabled()
  897. {
  898. return GetUiBaseModPanelClass().IsTransitionEffectEnabled();
  899. }