datablockEditor.tscript 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. %this.map.push();
  53. // Set the status bar here until all tool have been hooked up
  54. EditorGuiStatusBar.setInfo( "Datablock editor." );
  55. %numSelected = %this.getNumSelectedDatablocks();
  56. if( !%numSelected )
  57. EditorGuiStatusBar.setSelection( "" );
  58. else
  59. EditorGuiStatusBar.setSelection( %numSelected @ " datablocks selected" );
  60. %this.init();
  61. DatablockEditorPlugin.readSettings();
  62. if( EWorldEditor.getSelectionSize() == 1 )
  63. %this.onObjectSelected( EWorldEditor.getSelectedObject( 0 ) );
  64. Parent::onActivated( %this );
  65. }
  66. //---------------------------------------------------------------------------------------------
  67. function DatablockEditorPlugin::onDeactivated( %this )
  68. {
  69. DatablockEditorPlugin.writeSettings();
  70. DatablockEditorInspectorWindow.setVisible( false );
  71. DatablockEditorTreeWindow.setVisible( false );
  72. %this.map.pop();
  73. Parent::onDeactivated(%this);
  74. }
  75. //---------------------------------------------------------------------------------------------
  76. function DatablockEditorPlugin::onExitMission( %this )
  77. {
  78. DatablockEditorTree.clear();
  79. DatablockEditorInspector.inspect( "" );
  80. }
  81. //---------------------------------------------------------------------------------------------
  82. function DatablockEditorPlugin::openDatablock( %this, %datablock )
  83. {
  84. EditorGui.setEditor( DatablockEditorPlugin );
  85. %this.selectDatablock( %datablock );
  86. DatablockEditorTreeTabBook.selectedPage = 0;
  87. }
  88. //---------------------------------------------------------------------------------------------
  89. function DatablockEditorPlugin::setEditorFunction( %this )
  90. {
  91. return true;
  92. }
  93. //---------------------------------------------------------------------------------------------
  94. function DatablockEditorPlugin::onObjectSelected( %this, %object )
  95. {
  96. // Select datablock of object if this is a GameBase object.
  97. if( %object.isMemberOfClass( "GameBase" ) )
  98. %this.selectDatablock( %object.getDatablock() );
  99. else if( %object.isMemberOfClass( "SFXEmitter" ) && isObject( %object.track ) )
  100. %this.selectDatablock( %object.track );
  101. else if( %object.isMemberOfClass( "LightBase" ) && isObject( %object.animationType ) )
  102. %this.selectDatablock( %object.animationType );
  103. }
  104. //---------------------------------------------------------------------------------------------
  105. function DatablockEditorPlugin::populateTrees(%this)
  106. {
  107. // Populate datablock tree.
  108. if( %this.excludeClientOnlyDatablocks )
  109. %set = DataBlockGroup;
  110. else
  111. %set = DataBlockSet;
  112. DatablockEditorTree.clear();
  113. foreach( %datablock in %set )
  114. {
  115. %unlistedFound = false;
  116. %id = %datablock.getId();
  117. foreach( %obj in UnlistedDatablocks )
  118. if( %obj.getId() == %id )
  119. {
  120. %unlistedFound = true;
  121. break;
  122. }
  123. if( %unlistedFound )
  124. continue;
  125. %this.addExistingItem( %datablock, true );
  126. }
  127. DatablockEditorTree.sort( 0, true, false, false );
  128. // Populate datablock type tree.
  129. %classList = enumerateConsoleClasses( "SimDatablock" );
  130. DatablockEditorTypeTree.clear();
  131. foreach$( %datablockClass in %classList )
  132. {
  133. if( !%this.isExcludedDatablockType( %datablockClass )
  134. && DatablockEditorTypeTree.findItemByName( %datablockClass ) == 0 )
  135. DatablockEditorTypeTree.insertItem( 0, %datablockClass );
  136. }
  137. DatablockEditorTypeTree.sort( 0, false, false, false );
  138. }
  139. //---------------------------------------------------------------------------------------------
  140. function DatablockEditorPlugin::addExistingItem( %this, %datablock, %dontSort )
  141. {
  142. %tree = DatablockEditorTree;
  143. // Look up class at root level. Create if needed.
  144. %class = %datablock.getClassName();
  145. %parentID = %tree.findItemByName( %class );
  146. if( %parentID == 0 )
  147. %parentID = %tree.insertItem( 0, %class );
  148. // If the datablock is already there, don't
  149. // do anything.
  150. if( %tree.findItemByValue( %datablock.getId() ) )
  151. return;
  152. // It doesn't exist so add it.
  153. %name = %datablock.getName();
  154. if( %this.PM.isDirty( %datablock ) )
  155. %name = %name @ " *";
  156. %id = DatablockEditorTree.insertItem( %parentID, %name, %datablock.getId() );
  157. if( !%dontSort )
  158. DatablockEditorTree.sort( %parentID, false, false, false );
  159. return %id;
  160. }
  161. //---------------------------------------------------------------------------------------------
  162. function DatablockEditorPlugin::isExcludedDatablockType( %this, %className )
  163. {
  164. switch$( %className )
  165. {
  166. case "SimDatablock":
  167. return true;
  168. case "SFXTrack": // Abstract.
  169. return true;
  170. }
  171. return false;
  172. }
  173. //=============================================================================================
  174. // Settings.
  175. //=============================================================================================
  176. //---------------------------------------------------------------------------------------------
  177. function DatablockEditorPlugin::initSettings( %this )
  178. {
  179. EditorSettings.beginGroup("DatablockEditor", true);
  180. EditorSettings.setDefaultValue("libraryTab", "0");
  181. EditorSettings.endGroup();
  182. }
  183. //---------------------------------------------------------------------------------------------
  184. function DatablockEditorPlugin::readSettings( %this )
  185. {
  186. EditorSettings.beginGroup("DatablockEditor", true);
  187. DatablockEditorTreeTabBook.selectPage( EditorSettings.value( "libraryTab" ) );
  188. %db = EditorSettings.value( "selectedDatablock" );
  189. if( isObject( %db ) && %db.isMemberOfClass( "SimDatablock" ) )
  190. %this.selectDatablock( %db );
  191. EditorSettings.endGroup();
  192. }
  193. //---------------------------------------------------------------------------------------------
  194. function DatablockEditorPlugin::writeSettings( %this )
  195. {
  196. EditorSettings.beginGroup( "DatablockEditor", true );
  197. EditorSettings.setValue( "libraryTab", DatablockEditorTreeTabBook.getSelectedPage() );
  198. if( %this.getNumSelectedDatablocks() > 0 )
  199. EditorSettings.setValue( "selectedDatablock", %this.getSelectedDatablock().getName() );
  200. EditorSettings.endGroup();
  201. }
  202. //=============================================================================================
  203. // Persistence.
  204. //=============================================================================================
  205. //---------------------------------------------------------------------------------------------
  206. /// Return true if there is any datablock with unsaved changes.
  207. function DatablockEditorPlugin::isDirty( %this )
  208. {
  209. return %this.PM.hasDirty();
  210. }
  211. //---------------------------------------------------------------------------------------------
  212. /// Return true if any of the currently selected datablocks has unsaved changes.
  213. function DatablockEditorPlugin::selectedDatablockIsDirty( %this )
  214. {
  215. %tree = DatablockEditorTree;
  216. %count = %tree.getSelectedItemsCount();
  217. %selected = %tree.getSelectedItemList();
  218. foreach$( %id in %selected )
  219. {
  220. %db = %tree.getItemValue( %id );
  221. if( %this.PM.isDirty( %db ) )
  222. return true;
  223. }
  224. return false;
  225. }
  226. //---------------------------------------------------------------------------------------------
  227. function DatablockEditorPlugin::syncDirtyState( %this )
  228. {
  229. %tree = DatablockEditorTree;
  230. %count = %tree.getSelectedItemsCount();
  231. %selected = %tree.getSelectedItemList();
  232. %haveDirty = false;
  233. foreach$( %id in %selected )
  234. {
  235. %db = %tree.getItemValue( %id );
  236. if( %this.PM.isDirty( %db ) )
  237. {
  238. %this.flagDatablockAsDirty( %db, true );
  239. %haveDirty = true;
  240. }
  241. else
  242. %this.flagInspectorAsDirty( %db, false );
  243. }
  244. %this.flagInspectorAsDirty( %haveDirty );
  245. }
  246. //---------------------------------------------------------------------------------------------
  247. ///
  248. function DatablockEditorPlugin::flagInspectorAsDirty( %this, %dirty )
  249. {
  250. if( %dirty )
  251. DatablockEditorInspectorWindow.text = "Datablock *";
  252. else
  253. DatablockEditorInspectorWindow.text = "Datablock";
  254. }
  255. //---------------------------------------------------------------------------------------------
  256. function DatablockEditorPlugin::flagDatablockAsDirty(%this, %datablock, %dirty )
  257. {
  258. %tree = DatablockEditorTree;
  259. %id = %tree.findItemByValue( %datablock.getId() );
  260. if( %id == 0 )
  261. return;
  262. // Tag the item caption and sync the persistence manager.
  263. if( %dirty )
  264. {
  265. DatablockEditorTree.editItem( %id, %datablock.getName() @ " *", %datablock.getId() );
  266. %this.PM.setDirty( %datablock );
  267. }
  268. else
  269. {
  270. DatablockEditorTree.editItem( %id, %datablock.getName(), %datablock.getId() );
  271. %this.PM.removeDirty( %datablock );
  272. }
  273. // Sync the inspector dirty state.
  274. %this.flagInspectorAsDirty( %this.PM.hasDirty() );
  275. }
  276. //---------------------------------------------------------------------------------------------
  277. function DatablockEditorPlugin::showSaveNewFileDialog(%this)
  278. {
  279. %currentFile = %this.getSelectedDatablock().getFilename();
  280. getSaveFilename( "TorqueScript Files|*." @ $TorqueScriptFileExtension @ "|All Files|*.*", %this @ ".saveNewFileFinish", %currentFile, false );
  281. }
  282. //---------------------------------------------------------------------------------------------
  283. function DatablockEditorPlugin::saveNewFileFinish( %this, %newFileName )
  284. {
  285. // Clear the first responder to capture any inspector changes
  286. %ctrl = canvas.getFirstResponder();
  287. if( isObject(%ctrl) )
  288. %ctrl.clearFirstResponder();
  289. %tree = DatablockEditorTree;
  290. %count = %tree.getSelectedItemsCount();
  291. %selected = %tree.getSelectedItemList();
  292. foreach$( %id in %selected )
  293. {
  294. %db = %tree.getItemValue( %id );
  295. %db = %this.getSelectedDatablock();
  296. // Remove from current file.
  297. %oldFileName = %db.getFileName();
  298. if( %oldFileName !$= "" )
  299. %this.PM.removeObjectFromFile( %db, %oldFileName );
  300. // Save to new file.
  301. %this.PM.setDirty( %db, %newFileName );
  302. if( %this.PM.saveDirtyObject( %db ) )
  303. {
  304. // Clear dirty state.
  305. %this.flagDatablockAsDirty( %db, false );
  306. }
  307. }
  308. DatablockEditorInspectorWindow-->DatablockFile.setText( %newFileName );
  309. }
  310. //---------------------------------------------------------------------------------------------
  311. function DatablockEditorPlugin::save( %this )
  312. {
  313. // Clear the first responder to capture any inspector changes
  314. %ctrl = canvas.getFirstResponder();
  315. if( isObject(%ctrl) )
  316. %ctrl.clearFirstResponder();
  317. %tree = DatablockEditorTree;
  318. %count = %tree.getSelectedItemsCount();
  319. %selected = %tree.getSelectedItemList();
  320. for( %i = 0; %i < %count; %i ++ )
  321. {
  322. %id = getWord( %selected, %i );
  323. %db = %tree.getItemValue( %id );
  324. if( %this.PM.isDirty( %db ) )
  325. {
  326. %this.PM.saveDirtyObject( %db );
  327. %this.flagDatablockAsDirty( %db, false );
  328. }
  329. }
  330. }
  331. //=============================================================================================
  332. // Selection.
  333. //=============================================================================================
  334. //---------------------------------------------------------------------------------------------
  335. function DatablockEditorPlugin::getNumSelectedDatablocks( %this )
  336. {
  337. return DatablockEditorTree.getSelectedItemsCount();
  338. }
  339. //---------------------------------------------------------------------------------------------
  340. function DatablockEditorPlugin::getSelectedDatablock( %this, %index )
  341. {
  342. %tree = DatablockEditorTree;
  343. if( !%tree.getSelectedItemsCount() )
  344. return 0;
  345. if( !%index )
  346. %id = %tree.getSelectedItem();
  347. else
  348. %id = getWord( %tree.getSelectedItemList(), %index );
  349. return %tree.getItemValue( %id );
  350. }
  351. //---------------------------------------------------------------------------------------------
  352. function DatablockEditorPlugin::resetSelectedDatablock( %this )
  353. {
  354. DatablockEditorTree.clearSelection();
  355. DatablockEditorInspector.inspect(0);
  356. DatablockEditorInspectorWindow-->DatablockFile.setText("");
  357. EditorGuiStatusBar.setSelection( "" );
  358. }
  359. //---------------------------------------------------------------------------------------------
  360. function DatablockEditorPlugin::selectDatablockCheck( %this, %datablock )
  361. {
  362. if( %this.selectedDatablockIsDirty() )
  363. %this.showSaveDialog( %datablock );
  364. else
  365. %this.selectDatablock( %datablock );
  366. }
  367. //---------------------------------------------------------------------------------------------
  368. function DatablockEditorPlugin::selectDatablock( %this, %datablock, %add, %dontSyncTree )
  369. {
  370. if( %add )
  371. DatablockEditorInspector.addInspect( %datablock );
  372. else
  373. DatablockEditorInspector.inspect( %datablock );
  374. if( !%dontSyncTree )
  375. {
  376. %id = DatablockEditorTree.findItemByValue( %datablock.getId() );
  377. if( !%add )
  378. DatablockEditorTree.clearSelection();
  379. DatablockEditorTree.selectItem( %id, true );
  380. DatablockEditorTree.scrollVisible( %id );
  381. }
  382. %this.syncDirtyState();
  383. // Update the filename text field.
  384. %numSelected = %this.getNumSelectedDatablocks();
  385. %fileNameField = DatablockEditorInspectorWindow-->DatablockFile;
  386. if( %numSelected == 1 )
  387. {
  388. %fileName = %datablock.getFilename();
  389. if( %fileName !$= "" )
  390. %fileNameField.setText( %fileName );
  391. else
  392. %fileNameField.setText( $DATABLOCK_EDITOR_DEFAULT_FILENAME );
  393. }
  394. else
  395. {
  396. %fileNameField.setText( "" );
  397. }
  398. EditorGuiStatusBar.setSelection( %this.getNumSelectedDatablocks() @ " Datablocks Selected" );
  399. }
  400. //---------------------------------------------------------------------------------------------
  401. function DatablockEditorPlugin::unselectDatablock( %this, %datablock, %dontSyncTree )
  402. {
  403. DatablockEditorInspector.removeInspect( %datablock );
  404. if( !%dontSyncTree )
  405. {
  406. %id = DatablockEditorTree.findItemByValue( %datablock.getId() );
  407. DatablockEditorTree.selectItem( %id, false );
  408. }
  409. %this.syncDirtyState();
  410. // If we have exactly one selected datablock remaining, re-enable
  411. // the save-as button.
  412. %numSelected = %this.getNumSelectedDatablocks();
  413. if( %numSelected == 1 )
  414. {
  415. DatablockEditorInspectorWindow-->saveAsButton.setActive( true );
  416. %fileNameField = DatablockEditorInspectorWindow-->DatablockFile;
  417. %fileNameField.setText( %this.getSelectedDatablock().getFilename() );
  418. %fileNameField.setActive( true );
  419. }
  420. EditorGuiStatusBar.setSelection( %this.getNumSelectedDatablocks() @ " Datablocks Selected" );
  421. }
  422. //=============================================================================================
  423. // Creation and Deletion.
  424. //=============================================================================================
  425. //---------------------------------------------------------------------------------------------
  426. function DatablockEditorPlugin::deleteDatablock( %this )
  427. {
  428. %tree = DatablockEditorTree;
  429. // If we have more than single datablock selected,
  430. // turn our undos into a compound undo.
  431. %numSelected = %tree.getSelectedItemsCount();
  432. if( %numSelected > 1 )
  433. Editor.getUndoManager().pushCompound( "Delete Multiple Datablocks" );
  434. for( %i = 0; %i < %numSelected; %i ++ )
  435. {
  436. %id = %tree.getSelectedItem( %i );
  437. %db = %tree.getItemValue( %id );
  438. %fileName = %db.getFileName();
  439. // Remove the datablock from the tree.
  440. DatablockEditorTree.removeItem( %id );
  441. // Create undo.
  442. %action = %this.createUndo( ActionDeleteDatablock, "Delete Datablock" );
  443. %action.db = %db;
  444. %action.dbName = %db.getName();
  445. %action.fname = %fileName;
  446. %this.submitUndo( %action );
  447. // Kill the datablock in the file.
  448. if( %fileName !$= "" )
  449. %this.PM.removeObjectFromFile( %db );
  450. UnlistedDatablocks.add( %db );
  451. // Show some confirmation.
  452. if( %numSelected == 1 )
  453. toolsMessageBoxOk( "Datablock Deleted", "The datablock (" @ %db.getName() @ ") has been removed from " @
  454. "it's file (" @ %db.getFilename() @ ") and upon restart will cease to exist" );
  455. }
  456. // Close compound, if we were deleting multiple datablocks.
  457. if( %numSelected > 1 )
  458. Editor.getUndoManager().popCompound();
  459. // Show confirmation for multiple datablocks.
  460. if( %numSelected > 1 )
  461. toolsMessageBoxOk( "Datablocks Deleted", "The datablocks have been deleted and upon restart will cease to exist." );
  462. // Clear selection.
  463. DatablockEditorPlugin.resetSelectedDatablock();
  464. }
  465. //---------------------------------------------------------------------------------------------
  466. function DatablockEditorPlugin::createDatablock(%this)
  467. {
  468. %class = DatablockEditorTypeTree.getItemText(DatablockEditorTypeTree.getSelectedItem());
  469. if( %class !$= "" )
  470. {
  471. // Need to prompt for a name.
  472. DatablockEditorCreatePrompt-->CreateDatablockName.setText("Name");
  473. DatablockEditorCreatePrompt-->CreateDatablockName.selectAllText();
  474. // Populate the copy source dropdown.
  475. %list = DatablockEditorCreatePrompt-->CopySourceDropdown;
  476. %list.clear();
  477. %list.add( "", 0 );
  478. %set = DataBlockSet;
  479. %count = %set.getCount();
  480. for( %i = 0; %i < %count; %i ++ )
  481. {
  482. %datablock = %set.getObject( %i );
  483. %datablockClass = %datablock.getClassName();
  484. if( !isMemberOfClass( %datablockClass, %class ) )
  485. continue;
  486. %list.add( %datablock.getName(), %i + 1 );
  487. }
  488. // Set up state of client-side checkbox.
  489. %clientSideCheckBox = DatablockEditorCreatePrompt-->ClientSideCheckBox;
  490. %canBeClientSide = DatablockEditorPlugin::canBeClientSideDatablock( %class );
  491. %clientSideCheckBox.setStateOn( %canBeClientSide );
  492. %clientSideCheckBox.setActive( %canBeClientSide );
  493. // Show the dialog.
  494. canvas.pushDialog( DatablockEditorCreatePrompt, 0, true );
  495. DatablockEditorCreatePrompt-->promptWindow.text = "Create New Datablock in module: " @ DatablockEditorPlugin.targetCreationModule;
  496. }
  497. }
  498. //---------------------------------------------------------------------------------------------
  499. function DatablockEditorPlugin::pickDatablockPath(%this)
  500. {
  501. AssetBrowser_SelectModule.showDialog("DatablockEditorPlugin.pickedNewDBTargetModule");
  502. //SelectAssetPath.showDialog(AssetBrowser.dirHandler.currentAddress, "DatablockEditorPlugin.pickedNewDBPath", "Select New Datablock Path");
  503. AssetBrowser_SelectModuleWindow.selectWindow();
  504. }
  505. function DatablockEditorPlugin::pickedNewDBTargetModule(%this, %module)
  506. {
  507. DatablockEditorPlugin.targetCreationModule = %module;
  508. %moduleDef = ModuleDatabase.findModule(%module);
  509. $DATABLOCK_EDITOR_DEFAULT_FILENAME = %moduleDef.ModulePath @ "/scripts/managedData/managedDatablocks." @ $TorqueScriptFileExtension;
  510. if(!isDirectory(filePath($DATABLOCK_EDITOR_DEFAULT_FILENAME)))
  511. {
  512. AssetBrowser.dirHandler.createFolder(filePath($DATABLOCK_EDITOR_DEFAULT_FILENAME));
  513. }
  514. DatablockEditorPlugin.createDatablock();
  515. }
  516. function DatablockEditorPlugin::createPromptNameCheck(%this, %path)
  517. {
  518. %name = DatablockEditorCreatePrompt-->CreateDatablockName.getText();
  519. if( !Editor::validateObjectName( %name, true ) )
  520. return;
  521. // Fetch the copy source and clear the list.
  522. %copySource = DatablockEditorCreatePrompt-->copySourceDropdown.getText();
  523. DatablockEditorCreatePrompt-->copySourceDropdown.clear();
  524. // Remove the dialog and create the datablock.
  525. canvas.popDialog( DatablockEditorCreatePrompt );
  526. %this.createDatablockFinish( %name, %copySource );
  527. }
  528. //---------------------------------------------------------------------------------------------
  529. function DatablockEditorPlugin::createDatablockFinish( %this, %name, %copySource )
  530. {
  531. %class = DatablockEditorTypeTree.getItemText(DatablockEditorTypeTree.getSelectedItem());
  532. if( %class !$= "" )
  533. {
  534. %action = %this.createUndo( ActionCreateDatablock, "Create New Datablock" );
  535. if( DatablockEditorCreatePrompt-->ClientSideCheckBox.isStateOn() )
  536. %dbType = "singleton ";
  537. else
  538. %dbType = "datablock ";
  539. if( %copySource !$= "" )
  540. %eval = %dbType @ %class @ "(" @ %name @ " : " @ %copySource @ ") { canSaveDynamicFields = \"1\"; };";
  541. else
  542. %eval = %dbType @ %class @ "(" @ %name @ ") { canSaveDynamicFields = \"1\"; };";
  543. %res = eval( %eval );
  544. %action.db = %name.getId();
  545. %action.dbName = %name;
  546. %action.fname = $DATABLOCK_EDITOR_DEFAULT_FILENAME;
  547. %this.submitUndo( %action );
  548. %action.redo();
  549. }
  550. }
  551. //---------------------------------------------------------------------------------------------
  552. function DatablockEditorPlugin::canBeClientSideDatablock( %className )
  553. {
  554. switch$( %className )
  555. {
  556. case "SFXProfile" or
  557. "SFXPlayList" or
  558. "SFXAmbience" or
  559. "SFXEnvironment" or
  560. "SFXState" or
  561. "SFXDescription":
  562. return true;
  563. default:
  564. return false;
  565. }
  566. }
  567. //=============================================================================================
  568. // Events.
  569. //=============================================================================================
  570. //---------------------------------------------------------------------------------------------
  571. function DatablockEditorInspector::onInspectorFieldModified( %this, %object, %fieldName, %arrayIndex, %oldValue, %newValue )
  572. {
  573. // Same work to do as for the regular WorldEditor Inspector.
  574. Inspector::onInspectorFieldModified( %this, %object, %fieldName, %arrayIndex, %oldValue, %newValue );
  575. DatablockEditorPlugin.flagDatablockAsDirty( %object, true );
  576. }
  577. //---------------------------------------------------------------------------------------------
  578. function DatablockEditorInspector::onFieldSelected( %this, %fieldName, %fieldTypeStr, %fieldDoc )
  579. {
  580. DatablockFieldInfoControl.setText( "<font:ArialBold:14>" @ %fieldName @ "<font:ArialItalic:14> (" @ %fieldTypeStr @ ") " NL "<font:Arial:14>" @ %fieldDoc );
  581. }
  582. //---------------------------------------------------------------------------------------------
  583. function DatablockEditorInspector::onBeginCompoundEdit( %this )
  584. {
  585. Editor.getUndoManager().pushCompound( "Multiple Field Edit" );
  586. }
  587. //---------------------------------------------------------------------------------------------
  588. function DatablockEditorInspector::onEndCompoundEdit( %this, %discard )
  589. {
  590. Editor.getUndoManager().popCompound( %discard );
  591. }
  592. //---------------------------------------------------------------------------------------------
  593. function DatablockEditorInspector::onClear( %this )
  594. {
  595. DatablockFieldInfoControl.setText( "" );
  596. }
  597. //---------------------------------------------------------------------------------------------
  598. function DatablockEditorTree::onDeleteSelection( %this )
  599. {
  600. %this.undoDeleteList = "";
  601. }
  602. //---------------------------------------------------------------------------------------------
  603. function DatablockEditorTree::onDeleteObject( %this, %object )
  604. {
  605. // Append it to our list.
  606. %this.undoDeleteList = %this.undoDeleteList TAB %object;
  607. // We're gonna delete this ourselves in the
  608. // completion callback.
  609. return true;
  610. }
  611. //---------------------------------------------------------------------------------------------
  612. function DatablockEditorTree::onObjectDeleteCompleted( %this )
  613. {
  614. //MEDeleteUndoAction::submit( %this.undoDeleteList );
  615. // Let the world editor know to
  616. // clear its selection.
  617. //EWorldEditor.clearSelection();
  618. //EWorldEditor.isDirty = true;
  619. }
  620. //---------------------------------------------------------------------------------------------
  621. function DatablockEditorTree::onClearSelected(%this)
  622. {
  623. DatablockEditorInspector.inspect( 0 );
  624. }
  625. //---------------------------------------------------------------------------------------------
  626. function DatablockEditorTree::onAddSelection( %this, %id )
  627. {
  628. %obj = %this.getItemValue( %id );
  629. if( !isObject( %obj ) )
  630. %this.selectItem( %id, false );
  631. else
  632. DatablockEditorPlugin.selectDatablock( %obj, true, true );
  633. }
  634. //---------------------------------------------------------------------------------------------
  635. function DatablockEditorTree::onRemoveSelection( %this, %id )
  636. {
  637. %obj = %this.getItemValue( %id );
  638. if( isObject( %obj ) )
  639. DatablockEditorPlugin.unselectDatablock( %obj, true );
  640. }
  641. //---------------------------------------------------------------------------------------------
  642. function DatablockEditorTree::onRightMouseUp( %this, %id, %mousePos )
  643. {
  644. %datablock = %this.getItemValue( %id );
  645. if( !isObject( %datablock ) )
  646. return;
  647. if( !isObject( DatablockEditorTreePopup ) )
  648. new PopupMenu( DatablockEditorTreePopup )
  649. {
  650. superClass = "MenuBuilder";
  651. isPopup = true;
  652. item[ 0 ] = "Delete" TAB "" TAB "DatablockEditorPlugin.selectDatablock( %this.datablockObject ); DatablockEditorPlugin.deleteDatablock( %this.datablockObject );";
  653. item[ 1 ] = "Jump to Definition in Torsion" TAB "" TAB "EditorOpenDeclarationInTorsion( %this.datablockObject );";
  654. datablockObject = "";
  655. };
  656. DatablockEditorTreePopup.datablockObject = %datablock;
  657. DatablockEditorTreePopup.showPopup( Canvas );
  658. }
  659. //---------------------------------------------------------------------------------------------
  660. function DatablockEditorTreeTabBook::onTabSelected(%this, %text, %id)
  661. {
  662. switch(%id)
  663. {
  664. case 0:
  665. DatablockEditorTreeWindow-->DeleteSelection.visible = true;
  666. DatablockEditorTreeWindow-->CreateSelection.visible = false;
  667. case 1:
  668. DatablockEditorTreeWindow-->DeleteSelection.visible = false;
  669. DatablockEditorTreeWindow-->CreateSelection.visible = true;
  670. }
  671. }