ResourceFrame.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. import ResourceEditorProvider from "../ResourceEditorProvider";
  26. // the root content of editor widgets (rootContentWidget property) are extended with an editor field
  27. // so we can access the editor they belong to from the widget itself
  28. interface EditorRootContentWidget extends Atomic.UIWidget {
  29. editor: Editor.ResourceEditor;
  30. }
  31. class ResourceFrame extends ScriptWidget {
  32. tabcontainer: Atomic.UITabContainer;
  33. resourceLayout: Atomic.UILayout;
  34. resourceViewContainer: Atomic.UILayout;
  35. currentResourceEditor: Editor.ResourceEditor;
  36. wasClosed: boolean;
  37. resourceEditorProvider: ResourceEditorProvider;
  38. // editors have a rootCotentWidget which is what is a child of the tab container
  39. // editors can be looked up by the full path of what they are editing
  40. editors: { [path: string]: Editor.ResourceEditor; } = {};
  41. show(value: boolean) {
  42. if (value) {
  43. }
  44. }
  45. handleSaveResource(ev: EditorEvents.SaveResourceEvent) {
  46. if (this.currentResourceEditor) {
  47. this.currentResourceEditor.save();
  48. // Grab the path to this file and pass it to the save resource
  49. this.sendEvent(EditorEvents.SaveResourceNotification, {
  50. path: ev.path || this.currentResourceEditor.fullPath
  51. });
  52. }
  53. }
  54. handleDeleteResource(ev: EditorEvents.DeleteResourceEvent) {
  55. var editor = this.editors[ev.path];
  56. if (editor) {
  57. editor.close(true);
  58. delete this.editors[ev.path];
  59. }
  60. }
  61. handleSaveAllResources(data) {
  62. for (var i in this.editors) {
  63. this.editors[i].save();
  64. this.sendEvent(EditorEvents.SaveResourceNotification, {
  65. path: this.editors[i].fullPath
  66. });
  67. }
  68. }
  69. handleEditResource(ev: EditorEvents.EditResourceEvent) {
  70. var path = ev.path;
  71. if (this.editors[path]) {
  72. this.navigateToResource(path);
  73. return;
  74. }
  75. var editor = this.resourceEditorProvider.getEditor(ev.path, this.tabcontainer);
  76. if (editor) {
  77. // cast and add editor lookup on widget itself
  78. (<EditorRootContentWidget> editor.rootContentWidget).editor = editor;
  79. this.editors[path] = editor;
  80. this.tabcontainer.currentPage = this.tabcontainer.numPages - 1;
  81. editor.setFocus();
  82. }
  83. }
  84. navigateToResource(fullpath: string, lineNumber = -1, tokenPos: number = -1) {
  85. if (this.wasClosed) return;
  86. if (!this.editors[fullpath]) {
  87. return;
  88. }
  89. var editor = this.editors[fullpath];
  90. if (this.currentResourceEditor == editor) return;
  91. var root = this.tabcontainer.contentRoot;
  92. var i = 0;
  93. for (var child = root.firstChild; child; child = child.next, i++) {
  94. if (editor.rootContentWidget == child) {
  95. break;
  96. }
  97. }
  98. if (i < this.tabcontainer.numPages) {
  99. this.tabcontainer.currentPage = i;
  100. editor.setFocus();
  101. // this cast could be better
  102. var ext = Atomic.getExtension(fullpath);
  103. if (ext == ".js" && lineNumber != -1) {
  104. (<Editor.JSResourceEditor>editor).gotoLineNumber(lineNumber);
  105. }
  106. else if (ext == ".js" && tokenPos != -1) {
  107. (<Editor.JSResourceEditor>editor).gotoTokenPos(tokenPos);
  108. }
  109. }
  110. }
  111. handleCloseResource(ev: EditorEvents.EditorCloseResourceEvent) {
  112. this.wasClosed = false;
  113. var editor = ev.editor;
  114. var navigate = ev.navigateToAvailableResource;
  115. if (!editor)
  116. return;
  117. editor.unsubscribeFromAllEvents();
  118. var editors = Object.keys(this.editors);
  119. var closedIndex = editors.indexOf(editor.fullPath);
  120. if (editor.typeName == "SceneEditor3D") {
  121. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: (<Editor.SceneEditor3D> null) });
  122. }
  123. // remove from lookup
  124. delete this.editors[editor.fullPath];
  125. var root = this.tabcontainer.contentRoot;
  126. root.removeChild(editor.rootContentWidget);
  127. if (editor != this.currentResourceEditor) {
  128. this.wasClosed = true;
  129. return;
  130. } else {
  131. this.currentResourceEditor = null;
  132. this.tabcontainer.currentPage = -1;
  133. }
  134. if (navigate) {
  135. var nextEditor = editors[closedIndex + 1];
  136. if (nextEditor) {
  137. this.navigateToResource(nextEditor);
  138. } else {
  139. this.navigateToResource(editors[closedIndex - 1]);
  140. }
  141. }
  142. }
  143. handleResourceEditorChanged(data) {
  144. var editor = <Editor.ResourceEditor> data.editor;
  145. this.currentResourceEditor = editor;
  146. }
  147. handleRenameResource(ev:EditorEvents.RenameResourceEvent) {
  148. var editor = this.editors[ev.path];
  149. if (editor) {
  150. this.editors[ev.newPath] = editor;
  151. delete this.editors[ev.path];
  152. }
  153. }
  154. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  155. if (ev.type == Atomic.UI_EVENT_TYPE_TAB_CHANGED && ev.target == this.tabcontainer) {
  156. var w = <EditorRootContentWidget> this.tabcontainer.currentPageWidget;
  157. if (w && w.editor) {
  158. if (this.currentResourceEditor != w.editor) {
  159. if (w.editor.typeName == "SceneEditor3D") {
  160. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: (<Editor.SceneEditor3D> w.editor) });
  161. }
  162. this.sendEvent(UIEvents.ResourceEditorChanged, { editor: w.editor });
  163. }
  164. }
  165. }
  166. if (ev.type == Atomic.UI_EVENT_TYPE_POINTER_UP) {
  167. this.wasClosed = false;
  168. }
  169. // bubble
  170. return false;
  171. }
  172. shutdown() {
  173. // on exit close all open editors
  174. for (var path in this.editors) {
  175. this.sendEvent(EditorEvents.EditorResourceClose, { editor: this.editors[path], navigateToAvailableResource: false });
  176. }
  177. }
  178. handleProjectUnloaded(data) {
  179. for (var i in this.editors) {
  180. var editor = this.editors[i];
  181. this.sendEvent(EditorEvents.EditorResourceClose, { editor: editor, navigateToAvailableResource: false });
  182. editor.close();
  183. }
  184. }
  185. constructor(parent: Atomic.UIWidget) {
  186. super();
  187. this.load("AtomicEditor/editor/ui/resourceframe.tb.txt");
  188. this.gravity = Atomic.UI_GRAVITY_ALL;
  189. this.resourceViewContainer = <Atomic.UILayout> parent.getWidget("resourceviewcontainer");
  190. this.tabcontainer = <Atomic.UITabContainer> this.getWidget("tabcontainer");
  191. this.resourceLayout = <Atomic.UILayout> this.getWidget("resourcelayout");
  192. this.resourceViewContainer.addChild(this);
  193. this.resourceEditorProvider = new ResourceEditorProvider(this);
  194. this.resourceEditorProvider.loadStandardEditors();
  195. this.subscribeToEvent(EditorEvents.ProjectUnloadedNotification, (data) => this.handleProjectUnloaded(data));
  196. this.subscribeToEvent(EditorEvents.EditResource, (data) => this.handleEditResource(data));
  197. this.subscribeToEvent(EditorEvents.SaveResource, (data) => this.handleSaveResource(data));
  198. this.subscribeToEvent(EditorEvents.SaveAllResources, (data) => this.handleSaveAllResources(data));
  199. this.subscribeToEvent(EditorEvents.EditorResourceClose, (ev: EditorEvents.EditorCloseResourceEvent) => this.handleCloseResource(ev));
  200. this.subscribeToEvent(EditorEvents.RenameResourceNotification, (ev: EditorEvents.RenameResourceEvent) => this.handleRenameResource(ev));
  201. this.subscribeToEvent(EditorEvents.DeleteResourceNotification, (data) => this.handleDeleteResource(data));
  202. this.subscribeToEvent(UIEvents.ResourceEditorChanged, (data) => this.handleResourceEditorChanged(data));
  203. this.subscribeToEvent("WidgetEvent", (data) => this.handleWidgetEvent(data));
  204. }
  205. }
  206. export = ResourceFrame;