/// <summary> /// Formats the tree to look nicely. /// </summary> public static void PositionNodesNicely(BonsaiNode root, Vector2 anchor) { // Sort parent-child connections so formatter uses latest changes. foreach (BonsaiNode node in TreeTraversal.PreOrder(root)) { node.SortChildren(); } var positioning = new FormatPositioning(); foreach (BonsaiNode node in TreeTraversal.PostOrder(root)) { PositionHorizontal(node, positioning); } foreach (BonsaiNode node in TreeTraversal.PreOrder(root)) { PositionVertical(node); } // Move the entire subtree to the anchor. Vector2 offset = EditorSingleDrag.StartDrag(root, root.Center); EditorSingleDrag.SetSubtreePosition(root, anchor, offset); }
private static void PositionHorizontal(BonsaiNode node, FormatPositioning positioning) { float xCoord; int childCount = node.ChildCount(); // If it is a parent of 2 or more children then center in between the children. if (childCount > 1) { // Get the x-midpoint between the first and last children. Vector2 firstChildPos = node.GetChildAt(0).Center; Vector2 lastChildPos = node.GetChildAt(childCount - 1).Center; float xMid = (firstChildPos.x + lastChildPos.x) / 2f; xCoord = xMid; positioning.xIntermediate = xMid; } // A node with 1 child, place directly above child. else if (childCount == 1) { xCoord = positioning.xIntermediate; } // A leaf node else { float branchWidth = MaxWidthForBranchList(node); positioning.xLeaf += 0.5f * (positioning.lastLeafWidth + branchWidth) + FormatPositioning.xLeafSeparation; xCoord = positioning.xLeaf; positioning.xIntermediate = positioning.xLeaf; positioning.lastLeafWidth = branchWidth; } // Set to 0 on the y-axis for this pass. node.Center = new Vector2(xCoord, 0f); }
/// <summary> /// Formats the tree to look nicely. /// </summary> public static void PositionNodesNicely(BonsaiNode root, Vector2 anchor) { // Sort parent-child connections so formatter uses latest changes. TreeIterator <BonsaiNode> .Traverse( root, node => node.SortChildren()); var positioning = new FormatPositioning(); TreeIterator <BonsaiNode> .Traverse( root, node => PositionHorizontal(node, positioning), Traversal.PostOrder); TreeIterator <BonsaiNode> .Traverse( root, node => PositionVertical(node)); // Move the entire subtree to the anchor. Vector2 offset = EditorSingleDrag.StartDrag(root, root.Center); EditorSingleDrag.SetSubtreePosition(root, anchor, offset); }