HierarchyFrame.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import HierarchyFrameMenu = require("./HierarchyFrameMenu");
  2. import MenuItemSources = require("./menus/MenuItemSources");
  3. import EditorEvents = require("../editor/EditorEvents");
  4. class HierarchyFrame extends Atomic.UIWidget {
  5. scene: Atomic.Scene = null;
  6. hierList: Atomic.UIListView;
  7. menu: HierarchyFrameMenu;
  8. nodeIDToItemID = {};
  9. constructor(parent: Atomic.UIWidget) {
  10. super();
  11. this.menu = new HierarchyFrameMenu();
  12. this.load("AtomicEditor/editor/ui/hierarchyframe.tb.txt");
  13. this.gravity = Atomic.UI_GRAVITY_TOP_BOTTOM;
  14. var hierarchycontainer = parent.getWidget("hierarchycontainer");
  15. hierarchycontainer.addChild(this);
  16. hierarchycontainer = this.getWidget("hierarchycontainer");
  17. var hierList = this.hierList = new Atomic.UIListView();
  18. hierList.rootList.id = "hierList_";
  19. hierarchycontainer.addChild(hierList);
  20. this.subscribeToEvent(this, "WidgetEvent", (data) => this.handleWidgetEvent(data));
  21. this.subscribeToEvent(EditorEvents.ActiveNodeChange, (data) => {
  22. if (data.node)
  23. this.hierList.selectItemByID(data.node.id.toString());
  24. });
  25. this.subscribeToEvent(EditorEvents.ActiveSceneChange, (data) => this.handleActiveSceneChanged(data));
  26. this.subscribeToEvent(EditorEvents.SceneClosed, (ev: EditorEvents.SceneClosedEvent) => {
  27. if (ev.scene == this.scene) {
  28. this.unsubscribeFromEvents(this.scene);
  29. this.scene = null;
  30. this.populate();
  31. }
  32. });
  33. }
  34. handleNodeAdded(ev: Atomic.NodeAddedEvent) {
  35. var node = ev.node;
  36. if (this.filterNode(node))
  37. return;
  38. if (!node.parent || node.scene != this.scene)
  39. return;
  40. var parentID = this.nodeIDToItemID[node.parent.id];
  41. var childItemID = this.recursiveAddNode(parentID, node);
  42. this.nodeIDToItemID[node.id] = childItemID;
  43. }
  44. handleNodeRemoved(ev: Atomic.NodeRemovedEvent) {
  45. // on close
  46. if (!this.scene)
  47. return;
  48. var node = ev.node;
  49. if (this.filterNode(node))
  50. return;
  51. delete this.nodeIDToItemID[node.id];
  52. if (!node.parent || node.scene != this.scene)
  53. return;
  54. this.hierList.deleteItemByID(node.id.toString());
  55. this.sendEvent(EditorEvents.ActiveNodeChange, { node: ev.parent ? ev.parent : this.scene });
  56. }
  57. handleActiveSceneChanged(data) {
  58. if (this.scene)
  59. this.unsubscribeFromEvents(this.scene);
  60. this.scene = <Atomic.Scene> data.scene;
  61. this.populate();
  62. if (this.scene) {
  63. this.subscribeToEvent(this.scene, "NodeAdded", (ev: Atomic.NodeAddedEvent) => this.handleNodeAdded(ev));
  64. this.subscribeToEvent(this.scene, "NodeRemoved", (ev: Atomic.NodeRemovedEvent) => this.handleNodeRemoved(ev));
  65. this.subscribeToEvent(this.scene, "NodeNameChanged", (ev: Atomic.NodeNameChangedEvent) => {
  66. this.hierList.setItemText(ev.node.id.toString(), ev.node.name);
  67. });
  68. }
  69. }
  70. handleWidgetEvent(data: Atomic.UIWidgetEvent): boolean {
  71. if (data.type == Atomic.UI_EVENT_TYPE_POINTER_DOWN) {
  72. if (data.target == this.hierList.rootList) {
  73. var node = this.scene.getNode(Number(data.refid));
  74. if (node) {
  75. // set the widget's drag object
  76. var dragObject = new Atomic.UIDragObject(node, node.name.length ? "Node: " + node.name : "Node: (Anonymous)");
  77. this.hierList.rootList.dragObject = dragObject;
  78. // handle dropping on hierarchy, moving node
  79. this.subscribeToEvent(dragObject, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  80. var dragNode = <Atomic.Node> ev.dragObject.object;
  81. var dropNode: Atomic.Node = this.scene.getNode(Number(this.hierList.hoverItemID));
  82. if (!dropNode) {
  83. return;
  84. }
  85. // can't drop on self
  86. if (dragNode == dropNode) {
  87. return;
  88. }
  89. // check if dropping on child of ourselves
  90. var parent = dropNode.parent;
  91. while (parent) {
  92. if (parent == dragNode) {
  93. return;
  94. }
  95. parent = parent.parent;
  96. }
  97. // move it
  98. dropNode.addChild(dragNode);
  99. });
  100. }
  101. }
  102. }
  103. if (data.type == Atomic.UI_EVENT_TYPE_CLICK) {
  104. var id = data.target.id;
  105. if (this.menu.handlePopupMenu(data.target, data.refid))
  106. return true;
  107. if (id == "create popup") {
  108. if (data.refid == "create_node") {
  109. var selectedId = Number(this.hierList.rootList.selectedItemID);
  110. var node = this.scene.getNode(selectedId);
  111. node.createChild("Node");
  112. }
  113. }
  114. // create
  115. if (id == "menu create") {
  116. var src = MenuItemSources.getMenuItemSource("hierarchy create items");
  117. var menu = new Atomic.UIMenuWindow(data.target, "create popup");
  118. menu.show(src);
  119. return true;
  120. }
  121. if (id == "hierList_") {
  122. var list = <Atomic.UISelectList> data.target;
  123. var selectedId = Number(list.selectedItemID);
  124. var node = this.scene.getNode(selectedId);
  125. if (node) {
  126. this.sendEvent("EditorActiveNodeChange", { node: node });
  127. }
  128. return false;
  129. }
  130. }
  131. return false;
  132. }
  133. filterNode(node: Atomic.Node): boolean {
  134. if (!node) return false;
  135. if (node.name == "__atomic_sceneview3d_camera") return true;
  136. return false;
  137. }
  138. recursiveAddNode(parentID: number, node: Atomic.Node): number {
  139. if (this.filterNode(node))
  140. return;
  141. var name = node.name;
  142. if (!name.length)
  143. name = "(Anonymous)"
  144. var icon = "";
  145. if (node.isTemporary())
  146. icon = "ComponentBitmap";
  147. var childItemID = this.hierList.addChildItem(parentID, name, icon, node.id.toString());
  148. this.nodeIDToItemID[node.id] = childItemID;
  149. for (var i = 0; i < node.getNumChildren(false); i++) {
  150. this.recursiveAddNode(childItemID, node.getChildAtIndex(i));
  151. }
  152. return childItemID;
  153. }
  154. populate() {
  155. this.nodeIDToItemID = {};
  156. this.hierList.deleteAllItems();
  157. if (!this.scene)
  158. return;
  159. var parentID = this.hierList.addRootItem("Scene", "", this.scene.id.toString());
  160. this.nodeIDToItemID[this.scene.id] = parentID;
  161. for (var i = 0; i < this.scene.getNumChildren(false); i++) {
  162. this.recursiveAddNode(parentID, this.scene.getChildAtIndex(i));
  163. }
  164. this.hierList.rootList.value = 0;
  165. this.hierList.setExpanded(parentID, true);
  166. }
  167. }
  168. export = HierarchyFrame;