private static void PositionVertical(BonsaiNode node) { BonsaiNode parent = node.Parent; if (parent != null) { float ySeperation = parent.ChildCount() == 1 ? FormatPositioning.yLevelSeparation / 2f : FormatPositioning.yLevelSeparation; float x = node.Position.x; float y = parent.Position.y + parent.Size.y + ySeperation; node.Position = new Vector2(x, y); } }
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); }
public static void DrawNodeConnections(CanvasTransform t, BonsaiNode node) { if (node.ChildCount() == 0) { return; } var prefs = BonsaiPreferences.Instance; Color connectionColor = prefs.defaultConnectionColor; float connectionWidth = prefs.defaultConnectionWidth; if (node.Behaviour.GetStatusEditor() == Core.BehaviourNode.StatusEditor.Running) { connectionColor = prefs.runningStatusColor; connectionWidth = prefs.runningConnectionWidth; } // Start the Y anchor coord at the tip of the Output port. float yoffset = node.RectPositon.yMax; // Calculate the anchor position. float anchorX = node.RectPositon.center.x; float anchorY = (yoffset + node.GetNearestInputY()) / 2f; // Anchor line, between the first and last child. // Find the min and max X coords between the children and the parent. node.GetBoundsX(out float anchorLineStartX, out float anchorLineEndX); // Get start and end positions of the anchor line (The common line where the parent and children connect). var anchorLineStart = new Vector2(anchorLineStartX, anchorY); var anchorLineEnd = new Vector2(anchorLineEndX, anchorY); // The tip where the parent starts its line to connect to the anchor line. var parentAnchorTip = new Vector2(anchorX, yoffset); // The point where the parent connects to the anchor line. var parentAnchorLineConnection = new Vector2(anchorX, anchorY); // Draw the lines from the calculated positions. DrawLineCanvasSpace( t, parentAnchorTip, parentAnchorLineConnection, connectionColor, connectionWidth); DrawLineCanvasSpace( t, anchorLineStart, anchorLineEnd, prefs.defaultConnectionColor, prefs.defaultConnectionWidth); foreach (BonsaiNode child in node.Children) { // Get the positions to draw a line between the node and the anchor line. Vector2 center = child.InputRect.center; var anchorLineConnection = new Vector2(center.x, anchorY); // The node is running, hightlight the connection. if (child.Behaviour.GetStatusEditor() == Core.BehaviourNode.StatusEditor.Running) { DrawLineCanvasSpace( t, center, anchorLineConnection, prefs.runningStatusColor, prefs.runningConnectionWidth); // Hightlight the portion of the anchorline between the running child and parent node. DrawLineCanvasSpace( t, anchorLineConnection, parentAnchorLineConnection, prefs.runningStatusColor, prefs.runningConnectionWidth); } else { // The node is not running, draw a default connection. DrawLineCanvasSpace( t, anchorLineConnection, center, prefs.defaultConnectionColor, prefs.defaultConnectionWidth); } } }