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.

202 lines
5.9 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Core Movie Maker UI API
  4. //
  5. //=============================================================================
  6. #include "toolutils/toolswitchmenubutton.h"
  7. #include "vgui_controls/panel.h"
  8. #include "toolutils/toolmenubutton.h"
  9. #include "toolutils/enginetools_int.h"
  10. #include "tier1/keyvalues.h"
  11. #include "vgui_controls/menu.h"
  12. #include "toolframework/ienginetool.h"
  13. #include "vgui_controls/Frame.h"
  14. #include "vgui_controls/Button.h"
  15. #include "vgui_controls/ListPanel.h"
  16. #include "filesystem.h"
  17. #include "tier1/fmtstr.h"
  18. #include "toolframework/itoolframework.h"
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. using namespace vgui;
  22. class CLoadToolDialog : public Frame
  23. {
  24. DECLARE_CLASS_SIMPLE( CLoadToolDialog, Frame );
  25. public:
  26. CLoadToolDialog( Panel *panel );
  27. protected:
  28. virtual void OnCommand( const char *pCommand );
  29. ListPanel *m_pModuleList;
  30. Button *m_pLoadButton;
  31. Button *m_pCancelButton;
  32. };
  33. CLoadToolDialog::CLoadToolDialog( Panel *panel ) :
  34. BaseClass( panel, "LoadToolDialog" )
  35. {
  36. m_pModuleList = new ListPanel( this, "ModuleList" );
  37. m_pModuleList->AddActionSignalTarget( this );
  38. m_pModuleList->AddColumnHeader( 0, "text", "Text", 250, ListPanel::COLUMN_RESIZEWITHWINDOW );
  39. m_pModuleList->SetMultiselectEnabled( true );
  40. m_pLoadButton = new Button( this, "OkButton", "#VGui_OK", this, "Ok" );
  41. m_pCancelButton = new Button( this, "Cancel", "#vgui_Cancel", this, "Cancel" );
  42. SetSize( 300, 400 );
  43. SetSizeable( true );
  44. SetMoveable( true );
  45. // Iterate loadable tools
  46. // Search the directory structure.
  47. char searchpath[ MAX_PATH ];
  48. Q_strncpy( searchpath, "tools/*.dll", sizeof(searchpath) );
  49. CUtlVector< CUtlString > toolmodules;
  50. FileFindHandle_t handle;
  51. char const *findfn = g_pFullFileSystem->FindFirstEx( searchpath, "EXECUTABLE_PATH", &handle );
  52. while ( findfn )
  53. {
  54. if ( !g_pFullFileSystem->FindIsDirectory( handle ) )
  55. {
  56. char sz[ MAX_PATH ] = { 0 };
  57. Q_FileBase( findfn, sz, sizeof( sz ) );
  58. toolmodules.AddToTail( CUtlString( sz ) );
  59. }
  60. findfn = g_pFullFileSystem->FindNext( handle );
  61. }
  62. g_pFullFileSystem->FindClose( handle );
  63. for ( int i = 0; i < toolmodules.Count(); ++i )
  64. {
  65. KeyValues *item = new KeyValues( "item" );
  66. item->SetString( "text", toolmodules[ i ].String() );
  67. m_pModuleList->AddItem( item, 0, false, false );
  68. }
  69. LoadControlSettings( "resource/loadtooldialog.res" );
  70. }
  71. void CLoadToolDialog::OnCommand( const char *pCommand )
  72. {
  73. if ( !Q_stricmp( pCommand, "Ok" ) )
  74. {
  75. KeyValues *pActionKeys = new KeyValues( "OnLoadTools" );
  76. int nCount = m_pModuleList->GetSelectedItemsCount();
  77. pActionKeys->SetInt( "count", nCount );
  78. for ( int i = 0; i < nCount; ++i )
  79. {
  80. int itemID = m_pModuleList->GetSelectedItem( i );
  81. KeyValues *item = m_pModuleList->GetItem( itemID );
  82. pActionKeys->SetString( CFmtStr( "%i", i ), item->GetString( "text" ) );
  83. }
  84. CloseModal();
  85. PostActionSignal( pActionKeys );
  86. return;
  87. }
  88. if ( !Q_stricmp( pCommand, "Cancel" ) )
  89. {
  90. CloseModal();
  91. return;
  92. }
  93. BaseClass::OnCommand( pCommand );
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Menu to switch between tools
  97. //-----------------------------------------------------------------------------
  98. class CToolSwitchMenuButton : public CToolMenuButton
  99. {
  100. DECLARE_CLASS_SIMPLE( CToolSwitchMenuButton, CToolMenuButton );
  101. public:
  102. CToolSwitchMenuButton( Panel *parent, const char *panelName, const char *text, Panel *pActionTarget );
  103. virtual void OnShowMenu(Menu *menu);
  104. protected:
  105. MESSAGE_FUNC( OnShowLoadToolDialog, "OnShowLoadToolDialog" );
  106. MESSAGE_FUNC_PARAMS( OnLoadTools, "OnLoadTools", params );
  107. DHANDLE< CLoadToolDialog > m_hLoadToolDialog;
  108. };
  109. //-----------------------------------------------------------------------------
  110. // Global function to create the switch menu
  111. //-----------------------------------------------------------------------------
  112. CToolMenuButton* CreateToolSwitchMenuButton( Panel *parent, const char *panelName, const char *text, Panel *pActionTarget )
  113. {
  114. return new CToolSwitchMenuButton( parent, panelName, text, pActionTarget );
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Constructor
  118. //-----------------------------------------------------------------------------
  119. CToolSwitchMenuButton::CToolSwitchMenuButton( Panel *parent, const char *panelName, const char *text, Panel *pActionTarget ) :
  120. BaseClass( parent, panelName, text, pActionTarget )
  121. {
  122. SetMenu(m_pMenu);
  123. }
  124. void CToolSwitchMenuButton::OnShowLoadToolDialog()
  125. {
  126. m_hLoadToolDialog = new CLoadToolDialog( this );
  127. m_hLoadToolDialog->MoveToCenterOfScreen();
  128. m_hLoadToolDialog->AddActionSignalTarget( this );
  129. m_hLoadToolDialog->DoModal();
  130. }
  131. void CToolSwitchMenuButton::OnLoadTools( KeyValues *params )
  132. {
  133. int nCount = params->GetInt( "count", 0 );
  134. for ( int i = 0; i < nCount; ++i )
  135. {
  136. char const *pToolName = params->GetString( CFmtStr( "%i", i ), "" );
  137. if ( pToolName && *pToolName )
  138. {
  139. // Load it
  140. enginetools->LoadToolModule( pToolName, false );
  141. }
  142. }
  143. }
  144. //-----------------------------------------------------------------------------
  145. // Is called when the menu is made visible
  146. //-----------------------------------------------------------------------------
  147. void CToolSwitchMenuButton::OnShowMenu(Menu *menu)
  148. {
  149. BaseClass::OnShowMenu( menu );
  150. Reset();
  151. int c = enginetools->GetToolCount();
  152. for ( int i = 0 ; i < c; ++i )
  153. {
  154. char const *toolname = enginetools->GetToolName( i );
  155. char toolcmd[ 32 ];
  156. Q_snprintf( toolcmd, sizeof( toolcmd ), "OnTool%i", i );
  157. int id = AddCheckableMenuItem( toolname, toolname, new KeyValues ( "Command", "command", toolcmd ), m_pActionTarget );
  158. m_pMenu->SetItemEnabled( id, true );
  159. m_pMenu->SetMenuItemChecked( id, enginetools->IsTopmostTool( enginetools->GetToolSystem( i ) ) );
  160. }
  161. m_pMenu->AddSeparator();
  162. m_pMenu->AddMenuItem( "#ToolShowLoadToolDialog", new KeyValues( "OnShowLoadToolDialog" ), this );
  163. }