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.

341 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Core Movie Maker UI API
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "toolutils/basetoolsystem.h"
  8. #include "toolutils/recentfilelist.h"
  9. #include "toolutils/toolmenubar.h"
  10. #include "toolutils/toolswitchmenubutton.h"
  11. #include "toolutils/toolfilemenubutton.h"
  12. #include "toolutils/toolmenubutton.h"
  13. #include "vgui_controls/Menu.h"
  14. #include "tier1/KeyValues.h"
  15. #include "toolutils/enginetools_int.h"
  16. #include "toolframework/ienginetool.h"
  17. #include "vgui/IInput.h"
  18. #include "vgui/KeyCode.h"
  19. #include "vgui_controls/FileOpenDialog.h"
  20. #include "filesystem.h"
  21. #include "vgui/ilocalize.h"
  22. #include "dme_controls/elementpropertiestree.h"
  23. #include "tier0/icommandline.h"
  24. #include "materialsystem/imaterialsystem.h"
  25. #include "VGuiMatSurface/IMatSystemSurface.h"
  26. #include "tier3/tier3.h"
  27. #include "tier2/fileutils.h"
  28. using namespace vgui;
  29. const char *GetVGuiControlsModuleName()
  30. {
  31. return "SampleTool";
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Connect, disconnect
  35. //-----------------------------------------------------------------------------
  36. bool ConnectTools( CreateInterfaceFn factory )
  37. {
  38. return (materials != NULL) && (g_pMatSystemSurface != NULL);
  39. }
  40. void DisconnectTools( )
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Implementation of the sample tool
  45. //-----------------------------------------------------------------------------
  46. class CSampleTool : public CBaseToolSystem, public IFileMenuCallbacks
  47. {
  48. DECLARE_CLASS_SIMPLE( CSampleTool, CBaseToolSystem );
  49. public:
  50. CSampleTool();
  51. // Inherited from IToolSystem
  52. virtual const char *GetToolName() { return "Sample Tool"; }
  53. virtual const char *GetBindingsContextFile() { return "cfg/SampleTool.kb"; }
  54. virtual bool Init( );
  55. virtual void Shutdown();
  56. // Inherited from IFileMenuCallbacks
  57. virtual int GetFileMenuItemsEnabled( );
  58. virtual void AddRecentFilesToMenu( vgui::Menu *menu );
  59. virtual bool GetPerforceFileName( char *pFileName, int nMaxLen ) { return false; }
  60. virtual vgui::Panel* GetRootPanel() { return this; }
  61. // Inherited from CBaseToolSystem
  62. virtual vgui::HScheme GetToolScheme();
  63. virtual vgui::Menu *CreateActionMenu( vgui::Panel *pParent );
  64. virtual void OnCommand( const char *cmd );
  65. virtual const char *GetRegistryName() { return "SampleTool"; }
  66. virtual vgui::MenuBar *CreateMenuBar( CBaseToolSystem *pParent );
  67. public:
  68. MESSAGE_FUNC( OnNew, "OnNew" );
  69. MESSAGE_FUNC( OnOpen, "OnOpen" );
  70. MESSAGE_FUNC( OnSave, "OnSave" );
  71. MESSAGE_FUNC( OnSaveAs, "OnSaveAs" );
  72. MESSAGE_FUNC( OnClose, "OnClose" );
  73. MESSAGE_FUNC( OnCloseNoSave, "OnCloseNoSave" );
  74. MESSAGE_FUNC( OnMarkNotDirty, "OnMarkNotDirty" );
  75. MESSAGE_FUNC( OnExit, "OnExit" );
  76. void OpenFileFromHistory( int slot );
  77. virtual void SetupFileOpenDialog( vgui::FileOpenDialog *pDialog, bool bOpenFile, const char *pFileFormat, KeyValues *pContextKeyValues );
  78. virtual bool OnReadFileFromDisk( const char *pFileName, const char *pFileFormat, KeyValues *pContextKeyValues );
  79. virtual bool OnWriteFileToDisk( const char *pFileName, const char *pFileFormat, KeyValues *pContextKeyValues );
  80. virtual void OnFileOperationCompleted( const char *pFileType, bool bWroteFile, vgui::FileOpenStateMachine::CompletionState_t state, KeyValues *pContextKeyValues );
  81. private:
  82. // Loads up a new document
  83. void LoadDocument( const char *pDocName );
  84. // Updates the menu bar based on the current file
  85. void UpdateMenuBar( );
  86. // Shows element properties
  87. void ShowElementProperties( );
  88. virtual const char *GetLogoTextureName();
  89. };
  90. //-----------------------------------------------------------------------------
  91. // Singleton
  92. //-----------------------------------------------------------------------------
  93. CSampleTool *g_pSampleTool = NULL;
  94. void CreateTools()
  95. {
  96. g_pSampleTool = new CSampleTool();
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Constructor
  100. //-----------------------------------------------------------------------------
  101. CSampleTool::CSampleTool()
  102. {
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Init, shutdown
  106. //-----------------------------------------------------------------------------
  107. bool CSampleTool::Init( )
  108. {
  109. m_RecentFiles.LoadFromRegistry( GetRegistryName() );
  110. // NOTE: This has to happen before BaseClass::Init
  111. // g_pVGuiLocalize->AddFile( "resource/toolsample_%language%.txt" );
  112. if ( !BaseClass::Init( ) )
  113. return false;
  114. return true;
  115. }
  116. void CSampleTool::Shutdown()
  117. {
  118. m_RecentFiles.SaveToRegistry( GetRegistryName() );
  119. BaseClass::Shutdown();
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Derived classes can implement this to get a new scheme to be applied to this tool
  123. //-----------------------------------------------------------------------------
  124. vgui::HScheme CSampleTool::GetToolScheme()
  125. {
  126. return vgui::scheme()->LoadSchemeFromFile( "Resource/BoxRocket.res", "SampleTool" );
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Initializes the menu bar
  130. //-----------------------------------------------------------------------------
  131. vgui::MenuBar *CSampleTool::CreateMenuBar( CBaseToolSystem *pParent )
  132. {
  133. CToolMenuBar *pMenuBar = new CToolMenuBar( pParent, "Main Menu Bar" );
  134. // Sets info in the menu bar
  135. char title[ 64 ];
  136. ComputeMenuBarTitle( title, sizeof( title ) );
  137. pMenuBar->SetInfo( title );
  138. pMenuBar->SetToolName( GetToolName() );
  139. // Add menu buttons
  140. CToolMenuButton *pFileButton = CreateToolFileMenuButton( pMenuBar, "File", "&File", GetActionTarget(), this );
  141. CToolMenuButton *pSwitchButton = CreateToolSwitchMenuButton( pMenuBar, "Switcher", "&Tools", GetActionTarget() );
  142. pMenuBar->AddButton( pFileButton );
  143. pMenuBar->AddButton( pSwitchButton );
  144. return pMenuBar;
  145. }
  146. //-----------------------------------------------------------------------------
  147. // Creates the action menu
  148. //-----------------------------------------------------------------------------
  149. vgui::Menu *CSampleTool::CreateActionMenu( vgui::Panel *pParent )
  150. {
  151. vgui::Menu *pActionMenu = new Menu( pParent, "ActionMenu" );
  152. pActionMenu->AddMenuItem( "#ToolHide", new KeyValues( "Command", "command", "HideActionMenu" ), GetActionTarget() );
  153. return pActionMenu;
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Inherited from IFileMenuCallbacks
  157. //-----------------------------------------------------------------------------
  158. int CSampleTool::GetFileMenuItemsEnabled( )
  159. {
  160. int nFlags = FILE_ALL;
  161. if ( m_RecentFiles.IsEmpty() )
  162. {
  163. nFlags &= ~(FILE_RECENT | FILE_CLEAR_RECENT);
  164. }
  165. return nFlags;
  166. }
  167. void CSampleTool::AddRecentFilesToMenu( vgui::Menu *pMenu )
  168. {
  169. m_RecentFiles.AddToMenu( pMenu, GetActionTarget(), "OnRecent" );
  170. }
  171. //-----------------------------------------------------------------------------
  172. // Purpose:
  173. // Input : -
  174. //-----------------------------------------------------------------------------
  175. void CSampleTool::OnExit()
  176. {
  177. enginetools->Command( "quit\n" );
  178. }
  179. //-----------------------------------------------------------------------------
  180. // Handle commands from the action menu and other menus
  181. //-----------------------------------------------------------------------------
  182. void CSampleTool::OnCommand( const char *cmd )
  183. {
  184. if ( !V_stricmp( cmd, "HideActionMenu" ) )
  185. {
  186. if ( GetActionMenu() )
  187. {
  188. GetActionMenu()->SetVisible( false );
  189. }
  190. }
  191. else if ( const char *pSuffix = StringAfterPrefix( cmd, "OnRecent" ) )
  192. {
  193. int idx = Q_atoi( pSuffix );
  194. OpenFileFromHistory( idx );
  195. }
  196. else if( const char *pSuffix = StringAfterPrefix( cmd, "OnTool" ) )
  197. {
  198. int idx = Q_atoi( pSuffix );
  199. enginetools->SwitchToTool( idx );
  200. }
  201. else
  202. {
  203. BaseClass::OnCommand( cmd );
  204. }
  205. }
  206. //-----------------------------------------------------------------------------
  207. // Command handlers
  208. //-----------------------------------------------------------------------------
  209. void CSampleTool::OnNew()
  210. {
  211. // FIXME: Implement
  212. }
  213. void CSampleTool::OnOpen()
  214. {
  215. OpenFile( "txt" );
  216. }
  217. bool CSampleTool::OnReadFileFromDisk( const char *pFileName, const char *pFileFormat, KeyValues *pContextKeyValues )
  218. {
  219. // FIXME: Implement
  220. m_RecentFiles.Add( pFileName, pFileFormat );
  221. return true;
  222. }
  223. void CSampleTool::OnSave()
  224. {
  225. // FIXME: Implement
  226. }
  227. void CSampleTool::OnSaveAs()
  228. {
  229. SaveFile( NULL, NULL, 0 );
  230. }
  231. bool CSampleTool::OnWriteFileToDisk( const char *pFileName, const char *pFileFormat, KeyValues *pContextKeyValues )
  232. {
  233. // FIXME: Implement
  234. m_RecentFiles.Add( pFileName, pFileFormat );
  235. return true;
  236. }
  237. void CSampleTool::OnClose()
  238. {
  239. // FIXME: Implement
  240. }
  241. void CSampleTool::OnCloseNoSave()
  242. {
  243. // FIXME: Implement
  244. }
  245. void CSampleTool::OnMarkNotDirty()
  246. {
  247. // FIXME: Implement
  248. }
  249. //-----------------------------------------------------------------------------
  250. // Show the save document query dialog
  251. //-----------------------------------------------------------------------------
  252. void CSampleTool::OpenFileFromHistory( int slot )
  253. {
  254. const char *pFileName = m_RecentFiles.GetFile( slot );
  255. OnReadFileFromDisk( pFileName, NULL, 0 );
  256. }
  257. //-----------------------------------------------------------------------------
  258. // Called when file operations complete
  259. //-----------------------------------------------------------------------------
  260. void CSampleTool::OnFileOperationCompleted( const char *pFileType, bool bWroteFile, vgui::FileOpenStateMachine::CompletionState_t state, KeyValues *pContextKeyValues )
  261. {
  262. // FIXME: Implement
  263. }
  264. //-----------------------------------------------------------------------------
  265. // Show the File browser dialog
  266. //-----------------------------------------------------------------------------
  267. void CSampleTool::SetupFileOpenDialog( vgui::FileOpenDialog *pDialog, bool bOpenFile, const char *pFileFormat, KeyValues *pContextKeyValues )
  268. {
  269. char pStartingDir[ MAX_PATH ];
  270. GetModSubdirectory( NULL, pStartingDir, sizeof(pStartingDir) );
  271. pDialog->SetTitle( "Choose SampleTool .txt file", true );
  272. pDialog->SetStartDirectoryContext( "sample_session", pStartingDir );
  273. pDialog->AddFilter( "*.txt", "SampleTool (*.txt)", true );
  274. }
  275. const char *CSampleTool::GetLogoTextureName()
  276. {
  277. return "vgui/tools/sampletool/sampletool_logo";
  278. }