Editor.ts 13 KB

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