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.

386 lines
9.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "client_pch.h"
  8. #include "cl_demoeditorpanel.h"
  9. #include "cl_demoactionmanager.h"
  10. #include "cl_demoaction.h"
  11. #include <vgui_controls/Button.h>
  12. #include <vgui_controls/CheckButton.h>
  13. #include <vgui_controls/Label.h>
  14. #include <vgui_controls/Controls.h>
  15. #include <vgui/ISystem.h>
  16. #include <vgui/ISurface.h>
  17. #include <vgui_controls/PropertySheet.h>
  18. #include <vgui/IVGui.h>
  19. #include <vgui_controls/FileOpenDialog.h>
  20. #include <vgui_controls/ProgressBar.h>
  21. #include <vgui_controls/ListPanel.h>
  22. #include <vgui_controls/MenuButton.h>
  23. #include <vgui_controls/Menu.h>
  24. #include "cl_demoactioneditors.h"
  25. // memdbgon must be the last include file in a .cpp file!!!
  26. #include "tier0/memdbgon.h"
  27. using namespace vgui;
  28. // So new actions can have sequential/unique names
  29. static int g_nNewActionNumber = 1;
  30. //-----------------------------------------------------------------------------
  31. // Purpose: A menu button that knows how to parse cvar/command menu data from gamedir\scripts\debugmenu.txt
  32. //-----------------------------------------------------------------------------
  33. class CNewActionButton : public vgui::MenuButton
  34. {
  35. typedef vgui::MenuButton BaseClass;
  36. public:
  37. // Construction
  38. CNewActionButton( vgui::Panel *parent, const char *panelName, const char *text );
  39. private:
  40. // Menu associated with this button
  41. Menu *m_pMenu;
  42. };
  43. //-----------------------------------------------------------------------------
  44. // Purpose: Constructor
  45. //-----------------------------------------------------------------------------
  46. CNewActionButton::CNewActionButton(Panel *parent, const char *panelName, const char *text)
  47. : BaseClass( parent, panelName, text )
  48. {
  49. // Assume no menu
  50. m_pMenu = new Menu( this, "DemoEditNewAction" );
  51. int count = NUM_DEMO_ACTIONS;
  52. int i;
  53. for ( i = 1 ; i < count; i++ )
  54. {
  55. char const *actionType = CBaseDemoAction::NameForType( (DEMOACTION)i );
  56. m_pMenu->AddMenuItem( actionType, actionType, parent );
  57. m_pMenu->SetItemEnabled( actionType, CBaseDemoAction::HasEditorFactory( (DEMOACTION)i ) );
  58. }
  59. m_pMenu->MakePopup();
  60. MenuButton::SetMenu(m_pMenu);
  61. SetOpenDirection(Menu::UP);
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose: Basic help dialog
  65. //-----------------------------------------------------------------------------
  66. CDemoEditorPanel::CDemoEditorPanel( vgui::Panel *parent ) : Frame( parent, "DemoEditorPanel")
  67. {
  68. int w = 440;
  69. int h = 300;
  70. SetSize( w, h );
  71. SetTitle("Demo Editor", true);
  72. m_pSave = new vgui::Button( this, "DemoEditSave", "Save" );
  73. m_pRevert = new vgui::Button( this, "DemoEditRevert", "Revert" );;
  74. m_pOK = new vgui::Button( this, "DemoEditOk", "OK" );
  75. m_pCancel = new vgui::Button( this, "DemoEditCancel", "Cancel" );
  76. m_pNew = new CNewActionButton( this, "DemoEditNew", "New->" );
  77. m_pEdit = new vgui::Button( this, "DemoEditEdit", "Edit..." );
  78. m_pDelete = new vgui::Button( this, "DemoEditDelete", "Delete" );
  79. m_pCurrentDemo = new vgui::Label( this, "DemoName", "" );
  80. m_pActions = new vgui::ListPanel( this, "DemoActionList" );
  81. vgui::ivgui()->AddTickSignal( GetVPanel(), 0 );
  82. LoadControlSettings("Resource\\DemoEditorPanel.res");
  83. int xpos, ypos;
  84. parent->GetPos( xpos, ypos );
  85. ypos += parent->GetTall();
  86. SetPos( xpos, ypos );
  87. m_pActions->AddColumnHeader(0, "actionname", "Action", m_pActions->GetWide() / 3);
  88. m_pActions->AddColumnHeader(1, "actiontype", "Type", m_pActions->GetWide() / 3);
  89. m_pActions->AddColumnHeader(2, "actionstart", "Start", m_pActions->GetWide() / 3);
  90. OnRefresh();
  91. SetVisible( true );
  92. SetSizeable( false );
  93. SetMoveable( true );
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Purpose:
  97. //-----------------------------------------------------------------------------
  98. CDemoEditorPanel::~CDemoEditorPanel()
  99. {
  100. }
  101. void CDemoEditorPanel::OnTick()
  102. {
  103. BaseClass::OnTick();
  104. m_pCurrentDemo->SetText( demoaction->GetCurrentDemoFile() );
  105. bool hasdemo = demoaction->GetCurrentDemoFile()[0] ? true : false;
  106. if ( !hasdemo )
  107. {
  108. m_pNew->SetEnabled( false );
  109. m_pEdit->SetEnabled( false );
  110. m_pDelete->SetEnabled( false );
  111. m_pSave->SetEnabled( false );
  112. m_pRevert->SetEnabled( false );
  113. }
  114. else
  115. {
  116. m_pNew->SetEnabled( true );
  117. int count = demoaction->GetActionCount();
  118. m_pEdit->SetEnabled( count > 0 );
  119. m_pDelete->SetEnabled( count > 0 );
  120. if ( m_pActions && m_pActions->GetSelectedItemsCount() != 1 )
  121. {
  122. m_pEdit->SetEnabled( false );
  123. m_pDelete->SetEnabled( false );
  124. }
  125. m_pSave->SetEnabled( demoaction->IsDirty() );
  126. m_pRevert->SetEnabled( demoaction->IsDirty() );
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose:
  131. // Input : *command -
  132. // Output : Returns true on success, false on failure.
  133. //-----------------------------------------------------------------------------
  134. bool CDemoEditorPanel::IsNewActionCommand( char const *command )
  135. {
  136. DEMOACTION type = CBaseDemoAction::TypeForName( command );
  137. if ( type != DEMO_ACTION_UNKNOWN )
  138. {
  139. return true;
  140. }
  141. return false;
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Purpose:
  145. // Input : *actiontype -
  146. //-----------------------------------------------------------------------------
  147. void CDemoEditorPanel::CreateNewAction( char const *actiontype )
  148. {
  149. if ( m_hCurrentEditor != NULL )
  150. return;
  151. DEMOACTION type = CBaseDemoAction::TypeForName( actiontype );
  152. if ( type == DEMO_ACTION_UNKNOWN )
  153. return;
  154. CBaseDemoAction *action = CBaseDemoAction::CreateDemoAction( type );
  155. if ( action )
  156. {
  157. action->SetActionName( va( "Unnamed%i", g_nNewActionNumber++ ) );
  158. demoaction->SetDirty( true );
  159. m_hCurrentEditor = CBaseDemoAction::CreateActionEditor( action->GetType(), this, action, true );
  160. if ( m_hCurrentEditor != NULL )
  161. {
  162. m_hCurrentEditor->SetVisible( true );
  163. m_hCurrentEditor->SetSize( 400, 300 );
  164. }
  165. }
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Purpose:
  169. // Input : *command -
  170. //-----------------------------------------------------------------------------
  171. void CDemoEditorPanel::OnCommand(const char *command)
  172. {
  173. if ( !Q_strcasecmp( command, "edit" ) )
  174. {
  175. OnEdit();
  176. }
  177. else if ( !Q_strcasecmp( command, "delete" ) )
  178. {
  179. OnDelete();
  180. }
  181. else if ( !Q_strcasecmp( command, "save" ) )
  182. {
  183. OnSave();
  184. }
  185. else if ( !Q_strcasecmp( command, "Close" ) )
  186. {
  187. OnSave();
  188. MarkForDeletion();
  189. OnClose();
  190. }
  191. else if ( !Q_strcasecmp( command, "cancel" ) )
  192. {
  193. OnRevert();
  194. MarkForDeletion();
  195. OnClose();
  196. }
  197. else if ( !Q_strcasecmp( command, "revert" ) )
  198. {
  199. OnRevert();
  200. }
  201. else if ( IsNewActionCommand( command ) )
  202. {
  203. CreateNewAction( command );
  204. }
  205. else
  206. {
  207. BaseClass::OnCommand( command );
  208. }
  209. }
  210. void CDemoEditorPanel::OnVDMChanged( void )
  211. {
  212. OnRefresh();
  213. }
  214. void CDemoEditorPanel::PurgeActionList()
  215. {
  216. if ( !m_pActions )
  217. {
  218. Assert( 0 );
  219. return;
  220. }
  221. m_pActions->DeleteAllItems();
  222. }
  223. void CDemoEditorPanel::PopulateActionList()
  224. {
  225. PurgeActionList();
  226. int count = demoaction->GetActionCount();
  227. int i;
  228. for ( i = 0; i < count; i++ )
  229. {
  230. CBaseDemoAction *action = demoaction->GetAction( i );
  231. Assert( action );
  232. KeyValues *item = new KeyValues( "data", "actionname", action->GetActionName() );
  233. item->SetString( "actiontype", CBaseDemoAction::NameForType( action->GetType() ) );
  234. switch ( action->GetTimingType() )
  235. {
  236. default:
  237. case ACTION_USES_NEITHER:
  238. break;
  239. case ACTION_USES_TICK:
  240. {
  241. item->SetString( "actionstart", va( "Tick %i", action->GetStartTick() ) );
  242. }
  243. break;
  244. case ACTION_USES_TIME:
  245. {
  246. item->SetString( "actionstart", va( "Time %.3f", action->GetStartTime() ) );
  247. }
  248. break;
  249. }
  250. m_pActions->AddItem( item , 0, false, false);
  251. }
  252. }
  253. void CDemoEditorPanel::OnEdit()
  254. {
  255. if ( m_hCurrentEditor != NULL )
  256. return;
  257. int numselected = m_pActions->GetSelectedItemsCount();
  258. if ( numselected != 1 )
  259. return;
  260. int row = m_pActions->GetSelectedItem( 0 );
  261. if ( row == -1 )
  262. return;
  263. CBaseDemoAction *action = demoaction->GetAction( row );
  264. m_hCurrentEditor = CBaseDemoAction::CreateActionEditor( action->GetType(), this, action, false );
  265. if ( m_hCurrentEditor != NULL )
  266. {
  267. m_hCurrentEditor->SetVisible( true );
  268. m_hCurrentEditor->SetSize( 400, 300 );
  269. }
  270. // edit it
  271. // demoaction->SetDirty( true );
  272. // PopulateActionList();
  273. }
  274. void CDemoEditorPanel::OnDelete()
  275. {
  276. int numselected = m_pActions->GetSelectedItemsCount();
  277. if ( numselected < 1 )
  278. return;
  279. int i;
  280. for ( i = 0; i < numselected; i++ )
  281. {
  282. int row = m_pActions->GetSelectedItem(0);
  283. if ( row == -1 )
  284. continue;
  285. CBaseDemoAction *action = demoaction->GetAction( row );
  286. if ( action )
  287. {
  288. // This sets dirty bit
  289. demoaction->RemoveAction( action );
  290. }
  291. }
  292. OnRefresh();
  293. }
  294. void CDemoEditorPanel::OnSave()
  295. {
  296. demoaction->SaveToFile();
  297. }
  298. void CDemoEditorPanel::OnRevert()
  299. {
  300. demoaction->ReloadFromDisk();
  301. OnRefresh();
  302. }
  303. CBaseDemoAction *CDemoEditorPanel::FindActionByName( char const *name )
  304. {
  305. int count = demoaction->GetActionCount();
  306. int i;
  307. for ( i = 0; i < count; i++ )
  308. {
  309. CBaseDemoAction *action = demoaction->GetAction( i );
  310. Assert( action );
  311. if ( !Q_strcasecmp( name, action->GetActionName() ) )
  312. return action;
  313. }
  314. return NULL;
  315. }
  316. //-----------------------------------------------------------------------------
  317. // Purpose:
  318. //-----------------------------------------------------------------------------
  319. void CDemoEditorPanel::OnRefresh()
  320. {
  321. PopulateActionList();
  322. }