main.tscript 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. EditorGui.add( ForestEditorGui );
  34. EditorGui.add( ForestEditorPalleteWindow );
  35. EditorGui.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. %brushPath = "tools/forestEditor/brushes." @ $TorqueScriptFileExtension;
  82. if ( !isFile( %brushPath ) )
  83. %successfulFile = createPath( %brushPath );
  84. // This creates the ForestBrushGroup, all brushes, and elements.
  85. exec( %brushpath );
  86. if ( !isObject( ForestBrushGroup ) )
  87. {
  88. new SimGroup( ForestBrushGroup );
  89. %this.showError = true;
  90. }
  91. ForestEditBrushTree.open( ForestBrushGroup );
  92. if ( !isObject( ForestItemDataSet ) )
  93. new SimSet( ForestItemDataSet );
  94. ForestEditMeshTree.open( ForestItemDataSet );
  95. // Add ourselves to the window menu.
  96. %accel = EditorGui.addToEditorsMenu( "Forest Editor", "", ForestEditorPlugin );
  97. // Add ourselves to the tools menu.
  98. %tooltip = "Forest Editor (" @ %accel @ ")";
  99. EditorGui.addToToolsToolbar( "ForestEditorPlugin", "ForestEditorPalette", "ToolsModule:forest_editor_btn_n_image", %tooltip );
  100. //connect editor windows
  101. GuiWindowCtrl::attach( ForestEditorPropertiesWindow, ForestEditorPalleteWindow );
  102. ForestEditTabBook.selectPage(0);
  103. }
  104. function ForestEditorPlugin::onWorldEditorShutdown( %this )
  105. {
  106. if ( isObject( ForestBrushGroup ) )
  107. ForestBrushGroup.delete();
  108. if ( isObject( ForestDataManager ) )
  109. ForestDataManager.delete();
  110. }
  111. function ForestEditorPlugin::onActivated( %this )
  112. {
  113. EditorGui.bringToFront( ForestEditorGui );
  114. ForestEditorGui.setVisible( true );
  115. ForestEditorPalleteWindow.setVisible( true );
  116. ForestEditorPropertiesWindow.setVisible( true );
  117. ForestEditorGui.makeFirstResponder( true );
  118. EditorGuiToolbarStack.remove( EWorldEditorToolbar );
  119. EditorGuiToolbarStack.add( ForestEditToolbar );
  120. //Get our existing forest object in our current mission if we have one
  121. %forestObject = trim(parseMissionGroupForIds("Forest", ""));
  122. if(isObject(%forestObject))
  123. {
  124. ForestEditorGui.setActiveForest(%forestObject.getName());
  125. }
  126. %this.map.push();
  127. Parent::onActivated(%this);
  128. ForestEditBrushTree.open( ForestBrushGroup );
  129. ForestEditMeshTree.open( ForestItemDataSet );
  130. // Open the Brush tab.
  131. ForestEditTabBook.selectPage(0);
  132. // Sync the pallete button state
  133. // And toolbar.
  134. %tool = ForestEditorGui.getActiveTool();
  135. if ( isObject( %tool ) )
  136. %tool.onActivated();
  137. if ( !isObject( %tool ) )
  138. {
  139. ForestEditorPaintModeBtn.performClick();
  140. if ( ForestEditBrushTree.getItemCount() > 0 )
  141. {
  142. ForestEditBrushTree.selectItem( 0, true );
  143. }
  144. }
  145. else if ( %tool == ForestTools->SelectionTool )
  146. {
  147. %mode = GlobalGizmoProfile.mode;
  148. switch$ (%mode)
  149. {
  150. case "None":
  151. ForestEditorSelectModeBtn.performClick();
  152. case "Move":
  153. ForestEditorMoveModeBtn.performClick();
  154. case "Rotate":
  155. ForestEditorRotateModeBtn.performClick();
  156. case "Scale":
  157. ForestEditorScaleModeBtn.performClick();
  158. }
  159. }
  160. else if ( %tool == ForestTools->BrushTool )
  161. {
  162. %mode = ForestTools->BrushTool.mode;
  163. switch$ (%mode)
  164. {
  165. case "Paint":
  166. ForestEditorPaintModeBtn.performClick();
  167. case "Erase":
  168. ForestEditorEraseModeBtn.performClick();
  169. case "EraseSelected":
  170. ForestEditorEraseSelectedModeBtn.performClick();
  171. }
  172. }
  173. if ( %this.showError )
  174. toolsMessageBoxOK( "Error", "Your tools/forestEditor folder does not contain a valid brushes." @ $TorqueScriptFileExtension @ ". Brushes you create will not be saved!" );
  175. }
  176. function ForestEditorPlugin::onDeactivated( %this )
  177. {
  178. EditorGuiToolbarStack.add( EWorldEditorToolbar );
  179. EditorGuiToolbarStack.remove( ForestEditToolbar );
  180. ForestEditorGui.setVisible( false );
  181. ForestEditorPalleteWindow.setVisible( false );
  182. ForestEditorPropertiesWindow.setVisible( false );
  183. %tool = ForestEditorGui.getActiveTool();
  184. if ( isObject( %tool ) )
  185. %tool.onDeactivated();
  186. // Also take this opportunity to save.
  187. ForestDataManager.saveDirty();
  188. %this.map.pop();
  189. Parent::onDeactivated(%this);
  190. }
  191. function ForestEditorPlugin::isDirty( %this )
  192. {
  193. %dirty = %this.dirty || ForestEditorGui.isDirty();
  194. return %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. ForestBrushGroup.save( "tools/forestEditor/brushes." @ $TorqueScriptFileExtension );
  223. }
  224. function ForestEditorPlugin::onEditorSleep( %this )
  225. {
  226. }
  227. function ForestEditorPlugin::onEditMenuSelect( %this, %editMenu )
  228. {
  229. %hasSelection = false;
  230. %selTool = ForestTools->SelectionTool;
  231. if ( ForestEditorGui.getActiveTool() == %selTool )
  232. if ( %selTool.getSelectionCount() > 0 )
  233. %hasSelection = true;
  234. %editMenu.enableItem( 3, %hasSelection ); // Cut
  235. %editMenu.enableItem( 4, %hasSelection ); // Copy
  236. %editMenu.enableItem( 5, %hasSelection ); // Paste
  237. %editMenu.enableItem( 6, %hasSelection ); // Delete
  238. %editMenu.enableItem( 8, %hasSelection ); // Deselect
  239. }
  240. function ForestEditorPlugin::handleDelete( %this )
  241. {
  242. ForestTools->SelectionTool.deleteSelection();
  243. }
  244. function ForestEditorPlugin::handleDeselect( %this )
  245. {
  246. ForestTools->SelectionTool.clearSelection();
  247. }
  248. function ForestEditorPlugin::handleCut( %this )
  249. {
  250. ForestTools->SelectionTool.cutSelection();
  251. }
  252. function ForestEditorPlugin::handleCopy( %this )
  253. {
  254. ForestTools->SelectionTool.copySelection();
  255. }
  256. function ForestEditorPlugin::handlePaste( %this )
  257. {
  258. ForestTools->SelectionTool.pasteSelection();
  259. }