ResourceFrame.ts 9.2 KB

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