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.

244 lines
7.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: The main manager of the UI
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "uimanager.h"
  9. #include "legion.h"
  10. #include "appframework/vguimatsysapp.h"
  11. #include "vgui/IVGui.h"
  12. #include "vgui/ISurface.h"
  13. #include "VGuiMatSurface/IMatSystemSurface.h"
  14. #include "vgui_controls/controls.h"
  15. #include "vgui/ILocalize.h"
  16. #include "vgui_controls/EditablePanel.h"
  17. #include "vgui_controls/AnimationController.h"
  18. #include "filesystem.h"
  19. #include "tier3/tier3.h"
  20. #include "vgui_controls/consoledialog.h"
  21. #include "inputmanager.h"
  22. //-----------------------------------------------------------------------------
  23. // Console dialog for use in legion
  24. //-----------------------------------------------------------------------------
  25. class CLegionConsoleDialog : public vgui::CConsoleDialog
  26. {
  27. DECLARE_CLASS_SIMPLE( CLegionConsoleDialog, vgui::CConsoleDialog );
  28. public:
  29. CLegionConsoleDialog( vgui::Panel *pParent, const char *pName );
  30. virtual ~CLegionConsoleDialog();
  31. virtual void OnClose();
  32. MESSAGE_FUNC_CHARPTR( OnCommandSubmitted, "CommandSubmitted", command );
  33. };
  34. CLegionConsoleDialog::CLegionConsoleDialog( vgui::Panel *pParent, const char *pName ) : BaseClass ( pParent, pName )
  35. {
  36. AddActionSignalTarget( this );
  37. }
  38. CLegionConsoleDialog::~CLegionConsoleDialog()
  39. {
  40. g_pUIManager->HideConsole( );
  41. }
  42. //-----------------------------------------------------------------------------
  43. // A command was sent by the console
  44. //-----------------------------------------------------------------------------
  45. void CLegionConsoleDialog::OnCommandSubmitted( const char *pCommand )
  46. {
  47. g_pInputManager->AddCommand( pCommand );
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Deals with close
  51. //-----------------------------------------------------------------------------
  52. void CLegionConsoleDialog::OnClose()
  53. {
  54. g_pUIManager->HideConsole( );
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Singleton accessor
  58. //-----------------------------------------------------------------------------
  59. static CUIManager s_UIManager;
  60. extern CUIManager *g_pUIManager = &s_UIManager;
  61. static const char *s_pRootPanelNames[UI_ROOT_PANEL_COUNT] =
  62. {
  63. "RootGamePanel",
  64. "RootMenuPanel",
  65. "RootToolsPanel",
  66. };
  67. //-----------------------------------------------------------------------------
  68. // Constructor
  69. //-----------------------------------------------------------------------------
  70. CUIManager::CUIManager()
  71. {
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Init, shutdown
  75. //-----------------------------------------------------------------------------
  76. bool CUIManager::Init()
  77. {
  78. COMPILE_TIME_ASSERT( sizeof(s_pRootPanelNames) / sizeof(const char*) == UI_ROOT_PANEL_COUNT );
  79. // load the base localization file
  80. if (! vgui::scheme()->LoadSchemeFromFile("resource/legion.res", "Legion" ) )
  81. return false;
  82. vgui::filesystem()->AddSearchPath( "platform", "PLATFORM" );
  83. vgui::localize()->AddFile( vgui::filesystem(), "Resource/vgui_%language%.txt" );
  84. // start vgui
  85. g_pVGui->Start();
  86. // Run a frame to get the embedded panel to be the right size
  87. g_pVGui->RunFrame();
  88. int w, h;
  89. m_hEmbeddedPanel = g_pVGuiSurface->GetEmbeddedPanel();
  90. vgui::ipanel()->GetSize( m_hEmbeddedPanel, w, h );
  91. // add our root panels
  92. for ( int i = 0; i < UI_ROOT_PANEL_COUNT; ++i )
  93. {
  94. m_pRootPanels[i] = new vgui::EditablePanel( NULL, s_pRootPanelNames[i] );
  95. m_pRootPanels[i]->SetParent( m_hEmbeddedPanel );
  96. m_pRootPanels[i]->SetZPos( i );
  97. m_pRootPanels[i]->SetBounds( 0, 0, w, h );
  98. m_pRootPanels[i]->SetPaintBorderEnabled( false );
  99. m_pRootPanels[i]->SetPaintBackgroundEnabled( false );
  100. m_pRootPanels[i]->SetPaintEnabled( false );
  101. m_pRootPanels[i]->SetKeyBoardInputEnabled( i != UI_ROOT_GAME );
  102. m_pRootPanels[i]->SetMouseInputEnabled( i != UI_ROOT_GAME );
  103. m_pRootPanels[i]->SetVisible( false );
  104. m_pRootPanels[i]->SetCursor( vgui::dc_crosshair );
  105. m_pRootPanels[i]->SetAutoResize( vgui::Panel::PIN_TOPLEFT, vgui::Panel::AUTORESIZE_DOWNANDRIGHT, 0, 0, 0, 0 );
  106. }
  107. m_hConsole = NULL;
  108. vgui::surface()->Invalidate( m_hEmbeddedPanel );
  109. return true;
  110. }
  111. void CUIManager::Shutdown()
  112. {
  113. g_pVGui->Stop();
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Sets particular root panels to be visible
  117. //-----------------------------------------------------------------------------
  118. void CUIManager::EnablePanel( UIRootPanel_t id, bool bEnable )
  119. {
  120. m_pRootPanels[id]->SetVisible( bEnable );
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Toggles the console
  124. //-----------------------------------------------------------------------------
  125. void CUIManager::ToggleConsole( const CCommand &args )
  126. {
  127. if ( !m_hConsole.Get() )
  128. {
  129. m_hConsole = new CLegionConsoleDialog( m_pRootPanels[UI_ROOT_TOOLS], "Console" );
  130. // set the console to taking up most of the right-half of the screen
  131. int swide, stall;
  132. vgui::surface()->GetScreenSize(swide, stall);
  133. int offset = vgui::scheme()->GetProportionalScaledValue(16);
  134. m_hConsole->SetBounds(
  135. swide / 2 - (offset * 4),
  136. offset,
  137. (swide / 2) + (offset * 3),
  138. stall - (offset * 8));
  139. m_hConsole->SetVisible( false );
  140. }
  141. bool bMakeVisible = !m_hConsole->IsVisible();
  142. EnablePanel( UI_ROOT_TOOLS, bMakeVisible );
  143. if ( bMakeVisible )
  144. {
  145. m_hConsole->Activate();
  146. }
  147. else
  148. {
  149. m_hConsole->Hide();
  150. }
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Hides the console
  154. //-----------------------------------------------------------------------------
  155. void CUIManager::HideConsole()
  156. {
  157. EnablePanel( UI_ROOT_TOOLS, false );
  158. if ( m_hConsole.Get() )
  159. {
  160. m_hConsole->SetVisible( false );
  161. }
  162. }
  163. //-----------------------------------------------------------------------------
  164. // Per-frame update
  165. //-----------------------------------------------------------------------------
  166. void CUIManager::Update( )
  167. {
  168. vgui::GetAnimationController()->UpdateAnimations( IGameManager::CurrentTime() );
  169. g_pVGui->RunFrame();
  170. if ( !g_pVGui->IsRunning() )
  171. {
  172. IGameManager::Stop();
  173. }
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Attempt to process an input event, return true if it sholdn't be chained to the rest of the game
  177. //-----------------------------------------------------------------------------
  178. bool CUIManager::ProcessInputEvent( const InputEvent_t& event )
  179. {
  180. return g_pMatSystemSurface->HandleInputEvent( event );
  181. }
  182. //-----------------------------------------------------------------------------
  183. // Draws the UI
  184. //-----------------------------------------------------------------------------
  185. void CUIManager::DrawUI()
  186. {
  187. g_pVGuiSurface->PaintTraverseEx( m_hEmbeddedPanel, true );
  188. }
  189. //-----------------------------------------------------------------------------
  190. // Push, pop menus
  191. //-----------------------------------------------------------------------------
  192. vgui::Panel *CUIManager::GetRootPanel( UIRootPanel_t id )
  193. {
  194. return m_pRootPanels[id];
  195. }