main.tscript 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 Daniel Buckmaster
  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. // These values should align with enum PolyFlags in walkabout/nav.h
  23. $Nav::WalkFlag = 1 << 0;
  24. $Nav::SwimFlag = 1 << 1;
  25. $Nav::JumpFlag = 1 << 2;
  26. $Nav::LedgeFlag = 1 << 3;
  27. $Nav::DropFlag = 1 << 4;
  28. $Nav::ClimbFlag = 1 << 5;
  29. $Nav::TeleportFlag = 1 << 6;
  30. function initializeNavEditor()
  31. {
  32. echo(" % - Initializing Navigation Editor");
  33. // Execute all relevant scripts and GUIs.
  34. exec("./navEditor." @ $TorqueScriptFileExtension);
  35. exec("./NavEditorGui.gui");
  36. exec("./NavEditorToolbar.gui");
  37. exec("./NavEditorConsoleDlg.gui");
  38. exec("./CreateNewNavMeshDlg.gui");
  39. // Add ourselves to EditorGui, where all the other tools reside
  40. NavEditorGui.setVisible(false);
  41. NavEditorOptionsWindow.setVisible(false);
  42. NavEditorTreeWindow.setVisible(false);
  43. NavEditorConsoleDlg.setVisible(false);
  44. MainSceneTabPanel.add(NavEditorGui);
  45. MainSceneTabPanel.add(NavEditorOptionsWindow);
  46. MainSceneTabPanel.add(NavEditorTreeWindow);
  47. MainSceneTabPanel.add(NavEditorConsoleDlg);
  48. new ScriptObject(NavEditorPlugin)
  49. {
  50. superClass = "EditorPlugin";
  51. editorGui = NavEditorGui;
  52. };
  53. // Bind shortcuts for the nav editor.
  54. %map = new ActionMap();
  55. %map.bindCmd(keyboard, "1", "ENavEditorSelectModeBtn.performClick();", "");
  56. %map.bindCmd(keyboard, "2", "ENavEditorLinkModeBtn.performClick();", "");
  57. %map.bindCmd(keyboard, "3", "ENavEditorCoverModeBtn.performClick();", "");
  58. %map.bindCmd(keyboard, "4", "ENavEditorTileModeBtn.performClick();", "");
  59. %map.bindCmd(keyboard, "5", "ENavEditorTestModeBtn.performClick();", "");
  60. %map.bindCmd(keyboard, "c", "NavEditorConsoleBtn.performClick();", "");
  61. NavEditorPlugin.map = %map;
  62. NavEditorPlugin.initSettings();
  63. }
  64. function destroyNavEditor()
  65. {
  66. }
  67. function NavEditorPlugin::onWorldEditorStartup(%this)
  68. {
  69. // Add ourselves to the window menu.
  70. %accel = EditorGui.addToEditorsMenu("Navigation Editor", "", NavEditorPlugin);
  71. // Add ourselves to the ToolsToolbar.
  72. %tooltip = "Navigation Editor (" @ %accel @ ")";
  73. EditorGui.addToToolsToolbar("NavEditorPlugin", "NavEditorPalette", "ToolsModule:nav_editor_n_image", %tooltip);
  74. GuiWindowCtrl::attach(NavEditorOptionsWindow, NavEditorTreeWindow);
  75. // Add ourselves to the Editor Settings window.
  76. exec("./NavEditorSettingsTab.gui");
  77. //ESettingsWindow.addTabPage(ENavEditorSettingsPage);
  78. ENavEditorSettingsPage.init();
  79. // Add items to World Editor Creator
  80. ObjectCreator.beginGroup("Navigation");
  81. ObjectCreator.registerMissionObject("CoverPoint", "Cover point");
  82. ObjectCreator.registerMissionObject("NavPath", "Nav Path");
  83. ObjectCreator.registerMissionObject("NavMesh", "Navigation mesh");
  84. ObjectCreator.endGroup();
  85. }
  86. function ENavEditorSettingsPage::init(%this)
  87. {
  88. // Initialises the settings controls in the settings dialog box.
  89. %this-->SpawnClassOptions.clear();
  90. %this-->SpawnClassOptions.add("AIPlayer");
  91. %this-->SpawnClassOptions.setFirstSelected();
  92. }
  93. function EditorGui::SetNavPalletBar()
  94. {
  95. //Clears the button pallete stack
  96. EWToolsPaletteWindow.setStackCtrl(ToolsPaletteArray); //legacy ctrl adhereance
  97. EWToolsPaletteWindow.clearButtons();
  98. EWToolsPaletteWindow.setActionMap(WorldEditorInspectorPlugin.map);
  99. //Adds a button to the pallete stack
  100. //Name Icon Click Command Tooltip text Keybind
  101. EWToolsPaletteWindow.addButton("ViewNavMesh", "ToolsModule:visibility_toggle_n_image", "NavEditorGui.prepSelectionMode();", "", "View NavMesh", "1");
  102. EWToolsPaletteWindow.addButton("LinkMode", "ToolsModule:nav_link_n_image", "NavEditorGui.setMode(\"LinkMode\");", "", "Create off-mesh links", "2");
  103. EWToolsPaletteWindow.addButton("CoverMode", "ToolsModule:nav_cover_n_image", "NavEditorGui.setMode(\"CoverMode\");", "", "Edit cover", "3");
  104. EWToolsPaletteWindow.addButton("TileMode", "ToolsModule:select_bounds_n_image", "NavEditorGui.setMode(\"TileMode\");", "", "View tiles", "4");
  105. EWToolsPaletteWindow.addButton("TestMode", "ToolsModule:3rd_person_camera_n_image", "NavEditorGui.setMode(\"TestMode\");", "", "Test pathfinding", "5");
  106. EWToolsPaletteWindow.refresh();
  107. }
  108. function NavEditorPlugin::onActivated(%this)
  109. {
  110. %this.readSettings();
  111. // Set a global variable so everyone knows we're editing!
  112. $Nav::EditorOpen = true;
  113. // Start off in Select mode.
  114. ToolsPaletteArray->NavEditorSelectMode.performClick();
  115. EditorGui.bringToFront(NavEditorGui);
  116. NavEditorGui.setVisible(true);
  117. NavEditorGui.makeFirstResponder(true);
  118. EditorGuiToolbarStack.remove( EWorldEditorToolbar );
  119. EditorGuiToolbarStack.add( NavEditorToolbar );
  120. NavEditorOptionsWindow.setVisible(true);
  121. NavEditorTreeWindow.setVisible(true);
  122. // Inspect the ServerNavMeshSet, which contains all the NavMesh objects
  123. // in the mission.
  124. if(!isObject(ServerNavMeshSet))
  125. new SimSet(ServerNavMeshSet);
  126. if(ServerNavMeshSet.getCount() == 0)
  127. toolsMessageBoxYesNo("No NavMesh", "There is no NavMesh in this level. Would you like to create one?" SPC
  128. "If not, please use the Nav Editor to create a new NavMesh.",
  129. "Canvas.pushDialog(CreateNewNavMeshDlg);");
  130. NavTreeView.open(ServerNavMeshSet, true);
  131. // Push our keybindings to the top. (See initializeNavEditor for where this
  132. // map was created.)
  133. %this.map.push();
  134. // Store this on a dynamic field
  135. // in order to restore whatever setting
  136. // the user had before.
  137. %this.prevGizmoAlignment = GlobalGizmoProfile.alignment;
  138. // Always use Object alignment.
  139. GlobalGizmoProfile.alignment = "Object";
  140. // Set the status until some other editing mode adds useful information.
  141. EditorGuiStatusBar.setInfo("Navigation editor.");
  142. EditorGuiStatusBar.setSelection("");
  143. // Allow the Gui to setup.
  144. NavEditorGui.onEditorActivated();
  145. Parent::onActivated(%this);
  146. EditorGui.SetNavPalletBar();
  147. }
  148. function NavEditorPlugin::onDeactivated(%this)
  149. {
  150. %this.writeSettings();
  151. $Nav::EditorOpen = false;
  152. NavEditorGui.setVisible(false);
  153. EditorGuiToolbarStack.add( EWorldEditorToolbar );
  154. EditorGuiToolbarStack.remove( NavEditorToolbar );
  155. NavEditorOptionsWindow.setVisible(false);
  156. NavEditorTreeWindow.setVisible(false);
  157. %this.map.pop();
  158. // Restore the previous Gizmo alignment settings.
  159. GlobalGizmoProfile.alignment = %this.prevGizmoAlignment;
  160. // Allow the Gui to cleanup.
  161. NavEditorGui.onEditorDeactivated();
  162. Parent::onDeactivated(%this);
  163. }
  164. function NavEditorPlugin::onEditMenuSelect(%this, %editMenu)
  165. {
  166. %hasSelection = false;
  167. }
  168. function NavEditorPlugin::handleDelete(%this)
  169. {
  170. // Event happens when the user hits 'delete'.
  171. NavEditorGui.deleteSelected();
  172. }
  173. function NavEditorPlugin::handleEscape(%this)
  174. {
  175. return NavEditorGui.onEscapePressed();
  176. }
  177. function NavEditorPlugin::isDirty(%this)
  178. {
  179. return NavEditorGui.isDirty;
  180. }
  181. function NavEditorPlugin::onSaveMission(%this, %missionFile)
  182. {
  183. if(NavEditorGui.isDirty)
  184. {
  185. getScene(0).save(%missionFile);
  186. NavEditorGui.isDirty = false;
  187. }
  188. }
  189. //-----------------------------------------------------------------------------
  190. // Settings
  191. //-----------------------------------------------------------------------------
  192. function NavEditorPlugin::initSettings(%this)
  193. {
  194. EditorSettings.beginGroup("NavEditor", true);
  195. EditorSettings.setDefaultValue("SpawnClass", "AIPlayer");
  196. EditorSettings.setDefaultValue("SpawnDatablock", "DefaultPlayerData");
  197. EditorSettings.endGroup();
  198. }
  199. function NavEditorPlugin::readSettings(%this)
  200. {
  201. EditorSettings.beginGroup("NavEditor", true);
  202. // Currently these are globals because of the way they are accessed in navMesh.cpp.
  203. $Nav::Editor::renderMesh = EditorSettings.value("RenderMesh");
  204. $Nav::Editor::renderPortals = EditorSettings.value("RenderPortals");
  205. $Nav::Editor::renderBVTree = EditorSettings.value("RenderBVTree");
  206. NavEditorGui.spawnClass = EditorSettings.value("SpawnClass");
  207. NavEditorGui.spawnDatablock = EditorSettings.value("SpawnDatablock");
  208. NavEditorGui.backgroundBuild = EditorSettings.value("BackgroundBuild");
  209. NavEditorGui.saveIntermediates = EditorSettings.value("SaveIntermediates");
  210. NavEditorGui.playSoundWhenDone = EditorSettings.value("PlaySoundWhenDone");
  211. // Build in the background by default, unless a preference has been saved.
  212. if (NavEditorGui.backgroundBuild $= "")
  213. {
  214. NavEditorGui.backgroundBuild = true;
  215. }
  216. EditorSettings.endGroup();
  217. }
  218. function NavEditorPlugin::writeSettings(%this)
  219. {
  220. EditorSettings.beginGroup("NavEditor", true);
  221. EditorSettings.setValue("RenderMesh", $Nav::Editor::renderMesh);
  222. EditorSettings.setValue("RenderPortals", $Nav::Editor::renderPortals);
  223. EditorSettings.setValue("RenderBVTree", $Nav::Editor::renderBVTree);
  224. EditorSettings.setValue("SpawnClass", NavEditorGui.spawnClass);
  225. EditorSettings.setValue("SpawnDatablock", NavEditorGui.spawnDatablock);
  226. EditorSettings.setValue("BackgroundBuild", NavEditorGui.backgroundBuild);
  227. EditorSettings.setValue("SaveIntermediates", NavEditorGui.saveIntermediates);
  228. EditorSettings.setValue("PlaySoundWhenDone", NavEditorGui.playSoundWhenDone);
  229. EditorSettings.endGroup();
  230. }
  231. function ESettingsWindowPopup::onWake(%this)
  232. {
  233. %this.setSelected(%this.findText(EditorSettings.value(%this.editorSettingsValue)));
  234. }
  235. function ESettingsWindowPopup::onSelect(%this)
  236. {
  237. EditorSettings.setValue(%this.editorSettingsValue, %this.getText());
  238. eval(%this.editorSettingsRead);
  239. }