ResourceFrame.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 ScriptWidget = require("ui/ScriptWidget");
  8. import EditorEvents = require("editor/EditorEvents");
  9. import UIEvents = require("ui/UIEvents");
  10. // the root content of editor widgets (rootContentWidget property) are extended with an editor field
  11. // so we can access the editor they belong to from the widget itself
  12. interface EditorRootContentWidget extends Atomic.UIWidget {
  13. editor: Editor.ResourceEditor;
  14. }
  15. class ResourceFrame extends ScriptWidget {
  16. tabcontainer: Atomic.UITabContainer;
  17. resourceLayout: Atomic.UILayout;
  18. resourceViewContainer: Atomic.UILayout;
  19. currentResourceEditor: Editor.ResourceEditor;
  20. // editors have a rootCotentWidget which is what is a child of the tab container
  21. // editors can be looked up by the full path of what they are editing
  22. editors: { [path: string]: Editor.ResourceEditor; } = {};
  23. show(value: boolean) {
  24. if (value) {
  25. }
  26. }
  27. handleSaveResource(ev: EditorEvents.SaveResourceEvent) {
  28. if (this.currentResourceEditor)
  29. this.currentResourceEditor.save();
  30. }
  31. handleSaveAllResources(data) {
  32. for (var i in this.editors) {
  33. this.editors[i].save();
  34. }
  35. }
  36. handleEditResource(ev: EditorEvents.EditResourceEvent) {
  37. var path = ev.path;
  38. if (this.editors[path]) {
  39. this.navigateToResource(path);
  40. return;
  41. }
  42. var ext = Atomic.getExtension(path);
  43. var editor: Editor.ResourceEditor = null;
  44. if (ext == ".js" || ext == ".txt") {
  45. editor = new Editor.JSResourceEditor(path, this.tabcontainer);
  46. } else if (ext == ".scene") {
  47. var sceneEditor3D = new Editor.SceneEditor3D(path, this.tabcontainer);
  48. editor = sceneEditor3D;
  49. this.sendEvent(EditorEvents.ActiveSceneChange, { scene: sceneEditor3D.scene });
  50. }
  51. if (editor) {
  52. // cast and add editor lookup on widget itself
  53. (<EditorRootContentWidget> editor.rootContentWidget).editor = editor;
  54. this.editors[path] = editor;
  55. this.tabcontainer.currentPage = this.tabcontainer.numPages - 1;
  56. editor.setFocus();
  57. }
  58. }
  59. navigateToResource(fullpath: string, lineNumber = -1, tokenPos: number = -1) {
  60. if (!this.editors[fullpath]) {
  61. return;
  62. }
  63. var editor = this.editors[fullpath];
  64. if (this.currentResourceEditor == editor) return;
  65. var root = this.tabcontainer.contentRoot;
  66. var i = 0;
  67. for (var child = root.firstChild; child; child = child.next, i++) {
  68. if (editor.rootContentWidget == child) {
  69. break;
  70. }
  71. }
  72. if (i < this.tabcontainer.numPages) {
  73. this.tabcontainer.currentPage = i;
  74. editor.setFocus();
  75. // this cast could be better
  76. var ext = Atomic.getExtension(fullpath);
  77. if (ext == ".js" && lineNumber != -1) {
  78. (<Editor.JSResourceEditor>editor).gotoLineNumber(lineNumber);
  79. }
  80. else if (ext == ".js" && tokenPos != -1) {
  81. (<Editor.JSResourceEditor>editor).gotoTokenPos(tokenPos);
  82. }
  83. }
  84. }
  85. handleCloseResource(ev: EditorEvents.CloseResourceEvent) {
  86. var editor = ev.editor;
  87. var navigate = ev.navigateToAvailableResource;
  88. if (!editor)
  89. return;
  90. editor.unsubscribeFromAllEvents();
  91. var editors = Object.keys(this.editors);
  92. var closedIndex = editors.indexOf(editor.fullPath);
  93. // remove from lookup
  94. delete this.editors[editor.fullPath];
  95. var root = this.tabcontainer.contentRoot;
  96. root.removeChild(editor.rootContentWidget);
  97. if (editor != this.currentResourceEditor) {
  98. return;
  99. } else {
  100. this.currentResourceEditor = null;
  101. this.tabcontainer.currentPage = -1;
  102. }
  103. if (navigate) {
  104. var nextEditor = editors[closedIndex+1];
  105. if (nextEditor) {
  106. this.navigateToResource(nextEditor);
  107. } else {
  108. this.navigateToResource(editors[closedIndex-1]);
  109. }
  110. }
  111. }
  112. handleResourceEditorChanged(data) {
  113. var editor = <Editor.ResourceEditor> data.editor;
  114. this.currentResourceEditor = editor;
  115. }
  116. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  117. if (ev.type == Atomic.UI_EVENT_TYPE_TAB_CHANGED && ev.target == this.tabcontainer) {
  118. var w = <EditorRootContentWidget> this.tabcontainer.currentPageWidget;
  119. if (w && w.editor) {
  120. if (this.currentResourceEditor != w.editor) {
  121. if (w.editor.typeName == "SceneEditor3D") {
  122. this.sendEvent(EditorEvents.ActiveSceneChange, { scene: (<Editor.SceneEditor3D> w.editor).scene });
  123. }
  124. this.sendEvent(UIEvents.ResourceEditorChanged, { editor: w.editor });
  125. }
  126. }
  127. }
  128. // bubble
  129. return false;
  130. }
  131. shutdown() {
  132. // on exit close all open editors
  133. for (var path in this.editors) {
  134. this.sendEvent(EditorEvents.CloseResource, { editor: this.editors[path], navigateToAvailableResource: false });
  135. }
  136. }
  137. handleProjectUnloaded(data) {
  138. for (var i in this.editors) {
  139. this.editors[i].close();
  140. }
  141. }
  142. constructor(parent: Atomic.UIWidget) {
  143. super();
  144. this.load("AtomicEditor/editor/ui/resourceframe.tb.txt");
  145. this.gravity = Atomic.UI_GRAVITY_ALL;
  146. this.resourceViewContainer = <Atomic.UILayout> parent.getWidget("resourceviewcontainer");
  147. this.tabcontainer = <Atomic.UITabContainer> this.getWidget("tabcontainer");
  148. this.resourceLayout = <Atomic.UILayout> this.getWidget("resourcelayout");
  149. this.resourceViewContainer.addChild(this);
  150. this.subscribeToEvent("ProjectUnloaded", (data) => this.handleProjectUnloaded(data));
  151. this.subscribeToEvent(EditorEvents.EditResource, (data) => this.handleEditResource(data));
  152. this.subscribeToEvent(EditorEvents.SaveResource, (data) => this.handleSaveResource(data));
  153. this.subscribeToEvent(EditorEvents.SaveAllResources, (data) => this.handleSaveAllResources(data));
  154. this.subscribeToEvent(EditorEvents.CloseResource, (ev: EditorEvents.CloseResourceEvent) => this.handleCloseResource(ev));
  155. this.subscribeToEvent(UIEvents.ResourceEditorChanged, (data) => this.handleResourceEditorChanged(data));
  156. this.subscribeToEvent("WidgetEvent", (data) => this.handleWidgetEvent(data));
  157. }
  158. }
  159. export = ResourceFrame;