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.

1156 lines
30 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Core Movie Maker UI API
  4. //
  5. //=============================================================================
  6. #include "toolutils/basetoolsystem.h"
  7. #include "toolframework/ienginetool.h"
  8. #include "vgui/IPanel.h"
  9. #include "vgui_controls/Controls.h"
  10. #include "vgui_controls/Menu.h"
  11. #include "vgui/ISurface.h"
  12. #include "vgui_controls/Panel.h"
  13. #include "vgui_controls/FileOpenDialog.h"
  14. #include "vgui_controls/MessageBox.h"
  15. #include "vgui/Cursor.h"
  16. #include "vgui/iinput.h"
  17. #include "vgui/ivgui.h"
  18. #include "vgui_controls/AnimationController.h"
  19. #include "ienginevgui.h"
  20. #include "toolui.h"
  21. #include "toolutils/toolmenubar.h"
  22. #include "vgui/ilocalize.h"
  23. #include "toolutils/enginetools_int.h"
  24. #include "toolutils/vgui_tools.h"
  25. #include "icvar.h"
  26. #include "tier1/convar.h"
  27. #include "datamodel/dmelementfactoryhelper.h"
  28. #include "filesystem.h"
  29. #include "vgui_controls/savedocumentquery.h"
  30. #include "vgui_controls/perforcefilelistframe.h"
  31. #include "toolutils/miniviewport.h"
  32. #include "materialsystem/imaterialsystem.h"
  33. #include "materialsystem/imaterial.h"
  34. #include "materialsystem/imesh.h"
  35. #include "toolutils/BaseStatusBar.h"
  36. #include "movieobjects/movieobjects.h"
  37. #include "vgui_controls/KeyBoardEditorDialog.h"
  38. #include "vgui_controls/KeyBindingHelpDialog.h"
  39. #include "dmserializers/idmserializers.h"
  40. #include "tier2/renderutils.h"
  41. // memdbgon must be the last include file in a .cpp file!!!
  42. #include "tier0/memdbgon.h"
  43. using namespace vgui;
  44. extern IMaterialSystem *MaterialSystem();
  45. class CGlobalFlexController : public IGlobalFlexController
  46. {
  47. public:
  48. virtual int FindGlobalFlexController( const char *name )
  49. {
  50. return clienttools->FindGlobalFlexcontroller( name );
  51. }
  52. virtual const char *GetGlobalFlexControllerName( int idx )
  53. {
  54. return clienttools->GetGlobalFlexControllerName( idx );
  55. }
  56. };
  57. static CGlobalFlexController g_GlobalFlexController;
  58. extern IGlobalFlexController *g_pGlobalFlexController;
  59. //-----------------------------------------------------------------------------
  60. // Singleton interfaces
  61. //-----------------------------------------------------------------------------
  62. IServerTools *servertools = NULL;
  63. IClientTools *clienttools = NULL;
  64. //-----------------------------------------------------------------------------
  65. // External functions
  66. //-----------------------------------------------------------------------------
  67. void RegisterTool( IToolSystem *tool );
  68. //-----------------------------------------------------------------------------
  69. // Base tool system constructor
  70. //-----------------------------------------------------------------------------
  71. CBaseToolSystem::CBaseToolSystem( const char *pToolName /*="CBaseToolSystem"*/ ) :
  72. BaseClass( NULL, pToolName ),
  73. m_pBackground( 0 ),
  74. m_pLogo( 0 )
  75. {
  76. RegisterTool( this );
  77. SetAutoDelete( false );
  78. m_bGameInputEnabled = false;
  79. m_bFullscreenMode = false;
  80. m_bIsActive = false;
  81. m_bFullscreenToolModeEnabled = false;
  82. m_MostRecentlyFocused = NULL;
  83. SetKeyBoardInputEnabled( true );
  84. input()->RegisterKeyCodeUnhandledListener( GetVPanel() );
  85. m_pFileOpenStateMachine = new vgui::FileOpenStateMachine( this, this );
  86. m_pFileOpenStateMachine->AddActionSignalTarget( this );
  87. }
  88. void CBaseToolSystem::ApplySchemeSettings(IScheme *pScheme)
  89. {
  90. BaseClass::ApplySchemeSettings(pScheme);
  91. SetKeyBoardInputEnabled( true );
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Called at the end of engine startup (after client .dll and server .dll have been loaded)
  95. //-----------------------------------------------------------------------------
  96. bool CBaseToolSystem::Init( )
  97. {
  98. // Read shared localization info
  99. g_pVGuiLocalize->AddFile( "resource/dmecontrols_%language%.txt" );
  100. g_pVGuiLocalize->AddFile( "resource/toolshared_%language%.txt" );
  101. g_pVGuiLocalize->AddFile( "Resource/vgui_%language%.txt" );
  102. g_pVGuiLocalize->AddFile( "Resource/platform_%language%.txt" );
  103. g_pVGuiLocalize->AddFile( "resource/boxrocket_%language%.txt" );
  104. // Create the tool workspace
  105. SetParent( VGui_GetToolRootPanel() );
  106. // Deal with scheme
  107. vgui::HScheme hToolScheme = GetToolScheme();
  108. if ( hToolScheme != 0 )
  109. {
  110. SetScheme( hToolScheme );
  111. }
  112. m_KeyBindingsHandle = Panel::CreateKeyBindingsContext( GetBindingsContextFile(), "GAME" );
  113. SetKeyBindingsContext( m_KeyBindingsHandle );
  114. LoadKeyBindings();
  115. const char *pszBackground = GetBackgroundTextureName();
  116. if ( pszBackground )
  117. {
  118. m_pBackground = materials->FindMaterial( GetBackgroundTextureName() , TEXTURE_GROUP_VGUI );
  119. m_pBackground->IncrementReferenceCount();
  120. }
  121. const char *pszLogo = GetLogoTextureName();
  122. if ( pszLogo )
  123. {
  124. m_pLogo = materials->FindMaterial( GetLogoTextureName(), TEXTURE_GROUP_VGUI );
  125. m_pLogo->IncrementReferenceCount();
  126. }
  127. // Make the tool workspace the size of the screen
  128. int w, h;
  129. surface()->GetScreenSize( w, h );
  130. SetBounds( 0, 0, w, h );
  131. SetPaintBackgroundEnabled( true );
  132. SetPaintBorderEnabled( false );
  133. SetPaintEnabled( false );
  134. SetCursor( vgui::dc_none );
  135. SetVisible( false );
  136. // Create the tool UI
  137. m_pToolUI = new CToolUI( this, "ToolUI", this );
  138. // Create the mini viewport
  139. m_hMiniViewport = CreateMiniViewport( GetClientArea() );
  140. Assert( m_hMiniViewport.Get() );
  141. return true;
  142. }
  143. void CBaseToolSystem::ShowMiniViewport( bool state )
  144. {
  145. if ( !m_hMiniViewport.Get() )
  146. return;
  147. m_hMiniViewport->SetVisible( state );
  148. }
  149. void CBaseToolSystem::SetMiniViewportBounds( int x, int y, int width, int height )
  150. {
  151. if ( m_hMiniViewport )
  152. {
  153. m_hMiniViewport->SetBounds( x, y, width, height );
  154. }
  155. }
  156. void CBaseToolSystem::SetMiniViewportText( const char *pText )
  157. {
  158. if ( m_hMiniViewport )
  159. {
  160. m_hMiniViewport->SetOverlayText( pText );
  161. }
  162. }
  163. void CBaseToolSystem::GetMiniViewportEngineBounds( int &x, int &y, int &width, int &height )
  164. {
  165. if ( m_hMiniViewport )
  166. {
  167. m_hMiniViewport->GetEngineBounds( x, y, width, height );
  168. }
  169. }
  170. vgui::Panel *CBaseToolSystem::GetMiniViewport( void )
  171. {
  172. return m_hMiniViewport;
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Shut down
  176. //-----------------------------------------------------------------------------
  177. void CBaseToolSystem::Shutdown()
  178. {
  179. if ( m_pBackground )
  180. {
  181. m_pBackground->DecrementReferenceCount();
  182. }
  183. if ( m_pLogo )
  184. {
  185. m_pLogo->DecrementReferenceCount();
  186. }
  187. if ( m_hMiniViewport.Get() )
  188. {
  189. delete m_hMiniViewport.Get();
  190. }
  191. // Make sure anything "marked for deletion"
  192. // actually gets deleted before this dll goes away
  193. vgui::ivgui()->RunFrame();
  194. }
  195. //-----------------------------------------------------------------------------
  196. // Can the tool quit?
  197. //-----------------------------------------------------------------------------
  198. bool CBaseToolSystem::CanQuit()
  199. {
  200. return true;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Client, server init + shutdown
  204. //-----------------------------------------------------------------------------
  205. bool CBaseToolSystem::ServerInit( CreateInterfaceFn serverFactory )
  206. {
  207. servertools = ( IServerTools * )serverFactory( VSERVERTOOLS_INTERFACE_VERSION, NULL );
  208. if ( !servertools )
  209. {
  210. Error( "CBaseToolSystem::PostInit: Unable to get '%s' interface from game .dll\n", VSERVERTOOLS_INTERFACE_VERSION );
  211. }
  212. return true;
  213. }
  214. bool CBaseToolSystem::ClientInit( CreateInterfaceFn clientFactory )
  215. {
  216. clienttools = ( IClientTools * )clientFactory( VCLIENTTOOLS_INTERFACE_VERSION, NULL );
  217. if ( !clienttools )
  218. {
  219. Error( "CBaseToolSystem::PostInit: Unable to get '%s' interface from client .dll\n", VCLIENTTOOLS_INTERFACE_VERSION );
  220. }
  221. else
  222. {
  223. g_pGlobalFlexController = &g_GlobalFlexController; // don't set this until clienttools is connected
  224. }
  225. return true;
  226. }
  227. void CBaseToolSystem::ServerShutdown()
  228. {
  229. servertools = NULL;
  230. }
  231. void CBaseToolSystem::ClientShutdown()
  232. {
  233. clienttools = NULL;
  234. }
  235. //-----------------------------------------------------------------------------
  236. // Level init, shutdown for server
  237. //-----------------------------------------------------------------------------
  238. void CBaseToolSystem::ServerLevelInitPreEntity()
  239. {
  240. }
  241. void CBaseToolSystem::ServerLevelInitPostEntity()
  242. {
  243. }
  244. void CBaseToolSystem::ServerLevelShutdownPreEntity()
  245. {
  246. }
  247. void CBaseToolSystem::ServerLevelShutdownPostEntity()
  248. {
  249. }
  250. //-----------------------------------------------------------------------------
  251. // Think methods
  252. //-----------------------------------------------------------------------------
  253. void CBaseToolSystem::ServerFrameUpdatePreEntityThink()
  254. {
  255. }
  256. void CBaseToolSystem::Think( bool finalTick )
  257. {
  258. // run vgui animations
  259. vgui::GetAnimationController()->UpdateAnimations( enginetools->Time() );
  260. }
  261. void CBaseToolSystem::PostMessage( HTOOLHANDLE hEntity, KeyValues *message )
  262. {
  263. if ( !Q_stricmp( message->GetName(), "ReleaseLayoffTexture" ) )
  264. {
  265. if ( m_hMiniViewport.Get() )
  266. {
  267. m_hMiniViewport->ReleaseLayoffTexture();
  268. }
  269. return;
  270. }
  271. }
  272. void CBaseToolSystem::ServerFrameUpdatePostEntityThink()
  273. {
  274. }
  275. void CBaseToolSystem::ServerPreClientUpdate()
  276. {
  277. }
  278. void CBaseToolSystem::ServerPreSetupVisibility()
  279. {
  280. }
  281. const char* CBaseToolSystem::GetEntityData( const char *pActualEntityData )
  282. {
  283. return pActualEntityData;
  284. }
  285. //-----------------------------------------------------------------------------
  286. // Level init, shutdown for client
  287. //-----------------------------------------------------------------------------
  288. void CBaseToolSystem::ClientLevelInitPreEntity()
  289. {
  290. }
  291. void CBaseToolSystem::ClientLevelInitPostEntity()
  292. {
  293. }
  294. void CBaseToolSystem::ClientLevelShutdownPreEntity()
  295. {
  296. }
  297. void CBaseToolSystem::ClientLevelShutdownPostEntity()
  298. {
  299. }
  300. void CBaseToolSystem::ClientPreRender()
  301. {
  302. }
  303. void CBaseToolSystem::ClientPostRender()
  304. {
  305. }
  306. //-----------------------------------------------------------------------------
  307. // Tool activation/deactivation
  308. //-----------------------------------------------------------------------------
  309. void CBaseToolSystem::OnToolActivate()
  310. {
  311. m_bIsActive = true;
  312. UpdateUIVisibility( );
  313. // FIXME: Note that this is necessary because IsGameInputEnabled depends on m_bIsActive at the moment
  314. OnModeChanged();
  315. input()->SetModalSubTree( VGui_GetToolRootPanel(), GetVPanel(), IsGameInputEnabled() );
  316. input()->SetModalSubTreeReceiveMessages( !IsGameInputEnabled() );
  317. m_pToolUI->UpdateMenuBarTitle();
  318. }
  319. void CBaseToolSystem::OnToolDeactivate()
  320. {
  321. m_bIsActive = false;
  322. UpdateUIVisibility( );
  323. // FIXME: Note that this is necessary because IsGameInputEnabled depends on m_bIsActive at the moment
  324. OnModeChanged();
  325. input()->ReleaseModalSubTree();
  326. }
  327. //-----------------------------------------------------------------------------
  328. // Let tool override key events (ie ESC and ~)
  329. //-----------------------------------------------------------------------------
  330. bool CBaseToolSystem::TrapKey( ButtonCode_t key, bool down )
  331. {
  332. // Don't hook keyboard if not topmost
  333. if ( !m_bIsActive )
  334. return false; // didn't trap, continue processing
  335. // If in fullscreen toolMode, don't let ECSAPE bring up the game menu
  336. if ( !m_bGameInputEnabled && m_bFullscreenMode && ( key == KEY_ESCAPE ) )
  337. return true; // trapping this key, stop processing
  338. if ( down )
  339. {
  340. if ( key == TOGGLE_WINDOWED_KEY_CODE )
  341. {
  342. SetMode( m_bGameInputEnabled, !m_bFullscreenMode );
  343. return true; // trapping this key, stop processing
  344. }
  345. if ( key == TOGGLE_INPUT_KEY_CODE )
  346. {
  347. if ( input()->IsKeyDown( KEY_LCONTROL ) || input()->IsKeyDown( KEY_RCONTROL ) )
  348. {
  349. ToggleForceToolCamera();
  350. }
  351. else
  352. {
  353. SetMode( !m_bGameInputEnabled, m_bFullscreenMode );
  354. }
  355. return true; // trapping this key, stop processing
  356. }
  357. // If in IFM mode, let ~ switch to gameMode and toggle console
  358. if ( !IsGameInputEnabled() && ( key == '~' || key == '`' ) )
  359. {
  360. SetMode( true, m_bFullscreenMode );
  361. return false; // didn't trap, continue processing
  362. }
  363. }
  364. return false; // didn't trap, continue processing
  365. }
  366. //-----------------------------------------------------------------------------
  367. // Shows, hides the tool ui (menu, client area, status bar)
  368. //-----------------------------------------------------------------------------
  369. void CBaseToolSystem::SetToolUIVisible( bool bVisible )
  370. {
  371. if ( bVisible != m_pToolUI->IsVisible() )
  372. {
  373. m_pToolUI->SetVisible( bVisible );
  374. m_pToolUI->InvalidateLayout();
  375. }
  376. }
  377. //-----------------------------------------------------------------------------
  378. // Computes whether vgui is visible or not
  379. //-----------------------------------------------------------------------------
  380. void CBaseToolSystem::UpdateUIVisibility()
  381. {
  382. bool bIsVisible = m_bIsActive && ( !IsGameInputEnabled() || !m_bFullscreenMode );
  383. ShowUI( bIsVisible );
  384. }
  385. //-----------------------------------------------------------------------------
  386. // Changes game input + fullscreen modes
  387. //-----------------------------------------------------------------------------
  388. void CBaseToolSystem::EnableFullscreenToolMode( bool bEnable )
  389. {
  390. m_bFullscreenToolModeEnabled = bEnable;
  391. }
  392. //-----------------------------------------------------------------------------
  393. // Changed whether camera is forced to be tool camera
  394. //-----------------------------------------------------------------------------
  395. void CBaseToolSystem::ToggleForceToolCamera()
  396. {
  397. }
  398. //-----------------------------------------------------------------------------
  399. // Changes game input + fullscreen modes
  400. //-----------------------------------------------------------------------------
  401. void CBaseToolSystem::SetMode( bool bGameInputEnabled, bool bFullscreen )
  402. {
  403. Assert( m_bIsActive );
  404. if ( !m_bFullscreenToolModeEnabled )
  405. {
  406. if ( !bGameInputEnabled )
  407. {
  408. bFullscreen = false;
  409. }
  410. }
  411. if ( ( m_bFullscreenMode == bFullscreen ) && ( m_bGameInputEnabled == bGameInputEnabled ) )
  412. return;
  413. bool bOldGameInputEnabled = m_bGameInputEnabled;
  414. m_bFullscreenMode = bFullscreen;
  415. m_bGameInputEnabled = bGameInputEnabled;
  416. UpdateUIVisibility();
  417. if ( bOldGameInputEnabled != m_bGameInputEnabled )
  418. {
  419. Warning( "Input is now being sent to the %s\n", m_bGameInputEnabled ? "Game" : "Tools" );
  420. // The subtree starts at the tool system root panel. If game input is enabled then
  421. // the subtree should not receive or process input messages, otherwise it should
  422. Assert( input()->GetModalSubTree() );
  423. if ( input()->GetModalSubTree() )
  424. {
  425. input()->SetModalSubTreeReceiveMessages( !m_bGameInputEnabled );
  426. }
  427. }
  428. if ( m_pToolUI )
  429. {
  430. m_pToolUI->UpdateMenuBarTitle();
  431. }
  432. OnModeChanged( );
  433. }
  434. //-----------------------------------------------------------------------------
  435. // Keybinding
  436. //-----------------------------------------------------------------------------
  437. void CBaseToolSystem::LoadKeyBindings()
  438. {
  439. ReloadKeyBindings( m_KeyBindingsHandle );
  440. }
  441. void CBaseToolSystem::ShowKeyBindingsEditor( Panel *panel, KeyBindingContextHandle_t handle )
  442. {
  443. if ( !m_hKeyBindingsEditor.Get() )
  444. {
  445. // Show the editor
  446. m_hKeyBindingsEditor = new CKeyBoardEditorDialog( GetClientArea(), panel, handle );
  447. m_hKeyBindingsEditor->DoModal();
  448. }
  449. }
  450. void CBaseToolSystem::ShowKeyBindingsHelp( Panel *panel, KeyBindingContextHandle_t handle, vgui::KeyCode boundKey, int modifiers )
  451. {
  452. if ( m_hKeyBindingsHelp.Get() )
  453. {
  454. m_hKeyBindingsHelp->HelpKeyPressed();
  455. return;
  456. }
  457. m_hKeyBindingsHelp = new CKeyBindingHelpDialog( GetClientArea(), panel, handle, boundKey, modifiers );
  458. }
  459. vgui::KeyBindingContextHandle_t CBaseToolSystem::GetKeyBindingsHandle()
  460. {
  461. return m_KeyBindingsHandle;
  462. }
  463. void CBaseToolSystem::OnEditKeyBindings()
  464. {
  465. Panel *tool = GetMostRecentlyFocusedTool();
  466. if ( tool )
  467. {
  468. ShowKeyBindingsEditor( tool, tool->GetKeyBindingsContext() );
  469. }
  470. }
  471. void CBaseToolSystem::OnKeyBindingHelp()
  472. {
  473. Panel *tool = GetMostRecentlyFocusedTool();
  474. if ( tool )
  475. {
  476. CUtlVector< BoundKey_t * > list;
  477. LookupBoundKeys( "keybindinghelp", list );
  478. if ( list.Count() > 0 )
  479. {
  480. ShowKeyBindingsHelp( tool, tool->GetKeyBindingsContext(), (KeyCode)list[ 0 ]->keycode, list[ 0 ]->modifiers );
  481. }
  482. }
  483. }
  484. //-----------------------------------------------------------------------------
  485. // Registers tool window
  486. //-----------------------------------------------------------------------------
  487. void CBaseToolSystem::RegisterToolWindow( vgui::PHandle hPanel )
  488. {
  489. int i = m_Tools.AddToTail( hPanel );
  490. m_Tools[i]->SetKeyBindingsContext( m_KeyBindingsHandle );
  491. }
  492. void CBaseToolSystem::UnregisterAllToolWindows()
  493. {
  494. m_Tools.RemoveAll();
  495. m_MostRecentlyFocused = NULL;
  496. }
  497. Panel *CBaseToolSystem::GetMostRecentlyFocusedTool()
  498. {
  499. VPANEL focus = input()->GetFocus();
  500. int c = m_Tools.Count();
  501. for ( int i = 0; i < c; ++i )
  502. {
  503. Panel *p = m_Tools[ i ].Get();
  504. if ( !p )
  505. continue;
  506. // Not a visible tool
  507. if ( !p->GetParent() )
  508. continue;
  509. bool hasFocus = p->HasFocus();
  510. bool focusOnChild = focus && ipanel()->HasParent(focus, p->GetVPanel());
  511. if ( !hasFocus && !focusOnChild )
  512. {
  513. continue;
  514. }
  515. return p;
  516. }
  517. return m_MostRecentlyFocused.Get();
  518. }
  519. void CBaseToolSystem::PostMessageToActiveTool( KeyValues *pKeyValues, float flDelay )
  520. {
  521. Panel *pMostRecent = GetMostRecentlyFocusedTool();
  522. if ( pMostRecent )
  523. {
  524. Panel::PostMessage( pMostRecent->GetVPanel(), pKeyValues, flDelay );
  525. }
  526. }
  527. void CBaseToolSystem::PostMessageToActiveTool( const char *msg, float flDelay )
  528. {
  529. Panel *pMostRecent = GetMostRecentlyFocusedTool();
  530. if ( pMostRecent )
  531. {
  532. Panel::PostMessage( pMostRecent->GetVPanel(), new KeyValues( msg ), flDelay );
  533. }
  534. }
  535. void CBaseToolSystem::PostMessageToAllTools( KeyValues *message )
  536. {
  537. int nCount = enginetools->GetToolCount();
  538. for ( int i = 0; i < nCount; ++i )
  539. {
  540. IToolSystem *pToolSystem = const_cast<IToolSystem*>( enginetools->GetToolSystem( i ) );
  541. pToolSystem->PostMessage( HTOOLHANDLE_INVALID, message );
  542. }
  543. }
  544. void CBaseToolSystem::OnThink()
  545. {
  546. BaseClass::OnThink();
  547. VPANEL focus = input()->GetFocus();
  548. int c = m_Tools.Count();
  549. for ( int i = 0; i < c; ++i )
  550. {
  551. Panel *p = m_Tools[ i ].Get();
  552. if ( !p )
  553. continue;
  554. // Not a visible tool
  555. if ( !p->GetParent() )
  556. continue;
  557. bool hasFocus = p->HasFocus();
  558. bool focusOnChild = focus && ipanel()->HasParent(focus, p->GetVPanel());
  559. if ( !hasFocus && !focusOnChild )
  560. continue;
  561. m_MostRecentlyFocused = p;
  562. break;
  563. }
  564. }
  565. //-----------------------------------------------------------------------------
  566. // Let tool override viewport for engine
  567. //-----------------------------------------------------------------------------
  568. void CBaseToolSystem::AdjustEngineViewport( int& x, int& y, int& width, int& height )
  569. {
  570. if ( !m_hMiniViewport.Get() )
  571. return;
  572. bool enabled;
  573. int vpx, vpy, vpw, vph;
  574. m_hMiniViewport->GetViewport( enabled, vpx, vpy, vpw, vph );
  575. if ( !enabled )
  576. return;
  577. x = vpx;
  578. y = vpy;
  579. width = vpw;
  580. height = vph;
  581. }
  582. //-----------------------------------------------------------------------------
  583. // Let tool override view/camera
  584. //-----------------------------------------------------------------------------
  585. bool CBaseToolSystem::SetupEngineView( Vector &origin, QAngle &angles, float &fov )
  586. {
  587. return false;
  588. }
  589. //-----------------------------------------------------------------------------
  590. // Let tool override microphone
  591. //-----------------------------------------------------------------------------
  592. bool CBaseToolSystem::SetupAudioState( AudioState_t &audioState )
  593. {
  594. return false;
  595. }
  596. //-----------------------------------------------------------------------------
  597. // Should the game be allowed to render the view?
  598. //-----------------------------------------------------------------------------
  599. bool CBaseToolSystem::ShouldGameRenderView()
  600. {
  601. // Render through mini viewport unless in fullscreen mode
  602. if ( !IsVisible() )
  603. {
  604. return true;
  605. }
  606. if ( !m_hMiniViewport.Get() )
  607. return true;
  608. if ( !m_hMiniViewport->IsVisible() )
  609. {
  610. return true;
  611. }
  612. // Route through mini viewport
  613. return false;
  614. }
  615. bool CBaseToolSystem::IsThirdPersonCamera()
  616. {
  617. return false;
  618. }
  619. bool CBaseToolSystem::IsToolRecording()
  620. {
  621. return false;
  622. }
  623. IMaterialProxy *CBaseToolSystem::LookupProxy( const char *proxyName )
  624. {
  625. return NULL;
  626. }
  627. bool CBaseToolSystem::GetSoundSpatialization( int iUserData, int guid, SpatializationInfo_t& info )
  628. {
  629. // Always hearable (no changes)
  630. return true;
  631. }
  632. void CBaseToolSystem::HostRunFrameBegin()
  633. {
  634. }
  635. void CBaseToolSystem::HostRunFrameEnd()
  636. {
  637. }
  638. void CBaseToolSystem::RenderFrameBegin()
  639. {
  640. // If we can't see the engine window, do nothing
  641. if ( !IsVisible() || !IsActiveTool() )
  642. return;
  643. if ( !m_hMiniViewport.Get() || !m_hMiniViewport->IsVisible() )
  644. return;
  645. m_hMiniViewport->RenderFrameBegin();
  646. }
  647. void CBaseToolSystem::RenderFrameEnd()
  648. {
  649. }
  650. void CBaseToolSystem::VGui_PreRender( int paintMode )
  651. {
  652. }
  653. void CBaseToolSystem::VGui_PostRender( int paintMode )
  654. {
  655. }
  656. void CBaseToolSystem::VGui_PreSimulate()
  657. {
  658. if ( !m_bIsActive )
  659. return;
  660. // only show the gameUI when in gameMode
  661. vgui::VPANEL gameui = enginevgui->GetPanel( PANEL_GAMEUIDLL );
  662. if ( gameui != 0 )
  663. {
  664. bool wantsToBeSeen = IsGameInputEnabled() && (enginetools->IsGamePaused() || !enginetools->IsInGame() || enginetools->IsConsoleVisible());
  665. vgui::ipanel()->SetVisible(gameui, wantsToBeSeen);
  666. }
  667. // if there's no map loaded and we're in fullscreen toolMode, switch to gameMode
  668. // otherwise there's nothing to see or do...
  669. if ( !IsGameInputEnabled() && !IsVisible() && !enginetools->IsInGame() )
  670. {
  671. SetMode( true, m_bFullscreenMode );
  672. }
  673. }
  674. void CBaseToolSystem::VGui_PostSimulate()
  675. {
  676. }
  677. const char *CBaseToolSystem::MapName() const
  678. {
  679. return enginetools->GetCurrentMap();
  680. }
  681. //-----------------------------------------------------------------------------
  682. // Shows or hides the UI
  683. //-----------------------------------------------------------------------------
  684. bool CBaseToolSystem::ShowUI( bool bVisible )
  685. {
  686. bool bPrevVisible = IsVisible();
  687. if ( bPrevVisible == bVisible )
  688. return bPrevVisible;
  689. SetMouseInputEnabled( bVisible );
  690. SetVisible( bVisible );
  691. // Hide loading image if using bx movie UI
  692. // A bit of a hack because it more or less tunnels through to the client .dll, but the moviemaker assumes
  693. // single player anyway...
  694. if ( bVisible )
  695. {
  696. ConVar *pCv = ( ConVar * )cvar->FindVar( "cl_showpausedimage" );
  697. if ( pCv )
  698. {
  699. pCv->SetValue( 0 );
  700. }
  701. }
  702. return bPrevVisible;
  703. }
  704. //-----------------------------------------------------------------------------
  705. // Gets the action target to sent to panels so that the tool system's OnCommand is called
  706. //-----------------------------------------------------------------------------
  707. vgui::Panel *CBaseToolSystem::GetActionTarget()
  708. {
  709. return this;
  710. }
  711. //-----------------------------------------------------------------------------
  712. // Derived classes implement this to create a custom menubar
  713. //-----------------------------------------------------------------------------
  714. vgui::MenuBar *CBaseToolSystem::CreateMenuBar(CBaseToolSystem *pParent )
  715. {
  716. return new vgui::MenuBar( pParent, "ToolMenuBar" );
  717. }
  718. //-----------------------------------------------------------------------------
  719. // Purpose: Derived classes implement this to create a custom status bar, or return NULL for no status bar
  720. //-----------------------------------------------------------------------------
  721. vgui::Panel *CBaseToolSystem::CreateStatusBar( vgui::Panel *pParent )
  722. {
  723. return new CBaseStatusBar( this, "Status Bar" );
  724. }
  725. //-----------------------------------------------------------------------------
  726. // Gets at the action menu
  727. //-----------------------------------------------------------------------------
  728. vgui::Menu *CBaseToolSystem::GetActionMenu()
  729. {
  730. return m_hActionMenu;
  731. }
  732. //-----------------------------------------------------------------------------
  733. // Returns the client area
  734. //-----------------------------------------------------------------------------
  735. vgui::Panel* CBaseToolSystem::GetClientArea()
  736. {
  737. return m_pToolUI->GetClientArea();
  738. }
  739. //-----------------------------------------------------------------------------
  740. // Pops up the action menu
  741. //-----------------------------------------------------------------------------
  742. void CBaseToolSystem::OnMousePressed( vgui::MouseCode code )
  743. {
  744. if ( code == MOUSE_RIGHT )
  745. {
  746. InitActionMenu();
  747. }
  748. else
  749. {
  750. BaseClass::OnMousePressed( code );
  751. }
  752. }
  753. //-----------------------------------------------------------------------------
  754. // Creates the action menu
  755. //-----------------------------------------------------------------------------
  756. void CBaseToolSystem::InitActionMenu()
  757. {
  758. ShutdownActionMenu();
  759. // Let the tool system create the action menu
  760. m_hActionMenu = CreateActionMenu( this );
  761. if ( m_hActionMenu.Get() )
  762. {
  763. m_hActionMenu->SetVisible(true);
  764. PositionActionMenu();
  765. m_hActionMenu->RequestFocus();
  766. }
  767. }
  768. //-----------------------------------------------------------------------------
  769. // Destroy action menu
  770. //-----------------------------------------------------------------------------
  771. void CBaseToolSystem::ShutdownActionMenu()
  772. {
  773. if ( m_hActionMenu.Get() )
  774. {
  775. m_hActionMenu->MarkForDeletion();
  776. m_hActionMenu = NULL;
  777. }
  778. }
  779. void CBaseToolSystem::UpdateMenu( vgui::Menu *menu )
  780. {
  781. // Nothing
  782. }
  783. //-----------------------------------------------------------------------------
  784. // Positions the action menu when it's time to pop it up
  785. //-----------------------------------------------------------------------------
  786. void CBaseToolSystem::PositionActionMenu()
  787. {
  788. // get cursor position, this is local to this text edit window
  789. int cursorX, cursorY;
  790. input()->GetCursorPos(cursorX, cursorY);
  791. // relayout the menu immediately so that we know it's size
  792. m_hActionMenu->InvalidateLayout(true);
  793. // Get the menu size
  794. int menuWide, menuTall;
  795. m_hActionMenu->GetSize( menuWide, menuTall );
  796. // work out where the cursor is and therefore the best place to put the menu
  797. int wide, tall;
  798. GetSize( wide, tall );
  799. if (wide - menuWide > cursorX)
  800. {
  801. // menu hanging right
  802. if (tall - menuTall > cursorY)
  803. {
  804. // menu hanging down
  805. m_hActionMenu->SetPos(cursorX, cursorY);
  806. }
  807. else
  808. {
  809. // menu hanging up
  810. m_hActionMenu->SetPos(cursorX, cursorY - menuTall);
  811. }
  812. }
  813. else
  814. {
  815. // menu hanging left
  816. if (tall - menuTall > cursorY)
  817. {
  818. // menu hanging down
  819. m_hActionMenu->SetPos(cursorX - menuWide, cursorY);
  820. }
  821. else
  822. {
  823. // menu hanging up
  824. m_hActionMenu->SetPos(cursorX - menuWide, cursorY - menuTall);
  825. }
  826. }
  827. }
  828. //-----------------------------------------------------------------------------
  829. // Handles the clear recent files message
  830. //-----------------------------------------------------------------------------
  831. void CBaseToolSystem::OnClearRecent()
  832. {
  833. m_RecentFiles.Clear();
  834. m_RecentFiles.SaveToRegistry( GetRegistryName() );
  835. }
  836. //-----------------------------------------------------------------------------
  837. // Called by the file open state machine
  838. //-----------------------------------------------------------------------------
  839. void CBaseToolSystem::OnFileStateMachineFinished( KeyValues *pKeyValues )
  840. {
  841. KeyValues *pContext = pKeyValues->GetFirstTrueSubKey();
  842. bool bWroteFile = pKeyValues->GetInt( "wroteFile", 0 ) != 0;
  843. vgui::FileOpenStateMachine::CompletionState_t state = (vgui::FileOpenStateMachine::CompletionState_t)pKeyValues->GetInt( "completionState", vgui::FileOpenStateMachine::IN_PROGRESS );
  844. const char *pFileType = pKeyValues->GetString( "fileType" );
  845. OnFileOperationCompleted( pFileType, bWroteFile, state, pContext );
  846. }
  847. //-----------------------------------------------------------------------------
  848. // Show the File browser dialog
  849. //-----------------------------------------------------------------------------
  850. void CBaseToolSystem::OpenFile( const char *pOpenFileType, const char *pSaveFileName, const char *pSaveFileType, int nFlags, KeyValues *pContextKeyValues )
  851. {
  852. m_pFileOpenStateMachine->OpenFile( pOpenFileType, pContextKeyValues, pSaveFileName, pSaveFileType, nFlags );
  853. }
  854. void CBaseToolSystem::OpenFile( const char *pOpenFileName, const char *pOpenFileType, const char *pSaveFileName, const char *pSaveFileType, int nFlags, KeyValues *pContextKeyValues )
  855. {
  856. m_pFileOpenStateMachine->OpenFile( pOpenFileName, pOpenFileType, pContextKeyValues, pSaveFileName, pSaveFileType, nFlags );
  857. }
  858. //-----------------------------------------------------------------------------
  859. // Used to save a specified file, and deal with all the lovely dialogs
  860. //-----------------------------------------------------------------------------
  861. void CBaseToolSystem::SaveFile( const char *pFileName, const char *pFileType, int nFlags, KeyValues *pContextKeyValues )
  862. {
  863. m_pFileOpenStateMachine->SaveFile( pContextKeyValues, pFileName, pFileType, nFlags );
  864. }
  865. //-----------------------------------------------------------------------------
  866. // Paints the background
  867. //-----------------------------------------------------------------------------
  868. void CBaseToolSystem::PaintBackground()
  869. {
  870. int w, h;
  871. GetSize( w, h );
  872. int x, y;
  873. GetPos( x, y );
  874. LocalToScreen( x, y );
  875. CMatRenderContextPtr pRenderContext( materials );
  876. if ( m_pBackground )
  877. {
  878. int texWide = m_pBackground->GetMappingWidth();
  879. int texTall = m_pBackground->GetMappingHeight();
  880. float maxu = (float)w / (float)texWide;
  881. float maxv = (float)h / (float)texTall;
  882. RenderQuad( m_pBackground, x, y, w, h, surface()->GetZPos(), 0.0f, 0.0f, maxu, maxv, Color( 255, 255, 255, 255 ) );
  883. }
  884. bool hasDoc = HasDocument();
  885. if ( m_pLogo )
  886. {
  887. int texWide = m_pLogo->GetMappingWidth();
  888. float logoAspectRatio = 0.442;
  889. if ( hasDoc )
  890. {
  891. int logoW = texWide / 2;
  892. int logoH = logoW * logoAspectRatio;
  893. x = w - logoW - 15;
  894. y = h - logoH - 30;
  895. w = logoW;
  896. h = logoH;
  897. }
  898. else
  899. {
  900. int logoW = texWide;
  901. int logoH = logoW * logoAspectRatio;
  902. x = ( w - logoW ) / 2;
  903. y = ( h - logoH ) / 2;
  904. w = logoW;
  905. h = logoH;
  906. }
  907. int alpha = hasDoc ? 0 : 255;
  908. RenderQuad( m_pLogo, x, y, w, h, surface()->GetZPos(), 0.0f, 0.0f, 1.0f, 1.0f, Color( 255, 255, 255, alpha ) );
  909. }
  910. }
  911. const char *CBaseToolSystem::GetBackgroundTextureName()
  912. {
  913. return "vgui/tools/ifm/ifm_background";
  914. }
  915. bool CBaseToolSystem::HasDocument()
  916. {
  917. return false;
  918. }
  919. CMiniViewport *CBaseToolSystem::CreateMiniViewport( vgui::Panel *parent )
  920. {
  921. int w, h;
  922. surface()->GetScreenSize( w, h );
  923. CMiniViewport *vp = new CMiniViewport( parent, "MiniViewport" );
  924. Assert( vp );
  925. vp->SetVisible( true );
  926. int menuBarHeight = 28;
  927. int titleBarHeight = 22;
  928. int offset = 4;
  929. vp->SetBounds( ( 2 * w / 3 ) - offset, menuBarHeight + offset, w / 3, h / 3 + titleBarHeight);
  930. return vp;
  931. }
  932. void CBaseToolSystem::ComputeMenuBarTitle( char *buf, size_t buflen )
  933. {
  934. Q_snprintf( buf, buflen, ": %s [ %s - Switch Mode ] [ %s - Full Screen ]", IsGameInputEnabled() ? "Game Mode" : "Tool Mode", TOGGLE_INPUT_KEY_NAME, TOGGLE_WINDOWED_KEY_NAME );
  935. }
  936. void CBaseToolSystem::OnUnhandledMouseClick( int code )
  937. {
  938. if ( (MouseCode)code == MOUSE_LEFT )
  939. {
  940. // If tool ui is visible and we're running game in a window
  941. // and they click on the ifm it'll be unhandled, but in this case
  942. // we'll switch back to the IFM mode
  943. if ( !IsFullscreen() && IsGameInputEnabled() )
  944. {
  945. SetMode( false, m_bFullscreenMode );
  946. }
  947. }
  948. }