datablockEditor.tscript 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // Main code for the Datablock Editor plugin.
  23. $DATABLOCK_EDITOR_DEFAULT_FILENAME = "art/datablocks/managedDatablocks." @ $TorqueScriptFileExtension;
  24. //=============================================================================================
  25. // Initialization.
  26. //=============================================================================================
  27. //---------------------------------------------------------------------------------------------
  28. function DatablockEditorPlugin::init( %this )
  29. {
  30. if( !DatablockEditorTree.getItemCount() )
  31. %this.populateTrees();
  32. }
  33. //---------------------------------------------------------------------------------------------
  34. function DatablockEditorPlugin::onWorldEditorStartup( %this )
  35. {
  36. // Add ourselves to the window menu.
  37. %accel = EditorGui.addToEditorsMenu( "Datablock Editor", "", DatablockEditorPlugin );
  38. // Add ourselves to the ToolsToolbar
  39. %tooltip = "Datablock Editor (" @ %accel @ ")";
  40. EditorGui.addToToolsToolbar( "DatablockEditorPlugin", "DatablockEditorPalette", "ToolsModule:datablock_editor_n_image", %tooltip );
  41. //connect editor windows
  42. GuiWindowCtrl::Attach( DatablockEditorInspectorWindow, DatablockEditorTreeWindow);
  43. }
  44. //---------------------------------------------------------------------------------------------
  45. function DatablockEditorPlugin::onActivated( %this )
  46. {
  47. EditorGui-->WorldEditorToolbar.setVisible(false);
  48. EditorGui.bringToFront( DatablockEditorPlugin );
  49. DatablockEditorTreeWindow.setVisible( true );
  50. DatablockEditorInspectorWindow.setVisible( true );
  51. DatablockEditorInspectorWindow.makeFirstResponder( true );
  52. // Maximize the windows when set
  53. DatablockEditorGui.onWake();
  54. %this.map.push();
  55. // Set the status bar here until all tool have been hooked up
  56. EditorGuiStatusBar.setInfo( "Datablock editor." );
  57. %numSelected = %this.getNumSelectedDatablocks();
  58. if( !%numSelected )
  59. EditorGuiStatusBar.setSelection( "" );
  60. else
  61. EditorGuiStatusBar.setSelection( %numSelected @ " datablocks selected" );
  62. %this.init();
  63. DatablockEditorPlugin.readSettings();
  64. if( EWorldEditor.getSelectionSize() == 1 )
  65. %this.onObjectSelected( EWorldEditor.getSelectedObject( 0 ) );
  66. Parent::onActivated( %this );
  67. }
  68. //---------------------------------------------------------------------------------------------
  69. function DatablockEditorPlugin::onDeactivated( %this )
  70. {
  71. DatablockEditorPlugin.writeSettings();
  72. DatablockEditorInspectorWindow.setVisible( false );
  73. DatablockEditorTreeWindow.setVisible( false );
  74. %this.map.pop();
  75. Parent::onDeactivated(%this);
  76. }
  77. //---------------------------------------------------------------------------------------------
  78. function DatablockEditorPlugin::onExitMission( %this )
  79. {
  80. DatablockEditorTree.clear();
  81. DatablockEditorInspector.inspect( "" );
  82. }
  83. //---------------------------------------------------------------------------------------------
  84. function DatablockEditorPlugin::openDatablock( %this, %datablock )
  85. {
  86. EditorGui.setEditor( DatablockEditorPlugin );
  87. %this.selectDatablock( %datablock );
  88. DatablockEditorTreeTabBook.selectedPage = 0;
  89. }
  90. //---------------------------------------------------------------------------------------------
  91. function DatablockEditorPlugin::setEditorFunction( %this )
  92. {
  93. return true;
  94. }
  95. //---------------------------------------------------------------------------------------------
  96. function DatablockEditorPlugin::onObjectSelected( %this, %object )
  97. {
  98. // Select datablock of object if this is a GameBase object.
  99. if( %object.isMemberOfClass( "GameBase" ) )
  100. %this.selectDatablock( %object.getDatablock() );
  101. else if( %object.isMemberOfClass( "SFXEmitter" ) && isObject( %object.track ) )
  102. %this.selectDatablock( %object.track );
  103. else if( %object.isMemberOfClass( "LightBase" ) && isObject( %object.animationType ) )
  104. %this.selectDatablock( %object.animationType );
  105. }
  106. //---------------------------------------------------------------------------------------------
  107. function DatablockEditorPlugin::populateTrees(%this)
  108. {
  109. // Populate datablock tree.
  110. if( %this.excludeClientOnlyDatablocks )
  111. %set = DataBlockGroup;
  112. else
  113. %set = DataBlockSet;
  114. DatablockEditorTree.clear();
  115. foreach( %datablock in %set )
  116. {
  117. %unlistedFound = false;
  118. %id = %datablock.getId();
  119. foreach( %obj in UnlistedDatablocks )
  120. if( %obj.getId() == %id )
  121. {
  122. %unlistedFound = true;
  123. break;
  124. }
  125. if( %unlistedFound )
  126. continue;
  127. %this.addExistingItem( %datablock, true );
  128. }
  129. DatablockEditorTree.sort( 0, true, false, false );
  130. // Populate datablock type tree.
  131. %classList = enumerateConsoleClasses( "SimDatablock" );
  132. DatablockEditorTypeTree.clear();
  133. foreach$( %datablockClass in %classList )
  134. {
  135. if( !%this.isExcludedDatablockType( %datablockClass )
  136. && DatablockEditorTypeTree.findItemByName( %datablockClass ) == 0 )
  137. DatablockEditorTypeTree.insertItem( 0, %datablockClass );
  138. }
  139. DatablockEditorTypeTree.sort( 0, false, false, false );
  140. }
  141. //---------------------------------------------------------------------------------------------
  142. function DatablockEditorPlugin::addExistingItem( %this, %datablock, %dontSort )
  143. {
  144. %tree = DatablockEditorTree;
  145. // Look up class at root level. Create if needed.
  146. %class = %datablock.getClassName();
  147. %parentID = %tree.findItemByName( %class );
  148. if( %parentID == 0 )
  149. %parentID = %tree.insertItem( 0, %class );
  150. // If the datablock is already there, don't
  151. // do anything.
  152. if( %tree.findItemByValue( %datablock.getId() ) )
  153. return;
  154. // It doesn't exist so add it.
  155. %name = %datablock.getName();
  156. if( %this.PM.isDirty( %datablock ) )
  157. %name = %name @ " *";
  158. %id = DatablockEditorTree.insertItem( %parentID, %name, %datablock.getId() );
  159. if( !%dontSort )
  160. DatablockEditorTree.sort( %parentID, false, false, false );
  161. return %id;
  162. }
  163. //---------------------------------------------------------------------------------------------
  164. function DatablockEditorPlugin::isExcludedDatablockType( %this, %className )
  165. {
  166. switch$( %className )
  167. {
  168. case "SimDatablock":
  169. return true;
  170. case "SFXTrack": // Abstract.
  171. return true;
  172. }
  173. return false;
  174. }
  175. //=============================================================================================
  176. // Settings.
  177. //=============================================================================================
  178. //---------------------------------------------------------------------------------------------
  179. function DatablockEditorPlugin::initSettings( %this )
  180. {
  181. EditorSettings.beginGroup("DatablockEditor", true);
  182. EditorSettings.setDefaultValue("libraryTab", "0");
  183. EditorSettings.endGroup();
  184. }
  185. //---------------------------------------------------------------------------------------------
  186. function DatablockEditorPlugin::readSettings( %this )
  187. {
  188. EditorSettings.beginGroup("DatablockEditor", true);
  189. DatablockEditorTreeTabBook.selectPage( EditorSettings.value( "libraryTab" ) );
  190. %db = EditorSettings.value( "selectedDatablock" );
  191. if( isObject( %db ) && %db.isMemberOfClass( "SimDatablock" ) )
  192. %this.selectDatablock( %db );
  193. EditorSettings.endGroup();
  194. }
  195. //---------------------------------------------------------------------------------------------
  196. function DatablockEditorPlugin::writeSettings( %this )
  197. {
  198. EditorSettings.beginGroup( "DatablockEditor", true );
  199. EditorSettings.setValue( "libraryTab", DatablockEditorTreeTabBook.getSelectedPage() );
  200. if( %this.getNumSelectedDatablocks() > 0 )
  201. EditorSettings.setValue( "selectedDatablock", %this.getSelectedDatablock().getName() );
  202. EditorSettings.endGroup();
  203. }
  204. //=============================================================================================
  205. // Persistence.
  206. //=============================================================================================
  207. //---------------------------------------------------------------------------------------------
  208. /// Return true if there is any datablock with unsaved changes.
  209. function DatablockEditorPlugin::isDirty( %this )
  210. {
  211. return %this.PM.hasDirty();
  212. }
  213. //---------------------------------------------------------------------------------------------
  214. /// Return true if any of the currently selected datablocks has unsaved changes.
  215. function DatablockEditorPlugin::selectedDatablockIsDirty( %this )
  216. {
  217. %tree = DatablockEditorTree;
  218. %count = %tree.getSelectedItemsCount();
  219. %selected = %tree.getSelectedItemList();
  220. foreach$( %id in %selected )
  221. {
  222. %db = %tree.getItemValue( %id );
  223. if( %this.PM.isDirty( %db ) )
  224. return true;
  225. }
  226. return false;
  227. }
  228. //---------------------------------------------------------------------------------------------
  229. function DatablockEditorPlugin::syncDirtyState( %this )
  230. {
  231. %tree = DatablockEditorTree;
  232. %count = %tree.getSelectedItemsCount();
  233. %selected = %tree.getSelectedItemList();
  234. %haveDirty = false;
  235. foreach$( %id in %selected )
  236. {
  237. %db = %tree.getItemValue( %id );
  238. if( %this.PM.isDirty( %db ) )
  239. {
  240. %this.flagDatablockAsDirty( %db, true );
  241. %haveDirty = true;
  242. }
  243. else
  244. %this.flagInspectorAsDirty( %db, false );
  245. }
  246. %this.flagInspectorAsDirty( %haveDirty );
  247. }
  248. //---------------------------------------------------------------------------------------------
  249. ///
  250. function DatablockEditorPlugin::flagInspectorAsDirty( %this, %dirty )
  251. {
  252. if( %dirty )
  253. DatablockEditorInspectorWindow.text = "Datablock *";
  254. else
  255. DatablockEditorInspectorWindow.text = "Datablock";
  256. }
  257. //---------------------------------------------------------------------------------------------
  258. function DatablockEditorPlugin::flagDatablockAsDirty(%this, %datablock, %dirty )
  259. {
  260. %tree = DatablockEditorTree;
  261. %id = %tree.findItemByValue( %datablock.getId() );
  262. if( %id == 0 )
  263. return;
  264. // Tag the item caption and sync the persistence manager.
  265. if( %dirty )
  266. {
  267. DatablockEditorTree.editItem( %id, %datablock.getName() @ " *", %datablock.getId() );
  268. %this.PM.setDirty( %datablock );
  269. }
  270. else
  271. {
  272. DatablockEditorTree.editItem( %id, %datablock.getName(), %datablock.getId() );
  273. %this.PM.removeDirty( %datablock );
  274. }
  275. // Sync the inspector dirty state.
  276. %this.flagInspectorAsDirty( %this.PM.hasDirty() );
  277. }
  278. //---------------------------------------------------------------------------------------------
  279. function DatablockEditorPlugin::showSaveNewFileDialog(%this)
  280. {
  281. %currentFile = %this.getSelectedDatablock().getFilename();
  282. getSaveFilename( "TorqueScript Files|*." @ $TorqueScriptFileExtension @ "|All Files|*.*", %this @ ".saveNewFileFinish", %currentFile, false );
  283. }
  284. //---------------------------------------------------------------------------------------------
  285. function DatablockEditorPlugin::saveNewFileFinish( %this, %newFileName )
  286. {
  287. // Clear the first responder to capture any inspector changes
  288. %ctrl = canvas.getFirstResponder();
  289. if( isObject(%ctrl) )
  290. %ctrl.clearFirstResponder();
  291. %tree = DatablockEditorTree;
  292. %count = %tree.getSelectedItemsCount();
  293. %selected = %tree.getSelectedItemList();
  294. foreach$( %id in %selected )
  295. {
  296. %db = %tree.getItemValue( %id );
  297. %db = %this.getSelectedDatablock();
  298. // Remove from current file.
  299. %oldFileName = %db.getFileName();
  300. if( %oldFileName !$= "" )
  301. %this.PM.removeObjectFromFile( %db, %oldFileName );
  302. // Save to new file.
  303. %this.PM.setDirty( %db, %newFileName );
  304. if( %this.PM.saveDirtyObject( %db ) )
  305. {
  306. // Clear dirty state.
  307. %this.flagDatablockAsDirty( %db, false );
  308. }
  309. }
  310. DatablockEditorInspectorWindow-->DatablockFile.setText( %newFileName );
  311. }
  312. //---------------------------------------------------------------------------------------------
  313. function DatablockEditorPlugin::save( %this )
  314. {
  315. // Clear the first responder to capture any inspector changes
  316. %ctrl = canvas.getFirstResponder();
  317. if( isObject(%ctrl) )
  318. %ctrl.clearFirstResponder();
  319. %tree = DatablockEditorTree;
  320. %count = %tree.getSelectedItemsCount();
  321. %selected = %tree.getSelectedItemList();
  322. for( %i = 0; %i < %count; %i ++ )
  323. {
  324. %id = getWord( %selected, %i );
  325. %db = %tree.getItemValue( %id );
  326. if( %this.PM.isDirty( %db ) )
  327. {
  328. %this.PM.saveDirtyObject( %db );
  329. %this.flagDatablockAsDirty( %db, false );
  330. }
  331. }
  332. }
  333. //=============================================================================================
  334. // Selection.
  335. //=============================================================================================
  336. //---------------------------------------------------------------------------------------------
  337. function DatablockEditorPlugin::getNumSelectedDatablocks( %this )
  338. {
  339. return DatablockEditorTree.getSelectedItemsCount();
  340. }
  341. //---------------------------------------------------------------------------------------------
  342. function DatablockEditorPlugin::getSelectedDatablock( %this, %index )
  343. {
  344. %tree = DatablockEditorTree;
  345. if( !%tree.getSelectedItemsCount() )
  346. return 0;
  347. if( !%index )
  348. %id = %tree.getSelectedItem();
  349. else
  350. %id = getWord( %tree.getSelectedItemList(), %index );
  351. return %tree.getItemValue( %id );
  352. }
  353. //---------------------------------------------------------------------------------------------
  354. function DatablockEditorPlugin::resetSelectedDatablock( %this )
  355. {
  356. DatablockEditorTree.clearSelection();
  357. DatablockEditorInspector.inspect(0);
  358. DatablockEditorInspectorWindow-->DatablockFile.setText("");
  359. EditorGuiStatusBar.setSelection( "" );
  360. }
  361. //---------------------------------------------------------------------------------------------
  362. function DatablockEditorPlugin::selectDatablockCheck( %this, %datablock )
  363. {
  364. if( %this.selectedDatablockIsDirty() )
  365. %this.showSaveDialog( %datablock );
  366. else
  367. %this.selectDatablock( %datablock );
  368. }
  369. //---------------------------------------------------------------------------------------------
  370. function DatablockEditorPlugin::selectDatablock( %this, %datablock, %add, %dontSyncTree )
  371. {
  372. if( %add )
  373. DatablockEditorInspector.addInspect( %datablock );
  374. else
  375. DatablockEditorInspector.inspect( %datablock );
  376. if( !%dontSyncTree )
  377. {
  378. %id = DatablockEditorTree.findItemByValue( %datablock.getId() );
  379. if( !%add )
  380. DatablockEditorTree.clearSelection();
  381. DatablockEditorTree.selectItem( %id, true );
  382. DatablockEditorTree.scrollVisible( %id );
  383. }
  384. %this.syncDirtyState();
  385. // Update the filename text field.
  386. %numSelected = %this.getNumSelectedDatablocks();
  387. %fileNameField = DatablockEditorInspectorWindow-->DatablockFile;
  388. if( %numSelected == 1 )
  389. {
  390. %fileName = %datablock.getFilename();
  391. if( %fileName !$= "" )
  392. %fileNameField.setText( %fileName );
  393. else
  394. %fileNameField.setText( $DATABLOCK_EDITOR_DEFAULT_FILENAME );
  395. }
  396. else
  397. {
  398. %fileNameField.setText( "" );
  399. }
  400. EditorGuiStatusBar.setSelection( %this.getNumSelectedDatablocks() @ " Datablocks Selected" );
  401. }
  402. //---------------------------------------------------------------------------------------------
  403. function DatablockEditorPlugin::unselectDatablock( %this, %datablock, %dontSyncTree )
  404. {
  405. DatablockEditorInspector.removeInspect( %datablock );
  406. if( !%dontSyncTree )
  407. {
  408. %id = DatablockEditorTree.findItemByValue( %datablock.getId() );
  409. DatablockEditorTree.selectItem( %id, false );
  410. }
  411. %this.syncDirtyState();
  412. // If we have exactly one selected datablock remaining, re-enable
  413. // the save-as button.
  414. %numSelected = %this.getNumSelectedDatablocks();
  415. if( %numSelected == 1 )
  416. {
  417. DatablockEditorInspectorWindow-->saveAsButton.setActive( true );
  418. %fileNameField = DatablockEditorInspectorWindow-->DatablockFile;
  419. %fileNameField.setText( %this.getSelectedDatablock().getFilename() );
  420. %fileNameField.setActive( true );
  421. }
  422. EditorGuiStatusBar.setSelection( %this.getNumSelectedDatablocks() @ " Datablocks Selected" );
  423. }
  424. //=============================================================================================
  425. // Creation and Deletion.
  426. //=============================================================================================
  427. //---------------------------------------------------------------------------------------------
  428. function DatablockEditorPlugin::deleteDatablock( %this )
  429. {
  430. %tree = DatablockEditorTree;
  431. // If we have more than single datablock selected,
  432. // turn our undos into a compound undo.
  433. %numSelected = %tree.getSelectedItemsCount();
  434. if( %numSelected > 1 )
  435. Editor.getUndoManager().pushCompound( "Delete Multiple Datablocks" );
  436. for( %i = 0; %i < %numSelected; %i ++ )
  437. {
  438. %id = %tree.getSelectedItem( %i );
  439. %db = %tree.getItemValue( %id );
  440. %fileName = %db.getFileName();
  441. // Remove the datablock from the tree.
  442. DatablockEditorTree.removeItem( %id );
  443. // Create undo.
  444. %action = %this.createUndo( ActionDeleteDatablock, "Delete Datablock" );
  445. %action.db = %db;
  446. %action.dbName = %db.getName();
  447. %action.fname = %fileName;
  448. %this.submitUndo( %action );
  449. // Kill the datablock in the file.
  450. if( %fileName !$= "" )
  451. %this.PM.removeObjectFromFile( %db );
  452. UnlistedDatablocks.add( %db );
  453. // Show some confirmation.
  454. if( %numSelected == 1 )
  455. toolsMessageBoxOk( "Datablock Deleted", "The datablock (" @ %db.getName() @ ") has been removed from " @
  456. "it's file (" @ %db.getFilename() @ ") and upon restart will cease to exist" );
  457. }
  458. // Close compound, if we were deleting multiple datablocks.
  459. if( %numSelected > 1 )
  460. Editor.getUndoManager().popCompound();
  461. // Show confirmation for multiple datablocks.
  462. if( %numSelected > 1 )
  463. toolsMessageBoxOk( "Datablocks Deleted", "The datablocks have been deleted and upon restart will cease to exist." );
  464. // Clear selection.
  465. DatablockEditorPlugin.resetSelectedDatablock();
  466. }
  467. //---------------------------------------------------------------------------------------------
  468. function DatablockEditorPlugin::createNewDatablockOfType(%this, %class, %inheritFrom)
  469. {
  470. $DATABLOCK_EDITOR_NEWDB_CLASS = %class;
  471. $DATABLOCK_EDITOR_NEWDB_INHERITFROM = %inheritFrom;
  472. %this.pickDatablockPath();
  473. }
  474. function DatablockEditorPlugin::createDatablock(%this)
  475. {
  476. if($DATABLOCK_EDITOR_NEWDB_CLASS $= "")
  477. %class = DatablockEditorTypeTree.getItemText(DatablockEditorTypeTree.getSelectedItem());
  478. else
  479. %class = $DATABLOCK_EDITOR_NEWDB_CLASS;
  480. if( %class !$= "" )
  481. {
  482. // Need to prompt for a name.
  483. DatablockEditorCreatePrompt-->CreateDatablockName.setText("Name");
  484. DatablockEditorCreatePrompt-->CreateDatablockName.selectAllText();
  485. // Populate the copy source dropdown.
  486. %list = DatablockEditorCreatePrompt-->CopySourceDropdown;
  487. %list.clear();
  488. %list.add( "", 0 );
  489. %set = DataBlockSet;
  490. %count = %set.getCount();
  491. for( %i = 0; %i < %count; %i ++ )
  492. {
  493. %datablock = %set.getObject( %i );
  494. %datablockClass = %datablock.getClassName();
  495. if( !isMemberOfClass( %datablockClass, %class ) )
  496. continue;
  497. %list.add( %datablock.getName(), %i + 1 );
  498. }
  499. if($DATABLOCK_EDITOR_NEWDB_INHERITFROM !$= "")
  500. {
  501. %inheritFromListId = %list.findText($DATABLOCK_EDITOR_NEWDB_INHERITFROM);
  502. %list.setSelected(%inheritFromListId);
  503. }
  504. // Set up state of client-side checkbox.
  505. %clientSideCheckBox = DatablockEditorCreatePrompt-->ClientSideCheckBox;
  506. %canBeClientSide = DatablockEditorPlugin::canBeClientSideDatablock( %class );
  507. %clientSideCheckBox.setStateOn( %canBeClientSide );
  508. %clientSideCheckBox.setActive( %canBeClientSide );
  509. // Show the dialog.
  510. canvas.pushDialog( DatablockEditorCreatePrompt, 0, true );
  511. DatablockEditorCreatePrompt-->promptWindow.text = "Create New Datablock in module: " @ DatablockEditorPlugin.targetCreationModule;
  512. }
  513. }
  514. //---------------------------------------------------------------------------------------------
  515. function DatablockEditorPlugin::pickDatablockPath(%this)
  516. {
  517. AssetBrowser_SelectModule.showDialog("DatablockEditorPlugin.pickedNewDBTargetModule");
  518. //SelectAssetPath.showDialog(AssetBrowser.dirHandler.currentAddress, "DatablockEditorPlugin.pickedNewDBPath", "Select New Datablock Path");
  519. AssetBrowser_SelectModuleWindow.selectWindow();
  520. }
  521. function DatablockEditorPlugin::pickedNewDBTargetModule(%this, %module)
  522. {
  523. DatablockEditorPlugin.targetCreationModule = %module;
  524. %moduleDef = ModuleDatabase.findModule(%module);
  525. $DATABLOCK_EDITOR_DEFAULT_FILENAME = %moduleDef.ModulePath @ "/scripts/managedData/managedDatablocks." @ $TorqueScriptFileExtension;
  526. if(!isDirectory(filePath($DATABLOCK_EDITOR_DEFAULT_FILENAME)))
  527. {
  528. AssetBrowser.dirHandler.createFolder(filePath($DATABLOCK_EDITOR_DEFAULT_FILENAME));
  529. }
  530. DatablockEditorPlugin.createDatablock();
  531. }
  532. function DatablockEditorPlugin::createPromptNameCheck(%this, %path)
  533. {
  534. %name = DatablockEditorCreatePrompt-->CreateDatablockName.getText();
  535. if( !Editor::validateObjectName( %name, true ) )
  536. return;
  537. // Fetch the copy source and clear the list.
  538. %copySource = DatablockEditorCreatePrompt-->copySourceDropdown.getText();
  539. DatablockEditorCreatePrompt-->copySourceDropdown.clear();
  540. // Remove the dialog and create the datablock.
  541. canvas.popDialog( DatablockEditorCreatePrompt );
  542. %this.createDatablockFinish( %name, %copySource );
  543. }
  544. //---------------------------------------------------------------------------------------------
  545. function DatablockEditorPlugin::createDatablockFinish( %this, %name, %copySource )
  546. {
  547. if($DATABLOCK_EDITOR_NEWDB_CLASS $= "")
  548. %class = DatablockEditorTypeTree.getItemText(DatablockEditorTypeTree.getSelectedItem());
  549. else
  550. %class = $DATABLOCK_EDITOR_NEWDB_CLASS;
  551. if( %class !$= "" )
  552. {
  553. %action = %this.createUndo( ActionCreateDatablock, "Create New Datablock" );
  554. if( DatablockEditorCreatePrompt-->ClientSideCheckBox.isStateOn() )
  555. %dbType = "singleton ";
  556. else
  557. %dbType = "datablock ";
  558. if( %copySource !$= "" )
  559. %eval = %dbType @ %class @ "(" @ %name @ " : " @ %copySource @ ") { canSaveDynamicFields = \"1\"; };";
  560. else
  561. %eval = %dbType @ %class @ "(" @ %name @ ") { canSaveDynamicFields = \"1\"; };";
  562. %res = eval( %eval );
  563. %action.db = %name.getId();
  564. %action.dbName = %name;
  565. %action.fname = $DATABLOCK_EDITOR_DEFAULT_FILENAME;
  566. %this.submitUndo( %action );
  567. %action.redo();
  568. }
  569. $DATABLOCK_EDITOR_NEWDB_CLASS = "";
  570. }
  571. //---------------------------------------------------------------------------------------------
  572. function DatablockEditorPlugin::canBeClientSideDatablock( %className )
  573. {
  574. switch$( %className )
  575. {
  576. case "SFXProfile" or
  577. "SFXPlayList" or
  578. "SFXAmbience" or
  579. "SFXEnvironment" or
  580. "SFXState" or
  581. "SFXDescription":
  582. return true;
  583. default:
  584. return false;
  585. }
  586. }
  587. //------------------------------------------------------------------------------
  588. // GUI
  589. //------------------------------------------------------------------------------
  590. //-----------------------------------------------------------------------------
  591. function DatablockEditorGui::onWake(%this)
  592. {
  593. %fixedWindow = MeshRoadEditorTreeWindow;
  594. %fluidWindow = MeshRoadEditorOptionsWindow;
  595. if(EditorSettings.value( "WorldEditor/forceSidebarToSide" ) == 1)
  596. {
  597. // Let's dock the side panel to the right side
  598. %this.docked = false;
  599. %this.resizing = true;
  600. %this.dockSidePanel();
  601. }
  602. else
  603. {
  604. // Let's release the side panel so it can be moved
  605. %this.docked = true;
  606. %this.resizing = false;
  607. %this.releaseSidePanel();
  608. }
  609. }
  610. function DatablockEditorGui::maxSize(%this, %window)
  611. {
  612. // Resize the windows to the max height
  613. // and force these to the right side if set
  614. if(EditorSettings.value( "WorldEditor/forceSidebarToSide" ) == 1 && %this.resizing == true)
  615. {
  616. // prevent onResize after a resize
  617. %this.resizing = false;
  618. %fixedWindow = DatablockEditorTreeWindow;
  619. %fluidWindow = DatablockEditorInspectorWindow;
  620. %top = EditorGuiToolbar.extent.y + 6;
  621. %bottom = %top + 65 - 6;
  622. %maxHeight = Canvas.extent.y - %top - %bottom;
  623. // --- Fixed window (top) ------------------------------------------------
  624. // put it back if it moved
  625. %fixedWindow.position.x = Canvas.extent.x - %fixedWindow.extent.x;
  626. %fixedWindow.position.y = %top;
  627. // don't go beyond the canvas
  628. if(%fixedWindow.extent.y > %maxHeight)
  629. %fixedWindow.extent.y = %maxHeight - %fluidWindow.extent.y;
  630. %position = %fixedWindow.position.x SPC %fixedWindow.position.y;
  631. %extent = %window.extent.x SPC %fixedWindow.extent.y;
  632. %fixedWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  633. // --- Fluid window (bottom) ---------------------------------------------
  634. // position is relative to the top window
  635. %position = %fixedWindow.position.x SPC %fixedWindow.extent.y + %top;
  636. %extent = %window.extent.x SPC Canvas.extent.y - %fixedWindow.extent.y - %bottom;
  637. %fluidWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  638. // --- AssetBrowser window ----------------------------------------------
  639. if(isObject(AssetBrowserWindow))
  640. {
  641. // Only resize the AssetBrowser if it's docked
  642. if(AssetBrowserWindow.docked == true)
  643. {
  644. // The width is relative to the sidepanel
  645. %browserWidth = Canvas.extent.x - %extent.x;
  646. %browserHeight = AssetBrowserWindow.extent.y;
  647. %browserPosY = Canvas.extent.y - AssetBrowserWindow.extent.y - 33;
  648. AssetBrowserWindow.resize(0, %browserPosY, %browserWidth, %browserHeight);
  649. }
  650. }
  651. // --- Windowed Console --------------------------------------------------
  652. if(isObject(windowConsoleControl))
  653. {
  654. // Only resize the AssetBrowser if it's docked
  655. if(windowConsoleControl.docked == true)
  656. {
  657. // The width is relative to the sidepanel
  658. %consoleWidth = Canvas.extent.x - %extent.x;
  659. %consoleHeight = windowConsoleControl.extent.y;
  660. %consolePosY = Canvas.extent.y - windowConsoleControl.extent.y - 33;
  661. windowConsoleControl.resize(0, %consolePosY, %consoleWidth, %consoleHeight);
  662. }
  663. }
  664. }
  665. }
  666. function DatablockEditorTreeWindow::onMouseDragged(%this)
  667. {
  668. %parent = DatablockEditorGui;
  669. if(%parent.panelHidden == true)
  670. {
  671. %parent.showSidePanel();
  672. }
  673. if(%parent.resizing == false && %parent.docked == true)
  674. {
  675. %parent.resizing = true;
  676. %parent.maxSize(%this);
  677. }
  678. }
  679. function DatablockEditorInspectorWindow::onMouseDragged(%this)
  680. {
  681. %parent = DatablockEditorGui;
  682. if(%parent.panelHidden == true)
  683. {
  684. %parent.showSidePanel();
  685. }
  686. if(%parent.resizing == false && %parent.docked == true)
  687. {
  688. %parent.resizing = true;
  689. %parent.maxSize(%this);
  690. }
  691. }
  692. function DatablockEditorGui::onResize(%this, %newPosition, %newExtent)
  693. {
  694. // Window to focus on (mostly the fluid window)
  695. %window = DatablockEditorInspectorWindow;
  696. if(%window.panelHidden == true)
  697. {
  698. %window.showSidePanel();
  699. }
  700. if(%this.resizing == false && %this.docked == true)
  701. {
  702. // Only resize once
  703. %this.resizing = true;
  704. %this.maxSize(%window);
  705. }
  706. }
  707. function DatablockEditorGui::dockSidePanel()
  708. {
  709. %parent = DatablockEditorGui;
  710. %fixedWindow = DatablockEditorTreeWindow;
  711. %fluidWindow = DatablockEditorInspectorWindow;
  712. if(%parent.docked == true)
  713. return;
  714. // Move and resize the window(s)
  715. %parent.resizing = true;
  716. %parent.maxSize(%fluidWindow);
  717. %parent.docked = true;
  718. %fluidWindow.onMouseDragged();
  719. // Lock the windows in place
  720. %fixedWindow.canCollapse = "0";
  721. %fixedWindow.canMove = "0";
  722. %fluidWindow.canCollapse = "0";
  723. %fluidWindow.canMove = "0";
  724. DatablockEditorGui_UnDockBtn.Visible = "1";
  725. DatablockEditorGui_DockBtn.Visible = "0";
  726. DatablockEditorGui_showBtn.Visible = "0";
  727. DatablockEditorGui_hideBtn.Visible = "1";
  728. }
  729. function DatablockEditorGui::releaseSidePanel()
  730. {
  731. %parent = DatablockEditorGui;
  732. %fixedWindow = DatablockEditorTreeWindow;
  733. %fluidWindow = DatablockEditorInspectorWindow;
  734. if(%parent.docked == false)
  735. return;
  736. // Unlock the windows so that be moved
  737. %fixedWindow.canCollapse = "1";
  738. %fixedWindow.canMove = "1";
  739. %fluidWindow.canCollapse = "1";
  740. %fluidWindow.canMove = "1";
  741. DatablockEditorGui_UnDockBtn.Visible = "0";
  742. DatablockEditorGui_DockBtn.Visible = "1";
  743. DatablockEditorGui_showBtn.Visible = "0";
  744. DatablockEditorGui_hideBtn.Visible = "0";
  745. // Let's do a small resize so it's visually clear we're undocking
  746. %position = %fixedWindow.position.x - 6 SPC %fixedWindow.position.y + 6;
  747. %extent = %fixedWindow.extent.x SPC %fixedWindow.extent.y;
  748. %fixedWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  749. %position = %fluidWindow.position.x - 6 SPC %fluidWindow.position.y + 6;
  750. %extent = %fluidWindow.extent.x SPC %fluidWindow.extent.y - 12;
  751. %fluidWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  752. %parent.docked = false;
  753. %parent.resizing = false;
  754. }
  755. function DatablockEditorGui::hideSidePanel()
  756. {
  757. %parent = DatablockEditorGui;
  758. %fixedWindow = DatablockEditorTreeWindow;
  759. %fluidWindow = DatablockEditorInspectorWindow;
  760. DatablockEditorGui_showBtn.Visible = "1";
  761. DatablockEditorGui_hideBtn.Visible = "0";
  762. // hide the content of the panels
  763. %fixedWindow.titleText = %fixedWindow.text;
  764. %fluidWindow.titleText = %fluidWindow.text;
  765. %fixedWindow.text = "";
  766. DatablockEditorTreeTabBook.Visible = "0";
  767. %fluidWindow.text = "";
  768. DatablockEditorInspectorPanel.Visible = "0";
  769. DatablockFieldInfoControl.Visible = "0";
  770. DatablockFileField.Visible = "0";
  771. // Let's do a resize so that the panel is collapsed to the side
  772. %position = Canvas.extent.x - 24 SPC %fixedWindow.position.y;
  773. %extent = %fixedWindow.extent.x SPC %fixedWindow.extent.y;
  774. %fixedWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  775. %position = Canvas.extent.x - 24 SPC %fluidWindow.position.y;
  776. %extent = %fluidWindow.extent.x SPC %fluidWindow.extent.y;
  777. %fluidWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  778. %parent.panelHidden = true;
  779. }
  780. function DatablockEditorGui::showSidePanel()
  781. {
  782. %parent = DatablockEditorGui;
  783. %fixedWindow = DatablockEditorTreeWindow;
  784. %fluidWindow = DatablockEditorInspectorWindow;
  785. DatablockEditorGui_showBtn.Visible = "0";
  786. DatablockEditorGui_hideBtn.Visible = "1";
  787. // show the content of the panels
  788. // hide the content of the panels
  789. %fixedWindow.text = %fixedWindow.titleText;
  790. DatablockEditorTreeTabBook.Visible = "1";
  791. %fluidWindow.text = %fluidWindow.titleText;
  792. DatablockEditorInspectorPanel.Visible = "1";
  793. DatablockFieldInfoControl.Visible = "1";
  794. DatablockFileField.Visible = "1";
  795. %parent.resizing = true;
  796. %parent.maxSize(%fluidWindow);
  797. %parent.panelHidden = false;
  798. }
  799. //------------------------------------------------------------------------------
  800. //=============================================================================================
  801. // Events.
  802. //=============================================================================================
  803. //---------------------------------------------------------------------------------------------
  804. function DatablockEditorInspector::onInspectorFieldModified( %this, %object, %fieldName, %arrayIndex, %oldValue, %newValue )
  805. {
  806. // Same work to do as for the regular WorldEditor Inspector.
  807. Inspector::onInspectorFieldModified( %this, %object, %fieldName, %arrayIndex, %oldValue, %newValue );
  808. DatablockEditorPlugin.flagDatablockAsDirty( %object, true );
  809. }
  810. //---------------------------------------------------------------------------------------------
  811. function DatablockEditorInspector::onFieldSelected( %this, %fieldName, %fieldTypeStr, %fieldDoc )
  812. {
  813. DatablockFieldInfoControl.setText( "<font:" @ $Gui::fontTypeBold @ ":16>" @ %fieldName @ "<font:Arial Italic:14> (" @ %fieldTypeStr @ ") " @ " : " @ "<font:Arial:14>" @ %fieldDoc );
  814. }
  815. //---------------------------------------------------------------------------------------------
  816. function DatablockEditorInspector::onBeginCompoundEdit( %this )
  817. {
  818. Editor.getUndoManager().pushCompound( "Multiple Field Edit" );
  819. }
  820. //---------------------------------------------------------------------------------------------
  821. function DatablockEditorInspector::onEndCompoundEdit( %this, %discard )
  822. {
  823. Editor.getUndoManager().popCompound( %discard );
  824. }
  825. //---------------------------------------------------------------------------------------------
  826. function DatablockEditorInspector::onClear( %this )
  827. {
  828. DatablockFieldInfoControl.setText( "" );
  829. }
  830. //---------------------------------------------------------------------------------------------
  831. function DatablockEditorTree::onDeleteSelection( %this )
  832. {
  833. %this.undoDeleteList = "";
  834. }
  835. //---------------------------------------------------------------------------------------------
  836. function DatablockEditorTree::onDeleteObject( %this, %object )
  837. {
  838. // Append it to our list.
  839. %this.undoDeleteList = %this.undoDeleteList TAB %object;
  840. // We're gonna delete this ourselves in the
  841. // completion callback.
  842. return true;
  843. }
  844. //---------------------------------------------------------------------------------------------
  845. function DatablockEditorTree::onObjectDeleteCompleted( %this )
  846. {
  847. //MEDeleteUndoAction::submit( %this.undoDeleteList );
  848. // Let the world editor know to
  849. // clear its selection.
  850. //EWorldEditor.clearSelection();
  851. //EWorldEditor.isDirty = true;
  852. }
  853. //---------------------------------------------------------------------------------------------
  854. function DatablockEditorTree::onClearSelected(%this)
  855. {
  856. DatablockEditorInspector.inspect( 0 );
  857. }
  858. //---------------------------------------------------------------------------------------------
  859. function DatablockEditorTree::onAddSelection( %this, %id )
  860. {
  861. %obj = %this.getItemValue( %id );
  862. if( !isObject( %obj ) )
  863. %this.selectItem( %id, false );
  864. else
  865. DatablockEditorPlugin.selectDatablock( %obj, true, true );
  866. }
  867. //---------------------------------------------------------------------------------------------
  868. function DatablockEditorTree::onRemoveSelection( %this, %id )
  869. {
  870. %obj = %this.getItemValue( %id );
  871. if( isObject( %obj ) )
  872. DatablockEditorPlugin.unselectDatablock( %obj, true );
  873. }
  874. //---------------------------------------------------------------------------------------------
  875. function DatablockEditorTree::onRightMouseUp( %this, %id, %mousePos )
  876. {
  877. %datablock = %this.getItemValue( %id );
  878. if( !isObject( %datablock ) )
  879. return;
  880. if( !isObject( DatablockEditorTreePopup ) )
  881. new PopupMenu( DatablockEditorTreePopup )
  882. {
  883. superClass = "MenuBuilder";
  884. isPopup = true;
  885. item[ 0 ] = "Delete" TAB "" TAB "DatablockEditorPlugin.selectDatablock( DatablockEditorTreePopup.datablockObject ); DatablockEditorPlugin.deleteDatablock( DatablockEditorTreePopup.datablockObject );";
  886. item[ 1 ] = "Jump to Definition in Torsion" TAB "" TAB "EditorOpenDeclarationInTorsion( DatablockEditorTreePopup.datablockObject );";
  887. datablockObject = "";
  888. };
  889. DatablockEditorTreePopup.datablockObject = %datablock;
  890. DatablockEditorTreePopup.showPopup( Canvas );
  891. }
  892. //---------------------------------------------------------------------------------------------
  893. function DatablockEditorTreeTabBook::onTabSelected(%this, %text, %id)
  894. {
  895. switch(%id)
  896. {
  897. case 0:
  898. DatablockEditorTreeWindow-->DeleteSelection.visible = true;
  899. DatablockEditorTreeWindow-->CreateSelection.visible = false;
  900. case 1:
  901. DatablockEditorTreeWindow-->DeleteSelection.visible = false;
  902. DatablockEditorTreeWindow-->CreateSelection.visible = true;
  903. }
  904. }