alignNodes method
Aligns multiple nodes according to the specified alignment option.
Requires at least 2 nodes. Calculates alignment based on the bounds of all specified nodes.
Example:
controller.alignNodes(
['node1', 'node2', 'node3'],
NodeAlignment.left,
);
Implementation
void alignNodes(List<String> nodeIds, NodeAlignment alignment) {
if (nodeIds.length < 2) return;
final nodes = nodeIds.map((id) => _nodes[id]).whereType<Node<T>>().toList();
if (nodes.length < 2) return;
final bounds = _calculateNodesBounds(nodes);
if (bounds == null) return;
final leftmost = bounds.left;
final rightmost = bounds.right;
final topmost = bounds.top;
final bottommost = bounds.bottom;
final centerX = bounds.center.dx;
final centerY = bounds.center.dy;
runInAction(() {
for (final node in nodes) {
final currentPos = node.position.value;
double newX = currentPos.dx;
double newY = currentPos.dy;
switch (alignment) {
case NodeAlignment.left:
newX = leftmost;
case NodeAlignment.right:
newX = rightmost - node.size.value.width;
case NodeAlignment.top:
newY = topmost;
case NodeAlignment.bottom:
newY = bottommost - node.size.value.height;
case NodeAlignment.center:
newX = centerX - node.size.value.width / 2;
newY = centerY - node.size.value.height / 2;
case NodeAlignment.horizontalCenter:
newX = centerX - node.size.value.width / 2;
case NodeAlignment.verticalCenter:
newY = centerY - node.size.value.height / 2;
}
final newPosition = Offset(newX, newY);
node.position.value = newPosition;
node.setVisualPosition(_config.snapToGridIfEnabled(newPosition));
}
});
internalMarkNodesDirty(nodeIds);
rebuildConnectionSegmentsForNodes(nodeIds);
}