ResourceFrame.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. handleDeleteResource(ev: EditorEvents.DeleteResourceEvent) {
  53. var editor = this.editors[ev.path];
  54. if (editor) {
  55. editor.close(true);
  56. delete this.editors[ev.path];
  57. }
  58. }
  59. handleSaveAllResources(data) {
  60. for (var i in this.editors) {
  61. this.editors[i].save();
  62. this.sendEvent(EditorEvents.SaveResourceNotification, {
  63. path: this.editors[i].fullPath
  64. });
  65. }
  66. }
  67. handleEditResource(ev: EditorEvents.EditResourceEvent) {
  68. var path = ev.path;
  69. if (this.editors[path]) {
  70. this.navigateToResource(path);
  71. return;
  72. }
  73. var ext = Atomic.getExtension(path);
  74. var editor: Editor.ResourceEditor = null;
  75. if (ext == ".js" || ext == ".txt" || ext == ".json" || ext == ".ts") {
  76. let jseditor = new Editor.JSResourceEditor(path, this.tabcontainer);
  77. jseditor.subscribeToEvent(EditorEvents.UserPreferencesChangedNotification, (data) => {
  78. // We can get the webclient here now, though this might not be a great place to set this up
  79. let webClient = jseditor.webView.webClient;
  80. });
  81. editor = jseditor;
  82. } else if (ext == ".scene") {
  83. var sceneEditor3D = new Editor.SceneEditor3D(path, this.tabcontainer);
  84. editor = sceneEditor3D;
  85. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: sceneEditor3D });
  86. }
  87. if (editor) {
  88. // cast and add editor lookup on widget itself
  89. (<EditorRootContentWidget> editor.rootContentWidget).editor = editor;
  90. this.editors[path] = editor;
  91. this.tabcontainer.currentPage = this.tabcontainer.numPages - 1;
  92. editor.setFocus();
  93. }
  94. }
  95. navigateToResource(fullpath: string, lineNumber = -1, tokenPos: number = -1) {
  96. if (this.wasClosed) return;
  97. if (!this.editors[fullpath]) {
  98. return;
  99. }
  100. var editor = this.editors[fullpath];
  101. if (this.currentResourceEditor == editor) return;
  102. var root = this.tabcontainer.contentRoot;
  103. var i = 0;
  104. for (var child = root.firstChild; child; child = child.next, i++) {
  105. if (editor.rootContentWidget == child) {
  106. break;
  107. }
  108. }
  109. if (i < this.tabcontainer.numPages) {
  110. this.tabcontainer.currentPage = i;
  111. editor.setFocus();
  112. // this cast could be better
  113. var ext = Atomic.getExtension(fullpath);
  114. if (ext == ".js" && lineNumber != -1) {
  115. (<Editor.JSResourceEditor>editor).gotoLineNumber(lineNumber);
  116. }
  117. else if (ext == ".js" && tokenPos != -1) {
  118. (<Editor.JSResourceEditor>editor).gotoTokenPos(tokenPos);
  119. }
  120. }
  121. }
  122. handleCloseResource(ev: EditorEvents.EditorCloseResourceEvent) {
  123. this.wasClosed = false;
  124. var editor = ev.editor;
  125. var navigate = ev.navigateToAvailableResource;
  126. if (!editor)
  127. return;
  128. editor.unsubscribeFromAllEvents();
  129. var editors = Object.keys(this.editors);
  130. var closedIndex = editors.indexOf(editor.fullPath);
  131. if (editor.typeName == "SceneEditor3D") {
  132. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: (<Editor.SceneEditor3D> null) });
  133. }
  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) {
  155. var editor = <Editor.ResourceEditor> data.editor;
  156. this.currentResourceEditor = editor;
  157. }
  158. handleRenameResource(ev:EditorEvents.RenameResourceEvent) {
  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_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(EditorEvents.ActiveSceneEditorChange, { sceneEditor: (<Editor.SceneEditor3D> w.editor) });
  172. }
  173. this.sendEvent(UIEvents.ResourceEditorChanged, { editor: w.editor });
  174. }
  175. }
  176. }
  177. if (ev.type == Atomic.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(EditorEvents.EditorResourceClose, { editor: this.editors[path], navigateToAvailableResource: false });
  187. }
  188. }
  189. handleProjectUnloaded(data) {
  190. for (var i in this.editors) {
  191. this.editors[i].close();
  192. }
  193. }
  194. constructor(parent: Atomic.UIWidget) {
  195. super();
  196. this.load("AtomicEditor/editor/ui/resourceframe.tb.txt");
  197. this.gravity = Atomic.UI_GRAVITY_ALL;
  198. this.resourceViewContainer = <Atomic.UILayout> parent.getWidget("resourceviewcontainer");
  199. this.tabcontainer = <Atomic.UITabContainer> this.getWidget("tabcontainer");
  200. this.resourceLayout = <Atomic.UILayout> this.getWidget("resourcelayout");
  201. this.resourceViewContainer.addChild(this);
  202. this.subscribeToEvent(EditorEvents.ProjectUnloadedNotification, (data) => this.handleProjectUnloaded(data));
  203. this.subscribeToEvent(EditorEvents.EditResource, (data) => this.handleEditResource(data));
  204. this.subscribeToEvent(EditorEvents.SaveResource, (data) => this.handleSaveResource(data));
  205. this.subscribeToEvent(EditorEvents.SaveAllResources, (data) => this.handleSaveAllResources(data));
  206. this.subscribeToEvent(EditorEvents.EditorResourceClose, (ev: EditorEvents.EditorCloseResourceEvent) => this.handleCloseResource(ev));
  207. this.subscribeToEvent(EditorEvents.RenameResourceNotification, (ev: EditorEvents.RenameResourceEvent) => this.handleRenameResource(ev));
  208. this.subscribeToEvent(EditorEvents.DeleteResourceNotification, (data) => this.handleDeleteResource(data));
  209. this.subscribeToEvent(UIEvents.ResourceEditorChanged, (data) => this.handleResourceEditorChanged(data));
  210. this.subscribeToEvent("WidgetEvent", (data) => this.handleWidgetEvent(data));
  211. }
  212. }
  213. export = ResourceFrame;