ResourceFrame.ts 4.9 KB

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