main.tscript 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. //------------------------------------------------------------------------------
  23. // Shape Editor
  24. //------------------------------------------------------------------------------
  25. function initializeShapeEditor()
  26. {
  27. echo(" % - Initializing Shape Editor");
  28. exec("./gui/Profiles.ed." @ $TorqueScriptFileExtension);
  29. exec("./gui/shapeEdPreviewWindow.ed.gui");
  30. exec("./gui/shapeEdAnimWindow.ed.gui");
  31. exec("./gui/shapeEdAdvancedWindow.ed.gui");
  32. exec("./gui/ShapeEditorToolbar.ed.gui");
  33. exec("./gui/shapeEdSelectWindow.ed.gui");
  34. exec("./gui/shapeEdPropWindow.ed.gui");
  35. exec("./scripts/shapeEditor.ed." @ $TorqueScriptFileExtension);
  36. exec("./scripts/shapeEditorHints.ed." @ $TorqueScriptFileExtension);
  37. exec("./scripts/shapeEditorActions.ed." @ $TorqueScriptFileExtension);
  38. // Add windows to editor gui
  39. ShapeEdPreviewGui.setVisible(false);
  40. ShapeEdAnimWindow.setVisible(false);
  41. ShapeEdSelectWindow.setVisible(false);
  42. ShapeEdPropWindow.setVisible(false);
  43. EditorGui.add(ShapeEdPreviewGui);
  44. EditorGui.add(ShapeEdAnimWindow);
  45. EditorGui.add(ShapeEdAdvancedWindow);
  46. EditorGui.add(ShapeEdSelectWindow);
  47. EditorGui.add(ShapeEdPropWindow);
  48. new ScriptObject(ShapeEditorPlugin)
  49. {
  50. superClass = "EditorPlugin";
  51. editorGui = ShapeEdShapeView;
  52. };
  53. %map = new ActionMap();
  54. %map.bindCmd( keyboard, "escape", "ToolsToolbarArray->WorldEditorInspectorPalette.performClick();", "" );
  55. %map.bindCmd( keyboard, "1", "ShapeEditorNoneModeBtn.performClick();", "" );
  56. %map.bindCmd( keyboard, "2", "ShapeEditorMoveModeBtn.performClick();", "" );
  57. %map.bindCmd( keyboard, "3", "ShapeEditorRotateModeBtn.performClick();", "" );
  58. //%map.bindCmd( keyboard, "4", "ShapeEditorScaleModeBtn.performClick();", "" ); // not needed for the shape editor
  59. %map.bindCmd( keyboard, "n", "ShapeEditorToolbar->showNodes.performClick();", "" );
  60. %map.bindCmd( keyboard, "t", "ShapeEditorToolbar->ghostMode.performClick();", "" );
  61. %map.bindCmd( keyboard, "r", "ShapeEditorToolbar->wireframeMode.performClick();", "" );
  62. %map.bindCmd( keyboard, "f", "ShapeEditorToolbar->fitToShapeBtn.performClick();", "" );
  63. %map.bindCmd( keyboard, "g", "ShapeEditorToolbar->showGridBtn.performClick();", "" );
  64. %map.bindCmd( keyboard, "h", "ShapeEdSelectWindow->tabBook.selectPage( 2 );", "" ); // Load help tab
  65. %map.bindCmd( keyboard, "l", "ShapeEdSelectWindow->tabBook.selectPage( 1 );", "" ); // load Library Tab
  66. %map.bindCmd( keyboard, "j", "ShapeEdSelectWindow->tabBook.selectPage( 0 );", "" ); // load scene object Tab
  67. %map.bindCmd( keyboard, "SPACE", "ShapeEdAnimWindow.togglePause();", "" );
  68. %map.bindCmd( keyboard, "i", "ShapeEdSequences.onEditSeqInOut(\"in\", ShapeEdSeqSlider.getValue());", "" );
  69. %map.bindCmd( keyboard, "o", "ShapeEdSequences.onEditSeqInOut(\"out\", ShapeEdSeqSlider.getValue());", "" );
  70. %map.bindCmd( keyboard, "shift -", "ShapeEdSeqSlider.setValue(ShapeEdAnimWindow-->seqIn.getText());", "" );
  71. %map.bindCmd( keyboard, "shift =", "ShapeEdSeqSlider.setValue(ShapeEdAnimWindow-->seqOut.getText());", "" );
  72. %map.bindCmd( keyboard, "=", "ShapeEdAnimWindow-->stepFwdBtn.performClick();", "" );
  73. %map.bindCmd( keyboard, "-", "ShapeEdAnimWindow-->stepBkwdBtn.performClick();", "" );
  74. ShapeEditorPlugin.map = %map;
  75. ShapeEditorPlugin.initSettings();
  76. }
  77. function destroyShapeEditor()
  78. {
  79. }
  80. function SetToggleButtonValue(%ctrl, %value)
  81. {
  82. if ( %ctrl.getValue() != %value )
  83. %ctrl.performClick();
  84. }
  85. // Replace the command field in an Editor PopupMenu item (returns the original value)
  86. function ShapeEditorPlugin::replaceMenuCmd(%this, %menuTitle, %id, %newCmd)
  87. {
  88. %menu = EditorGui.findMenu( %menuTitle );
  89. %cmd = getField( %menu.item[%id], 2 );
  90. %menu.setItemCommand( %id, %newCmd );
  91. return %cmd;
  92. }
  93. function ShapeEditorPlugin::onWorldEditorStartup(%this)
  94. {
  95. // Add ourselves to the window menu.
  96. %accel = EditorGui.addToEditorsMenu("Shape Editor", "", ShapeEditorPlugin);
  97. // Add ourselves to the ToolsToolbar
  98. %tooltip = "Shape Editor (" @ %accel @ ")";
  99. EditorGui.addToToolsToolbar( "ShapeEditorPlugin", "ShapeEditorPalette", "ToolsModule:shape_editor_n_image", %tooltip );
  100. // Add ourselves to the Editor Settings window
  101. exec( "./gui/ShapeEditorSettingsTab.gui" );
  102. //ESettingsWindow.addTabPage( EShapeEditorSettingsPage );
  103. GuiWindowCtrl::attach(ShapeEdPropWindow, ShapeEdSelectWindow);
  104. ShapeEdAnimWindow.resize( -1, 526, 593, 53 );
  105. // Initialise gui
  106. ShapeEdSeqNodeTabBook.selectPage(0);
  107. ShapeEdAdvancedWindow-->tabBook.selectPage(0);
  108. ShapeEdSelectWindow-->tabBook.selectPage(0);
  109. SetToggleButtonValue( ShapeEditorToolbar-->orbitNodeBtn, 0 );
  110. SetToggleButtonValue( ShapeEditorToolbar-->ghostMode, 0 );
  111. // Initialise hints menu
  112. ShapeEdHintMenu.clear();
  113. %count = ShapeHintGroup.getCount();
  114. for (%i = 0; %i < %count; %i++)
  115. {
  116. %hint = ShapeHintGroup.getObject(%i);
  117. ShapeEdHintMenu.add(%hint.objectType, %hint);
  118. }
  119. }
  120. function ShapeEditorPlugin::openShapeAsset(%this, %assetDef)
  121. {
  122. %this.selectedAssetDef = %assetDef;
  123. %this.open(%this.selectedAssetDef);
  124. }
  125. function ShapeEditorPlugin::openShapeAssetId(%this, %assetId)
  126. {
  127. %this.selectedAssetDef = AssetDatabase.acquireAsset(%assetId);
  128. //%this.selectedAssetDef = %assetDef;
  129. %this.open(%this.selectedAssetDef);
  130. }
  131. function ShapeEditorPlugin::open(%this, %shapeAsset)
  132. {
  133. if ( !%this.isActivated )
  134. {
  135. // Activate the Shape Editor
  136. EditorGui.setEditor( %this, true );
  137. // Get editor settings (note the sun angle is not configured in the settings
  138. // dialog, so apply the settings here instead of in readSettings)
  139. %this.readSettings();
  140. ShapeEdShapeView.sunAngleX = EditorSettings.value("ShapeEditor/SunAngleX");
  141. ShapeEdShapeView.sunAngleZ = EditorSettings.value("ShapeEditor/SunAngleZ");
  142. EWorldEditor.forceLoadDAE = EditorSettings.value("forceLoadDAE");
  143. $wasInWireFrameMode = $gfx::wireframe;
  144. ShapeEditorToolbar-->wireframeMode.setStateOn($gfx::wireframe);
  145. if ( GlobalGizmoProfile.getFieldValue(alignment) $= "Object" )
  146. ShapeEdNodes-->objectTransform.setStateOn(1);
  147. else
  148. ShapeEdNodes-->worldTransform.setStateOn(1);
  149. // Initialise and show the shape editor
  150. ShapeEdShapeTreeView.open(getScene(0));
  151. ShapeEdShapeTreeView.buildVisibleTree(true);
  152. ShapeEdPreviewGui.setVisible(true);
  153. ShapeEdSelectWindow.setVisible(true);
  154. ShapeEdPropWindow.setVisible(true);
  155. ShapeEdAnimWindow.setVisible(true);
  156. ShapeEdAdvancedWindow.setVisible(ShapeEditorToolbar-->showAdvanced.getValue());
  157. EditorGui.bringToFront(ShapeEdPreviewGui);
  158. ToolsPaletteArray->WorldEditorMove.performClick();
  159. %this.map.push();
  160. // Switch to the ShapeEditor UndoManager
  161. %this.oldUndoMgr = Editor.getUndoManager();
  162. Editor.setUndoManager( ShapeEdUndoManager );
  163. ShapeEdShapeView.setDisplayType( EditorGui.currentDisplayType );
  164. %this.initStatusBar();
  165. // Customise menu bar
  166. %this.oldCamFitCmd = %this.replaceMenuCmd( "Camera", 8, "ShapeEdShapeView.fitToShape();" );
  167. %this.oldCamFitOrbitCmd = %this.replaceMenuCmd( "Camera", 9, "ShapeEdShapeView.fitToShape();" );
  168. Parent::onActivated(%this);
  169. }
  170. // Select the new shape
  171. if (isObject(ShapeEditor.shape) && (ShapeEditor.shape.baseShapeAsset $= %shapeAsset))
  172. {
  173. // Shape is already selected => re-highlight the selected material if necessary
  174. ShapeEdMaterials.updateSelectedMaterial(ShapeEdMaterials-->highlightMaterial.getValue());
  175. }
  176. else if (%shapeAsset !$= "")
  177. {
  178. ShapeEditor.selectShape(%shapeAsset, ShapeEditor.isDirty());
  179. // 'fitToShape' only works after the GUI has been rendered, so force a repaint first
  180. Canvas.repaint();
  181. ShapeEdShapeView.fitToShape();
  182. }
  183. }
  184. function ShapeEditorPlugin::onActivated(%this)
  185. {
  186. %this.open("");
  187. // Try to start with the shape selected in the world editor
  188. %count = EWorldEditor.getSelectionSize();
  189. for (%i = 0; %i < %count; %i++)
  190. {
  191. %obj = EWorldEditor.getSelectedObject(%i);
  192. %shapeFile = ShapeEditor.getObjectShapeFile(%obj);
  193. if (%shapeFile !$= "" && isFile(%shapeFile))
  194. {
  195. if (!isObject(ShapeEditor.shape) || (ShapeEditor.shape.baseShape !$= %shapeFile))
  196. {
  197. // Call the 'onSelect' method directly if the object is not in the
  198. // MissionGroup tree (such as a Player or Projectile object).
  199. ShapeEdShapeTreeView.clearSelection();
  200. if (!ShapeEdShapeTreeView.selectItem(%obj))
  201. ShapeEdShapeTreeView.onSelect(%obj);
  202. // 'fitToShape' only works after the GUI has been rendered, so force a repaint first
  203. Canvas.repaint();
  204. ShapeEdShapeView.fitToShape();
  205. }
  206. break;
  207. }
  208. else if(%shapeFile !$= "")
  209. {
  210. %this.openShapeAssetId(%shapeFile);
  211. }
  212. }
  213. EditorGuiToolbarStack.remove( EWorldEditorToolbar );
  214. EditorGuiToolbarStack.add( ShapeEditorToolbar );
  215. }
  216. function ShapeEditorPlugin::initStatusBar(%this)
  217. {
  218. EditorGuiStatusBar.setInfo("Shape editor ( Shift Click ) to speed up camera.");
  219. EditorGuiStatusBar.setSelection( ShapeEditor.shape.baseShape );
  220. }
  221. function ShapeEditorPlugin::onDeactivated(%this)
  222. {
  223. %this.writeSettings();
  224. // Notify game objects if shape has been modified
  225. if ( ShapeEditor.isDirty() )
  226. ShapeEditor.shape.notifyShapeChanged();
  227. $gfx::wireframe = $wasInWireFrameMode;
  228. ShapeEdMaterials.updateSelectedMaterial(false);
  229. EditorGuiToolbarStack.add( EWorldEditorToolbar );
  230. EditorGuiToolbarStack.remove( ShapeEditorToolbar );
  231. ShapeEdPreviewGui.setVisible(false);
  232. ShapeEdSelectWindow.setVisible(false);
  233. ShapeEdPropWindow.setVisible(false);
  234. ShapeEdAnimWindow.setVisible(false);
  235. ShapeEdAdvancedWindow.setVisible(false);
  236. if( EditorGui-->MatEdPropertiesWindow.visible )
  237. {
  238. ShapeEdMaterials.editSelectedMaterialEnd( true );
  239. }
  240. %this.map.pop();
  241. // Restore the original undo manager
  242. Editor.setUndoManager( %this.oldUndoMgr );
  243. // Restore menu bar
  244. %this.replaceMenuCmd( "Camera", 8, %this.oldCamFitCmd );
  245. %this.replaceMenuCmd( "Camera", 9, %this.oldCamFitOrbitCmd );
  246. Parent::onDeactivated(%this);
  247. }
  248. function ShapeEditorPlugin::onExitMission( %this )
  249. {
  250. // unselect the current shape
  251. ShapeEdShapeView.setModel( "" );
  252. if (ShapeEditor.shape != -1)
  253. ShapeEditor.shape.delete();
  254. ShapeEditor.shape = 0;
  255. ShapeEdUndoManager.clearAll();
  256. ShapeEditor.setDirty( false );
  257. ShapeEdSequenceList.clear();
  258. ShapeEdNodeTreeView.removeItem( 0 );
  259. ShapeEdPropWindow.update_onNodeSelectionChanged( -1 );
  260. ShapeEdDetailTree.removeItem( 0 );
  261. ShapeEdMaterialList.clear();
  262. ShapeEdMountWindow-->mountList.clear();
  263. ShapeEdThreadWindow-->seqList.clear();
  264. ShapeEdThreadList.clear();
  265. }
  266. function ShapeEditorPlugin::openShape( %this, %path, %discardChangesToCurrent )
  267. {
  268. EditorGui.setEditor( ShapeEditorPlugin );
  269. if( ShapeEditor.isDirty() && !%discardChangesToCurrent )
  270. {
  271. toolsMessageBoxYesNo( "Save Changes?",
  272. "Save changes to current shape?",
  273. "ShapeEditor.saveChanges(); ShapeEditorPlugin.openShape(\"" @ %path @ "\");",
  274. "ShapeEditorPlugin.openShape(\"" @ %path @ "\");" );
  275. return;
  276. }
  277. ShapeEditor.selectShape( %path );
  278. ShapeEdShapeView.fitToShape();
  279. }
  280. function shapeEditorWireframeMode()
  281. {
  282. $gfx::wireframe = !$gfx::wireframe;
  283. ShapeEditorToolbar-->wireframeMode.setStateOn($gfx::wireframe);
  284. }
  285. //-----------------------------------------------------------------------------
  286. // Settings
  287. //-----------------------------------------------------------------------------
  288. function ShapeEditorPlugin::initSettings( %this )
  289. {
  290. EditorSettings.beginGroup( "ShapeEditor", true );
  291. // Display options
  292. EditorSettings.setDefaultValue( "BackgroundColor", "0 0 0 100" );
  293. EditorSettings.setDefaultValue( "HighlightMaterial", 1 );
  294. EditorSettings.setDefaultValue( "ShowNodes", 1 );
  295. EditorSettings.setDefaultValue( "ShowBounds", 0 );
  296. EditorSettings.setDefaultValue( "ShowObjBox", 1 );
  297. EditorSettings.setDefaultValue( "RenderMounts", 1 );
  298. EditorSettings.setDefaultValue( "RenderCollision", 0 );
  299. // Grid
  300. EditorSettings.setDefaultValue( "ShowGrid", 1 );
  301. EditorSettings.setDefaultValue( "GridSize", 0.1 );
  302. EditorSettings.setDefaultValue( "GridDimension", "40 40" );
  303. // Sun
  304. EditorSettings.setDefaultValue( "SunDiffuseColor", "255 255 255 255" );
  305. EditorSettings.setDefaultValue( "SunAmbientColor", "180 180 180 255" );
  306. EditorSettings.setDefaultValue( "SunAngleX", "45" );
  307. EditorSettings.setDefaultValue( "SunAngleZ", "135" );
  308. // Sub-windows
  309. EditorSettings.setDefaultValue( "AdvancedWndVisible", "1" );
  310. EditorSettings.endGroup();
  311. }
  312. function ShapeEditorPlugin::readSettings( %this )
  313. {
  314. EditorSettings.beginGroup( "ShapeEditor", true );
  315. // Display options
  316. ShapeEdPreviewGui-->previewBackground.color = ColorIntToFloat( EditorSettings.value("BackgroundColor") );
  317. SetToggleButtonValue( ShapeEdMaterials-->highlightMaterial, EditorSettings.value( "HighlightMaterial" ) );
  318. SetToggleButtonValue( ShapeEditorToolbar-->showNodes, EditorSettings.value( "ShowNodes" ) );
  319. SetToggleButtonValue( ShapeEditorToolbar-->showBounds, EditorSettings.value( "ShowBounds" ) );
  320. SetToggleButtonValue( ShapeEditorToolbar-->showObjBox, EditorSettings.value( "ShowObjBox" ) );
  321. SetToggleButtonValue( ShapeEditorToolbar-->renderColMeshes, EditorSettings.value( "RenderCollision" ) );
  322. SetToggleButtonValue( ShapeEdMountWindow-->renderMounts, EditorSettings.value( "RenderMounts" ) );
  323. // Grid
  324. SetToggleButtonValue( ShapeEditorToolbar-->showGridBtn, EditorSettings.value( "ShowGrid" ) );
  325. ShapeEdShapeView.gridSize = EditorSettings.value( "GridSize" );
  326. ShapeEdShapeView.gridDimension = EditorSettings.value( "GridDimension" );
  327. // Sun
  328. ShapeEdShapeView.sunDiffuse = EditorSettings.value("SunDiffuseColor");
  329. ShapeEdShapeView.sunAmbient = EditorSettings.value("SunAmbientColor");
  330. // Sub-windows
  331. SetToggleButtonValue( ShapeEditorToolbar-->showAdvanced, EditorSettings.value( "AdvancedWndVisible" ) );
  332. EditorSettings.endGroup();
  333. }
  334. function ShapeEditorPlugin::writeSettings( %this )
  335. {
  336. EditorSettings.beginGroup( "ShapeEditor", true );
  337. // Display options
  338. EditorSettings.setValue( "BackgroundColor", ColorFloatToInt( ShapeEdPreviewGui-->previewBackground.color ) );
  339. EditorSettings.setValue( "HighlightMaterial", ShapeEdMaterials-->highlightMaterial.getValue() );
  340. EditorSettings.setValue( "ShowNodes", ShapeEditorToolbar-->showNodes.getValue() );
  341. EditorSettings.setValue( "ShowBounds", ShapeEditorToolbar-->showBounds.getValue() );
  342. EditorSettings.setValue( "ShowObjBox", ShapeEditorToolbar-->showObjBox.getValue() );
  343. EditorSettings.setValue( "RenderCollision", ShapeEditorToolbar-->renderColMeshes.getValue() );
  344. EditorSettings.setValue( "RenderMounts", ShapeEdMountWindow-->renderMounts.getValue() );
  345. // Grid
  346. EditorSettings.setValue( "ShowGrid", ShapeEditorToolbar-->showGridBtn.getValue() );
  347. EditorSettings.setValue( "GridSize", ShapeEdShapeView.gridSize );
  348. EditorSettings.setValue( "GridDimension", ShapeEdShapeView.gridDimension );
  349. // Sun
  350. EditorSettings.setValue( "SunDiffuseColor", ShapeEdShapeView.sunDiffuse );
  351. EditorSettings.setValue( "SunAmbientColor", ShapeEdShapeView.sunAmbient );
  352. EditorSettings.setValue( "SunAngleX", ShapeEdShapeView.sunAngleX );
  353. EditorSettings.setValue( "SunAngleZ", ShapeEdShapeView.sunAngleZ );
  354. // Sub-windows
  355. EditorSettings.setValue( "AdvancedWndVisible", ShapeEditorToolbar-->showAdvanced.getValue() );
  356. EditorSettings.endGroup();
  357. }