ResourceFrame.ts 6.4 KB

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