HierarchyFrame.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import HierarchyFrameMenu = require("./menus/HierarchyFrameMenu");
  2. import MenuItemSources = require("./menus/MenuItemSources");
  3. import EditorEvents = require("editor/EditorEvents");
  4. var IconTemporary = "ComponentBitmap";
  5. class HierarchyFrame extends Atomic.UIWidget {
  6. scene: Atomic.Scene = null;
  7. hierList: Atomic.UIListView;
  8. menu: HierarchyFrameMenu;
  9. nodeIDToItemID = {};
  10. constructor(parent: Atomic.UIWidget) {
  11. super();
  12. this.menu = new HierarchyFrameMenu();
  13. this.load("AtomicEditor/editor/ui/hierarchyframe.tb.txt");
  14. this.gravity = Atomic.UI_GRAVITY_TOP_BOTTOM;
  15. var hierarchycontainer = parent.getWidget("hierarchycontainer");
  16. hierarchycontainer.addChild(this);
  17. hierarchycontainer = this.getWidget("hierarchycontainer");
  18. var hierList = this.hierList = new Atomic.UIListView();
  19. hierList.rootList.id = "hierList_";
  20. hierarchycontainer.addChild(hierList);
  21. this.subscribeToEvent(this, "WidgetEvent", (data) => this.handleWidgetEvent(data));
  22. this.subscribeToEvent(EditorEvents.ActiveNodeChange, (data) => {
  23. if (data.node)
  24. this.hierList.selectItemByID(data.node.id.toString());
  25. });
  26. this.subscribeToEvent(EditorEvents.ActiveSceneChange, (data) => this.handleActiveSceneChanged(data));
  27. this.subscribeToEvent(EditorEvents.SceneClosed, (ev: EditorEvents.SceneClosedEvent) => {
  28. if (ev.scene == this.scene) {
  29. this.unsubscribeFromEvents(this.scene);
  30. this.scene = null;
  31. this.populate();
  32. }
  33. });
  34. this.subscribeToEvent("ComponentAdded", (ev: Atomic.ComponentAddedEvent) => {
  35. if (!ev.component || ev.component.typeName != "PrefabComponent") return;
  36. var node = ev.node;
  37. var itemID = this.nodeIDToItemID[node.id];
  38. if (itemID) {
  39. this.hierList.setItemTextSkin(node.id.toString(), "HierarchyPrefabText");
  40. }
  41. });
  42. this.subscribeToEvent("ComponentRemoved", (ev: Atomic.ComponentRemovedEvent) => {
  43. if (!ev.component || ev.component.typeName != "PrefabComponent") return;
  44. var node = ev.node;
  45. var itemID = this.nodeIDToItemID[node.id];
  46. if (itemID) {
  47. this.hierList.setItemTextSkin(node.id.toString(), "Folder");
  48. }
  49. });
  50. this.subscribeToEvent("TemporaryChanged", (ev: Atomic.TemporaryChangedEvent) => {
  51. // this can happen on a temporary status change on a non-scripted class instance
  52. if (!ev.serializable) {
  53. return;
  54. }
  55. if (ev.serializable.typeName == "Node") {
  56. var node = <Atomic.Node> ev.serializable;
  57. var itemID = this.nodeIDToItemID[node.id];
  58. if (itemID) {
  59. this.hierList.setItemIcon(node.id.toString(), node.isTemporary() ? IconTemporary : "");
  60. }
  61. }
  62. });
  63. }
  64. handleNodeAdded(ev: Atomic.NodeAddedEvent) {
  65. var node = ev.node;
  66. if (this.filterNode(node))
  67. return;
  68. if (!node.parent || node.scene != this.scene)
  69. return;
  70. var parentID = this.nodeIDToItemID[node.parent.id];
  71. var childItemID = this.recursiveAddNode(parentID, node);
  72. this.nodeIDToItemID[node.id] = childItemID;
  73. }
  74. handleNodeRemoved(ev: Atomic.NodeRemovedEvent) {
  75. // on close
  76. if (!this.scene)
  77. return;
  78. var node = ev.node;
  79. if (this.filterNode(node))
  80. return;
  81. delete this.nodeIDToItemID[node.id];
  82. if (!node.parent || node.scene != this.scene)
  83. return;
  84. this.hierList.deleteItemByID(node.id.toString());
  85. var selectedId = Number(this.hierList.rootList.selectedItemID);
  86. var selectedNode = this.scene.getNode(selectedId);
  87. if (selectedNode == node) {
  88. this.sendEvent(EditorEvents.ActiveNodeChange, { node: ev.parent ? ev.parent : this.scene });
  89. }
  90. }
  91. handleActiveSceneChanged(data) {
  92. if (this.scene)
  93. this.unsubscribeFromEvents(this.scene);
  94. // clear selected node
  95. this.sendEvent(EditorEvents.ActiveNodeChange, { node: null });
  96. this.scene = <Atomic.Scene> data.scene;
  97. this.populate();
  98. if (this.scene) {
  99. this.subscribeToEvent(this.scene, "NodeAdded", (ev: Atomic.NodeAddedEvent) => this.handleNodeAdded(ev));
  100. this.subscribeToEvent(this.scene, "NodeRemoved", (ev: Atomic.NodeRemovedEvent) => this.handleNodeRemoved(ev));
  101. this.subscribeToEvent(this.scene, "NodeNameChanged", (ev: Atomic.NodeNameChangedEvent) => {
  102. this.hierList.setItemText(ev.node.id.toString(), ev.node.name);
  103. });
  104. }
  105. }
  106. handleWidgetEvent(data: Atomic.UIWidgetEvent): boolean {
  107. if (data.type == Atomic.UI_EVENT_TYPE_KEY_UP) {
  108. if (data.key == Atomic.KEY_DELETE || data.key == Atomic.KEY_BACKSPACE) {
  109. var selectedId = Number(this.hierList.rootList.selectedItemID);
  110. var node = this.scene.getNode(selectedId);
  111. if (node) {
  112. node.removeAllComponents();
  113. node.remove();
  114. }
  115. }
  116. }
  117. if (data.type == Atomic.UI_EVENT_TYPE_POINTER_DOWN) {
  118. if (data.target == this.hierList.rootList) {
  119. var node = this.scene.getNode(Number(data.refid));
  120. if (node) {
  121. // set the widget's drag object
  122. var dragObject = new Atomic.UIDragObject(node, node.name.length ? "Node: " + node.name : "Node: (Anonymous)");
  123. this.hierList.rootList.dragObject = dragObject;
  124. // handle dropping on hierarchy, moving node
  125. this.subscribeToEvent(this.hierList.rootList, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  126. var dragNode = <Atomic.Node> ev.dragObject.object;
  127. var dropNode: Atomic.Node = this.scene.getNode(Number(this.hierList.hoverItemID));
  128. if (!dropNode) {
  129. return;
  130. }
  131. // can't drop on self
  132. if (dragNode == dropNode) {
  133. return;
  134. }
  135. // check if dropping on child of ourselves
  136. var parent = dropNode.parent;
  137. while (parent) {
  138. if (parent == dragNode) {
  139. return;
  140. }
  141. parent = parent.parent;
  142. }
  143. // move it
  144. dropNode.addChild(dragNode);
  145. });
  146. }
  147. }
  148. }
  149. if (data.type == Atomic.UI_EVENT_TYPE_CLICK) {
  150. var id = data.target.id;
  151. if (id == "create popup") {
  152. var selectedId = Number(this.hierList.rootList.selectedItemID);
  153. var node = this.scene.getNode(selectedId);
  154. if (this.menu.handlePopupMenu(data.target, data.refid, node))
  155. return true;
  156. }
  157. // create
  158. if (id == "menu create") {
  159. if (!ToolCore.toolSystem.project) return;
  160. var src = MenuItemSources.getMenuItemSource("hierarchy create items");
  161. var menu = new Atomic.UIMenuWindow(data.target, "create popup");
  162. menu.show(src);
  163. return true;
  164. }
  165. if (id == "hierList_") {
  166. var list = <Atomic.UISelectList> data.target;
  167. var selectedId = Number(list.selectedItemID);
  168. var node = this.scene.getNode(selectedId);
  169. if (node) {
  170. this.sendEvent("EditorActiveNodeChange", { node: node });
  171. }
  172. return false;
  173. }
  174. }
  175. return false;
  176. }
  177. filterNode(node: Atomic.Node): boolean {
  178. if (!node) return false;
  179. if (node.name == "__atomic_sceneview3d_camera") return true;
  180. return false;
  181. }
  182. recursiveAddNode(parentID: number, node: Atomic.Node): number {
  183. if (this.filterNode(node))
  184. return;
  185. var name = node.name;
  186. if (!name.length)
  187. name = "(Anonymous)"
  188. var icon = "";
  189. if (node.isTemporary())
  190. icon = IconTemporary;
  191. var childItemID = this.hierList.addChildItem(parentID, name, icon, node.id.toString());
  192. if (node.getComponent("PrefabComponent")) {
  193. this.hierList.setItemTextSkin(node.id.toString(), "HierarchyPrefabText");
  194. }
  195. this.nodeIDToItemID[node.id] = childItemID;
  196. for (var i = 0; i < node.getNumChildren(false); i++) {
  197. this.recursiveAddNode(childItemID, node.getChildAtIndex(i));
  198. }
  199. return childItemID;
  200. }
  201. populate() {
  202. this.nodeIDToItemID = {};
  203. this.hierList.deleteAllItems();
  204. if (!this.scene)
  205. return;
  206. var parentID = this.hierList.addRootItem("Scene", "", this.scene.id.toString());
  207. this.nodeIDToItemID[this.scene.id] = parentID;
  208. for (var i = 0; i < this.scene.getNumChildren(false); i++) {
  209. this.recursiveAddNode(parentID, this.scene.getChildAtIndex(i));
  210. }
  211. this.hierList.rootList.value = 0;
  212. this.hierList.setExpanded(parentID, true);
  213. }
  214. }
  215. export = HierarchyFrame;