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.

205 lines
5.7 KiB

  1. #include "stdafx.h"
  2. #include "hammer.h"
  3. #include "hammervgui.h"
  4. #include <vgui/IVGui.h>
  5. #include <vgui/ISurface.h>
  6. #include <vgui/ISystem.h>
  7. #include "vgui/IInput.h"
  8. #include "vgui_controls/EditablePanel.h"
  9. #include <VGuiMatSurface/IMatSystemSurface.h>
  10. #include <matsys_controls/matsyscontrols.h>
  11. #include "material.h"
  12. #include "vgui_controls/AnimationController.h"
  13. #include "inputsystem/iinputsystem.h"
  14. #include "VGuiWnd.h"
  15. #include "toolutils/enginetools_int.h"
  16. #include "toolframework/ienginetool.h"
  17. #include "inputsystem/iinputstacksystem.h"
  18. //-----------------------------------------------------------------------------
  19. // Purpose: singleton accessor
  20. //-----------------------------------------------------------------------------
  21. // This window doesn't do anything other than tell CMatSystemSurface::CalculateMouseVisible to deem the mouse visible.
  22. class CDummyPopupPanel : public vgui::Panel
  23. {
  24. public:
  25. virtual void PaintBackground() {}
  26. virtual void Paint() {}
  27. };
  28. static CHammerVGui s_HammerVGui;
  29. CHammerVGui *HammerVGui()
  30. {
  31. return &s_HammerVGui;
  32. }
  33. CHammerVGui::CHammerVGui(void)
  34. {
  35. m_pActiveWindow = NULL;
  36. m_hMainWindow = NULL;
  37. m_pDummyPopup = NULL;
  38. m_bCurrentDialogIsModal = false;
  39. m_hHammerScheme = NULL;
  40. m_hVguiInputContext = INPUT_CONTEXT_HANDLE_INVALID;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Setup the base vgui panels
  44. //-----------------------------------------------------------------------------
  45. bool CHammerVGui::Init( HWND hWindow )
  46. {
  47. m_hMainWindow = hWindow;
  48. if ( !APP()->IsFoundryMode() ) // We don't need to init most stuff in Foundry mode because the engine has already done it.
  49. {
  50. // initialize vgui_control interfaces
  51. if (!vgui::VGui_InitInterfacesList( "HAMMER", &g_Factory, 1 ))
  52. return false;
  53. if ( !vgui::VGui_InitMatSysInterfacesList( "HAMMER", &g_Factory, 1 ) )
  54. return false;
  55. if ( !g_pMatSystemSurface )
  56. return false;
  57. // configuration settings
  58. vgui::system()->SetUserConfigFile("hammer.vdf", "EXECUTABLE_PATH");
  59. // Are we trapping input?
  60. g_pMatSystemSurface->EnableWindowsMessages( true );
  61. }
  62. m_hVguiInputContext = g_pInputStackSystem->PushInputContext();
  63. g_pMatSystemSurface->SetInputContext( m_hVguiInputContext );
  64. // Need to be able to play sounds through vgui
  65. // g_pMatSystemSurface->InstallPlaySoundFunc( VGui_PlaySound );
  66. // load scheme
  67. m_hHammerScheme = vgui::scheme()->LoadSchemeFromFile("//PLATFORM/Resource/SourceScheme.res", "Hammer");
  68. if ( !m_hHammerScheme )
  69. {
  70. return false;
  71. }
  72. if ( !APP()->IsFoundryMode() ) // We don't need to init most stuff in Foundry mode because the engine has already done it.
  73. {
  74. // Start the App running
  75. vgui::ivgui()->Start();
  76. vgui::ivgui()->SetSleep(false);
  77. // Create a popup window. This window doesn't do anything other than tell CMatSystemSurface::CalculateMouseVisible to deem the mouse visible.
  78. m_pDummyPopup = new CDummyPopupPanel;
  79. m_pDummyPopup->MakePopup( false, true );
  80. m_pDummyPopup->SetVisible( true );
  81. }
  82. return true;
  83. }
  84. void CHammerVGui::SetFocus( CVGuiWnd *pVGuiWnd )
  85. {
  86. if ( pVGuiWnd == m_pActiveWindow )
  87. return;
  88. g_pInputSystem->PollInputState();
  89. vgui::ivgui()->RunFrame();
  90. g_pMatSystemSurface->SetAppDrivesInput( true );
  91. g_pInputSystem->DetachFromWindow( );
  92. // Disable mouse input on the previous panel so it doesn't get input the engine should get.
  93. if ( m_pActiveWindow && m_pActiveWindow->GetMainPanel() )
  94. {
  95. m_pActiveWindow->GetMainPanel()->SetMouseInputEnabled( false );
  96. }
  97. if ( pVGuiWnd )
  98. {
  99. m_pActiveWindow = pVGuiWnd;
  100. m_bCurrentDialogIsModal = m_pActiveWindow->IsModal();
  101. Assert( pVGuiWnd->GetMainPanel() != NULL );
  102. if ( pVGuiWnd->GetMainPanel() )
  103. pVGuiWnd->GetMainPanel()->SetMouseInputEnabled( true );
  104. g_pInputSystem->AttachToWindow( pVGuiWnd->GetParentWnd()->GetSafeHwnd() );
  105. g_pMatSystemSurface->SetAppDrivesInput( !m_bCurrentDialogIsModal );
  106. vgui::ivgui()->ActivateContext( pVGuiWnd->GetVGuiContext() );
  107. // If this is a modal VGuiWnd (like the model browser), don't let the engine's message loop get called at all
  108. // or else it'll screw up stuff - it'll give focus to other CVGuiWnds and the engine might drive
  109. // some vgui stuff instead of the VGuiWnd message loop (in CVGuiWnd::WindowProcVGui).
  110. if ( pVGuiWnd->IsModal() && enginetools )
  111. ::EnableWindow( (HWND)enginetools->GetEngineHwnd(), false );
  112. }
  113. else
  114. {
  115. if ( enginetools )
  116. {
  117. // We can't call m_pActiveWindow->IsModal here because it might be in its destructor (as with the model browser)
  118. // and it's a virtual function.
  119. if ( m_bCurrentDialogIsModal )
  120. ::EnableWindow( (HWND)enginetools->GetEngineHwnd(), true );
  121. g_pInputSystem->AttachToWindow( enginetools->GetEngineHwnd() );
  122. g_pMatSystemSurface->SetAppDrivesInput( true );
  123. }
  124. m_pActiveWindow = NULL;
  125. vgui::ivgui()->ActivateContext( vgui::DEFAULT_VGUI_CONTEXT );
  126. }
  127. }
  128. bool CHammerVGui::HasFocus( CVGuiWnd *pWnd )
  129. {
  130. return m_pActiveWindow == pWnd;
  131. }
  132. void CHammerVGui::Simulate()
  133. {
  134. // VPROF( "CHammerVGui::Simulate" );
  135. if ( !IsInitialized() )
  136. return;
  137. g_pInputSystem->PollInputState();
  138. vgui::ivgui()->RunFrame();
  139. // run vgui animations
  140. vgui::GetAnimationController()->UpdateAnimations( vgui::system()->GetCurrentTime() );
  141. }
  142. void CHammerVGui::Shutdown()
  143. {
  144. // Give panels a chance to settle so things
  145. // Marked for deletion will actually get deleted
  146. if ( !IsInitialized() )
  147. return;
  148. if ( m_pDummyPopup )
  149. {
  150. delete m_pDummyPopup;
  151. m_pDummyPopup = NULL;
  152. }
  153. if ( m_hVguiInputContext != INPUT_CONTEXT_HANDLE_INVALID )
  154. {
  155. g_pMatSystemSurface->SetInputContext( NULL );
  156. g_pInputStackSystem->PopInputContext();
  157. m_hVguiInputContext = INPUT_CONTEXT_HANDLE_INVALID;
  158. }
  159. g_pInputSystem->PollInputState();
  160. vgui::ivgui()->RunFrame();
  161. // stop the App running
  162. vgui::ivgui()->Stop();
  163. }
  164. CHammerVGui::~CHammerVGui(void)
  165. {
  166. }