ResourceFrame.ts 7.2 KB

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