/// <summary> /// /// </summary> /// <param name="folderNode"></param> /// <param name="index"></param> /// <returns></returns> int CheckChildren(GTreeNode folderNode, int index) { int cnt = folderNode.numChildren; for (int i = 0; i < cnt; i++) { index++; GTreeNode node = folderNode.GetChildAt(i); if (node.cell == null) { CreateCell(node); } if (node.cell.parent == null) { AddChildAt(node.cell, index); } if (node.isFolder && node.expanded) { index = CheckChildren(node, index); } } return(index); }
void __expandedStateChanged(EventContext context) { Controller cc = (Controller)context.sender; GTreeNode node = cc.parent._treeNode; node.expanded = cc.selectedIndex == 1; }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <returns></returns> int GetInsertIndexForNode(GTreeNode node) { GTreeNode prevNode = node.GetPrevSibling(); if (prevNode == null) { prevNode = node.parent; } int insertIndex = GetChildIndex(prevNode.cell) + 1; int myLevel = node.level; int cnt = this.numChildren; for (int i = insertIndex; i < cnt; i++) { GTreeNode testNode = GetChildAt(i)._treeNode; if (testNode.level <= myLevel) { break; } insertIndex++; } return(insertIndex); }
/// <summary> /// /// </summary> /// <param name="node"></param> internal void _AfterExpanded(GTreeNode node) { if (node == _rootNode) { CheckChildren(_rootNode, 0); return; } if (this.treeNodeWillExpand != null) { this.treeNodeWillExpand(node, true); } if (node._cell == null) { return; } if (this.treeNodeRender != null) { this.treeNodeRender(node, node._cell); } Controller cc = node._cell.GetController("expanded"); if (cc != null) { cc.selectedIndex = 1; } if (node._cell.parent != null) { CheckChildren(node, GetChildIndex(node._cell)); } }
/// <summary> /// /// </summary> /// <param name="child"></param> /// <param name="index"></param> public void SetChildIndex(GTreeNode child, int index) { int oldIndex = _children.IndexOf(child); if (oldIndex == -1) { throw new Exception("Not a child of this container"); } int cnt = _children.Count; if (index < 0) { index = 0; } else if (index > cnt) { index = cnt; } if (oldIndex == index) { return; } _children.RemoveAt(oldIndex); _children.Insert(index, child); if (tree != null && this == tree.rootNode || _cell != null && _cell.parent != null && _expanded) { tree._AfterMoved(child); } }
/// <summary> /// /// </summary> /// <param name="node"></param> internal void _AfterCollapsed(GTreeNode node) { if (node == _rootNode) { CheckChildren(_rootNode, 0); return; } if (this.treeNodeWillExpand != null) { this.treeNodeWillExpand(node, false); } if (node._cell == null) { return; } if (this.treeNodeRender != null) { this.treeNodeRender(node, node._cell); } Controller cc = node._cell.GetController("expanded"); if (cc != null) { cc.selectedIndex = 0; } if (node._cell.parent != null) { HideFolderNode(node); } }
/// <summary> /// /// </summary> /// <param name="node"></param> internal void _AfterMoved(GTreeNode node) { int startIndex = GetChildIndex(node._cell); int endIndex; if (node.isFolder) { endIndex = GetFolderEndIndex(startIndex, node.level); } else { endIndex = startIndex + 1; } int insertIndex = GetInsertIndexForNode(node); int cnt = endIndex - startIndex; if (insertIndex < startIndex) { for (int i = 0; i < cnt; i++) { GObject obj = GetChildAt(startIndex + i); SetChildIndex(obj, insertIndex + i); } } else { for (int i = 0; i < cnt; i++) { GObject obj = GetChildAt(startIndex); SetChildIndex(obj, insertIndex); } } }
/// <summary> /// /// </summary> public GTree() { _indent = 30; _rootNode = new GTreeNode(true); _rootNode._SetTree(this); _rootNode.expanded = true; }
/// <summary> /// /// </summary> /// <param name="index1"></param> /// <param name="index2"></param> public void SwapChildrenAt(int index1, int index2) { GTreeNode child1 = _children[index1]; GTreeNode child2 = _children[index2]; SetChildIndex(child1, index2); SetChildIndex(child2, index1); }
/// <summary> /// /// </summary> /// <param name="child"></param> /// <returns></returns> public GTreeNode RemoveChild(GTreeNode child) { int childIndex = _children.IndexOf(child); if (childIndex != -1) { RemoveChildAt(childIndex); } return(child); }
/// <summary> /// /// </summary> public void ExpandToRoot() { GTreeNode p = this; while (p != null) { p.expanded = true; p = p.parent; } }
/// <summary> /// /// </summary> /// <param name="child1"></param> /// <param name="child2"></param> public void SwapChildren(GTreeNode child1, GTreeNode child2) { int index1 = _children.IndexOf(child1); int index2 = _children.IndexOf(child2); if (index1 == -1 || index2 == -1) { throw new Exception("Not a child of this container"); } SwapChildrenAt(index1, index2); }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <param name="scrollItToView"></param> public void SelectNode(GTreeNode node, bool scrollItToView) { GTreeNode parentNode = node.parent; while (parentNode != null && parentNode != _rootNode) { parentNode.expanded = true; parentNode = parentNode.parent; } AddSelection(GetChildIndex(node.cell), scrollItToView); }
/// <summary> /// /// </summary> /// <param name="folderNode"></param> public void ExpandAll(GTreeNode folderNode) { folderNode.expanded = true; int cnt = folderNode.numChildren; for (int i = 0; i < cnt; i++) { GTreeNode node = folderNode.GetChildAt(i); if (node.isFolder) { ExpandAll(node); } } }
private int GetFolderEndIndex(int startIndex, int level) { int cnt = this.numChildren; for (int i = startIndex + 1; i < cnt; i++) { GTreeNode node = GetChildAt(i)._treeNode; if (node.level <= level) { return(i); } } return(cnt); }
/// <summary> /// /// </summary> /// <param name="child"></param> /// <param name="index"></param> /// <returns></returns> public GTreeNode AddChildAt(GTreeNode child, int index) { if (child == null) { throw new Exception("child is null"); } int numChildren = _children.Count; if (index >= 0 && index <= numChildren) { if (child.parent == this) { SetChildIndex(child, index); } else { if (child.parent != null) { child.parent.RemoveChild(child); } int cnt = _children.Count; if (index == cnt) { _children.Add(child); } else { _children.Insert(index, child); } child.parent = this; child._level = _level + 1; child._SetTree(this.tree); if (tree != null && this == tree.rootNode || _cell != null && _cell.parent != null && _expanded) { tree._AfterInserted(child); } } return(child); } else { throw new Exception("Invalid child index"); } }
/// <summary> /// /// </summary> /// <param name="result"></param> /// <returns></returns> public List <GTreeNode> GetSelectedNodes(List <GTreeNode> result) { if (result == null) { result = new List <GTreeNode>(); } helperIntList.Clear(); List <int> sels = GetSelection(helperIntList); int cnt = sels.Count; for (int i = 0; i < cnt; i++) { GTreeNode node = GetChildAt(sels[i])._treeNode; result.Add(node); } return(result); }
/// <summary> /// /// </summary> /// <param name="folderNode"></param> public void CollapseAll(GTreeNode folderNode) { if (folderNode != _rootNode) { folderNode.expanded = false; } int cnt = folderNode.numChildren; for (int i = 0; i < cnt; i++) { GTreeNode node = folderNode.GetChildAt(i); if (node.isFolder) { CollapseAll(node); } } }
/// <summary> /// /// </summary> /// <param name="folderNode"></param> void HideFolderNode(GTreeNode folderNode) { int cnt = folderNode.numChildren; for (int i = 0; i < cnt; i++) { GTreeNode node = folderNode.GetChildAt(i); if (node.cell != null && node.cell.parent != null) { RemoveChild(node.cell); } if (node.isFolder && node.expanded) { HideFolderNode(node); } } }
/// <summary> /// /// </summary> /// <param name="node"></param> void CreateCell(GTreeNode node) { GComponent child = itemPool.GetObject(string.IsNullOrEmpty(node._resURL) ? this.defaultItem : node._resURL) as GComponent; if (child == null) { throw new Exception("FairyGUI: cannot create tree node object."); } child.displayObject.home = this.displayObject.cachedTransform; child._treeNode = node; node._cell = child; GObject indentObj = node.cell.GetChild("indent"); if (indentObj != null) { indentObj.width = (node.level - 1) * indent; } Controller cc; cc = child.GetController("expanded"); if (cc != null) { cc.onChanged.Add(__expandedStateChanged); cc.selectedIndex = node.expanded ? 1 : 0; } cc = child.GetController("leaf"); if (cc != null) { cc.selectedIndex = node.isFolder ? 0 : 1; } if (node.isFolder) { child.onTouchBegin.Add(__cellTouchBegin); } if (treeNodeRender != null) { treeNodeRender(node, node._cell); } }
internal void _SetTree(GTree value) { tree = value; if (tree != null && tree.treeNodeWillExpand != null && _expanded) { tree.treeNodeWillExpand(this, true); } if (_children != null) { int cnt = _children.Count; for (int i = 0; i < cnt; i++) { GTreeNode node = _children[i]; node._level = _level + 1; node._SetTree(value); } } }
/// <summary> /// /// </summary> /// <param name="node"></param> internal void _AfterInserted(GTreeNode node) { if (node._cell == null) { CreateCell(node); } int index = GetInsertIndexForNode(node); AddChildAt(node.cell, index); if (treeNodeRender != null) { treeNodeRender(node, node._cell); } if (node.isFolder && node.expanded) { CheckChildren(node, index); } }
/// <summary> /// /// </summary> /// <param name="index"></param> /// <returns></returns> public GTreeNode RemoveChildAt(int index) { if (index >= 0 && index < numChildren) { GTreeNode child = _children[index]; _children.RemoveAt(index); child.parent = null; if (tree != null) { child._SetTree(null); tree._AfterRemoved(child); } return(child); } else { throw new Exception("Invalid child index"); } }
/// <summary> /// /// </summary> /// <param name="node"></param> void CreateCell(GTreeNode node) { GComponent child = itemPool.GetObject(node._resURL) as GComponent; if (child == null) { throw new Exception("FairyGUI: cannot create tree node object."); } child._treeNode = node; node._cell = child; GObject indentObj = node.cell.GetChild("indent"); if (indentObj != null) { indentObj.width = (node.level - 1) * indent; } Controller cc; cc = child.GetController("expanded"); if (cc != null) { cc.onChanged.Add(__expandedStateChanged); cc.selectedIndex = node.expanded ? 1 : 0; } cc = child.GetController("leaf"); if (cc != null) { cc.selectedIndex = node.isFolder ? 0 : 1; } if (treeNodeRender != null) { treeNodeRender(node, node._cell); } }
override protected void DispatchItemEvent(GObject item, EventContext context) { if (_clickToExpand != 0) { GTreeNode node = item._treeNode; if (node != null && _expandedStatusInEvt == node.expanded) { if (_clickToExpand == 2) { if (context.inputEvent.isDoubleClick) { node.expanded = !node.expanded; } } else { node.expanded = !node.expanded; } } } base.DispatchItemEvent(item, context); }
/// <summary> /// /// </summary> /// <param name="node"></param> void RemoveNode(GTreeNode node) { if (node.cell != null) { if (node.cell.parent != null) { RemoveChild(node.cell); } itemPool.ReturnObject(node.cell); node._cell._treeNode = null; node._cell = null; } if (node.isFolder) { int cnt = node.numChildren; for (int i = 0; i < cnt; i++) { GTreeNode node2 = node.GetChildAt(i); RemoveNode(node2); } } }
override protected void ReadItems(ByteBuffer buffer) { int nextPos; string str; bool isFolder; GTreeNode lastNode = null; int level; int prevLevel = 0; int cnt = buffer.ReadShort(); for (int i = 0; i < cnt; i++) { nextPos = buffer.ReadShort(); nextPos += buffer.position; str = buffer.ReadS(); if (str == null) { str = this.defaultItem; if (str == null) { buffer.position = nextPos; continue; } } isFolder = buffer.ReadBool(); level = buffer.ReadByte(); GTreeNode node = new GTreeNode(isFolder, str); node.expanded = true; if (i == 0) { _rootNode.AddChild(node); } else { if (level > prevLevel) { lastNode.AddChild(node); } else if (level < prevLevel) { for (int j = level; j <= prevLevel; j++) { lastNode = lastNode.parent; } lastNode.AddChild(node); } else { lastNode.parent.AddChild(node); } } lastNode = node; prevLevel = level; SetupItem(buffer, node.cell); buffer.position = nextPos; } }
/// <summary> /// /// </summary> /// <param name="node"></param> public void UnselectNode(GTreeNode node) { RemoveSelection(GetChildIndex(node.cell)); }
void __cellTouchBegin(EventContext context) { GTreeNode node = ((GObject)context.sender)._treeNode; _expandedStatusInEvt = node.expanded; }
/// <summary> /// /// </summary> /// <param name="node"></param> internal void _AfterRemoved(GTreeNode node) { RemoveNode(node); }