/// <summary>
/// Recursively goes through the node tree until it reaches the ends and places the Y coordinate for each node
/// Each node is moved with relation to nodes above and to the right of it.
/// </summary>
public void PositionNodes(TreeNodeGroup nodeGroup, int depth)
{
if (nodeGroup.ParentChildGroups.Count == 0) //is a leaf node
{
if (!_depthYMargin.ContainsKey(depth))
{
//if you're positioning nodes below nodes that have already been positioned but deeper into the tree
//they must be lower than those nodes above, this gets that value and passes it on
if (depth > _maxTreeDepth && _depthYMargin.ContainsKey(_maxTreeDepth))
{
_depthYMargin[depth] = _depthYMargin[_maxTreeDepth];
_maxTreeDepth = depth;
}
else
{
_depthYMargin[depth] = MarginFromTop;
if (depth > _maxTreeDepth)
{
_maxTreeDepth = depth;
}
}
}
nodeGroup.ParentNode.MoveNode(new Point(nodeGroup.ParentNode.Location.X, _depthYMargin[depth]), true);
_depthYMargin[depth] += nodeGroup.ParentNode.NodeControl.ActualHeight + VERTICAL_BUFFER;
}
else //is a parent node
{
bool firstRow = false;
for (int i = 0; i < nodeGroup.ParentChildGroups.Count; i++)
{
if (!_depthYMargin.ContainsKey(depth))
{
firstRow = true;
_depthYMargin[depth] = nodeGroup.ParentNode.NodeControl.ActualHeight + VERTICAL_BUFFER;
}
else
{
firstRow = false;
}
PositionNodes(nodeGroup.ParentChildGroups[i], depth+1);
}
double position = nodeGroup.GetYPosition();
//if it's not the top most nodes check that it doesn't collide with a row of nodes above it
if ((position < _depthYMargin[depth] - MarginFromTop) && !firstRow)
{
OffsetNodes(nodeGroup, depth); //collision was detected, move all the nodes from this node to the depth of the tree down
}
else
{
nodeGroup.ParentNode.MoveNode(new Point(nodeGroup.ParentNode.Location.X, position), true);
_depthYMargin[depth] = position + nodeGroup.ParentNode.NodeControl.ActualHeight + VERTICAL_BUFFER;
}
}
}