ResourceFrame.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import ScriptWidget = require("./ScriptWidget");
  2. import EditorEvents = require("../editor/EditorEvents");
  3. import UIEvents = require("./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. handleEditResource(ev: EditorEvents.EditResourceEvent) {
  22. var path = ev.path;
  23. if (this.editors[path]) {
  24. return;
  25. }
  26. var ext = Atomic.getExtension(path);
  27. var editor: Editor.ResourceEditor = null;
  28. if (ext == ".js") {
  29. editor = new Editor.JSResourceEditor(path, this.tabcontainer);
  30. } else if (ext == ".scene") {
  31. var sceneEditor3D = new Editor.SceneEditor3D(path, this.tabcontainer);
  32. editor = sceneEditor3D;
  33. this.sendEvent("EditorActiveSceneChanged", { scene : sceneEditor3D.scene });
  34. }
  35. if (editor) {
  36. // cast and add editor lookup on widget itself
  37. (<EditorRootContentWidget> editor.rootContentWidget).editor = editor;
  38. this.editors[path] = editor;
  39. this.tabcontainer.currentPage = this.tabcontainer.numPages - 1;
  40. editor.setFocus();
  41. }
  42. }
  43. navigateToResource(fullpath: string, lineNumber = -1, tokenPos: number = -1) {
  44. if (!this.editors[fullpath]) {
  45. return;
  46. }
  47. var editor = this.editors[fullpath];
  48. var root = this.tabcontainer.contentRoot;
  49. var i = 0;
  50. for (var child = root.firstChild; child; child = child.next, i++) {
  51. if (editor.rootContentWidget == child) {
  52. break;
  53. }
  54. }
  55. if (i < this.tabcontainer.numPages) {
  56. this.tabcontainer.currentPage = i;
  57. editor.setFocus();
  58. // this cast could be better
  59. var ext = Atomic.getExtension(fullpath);
  60. if (ext == ".js" && lineNumber != -1) {
  61. (<Editor.JSResourceEditor>editor).gotoLineNumber(lineNumber);
  62. }
  63. else if (ext == ".js" && tokenPos != -1) {
  64. (<Editor.JSResourceEditor>editor).gotoTokenPos(tokenPos);
  65. }
  66. }
  67. }
  68. handleCloseResourceEditor(data) {
  69. var editor = <Editor.ResourceEditor> data.editor;
  70. var navigate = <boolean> data.navigateToAvailableResource;
  71. editor.unsubscribeFromAllEvents();
  72. // remove from lookup
  73. delete this.editors[editor.fullPath];
  74. var root = this.tabcontainer.contentRoot;
  75. root.removeChild(editor.rootContentWidget);
  76. this.tabcontainer.currentPage = -1;
  77. if (navigate) {
  78. var keys = Object.keys(this.editors);
  79. if (keys.length) {
  80. this.navigateToResource(keys[keys.length - 1]);
  81. }
  82. }
  83. }
  84. handleResourceEditorChanged(data) {
  85. var editor = <Editor.ResourceEditor> data.editor;
  86. this.currentResourceEditor = editor;
  87. }
  88. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  89. if (ev.type == Atomic.UI_EVENT_TYPE_TAB_CHANGED && ev.target == this.tabcontainer) {
  90. var w = <EditorRootContentWidget> this.tabcontainer.currentPageWidget;
  91. if (w && w.editor) {
  92. if (this.currentResourceEditor != w.editor) {
  93. this.sendEvent(UIEvents.ResourceEditorChanged, { editor: w.editor });
  94. }
  95. }
  96. }
  97. // bubble
  98. return false;
  99. }
  100. constructor(parent: Atomic.UIWidget) {
  101. super();
  102. this.load("AtomicEditor/editor/ui/resourceframe.tb.txt");
  103. this.gravity = Atomic.UI_GRAVITY_ALL;
  104. this.resourceViewContainer = <Atomic.UILayout> parent.getWidget("resourceviewcontainer");
  105. this.tabcontainer = <Atomic.UITabContainer> this.getWidget("tabcontainer");
  106. this.resourceLayout = <Atomic.UILayout> this.getWidget("resourcelayout");
  107. this.resourceViewContainer.addChild(this);
  108. this.subscribeToEvent(EditorEvents.EditResource, (data) => this.handleEditResource(data));
  109. this.subscribeToEvent(UIEvents.CloseResourceEditor, (data) => this.handleCloseResourceEditor(data));
  110. this.subscribeToEvent(UIEvents.ResourceEditorChanged, (data) => this.handleResourceEditorChanged(data));
  111. this.subscribeToEvent("WidgetEvent", (data) => this.handleWidgetEvent(data));
  112. }
  113. }
  114. export = ResourceFrame;