mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
duplicated space
This commit is contained in:
@ -1,4 +1,6 @@
|
|||||||
// Flutter imports
|
// Flutter imports
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
|
||||||
@ -551,62 +553,62 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
const double nodeHeight = 100;
|
const double nodeHeight = 100;
|
||||||
const double horizontalGap = 60;
|
const double horizontalGap = 60;
|
||||||
const double verticalGap = 180;
|
const double verticalGap = 180;
|
||||||
|
const double rootGap = 400; // extra space between different roots
|
||||||
|
|
||||||
double canvasRightEdge = 1000;
|
double canvasRightEdge = 1000;
|
||||||
double canvasBottomEdge = 1000;
|
double canvasBottomEdge = 1000;
|
||||||
|
|
||||||
double _calculateSubtreeWidth(SpaceModel node) {
|
double calculateSubtreeWidth(SpaceModel node) {
|
||||||
if (node.children.isEmpty) return nodeWidth;
|
if (node.children.isEmpty) return nodeWidth;
|
||||||
double totalWidth = 0;
|
double totalWidth = 0;
|
||||||
for (var child in node.children) {
|
for (var child in node.children) {
|
||||||
totalWidth += _calculateSubtreeWidth(child) + horizontalGap;
|
totalWidth += calculateSubtreeWidth(child) + horizontalGap;
|
||||||
}
|
}
|
||||||
return totalWidth - horizontalGap;
|
return totalWidth - horizontalGap;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _layoutTree(SpaceModel node, double startX, double y) {
|
void layoutSubtree(SpaceModel node, double startX, double y) {
|
||||||
double subtreeWidth = _calculateSubtreeWidth(node);
|
double subtreeWidth = calculateSubtreeWidth(node);
|
||||||
double centerX = startX + subtreeWidth / 2 - nodeWidth / 2;
|
double centerX = startX + subtreeWidth / 2 - nodeWidth / 2;
|
||||||
node.position = Offset(centerX, y);
|
node.position = Offset(centerX, y);
|
||||||
|
|
||||||
canvasRightEdge =
|
canvasRightEdge = max(canvasRightEdge, centerX + nodeWidth);
|
||||||
centerX + nodeWidth > canvasRightEdge ? centerX + nodeWidth : canvasRightEdge;
|
canvasBottomEdge = max(canvasBottomEdge, y + nodeHeight);
|
||||||
canvasBottomEdge = y + nodeHeight > canvasBottomEdge ? y + nodeHeight : canvasBottomEdge;
|
|
||||||
|
|
||||||
if (node.children.length == 1) {
|
if (node.children.length == 1) {
|
||||||
final child = node.children.first;
|
final child = node.children.first;
|
||||||
double parentCenterX = node.position.dx + nodeWidth / 2;
|
layoutSubtree(child, centerX, y + verticalGap);
|
||||||
double childX = parentCenterX - nodeWidth / 2;
|
|
||||||
double childY = y + verticalGap;
|
|
||||||
|
|
||||||
child.position = Offset(childX, childY);
|
|
||||||
_layoutTree(child, childX, childY);
|
|
||||||
} else {
|
} else {
|
||||||
double childX = startX;
|
double childX = startX;
|
||||||
for (var child in node.children) {
|
for (var child in node.children) {
|
||||||
double childWidth = _calculateSubtreeWidth(child);
|
double childWidth = calculateSubtreeWidth(child);
|
||||||
_layoutTree(child, childX, y + verticalGap);
|
layoutSubtree(child, childX, y + verticalGap);
|
||||||
childX += childWidth + horizontalGap;
|
childX += childWidth + horizontalGap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final SpaceModel? root = spaces.firstWhere(
|
// ⚡ New: layout each root separately
|
||||||
(s) => s.parent == null,
|
final List<SpaceModel> roots = spaces
|
||||||
orElse: () => spaces.first,
|
.where((s) =>
|
||||||
);
|
s.parent == null &&
|
||||||
|
s.status != SpaceStatus.deleted &&
|
||||||
|
s.status != SpaceStatus.parentDeleted)
|
||||||
|
.toList();
|
||||||
|
|
||||||
if (root != null) {
|
double currentX = 100; // start some margin from left
|
||||||
double totalTreeWidth = _calculateSubtreeWidth(root);
|
double currentY = 100; // top margin
|
||||||
double startingX = (canvasWidth / 2) - (totalTreeWidth / 2);
|
|
||||||
|
|
||||||
_layoutTree(root, startingX, 100);
|
for (var root in roots) {
|
||||||
|
layoutSubtree(root, currentX, currentY);
|
||||||
setState(() {
|
double rootWidth = calculateSubtreeWidth(root);
|
||||||
canvasWidth = canvasRightEdge + 200;
|
currentX += rootWidth + rootGap;
|
||||||
canvasHeight = canvasBottomEdge + 200;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
canvasWidth = canvasRightEdge + 400;
|
||||||
|
canvasHeight = canvasBottomEdge + 400;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onDuplicate(BuildContext parentContext) {
|
void _onDuplicate(BuildContext parentContext) {
|
||||||
@ -690,110 +692,19 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _duplicateSpace(SpaceModel space) {
|
void _duplicateSpace(SpaceModel space) {
|
||||||
final Map<SpaceModel, SpaceModel> originalToDuplicate = {};
|
final double horizontalGap = 250.0;
|
||||||
double horizontalGap = 250.0; // Increased spacing
|
final double verticalGap = 180.0;
|
||||||
double verticalGap = 180.0; // Adjusted for better visualization
|
final double nodeWidth = 200;
|
||||||
|
final double nodeHeight = 100;
|
||||||
|
final double breathingSpace = 300.0; // extra gap after original tree
|
||||||
|
|
||||||
print("🟢 Duplicating: ${space.name}");
|
/// Helper to recursively duplicate a node and its children
|
||||||
|
|
||||||
/// **Find a new position ensuring no overlap**
|
|
||||||
Offset getBalancedChildPosition(SpaceModel parent) {
|
|
||||||
const double nodeWidth = 200;
|
|
||||||
const double horizontalGap = 60;
|
|
||||||
const double verticalGap = 180;
|
|
||||||
|
|
||||||
int numSiblings = parent.children.length;
|
|
||||||
double totalWidth = numSiblings * (nodeWidth + horizontalGap);
|
|
||||||
|
|
||||||
// Calculate position directly beneath the parent, spaced horizontally
|
|
||||||
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**
|
|
||||||
void realignTree() {
|
|
||||||
const double nodeWidth = 200;
|
|
||||||
const double nodeHeight = 100;
|
|
||||||
const double horizontalGap = 60;
|
|
||||||
const double verticalGap = 180;
|
|
||||||
|
|
||||||
double canvasRightEdge = 1000;
|
|
||||||
double canvasBottomEdge = 1000;
|
|
||||||
|
|
||||||
// Step 1: Calculate the width of any subtree
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 3: Start centered
|
|
||||||
final SpaceModel? root = spaces.firstWhere(
|
|
||||||
(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;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// **Recursive duplication logic**
|
|
||||||
SpaceModel duplicateRecursive(SpaceModel original, SpaceModel? duplicatedParent) {
|
SpaceModel duplicateRecursive(SpaceModel original, SpaceModel? duplicatedParent) {
|
||||||
Offset newPosition = duplicatedParent == null
|
|
||||||
? Offset(original.position.dx + horizontalGap, original.position.dy)
|
|
||||||
: getBalancedChildPosition(duplicatedParent);
|
|
||||||
|
|
||||||
final duplicatedName = SpaceHelper.generateUniqueSpaceName(original.name, spaces);
|
final duplicatedName = SpaceHelper.generateUniqueSpaceName(original.name, spaces);
|
||||||
|
|
||||||
final duplicated = SpaceModel(
|
final duplicated = SpaceModel(
|
||||||
name: duplicatedName,
|
name: duplicatedName,
|
||||||
icon: original.icon,
|
icon: original.icon,
|
||||||
position: newPosition,
|
position: Offset.zero,
|
||||||
isPrivate: original.isPrivate,
|
isPrivate: original.isPrivate,
|
||||||
children: [],
|
children: [],
|
||||||
status: SpaceStatus.newSpace,
|
status: SpaceStatus.newSpace,
|
||||||
@ -803,27 +714,20 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
tags: original.tags,
|
tags: original.tags,
|
||||||
);
|
);
|
||||||
|
|
||||||
setState(() {
|
spaces.add(duplicated);
|
||||||
spaces.add(duplicated);
|
|
||||||
_updateNodePosition(duplicated, duplicated.position);
|
|
||||||
|
|
||||||
if (duplicatedParent != null) {
|
if (duplicatedParent != null) {
|
||||||
final newConnection = Connection(
|
final newConnection = Connection(
|
||||||
startSpace: duplicatedParent,
|
startSpace: duplicatedParent,
|
||||||
endSpace: duplicated,
|
endSpace: duplicated,
|
||||||
direction: "down",
|
direction: "down",
|
||||||
);
|
);
|
||||||
connections.add(newConnection);
|
connections.add(newConnection);
|
||||||
duplicated.incomingConnection = newConnection;
|
duplicated.incomingConnection = newConnection;
|
||||||
duplicatedParent.addOutgoingConnection(newConnection);
|
duplicatedParent.addOutgoingConnection(newConnection);
|
||||||
duplicatedParent.children.add(duplicated);
|
duplicatedParent.children.add(duplicated);
|
||||||
}
|
}
|
||||||
|
|
||||||
// **Recalculate the whole tree to avoid overlaps**
|
|
||||||
realignTree();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Recursively duplicate children
|
|
||||||
for (var child in original.children) {
|
for (var child in original.children) {
|
||||||
duplicateRecursive(child, duplicated);
|
duplicateRecursive(child, duplicated);
|
||||||
}
|
}
|
||||||
@ -831,16 +735,49 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
return duplicated;
|
return duplicated;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// **Handle root duplication**
|
/// Layout a subtree rooted at node
|
||||||
if (space.parent == null) {
|
void layoutSubtree(SpaceModel node, double startX, double startY) {
|
||||||
SpaceModel duplicatedRoot = duplicateRecursive(space, null);
|
double calculateSubtreeWidth(SpaceModel n) {
|
||||||
|
if (n.children.isEmpty) return nodeWidth;
|
||||||
|
double width = 0;
|
||||||
|
for (var child in n.children) {
|
||||||
|
width += calculateSubtreeWidth(child) + horizontalGap;
|
||||||
|
}
|
||||||
|
return width - horizontalGap;
|
||||||
|
}
|
||||||
|
|
||||||
setState(() {
|
void assignPositions(SpaceModel n, double x, double y) {
|
||||||
spaces.add(duplicatedRoot);
|
double subtreeWidth = calculateSubtreeWidth(n);
|
||||||
realignTree();
|
double centerX = x + subtreeWidth / 2 - nodeWidth / 2;
|
||||||
});
|
n.position = Offset(centerX, y);
|
||||||
} else {
|
|
||||||
duplicateRecursive(space, space.parent);
|
if (n.children.length == 1) {
|
||||||
|
assignPositions(n.children.first, centerX, y + verticalGap);
|
||||||
|
} else {
|
||||||
|
double childX = x;
|
||||||
|
for (var child in n.children) {
|
||||||
|
double childWidth = calculateSubtreeWidth(child);
|
||||||
|
assignPositions(child, childX, y + verticalGap);
|
||||||
|
childX += childWidth + horizontalGap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double totalSubtreeWidth = calculateSubtreeWidth(node);
|
||||||
|
assignPositions(node, startX, startY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Actual duplication process
|
||||||
|
setState(() {
|
||||||
|
if (space.parent == null) {
|
||||||
|
// Duplicating a ROOT node
|
||||||
|
SpaceModel duplicatedRoot = duplicateRecursive(space, null);
|
||||||
|
realignTree();
|
||||||
|
} else {
|
||||||
|
// Duplicating a CHILD node inside its parent
|
||||||
|
SpaceModel duplicated = duplicateRecursive(space, space.parent);
|
||||||
|
realignTree();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user