main.tscript 9.8 KB

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