ResourceFrame.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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, ev.lineNumber);
  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) {
  89. this.setCursorPositionInEditor(editor, lineNumber, tokenPos);
  90. return;
  91. }
  92. var root = this.tabcontainer.contentRoot;
  93. var i = 0;
  94. for (var child = root.firstChild; child; child = child.next, i++) {
  95. if (editor.rootContentWidget == child) {
  96. break;
  97. }
  98. }
  99. if (i < this.tabcontainer.numPages) {
  100. this.tabcontainer.currentPage = i;
  101. editor.setFocus();
  102. this.setCursorPositionInEditor(editor, lineNumber, tokenPos);
  103. }
  104. }
  105. /**
  106. *
  107. * Set the cursor to the correct line number in the editor
  108. * @param {Editor.ResourceEditor} editor
  109. * @param {any} [lineNumber=-1]
  110. * @param {any} [tokenPos=-1]
  111. *
  112. * @memberOf ResourceFrame
  113. */
  114. setCursorPositionInEditor(editor: Editor.ResourceEditor, lineNumber = -1, tokenPos = -1) {
  115. if (editor instanceof Editor.JSResourceEditor) {
  116. if (lineNumber != -1) {
  117. (<Editor.JSResourceEditor>editor).gotoLineNumber(lineNumber);
  118. }
  119. if (tokenPos != -1) {
  120. (<Editor.JSResourceEditor>editor).gotoTokenPos(tokenPos);
  121. }
  122. }
  123. }
  124. handleCloseResource(ev: Editor.EditorResourceCloseEvent) {
  125. this.wasClosed = false;
  126. var editor = ev.editor;
  127. var navigate = ev.navigateToAvailableResource;
  128. if (!editor)
  129. return;
  130. editor.unsubscribeFromAllEvents();
  131. (<EditorRootContentWidget> editor.rootContentWidget).editor = null;
  132. var editors = Object.keys(this.editors);
  133. var closedIndex = editors.indexOf(editor.fullPath);
  134. // remove from lookup
  135. delete this.editors[editor.fullPath];
  136. var root = this.tabcontainer.contentRoot;
  137. root.removeChild(editor.rootContentWidget);
  138. if (editor != this.currentResourceEditor) {
  139. this.wasClosed = true;
  140. return;
  141. } else {
  142. this.currentResourceEditor = null;
  143. this.tabcontainer.currentPage = -1;
  144. }
  145. if (navigate) {
  146. var nextEditor = editors[closedIndex + 1];
  147. if (nextEditor) {
  148. this.navigateToResource(nextEditor);
  149. } else {
  150. this.navigateToResource(editors[closedIndex - 1]);
  151. }
  152. }
  153. }
  154. handleResourceEditorChanged(data: Editor.EditorResourceEditorChangedEvent) {
  155. var editor = data.resourceEditor;
  156. this.currentResourceEditor = editor;
  157. }
  158. handleRenameResource(ev:Editor.EditorRenameResourceNotificationEvent) {
  159. var editor = this.editors[ev.path];
  160. if (editor) {
  161. this.editors[ev.newPath] = editor;
  162. delete this.editors[ev.path];
  163. }
  164. }
  165. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  166. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_TAB_CHANGED && ev.target == this.tabcontainer) {
  167. var w = <EditorRootContentWidget> this.tabcontainer.currentPageWidget;
  168. if (w && w.editor) {
  169. if (this.currentResourceEditor != w.editor) {
  170. if (w.editor.typeName == "SceneEditor3D") {
  171. this.sendEvent(Editor.EditorActiveSceneEditorChangeEventData({ sceneEditor: (<Editor.SceneEditor3D> w.editor) }));
  172. }
  173. this.sendEvent(Editor.EditorResourceEditorChangedEventData({ resourceEditor: w.editor }));
  174. }
  175. }
  176. }
  177. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_POINTER_UP) {
  178. this.wasClosed = false;
  179. }
  180. // bubble
  181. return false;
  182. }
  183. shutdown() {
  184. // on exit close all open editors
  185. for (var path in this.editors) {
  186. this.sendEvent(Editor.EditorResourceCloseEventData({ editor: this.editors[path], navigateToAvailableResource: false }));
  187. }
  188. }
  189. handleProjectUnloaded(data) {
  190. for (var i in this.editors) {
  191. var editor = this.editors[i];
  192. this.sendEvent(Editor.EditorResourceCloseEventData({ editor: editor, navigateToAvailableResource: false }));
  193. editor.close();
  194. }
  195. }
  196. constructor(parent: Atomic.UIWidget) {
  197. super();
  198. this.load("AtomicEditor/editor/ui/resourceframe.tb.txt");
  199. this.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  200. this.resourceViewContainer = <Atomic.UILayout> parent.getWidget("resourceviewcontainer");
  201. this.tabcontainer = <Atomic.UITabContainer> this.getWidget("tabcontainer");
  202. this.resourceLayout = <Atomic.UILayout> this.getWidget("resourcelayout");
  203. this.resourceViewContainer.addChild(this);
  204. this.resourceEditorProvider = new ResourceEditorProvider(this);
  205. this.resourceEditorProvider.loadStandardEditors();
  206. this.subscribeToEvent(Editor.ProjectUnloadedNotificationEvent((data) => this.handleProjectUnloaded(data)));
  207. this.subscribeToEvent(Editor.EditorEditResourceEvent((data) => this.handleEditResource(data)));
  208. this.subscribeToEvent(Editor.EditorSaveResourceEvent((data) => this.handleSaveResource(data)));
  209. this.subscribeToEvent(Editor.EditorSaveAllResourcesEvent((data) => this.handleSaveAllResources()));
  210. this.subscribeToEvent(Editor.EditorResourceCloseEvent((ev: Editor.EditorResourceCloseEvent) => this.handleCloseResource(ev)));
  211. this.subscribeToEvent(Editor.EditorRenameResourceNotificationEvent((ev: Editor.EditorRenameResourceNotificationEvent) => this.handleRenameResource(ev)));
  212. this.subscribeToEvent(Editor.EditorDeleteResourceNotificationEvent((data) => this.handleDeleteResource(data)));
  213. this.subscribeToEvent(Editor.EditorResourceEditorChangedEvent((data) => this.handleResourceEditorChanged(data)));
  214. this.subscribeToEvent(Atomic.UIWidgetEvent((data) => this.handleWidgetEvent(data)));
  215. }
  216. }
  217. export = ResourceFrame;