public static void RenderTree(IRenderContext g, TypeTree tree) { RenderNode rRoot = RenderNode.ConvertFromNode(tree.root); Size size = rRoot.GetSize(); g.DrawBackground(size); DrawTree(g, rRoot, rRoot.GetOriginX((int)size.Width), SquareSize); }
public static RenderNode ConvertFromNode(Node root) { RenderNode newNode = MakeNode(root, 0, 0); newNode.isRoot = true; newNode.EstimateXBounds(); newNode.Arrange(); newNode.ApplyXOffset(); return(newNode); }
private void MergeBounds(RenderNode node, bool withOffset) { int offset = withOffset ? node.xOffset : 0; if (node.xMin + offset < xMin || xMin == -1) { xMin = node.xMin + offset; } if (node.xMax + offset > xMax || xMax == -1) { xMax = node.xMax + offset; } }
private void IterateHeight(RenderNode root, int height) { if (left != null) { left.IterateHeight(root, height + 1); } if (right != null) { right.IterateHeight(root, height + 1); } if (root.height < height) { root.height = height; } }
private static Size DrawTree(IRenderContext g, RenderNode rRoot, int x, int y) { RenderNode left; RenderNode right; Size size; foreach (RenderNode n in rRoot.GetAllNodes()) { bool hasChild = false; int nx = n.x + x; int ny = n.y + y; g.DrawRectangle(nx - SquareSize / 2, ny - SquareSize / 2, SquareSize, SquareSize); size = g.MeasureString(n.display); g.DrawString(n.display, nx - size.Width / 2, ny - size.Height / 2); if (!n.isRoot) { g.DrawLine(nx, ny - SquareSize / 2, nx, ny - DefaultDistance / 2); } left = n.GetLeft(); right = n.GetRight(); if (left != null) { g.DrawLine(nx, ny + DefaultDistance / 2, left.x + x, ny + DefaultDistance / 2); hasChild = true; } if (right != null) { g.DrawLine(nx, ny + DefaultDistance / 2, right.x + x, ny + DefaultDistance / 2); hasChild = true; } if (hasChild) { g.DrawLine(nx, ny + SquareSize / 2, nx, ny + DefaultDistance / 2); } } return(rRoot.GetSize()); }
private static RenderNode MakeNode(Node node, int px, int py) { RenderNode rNode = new RenderNode(node); rNode.x = px; rNode.y = py; if (node.left != null) { rNode.left = MakeNode(node.left, px - TreeRenderer.DefaultDistance, py + TreeRenderer.DefaultDistance); } if (node.right != null) { rNode.right = MakeNode(node.right, px + TreeRenderer.DefaultDistance, py + TreeRenderer.DefaultDistance); } return(rNode); }
// pre-order iterate public List <RenderNode> GetAllNodes() { List <RenderNode> nodes = new List <RenderNode>(); Stack <RenderNode> stack = new Stack <RenderNode>(); stack.Push(this); while (stack.Count > 0) { RenderNode node = stack.Pop(); nodes.Add(node); if (node.right != null) { stack.Push(node.right); } if (node.left != null) { stack.Push(node.left); } } return(nodes); }