datablockEditor.tscript 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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("");
  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 " @ %class @ " 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. {
  537. toolsMessageBoxOK("Error", "Unable to validate new datablock name. Please make sure it's an alphanumeric value and that it is unique!");
  538. return;
  539. }
  540. // Fetch the copy source and clear the list.
  541. %copySource = DatablockEditorCreatePrompt-->copySourceDropdown.getText();
  542. DatablockEditorCreatePrompt-->copySourceDropdown.clear();
  543. // Remove the dialog and create the datablock.
  544. canvas.popDialog( DatablockEditorCreatePrompt );
  545. %this.createDatablockFinish( %name, %copySource );
  546. }
  547. //---------------------------------------------------------------------------------------------
  548. function DatablockEditorPlugin::createDatablockFinish( %this, %name, %copySource )
  549. {
  550. if($DATABLOCK_EDITOR_NEWDB_CLASS $= "")
  551. %class = DatablockEditorTypeTree.getItemText(DatablockEditorTypeTree.getSelectedItem());
  552. else
  553. %class = $DATABLOCK_EDITOR_NEWDB_CLASS;
  554. if( %class !$= "" )
  555. {
  556. %action = %this.createUndo( ActionCreateDatablock, "Create New Datablock" );
  557. if( DatablockEditorCreatePrompt-->ClientSideCheckBox.isStateOn() )
  558. %dbType = "singleton ";
  559. else
  560. %dbType = "datablock ";
  561. if( %copySource !$= "" )
  562. %eval = %dbType @ %class @ "(" @ %name @ " : " @ %copySource @ ") { canSaveDynamicFields = \"1\"; };";
  563. else
  564. %eval = %dbType @ %class @ "(" @ %name @ ") { canSaveDynamicFields = \"1\"; };";
  565. %res = eval( %eval );
  566. %action.db = %name.getId();
  567. %action.dbName = %name;
  568. %action.fname = $DATABLOCK_EDITOR_DEFAULT_FILENAME;
  569. %this.submitUndo( %action );
  570. %action.redo();
  571. DatablockEditorPlugin.openDatablock(%name);
  572. }
  573. $DATABLOCK_EDITOR_NEWDB_CLASS = "";
  574. }
  575. //---------------------------------------------------------------------------------------------
  576. function DatablockEditorPlugin::canBeClientSideDatablock( %className )
  577. {
  578. switch$( %className )
  579. {
  580. case "SFXProfile" or
  581. "SFXPlayList" or
  582. "SFXAmbience" or
  583. "SFXEnvironment" or
  584. "SFXState" or
  585. "SFXDescription":
  586. return true;
  587. default:
  588. return false;
  589. }
  590. }
  591. //------------------------------------------------------------------------------
  592. // GUI
  593. //------------------------------------------------------------------------------
  594. //-----------------------------------------------------------------------------
  595. function DatablockEditorGui::onWake(%this)
  596. {
  597. %fixedWindow = MeshRoadEditorTreeWindow;
  598. %fluidWindow = MeshRoadEditorOptionsWindow;
  599. if(EditorSettings.value( "WorldEditor/forceSidebarToSide" ) == 1)
  600. {
  601. // Let's dock the side panel to the right side
  602. %this.docked = false;
  603. %this.resizing = true;
  604. %this.dockSidePanel();
  605. }
  606. else
  607. {
  608. // Let's release the side panel so it can be moved
  609. %this.docked = true;
  610. %this.resizing = false;
  611. %this.releaseSidePanel();
  612. }
  613. }
  614. function DatablockEditorGui::maxSize(%this, %window)
  615. {
  616. // Resize the windows to the max height
  617. // and force these to the right side if set
  618. if(EditorSettings.value( "WorldEditor/forceSidebarToSide" ) == 1 && %this.resizing == true)
  619. {
  620. // prevent onResize after a resize
  621. %this.resizing = false;
  622. %fixedWindow = DatablockEditorTreeWindow;
  623. %fluidWindow = DatablockEditorInspectorWindow;
  624. %offset = -1; // tweak the vertical offset so that it aligns neatly
  625. %top = EditorGuiToolbar.extent.y + %offset;
  626. %bottom = %top + 59;
  627. %maxHeight = Canvas.extent.y - %top - %bottom;
  628. // --- Fixed window (top) ------------------------------------------------
  629. // put it back if it moved
  630. %fixedWindow.position.x = Canvas.extent.x - %fixedWindow.extent.x;
  631. %fixedWindow.position.y = %top;
  632. // don't go beyond the canvas
  633. if(%fixedWindow.extent.y > %maxHeight)
  634. %fixedWindow.extent.y = %maxHeight - %fluidWindow.extent.y;
  635. %position = %fixedWindow.position.x SPC %fixedWindow.position.y;
  636. %extent = %window.extent.x SPC %fixedWindow.extent.y;
  637. %fixedWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  638. // --- Fluid window (bottom) ---------------------------------------------
  639. // position is relative to the top window
  640. %position = %fixedWindow.position.x SPC %fixedWindow.extent.y + %top;
  641. %extent = %window.extent.x SPC Canvas.extent.y - %fixedWindow.extent.y - %bottom;
  642. %fluidWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  643. // --- AssetBrowser window ----------------------------------------------
  644. if(isObject(AssetBrowserWindow))
  645. {
  646. // Only resize the AssetBrowser if it's docked
  647. if(AssetBrowserWindow.docked == true)
  648. {
  649. // The width is relative to the sidepanel
  650. %browserWidth = Canvas.extent.x - %extent.x;
  651. %browserHeight = AssetBrowserWindow.extent.y;
  652. %browserPosY = Canvas.extent.y - AssetBrowserWindow.extent.y - 33;
  653. AssetBrowserWindow.resize(0, %browserPosY, %browserWidth, %browserHeight);
  654. }
  655. }
  656. // --- Windowed Console --------------------------------------------------
  657. if(isObject(windowConsoleControl))
  658. {
  659. // Only resize the AssetBrowser if it's docked
  660. if(windowConsoleControl.docked == true)
  661. {
  662. // The width is relative to the sidepanel
  663. %consoleWidth = Canvas.extent.x - %extent.x;
  664. %consoleHeight = windowConsoleControl.extent.y;
  665. %consolePosY = Canvas.extent.y - windowConsoleControl.extent.y - 33;
  666. windowConsoleControl.resize(0, %consolePosY, %consoleWidth, %consoleHeight);
  667. }
  668. }
  669. }
  670. }
  671. function DatablockEditorTreeWindow::onMouseDragged(%this)
  672. {
  673. %parent = DatablockEditorGui;
  674. if(%parent.panelHidden == true)
  675. {
  676. %parent.showSidePanel();
  677. }
  678. if(%parent.resizing == false && %parent.docked == true)
  679. {
  680. %parent.resizing = true;
  681. %parent.maxSize(%this);
  682. }
  683. }
  684. function DatablockEditorInspectorWindow::onMouseDragged(%this)
  685. {
  686. %parent = DatablockEditorGui;
  687. if(%parent.panelHidden == true)
  688. {
  689. %parent.showSidePanel();
  690. }
  691. if(%parent.resizing == false && %parent.docked == true)
  692. {
  693. %parent.resizing = true;
  694. %parent.maxSize(%this);
  695. }
  696. }
  697. function DatablockEditorGui::onResize(%this, %newPosition, %newExtent)
  698. {
  699. // Window to focus on (mostly the fluid window)
  700. %window = DatablockEditorInspectorWindow;
  701. if(%window.panelHidden == true)
  702. {
  703. %window.showSidePanel();
  704. }
  705. if(%this.resizing == false && %this.docked == true)
  706. {
  707. // Only resize once
  708. %this.resizing = true;
  709. %this.maxSize(%window);
  710. }
  711. }
  712. function DatablockEditorGui::dockSidePanel()
  713. {
  714. %parent = DatablockEditorGui;
  715. %fixedWindow = DatablockEditorTreeWindow;
  716. %fluidWindow = DatablockEditorInspectorWindow;
  717. if(%parent.docked == true)
  718. return;
  719. // Move and resize the window(s)
  720. %parent.resizing = true;
  721. %parent.maxSize(%fluidWindow);
  722. %parent.docked = true;
  723. %fluidWindow.onMouseDragged();
  724. // Lock the windows in place
  725. %fixedWindow.canCollapse = "0";
  726. %fixedWindow.canMove = "0";
  727. %fluidWindow.canCollapse = "0";
  728. %fluidWindow.canMove = "0";
  729. DatablockEditorGui_UnDockBtn.Visible = "1";
  730. DatablockEditorGui_DockBtn.Visible = "0";
  731. DatablockEditorGui_showBtn.Visible = "0";
  732. DatablockEditorGui_hideBtn.Visible = "1";
  733. }
  734. function DatablockEditorGui::releaseSidePanel()
  735. {
  736. %parent = DatablockEditorGui;
  737. %fixedWindow = DatablockEditorTreeWindow;
  738. %fluidWindow = DatablockEditorInspectorWindow;
  739. if(%parent.docked == false)
  740. return;
  741. // Unlock the windows so that be moved
  742. %fixedWindow.canCollapse = "1";
  743. %fixedWindow.canMove = "1";
  744. %fluidWindow.canCollapse = "1";
  745. %fluidWindow.canMove = "1";
  746. DatablockEditorGui_UnDockBtn.Visible = "0";
  747. DatablockEditorGui_DockBtn.Visible = "1";
  748. DatablockEditorGui_showBtn.Visible = "0";
  749. DatablockEditorGui_hideBtn.Visible = "0";
  750. // Let's do a small resize so it's visually clear we're undocking
  751. %position = %fixedWindow.position.x - 6 SPC %fixedWindow.position.y + 6;
  752. %extent = %fixedWindow.extent.x SPC %fixedWindow.extent.y;
  753. %fixedWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  754. %position = %fluidWindow.position.x - 6 SPC %fluidWindow.position.y + 6;
  755. %extent = %fluidWindow.extent.x SPC %fluidWindow.extent.y - 12;
  756. %fluidWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  757. %parent.docked = false;
  758. %parent.resizing = false;
  759. }
  760. function DatablockEditorGui::hideSidePanel()
  761. {
  762. %parent = DatablockEditorGui;
  763. %fixedWindow = DatablockEditorTreeWindow;
  764. %fluidWindow = DatablockEditorInspectorWindow;
  765. DatablockEditorGui_showBtn.Visible = "1";
  766. DatablockEditorGui_hideBtn.Visible = "0";
  767. // hide the content of the panels
  768. %fixedWindow.titleText = %fixedWindow.text;
  769. %fluidWindow.titleText = %fluidWindow.text;
  770. %fixedWindow.text = "";
  771. DatablockEditorTreeTabBook.Visible = "0";
  772. %fluidWindow.text = "";
  773. DatablockEditorInspectorPanel.Visible = "0";
  774. DatablockFieldInfoControl.Visible = "0";
  775. DatablockFileField.Visible = "0";
  776. // Let's do a resize so that the panel is collapsed to the side
  777. %position = Canvas.extent.x - 24 SPC %fixedWindow.position.y;
  778. %extent = %fixedWindow.extent.x SPC %fixedWindow.extent.y;
  779. %fixedWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  780. %position = Canvas.extent.x - 24 SPC %fluidWindow.position.y;
  781. %extent = %fluidWindow.extent.x SPC %fluidWindow.extent.y;
  782. %fluidWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  783. %parent.panelHidden = true;
  784. }
  785. function DatablockEditorGui::showSidePanel()
  786. {
  787. %parent = DatablockEditorGui;
  788. %fixedWindow = DatablockEditorTreeWindow;
  789. %fluidWindow = DatablockEditorInspectorWindow;
  790. DatablockEditorGui_showBtn.Visible = "0";
  791. DatablockEditorGui_hideBtn.Visible = "1";
  792. // show the content of the panels
  793. // hide the content of the panels
  794. %fixedWindow.text = %fixedWindow.titleText;
  795. DatablockEditorTreeTabBook.Visible = "1";
  796. %fluidWindow.text = %fluidWindow.titleText;
  797. DatablockEditorInspectorPanel.Visible = "1";
  798. DatablockFieldInfoControl.Visible = "1";
  799. DatablockFileField.Visible = "1";
  800. %parent.resizing = true;
  801. %parent.maxSize(%fluidWindow);
  802. %parent.panelHidden = false;
  803. }
  804. //------------------------------------------------------------------------------
  805. //=============================================================================================
  806. // Events.
  807. //=============================================================================================
  808. //---------------------------------------------------------------------------------------------
  809. function DatablockEditorInspector::onInspectorFieldModified( %this, %object, %fieldName, %arrayIndex, %oldValue, %newValue )
  810. {
  811. // Same work to do as for the regular WorldEditor Inspector.
  812. Inspector::onInspectorFieldModified( %this, %object, %fieldName, %arrayIndex, %oldValue, %newValue );
  813. DatablockEditorPlugin.flagDatablockAsDirty( %object, true );
  814. }
  815. //---------------------------------------------------------------------------------------------
  816. function DatablockEditorInspector::onFieldSelected( %this, %fieldName, %fieldTypeStr, %fieldDoc )
  817. {
  818. DatablockFieldInfoControl.setText( "<font:" @ $Gui::fontTypeBold @ ":16>" @ %fieldName @ "<font:Arial Italic:14> (" @ %fieldTypeStr @ ") " @ " : " @ "<font:Arial:14>" @ %fieldDoc );
  819. }
  820. //---------------------------------------------------------------------------------------------
  821. function DatablockEditorInspector::onBeginCompoundEdit( %this )
  822. {
  823. Editor.getUndoManager().pushCompound( "Multiple Field Edit" );
  824. }
  825. //---------------------------------------------------------------------------------------------
  826. function DatablockEditorInspector::onEndCompoundEdit( %this, %discard )
  827. {
  828. Editor.getUndoManager().popCompound( %discard );
  829. }
  830. //---------------------------------------------------------------------------------------------
  831. function DatablockEditorInspector::onClear( %this )
  832. {
  833. DatablockFieldInfoControl.setText( "" );
  834. }
  835. //---------------------------------------------------------------------------------------------
  836. function DatablockEditorTree::onDeleteSelection( %this )
  837. {
  838. %this.undoDeleteList = "";
  839. }
  840. //---------------------------------------------------------------------------------------------
  841. function DatablockEditorTree::onDeleteObject( %this, %object )
  842. {
  843. // Append it to our list.
  844. %this.undoDeleteList = %this.undoDeleteList TAB %object;
  845. // We're gonna delete this ourselves in the
  846. // completion callback.
  847. return true;
  848. }
  849. //---------------------------------------------------------------------------------------------
  850. function DatablockEditorTree::onObjectDeleteCompleted( %this )
  851. {
  852. //MEDeleteUndoAction::submit( %this.undoDeleteList );
  853. // Let the world editor know to
  854. // clear its selection.
  855. //EWorldEditor.clearSelection();
  856. //EWorldEditor.isDirty = true;
  857. }
  858. //---------------------------------------------------------------------------------------------
  859. function DatablockEditorTree::onClearSelected(%this)
  860. {
  861. DatablockEditorInspector.inspect( 0 );
  862. }
  863. //---------------------------------------------------------------------------------------------
  864. function DatablockEditorTree::onAddSelection( %this, %id )
  865. {
  866. %obj = %this.getItemValue( %id );
  867. if( !isObject( %obj ) )
  868. %this.selectItem( %id, false );
  869. else
  870. DatablockEditorPlugin.selectDatablock( %obj, true, true );
  871. }
  872. //---------------------------------------------------------------------------------------------
  873. function DatablockEditorTree::onRemoveSelection( %this, %id )
  874. {
  875. %obj = %this.getItemValue( %id );
  876. if( isObject( %obj ) )
  877. DatablockEditorPlugin.unselectDatablock( %obj, true );
  878. }
  879. //---------------------------------------------------------------------------------------------
  880. function DatablockEditorTree::onRightMouseUp( %this, %id, %mousePos )
  881. {
  882. %datablock = %this.getItemValue( %id );
  883. if( !isObject( %datablock ) )
  884. return;
  885. if( !isObject( DatablockEditorTreePopup ) )
  886. new PopupMenu( DatablockEditorTreePopup )
  887. {
  888. superClass = "MenuBuilder";
  889. isPopup = true;
  890. item[ 0 ] = "Delete" TAB "" TAB "DatablockEditorPlugin.selectDatablock( DatablockEditorTreePopup.datablockObject ); DatablockEditorPlugin.deleteDatablock( DatablockEditorTreePopup.datablockObject );";
  891. item[ 1 ] = "Jump to Definition in Torsion" TAB "" TAB "EditorOpenDeclarationInTorsion( DatablockEditorTreePopup.datablockObject );";
  892. datablockObject = "";
  893. };
  894. DatablockEditorTreePopup.datablockObject = %datablock;
  895. DatablockEditorTreePopup.showPopup( Canvas );
  896. }
  897. //---------------------------------------------------------------------------------------------
  898. function DatablockEditorTreeTabBook::onTabSelected(%this, %text, %id)
  899. {
  900. switch(%id)
  901. {
  902. case 0:
  903. DatablockEditorTreeWindow-->DeleteSelection.visible = true;
  904. DatablockEditorTreeWindow-->CreateSelection.visible = false;
  905. case 1:
  906. DatablockEditorTreeWindow-->DeleteSelection.visible = false;
  907. DatablockEditorTreeWindow-->CreateSelection.visible = true;
  908. }
  909. }