datablockEditor.tscript 38 KB

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