main.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.cs");
  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. NavEditorToolbar.setVisible(false);
  42. NavEditorOptionsWindow.setVisible(false);
  43. NavEditorTreeWindow.setVisible(false);
  44. NavEditorConsoleDlg.setVisible(false);
  45. EditorGui.add(NavEditorGui);
  46. EditorGui.add(NavEditorToolbar);
  47. EditorGui.add(NavEditorOptionsWindow);
  48. EditorGui.add(NavEditorTreeWindow);
  49. EditorGui.add(NavEditorConsoleDlg);
  50. new ScriptObject(NavEditorPlugin)
  51. {
  52. superClass = "EditorPlugin";
  53. editorGui = NavEditorGui;
  54. };
  55. // Bind shortcuts for the nav editor.
  56. %map = new ActionMap();
  57. %map.bindCmd(keyboard, "1", "ENavEditorSelectModeBtn.performClick();", "");
  58. %map.bindCmd(keyboard, "2", "ENavEditorLinkModeBtn.performClick();", "");
  59. %map.bindCmd(keyboard, "3", "ENavEditorCoverModeBtn.performClick();", "");
  60. %map.bindCmd(keyboard, "4", "ENavEditorTileModeBtn.performClick();", "");
  61. %map.bindCmd(keyboard, "5", "ENavEditorTestModeBtn.performClick();", "");
  62. %map.bindCmd(keyboard, "c", "NavEditorConsoleBtn.performClick();", "");
  63. NavEditorPlugin.map = %map;
  64. NavEditorPlugin.initSettings();
  65. }
  66. function destroyNavEditor()
  67. {
  68. }
  69. function NavEditorPlugin::onWorldEditorStartup(%this)
  70. {
  71. // Add ourselves to the window menu.
  72. %accel = EditorGui.addToEditorsMenu("Navigation Editor", "", NavEditorPlugin);
  73. // Add ourselves to the ToolsToolbar.
  74. %tooltip = "Navigation Editor (" @ %accel @ ")";
  75. EditorGui.addToToolsToolbar("NavEditorPlugin", "NavEditorPalette", expandFilename("tools/navEditor/images/nav-editor"), %tooltip);
  76. GuiWindowCtrl::attach(NavEditorOptionsWindow, NavEditorTreeWindow);
  77. // Add ourselves to the Editor Settings window.
  78. exec("./NavEditorSettingsTab.gui");
  79. ESettingsWindow.addTabPage(ENavEditorSettingsPage);
  80. ENavEditorSettingsPage.init();
  81. // Add items to World Editor Creator
  82. EWCreatorWindow.beginGroup("Navigation");
  83. EWCreatorWindow.registerMissionObject("CoverPoint", "Cover point");
  84. EWCreatorWindow.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 NavEditorPlugin::onActivated(%this)
  94. {
  95. %this.readSettings();
  96. // Set a global variable so everyone knows we're editing!
  97. $Nav::EditorOpen = true;
  98. // Start off in Select mode.
  99. ToolsPaletteArray->NavEditorSelectMode.performClick();
  100. EditorGui.bringToFront(NavEditorGui);
  101. NavEditorGui.setVisible(true);
  102. NavEditorGui.makeFirstResponder(true);
  103. NavEditorToolbar.setVisible(true);
  104. NavEditorOptionsWindow.setVisible(true);
  105. NavEditorTreeWindow.setVisible(true);
  106. // Inspect the ServerNavMeshSet, which contains all the NavMesh objects
  107. // in the mission.
  108. if(!isObject(ServerNavMeshSet))
  109. new SimSet(ServerNavMeshSet);
  110. if(ServerNavMeshSet.getCount() == 0)
  111. MessageBoxYesNo("No NavMesh", "There is no NavMesh in this level. Would you like to create one?" SPC
  112. "If not, please use the Nav Editor to create a new NavMesh.",
  113. "Canvas.pushDialog(CreateNewNavMeshDlg);");
  114. NavTreeView.open(ServerNavMeshSet, true);
  115. // Push our keybindings to the top. (See initializeNavEditor for where this
  116. // map was created.)
  117. %this.map.push();
  118. // Store this on a dynamic field
  119. // in order to restore whatever setting
  120. // the user had before.
  121. %this.prevGizmoAlignment = GlobalGizmoProfile.alignment;
  122. // Always use Object alignment.
  123. GlobalGizmoProfile.alignment = "Object";
  124. // Set the status until some other editing mode adds useful information.
  125. EditorGuiStatusBar.setInfo("Navigation editor.");
  126. EditorGuiStatusBar.setSelection("");
  127. // Allow the Gui to setup.
  128. NavEditorGui.onEditorActivated();
  129. Parent::onActivated(%this);
  130. }
  131. function NavEditorPlugin::onDeactivated(%this)
  132. {
  133. %this.writeSettings();
  134. $Nav::EditorOpen = false;
  135. NavEditorGui.setVisible(false);
  136. NavEditorToolbar.setVisible(false);
  137. NavEditorOptionsWindow.setVisible(false);
  138. NavEditorTreeWindow.setVisible(false);
  139. %this.map.pop();
  140. // Restore the previous Gizmo alignment settings.
  141. GlobalGizmoProfile.alignment = %this.prevGizmoAlignment;
  142. // Allow the Gui to cleanup.
  143. NavEditorGui.onEditorDeactivated();
  144. Parent::onDeactivated(%this);
  145. }
  146. function NavEditorPlugin::onEditMenuSelect(%this, %editMenu)
  147. {
  148. %hasSelection = false;
  149. }
  150. function NavEditorPlugin::handleDelete(%this)
  151. {
  152. // Event happens when the user hits 'delete'.
  153. NavEditorGui.deleteSelected();
  154. }
  155. function NavEditorPlugin::handleEscape(%this)
  156. {
  157. return NavEditorGui.onEscapePressed();
  158. }
  159. function NavEditorPlugin::isDirty(%this)
  160. {
  161. return NavEditorGui.isDirty;
  162. }
  163. function NavEditorPlugin::onSaveMission(%this, %missionFile)
  164. {
  165. if(NavEditorGui.isDirty)
  166. {
  167. MissionGroup.save(%missionFile);
  168. NavEditorGui.isDirty = false;
  169. }
  170. }
  171. //-----------------------------------------------------------------------------
  172. // Settings
  173. //-----------------------------------------------------------------------------
  174. function NavEditorPlugin::initSettings(%this)
  175. {
  176. EditorSettings.beginGroup("NavEditor", true);
  177. EditorSettings.setDefaultValue("SpawnClass", "AIPlayer");
  178. EditorSettings.setDefaultValue("SpawnDatablock", "DefaultPlayerData");
  179. EditorSettings.endGroup();
  180. }
  181. function NavEditorPlugin::readSettings(%this)
  182. {
  183. EditorSettings.beginGroup("NavEditor", true);
  184. // Currently these are globals because of the way they are accessed in navMesh.cpp.
  185. $Nav::Editor::renderMesh = EditorSettings.value("RenderMesh");
  186. $Nav::Editor::renderPortals = EditorSettings.value("RenderPortals");
  187. $Nav::Editor::renderBVTree = EditorSettings.value("RenderBVTree");
  188. NavEditorGui.spawnClass = EditorSettings.value("SpawnClass");
  189. NavEditorGui.spawnDatablock = EditorSettings.value("SpawnDatablock");
  190. NavEditorGui.backgroundBuild = EditorSettings.value("BackgroundBuild");
  191. NavEditorGui.saveIntermediates = EditorSettings.value("SaveIntermediates");
  192. NavEditorGui.playSoundWhenDone = EditorSettings.value("PlaySoundWhenDone");
  193. // Build in the background by default, unless a preference has been saved.
  194. if (NavEditorGui.backgroundBuild $= "")
  195. {
  196. NavEditorGui.backgroundBuild = true;
  197. }
  198. EditorSettings.endGroup();
  199. }
  200. function NavEditorPlugin::writeSettings(%this)
  201. {
  202. EditorSettings.beginGroup("NavEditor", true);
  203. EditorSettings.setValue("RenderMesh", $Nav::Editor::renderMesh);
  204. EditorSettings.setValue("RenderPortals", $Nav::Editor::renderPortals);
  205. EditorSettings.setValue("RenderBVTree", $Nav::Editor::renderBVTree);
  206. EditorSettings.setValue("SpawnClass", NavEditorGui.spawnClass);
  207. EditorSettings.setValue("SpawnDatablock", NavEditorGui.spawnDatablock);
  208. EditorSettings.setValue("BackgroundBuild", NavEditorGui.backgroundBuild);
  209. EditorSettings.setValue("SaveIntermediates", NavEditorGui.saveIntermediates);
  210. EditorSettings.setValue("PlaySoundWhenDone", NavEditorGui.playSoundWhenDone);
  211. EditorSettings.endGroup();
  212. }
  213. function ESettingsWindowPopup::onWake(%this)
  214. {
  215. %this.setSelected(%this.findText(EditorSettings.value(%this.editorSettingsValue)));
  216. }
  217. function ESettingsWindowPopup::onSelect(%this)
  218. {
  219. EditorSettings.setValue(%this.editorSettingsValue, %this.getText());
  220. eval(%this.editorSettingsRead);
  221. }