Editor.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 EditorUI = require("ui/EditorUI");
  23. import UIEvents = require("ui/UIEvents");
  24. import PlayMode = require("ui/playmode/PlayMode");
  25. import EditorLicense = require("./EditorLicense");
  26. import EditorEvents = require("./EditorEvents");
  27. import Preferences = require("./Preferences");
  28. class Editor extends Atomic.ScriptObject {
  29. project: ToolCore.Project;
  30. editorLicense: EditorLicense;
  31. playMode: PlayMode;
  32. static instance: Editor;
  33. projectCloseRequested: boolean;
  34. exitRequested: boolean;
  35. constructor() {
  36. super();
  37. // limit the framerate to limit CPU usage
  38. Atomic.getEngine().maxFps = 60;
  39. Atomic.getEngine().autoExit = false;
  40. Editor.instance = this;
  41. Preferences.getInstance().read();
  42. this.initUI();
  43. this.editorLicense = new EditorLicense();
  44. EditorUI.initialize(this);
  45. this.playMode = new PlayMode();
  46. Atomic.getResourceCache().autoReloadResources = true;
  47. this.subscribeToEvent(EditorEvents.LoadProject, (data) => this.handleEditorLoadProject(data));
  48. this.subscribeToEvent(EditorEvents.CloseProject, (data) => this.handleEditorCloseProject(data));
  49. this.subscribeToEvent(EditorEvents.ProjectUnloadedNotification, (data) => {
  50. Atomic.graphics.windowTitle = "AtomicEditor";
  51. this.handleProjectUnloaded(data);
  52. });
  53. this.subscribeToEvent("IPCPlayerWindowChanged", (data) => {
  54. var playerWindow = Preferences.getInstance().playerWindow;
  55. //if player window is maximized, then we want keep the window size from the previous state
  56. if (data.maximized) {
  57. playerWindow.x = data.posX;
  58. playerWindow.y = data.posY;
  59. playerWindow.monitor = data.monitor;
  60. playerWindow.maximized = true;
  61. } else {
  62. playerWindow = {x: data.posX, y: data.posY, width: data.width, height: data.height, monitor: data.monitor, maximized: data.maximized};
  63. }
  64. Preferences.getInstance().savePlayerWindowData(playerWindow);
  65. });
  66. this.subscribeToEvent("ScreenMode", (data:Atomic.ScreenModeEvent) => this.saveWindowPreferences(data));
  67. this.subscribeToEvent("WindowPos", (data:Atomic.ScreenModeEvent) => this.saveWindowPreferences(data));
  68. this.subscribeToEvent("ExitRequested", (data) => this.handleExitRequested(data));
  69. this.subscribeToEvent("ProjectLoaded", (data) => {
  70. Atomic.graphics.windowTitle = "AtomicEditor - " + data.projectPath;
  71. Preferences.getInstance().registerRecentProject(data.projectPath);
  72. });
  73. this.subscribeToEvent("EditorResourceCloseCanceled", (data) => {
  74. //if user canceled closing the resource, then user has changes that he doesn't want to lose
  75. //so cancel exit/project close request and unsubscribe from event to avoid closing all the editors again
  76. this.exitRequested = false;
  77. this.projectCloseRequested = false;
  78. this.unsubscribeFromEvent(EditorEvents.EditorResourceClose);
  79. });
  80. this.parseArguments();
  81. }
  82. initUI() {
  83. var uiData = Preferences.getInstance().uiData;
  84. var ui = Atomic.ui;
  85. ui.loadSkin(uiData.skinPath + "/skin.tb.txt", uiData.defaultSkinPath + "/skin.tb.txt");
  86. ui.addFont(uiData.fontFile, uiData.fontName);
  87. ui.addFont("AtomicEditor/resources/MesloLGS-Regular.ttf", "Monaco");
  88. ui.setDefaultFont(uiData.fontName, uiData.fontSize);
  89. }
  90. saveWindowPreferences(data: Atomic.ScreenModeEvent): boolean {
  91. var graphics = Atomic.getGraphics();
  92. if (!graphics) return false;
  93. var pos = graphics.getWindowPosition();
  94. var width = graphics.getWidth();
  95. var height = graphics.getHeight();
  96. var monitor = graphics.getCurrentMonitor();
  97. var editorWindowData = Preferences.getInstance().editorWindow;
  98. if (graphics.getMaximized()) {
  99. editorWindowData.x = pos[0];
  100. editorWindowData.y = pos[1];
  101. editorWindowData.maximized = true;
  102. editorWindowData.monitor = monitor;
  103. } else {
  104. editorWindowData = {x: pos[0], y: pos[1], width: width, height: height, monitor: monitor, maximized: false};
  105. }
  106. Preferences.getInstance().saveEditorWindowData(editorWindowData);
  107. return true;
  108. }
  109. /**
  110. * Return a preference value or the provided default from the user settings file
  111. * @param {string} extensionName name of the extension the preference lives under
  112. * @param {string} preferenceName name of the preference to retrieve
  113. * @param {number | boolean | string} defaultValue value to return if pref doesn't exist
  114. * @return {number|boolean|string}
  115. */
  116. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
  117. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
  118. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
  119. getUserPreference(extensionName: string, preferenceName: string, defaultValue?: any): any {
  120. return Preferences.getInstance().getUserPreference(extensionName, preferenceName, defaultValue);
  121. }
  122. /**
  123. * Sets a user preference value in the user settings file
  124. * @param {string} extensionName name of the extension the preference lives under
  125. * @param {string} preferenceName name of the preference to set
  126. * @param {number | boolean | string} value value to set
  127. */
  128. setUserPreference(extensionName: string, preferenceName: string, value: number | boolean | string) {
  129. Preferences.getInstance().setUserPreference(extensionName, preferenceName, value);
  130. WebView.WebBrowserHost.setGlobalStringProperty("HOST_Preferences", "ProjectPreferences", JSON.stringify(Preferences.getInstance().cachedProjectPreferences, null, 2 ));
  131. this.sendEvent(EditorEvents.UserPreferencesChangedNotification);
  132. }
  133. /**
  134. * Sets a group of user preference values in the user settings file located in the project. Elements in the
  135. * group will merge in with existing group preferences. Use this method if setting a bunch of settings
  136. * at once.
  137. * @param {string} settingsGroup name of the group the preference lives under
  138. * @param {string} groupPreferenceValues an object literal containing all of the preferences for the group.
  139. */
  140. setUserPreferenceGroup(settingsGroup: string, groupPreferenceValues: Object) {
  141. Preferences.getInstance().setUserPreferenceGroup(settingsGroup, groupPreferenceValues);
  142. WebView.WebBrowserHost.setGlobalStringProperty("HOST_Preferences", "ProjectPreferences", JSON.stringify(Preferences.getInstance().cachedProjectPreferences, null, 2 ));
  143. this.sendEvent(EditorEvents.UserPreferencesChangedNotification);
  144. }
  145. handleEditorLoadProject(event: EditorEvents.LoadProjectEvent): boolean {
  146. var system = ToolCore.getToolSystem();
  147. if (system.project) {
  148. this.sendEvent(UIEvents.MessageModalEvent,
  149. { type: "error", title: "Project already loaded", message: "Project already loaded" });
  150. return false;
  151. }
  152. const loaded = system.loadProject(event.path);
  153. if (loaded) {
  154. WebView.WebBrowserHost.setGlobalStringProperty("HOST_Preferences", "ProjectPreferences", JSON.stringify(Preferences.getInstance().cachedProjectPreferences, null, 2 ));
  155. WebView.WebBrowserHost.setGlobalStringProperty("HOST_Preferences", "ApplicationPreferences", JSON.stringify(Preferences.getInstance().cachedApplicationPreferences, null, 2 ));
  156. this.sendEvent(EditorEvents.LoadProjectNotification, event);
  157. }
  158. return loaded;
  159. }
  160. closeAllResourceEditors() {
  161. var editor = EditorUI.getCurrentResourceEditor();
  162. if (!editor) {
  163. if (this.exitRequested) {
  164. this.exit();
  165. } else if (this.projectCloseRequested) {
  166. this.closeProject();
  167. }
  168. return;
  169. }
  170. //wait when we close resource editor to check another resource editor for unsaved changes and close it
  171. this.subscribeToEvent(EditorEvents.EditorResourceClose, (data) => {
  172. this.closeAllResourceEditors();
  173. });
  174. editor.requestClose();
  175. }
  176. handleEditorCloseProject(event) {
  177. this.projectCloseRequested = true;
  178. this.sendEvent(EditorEvents.ProjectUnloadedNotification, event);
  179. this.closeAllResourceEditors();
  180. }
  181. closeProject() {
  182. this.sendEvent("IPCPlayerExitRequest");
  183. var system = ToolCore.getToolSystem();
  184. if (system.project) {
  185. system.closeProject();
  186. this.sendEvent(EditorEvents.ProjectClosed);
  187. this.projectCloseRequested = false;
  188. this.unsubscribeFromEvent(EditorEvents.EditorResourceClose);
  189. }
  190. }
  191. handleProjectUnloaded(event) {
  192. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: null });
  193. }
  194. parseArguments() {
  195. var args = Atomic.getArguments();
  196. var idx = 0;
  197. while (idx < args.length) {
  198. if (args[idx] == "--project") {
  199. this.sendEvent(EditorEvents.LoadProject, { path: args[idx + 1] });
  200. }
  201. idx++;
  202. }
  203. }
  204. // event handling
  205. handleExitRequested(data) {
  206. if (this.exitRequested) return;
  207. this.sendEvent("IPCPlayerExitRequest");
  208. this.exitRequested = true;
  209. this.closeAllResourceEditors();
  210. }
  211. exit() {
  212. //Preferences.getInstance().write();
  213. EditorUI.shutdown();
  214. Atomic.getEngine().exit();
  215. }
  216. }
  217. export = Editor;