Editor.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. this.initUI();
  28. this.editorLicense = new EditorLicense();
  29. EditorUI.initialize();
  30. this.playMode = new PlayMode();
  31. Atomic.getResourceCache().autoReloadResources = true;
  32. this.subscribeToEvent(EditorEvents.LoadProject, (data) => this.handleEditorLoadProject(data));
  33. this.subscribeToEvent(EditorEvents.CloseProject, (data) => this.handleEditorCloseProject(data));
  34. this.subscribeToEvent("ProjectUnloaded", (data) => {
  35. Atomic.graphics.windowTitle = "AtomicEditor";
  36. this.handleProjectUnloaded(data)
  37. });
  38. this.subscribeToEvent("IPCPlayerWindowChanged", (data) => {
  39. Preferences.getInstance().savePlayerWindowData(data.posX, data.posY, data.width, data.height, data.monitor);
  40. });
  41. this.subscribeToEvent("ExitRequested", (data) => this.handleExitRequested(data));
  42. this.subscribeToEvent("ProjectLoaded", (data) => {
  43. Atomic.graphics.windowTitle = "AtomicEditor - " + data.projectPath;
  44. Preferences.getInstance().registerRecentProject(data.projectPath);
  45. });
  46. this.subscribeToEvent("EditorResourceCloseCanceled", (data) => {
  47. //if user canceled closing the resource, then user has changes that he doesn't want to lose
  48. //so cancel exit/project close request and unsubscribe from event to avoid closing all the editors again
  49. this.exitRequested = false;
  50. this.projectCloseRequested = false;
  51. this.unsubscribeFromEvent(EditorEvents.EditorResourceClose);
  52. });
  53. this.parseArguments();
  54. }
  55. initUI() {
  56. var ui = Atomic.ui;
  57. ui.loadSkin("AtomicEditor/resources/default_skin/skin.tb.txt", "AtomicEditor/editor/skin/skin.tb.txt");
  58. ui.addFont("AtomicEditor/resources/vera.ttf", "Vera");
  59. ui.addFont("AtomicEditor/resources/MesloLGS-Regular.ttf", "Monaco");
  60. ui.setDefaultFont("Vera", 12);
  61. }
  62. handleEditorLoadProject(event: EditorEvents.LoadProjectEvent): boolean {
  63. var system = ToolCore.getToolSystem();
  64. if (system.project) {
  65. this.sendEvent(UIEvents.MessageModalEvent,
  66. { type: "error", title: "Project already loaded", message: "Project already loaded" });
  67. return false;
  68. }
  69. return system.loadProject(event.path);
  70. }
  71. closeAllResourceEditors() {
  72. var editor = EditorUI.getCurrentResourceEditor();
  73. if (!editor) {
  74. if (this.exitRequested) {
  75. this.exit();
  76. } else if (this.projectCloseRequested) {
  77. this.closeProject();
  78. }
  79. return;
  80. }
  81. //wait when we close resource editor to check another resource editor for unsaved changes and close it
  82. this.subscribeToEvent(EditorEvents.EditorResourceClose, (data) => {
  83. this.closeAllResourceEditors();
  84. });
  85. editor.requestClose();
  86. }
  87. handleEditorCloseProject(event) {
  88. this.projectCloseRequested = true;
  89. this.closeAllResourceEditors();
  90. }
  91. closeProject() {
  92. this.sendEvent("IPCPlayerExitRequest");
  93. var system = ToolCore.getToolSystem();
  94. if (system.project) {
  95. system.closeProject();
  96. }
  97. }
  98. handleProjectUnloaded(event) {
  99. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: null });
  100. }
  101. parseArguments() {
  102. var args = Atomic.getArguments();
  103. var idx = 0;
  104. while (idx < args.length) {
  105. if (args[idx] == "--project") {
  106. this.sendEvent(EditorEvents.LoadProject, { path: args[idx + 1] });
  107. }
  108. idx++;
  109. }
  110. }
  111. // event handling
  112. handleExitRequested(data) {
  113. if (this.exitRequested) return;
  114. this.sendEvent("IPCPlayerExitRequest");
  115. this.exitRequested = true;
  116. this.closeAllResourceEditors();
  117. }
  118. exit() {
  119. Preferences.getInstance().write();
  120. EditorUI.shutdown();
  121. Atomic.getEngine().exit();
  122. }
  123. }
  124. export = Editor;