main.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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::open(%this, %filename)
  124. {
  125. if ( !%this.isActivated )
  126. {
  127. // Activate the Shape Editor
  128. EditorGui.setEditor( %this, true );
  129. // Get editor settings (note the sun angle is not configured in the settings
  130. // dialog, so apply the settings here instead of in readSettings)
  131. %this.readSettings();
  132. ShapeEdShapeView.sunAngleX = EditorSettings.value("ShapeEditor/SunAngleX");
  133. ShapeEdShapeView.sunAngleZ = EditorSettings.value("ShapeEditor/SunAngleZ");
  134. EWorldEditor.forceLoadDAE = EditorSettings.value("forceLoadDAE");
  135. $wasInWireFrameMode = $gfx::wireframe;
  136. ShapeEditorToolbar-->wireframeMode.setStateOn($gfx::wireframe);
  137. if ( GlobalGizmoProfile.getFieldValue(alignment) $= "Object" )
  138. ShapeEdNodes-->objectTransform.setStateOn(1);
  139. else
  140. ShapeEdNodes-->worldTransform.setStateOn(1);
  141. // Initialise and show the shape editor
  142. ShapeEdShapeTreeView.open(MissionGroup);
  143. ShapeEdShapeTreeView.buildVisibleTree(true);
  144. ShapeEdPreviewGui.setVisible(true);
  145. ShapeEdSelectWindow.setVisible(true);
  146. ShapeEdPropWindow.setVisible(true);
  147. ShapeEdAnimWindow.setVisible(true);
  148. ShapeEdAdvancedWindow.setVisible(ShapeEditorToolbar-->showAdvanced.getValue());
  149. ShapeEditorToolbar.setVisible(true);
  150. EditorGui.bringToFront(ShapeEdPreviewGui);
  151. ToolsPaletteArray->WorldEditorMove.performClick();
  152. %this.map.push();
  153. // Switch to the ShapeEditor UndoManager
  154. %this.oldUndoMgr = Editor.getUndoManager();
  155. Editor.setUndoManager( ShapeEdUndoManager );
  156. ShapeEdShapeView.setDisplayType( EditorGui.currentDisplayType );
  157. %this.initStatusBar();
  158. // Customise menu bar
  159. %this.oldCamFitCmd = %this.replaceMenuCmd( "Camera", 8, "ShapeEdShapeView.fitToShape();" );
  160. %this.oldCamFitOrbitCmd = %this.replaceMenuCmd( "Camera", 9, "ShapeEdShapeView.fitToShape();" );
  161. Parent::onActivated(%this);
  162. }
  163. // Select the new shape
  164. if (isObject(ShapeEditor.shape) && (ShapeEditor.shape.baseShape $= %filename))
  165. {
  166. // Shape is already selected => re-highlight the selected material if necessary
  167. ShapeEdMaterials.updateSelectedMaterial(ShapeEdMaterials-->highlightMaterial.getValue());
  168. }
  169. else if (%filename !$= "")
  170. {
  171. ShapeEditor.selectShape(%filename, ShapeEditor.isDirty());
  172. // 'fitToShape' only works after the GUI has been rendered, so force a repaint first
  173. Canvas.repaint();
  174. ShapeEdShapeView.fitToShape();
  175. }
  176. }
  177. function ShapeEditorPlugin::onActivated(%this)
  178. {
  179. %this.open("");
  180. // Try to start with the shape selected in the world editor
  181. %count = EWorldEditor.getSelectionSize();
  182. for (%i = 0; %i < %count; %i++)
  183. {
  184. %obj = EWorldEditor.getSelectedObject(%i);
  185. %shapeFile = ShapeEditor.getObjectShapeFile(%obj);
  186. if (%shapeFile !$= "")
  187. {
  188. if (!isObject(ShapeEditor.shape) || (ShapeEditor.shape.baseShape !$= %shapeFile))
  189. {
  190. // Call the 'onSelect' method directly if the object is not in the
  191. // MissionGroup tree (such as a Player or Projectile object).
  192. ShapeEdShapeTreeView.clearSelection();
  193. if (!ShapeEdShapeTreeView.selectItem(%obj))
  194. ShapeEdShapeTreeView.onSelect(%obj);
  195. // 'fitToShape' only works after the GUI has been rendered, so force a repaint first
  196. Canvas.repaint();
  197. ShapeEdShapeView.fitToShape();
  198. }
  199. break;
  200. }
  201. }
  202. }
  203. function ShapeEditorPlugin::initStatusBar(%this)
  204. {
  205. EditorGuiStatusBar.setInfo("Shape editor ( Shift Click ) to speed up camera.");
  206. EditorGuiStatusBar.setSelection( ShapeEditor.shape.baseShape );
  207. }
  208. function ShapeEditorPlugin::onDeactivated(%this)
  209. {
  210. %this.writeSettings();
  211. // Notify game objects if shape has been modified
  212. if ( ShapeEditor.isDirty() )
  213. ShapeEditor.shape.notifyShapeChanged();
  214. $gfx::wireframe = $wasInWireFrameMode;
  215. ShapeEdMaterials.updateSelectedMaterial(false);
  216. ShapeEditorToolbar.setVisible(false);
  217. ShapeEdPreviewGui.setVisible(false);
  218. ShapeEdSelectWindow.setVisible(false);
  219. ShapeEdPropWindow.setVisible(false);
  220. ShapeEdAnimWindow.setVisible(false);
  221. ShapeEdAdvancedWindow.setVisible(false);
  222. if( EditorGui-->MatEdPropertiesWindow.visible )
  223. {
  224. ShapeEdMaterials.editSelectedMaterialEnd( true );
  225. }
  226. %this.map.pop();
  227. // Restore the original undo manager
  228. Editor.setUndoManager( %this.oldUndoMgr );
  229. // Restore menu bar
  230. %this.replaceMenuCmd( "Camera", 8, %this.oldCamFitCmd );
  231. %this.replaceMenuCmd( "Camera", 9, %this.oldCamFitOrbitCmd );
  232. Parent::onDeactivated(%this);
  233. }
  234. function ShapeEditorPlugin::onExitMission( %this )
  235. {
  236. // unselect the current shape
  237. ShapeEdShapeView.setModel( "" );
  238. if (ShapeEditor.shape != -1)
  239. ShapeEditor.shape.delete();
  240. ShapeEditor.shape = 0;
  241. ShapeEdUndoManager.clearAll();
  242. ShapeEditor.setDirty( false );
  243. ShapeEdSequenceList.clear();
  244. ShapeEdNodeTreeView.removeItem( 0 );
  245. ShapeEdPropWindow.update_onNodeSelectionChanged( -1 );
  246. ShapeEdDetailTree.removeItem( 0 );
  247. ShapeEdMaterialList.clear();
  248. ShapeEdMountWindow-->mountList.clear();
  249. ShapeEdThreadWindow-->seqList.clear();
  250. ShapeEdThreadList.clear();
  251. }
  252. function ShapeEditorPlugin::openShape( %this, %path, %discardChangesToCurrent )
  253. {
  254. EditorGui.setEditor( ShapeEditorPlugin );
  255. if( ShapeEditor.isDirty() && !%discardChangesToCurrent )
  256. {
  257. MessageBoxYesNo( "Save Changes?",
  258. "Save changes to current shape?",
  259. "ShapeEditor.saveChanges(); ShapeEditorPlugin.openShape(\"" @ %path @ "\");",
  260. "ShapeEditorPlugin.openShape(\"" @ %path @ "\");" );
  261. return;
  262. }
  263. ShapeEditor.selectShape( %path );
  264. ShapeEdShapeView.fitToShape();
  265. }
  266. function shapeEditorWireframeMode()
  267. {
  268. $gfx::wireframe = !$gfx::wireframe;
  269. ShapeEditorToolbar-->wireframeMode.setStateOn($gfx::wireframe);
  270. }
  271. //-----------------------------------------------------------------------------
  272. // Settings
  273. //-----------------------------------------------------------------------------
  274. function ShapeEditorPlugin::initSettings( %this )
  275. {
  276. EditorSettings.beginGroup( "ShapeEditor", true );
  277. // Display options
  278. EditorSettings.setDefaultValue( "BackgroundColor", "0 0 0 100" );
  279. EditorSettings.setDefaultValue( "HighlightMaterial", 1 );
  280. EditorSettings.setDefaultValue( "ShowNodes", 1 );
  281. EditorSettings.setDefaultValue( "ShowBounds", 0 );
  282. EditorSettings.setDefaultValue( "ShowObjBox", 1 );
  283. EditorSettings.setDefaultValue( "RenderMounts", 1 );
  284. EditorSettings.setDefaultValue( "RenderCollision", 0 );
  285. // Grid
  286. EditorSettings.setDefaultValue( "ShowGrid", 1 );
  287. EditorSettings.setDefaultValue( "GridSize", 0.1 );
  288. EditorSettings.setDefaultValue( "GridDimension", "40 40" );
  289. // Sun
  290. EditorSettings.setDefaultValue( "SunDiffuseColor", "255 255 255 255" );
  291. EditorSettings.setDefaultValue( "SunAmbientColor", "180 180 180 255" );
  292. EditorSettings.setDefaultValue( "SunAngleX", "45" );
  293. EditorSettings.setDefaultValue( "SunAngleZ", "135" );
  294. // Sub-windows
  295. EditorSettings.setDefaultValue( "AdvancedWndVisible", "1" );
  296. EditorSettings.endGroup();
  297. }
  298. function ShapeEditorPlugin::readSettings( %this )
  299. {
  300. EditorSettings.beginGroup( "ShapeEditor", true );
  301. // Display options
  302. ShapeEdPreviewGui-->previewBackground.color = ColorIntToFloat( EditorSettings.value("BackgroundColor") );
  303. SetToggleButtonValue( ShapeEdMaterials-->highlightMaterial, EditorSettings.value( "HighlightMaterial" ) );
  304. SetToggleButtonValue( ShapeEditorToolbar-->showNodes, EditorSettings.value( "ShowNodes" ) );
  305. SetToggleButtonValue( ShapeEditorToolbar-->showBounds, EditorSettings.value( "ShowBounds" ) );
  306. SetToggleButtonValue( ShapeEditorToolbar-->showObjBox, EditorSettings.value( "ShowObjBox" ) );
  307. SetToggleButtonValue( ShapeEditorToolbar-->renderColMeshes, EditorSettings.value( "RenderCollision" ) );
  308. SetToggleButtonValue( ShapeEdMountWindow-->renderMounts, EditorSettings.value( "RenderMounts" ) );
  309. // Grid
  310. SetToggleButtonValue( ShapeEditorToolbar-->showGridBtn, EditorSettings.value( "ShowGrid" ) );
  311. ShapeEdShapeView.gridSize = EditorSettings.value( "GridSize" );
  312. ShapeEdShapeView.gridDimension = EditorSettings.value( "GridDimension" );
  313. // Sun
  314. ShapeEdShapeView.sunDiffuse = EditorSettings.value("SunDiffuseColor");
  315. ShapeEdShapeView.sunAmbient = EditorSettings.value("SunAmbientColor");
  316. // Sub-windows
  317. SetToggleButtonValue( ShapeEditorToolbar-->showAdvanced, EditorSettings.value( "AdvancedWndVisible" ) );
  318. EditorSettings.endGroup();
  319. }
  320. function ShapeEditorPlugin::writeSettings( %this )
  321. {
  322. EditorSettings.beginGroup( "ShapeEditor", true );
  323. // Display options
  324. EditorSettings.setValue( "BackgroundColor", ColorFloatToInt( ShapeEdPreviewGui-->previewBackground.color ) );
  325. EditorSettings.setValue( "HighlightMaterial", ShapeEdMaterials-->highlightMaterial.getValue() );
  326. EditorSettings.setValue( "ShowNodes", ShapeEditorToolbar-->showNodes.getValue() );
  327. EditorSettings.setValue( "ShowBounds", ShapeEditorToolbar-->showBounds.getValue() );
  328. EditorSettings.setValue( "ShowObjBox", ShapeEditorToolbar-->showObjBox.getValue() );
  329. EditorSettings.setValue( "RenderCollision", ShapeEditorToolbar-->renderColMeshes.getValue() );
  330. EditorSettings.setValue( "RenderMounts", ShapeEdMountWindow-->renderMounts.getValue() );
  331. // Grid
  332. EditorSettings.setValue( "ShowGrid", ShapeEditorToolbar-->showGridBtn.getValue() );
  333. EditorSettings.setValue( "GridSize", ShapeEdShapeView.gridSize );
  334. EditorSettings.setValue( "GridDimension", ShapeEdShapeView.gridDimension );
  335. // Sun
  336. EditorSettings.setValue( "SunDiffuseColor", ShapeEdShapeView.sunDiffuse );
  337. EditorSettings.setValue( "SunAmbientColor", ShapeEdShapeView.sunAmbient );
  338. EditorSettings.setValue( "SunAngleX", ShapeEdShapeView.sunAngleX );
  339. EditorSettings.setValue( "SunAngleZ", ShapeEdShapeView.sunAngleZ );
  340. // Sub-windows
  341. EditorSettings.setValue( "AdvancedWndVisible", ShapeEditorToolbar-->showAdvanced.getValue() );
  342. EditorSettings.endGroup();
  343. }