main.tscript 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 EditorGui::SetForestPalletBar()
  100. {
  101. //Clears the button pallete stack
  102. EWToolsPaletteWindow.setStackCtrl(ToolsPaletteArray); //legacy ctrl adhereance
  103. EWToolsPaletteWindow.clearButtons();
  104. EWToolsPaletteWindow.setActionMap(WorldEditorInspectorPlugin.map);
  105. //Adds a button to the pallete stack
  106. //Name Icon Click Command Tooltip text Keybind
  107. EWToolsPaletteWindow.addButton("SelectItem", "ToolsModule:arrow_n_image", "GlobalGizmoProfile.mode = \"None\"; ForestEditorGui.setActiveTool(ForestTools->SelectionTool);", "", "Select Item", "1");
  108. EWToolsPaletteWindow.addButton("MoveItem", "ToolsModule:translate_n_image", "GlobalGizmoProfile.mode = \"Move\"; ForestEditorGui.setActiveTool(ForestTools->SelectionTool);", "", "Move Item", "2");
  109. EWToolsPaletteWindow.addButton("RotateItem", "ToolsModule:rotate_n_image", "GlobalGizmoProfile.mode = \"Rotate\"; ForestEditorGui.setActiveTool(ForestTools->SelectionTool);", "", "Rotate Item", "3");
  110. EWToolsPaletteWindow.addButton("ScaleItem", "ToolsModule:scale_n_image", "GlobalGizmoProfile.mode = \"Scale\"; ForestEditorGui.setActiveTool(ForestTools->SelectionTool);", "", "Scale Item", "4");
  111. EWToolsPaletteWindow.addButton("Paint", "ToolsModule:paint_forest_btn_n_image", "ForestEditorGui.setActiveTool( ForestTools->BrushTool ); ForestTools->BrushTool.mode = \"Paint\";", "", "Paint", "5");
  112. EWToolsPaletteWindow.addButton("EraseAll", "ToolsModule:erase_all_btn_n_image", "ForestEditorGui.setActiveTool( ForestTools->BrushTool ); ForestTools->BrushTool.mode = \"Erase\";", "", "Erase All", "6");
  113. EWToolsPaletteWindow.addButton("EraseSelected", "ToolsModule:erase_element_btn_n_image", "ForestEditorGui.setActiveTool( ForestTools->BrushTool ); ForestTools->BrushTool.mode = \"EraseSelected\";", "", "Erase Selected", "7");
  114. EWToolsPaletteWindow.refresh();
  115. }
  116. function ForestEditorPlugin::onActivated( %this )
  117. {
  118. EditorGui.bringToFront( ForestEditorGui );
  119. ForestEditorGui.setVisible( true );
  120. ForestEditorPalleteWindow.setVisible( true );
  121. ForestEditorPropertiesWindow.setVisible( true );
  122. ForestEditorGui.makeFirstResponder( true );
  123. EditorGuiToolbarStack.remove( EWorldEditorToolbar );
  124. EditorGuiToolbarStack.add( ForestEditToolbar );
  125. //Get our existing forest object in our current mission if we have one
  126. %forestObject = trim(parseMissionGroupForIds("Forest", ""));
  127. if(isObject(%forestObject))
  128. {
  129. ForestEditorGui.setActiveForest(%forestObject.getName());
  130. }
  131. %this.map.push();
  132. Parent::onActivated(%this);
  133. EditorGui.SetForestPalletBar();
  134. ForestEditBrushTree.open( ForestBrushSet );
  135. ForestEditMeshTree.open( ForestItemDataSet );
  136. // Open the Brush tab.
  137. ForestEditTabBook.selectPage(0);
  138. // Sync the pallete button state
  139. // And toolbar.
  140. %tool = ForestEditorGui.getActiveTool();
  141. if ( isObject( %tool ) )
  142. %tool.onActivated();
  143. if ( !isObject( %tool ) )
  144. {
  145. ForestEditorPaintModeBtn.performClick();
  146. if ( ForestEditBrushTree.getItemCount() > 0 )
  147. {
  148. ForestEditBrushTree.selectItem( 0, true );
  149. }
  150. }
  151. else if ( %tool == ForestTools->SelectionTool )
  152. {
  153. %mode = GlobalGizmoProfile.mode;
  154. switch$ (%mode)
  155. {
  156. case "None":
  157. ForestEditorSelectModeBtn.performClick();
  158. case "Move":
  159. ForestEditorMoveModeBtn.performClick();
  160. case "Rotate":
  161. ForestEditorRotateModeBtn.performClick();
  162. case "Scale":
  163. ForestEditorScaleModeBtn.performClick();
  164. }
  165. }
  166. else if ( %tool == ForestTools->BrushTool )
  167. {
  168. %mode = ForestTools->BrushTool.mode;
  169. switch$ (%mode)
  170. {
  171. case "Paint":
  172. ForestEditorPaintModeBtn.performClick();
  173. case "Erase":
  174. ForestEditorEraseModeBtn.performClick();
  175. case "EraseSelected":
  176. ForestEditorEraseSelectedModeBtn.performClick();
  177. }
  178. }
  179. if ( %this.showError )
  180. toolsMessageBoxOK( "Error", "Your tools/forestEditor folder does not contain a valid brushes." @ $TorqueScriptFileExtension @ ". Brushes you create will not be saved!" );
  181. }
  182. function ForestEditorPlugin::onDeactivated( %this )
  183. {
  184. EditorGuiToolbarStack.add( EWorldEditorToolbar );
  185. EditorGuiToolbarStack.remove( ForestEditToolbar );
  186. ForestEditorGui.setVisible( false );
  187. ForestEditorPalleteWindow.setVisible( false );
  188. ForestEditorPropertiesWindow.setVisible( false );
  189. %tool = ForestEditorGui.getActiveTool();
  190. if ( isObject( %tool ) )
  191. %tool.onDeactivated();
  192. // Also take this opportunity to save.
  193. ForestDataManager.saveDirty();
  194. %this.saveBrushSet();
  195. %this.map.pop();
  196. Parent::onDeactivated(%this);
  197. }
  198. function ForestEditorPlugin::saveBrushSet(%this)
  199. {
  200. for(%i=0; %i < ForestBrushSet.getCount(); %i++)
  201. {
  202. %group = ForestBrushSet.getObject(%i);
  203. if(%group.isMemberOfClass("ForestBrushGroup"))
  204. {
  205. %fileName = %group.getFileName();
  206. %group.save(%group.getFileName());
  207. }
  208. }
  209. }
  210. function ForestEditorPlugin::isDirty( %this )
  211. {
  212. return %this.dirty;
  213. }
  214. function ForestEditorPlugin::clearDirty( %this )
  215. {
  216. %this.dirty = false;
  217. }
  218. function ForestEditorPlugin::onSaveMission( %this, %missionFile )
  219. {
  220. ForestDataManager.saveDirty();
  221. //First, find out if we have an existing forest object
  222. %forestObject = trim(parseMissionGroupForIds("Forest", ""));
  223. if ( isObject( %forestObject ) )
  224. {
  225. //We do. Next, see if we have a file already by polling the datafield.
  226. if(%forestObject.dataFile !$= "")
  227. {
  228. //If we do, just save to the provided file.
  229. %forestObject.saveDataFile(%forestObject.dataFile);
  230. }
  231. else
  232. {
  233. //We don't, so we'll save in the same place as the mission file and give it the missionpath\missionName.forest
  234. //naming convention.
  235. %path = filePath(%missionFile);
  236. %missionName = fileBase(%missionFile);
  237. %forestObject.saveDataFile(%path @ "/" @ %missionName @ ".forest");
  238. }
  239. }
  240. //Make sure our data is up to date too
  241. %this.saveBrushSet();
  242. }
  243. function ForestEditorPlugin::onEditorSleep( %this )
  244. {
  245. }
  246. function ForestEditorPlugin::onEditMenuSelect( %this, %editMenu )
  247. {
  248. %hasSelection = false;
  249. %selTool = ForestTools->SelectionTool;
  250. if ( ForestEditorGui.getActiveTool() == %selTool )
  251. if ( %selTool.getSelectionCount() > 0 )
  252. %hasSelection = true;
  253. %editMenu.enableItem( 3, %hasSelection ); // Cut
  254. %editMenu.enableItem( 4, %hasSelection ); // Copy
  255. %editMenu.enableItem( 5, %hasSelection ); // Paste
  256. %editMenu.enableItem( 6, %hasSelection ); // Delete
  257. %editMenu.enableItem( 8, %hasSelection ); // Deselect
  258. }
  259. function ForestEditorPlugin::handleDelete( %this )
  260. {
  261. ForestTools->SelectionTool.deleteSelection();
  262. }
  263. function ForestEditorPlugin::handleDeselect( %this )
  264. {
  265. ForestTools->SelectionTool.clearSelection();
  266. }
  267. function ForestEditorPlugin::handleCut( %this )
  268. {
  269. ForestTools->SelectionTool.cutSelection();
  270. }
  271. function ForestEditorPlugin::handleCopy( %this )
  272. {
  273. ForestTools->SelectionTool.copySelection();
  274. }
  275. function ForestEditorPlugin::handlePaste( %this )
  276. {
  277. ForestTools->SelectionTool.pasteSelection();
  278. }