ResourceFrame.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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" || ext == ".xml" || ext == ".hlsl") {
  76. editor = new Editor.JSResourceEditor(path, this.tabcontainer);
  77. } else if (ext == ".scene") {
  78. var sceneEditor3D = new Editor.SceneEditor3D(path, this.tabcontainer);
  79. editor = sceneEditor3D;
  80. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: sceneEditor3D });
  81. }
  82. if (editor) {
  83. // cast and add editor lookup on widget itself
  84. (<EditorRootContentWidget> editor.rootContentWidget).editor = editor;
  85. this.editors[path] = editor;
  86. this.tabcontainer.currentPage = this.tabcontainer.numPages - 1;
  87. editor.setFocus();
  88. }
  89. }
  90. navigateToResource(fullpath: string, lineNumber = -1, tokenPos: number = -1) {
  91. if (this.wasClosed) return;
  92. if (!this.editors[fullpath]) {
  93. return;
  94. }
  95. var editor = this.editors[fullpath];
  96. if (this.currentResourceEditor == editor) return;
  97. var root = this.tabcontainer.contentRoot;
  98. var i = 0;
  99. for (var child = root.firstChild; child; child = child.next, i++) {
  100. if (editor.rootContentWidget == child) {
  101. break;
  102. }
  103. }
  104. if (i < this.tabcontainer.numPages) {
  105. this.tabcontainer.currentPage = i;
  106. editor.setFocus();
  107. // this cast could be better
  108. var ext = Atomic.getExtension(fullpath);
  109. if (ext == ".js" && lineNumber != -1) {
  110. (<Editor.JSResourceEditor>editor).gotoLineNumber(lineNumber);
  111. }
  112. else if (ext == ".js" && tokenPos != -1) {
  113. (<Editor.JSResourceEditor>editor).gotoTokenPos(tokenPos);
  114. }
  115. }
  116. }
  117. handleCloseResource(ev: EditorEvents.EditorCloseResourceEvent) {
  118. this.wasClosed = false;
  119. var editor = ev.editor;
  120. var navigate = ev.navigateToAvailableResource;
  121. if (!editor)
  122. return;
  123. editor.unsubscribeFromAllEvents();
  124. var editors = Object.keys(this.editors);
  125. var closedIndex = editors.indexOf(editor.fullPath);
  126. if (editor.typeName == "SceneEditor3D") {
  127. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: (<Editor.SceneEditor3D> null) });
  128. }
  129. // remove from lookup
  130. delete this.editors[editor.fullPath];
  131. var root = this.tabcontainer.contentRoot;
  132. root.removeChild(editor.rootContentWidget);
  133. if (editor != this.currentResourceEditor) {
  134. this.wasClosed = true;
  135. return;
  136. } else {
  137. this.currentResourceEditor = null;
  138. this.tabcontainer.currentPage = -1;
  139. }
  140. if (navigate) {
  141. var nextEditor = editors[closedIndex + 1];
  142. if (nextEditor) {
  143. this.navigateToResource(nextEditor);
  144. } else {
  145. this.navigateToResource(editors[closedIndex - 1]);
  146. }
  147. }
  148. }
  149. handleResourceEditorChanged(data) {
  150. var editor = <Editor.ResourceEditor> data.editor;
  151. this.currentResourceEditor = editor;
  152. }
  153. handleRenameResource(ev:EditorEvents.RenameResourceEvent) {
  154. var editor = this.editors[ev.path];
  155. if (editor) {
  156. this.editors[ev.newPath] = editor;
  157. delete this.editors[ev.path];
  158. }
  159. }
  160. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  161. if (ev.type == Atomic.UI_EVENT_TYPE_TAB_CHANGED && ev.target == this.tabcontainer) {
  162. var w = <EditorRootContentWidget> this.tabcontainer.currentPageWidget;
  163. if (w && w.editor) {
  164. if (this.currentResourceEditor != w.editor) {
  165. if (w.editor.typeName == "SceneEditor3D") {
  166. this.sendEvent(EditorEvents.ActiveSceneEditorChange, { sceneEditor: (<Editor.SceneEditor3D> w.editor) });
  167. }
  168. this.sendEvent(UIEvents.ResourceEditorChanged, { editor: w.editor });
  169. }
  170. }
  171. }
  172. if (ev.type == Atomic.UI_EVENT_TYPE_POINTER_UP) {
  173. this.wasClosed = false;
  174. }
  175. // bubble
  176. return false;
  177. }
  178. shutdown() {
  179. // on exit close all open editors
  180. for (var path in this.editors) {
  181. this.sendEvent(EditorEvents.EditorResourceClose, { editor: this.editors[path], navigateToAvailableResource: false });
  182. }
  183. }
  184. handleProjectUnloaded(data) {
  185. for (var i in this.editors) {
  186. this.editors[i].close();
  187. }
  188. }
  189. constructor(parent: Atomic.UIWidget) {
  190. super();
  191. this.load("AtomicEditor/editor/ui/resourceframe.tb.txt");
  192. this.gravity = Atomic.UI_GRAVITY_ALL;
  193. this.resourceViewContainer = <Atomic.UILayout> parent.getWidget("resourceviewcontainer");
  194. this.tabcontainer = <Atomic.UITabContainer> this.getWidget("tabcontainer");
  195. this.resourceLayout = <Atomic.UILayout> this.getWidget("resourcelayout");
  196. this.resourceViewContainer.addChild(this);
  197. this.subscribeToEvent(EditorEvents.ProjectUnloadedNotification, (data) => this.handleProjectUnloaded(data));
  198. this.subscribeToEvent(EditorEvents.EditResource, (data) => this.handleEditResource(data));
  199. this.subscribeToEvent(EditorEvents.SaveResource, (data) => this.handleSaveResource(data));
  200. this.subscribeToEvent(EditorEvents.SaveAllResources, (data) => this.handleSaveAllResources(data));
  201. this.subscribeToEvent(EditorEvents.EditorResourceClose, (ev: EditorEvents.EditorCloseResourceEvent) => this.handleCloseResource(ev));
  202. this.subscribeToEvent(EditorEvents.RenameResourceNotification, (ev: EditorEvents.RenameResourceEvent) => this.handleRenameResource(ev));
  203. this.subscribeToEvent(EditorEvents.DeleteResourceNotification, (data) => this.handleDeleteResource(data));
  204. this.subscribeToEvent(UIEvents.ResourceEditorChanged, (data) => this.handleResourceEditorChanged(data));
  205. this.subscribeToEvent("WidgetEvent", (data) => this.handleWidgetEvent(data));
  206. }
  207. }
  208. export = ResourceFrame;