private static void DrawPorts(ManagedTopologyNode node, Graphics graphics) { int numChildren = 0; for (uint i = 0; i < node.GetNumPorts(); i++) { if (node.GetPortType(i) == ManagedTopologyNode.PortType.ConnectedToChild) { numChildren++; } } int childIndex = 0; for (uint i = 0; i < node.GetNumPorts(); i++) { ManagedTopologyNode.PortType currPort = node.GetPortType(i); switch (currPort) { case ManagedTopologyNode.PortType.NotConnected: break; case ManagedTopologyNode.PortType.ConnectedToParent: graphics.FillRectangle( Brushes.Black, (NodeWidth / 2) - (PortWidth / 2), 0, PortWidth, PortHeight); break; case ManagedTopologyNode.PortType.ConnectedToChild: int startX = (NodeWidth / (numChildren + 1)) * (childIndex + 1); graphics.FillRectangle( Brushes.Black, startX - (PortWidth / 2), NodeHeight - PortHeight, PortWidth, PortHeight); childIndex++; break; default: break; } } }
private int ParseNode(ManagedTopologyNode node, int width, int height, int horzOffset, int vertOffset) { // To improve the layout of the tree, the allocation of width is // determined by the number of second generation children rather than // just immediate children. int num2ndGenChildren = CountNum2ndGenChildren(node); int widthUnit; if (num2ndGenChildren == 0) { widthUnit = width; } else { widthUnit = width / num2ndGenChildren; } int nextHorzOffset = horzOffset; // Calculate the position of the node int nodeXPos = horzOffset + (width / 2); int nodeYPos = vertOffset + (height / 2); // Add this node to the list of surfaces to be drawn InsertNewNodeSurface(node, nodeXPos, nodeYPos); // Figure out there are any children to be recursively drawn int accumOverlap = 0; uint childIndex = 0; for (uint portIndex = 0; portIndex < node.GetNumPorts(); portIndex++) { ManagedTopologyNode.PortType currPort = node.GetPortType(portIndex); if (currPort == ManagedTopologyNode.PortType.ConnectedToChild) { ManagedTopologyNode currChild = node.GetChild(childIndex); int thisWidth = widthUnit * ((int)currChild.GetNumChildren()); if (thisWidth == 0) { thisWidth = widthUnit; } int thisCenterX = nextHorzOffset + (thisWidth / 2); int thisCenterY = vertOffset + height + (height / 2); int surfaceWidth, surfaceHeight; NodeSurface.GetDimensions(out surfaceWidth, out surfaceHeight); // There might be more than 1 child, so perform some // calculations so that the lines don't start from // the same point int numChildren = (int)node.GetNumChildren(); int startX = unchecked ((int)(nodeXPos + ((surfaceWidth / (numChildren + 1)) * (childIndex + 1)) - (surfaceWidth / 2))); int startY = nodeYPos + (surfaceHeight / 2); int endX = thisCenterX; int endY = thisCenterY - (surfaceHeight / 2); // Start a line to be drawn later Line newLine = new Line(startX, startY, endX, endY); m_lines.Add(newLine); ManagedTopologyNode nextChild = node.GetChild(childIndex++); accumOverlap += ParseNode( nextChild, thisWidth, height, nextHorzOffset, vertOffset + height); nextHorzOffset += thisWidth; } } int nodeWidth, nodeHeight; NodeSurface.GetDimensions(out nodeWidth, out nodeHeight); if (width < nodeWidth) { accumOverlap += nodeWidth - width; } if (m_usedWidth < (horzOffset + width)) { accumOverlap += (horzOffset + width) - m_usedWidth; } return(accumOverlap); }