ResourceFrame.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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: Editor.EditorSaveResourceEvent) {
  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(Editor.EditorSaveResourceNotificationEventType, {
  50. path: ev.path || this.currentResourceEditor.fullPath
  51. });
  52. }
  53. }
  54. handleDeleteResource(ev: Editor.EditorDeleteResourceEvent) {
  55. var editor = this.editors[ev.path];
  56. if (editor) {
  57. editor.close(true);
  58. delete this.editors[ev.path];
  59. }
  60. }
  61. handleSaveAllResources() {
  62. for (var i in this.editors) {
  63. this.editors[i].save();
  64. this.sendEvent(Editor.EditorSaveResourceNotificationEventType, {
  65. path: this.editors[i].fullPath
  66. });
  67. }
  68. }
  69. handleEditResource(ev: Editor.EditorEditResourceEvent) {
  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: Editor.EditorResourceCloseEvent) {
  112. this.wasClosed = false;
  113. var editor = ev.editor;
  114. var navigate = ev.navigateToAvailableResource;
  115. if (!editor)
  116. return;
  117. editor.unsubscribeFromAllEvents();
  118. (<EditorRootContentWidget> editor.rootContentWidget).editor = null;
  119. var editors = Object.keys(this.editors);
  120. var closedIndex = editors.indexOf(editor.fullPath);
  121. // remove from lookup
  122. delete this.editors[editor.fullPath];
  123. var root = this.tabcontainer.contentRoot;
  124. root.removeChild(editor.rootContentWidget);
  125. if (editor != this.currentResourceEditor) {
  126. this.wasClosed = true;
  127. return;
  128. } else {
  129. this.currentResourceEditor = null;
  130. this.tabcontainer.currentPage = -1;
  131. }
  132. if (navigate) {
  133. var nextEditor = editors[closedIndex + 1];
  134. if (nextEditor) {
  135. this.navigateToResource(nextEditor);
  136. } else {
  137. this.navigateToResource(editors[closedIndex - 1]);
  138. }
  139. }
  140. }
  141. handleResourceEditorChanged(data) {
  142. var editor = <Editor.ResourceEditor> data.editor;
  143. this.currentResourceEditor = editor;
  144. }
  145. handleRenameResource(ev:Editor.EditorRenameResourceNotificationEvent) {
  146. var editor = this.editors[ev.path];
  147. if (editor) {
  148. this.editors[ev.newPath] = editor;
  149. delete this.editors[ev.path];
  150. }
  151. }
  152. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  153. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_TAB_CHANGED && ev.target == this.tabcontainer) {
  154. var w = <EditorRootContentWidget> this.tabcontainer.currentPageWidget;
  155. if (w && w.editor) {
  156. if (this.currentResourceEditor != w.editor) {
  157. if (w.editor.typeName == "SceneEditor3D") {
  158. this.sendEvent(Editor.EditorActiveSceneEditorChangeEventType, { sceneEditor: (<Editor.SceneEditor3D> w.editor) });
  159. }
  160. this.sendEvent(UIEvents.ResourceEditorChanged, { editor: w.editor });
  161. }
  162. }
  163. }
  164. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_POINTER_UP) {
  165. this.wasClosed = false;
  166. }
  167. // bubble
  168. return false;
  169. }
  170. shutdown() {
  171. // on exit close all open editors
  172. for (var path in this.editors) {
  173. this.sendEvent(Editor.EditorResourceCloseEventType, { editor: this.editors[path], navigateToAvailableResource: false });
  174. }
  175. }
  176. handleProjectUnloaded(data) {
  177. for (var i in this.editors) {
  178. var editor = this.editors[i];
  179. this.sendEvent(Editor.EditorResourceCloseEventType, { editor: editor, navigateToAvailableResource: false });
  180. editor.close();
  181. }
  182. }
  183. constructor(parent: Atomic.UIWidget) {
  184. super();
  185. this.load("AtomicEditor/editor/ui/resourceframe.tb.txt");
  186. this.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  187. this.resourceViewContainer = <Atomic.UILayout> parent.getWidget("resourceviewcontainer");
  188. this.tabcontainer = <Atomic.UITabContainer> this.getWidget("tabcontainer");
  189. this.resourceLayout = <Atomic.UILayout> this.getWidget("resourcelayout");
  190. this.resourceViewContainer.addChild(this);
  191. this.resourceEditorProvider = new ResourceEditorProvider(this);
  192. this.resourceEditorProvider.loadStandardEditors();
  193. this.subscribeToEvent(EditorEvents.ProjectUnloadedNotificationEvent((data) => this.handleProjectUnloaded(data)));
  194. this.subscribeToEvent(Editor.EditorEditResourceEvent((data) => this.handleEditResource(data)));
  195. this.subscribeToEvent(Editor.EditorSaveResourceEvent((data) => this.handleSaveResource(data)));
  196. this.subscribeToEvent(Editor.EditorSaveAllResourcesEvent((data) => this.handleSaveAllResources()));
  197. this.subscribeToEvent(Editor.EditorResourceCloseEvent((ev: Editor.EditorResourceCloseEvent) => this.handleCloseResource(ev)));
  198. this.subscribeToEvent(Editor.EditorRenameResourceNotificationEvent((ev: Editor.EditorRenameResourceNotificationEvent) => this.handleRenameResource(ev)));
  199. this.subscribeToEvent(Editor.EditorDeleteResourceNotificationEvent((data) => this.handleDeleteResource(data)));
  200. this.subscribeToEvent(UIEvents.ResourceEditorChangedEvent((data) => this.handleResourceEditorChanged(data)));
  201. this.subscribeToEvent(Atomic.UIWidgetEvent((data) => this.handleWidgetEvent(data)));
  202. }
  203. }
  204. export = ResourceFrame;