Editor.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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();
  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 ui = Atomic.ui;
  84. ui.loadSkin("AtomicEditor/resources/default_skin/skin.tb.txt", "AtomicEditor/editor/skin/skin.tb.txt");
  85. ui.addFont("AtomicEditor/resources/vera.ttf", "Vera");
  86. ui.addFont("AtomicEditor/resources/MesloLGS-Regular.ttf", "Monaco");
  87. ui.setDefaultFont("Vera", 12);
  88. }
  89. saveWindowPreferences(data: Atomic.ScreenModeEvent): 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. handleEditorLoadProject(event: EditorEvents.LoadProjectEvent): boolean {
  109. var system = ToolCore.getToolSystem();
  110. if (system.project) {
  111. this.sendEvent(UIEvents.MessageModalEvent,
  112. { type: "error", title: "Project already loaded", message: "Project already loaded" });
  113. return false;
  114. }
  115. const loaded = system.loadProject(event.path);
  116. if (loaded) {
  117. this.sendEvent(EditorEvents.LoadProjectNotification, event);
  118. }
  119. return loaded;
  120. }
  121. closeAllResourceEditors() {
  122. var editor = EditorUI.getCurrentResourceEditor();
  123. if (!editor) {
  124. if (this.exitRequested) {
  125. this.exit();
  126. } else if (this.projectCloseRequested) {
  127. this.closeProject();
  128. }
  129. return;
  130. }
  131. //wait when we close resource editor to check another resource editor for unsaved changes and close it
  132. this.subscribeToEvent(EditorEvents.EditorResourceClose, (data) => {
  133. this.closeAllResourceEditors();
  134. });
  135. editor.requestClose();
  136. }
  137. handleEditorCloseProject(event) {
  138. this.projectCloseRequested = true;
  139. this.sendEvent(EditorEvents.ProjectUnloadedNotification, event);
  140. this.closeAllResourceEditors();
  141. }
  142. closeProject() {
  143. this.sendEvent("IPCPlayerExitRequest");
  144. var system = ToolCore.getToolSystem();
  145. if (system.project) {
  146. system.closeProject();
  147. this.sendEvent(EditorEvents.ProjectClosed);
  148. this.projectCloseRequested = false;
  149. this.unsubscribeFromEvent(EditorEvents.EditorResourceClose);
  150. }
  151. }
  152. handleProjectUnloaded(event) {
  153. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: null });
  154. }
  155. parseArguments() {
  156. var args = Atomic.getArguments();
  157. var idx = 0;
  158. while (idx < args.length) {
  159. if (args[idx] == "--project") {
  160. this.sendEvent(EditorEvents.LoadProject, { path: args[idx + 1] });
  161. }
  162. idx++;
  163. }
  164. }
  165. // event handling
  166. handleExitRequested(data) {
  167. if (this.exitRequested) return;
  168. this.sendEvent("IPCPlayerExitRequest");
  169. this.exitRequested = true;
  170. this.closeAllResourceEditors();
  171. }
  172. exit() {
  173. //Preferences.getInstance().write();
  174. EditorUI.shutdown();
  175. Atomic.getEngine().exit();
  176. }
  177. }
  178. export = Editor;