2
0

datablockEditor.tscript 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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::createDatablock(%this)
  469. {
  470. %class = DatablockEditorTypeTree.getItemText(DatablockEditorTypeTree.getSelectedItem());
  471. if( %class !$= "" )
  472. {
  473. // Need to prompt for a name.
  474. DatablockEditorCreatePrompt-->CreateDatablockName.setText("Name");
  475. DatablockEditorCreatePrompt-->CreateDatablockName.selectAllText();
  476. // Populate the copy source dropdown.
  477. %list = DatablockEditorCreatePrompt-->CopySourceDropdown;
  478. %list.clear();
  479. %list.add( "", 0 );
  480. %set = DataBlockSet;
  481. %count = %set.getCount();
  482. for( %i = 0; %i < %count; %i ++ )
  483. {
  484. %datablock = %set.getObject( %i );
  485. %datablockClass = %datablock.getClassName();
  486. if( !isMemberOfClass( %datablockClass, %class ) )
  487. continue;
  488. %list.add( %datablock.getName(), %i + 1 );
  489. }
  490. // Set up state of client-side checkbox.
  491. %clientSideCheckBox = DatablockEditorCreatePrompt-->ClientSideCheckBox;
  492. %canBeClientSide = DatablockEditorPlugin::canBeClientSideDatablock( %class );
  493. %clientSideCheckBox.setStateOn( %canBeClientSide );
  494. %clientSideCheckBox.setActive( %canBeClientSide );
  495. // Show the dialog.
  496. canvas.pushDialog( DatablockEditorCreatePrompt, 0, true );
  497. DatablockEditorCreatePrompt-->promptWindow.text = "Create New Datablock in module: " @ DatablockEditorPlugin.targetCreationModule;
  498. }
  499. }
  500. //---------------------------------------------------------------------------------------------
  501. function DatablockEditorPlugin::pickDatablockPath(%this)
  502. {
  503. AssetBrowser_SelectModule.showDialog("DatablockEditorPlugin.pickedNewDBTargetModule");
  504. //SelectAssetPath.showDialog(AssetBrowser.dirHandler.currentAddress, "DatablockEditorPlugin.pickedNewDBPath", "Select New Datablock Path");
  505. AssetBrowser_SelectModuleWindow.selectWindow();
  506. }
  507. function DatablockEditorPlugin::pickedNewDBTargetModule(%this, %module)
  508. {
  509. DatablockEditorPlugin.targetCreationModule = %module;
  510. %moduleDef = ModuleDatabase.findModule(%module);
  511. $DATABLOCK_EDITOR_DEFAULT_FILENAME = %moduleDef.ModulePath @ "/scripts/managedData/managedDatablocks." @ $TorqueScriptFileExtension;
  512. if(!isDirectory(filePath($DATABLOCK_EDITOR_DEFAULT_FILENAME)))
  513. {
  514. AssetBrowser.dirHandler.createFolder(filePath($DATABLOCK_EDITOR_DEFAULT_FILENAME));
  515. }
  516. DatablockEditorPlugin.createDatablock();
  517. }
  518. function DatablockEditorPlugin::createPromptNameCheck(%this, %path)
  519. {
  520. %name = DatablockEditorCreatePrompt-->CreateDatablockName.getText();
  521. if( !Editor::validateObjectName( %name, true ) )
  522. return;
  523. // Fetch the copy source and clear the list.
  524. %copySource = DatablockEditorCreatePrompt-->copySourceDropdown.getText();
  525. DatablockEditorCreatePrompt-->copySourceDropdown.clear();
  526. // Remove the dialog and create the datablock.
  527. canvas.popDialog( DatablockEditorCreatePrompt );
  528. %this.createDatablockFinish( %name, %copySource );
  529. }
  530. //---------------------------------------------------------------------------------------------
  531. function DatablockEditorPlugin::createDatablockFinish( %this, %name, %copySource )
  532. {
  533. %class = DatablockEditorTypeTree.getItemText(DatablockEditorTypeTree.getSelectedItem());
  534. if( %class !$= "" )
  535. {
  536. %action = %this.createUndo( ActionCreateDatablock, "Create New Datablock" );
  537. if( DatablockEditorCreatePrompt-->ClientSideCheckBox.isStateOn() )
  538. %dbType = "singleton ";
  539. else
  540. %dbType = "datablock ";
  541. if( %copySource !$= "" )
  542. %eval = %dbType @ %class @ "(" @ %name @ " : " @ %copySource @ ") { canSaveDynamicFields = \"1\"; };";
  543. else
  544. %eval = %dbType @ %class @ "(" @ %name @ ") { canSaveDynamicFields = \"1\"; };";
  545. %res = eval( %eval );
  546. %action.db = %name.getId();
  547. %action.dbName = %name;
  548. %action.fname = $DATABLOCK_EDITOR_DEFAULT_FILENAME;
  549. %this.submitUndo( %action );
  550. %action.redo();
  551. }
  552. }
  553. //---------------------------------------------------------------------------------------------
  554. function DatablockEditorPlugin::canBeClientSideDatablock( %className )
  555. {
  556. switch$( %className )
  557. {
  558. case "SFXProfile" or
  559. "SFXPlayList" or
  560. "SFXAmbience" or
  561. "SFXEnvironment" or
  562. "SFXState" or
  563. "SFXDescription":
  564. return true;
  565. default:
  566. return false;
  567. }
  568. }
  569. //------------------------------------------------------------------------------
  570. // GUI
  571. //------------------------------------------------------------------------------
  572. //-----------------------------------------------------------------------------
  573. function DatablockEditorGui::onWake(%this)
  574. {
  575. %fixedWindow = MeshRoadEditorTreeWindow;
  576. %fluidWindow = MeshRoadEditorOptionsWindow;
  577. if(EditorSettings.value( "WorldEditor/forceSidebarToSide" ) == 1)
  578. {
  579. // Let's dock the side panel to the right side
  580. %this.docked = false;
  581. %this.resizing = true;
  582. %this.dockSidePanel();
  583. }
  584. else
  585. {
  586. // Let's release the side panel so it can be moved
  587. %this.docked = true;
  588. %this.resizing = false;
  589. %this.releaseSidePanel();
  590. }
  591. }
  592. function DatablockEditorGui::maxSize(%this, %window)
  593. {
  594. // Resize the windows to the max height
  595. // and force these to the right side if set
  596. if(EditorSettings.value( "WorldEditor/forceSidebarToSide" ) == 1 && %this.resizing == true)
  597. {
  598. // prevent onResize after a resize
  599. %this.resizing = false;
  600. %fixedWindow = DatablockEditorTreeWindow;
  601. %fluidWindow = DatablockEditorInspectorWindow;
  602. %top = EditorGuiToolbar.extent.y + 6;
  603. %bottom = %top + 65 - 6;
  604. %maxHeight = Canvas.extent.y - %top - %bottom;
  605. // --- Fixed window (top) ------------------------------------------------
  606. // put it back if it moved
  607. %fixedWindow.position.x = Canvas.extent.x - %fixedWindow.extent.x;
  608. %fixedWindow.position.y = %top;
  609. // don't go beyond the canvas
  610. if(%fixedWindow.extent.y > %maxHeight)
  611. %fixedWindow.extent.y = %maxHeight - %fluidWindow.extent.y;
  612. %position = %fixedWindow.position.x SPC %fixedWindow.position.y;
  613. %extent = %window.extent.x SPC %fixedWindow.extent.y;
  614. %fixedWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  615. // --- Fluid window (bottom) ---------------------------------------------
  616. // position is relative to the top window
  617. %position = %fixedWindow.position.x SPC %fixedWindow.extent.y + %top;
  618. %extent = %window.extent.x SPC Canvas.extent.y - %fixedWindow.extent.y - %bottom;
  619. %fluidWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  620. // --- AssetBrowser window ----------------------------------------------
  621. if(isObject(AssetBrowserWindow))
  622. {
  623. // Only resize the AssetBrowser if it's docked
  624. if(AssetBrowserWindow.docked == true)
  625. {
  626. // The width is relative to the sidepanel
  627. %browserWidth = Canvas.extent.x - %extent.x;
  628. %browserHeight = AssetBrowserWindow.extent.y;
  629. %browserPosY = Canvas.extent.y - AssetBrowserWindow.extent.y - 33;
  630. AssetBrowserWindow.resize(0, %browserPosY, %browserWidth, %browserHeight);
  631. }
  632. }
  633. // --- Windowed Console --------------------------------------------------
  634. if(isObject(windowConsoleControl))
  635. {
  636. // Only resize the AssetBrowser if it's docked
  637. if(windowConsoleControl.docked == true)
  638. {
  639. // The width is relative to the sidepanel
  640. %consoleWidth = Canvas.extent.x - %extent.x;
  641. %consoleHeight = windowConsoleControl.extent.y;
  642. %consolePosY = Canvas.extent.y - windowConsoleControl.extent.y - 33;
  643. windowConsoleControl.resize(0, %consolePosY, %consoleWidth, %consoleHeight);
  644. }
  645. }
  646. }
  647. }
  648. function DatablockEditorTreeWindow::onMouseDragged(%this)
  649. {
  650. %parent = DatablockEditorGui;
  651. if(%parent.panelHidden == true)
  652. {
  653. %parent.showSidePanel();
  654. }
  655. if(%parent.resizing == false && %parent.docked == true)
  656. {
  657. %parent.resizing = true;
  658. %parent.maxSize(%this);
  659. }
  660. }
  661. function DatablockEditorInspectorWindow::onMouseDragged(%this)
  662. {
  663. %parent = DatablockEditorGui;
  664. if(%parent.panelHidden == true)
  665. {
  666. %parent.showSidePanel();
  667. }
  668. if(%parent.resizing == false && %parent.docked == true)
  669. {
  670. %parent.resizing = true;
  671. %parent.maxSize(%this);
  672. }
  673. }
  674. function DatablockEditorGui::onResize(%this, %newPosition, %newExtent)
  675. {
  676. // Window to focus on (mostly the fluid window)
  677. %window = DatablockEditorInspectorWindow;
  678. if(%window.panelHidden == true)
  679. {
  680. %window.showSidePanel();
  681. }
  682. if(%this.resizing == false && %this.docked == true)
  683. {
  684. // Only resize once
  685. %this.resizing = true;
  686. %this.maxSize(%window);
  687. }
  688. }
  689. function DatablockEditorGui::dockSidePanel()
  690. {
  691. %parent = DatablockEditorGui;
  692. %fixedWindow = DatablockEditorTreeWindow;
  693. %fluidWindow = DatablockEditorInspectorWindow;
  694. if(%parent.docked == true)
  695. return;
  696. // Move and resize the window(s)
  697. %parent.resizing = true;
  698. %parent.maxSize(%fluidWindow);
  699. %parent.docked = true;
  700. %fluidWindow.onMouseDragged();
  701. // Lock the windows in place
  702. %fixedWindow.canCollapse = "0";
  703. %fixedWindow.canMove = "0";
  704. %fluidWindow.canCollapse = "0";
  705. %fluidWindow.canMove = "0";
  706. DatablockEditorGui_UnDockBtn.Visible = "1";
  707. DatablockEditorGui_DockBtn.Visible = "0";
  708. DatablockEditorGui_showBtn.Visible = "0";
  709. DatablockEditorGui_hideBtn.Visible = "1";
  710. }
  711. function DatablockEditorGui::releaseSidePanel()
  712. {
  713. %parent = DatablockEditorGui;
  714. %fixedWindow = DatablockEditorTreeWindow;
  715. %fluidWindow = DatablockEditorInspectorWindow;
  716. if(%parent.docked == false)
  717. return;
  718. // Unlock the windows so that be moved
  719. %fixedWindow.canCollapse = "1";
  720. %fixedWindow.canMove = "1";
  721. %fluidWindow.canCollapse = "1";
  722. %fluidWindow.canMove = "1";
  723. DatablockEditorGui_UnDockBtn.Visible = "0";
  724. DatablockEditorGui_DockBtn.Visible = "1";
  725. DatablockEditorGui_showBtn.Visible = "0";
  726. DatablockEditorGui_hideBtn.Visible = "0";
  727. // Let's do a small resize so it's visually clear we're undocking
  728. %position = %fixedWindow.position.x - 6 SPC %fixedWindow.position.y + 6;
  729. %extent = %fixedWindow.extent.x SPC %fixedWindow.extent.y;
  730. %fixedWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  731. %position = %fluidWindow.position.x - 6 SPC %fluidWindow.position.y + 6;
  732. %extent = %fluidWindow.extent.x SPC %fluidWindow.extent.y - 12;
  733. %fluidWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  734. %parent.docked = false;
  735. %parent.resizing = false;
  736. }
  737. function DatablockEditorGui::hideSidePanel()
  738. {
  739. %parent = DatablockEditorGui;
  740. %fixedWindow = DatablockEditorTreeWindow;
  741. %fluidWindow = DatablockEditorInspectorWindow;
  742. DatablockEditorGui_showBtn.Visible = "1";
  743. DatablockEditorGui_hideBtn.Visible = "0";
  744. // hide the content of the panels
  745. %fixedWindow.titleText = %fixedWindow.text;
  746. %fluidWindow.titleText = %fluidWindow.text;
  747. %fixedWindow.text = "";
  748. DatablockEditorTreeTabBook.Visible = "0";
  749. %fluidWindow.text = "";
  750. DatablockEditorInspectorPanel.Visible = "0";
  751. DatablockFieldInfoControl.Visible = "0";
  752. DatablockFileField.Visible = "0";
  753. // Let's do a resize so that the panel is collapsed to the side
  754. %position = Canvas.extent.x - 24 SPC %fixedWindow.position.y;
  755. %extent = %fixedWindow.extent.x SPC %fixedWindow.extent.y;
  756. %fixedWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  757. %position = Canvas.extent.x - 24 SPC %fluidWindow.position.y;
  758. %extent = %fluidWindow.extent.x SPC %fluidWindow.extent.y;
  759. %fluidWindow.resize(%position.x, %position.y, %extent.x, %extent.y);
  760. %parent.panelHidden = true;
  761. }
  762. function DatablockEditorGui::showSidePanel()
  763. {
  764. %parent = DatablockEditorGui;
  765. %fixedWindow = DatablockEditorTreeWindow;
  766. %fluidWindow = DatablockEditorInspectorWindow;
  767. DatablockEditorGui_showBtn.Visible = "0";
  768. DatablockEditorGui_hideBtn.Visible = "1";
  769. // show the content of the panels
  770. // hide the content of the panels
  771. %fixedWindow.text = %fixedWindow.titleText;
  772. DatablockEditorTreeTabBook.Visible = "1";
  773. %fluidWindow.text = %fluidWindow.titleText;
  774. DatablockEditorInspectorPanel.Visible = "1";
  775. DatablockFieldInfoControl.Visible = "1";
  776. DatablockFileField.Visible = "1";
  777. %parent.resizing = true;
  778. %parent.maxSize(%fluidWindow);
  779. %parent.panelHidden = false;
  780. }
  781. //------------------------------------------------------------------------------
  782. //=============================================================================================
  783. // Events.
  784. //=============================================================================================
  785. //---------------------------------------------------------------------------------------------
  786. function DatablockEditorInspector::onInspectorFieldModified( %this, %object, %fieldName, %arrayIndex, %oldValue, %newValue )
  787. {
  788. // Same work to do as for the regular WorldEditor Inspector.
  789. Inspector::onInspectorFieldModified( %this, %object, %fieldName, %arrayIndex, %oldValue, %newValue );
  790. DatablockEditorPlugin.flagDatablockAsDirty( %object, true );
  791. }
  792. //---------------------------------------------------------------------------------------------
  793. function DatablockEditorInspector::onFieldSelected( %this, %fieldName, %fieldTypeStr, %fieldDoc )
  794. {
  795. DatablockFieldInfoControl.setText( "<font:" @ $Gui::fontTypeBold @ ":16>" @ %fieldName @ "<font:Arial Italic:14> (" @ %fieldTypeStr @ ") " @ " : " @ "<font:Arial:14>" @ %fieldDoc );
  796. }
  797. //---------------------------------------------------------------------------------------------
  798. function DatablockEditorInspector::onBeginCompoundEdit( %this )
  799. {
  800. Editor.getUndoManager().pushCompound( "Multiple Field Edit" );
  801. }
  802. //---------------------------------------------------------------------------------------------
  803. function DatablockEditorInspector::onEndCompoundEdit( %this, %discard )
  804. {
  805. Editor.getUndoManager().popCompound( %discard );
  806. }
  807. //---------------------------------------------------------------------------------------------
  808. function DatablockEditorInspector::onClear( %this )
  809. {
  810. DatablockFieldInfoControl.setText( "" );
  811. }
  812. //---------------------------------------------------------------------------------------------
  813. function DatablockEditorTree::onDeleteSelection( %this )
  814. {
  815. %this.undoDeleteList = "";
  816. }
  817. //---------------------------------------------------------------------------------------------
  818. function DatablockEditorTree::onDeleteObject( %this, %object )
  819. {
  820. // Append it to our list.
  821. %this.undoDeleteList = %this.undoDeleteList TAB %object;
  822. // We're gonna delete this ourselves in the
  823. // completion callback.
  824. return true;
  825. }
  826. //---------------------------------------------------------------------------------------------
  827. function DatablockEditorTree::onObjectDeleteCompleted( %this )
  828. {
  829. //MEDeleteUndoAction::submit( %this.undoDeleteList );
  830. // Let the world editor know to
  831. // clear its selection.
  832. //EWorldEditor.clearSelection();
  833. //EWorldEditor.isDirty = true;
  834. }
  835. //---------------------------------------------------------------------------------------------
  836. function DatablockEditorTree::onClearSelected(%this)
  837. {
  838. DatablockEditorInspector.inspect( 0 );
  839. }
  840. //---------------------------------------------------------------------------------------------
  841. function DatablockEditorTree::onAddSelection( %this, %id )
  842. {
  843. %obj = %this.getItemValue( %id );
  844. if( !isObject( %obj ) )
  845. %this.selectItem( %id, false );
  846. else
  847. DatablockEditorPlugin.selectDatablock( %obj, true, true );
  848. }
  849. //---------------------------------------------------------------------------------------------
  850. function DatablockEditorTree::onRemoveSelection( %this, %id )
  851. {
  852. %obj = %this.getItemValue( %id );
  853. if( isObject( %obj ) )
  854. DatablockEditorPlugin.unselectDatablock( %obj, true );
  855. }
  856. //---------------------------------------------------------------------------------------------
  857. function DatablockEditorTree::onRightMouseUp( %this, %id, %mousePos )
  858. {
  859. %datablock = %this.getItemValue( %id );
  860. if( !isObject( %datablock ) )
  861. return;
  862. if( !isObject( DatablockEditorTreePopup ) )
  863. new PopupMenu( DatablockEditorTreePopup )
  864. {
  865. superClass = "MenuBuilder";
  866. isPopup = true;
  867. item[ 0 ] = "Delete" TAB "" TAB "DatablockEditorPlugin.selectDatablock( DatablockEditorTreePopup.datablockObject ); DatablockEditorPlugin.deleteDatablock( DatablockEditorTreePopup.datablockObject );";
  868. item[ 1 ] = "Jump to Definition in Torsion" TAB "" TAB "EditorOpenDeclarationInTorsion( DatablockEditorTreePopup.datablockObject );";
  869. datablockObject = "";
  870. };
  871. DatablockEditorTreePopup.datablockObject = %datablock;
  872. DatablockEditorTreePopup.showPopup( Canvas );
  873. }
  874. //---------------------------------------------------------------------------------------------
  875. function DatablockEditorTreeTabBook::onTabSelected(%this, %text, %id)
  876. {
  877. switch(%id)
  878. {
  879. case 0:
  880. DatablockEditorTreeWindow-->DeleteSelection.visible = true;
  881. DatablockEditorTreeWindow-->CreateSelection.visible = false;
  882. case 1:
  883. DatablockEditorTreeWindow-->DeleteSelection.visible = false;
  884. DatablockEditorTreeWindow-->CreateSelection.visible = true;
  885. }
  886. }