mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
fixed the issue in aligning child space
This commit is contained in:
@ -336,6 +336,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
}
|
}
|
||||||
spaces.add(newSpace);
|
spaces.add(newSpace);
|
||||||
_updateNodePosition(newSpace, newSpace.position);
|
_updateNodePosition(newSpace, newSpace.position);
|
||||||
|
realignTree();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -450,7 +451,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
|
|
||||||
void _saveSpaces() {
|
void _saveSpaces() {
|
||||||
if (widget.selectedCommunity == null) {
|
if (widget.selectedCommunity == null) {
|
||||||
debugPrint("No community selected for saving spaces.");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -530,34 +530,82 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Offset getBalancedChildPosition(SpaceModel parent) {
|
Offset getBalancedChildPosition(SpaceModel parent) {
|
||||||
int totalSiblings = parent.children.length + 1;
|
const double nodeWidth = 200;
|
||||||
double totalWidth = (totalSiblings - 1) * 250; // Horizontal spacing
|
const double verticalGap = 180;
|
||||||
double startX = parent.position.dx - (totalWidth / 2);
|
|
||||||
|
|
||||||
Offset position = Offset(startX + (parent.children.length * 250), parent.position.dy + 180);
|
if (parent.children.isEmpty) {
|
||||||
|
// First child → exactly center
|
||||||
|
return Offset(parent.position.dx, parent.position.dy + verticalGap);
|
||||||
|
} else {
|
||||||
|
// More children → arrange them spaced horizontally
|
||||||
|
double totalWidth = (parent.children.length) * (nodeWidth + 60);
|
||||||
|
double startX = parent.position.dx - (totalWidth / 2);
|
||||||
|
|
||||||
// Check for overlaps & adjust
|
double childX = startX + (parent.children.length * (nodeWidth + 60));
|
||||||
while (spaces.any((s) => (s.position - position).distance < 250)) {
|
return Offset(childX, parent.position.dy + verticalGap);
|
||||||
position = Offset(position.dx + 250, position.dy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return position;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void realignTree() {
|
void realignTree() {
|
||||||
void updatePositions(SpaceModel node, double x, double y) {
|
const double nodeWidth = 200;
|
||||||
node.position = Offset(x, y);
|
const double nodeHeight = 100;
|
||||||
|
const double horizontalGap = 60;
|
||||||
|
const double verticalGap = 180;
|
||||||
|
|
||||||
int numChildren = node.children.length;
|
double canvasRightEdge = 1000;
|
||||||
double childStartX = x - ((numChildren - 1) * 250) / 2;
|
double canvasBottomEdge = 1000;
|
||||||
|
|
||||||
for (int i = 0; i < numChildren; i++) {
|
double _calculateSubtreeWidth(SpaceModel node) {
|
||||||
updatePositions(node.children[i], childStartX + (i * 250), y + 180);
|
if (node.children.isEmpty) return nodeWidth;
|
||||||
|
double totalWidth = 0;
|
||||||
|
for (var child in node.children) {
|
||||||
|
totalWidth += _calculateSubtreeWidth(child) + horizontalGap;
|
||||||
|
}
|
||||||
|
return totalWidth - horizontalGap;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _layoutTree(SpaceModel node, double startX, double y) {
|
||||||
|
double subtreeWidth = _calculateSubtreeWidth(node);
|
||||||
|
double centerX = startX + subtreeWidth / 2 - nodeWidth / 2;
|
||||||
|
node.position = Offset(centerX, y);
|
||||||
|
|
||||||
|
canvasRightEdge =
|
||||||
|
centerX + nodeWidth > canvasRightEdge ? centerX + nodeWidth : canvasRightEdge;
|
||||||
|
canvasBottomEdge = y + nodeHeight > canvasBottomEdge ? y + nodeHeight : canvasBottomEdge;
|
||||||
|
|
||||||
|
if (node.children.length == 1) {
|
||||||
|
final child = node.children.first;
|
||||||
|
double parentCenterX = node.position.dx + nodeWidth / 2;
|
||||||
|
double childX = parentCenterX - nodeWidth / 2;
|
||||||
|
double childY = y + verticalGap;
|
||||||
|
|
||||||
|
child.position = Offset(childX, childY);
|
||||||
|
_layoutTree(child, childX, childY);
|
||||||
|
} else {
|
||||||
|
double childX = startX;
|
||||||
|
for (var child in node.children) {
|
||||||
|
double childWidth = _calculateSubtreeWidth(child);
|
||||||
|
_layoutTree(child, childX, y + verticalGap);
|
||||||
|
childX += childWidth + horizontalGap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spaces.isNotEmpty) {
|
final SpaceModel? root = spaces.firstWhere(
|
||||||
updatePositions(spaces.first, spaces.first.position.dx, spaces.first.position.dy);
|
(s) => s.parent == null,
|
||||||
|
orElse: () => spaces.first,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (root != null) {
|
||||||
|
double totalTreeWidth = _calculateSubtreeWidth(root);
|
||||||
|
double startingX = (canvasWidth / 2) - (totalTreeWidth / 2);
|
||||||
|
|
||||||
|
_layoutTree(root, startingX, 100);
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
canvasWidth = canvasRightEdge + 200;
|
||||||
|
canvasHeight = canvasBottomEdge + 200;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -650,38 +698,87 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
|
|
||||||
/// **Find a new position ensuring no overlap**
|
/// **Find a new position ensuring no overlap**
|
||||||
Offset getBalancedChildPosition(SpaceModel parent) {
|
Offset getBalancedChildPosition(SpaceModel parent) {
|
||||||
int totalSiblings = parent.children.length + 1;
|
const double nodeWidth = 200;
|
||||||
double totalWidth = (totalSiblings - 1) * horizontalGap;
|
const double horizontalGap = 60;
|
||||||
double startX = parent.position.dx - (totalWidth / 2);
|
const double verticalGap = 180;
|
||||||
Offset position = Offset(
|
|
||||||
startX + (parent.children.length * horizontalGap), parent.position.dy + verticalGap);
|
|
||||||
|
|
||||||
// **Check for overlaps & adjust**
|
int numSiblings = parent.children.length;
|
||||||
while (spaces.any((s) => (s.position - position).distance < horizontalGap)) {
|
double totalWidth = numSiblings * (nodeWidth + horizontalGap);
|
||||||
position = Offset(position.dx + horizontalGap, position.dy);
|
|
||||||
}
|
|
||||||
|
|
||||||
print("🔹 New position for ${parent.name}: (${position.dx}, ${position.dy})");
|
// Calculate position directly beneath the parent, spaced horizontally
|
||||||
return position;
|
double x = parent.position.dx - (totalWidth / 2) + numSiblings * (nodeWidth + horizontalGap);
|
||||||
|
double y = parent.position.dy + verticalGap;
|
||||||
|
|
||||||
|
return Offset(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// **Realign the entire tree after duplication**
|
/// **Realign the entire tree after duplication**
|
||||||
void realignTree() {
|
void realignTree() {
|
||||||
void updatePositions(SpaceModel node, double x, double y) {
|
const double nodeWidth = 200;
|
||||||
node.position = Offset(x, y);
|
const double nodeHeight = 100;
|
||||||
print("✅ Adjusted ${node.name} to (${x}, ${y})");
|
const double horizontalGap = 60;
|
||||||
|
const double verticalGap = 180;
|
||||||
|
|
||||||
int numChildren = node.children.length;
|
double canvasRightEdge = 1000;
|
||||||
double childStartX = x - ((numChildren - 1) * horizontalGap) / 2;
|
double canvasBottomEdge = 1000;
|
||||||
|
|
||||||
for (int i = 0; i < numChildren; i++) {
|
// Step 1: Calculate the width of any subtree
|
||||||
updatePositions(node.children[i], childStartX + (i * horizontalGap), y + verticalGap);
|
double _calculateSubtreeWidth(SpaceModel node) {
|
||||||
|
if (node.children.isEmpty) return nodeWidth;
|
||||||
|
double totalWidth = 0;
|
||||||
|
for (var child in node.children) {
|
||||||
|
totalWidth += _calculateSubtreeWidth(child) + horizontalGap;
|
||||||
|
}
|
||||||
|
return totalWidth - horizontalGap;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _layoutTree(SpaceModel node, double startX, double y) {
|
||||||
|
double subtreeWidth = _calculateSubtreeWidth(node);
|
||||||
|
double centerX = startX + subtreeWidth / 2 - nodeWidth / 2;
|
||||||
|
node.position = Offset(centerX, y);
|
||||||
|
|
||||||
|
canvasRightEdge =
|
||||||
|
centerX + nodeWidth > canvasRightEdge ? centerX + nodeWidth : canvasRightEdge;
|
||||||
|
canvasBottomEdge = y + nodeHeight > canvasBottomEdge ? y + nodeHeight : canvasBottomEdge;
|
||||||
|
|
||||||
|
if (node.children.length == 1) {
|
||||||
|
final child = node.children.first;
|
||||||
|
|
||||||
|
double parentCenterX = node.position.dx + nodeWidth / 2;
|
||||||
|
double childX = parentCenterX - nodeWidth / 2;
|
||||||
|
double childY = y + verticalGap;
|
||||||
|
|
||||||
|
child.position = Offset(childX, childY);
|
||||||
|
|
||||||
|
_layoutTree(child, childX, childY);
|
||||||
|
} else {
|
||||||
|
double childX = startX;
|
||||||
|
for (var child in node.children) {
|
||||||
|
double childWidth = _calculateSubtreeWidth(child);
|
||||||
|
_layoutTree(child, childX, y + verticalGap);
|
||||||
|
childX += childWidth + horizontalGap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spaces.isNotEmpty) {
|
// Step 3: Start centered
|
||||||
print("🔄 Realigning tree...");
|
final SpaceModel? root = spaces.firstWhere(
|
||||||
updatePositions(spaces.first, spaces.first.position.dx, spaces.first.position.dy);
|
(s) => s.parent == null,
|
||||||
|
orElse: () => spaces.first,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (root != null) {
|
||||||
|
// Calculate total width of full tree
|
||||||
|
double totalTreeWidth = _calculateSubtreeWidth(root);
|
||||||
|
|
||||||
|
double startingX = (canvasWidth / 2) - (totalTreeWidth / 2);
|
||||||
|
|
||||||
|
_layoutTree(root, startingX, 100);
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
canvasWidth = canvasRightEdge + 200; // give breathing space
|
||||||
|
canvasHeight = canvasBottomEdge + 200;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -692,8 +789,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
: getBalancedChildPosition(duplicatedParent);
|
: getBalancedChildPosition(duplicatedParent);
|
||||||
|
|
||||||
final duplicatedName = SpaceHelper.generateUniqueSpaceName(original.name, spaces);
|
final duplicatedName = SpaceHelper.generateUniqueSpaceName(original.name, spaces);
|
||||||
print(
|
|
||||||
"🟡 Duplicating ${original.name} → ${duplicatedName} at (${newPosition.dx}, ${newPosition.dy})");
|
|
||||||
|
|
||||||
final duplicated = SpaceModel(
|
final duplicated = SpaceModel(
|
||||||
name: duplicatedName,
|
name: duplicatedName,
|
||||||
@ -722,7 +817,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
duplicated.incomingConnection = newConnection;
|
duplicated.incomingConnection = newConnection;
|
||||||
duplicatedParent.addOutgoingConnection(newConnection);
|
duplicatedParent.addOutgoingConnection(newConnection);
|
||||||
duplicatedParent.children.add(duplicated);
|
duplicatedParent.children.add(duplicated);
|
||||||
print("🔗 Created connection: ${duplicatedParent.name} → ${duplicated.name}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// **Recalculate the whole tree to avoid overlaps**
|
// **Recalculate the whole tree to avoid overlaps**
|
||||||
@ -739,19 +833,14 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
|
|
||||||
/// **Handle root duplication**
|
/// **Handle root duplication**
|
||||||
if (space.parent == null) {
|
if (space.parent == null) {
|
||||||
print("🟠 Duplicating root node: ${space.name}");
|
|
||||||
SpaceModel duplicatedRoot = duplicateRecursive(space, null);
|
SpaceModel duplicatedRoot = duplicateRecursive(space, null);
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
spaces.add(duplicatedRoot);
|
spaces.add(duplicatedRoot);
|
||||||
realignTree();
|
realignTree();
|
||||||
});
|
});
|
||||||
|
|
||||||
print("✅ Root duplication successful: ${duplicatedRoot.name}");
|
|
||||||
} else {
|
} else {
|
||||||
duplicateRecursive(space, space.parent);
|
duplicateRecursive(space, space.parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
print("🟢 Finished duplication process for: ${space.name}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user