ResourceFrame.ts 6.7 KB

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