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