Editor.ts 4.7 KB

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