Editor.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. import EditorUI = require("ui/EditorUI");
  8. import UIEvents = require("ui/UIEvents");
  9. import PlayMode = require("ui/playmode/PlayMode");
  10. import EditorLicense = require("./EditorLicense");
  11. import EditorEvents = require("./EditorEvents");
  12. import Preferences = require("./Preferences");
  13. class Editor extends Atomic.ScriptObject {
  14. project: ToolCore.Project;
  15. editorLicense: EditorLicense;
  16. playMode: PlayMode;
  17. static instance: Editor;
  18. projectCloseRequested: boolean;
  19. exitRequested: boolean;
  20. constructor() {
  21. super();
  22. // limit the framerate to limit CPU usage
  23. Atomic.getEngine().maxFps = 60;
  24. Atomic.getEngine().autoExit = false;
  25. Editor.instance = this;
  26. Preferences.getInstance().read();
  27. var editorWindow = Preferences.getInstance().editorWindow;
  28. if (editorWindow) {
  29. Atomic.graphics.setWindowSize(editorWindow.width, editorWindow.height);
  30. Atomic.graphics.setWindowPosition(editorWindow.x, editorWindow.y);
  31. }
  32. this.initUI();
  33. this.editorLicense = new EditorLicense();
  34. EditorUI.initialize();
  35. this.playMode = new PlayMode();
  36. Atomic.getResourceCache().autoReloadResources = true;
  37. this.subscribeToEvent(EditorEvents.LoadProject, (data) => this.handleEditorLoadProject(data));
  38. this.subscribeToEvent(EditorEvents.CloseProject, (data) => this.handleEditorCloseProject(data));
  39. this.subscribeToEvent("ProjectUnloaded", (data) => {
  40. Atomic.graphics.windowTitle = "AtomicEditor";
  41. this.handleProjectUnloaded(data)
  42. });
  43. this.subscribeToEvent("IPCPlayerWindow", (data) => {
  44. Preferences.getInstance().savePlayerWindowData(data.posX, data.posY, data.width, data.height);
  45. });
  46. this.subscribeToEvent("ExitRequested", (data) => this.handleExitRequested(data));
  47. this.subscribeToEvent("ProjectLoaded", (data) => {
  48. Atomic.graphics.windowTitle = "AtomicEditor - " + data.projectPath;
  49. Preferences.getInstance().registerRecentProject(data.projectPath);
  50. });
  51. this.subscribeToEvent("EditorResourceCloseCanceled", (data) => {
  52. //if user canceled closing the resource, then user has changes that he doesn't want to lose
  53. //so cancel exit/project close request and unsubscribe from event to avoid closing all the editors again
  54. this.exitRequested = false;
  55. this.projectCloseRequested = false;
  56. this.unsubscribeFromEvent(EditorEvents.EditorResourceClose);
  57. });
  58. this.parseArguments();
  59. }
  60. initUI() {
  61. var ui = Atomic.ui;
  62. ui.loadSkin("AtomicEditor/resources/default_skin/skin.tb.txt", "AtomicEditor/editor/skin/skin.tb.txt");
  63. ui.addFont("AtomicEditor/resources/vera.ttf", "Vera");
  64. ui.addFont("AtomicEditor/resources/MesloLGS-Regular.ttf", "Monaco");
  65. ui.setDefaultFont("Vera", 12);
  66. }
  67. handleEditorLoadProject(event: EditorEvents.LoadProjectEvent): boolean {
  68. var system = ToolCore.getToolSystem();
  69. if (system.project) {
  70. this.sendEvent(UIEvents.MessageModalEvent,
  71. { type: "error", title: "Project already loaded", message: "Project already loaded" });
  72. return false;
  73. }
  74. return system.loadProject(event.path);
  75. }
  76. closeAllResourceEditors() {
  77. var editor = EditorUI.getCurrentResourceEditor();
  78. if (!editor) {
  79. if (this.exitRequested) {
  80. this.exit();
  81. } else if (this.projectCloseRequested) {
  82. this.closeProject();
  83. }
  84. return;
  85. }
  86. //wait when we close resource editor to check another resource editor for unsaved changes and close it
  87. this.subscribeToEvent(EditorEvents.EditorResourceClose, (data) => {
  88. this.closeAllResourceEditors();
  89. });
  90. editor.requestClose();
  91. }
  92. handleEditorCloseProject(event) {
  93. this.projectCloseRequested = true;
  94. this.closeAllResourceEditors();
  95. }
  96. closeProject() {
  97. this.sendEvent("IPCPlayerExitRequest");
  98. var system = ToolCore.getToolSystem();
  99. if (system.project) {
  100. system.closeProject();
  101. }
  102. }
  103. handleProjectUnloaded(event) {
  104. this.sendEvent(EditorEvents.ActiveSceneChange, { scene: null });
  105. }
  106. parseArguments() {
  107. var args = Atomic.getArguments();
  108. var idx = 0;
  109. while (idx < args.length) {
  110. if (args[idx] == "--project") {
  111. this.sendEvent(EditorEvents.LoadProject, { path: args[idx + 1] });
  112. }
  113. idx++;
  114. }
  115. }
  116. // event handling
  117. handleExitRequested(data) {
  118. this.sendEvent("IPCPlayerExitRequest");
  119. this.exitRequested = true;
  120. this.closeAllResourceEditors();
  121. }
  122. exit() {
  123. Preferences.getInstance().write();
  124. EditorUI.shutdown();
  125. Atomic.getEngine().exit();
  126. }
  127. }
  128. export = Editor;