MainFrameMenu.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES 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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import strings = require("../../EditorStrings");
  23. import EditorUI = require("../../EditorUI");
  24. import MenuItemSources = require("./MenuItemSources");
  25. import Preferences = require("editor/Preferences");
  26. import ServiceLocator from "../../../hostExtensions/ServiceLocator";
  27. class MainFrameMenu extends Atomic.ScriptObject {
  28. private pluginMenuItemSource: Atomic.UIMenuItemSource;
  29. constructor() {
  30. super();
  31. MenuItemSources.createMenuItemSource("menu edit", editItems);
  32. MenuItemSources.createMenuItemSource("menu file", fileItems);
  33. MenuItemSources.createMenuItemSource("menu build", buildItems);
  34. MenuItemSources.createMenuItemSource("menu tools", toolsItems);
  35. MenuItemSources.createMenuItemSource("menu developer", developerItems);
  36. MenuItemSources.createMenuItemSource("menu help", helpItems);
  37. this.goScreenshot = 0;
  38. this.subscribeToEvent(Atomic.UpdateEvent((ev) => this.handleScreenshot(ev)));
  39. }
  40. createPluginMenuItemSource(id: string, items: any): Atomic.UIMenuItemSource {
  41. if (!this.pluginMenuItemSource) {
  42. var developerMenuItemSource = MenuItemSources.getMenuItemSource("menu developer");
  43. this.pluginMenuItemSource = MenuItemSources.createSubMenuItemSource(developerMenuItemSource, "Plugins", {});
  44. }
  45. return MenuItemSources.createSubMenuItemSource(this.pluginMenuItemSource , id, items);
  46. }
  47. removePluginMenuItemSource(id: string) {
  48. if (this.pluginMenuItemSource) {
  49. this.pluginMenuItemSource.removeItemWithStr(id);
  50. if (0 == this.pluginMenuItemSource.itemCount) {
  51. var developerMenuItemSource = MenuItemSources.getMenuItemSource("menu developer");
  52. developerMenuItemSource.removeItemWithStr("Plugins");
  53. this.pluginMenuItemSource = null;
  54. }
  55. }
  56. }
  57. handleScreenshot(ev) {
  58. if ( this.goScreenshot > 0 ) {
  59. this.goScreenshot--;
  60. if ( this.goScreenshot == 0 )
  61. EditorUI.getShortcuts().invokeScreenshot();
  62. }
  63. }
  64. handlePopupMenu(target: Atomic.UIWidget, refid: string): boolean {
  65. if (target.id == "menu edit popup") {
  66. if (refid == "edit play") {
  67. EditorUI.getShortcuts().invokePlayOrStopPlayer();
  68. return true;
  69. }
  70. if (refid == "edit pause") {
  71. EditorUI.getShortcuts().invokePauseOrResumePlayer();
  72. return true;
  73. }
  74. if (refid == "edit step") {
  75. EditorUI.getShortcuts().invokeStepPausedPlayer();
  76. return true;
  77. }
  78. if (refid == "edit play debug") {
  79. EditorUI.getShortcuts().invokePlayOrStopPlayer(true);
  80. return true;
  81. }
  82. if (refid == "edit format code") {
  83. EditorUI.getShortcuts().invokeFormatCode();
  84. return true;
  85. }
  86. if (refid == "edit undo") {
  87. EditorUI.getShortcuts().invokeUndo();
  88. return true;
  89. }
  90. if (refid == "edit redo") {
  91. EditorUI.getShortcuts().invokeRedo();
  92. return true;
  93. }
  94. if (refid == "edit cut") {
  95. EditorUI.getShortcuts().invokeCut();
  96. return true;
  97. }
  98. if (refid == "edit copy") {
  99. EditorUI.getShortcuts().invokeCopy();
  100. return true;
  101. }
  102. if (refid == "edit paste") {
  103. EditorUI.getShortcuts().invokePaste();
  104. return true;
  105. }
  106. if (refid == "edit select all") {
  107. EditorUI.getShortcuts().invokeSelectAll();
  108. return true;
  109. }
  110. if (refid == "edit snap settings") {
  111. EditorUI.getModelOps().showSnapSettings();
  112. return true;
  113. }
  114. if (refid == "edit frame selected") {
  115. EditorUI.getShortcuts().invokeFrameSelected();
  116. return true;
  117. }
  118. return false;
  119. } else if (target.id == "menu file popup") {
  120. if (refid == "quit") {
  121. this.sendEvent(Atomic.ExitRequestedEventType);
  122. return true;
  123. }
  124. if (refid == "file new project") {
  125. if (ToolCore.toolSystem.project) {
  126. EditorUI.showModalError("Project Open",
  127. "Please close the current project before creating a new one");
  128. return true;
  129. }
  130. var mo = EditorUI.getModelOps();
  131. mo.showNewProject();
  132. return true;
  133. }
  134. if (refid == "file open project") {
  135. var utils = new Editor.FileUtils();
  136. var path = utils.openProjectFileDialog();
  137. if (path == "") {
  138. return true;
  139. }
  140. var requestProjectLoad = () => this.sendEvent(Editor.RequestProjectLoadEventData({ path: path }));
  141. if (ToolCore.toolSystem.project) {
  142. this.subscribeToEvent(Editor.EditorProjectClosedEvent(() => {
  143. this.unsubscribeFromEvent(Editor.EditorProjectClosedEventType);
  144. requestProjectLoad();
  145. }));
  146. this.sendEvent(Editor.EditorCloseProjectEventType);
  147. } else {
  148. requestProjectLoad();
  149. }
  150. return true;
  151. }
  152. if (refid == "file close project") {
  153. this.sendEvent(Editor.EditorCloseProjectEventType);
  154. return true;
  155. }
  156. if (refid == "file save file") {
  157. EditorUI.getShortcuts().invokeFileSave();
  158. return true;
  159. }
  160. if (refid == "file close file") {
  161. EditorUI.getShortcuts().invokeFileClose();
  162. return true;
  163. }
  164. if (refid == "file save all") {
  165. this.sendEvent(Editor.EditorSaveAllResourcesEventType);
  166. return true;
  167. }
  168. return false;
  169. } else if (target.id == "menu developer popup") {
  170. if (refid == "toggle theme") {
  171. Preferences.getInstance().toggleTheme();
  172. return true;
  173. }
  174. if (refid == "toggle codeeditor") {
  175. var ctheme = EditorUI.getEditor().getApplicationPreference( "codeEditor", "theme", "");
  176. if ( ctheme == "vs-dark" )
  177. EditorUI.getEditor().setApplicationPreference( "codeEditor", "theme", "vs");
  178. else
  179. EditorUI.getEditor().setApplicationPreference( "codeEditor", "theme", "vs-dark");
  180. return true;
  181. }
  182. if ( refid == "screenshot") {
  183. this.goScreenshot = 19; // number of ticks to wait for the menu to close
  184. return true;
  185. }
  186. if (refid == "developer show console") {
  187. Atomic.ui.showConsole(true);
  188. return true;
  189. }
  190. if (refid == "developer show uidebugger") {
  191. if (Atomic.engine.debugBuild) {
  192. Atomic.UI.debugShowSettingsWindow(EditorUI.getView());
  193. }
  194. else {
  195. EditorUI.showModalError("Debug Build Required",
  196. "UIDebugger currently requires a Debug engine build");
  197. }
  198. return true;
  199. }
  200. if (refid == "developer assetdatabase scan") {
  201. ToolCore.assetDatabase.scan();
  202. return true;
  203. }
  204. if (refid == "developer assetdatabase force") {
  205. ToolCore.assetDatabase.reimportAllAssets();
  206. return true;
  207. }
  208. //Sets all value in prefs.json to default and shuts down the editor.
  209. if (refid == "developer clear preferences") {
  210. var myPrefs = Preferences.getInstance();
  211. myPrefs.useDefaultConfig();
  212. myPrefs.saveEditorWindowData(myPrefs.editorWindow);
  213. myPrefs.savePlayerWindowData(myPrefs.playerWindow);
  214. Atomic.getEngine().exit();
  215. return true;
  216. }
  217. // If we got here, then we may have been injected by a plugin. Notify the plugins
  218. return ServiceLocator.uiServices.menuItemClicked(refid);
  219. } else if (target.id == "menu tools popup") {
  220. if (refid == "tools toggle profiler") {
  221. Atomic.ui.toggleDebugHud();
  222. return true;
  223. } if (refid == "tools perf profiler") {
  224. Atomic.ui.debugHudProfileMode = Atomic.DebugHudProfileMode.DEBUG_HUD_PROFILE_PERFORMANCE;
  225. Atomic.ui.showDebugHud(true);
  226. return true;
  227. } else if (refid == "tools metrics profiler") {
  228. Atomic.ui.debugHudProfileMode = Atomic.DebugHudProfileMode.DEBUG_HUD_PROFILE_METRICS;
  229. Atomic.ui.showDebugHud(true);
  230. return true;
  231. } else if (refid.indexOf("tools log") != -1) {
  232. let logName = refid.indexOf("editor") != -1 ? "AtomicEditor" : "AtomicPlayer";
  233. let logFolder = Atomic.fileSystem.getAppPreferencesDir(logName, "Logs");
  234. Atomic.fileSystem.systemOpen(logFolder);
  235. }
  236. } else if (target.id == "menu build popup") {
  237. if (refid == "build build") {
  238. EditorUI.getModelOps().showBuild();
  239. return true;
  240. } else if (refid == "build settings") {
  241. EditorUI.getModelOps().showBuildSettings();
  242. return true;
  243. }
  244. } else if (target.id == "menu help popup") {
  245. if (refid == "about atomic editor") {
  246. EditorUI.getModelOps().showAbout();
  247. return true;
  248. }
  249. if (refid == "help what new") {
  250. EditorUI.getModelOps().showNewBuildWindow(false);
  251. return true;
  252. }
  253. let urlLookup = {
  254. "help doc wiki" : "https://github.com/AtomicGameEngine/AtomicGameEngine/wiki/",
  255. "help chat" : "https://gitter.im/AtomicGameEngine/AtomicGameEngine/",
  256. "help api js" : "http://docs.atomicgameengine.com/api/modules/atomic.html",
  257. "help api csharp" : "http://docs.atomicgameengine.com/csharp/AtomicEngine/",
  258. "help api cplusplus" : "http://docs.atomicgameengine.com/cpp",
  259. "help support" : "https://discourse.atomicgameengine.com/",
  260. "help github" : "https://github.com/AtomicGameEngine/AtomicGameEngine/"
  261. };
  262. if (urlLookup[refid]) {
  263. Atomic.fileSystem.systemOpen(urlLookup[refid]);
  264. return true;
  265. }
  266. return false;
  267. } else {
  268. // console.log("Menu: " + target.id + " clicked");
  269. }
  270. }
  271. goScreenshot: number;
  272. }
  273. export = MainFrameMenu;
  274. // initialization
  275. var StringID = strings.StringID;
  276. var editItems = {
  277. "Undo": ["edit undo", StringID.ShortcutUndo],
  278. "Redo": ["edit redo", StringID.ShortcutRedo],
  279. "-1": null,
  280. "Cut": ["edit cut", StringID.ShortcutCut],
  281. "Copy": ["edit copy", StringID.ShortcutCopy],
  282. "Paste": ["edit paste", StringID.ShortcutPaste],
  283. "Select All": ["edit select all", StringID.ShortcutSelectAll],
  284. "-2": null,
  285. "Frame Selected": ["edit frame selected", StringID.ShortcutFrameSelected],
  286. "-3": null,
  287. "Find": ["edit find", StringID.ShortcutFind],
  288. "Find Next": ["edit find next", StringID.ShortcutFindNext],
  289. "Find Prev": ["edit find prev", StringID.ShortcutFindPrev],
  290. "-4": null,
  291. "Format Code": ["edit format code", StringID.ShortcutBeautify],
  292. "-5": null,
  293. "Play": ["edit play", StringID.ShortcutPlay],
  294. "Pause/Resume": ["edit pause", StringID.ShortcutPause],
  295. "Step": ["edit step", StringID.ShortcutStep],
  296. "Debug (C# Project)": ["edit play debug", StringID.ShortcutPlayDebug],
  297. "-6": null,
  298. "Snap Settings": ["edit snap settings"]
  299. };
  300. var toolsItems = {
  301. "Profiler": {
  302. "Toggle HUD": ["tools toggle profiler"],
  303. "Profile Performance": ["tools perf profiler"],
  304. "Profile Metrics": ["tools metrics profiler"]
  305. },
  306. "Logs": {
  307. "Player Log": ["tools log player"],
  308. "Editor Log": ["tools log editor"]
  309. }
  310. };
  311. var buildItems = {
  312. "Build": ["build build", StringID.ShortcutBuild],
  313. "Build Settings": ["build settings", StringID.ShortcutBuildSettings]
  314. };
  315. var developerItems = {
  316. "Toggle Theme": ["toggle theme"],
  317. "Toggle Code Editor Theme": ["toggle codeeditor"],
  318. "ScreenShot": ["screenshot", StringID.ShortcutScreenshot],
  319. "Show Console": ["developer show console"],
  320. "Clear Preferences": ["developer clear preferences"], //Adds clear preference to developer menu items list
  321. "Debug": {
  322. "UI Debugger": ["developer show uidebugger"],
  323. "Asset Database": {
  324. "Scan": ["developer assetdatabase scan"],
  325. "Force Reimport": ["developer assetdatabase force"]
  326. }
  327. }
  328. };
  329. var fileItems = {
  330. "New Project": ["file new project"],
  331. "Open Project": ["file open project"],
  332. "Save Project": ["file save project"],
  333. "-1": null,
  334. "Close Project": ["file close project"],
  335. "-2": null,
  336. "Save File": ["file save file", StringID.ShortcutSaveFile],
  337. "Save All Files": ["file save all"],
  338. "Close File": ["file close file", StringID.ShortcutCloseFile],
  339. "-3": null,
  340. "Quit": "quit"
  341. };
  342. var helpItems = {
  343. "Atomic Community Support": ["help support"],
  344. "Atomic Chat": ["help chat"],
  345. "-1": null,
  346. "Documentation Wiki": "help doc wiki",
  347. "API References": {
  348. "JavaScript & TypeScript": ["help api js"],
  349. "C#": ["help api csharp"],
  350. "C++": ["help api cplusplus"]
  351. },
  352. "-2": null,
  353. "Atomic Game Engine on GitHub": ["help github"],
  354. "-3": null,
  355. "What's New": "help what new",
  356. "About Atomic Editor": "about atomic editor"
  357. };