MainFrameMenu.ts 16 KB

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