MainFrameMenu.ts 15 KB

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