| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- import HierarchyFrameMenu = require("./HierarchyFrameMenu");
- import MenuItemSources = require("./menus/MenuItemSources");
- import EditorEvents = require("../editor/EditorEvents");
- var IconTemporary = "ComponentBitmap";
- class HierarchyFrame extends Atomic.UIWidget {
- scene: Atomic.Scene = null;
- hierList: Atomic.UIListView;
- menu: HierarchyFrameMenu;
- nodeIDToItemID = {};
- constructor(parent: Atomic.UIWidget) {
- super();
- this.menu = new HierarchyFrameMenu();
- this.load("AtomicEditor/editor/ui/hierarchyframe.tb.txt");
- this.gravity = Atomic.UI_GRAVITY_TOP_BOTTOM;
- var hierarchycontainer = parent.getWidget("hierarchycontainer");
- hierarchycontainer.addChild(this);
- hierarchycontainer = this.getWidget("hierarchycontainer");
- var hierList = this.hierList = new Atomic.UIListView();
- hierList.rootList.id = "hierList_";
- hierarchycontainer.addChild(hierList);
- this.subscribeToEvent(this, "WidgetEvent", (data) => this.handleWidgetEvent(data));
- this.subscribeToEvent(EditorEvents.ActiveNodeChange, (data) => {
- if (data.node)
- this.hierList.selectItemByID(data.node.id.toString());
- });
- this.subscribeToEvent(EditorEvents.ActiveSceneChange, (data) => this.handleActiveSceneChanged(data));
- this.subscribeToEvent(EditorEvents.SceneClosed, (ev: EditorEvents.SceneClosedEvent) => {
- if (ev.scene == this.scene) {
- this.unsubscribeFromEvents(this.scene);
- this.scene = null;
- this.populate();
- }
- });
- this.subscribeToEvent("TemporaryChanged", (ev: Atomic.TemporaryChangedEvent) => {
- if (ev.serializable.typeName == "Node") {
- var node = <Atomic.Node> ev.serializable;
- var itemID = this.nodeIDToItemID[node.id];
- if (itemID) {
- this.hierList.setItemIcon(node.id.toString(), node.isTemporary() ? IconTemporary : "");
- }
- }
- });
- }
- handleNodeAdded(ev: Atomic.NodeAddedEvent) {
- var node = ev.node;
- if (this.filterNode(node))
- return;
- if (!node.parent || node.scene != this.scene)
- return;
- var parentID = this.nodeIDToItemID[node.parent.id];
- var childItemID = this.recursiveAddNode(parentID, node);
- this.nodeIDToItemID[node.id] = childItemID;
- }
- handleNodeRemoved(ev: Atomic.NodeRemovedEvent) {
- // on close
- if (!this.scene)
- return;
- var node = ev.node;
- if (this.filterNode(node))
- return;
- delete this.nodeIDToItemID[node.id];
- if (!node.parent || node.scene != this.scene)
- return;
- this.hierList.deleteItemByID(node.id.toString());
- var selectedId = Number(this.hierList.rootList.selectedItemID);
- var selectedNode = this.scene.getNode(selectedId);
- if (selectedNode == node) {
- this.sendEvent(EditorEvents.ActiveNodeChange, { node: ev.parent ? ev.parent : this.scene });
- }
- }
- handleActiveSceneChanged(data) {
- if (this.scene)
- this.unsubscribeFromEvents(this.scene);
- // clear selected node
- this.sendEvent(EditorEvents.ActiveNodeChange, { node: null });
- this.scene = <Atomic.Scene> data.scene;
- this.populate();
- if (this.scene) {
- this.subscribeToEvent(this.scene, "NodeAdded", (ev: Atomic.NodeAddedEvent) => this.handleNodeAdded(ev));
- this.subscribeToEvent(this.scene, "NodeRemoved", (ev: Atomic.NodeRemovedEvent) => this.handleNodeRemoved(ev));
- this.subscribeToEvent(this.scene, "NodeNameChanged", (ev: Atomic.NodeNameChangedEvent) => {
- this.hierList.setItemText(ev.node.id.toString(), ev.node.name);
- });
- }
- }
- handleWidgetEvent(data: Atomic.UIWidgetEvent): boolean {
- if (data.type == Atomic.UI_EVENT_TYPE_KEY_UP) {
- if (data.key == Atomic.KEY_DELETE || data.key == Atomic.KEY_BACKSPACE) {
- var selectedId = Number(this.hierList.rootList.selectedItemID);
- var node = this.scene.getNode(selectedId);
- if (node) {
- node.removeAllComponents();
- node.remove();
- }
- }
- }
- if (data.type == Atomic.UI_EVENT_TYPE_POINTER_DOWN) {
- if (data.target == this.hierList.rootList) {
- var node = this.scene.getNode(Number(data.refid));
- if (node) {
- // set the widget's drag object
- var dragObject = new Atomic.UIDragObject(node, node.name.length ? "Node: " + node.name : "Node: (Anonymous)");
- this.hierList.rootList.dragObject = dragObject;
- // handle dropping on hierarchy, moving node
- this.subscribeToEvent(dragObject, "DragEnded", (ev: Atomic.DragEndedEvent) => {
- var dragNode = <Atomic.Node> ev.dragObject.object;
- var dropNode: Atomic.Node = this.scene.getNode(Number(this.hierList.hoverItemID));
- if (!dropNode) {
- return;
- }
- // can't drop on self
- if (dragNode == dropNode) {
- return;
- }
- // check if dropping on child of ourselves
- var parent = dropNode.parent;
- while (parent) {
- if (parent == dragNode) {
- return;
- }
- parent = parent.parent;
- }
- // move it
- dropNode.addChild(dragNode);
- });
- }
- }
- }
- if (data.type == Atomic.UI_EVENT_TYPE_CLICK) {
- var id = data.target.id;
- if (id == "create popup") {
- var selectedId = Number(this.hierList.rootList.selectedItemID);
- var node = this.scene.getNode(selectedId);
- if (this.menu.handlePopupMenu(data.target, data.refid, node))
- return true;
- }
- // create
- if (id == "menu create") {
- if (!ToolCore.toolSystem.project) return;
- var src = MenuItemSources.getMenuItemSource("hierarchy create items");
- var menu = new Atomic.UIMenuWindow(data.target, "create popup");
- menu.show(src);
- return true;
- }
- if (id == "hierList_") {
- var list = <Atomic.UISelectList> data.target;
- var selectedId = Number(list.selectedItemID);
- var node = this.scene.getNode(selectedId);
- if (node) {
- this.sendEvent("EditorActiveNodeChange", { node: node });
- }
- return false;
- }
- }
- return false;
- }
- filterNode(node: Atomic.Node): boolean {
- if (!node) return false;
- if (node.name == "__atomic_sceneview3d_camera") return true;
- return false;
- }
- recursiveAddNode(parentID: number, node: Atomic.Node): number {
- if (this.filterNode(node))
- return;
- var name = node.name;
- if (!name.length)
- name = "(Anonymous)"
- var icon = "";
- if (node.isTemporary())
- icon = IconTemporary;
- var childItemID = this.hierList.addChildItem(parentID, name, icon, node.id.toString());
- this.nodeIDToItemID[node.id] = childItemID;
- for (var i = 0; i < node.getNumChildren(false); i++) {
- this.recursiveAddNode(childItemID, node.getChildAtIndex(i));
- }
- return childItemID;
- }
- populate() {
- this.nodeIDToItemID = {};
- this.hierList.deleteAllItems();
- if (!this.scene)
- return;
- var parentID = this.hierList.addRootItem("Scene", "", this.scene.id.toString());
- this.nodeIDToItemID[this.scene.id] = parentID;
- for (var i = 0; i < this.scene.getNumChildren(false); i++) {
- this.recursiveAddNode(parentID, this.scene.getChildAtIndex(i));
- }
- this.hierList.rootList.value = 0;
- this.hierList.setExpanded(parentID, true);
- }
- }
- export = HierarchyFrame;
|