Editor.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 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. * Sets a group of user preference values in the user settings file located in the project. Elements in the
  138. * group will merge in with existing group preferences. Use this method if setting a bunch of settings
  139. * at once.
  140. * @param {string} settingsGroup name of the group the preference lives under
  141. * @param {string} groupPreferenceValues an object literal containing all of the preferences for the group.
  142. */
  143. setUserPreferenceGroup(settingsGroup: string, groupPreferenceValues: Object) {
  144. Preferences.getInstance().setUserPreferenceGroup(settingsGroup, groupPreferenceValues);
  145. WebView.WebBrowserHost.setGlobalStringProperty("HOST_Preferences", "ProjectPreferences", JSON.stringify(Preferences.getInstance().cachedProjectPreferences, null, 2 ));
  146. const eventData: Editor.UserPreferencesChangedNotificationEvent = {
  147. projectPreferences: JSON.stringify(Preferences.getInstance().cachedProjectPreferences),
  148. applicationPreferences: JSON.stringify(Preferences.getInstance().cachedApplicationPreferences)
  149. };
  150. this.sendEvent(Editor.UserPreferencesChangedNotificationEventData(eventData));
  151. }
  152. handleEditorLoadProject(event: Editor.LoadProjectNotificationEvent): boolean {
  153. var system = ToolCore.getToolSystem();
  154. if (system.project) {
  155. this.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: "Project already loaded", message: "Project already loaded" }));
  156. return false;
  157. }
  158. const loaded = system.loadProject(event.path);
  159. if (loaded) {
  160. Preferences.getInstance().loadUserPrefs();
  161. WebView.WebBrowserHost.setGlobalStringProperty("HOST_Preferences", "ProjectPreferences", JSON.stringify(Preferences.getInstance().cachedProjectPreferences, null, 2 ));
  162. WebView.WebBrowserHost.setGlobalStringProperty("HOST_Preferences", "ApplicationPreferences", JSON.stringify(Preferences.getInstance().cachedApplicationPreferences, null, 2 ));
  163. this.sendEvent(Editor.LoadProjectNotificationEventData(event));
  164. }
  165. return loaded;
  166. }
  167. closeAllResourceEditors() {
  168. var editor = EditorUI.getCurrentResourceEditor();
  169. if (!editor) {
  170. if (this.exitRequested) {
  171. this.exit();
  172. } else if (this.projectCloseRequested) {
  173. this.closeProject();
  174. }
  175. return;
  176. }
  177. //wait when we close resource editor to check another resource editor for unsaved changes and close it
  178. this.subscribeToEvent(Editor.EditorResourceCloseEvent((data) => {
  179. this.closeAllResourceEditors();
  180. }));
  181. editor.requestClose();
  182. }
  183. handleEditorCloseProject(event: Editor.ProjectUnloadedNotificationEvent) {
  184. this.projectCloseRequested = true;
  185. this.sendEvent(Editor.ProjectUnloadedNotificationEventData());
  186. this.closeAllResourceEditors();
  187. }
  188. closeProject() {
  189. this.sendEvent(EditorEvents.IPCPlayerExitRequestEventType);
  190. var system = ToolCore.getToolSystem();
  191. if (system.project) {
  192. system.closeProject();
  193. this.sendEvent(Editor.EditorProjectClosedEventType);
  194. this.projectCloseRequested = false;
  195. this.unsubscribeFromEvent(Editor.EditorResourceCloseEventType);
  196. }
  197. }
  198. handleProjectUnloaded(event) {
  199. this.sendEvent(Editor.EditorActiveSceneEditorChangeEventData({ sceneEditor: null }));
  200. }
  201. parseArguments() {
  202. var args = Atomic.getArguments();
  203. var idx = 0;
  204. while (idx < args.length) {
  205. if (args[idx] == "--project") {
  206. this.sendEvent(Editor.EditorLoadProjectEventData({ path: args[idx + 1] }));
  207. }
  208. idx++;
  209. }
  210. }
  211. // event handling
  212. handleExitRequested(data) {
  213. if (this.exitRequested) return;
  214. this.exitRequested = true;
  215. this.closeAllResourceEditors();
  216. }
  217. exit() {
  218. //Preferences.getInstance().write();
  219. EditorUI.shutdown();
  220. Atomic.getEngine().exit();
  221. }
  222. }
  223. export default AtomicEditor;