ResourceFrame.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import ScriptWidget = require("ui/ScriptWidget");
  23. import EditorEvents = require("editor/EditorEvents");
  24. import UIEvents = require("ui/UIEvents");
  25. // the root content of editor widgets (rootContentWidget property) are extended with an editor field
  26. // so we can access the editor they belong to from the widget itself
  27. interface EditorRootContentWidget extends Atomic.UIWidget {
  28. editor: Editor.ResourceEditor;
  29. }
  30. class ResourceFrame extends ScriptWidget {
  31. tabcontainer: Atomic.UITabContainer;
  32. resourceLayout: Atomic.UILayout;
  33. resourceViewContainer: Atomic.UILayout;
  34. currentResourceEditor: Editor.ResourceEditor;
  35. wasClosed: boolean;
  36. // editors have a rootCotentWidget which is what is a child of the tab container
  37. // editors can be looked up by the full path of what they are editing
  38. editors: { [path: string]: Editor.ResourceEditor; } = {};
  39. show(value: boolean) {
  40. if (value) {
  41. }
  42. }
  43. handleSaveResource(ev: EditorEvents.SaveResourceEvent) {
  44. if (this.currentResourceEditor) {
  45. this.currentResourceEditor.save();
  46. }
  47. }
  48. handleSaveAllResources(data) {
  49. for (var i in this.editors) {
  50. this.editors[i].save();
  51. }
  52. }
  53. handleEditResource(ev: EditorEvents.EditResourceEvent) {
  54. var path = ev.path;
  55. if (this.editors[path]) {
  56. this.navigateToResource(path);
  57. return;
  58. }
  59. var ext = Atomic.getExtension(path);
  60. var editor: Editor.ResourceEditor = null;
  61. if (ext == ".js" || ext == ".txt" || ext == ".json" || ext == ".ts") {
  62. editor = new Editor.JSResourceEditor(path, this.tabcontainer);
  63. } else if (ext == ".scene") {
  64. var sceneEditor3D = new Editor.SceneEditor3D(path, this.tabcontainer);
  65. editor = sceneEditor3D;
  66. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: sceneEditor3D });
  67. }
  68. if (editor) {
  69. // cast and add editor lookup on widget itself
  70. (<EditorRootContentWidget> editor.rootContentWidget).editor = editor;
  71. this.editors[path] = editor;
  72. this.tabcontainer.currentPage = this.tabcontainer.numPages - 1;
  73. editor.setFocus();
  74. }
  75. }
  76. navigateToResource(fullpath: string, lineNumber = -1, tokenPos: number = -1) {
  77. if (this.wasClosed) return;
  78. if (!this.editors[fullpath]) {
  79. return;
  80. }
  81. var editor = this.editors[fullpath];
  82. if (this.currentResourceEditor == editor) return;
  83. var root = this.tabcontainer.contentRoot;
  84. var i = 0;
  85. for (var child = root.firstChild; child; child = child.next, i++) {
  86. if (editor.rootContentWidget == child) {
  87. break;
  88. }
  89. }
  90. if (i < this.tabcontainer.numPages) {
  91. this.tabcontainer.currentPage = i;
  92. editor.setFocus();
  93. // this cast could be better
  94. var ext = Atomic.getExtension(fullpath);
  95. if (ext == ".js" && lineNumber != -1) {
  96. (<Editor.JSResourceEditor>editor).gotoLineNumber(lineNumber);
  97. }
  98. else if (ext == ".js" && tokenPos != -1) {
  99. (<Editor.JSResourceEditor>editor).gotoTokenPos(tokenPos);
  100. }
  101. }
  102. }
  103. handleCloseResource(ev: EditorEvents.EditorCloseResourceEvent) {
  104. this.wasClosed = false;
  105. var editor = ev.editor;
  106. var navigate = ev.navigateToAvailableResource;
  107. if (!editor)
  108. return;
  109. editor.unsubscribeFromAllEvents();
  110. var editors = Object.keys(this.editors);
  111. var closedIndex = editors.indexOf(editor.fullPath);
  112. if (editor.typeName == "SceneEditor3D") {
  113. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: (<Editor.SceneEditor3D> null) });
  114. }
  115. // remove from lookup
  116. delete this.editors[editor.fullPath];
  117. var root = this.tabcontainer.contentRoot;
  118. root.removeChild(editor.rootContentWidget);
  119. if (editor != this.currentResourceEditor) {
  120. this.wasClosed = true;
  121. return;
  122. } else {
  123. this.currentResourceEditor = null;
  124. this.tabcontainer.currentPage = -1;
  125. }
  126. if (navigate) {
  127. var nextEditor = editors[closedIndex + 1];
  128. if (nextEditor) {
  129. this.navigateToResource(nextEditor);
  130. } else {
  131. this.navigateToResource(editors[closedIndex - 1]);
  132. }
  133. }
  134. }
  135. handleResourceEditorChanged(data) {
  136. var editor = <Editor.ResourceEditor> data.editor;
  137. this.currentResourceEditor = editor;
  138. }
  139. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  140. if (ev.type == Atomic.UI_EVENT_TYPE_TAB_CHANGED && ev.target == this.tabcontainer) {
  141. var w = <EditorRootContentWidget> this.tabcontainer.currentPageWidget;
  142. if (w && w.editor) {
  143. if (this.currentResourceEditor != w.editor) {
  144. if (w.editor.typeName == "SceneEditor3D") {
  145. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: (<Editor.SceneEditor3D> w.editor) });
  146. }
  147. this.sendEvent(UIEvents.ResourceEditorChanged, { editor: w.editor });
  148. }
  149. }
  150. }
  151. if (ev.type == Atomic.UI_EVENT_TYPE_POINTER_UP) {
  152. this.wasClosed = false;
  153. }
  154. // bubble
  155. return false;
  156. }
  157. shutdown() {
  158. // on exit close all open editors
  159. for (var path in this.editors) {
  160. this.sendEvent(EditorEvents.EditorResourceClose, { editor: this.editors[path], navigateToAvailableResource: false });
  161. }
  162. }
  163. handleProjectUnloaded(data) {
  164. for (var i in this.editors) {
  165. this.editors[i].close();
  166. }
  167. }
  168. constructor(parent: Atomic.UIWidget) {
  169. super();
  170. this.load("AtomicEditor/editor/ui/resourceframe.tb.txt");
  171. this.gravity = Atomic.UI_GRAVITY_ALL;
  172. this.resourceViewContainer = <Atomic.UILayout> parent.getWidget("resourceviewcontainer");
  173. this.tabcontainer = <Atomic.UITabContainer> this.getWidget("tabcontainer");
  174. this.resourceLayout = <Atomic.UILayout> this.getWidget("resourcelayout");
  175. this.resourceViewContainer.addChild(this);
  176. this.subscribeToEvent("ProjectUnloaded", (data) => this.handleProjectUnloaded(data));
  177. this.subscribeToEvent(EditorEvents.EditResource, (data) => this.handleEditResource(data));
  178. this.subscribeToEvent(EditorEvents.SaveResource, (data) => this.handleSaveResource(data));
  179. this.subscribeToEvent(EditorEvents.SaveAllResources, (data) => this.handleSaveAllResources(data));
  180. this.subscribeToEvent(EditorEvents.EditorResourceClose, (ev: EditorEvents.EditorCloseResourceEvent) => this.handleCloseResource(ev));
  181. this.subscribeToEvent(UIEvents.ResourceEditorChanged, (data) => this.handleResourceEditorChanged(data));
  182. this.subscribeToEvent("WidgetEvent", (data) => this.handleWidgetEvent(data));
  183. }
  184. }
  185. export = ResourceFrame;