ResourceFrame.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. // Grab the path to this file and pass it to the save resource
  47. this.sendEvent(EditorEvents.SaveResourceNotification, {
  48. path: ev.path || this.currentResourceEditor.fullPath
  49. });
  50. }
  51. }
  52. handleSaveAllResources(data) {
  53. for (var i in this.editors) {
  54. this.editors[i].save();
  55. this.sendEvent(EditorEvents.SaveResourceNotification, {
  56. path: this.editors[i].fullPath
  57. });
  58. }
  59. }
  60. handleEditResource(ev: EditorEvents.EditResourceEvent) {
  61. var path = ev.path;
  62. if (this.editors[path]) {
  63. this.navigateToResource(path);
  64. return;
  65. }
  66. var ext = Atomic.getExtension(path);
  67. var editor: Editor.ResourceEditor = null;
  68. if (ext == ".js" || ext == ".txt" || ext == ".json" || ext == ".ts") {
  69. editor = new Editor.JSResourceEditor(path, this.tabcontainer);
  70. } else if (ext == ".scene") {
  71. var sceneEditor3D = new Editor.SceneEditor3D(path, this.tabcontainer);
  72. editor = sceneEditor3D;
  73. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: sceneEditor3D });
  74. }
  75. if (editor) {
  76. // cast and add editor lookup on widget itself
  77. (<EditorRootContentWidget> editor.rootContentWidget).editor = editor;
  78. this.editors[path] = editor;
  79. this.tabcontainer.currentPage = this.tabcontainer.numPages - 1;
  80. editor.setFocus();
  81. }
  82. }
  83. navigateToResource(fullpath: string, lineNumber = -1, tokenPos: number = -1) {
  84. if (this.wasClosed) return;
  85. if (!this.editors[fullpath]) {
  86. return;
  87. }
  88. var editor = this.editors[fullpath];
  89. if (this.currentResourceEditor == editor) return;
  90. var root = this.tabcontainer.contentRoot;
  91. var i = 0;
  92. for (var child = root.firstChild; child; child = child.next, i++) {
  93. if (editor.rootContentWidget == child) {
  94. break;
  95. }
  96. }
  97. if (i < this.tabcontainer.numPages) {
  98. this.tabcontainer.currentPage = i;
  99. editor.setFocus();
  100. // this cast could be better
  101. var ext = Atomic.getExtension(fullpath);
  102. if (ext == ".js" && lineNumber != -1) {
  103. (<Editor.JSResourceEditor>editor).gotoLineNumber(lineNumber);
  104. }
  105. else if (ext == ".js" && tokenPos != -1) {
  106. (<Editor.JSResourceEditor>editor).gotoTokenPos(tokenPos);
  107. }
  108. }
  109. }
  110. handleCloseResource(ev: EditorEvents.EditorCloseResourceEvent) {
  111. this.wasClosed = false;
  112. var editor = ev.editor;
  113. var navigate = ev.navigateToAvailableResource;
  114. if (!editor)
  115. return;
  116. editor.unsubscribeFromAllEvents();
  117. var editors = Object.keys(this.editors);
  118. var closedIndex = editors.indexOf(editor.fullPath);
  119. if (editor.typeName == "SceneEditor3D") {
  120. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: (<Editor.SceneEditor3D> null) });
  121. }
  122. // remove from lookup
  123. delete this.editors[editor.fullPath];
  124. var root = this.tabcontainer.contentRoot;
  125. root.removeChild(editor.rootContentWidget);
  126. if (editor != this.currentResourceEditor) {
  127. this.wasClosed = true;
  128. return;
  129. } else {
  130. this.currentResourceEditor = null;
  131. this.tabcontainer.currentPage = -1;
  132. }
  133. if (navigate) {
  134. var nextEditor = editors[closedIndex + 1];
  135. if (nextEditor) {
  136. this.navigateToResource(nextEditor);
  137. } else {
  138. this.navigateToResource(editors[closedIndex - 1]);
  139. }
  140. }
  141. }
  142. handleResourceEditorChanged(data) {
  143. var editor = <Editor.ResourceEditor> data.editor;
  144. this.currentResourceEditor = editor;
  145. }
  146. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  147. if (ev.type == Atomic.UI_EVENT_TYPE_TAB_CHANGED && ev.target == this.tabcontainer) {
  148. var w = <EditorRootContentWidget> this.tabcontainer.currentPageWidget;
  149. if (w && w.editor) {
  150. if (this.currentResourceEditor != w.editor) {
  151. if (w.editor.typeName == "SceneEditor3D") {
  152. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: (<Editor.SceneEditor3D> w.editor) });
  153. }
  154. this.sendEvent(UIEvents.ResourceEditorChanged, { editor: w.editor });
  155. }
  156. }
  157. }
  158. if (ev.type == Atomic.UI_EVENT_TYPE_POINTER_UP) {
  159. this.wasClosed = false;
  160. }
  161. // bubble
  162. return false;
  163. }
  164. shutdown() {
  165. // on exit close all open editors
  166. for (var path in this.editors) {
  167. this.sendEvent(EditorEvents.EditorResourceClose, { editor: this.editors[path], navigateToAvailableResource: false });
  168. }
  169. }
  170. handleProjectUnloaded(data) {
  171. for (var i in this.editors) {
  172. this.editors[i].close();
  173. }
  174. }
  175. constructor(parent: Atomic.UIWidget) {
  176. super();
  177. this.load("AtomicEditor/editor/ui/resourceframe.tb.txt");
  178. this.gravity = Atomic.UI_GRAVITY_ALL;
  179. this.resourceViewContainer = <Atomic.UILayout> parent.getWidget("resourceviewcontainer");
  180. this.tabcontainer = <Atomic.UITabContainer> this.getWidget("tabcontainer");
  181. this.resourceLayout = <Atomic.UILayout> this.getWidget("resourcelayout");
  182. this.resourceViewContainer.addChild(this);
  183. this.subscribeToEvent("ProjectUnloaded", (data) => this.handleProjectUnloaded(data));
  184. this.subscribeToEvent(EditorEvents.EditResource, (data) => this.handleEditResource(data));
  185. this.subscribeToEvent(EditorEvents.SaveResource, (data) => this.handleSaveResource(data));
  186. this.subscribeToEvent(EditorEvents.SaveAllResources, (data) => this.handleSaveAllResources(data));
  187. this.subscribeToEvent(EditorEvents.EditorResourceClose, (ev: EditorEvents.EditorCloseResourceEvent) => this.handleCloseResource(ev));
  188. this.subscribeToEvent(UIEvents.ResourceEditorChanged, (data) => this.handleResourceEditorChanged(data));
  189. this.subscribeToEvent("WidgetEvent", (data) => this.handleWidgetEvent(data));
  190. }
  191. }
  192. export = ResourceFrame;