main.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. function initializeForestEditor()
  23. {
  24. echo(" % - Initializing Forest Editor");
  25. exec( "./forestEditor.cs" );
  26. exec( "./forestEditorGui.gui" );
  27. exec( "./forestEditToolbar.ed.gui" );
  28. exec( "./forestEditorGui.cs" );
  29. exec( "./tools.cs" );
  30. ForestEditorGui.setVisible( false );
  31. ForestEditorPalleteWindow.setVisible( false );
  32. ForestEditorPropertiesWindow.setVisible( false );
  33. ForestEditToolbar.setVisible( false );
  34. EditorGui.add( ForestEditorGui );
  35. EditorGui.add( ForestEditorPalleteWindow );
  36. EditorGui.add( ForestEditorPropertiesWindow );
  37. EditorGui.add( ForestEditToolbar );
  38. new ScriptObject( ForestEditorPlugin )
  39. {
  40. superClass = "EditorPlugin";
  41. editorGui = ForestEditorGui;
  42. };
  43. new SimSet(ForestTools)
  44. {
  45. new ForestBrushTool()
  46. {
  47. internalName = "BrushTool";
  48. toolTip = "Paint Tool";
  49. buttonImage = "tools/forest/images/brushTool";
  50. };
  51. new ForestSelectionTool()
  52. {
  53. internalName = "SelectionTool";
  54. toolTip = "Selection Tool";
  55. buttonImage = "tools/forest/images/selectionTool";
  56. };
  57. };
  58. %map = new ActionMap();
  59. %map.bindCmd( keyboard, "1", "ForestEditorSelectModeBtn.performClick();", "" ); // Select
  60. %map.bindCmd( keyboard, "2", "ForestEditorMoveModeBtn.performClick();", "" ); // Move
  61. %map.bindCmd( keyboard, "3", "ForestEditorRotateModeBtn.performClick();", "" ); // Rotate
  62. %map.bindCmd( keyboard, "4", "ForestEditorScaleModeBtn.performClick();", "" ); // Scale
  63. %map.bindCmd( keyboard, "5", "ForestEditorPaintModeBtn.performClick();", "" ); // Paint
  64. %map.bindCmd( keyboard, "6", "ForestEditorEraseModeBtn.performClick();", "" ); // Erase
  65. %map.bindCmd( keyboard, "7", "ForestEditorEraseSelectedModeBtn.performClick();", "" ); // EraseSelected
  66. //%map.bindCmd( keyboard, "backspace", "ForestEditorGui.onDeleteKey();", "" );
  67. //%map.bindCmd( keyboard, "delete", "ForestEditorGui.onDeleteKey();", "" );
  68. ForestEditorPlugin.map = %map;
  69. }
  70. function destroyForestEditor()
  71. {
  72. }
  73. // NOTE: debugging helper.
  74. function reinitForest()
  75. {
  76. exec( "./main.cs" );
  77. exec( "./forestEditorGui.cs" );
  78. exec( "./tools.cs" );
  79. }
  80. function ForestEditorPlugin::onWorldEditorStartup( %this )
  81. {
  82. new PersistenceManager( ForestDataManager );
  83. %brushPath = "art/forest/brushes.cs";
  84. if ( !isFile( %brushPath ) )
  85. createPath( %brushPath );
  86. // This creates the ForestBrushGroup, all brushes, and elements.
  87. exec( %brushpath );
  88. if ( !isObject( ForestBrushGroup ) )
  89. {
  90. new SimGroup( ForestBrushGroup );
  91. %this.showError = true;
  92. }
  93. ForestEditBrushTree.open( ForestBrushGroup );
  94. if ( !isObject( ForestItemDataSet ) )
  95. new SimSet( ForestItemDataSet );
  96. ForestEditMeshTree.open( ForestItemDataSet );
  97. // Add ourselves to the window menu.
  98. %accel = EditorGui.addToEditorsMenu( "Forest Editor", "", ForestEditorPlugin );
  99. // Add ourselves to the tools menu.
  100. %tooltip = "Forest Editor (" @ %accel @ ")";
  101. EditorGui.addToToolsToolbar( "ForestEditorPlugin", "ForestEditorPalette", expandFilename("tools/forestEditor/images/forest-editor-btn"), %tooltip );
  102. //connect editor windows
  103. GuiWindowCtrl::attach( ForestEditorPropertiesWindow, ForestEditorPalleteWindow );
  104. ForestEditTabBook.selectPage(0);
  105. }
  106. function ForestEditorPlugin::onWorldEditorShutdown( %this )
  107. {
  108. if ( isObject( ForestBrushGroup ) )
  109. ForestBrushGroup.delete();
  110. if ( isObject( ForestDataManager ) )
  111. ForestDataManager.delete();
  112. }
  113. function ForestEditorPlugin::onActivated( %this )
  114. {
  115. EditorGui.bringToFront( ForestEditorGui );
  116. ForestEditorGui.setVisible( true );
  117. ForestEditorPalleteWindow.setVisible( true );
  118. ForestEditorPropertiesWindow.setVisible( true );
  119. ForestEditorGui.makeFirstResponder( true );
  120. //ForestEditToolbar.setVisible( true );
  121. //Get our existing forest object in our current mission if we have one
  122. %forestObject = trim(parseMissionGroupForIds("Forest", ""));
  123. if(isObject(%forestObject))
  124. {
  125. ForestEditorGui.setActiveForest(%forestObject.getName());
  126. }
  127. %this.map.push();
  128. Parent::onActivated(%this);
  129. ForestEditBrushTree.open( ForestBrushGroup );
  130. ForestEditMeshTree.open( ForestItemDataSet );
  131. // Open the Brush tab.
  132. ForestEditTabBook.selectPage(0);
  133. // Sync the pallete button state
  134. // And toolbar.
  135. %tool = ForestEditorGui.getActiveTool();
  136. if ( isObject( %tool ) )
  137. %tool.onActivated();
  138. if ( !isObject( %tool ) )
  139. {
  140. ForestEditorPaintModeBtn.performClick();
  141. if ( ForestEditBrushTree.getItemCount() > 0 )
  142. {
  143. ForestEditBrushTree.selectItem( 0, true );
  144. }
  145. }
  146. else if ( %tool == ForestTools->SelectionTool )
  147. {
  148. %mode = GlobalGizmoProfile.mode;
  149. switch$ (%mode)
  150. {
  151. case "None":
  152. ForestEditorSelectModeBtn.performClick();
  153. case "Move":
  154. ForestEditorMoveModeBtn.performClick();
  155. case "Rotate":
  156. ForestEditorRotateModeBtn.performClick();
  157. case "Scale":
  158. ForestEditorScaleModeBtn.performClick();
  159. }
  160. }
  161. else if ( %tool == ForestTools->BrushTool )
  162. {
  163. %mode = ForestTools->BrushTool.mode;
  164. switch$ (%mode)
  165. {
  166. case "Paint":
  167. ForestEditorPaintModeBtn.performClick();
  168. case "Erase":
  169. ForestEditorEraseModeBtn.performClick();
  170. case "EraseSelected":
  171. ForestEditorEraseSelectedModeBtn.performClick();
  172. }
  173. }
  174. if ( %this.showError )
  175. MessageBoxOK( "Error", "Your art/forest folder does not contain a valid brushes.cs. Brushes you create will not be saved!" );
  176. }
  177. function ForestEditorPlugin::onDeactivated( %this )
  178. {
  179. ForestEditorGui.setVisible( false );
  180. ForestEditorPalleteWindow.setVisible( false );
  181. ForestEditorPropertiesWindow.setVisible( false );
  182. %tool = ForestEditorGui.getActiveTool();
  183. if ( isObject( %tool ) )
  184. %tool.onDeactivated();
  185. // Also take this opportunity to save.
  186. ForestDataManager.saveDirty();
  187. %this.map.pop();
  188. Parent::onDeactivated(%this);
  189. }
  190. function ForestEditorPlugin::isDirty( %this )
  191. {
  192. %dirty = %this.dirty || ForestEditorGui.isDirty();
  193. return %dirty;
  194. }
  195. function ForestEditorPlugin::clearDirty( %this )
  196. {
  197. %this.dirty = false;
  198. }
  199. function ForestEditorPlugin::onSaveMission( %this, %missionFile )
  200. {
  201. ForestDataManager.saveDirty();
  202. //First, find out if we have an existing forest object
  203. %forestObject = trim(parseMissionGroupForIds("Forest", ""));
  204. if ( isObject( %forestObject ) )
  205. {
  206. //We do. Next, see if we have a file already by polling the datafield.
  207. if(%forestObject.dataFile !$= "")
  208. {
  209. //If we do, just save to the provided file.
  210. %forestObject.saveDataFile(%forestObject.dataFile);
  211. }
  212. else
  213. {
  214. //We don't, so we'll save in the same place as the mission file and give it the missionpath\missionName.forest
  215. //naming convention.
  216. %path = filePath(%missionFile);
  217. %missionName = fileBase(%missionFile);
  218. %forestObject.saveDataFile(%path @ "/" @ %missionName @ ".forest");
  219. }
  220. }
  221. ForestBrushGroup.save( "art/forest/brushes.cs" );
  222. }
  223. function ForestEditorPlugin::onEditorSleep( %this )
  224. {
  225. }
  226. function ForestEditorPlugin::onEditMenuSelect( %this, %editMenu )
  227. {
  228. %hasSelection = false;
  229. %selTool = ForestTools->SelectionTool;
  230. if ( ForestEditorGui.getActiveTool() == %selTool )
  231. if ( %selTool.getSelectionCount() > 0 )
  232. %hasSelection = true;
  233. %editMenu.enableItem( 3, %hasSelection ); // Cut
  234. %editMenu.enableItem( 4, %hasSelection ); // Copy
  235. %editMenu.enableItem( 5, %hasSelection ); // Paste
  236. %editMenu.enableItem( 6, %hasSelection ); // Delete
  237. %editMenu.enableItem( 8, %hasSelection ); // Deselect
  238. }
  239. function ForestEditorPlugin::handleDelete( %this )
  240. {
  241. ForestTools->SelectionTool.deleteSelection();
  242. }
  243. function ForestEditorPlugin::handleDeselect( %this )
  244. {
  245. ForestTools->SelectionTool.clearSelection();
  246. }
  247. function ForestEditorPlugin::handleCut( %this )
  248. {
  249. ForestTools->SelectionTool.cutSelection();
  250. }
  251. function ForestEditorPlugin::handleCopy( %this )
  252. {
  253. ForestTools->SelectionTool.copySelection();
  254. }
  255. function ForestEditorPlugin::handlePaste( %this )
  256. {
  257. ForestTools->SelectionTool.pasteSelection();
  258. }