HierarchyFrame.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 HierarchyFrameMenu = require("./menus/HierarchyFrameMenu");
  23. import MenuItemSources = require("./menus/MenuItemSources");
  24. import EditorEvents = require("editor/EditorEvents");
  25. import EditorUI = require("ui/EditorUI");
  26. import SearchBarFiltering = require("resources/SearchBarFiltering");
  27. var IconTemporary = "ComponentBitmap";
  28. class HierarchyFrame extends Atomic.UIWidget {
  29. scene: Atomic.Scene = null;
  30. sceneEditor: Editor.SceneEditor3D;
  31. hierList: Atomic.UIListView;
  32. menu: HierarchyFrameMenu;
  33. nodeIDToItemID = {};
  34. uiSearchBar: SearchBarFiltering.UISearchBar = new SearchBarFiltering.UISearchBar();
  35. search: boolean = false;
  36. searchEdit: Atomic.UIEditField;
  37. selectedNode: Atomic.Node;
  38. canReparent: boolean;
  39. constructor(parent: Atomic.UIWidget) {
  40. super();
  41. this.menu = new HierarchyFrameMenu();
  42. this.load("AtomicEditor/editor/ui/hierarchyframe.tb.txt");
  43. this.gravity = Atomic.UI_GRAVITY_TOP_BOTTOM;
  44. this.searchEdit = <Atomic.UIEditField>this.getWidget("filter");
  45. this.canReparent = true;
  46. var hierarchycontainer = parent.getWidget("hierarchycontainer");
  47. hierarchycontainer.addChild(this);
  48. hierarchycontainer = this.getWidget("hierarchycontainer");
  49. var hierList = this.hierList = new Atomic.UIListView();
  50. hierList.multiSelect = true;
  51. hierList.rootList.id = "hierList_";
  52. hierList.subscribeToEvent("UIListViewSelectionChanged", (event: Atomic.UIListViewSelectionChangedEvent) => this.handleHierListSelectionChangedEvent(event));
  53. hierarchycontainer.addChild(hierList);
  54. this.subscribeToEvent(this, "WidgetEvent", (data) => this.handleWidgetEvent(data));
  55. this.subscribeToEvent(EditorEvents.ActiveSceneEditorChange, (data) => this.handleActiveSceneEditorChanged(data));
  56. // on mouse up clear the list's drag object
  57. this.subscribeToEvent("MouseButtonUp", () => {
  58. // handle dropping on hierarchy, moving node, dropping prefabs, etc
  59. this.subscribeToEvent(this.hierList.rootList, "DragEnded", (data) => this.handleDragEnded(data));
  60. this.hierList.rootList.dragObject = null;
  61. });
  62. this.subscribeToEvent("KeyDown", () => {
  63. this.canReparent = false;
  64. });
  65. this.subscribeToEvent("KeyUp", () => {
  66. this.canReparent = true;
  67. });
  68. this.subscribeToEvent(EditorEvents.ProjectClosed, (ev) => {
  69. this.scene = null;
  70. this.populate();
  71. });
  72. this.subscribeToEvent(EditorEvents.SceneClosed, (ev: EditorEvents.SceneClosedEvent) => {
  73. if (ev.scene == this.scene) {
  74. this.unsubscribeFromEvents(this.scene);
  75. this.scene = null;
  76. this.populate();
  77. }
  78. });
  79. this.subscribeToEvent("ComponentAdded", (ev: Atomic.ComponentAddedEvent) => {
  80. if (!ev.component || ev.component.typeName != "PrefabComponent") return;
  81. var node = ev.node;
  82. var itemID = this.nodeIDToItemID[node.id];
  83. if (itemID) {
  84. this.hierList.setItemTextSkin(node.id.toString(), "HierarchyPrefabText");
  85. }
  86. });
  87. this.subscribeToEvent("ComponentRemoved", (ev: Atomic.ComponentRemovedEvent) => {
  88. if (!ev.component || ev.component.typeName != "PrefabComponent") return;
  89. var node = ev.node;
  90. var itemID = this.nodeIDToItemID[node.id];
  91. if (itemID) {
  92. this.hierList.setItemTextSkin(node.id.toString(), "Folder");
  93. }
  94. });
  95. this.subscribeToEvent("TemporaryChanged", (ev: Atomic.TemporaryChangedEvent) => {
  96. // this can happen on a temporary status change on a non-scripted class instance
  97. if (!ev.serializable) {
  98. return;
  99. }
  100. if (ev.serializable.typeName == "Node") {
  101. var node = <Atomic.Node>ev.serializable;
  102. var itemID = this.nodeIDToItemID[node.id];
  103. if (itemID) {
  104. this.hierList.setItemIcon(node.id.toString(), node.isTemporary() ? IconTemporary : "");
  105. }
  106. }
  107. });
  108. // Activates search while user is typing in search widget
  109. this.searchEdit.subscribeToEvent(this.searchEdit, "WidgetEvent", (data) => {
  110. if (!ToolCore.toolSystem.project) return;
  111. if (data.type == Atomic.UI_EVENT_TYPE_KEY_UP) {
  112. this.search = true;
  113. this.populate();
  114. if (this.searchEdit.text == "" && this.selectedNode) {
  115. this.hierList.selectItemByID(this.selectedNode.id.toString(), true); //maintains selected item after search is cancelled
  116. }
  117. }
  118. });
  119. }
  120. handleSceneEditNodeAdded(ev: Editor.SceneEditNodeAddedEvent) {
  121. var node = ev.node;
  122. if (this.filterNode(node))
  123. return;
  124. var parentID = this.nodeIDToItemID[node.parent.id];
  125. var childItemID = this.recursiveAddNode(parentID, node);
  126. this.nodeIDToItemID[node.id] = childItemID;
  127. }
  128. handleSceneEditNodeRemoved(ev: Editor.SceneEditNodeRemovedEvent) {
  129. // on close
  130. if (!this.scene)
  131. return;
  132. var node = ev.node;
  133. if (this.filterNode(node))
  134. return;
  135. delete this.nodeIDToItemID[node.id];
  136. this.hierList.deleteItemByID(node.id.toString());
  137. }
  138. handleActiveSceneEditorChanged(event: EditorEvents.ActiveSceneEditorChangeEvent) {
  139. if (this.scene)
  140. this.unsubscribeFromEvents(this.scene);
  141. this.sceneEditor = null;
  142. this.scene = null;
  143. if (!event.sceneEditor)
  144. return;
  145. this.sceneEditor = event.sceneEditor;
  146. this.scene = event.sceneEditor.scene;
  147. this.populate();
  148. if (this.scene) {
  149. this.subscribeToEvent(this.scene, "SceneNodeSelected", (event: Editor.SceneNodeSelectedEvent) => this.handleSceneNodeSelected(event));
  150. this.subscribeToEvent(this.scene, "SceneEditNodeAdded", (ev: Editor.SceneEditNodeAddedEvent) => this.handleSceneEditNodeAdded(ev));
  151. this.subscribeToEvent(this.scene, "SceneEditNodeRemoved", (ev: Editor.SceneEditNodeRemovedEvent) => this.handleSceneEditNodeRemoved(ev));
  152. this.subscribeToEvent(this.scene, "NodeNameChanged", (ev: Atomic.NodeNameChangedEvent) => {
  153. this.hierList.setItemText(ev.node.id.toString(), ev.node.name);
  154. });
  155. this.subscribeToEvent(this.scene, "SceneEditNodeReparent", (ev) => {
  156. if (!ev.added) {
  157. delete this.nodeIDToItemID[ev.node.id];
  158. this.hierList.deleteItemByID(ev.node.id.toString());
  159. } else {
  160. var parentID = this.nodeIDToItemID[ev.node.parent.id];
  161. var childItemID = this.recursiveAddNode(parentID, ev.node);
  162. this.nodeIDToItemID[ev.node.id] = childItemID;
  163. }
  164. });
  165. }
  166. }
  167. filterNode(node: Atomic.Node): boolean {
  168. if (!node) return false;
  169. if (node.name == "__atomic_sceneview3d_camera") return true;
  170. return false;
  171. }
  172. recursiveAddNode(parentID: number, node: Atomic.Node): number {
  173. if (this.filterNode(node))
  174. return;
  175. var name = node.name;
  176. if (!name.length)
  177. name = "(Anonymous)";
  178. var icon = "";
  179. if (node.isTemporary())
  180. icon = IconTemporary;
  181. var childItemID = this.hierList.addChildItem(parentID, name, icon, node.id.toString());
  182. if (node.getComponent("PrefabComponent")) {
  183. this.hierList.setItemTextSkin(node.id.toString(), "HierarchyPrefabText");
  184. }
  185. this.nodeIDToItemID[node.id] = childItemID;
  186. for (var i = 0; i < node.getNumChildren(false); i++) {
  187. this.recursiveAddNode(childItemID, node.getChildAtIndex(i));
  188. }
  189. return childItemID;
  190. }
  191. // Searches folders within folders recursively
  192. searchHierarchyFolder(asset: Atomic.Node, parentID: number, searchText: string) {
  193. for (var i = 0; i < asset.getNumChildren(false); i++) {
  194. var childAsset = asset.getChildAtIndex(i);
  195. if (this.uiSearchBar.searchPopulate(searchText, childAsset.name)) {
  196. this.recursiveAddNode(parentID, childAsset);
  197. }
  198. if (childAsset.getNumChildren(false) > 0) {
  199. this.searchHierarchyFolder(childAsset, parentID, searchText);
  200. }
  201. }
  202. }
  203. // Populates frame when the search bar is used
  204. populate() {
  205. this.nodeIDToItemID = {};
  206. this.hierList.deleteAllItems();
  207. if (!this.scene)
  208. return;
  209. var parentID = this.hierList.addRootItem("Scene", "", this.scene.id.toString());
  210. this.nodeIDToItemID[this.scene.id] = parentID;
  211. var asset = this.scene;
  212. if (this.searchEdit.text == "" || !this.search) {
  213. for (var i = 0; i < this.scene.getNumChildren(false); i++) {
  214. this.recursiveAddNode(parentID, this.scene.getChildAtIndex(i));
  215. }
  216. } else if (this.search) {
  217. this.searchHierarchyFolder(asset, parentID, this.searchEdit.text);
  218. }
  219. this.hierList.rootList.value = -1;
  220. this.hierList.setExpanded(parentID, true);
  221. }
  222. handleDragEnded(ev: Atomic.DragEndedEvent) {
  223. if (!ev.dragObject.object)
  224. return;
  225. var dropNode: Atomic.Node = this.scene.getNode(Number(this.hierList.hoverItemID));
  226. if (!dropNode) return;
  227. var dragNodeTypeName = ev.dragObject.object.typeName;
  228. if (!this.canReparent)
  229. return;
  230. if (dragNodeTypeName == "Node") {
  231. if (this.sceneEditor.selection.getSelectedNodeCount() < 2) {
  232. var dragNode = <Atomic.Node>ev.dragObject.object;
  233. this.sceneEditor.reparentNode(dragNode, dropNode);
  234. } else {
  235. var tempSelectedList = [];
  236. for (var i = 0; i < this.sceneEditor.selection.getSelectedNodeCount(); i++)
  237. tempSelectedList.push(this.sceneEditor.selection.getSelectedNode(i));
  238. for (var j in tempSelectedList) {
  239. var tempNode = tempSelectedList[j];
  240. var typeName = tempNode.typeName;
  241. if (typeName == "Node")
  242. this.sceneEditor.reparentNode(tempNode, dropNode);
  243. }
  244. }
  245. } else if (dragNodeTypeName == "Asset") {
  246. var asset = <ToolCore.Asset>ev.dragObject.object;
  247. var newNode = asset.instantiateNode(dropNode, asset.name);
  248. if (newNode) {
  249. this.sceneEditor.registerNode(newNode);
  250. // getting a click event after this (I think) which
  251. // is causing the dropNode to be selected
  252. this.sceneEditor.selection.addNode(newNode, true);
  253. }
  254. }
  255. }
  256. handleSceneNodeSelected(ev: Editor.SceneNodeSelectedEvent) {
  257. this.selectedNode = ev.node; //Stores selection for when the search is cancelled
  258. this.hierList.selectItemByID(ev.node.id.toString(), ev.selected);
  259. }
  260. handleHierListSelectionChangedEvent(event: Atomic.UIListViewSelectionChangedEvent) {
  261. if (!this.scene)
  262. return;
  263. var node = this.scene.getNode(Number(event.refid));
  264. if (node) {
  265. if (event.selected)
  266. this.sceneEditor.selection.addNode(node);
  267. else
  268. this.sceneEditor.selection.removeNode(node);
  269. }
  270. }
  271. handleWidgetEvent(data: Atomic.UIWidgetEvent): boolean {
  272. if (data.type == Atomic.UI_EVENT_TYPE_KEY_UP) {
  273. // node deletion
  274. if (data.key == Atomic.KEY_DELETE || data.key == Atomic.KEY_BACKSPACE) {
  275. this.sceneEditor.selection.delete();
  276. }
  277. } else if (data.type == Atomic.UI_EVENT_TYPE_POINTER_DOWN) {
  278. if (data.target == this.hierList.rootList) {
  279. var node = this.scene.getNode(Number(data.refid));
  280. if (node) {
  281. // set the widget's drag object
  282. var dragObject = new Atomic.UIDragObject(node, node.name.length ? "Node: " + node.name : "Node: (Anonymous)");
  283. this.hierList.rootList.dragObject = dragObject;
  284. }
  285. }
  286. } else if (data.type == Atomic.UI_EVENT_TYPE_CLICK) {
  287. if (this.menu.handleNodeContextMenu(data.target, data.refid)) {
  288. return true;
  289. }
  290. var id = data.target.id;
  291. if (id == "create popup") {
  292. var selectedId = Number(this.hierList.rootList.selectedItemID);
  293. var node = this.scene.getNode(selectedId);
  294. if (!node)
  295. node = this.scene;
  296. if (this.menu.handlePopupMenu(data.target, data.refid, node))
  297. return true;
  298. }
  299. // create
  300. if (id == "menu create") {
  301. if (!ToolCore.toolSystem.project) return;
  302. var src = MenuItemSources.getMenuItemSource("hierarchy create items");
  303. var menu = new Atomic.UIMenuWindow(data.target, "create popup");
  304. menu.show(src);
  305. return true;
  306. }
  307. // cancel search
  308. if (id == "cancel search") {
  309. if (!this.scene) {
  310. this.searchEdit.text = "";
  311. return;
  312. }
  313. if (!ToolCore.toolSystem.project) return;
  314. this.searchEdit.text = "";
  315. this.populate();
  316. this.search = false;
  317. if (this.selectedNode) {
  318. this.hierList.selectItemByID(this.selectedNode.id.toString(), true); //maintains selected item after search is cancelled
  319. }
  320. }
  321. } else if (data.type == Atomic.UI_EVENT_TYPE_RIGHT_POINTER_UP) {
  322. var id = data.target.id;
  323. var node: Atomic.Node;
  324. if (id == "hierList_")
  325. node = this.scene.getNode(Number(this.hierList.hoverItemID));
  326. else
  327. node = this.scene.getNode(Number(id));
  328. if (node) {
  329. this.menu.createNodeContextMenu(this, node, data.x, data.y);
  330. }
  331. }
  332. return false;
  333. }
  334. }
  335. export = HierarchyFrame;