This commit is contained in:
Халимов Рустам
2026-01-11 14:26:00 +03:00
parent eb8c5c48c7
commit 94eb2f00b2
2 changed files with 139 additions and 132 deletions

View File

@@ -61,48 +61,43 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
};
const selectedData = getSelectedPartition();
// --- CORE LOGIC: Two-Pass Limit Calculation (Sync with 3D) ---
const calculateLimits = (target: { axis: Axis, offset: number }, allParts: Partition[], limitMap: LimitMap | null) => {
let min = 0;
let max = 1;
const mid = target.offset;
// --- SOLVER (Копия из geometryGenerator для синхронизации) ---
const solveWallLimits = (parts: Partition[]): LimitMap => {
const limits: LimitMap = {};
parts.forEach(p => { limits[p.id] = { min: 0, max: 1 }; });
allParts.forEach(p => {
if (p.axis === target.axis) return;
for (let pass = 0; pass < 3; pass++) {
parts.forEach(target => {
let newMin = 0;
let newMax = 1;
const center = target.offset;
let pMin = p.min ?? 0;
let pMax = p.max ?? 1;
if (limitMap && limitMap[p.id]) {
pMin = limitMap[p.id].min;
pMax = limitMap[p.id].max;
}
parts.forEach(obstacle => {
if (target.id === obstacle.id) return;
if (target.axis === obstacle.axis) return;
if (target.offset > pMin && target.offset < pMax) {
if (p.offset < mid) min = Math.max(min, p.offset);
else if (p.offset > mid) max = Math.min(max, p.offset);
}
});
return { min, max };
const obsMin = limits[obstacle.id].min;
const obsMax = limits[obstacle.id].max;
if (target.offset > obsMin && target.offset < obsMax) {
if (obstacle.offset < center) newMin = Math.max(newMin, obstacle.offset);
else if (obstacle.offset > center) newMax = Math.min(newMax, obstacle.offset);
}
});
limits[target.id] = { min: newMin, max: newMax };
});
}
return limits;
};
// Helper to get final limits for a list of parts
const getFinalLimitsMap = (parts: Partition[]): LimitMap => {
const map: LimitMap = {};
// Pass 1
parts.forEach(p => { map[p.id] = calculateLimits(p, parts, null); });
// Pass 2
parts.forEach(p => { map[p.id] = calculateLimits(p, parts, map); });
return map;
};
// Calculate phantom box under cursor using Raycasting
// Расчет границ для НОВОЙ стенки (используем уже решенные границы существующих)
const getCursorBox = (lx: number, ly: number, parts: Partition[], limitMap: LimitMap) => {
let minX = 0, maxX = 1;
let minY = 0, maxY = 1;
parts.forEach(p => {
// Берем актуальные границы
const { min: pMin, max: pMax } = limitMap[p.id];
// Ignore collapsed walls
if (pMax - pMin < 0.001) return;
if (p.axis === 'x') {
@@ -127,8 +122,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const newPart: Partition = {
id: Math.random().toString(36).substr(2, 9),
axis, offset, min, max,
height: config.drawer.height || 80,
rounded: false
height: config.drawer.height || 80, rounded: false
};
onChange({ ...splits, partitions: { ...safePartitions, [key]: [...current, newPart] } });
setSelectedPartitionId(newPart.id);
@@ -155,7 +149,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
setDragging(null);
};
// -- MOUSE HANDLERS --
// -- MOUSE --
const handleMouseMove = (e: React.MouseEvent) => {
if (!svgRef.current) return;
const rect = svgRef.current.getBoundingClientRect();
@@ -217,7 +211,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
setHoveredCell(cellIdx);
const key = `${cellIdx.i}-${cellIdx.j}`;
const parts = safePartitions[key] || [];
const limitMap = getFinalLimitsMap(parts); // Calculate limits once per move
const limitMap = solveWallLimits(parts); // Вызываем солвер
const cx1 = sortedX[cellIdx.i]; const cx2 = sortedX[cellIdx.i+1];
const cy1 = sortedY[cellIdx.j]; const cy2 = sortedY[cellIdx.j+1];
@@ -229,7 +223,6 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const SNAP = 0.05;
for (const p of parts) {
const { min, max } = limitMap[p.id];
if (max - min < 0.001) continue; // Skip collapsed
if (p.axis === 'x') {
if (ly >= min && ly <= max && Math.abs(lx - p.offset) < SNAP * (aspectRatio > 1 ? 1 : aspectRatio)) found = p;
} else {
@@ -242,20 +235,25 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
} else {
const box = getCursorBox(lx, ly, parts, limitMap);
const axis = (lx - box.minX < box.maxX - lx) && (lx - box.minX < Math.min(ly - box.minY, box.maxY - ly)) ? 'x' :
(box.maxX - lx < lx - box.minX) && (box.maxX - lx < Math.min(ly - box.minY, box.maxY - ly)) ? 'x' :
Math.min(lx - box.minX, box.maxX - lx) < Math.min(ly - box.minY, box.maxY - ly) ? 'x' : 'y';
// Лучше: перпендикулярно ближайшей стороне
const distL = lx - box.minX; const distR = box.maxX - lx;
const distT = ly - box.minY; const distB = box.maxY - ly;
const minX = Math.min(distL, distR);
const minY = Math.min(distT, distB);
const minD = Math.min(distL, distR, distT, distB);
const axis = minX < minY ? 'y' : 'x';
const newAxis = (minD === distL || minD === distR) ? 'x' : 'y';
const width = box.maxX - box.minX;
const height = box.maxY - box.minY;
if ((axis === 'y' && height > 0.05) || (axis === 'x' && width > 0.05)) {
const offset = axis === 'x' ? lx : ly;
const min = axis === 'x' ? box.minY : box.minX;
const max = axis === 'x' ? box.maxY : box.maxX;
setPhantomPartition({ axis, offset, min, max });
if ((newAxis === 'y' && height > 0.05) || (newAxis === 'x' && width > 0.05)) {
const offset = newAxis === 'x' ? lx : ly;
const min = newAxis === 'x' ? box.minY : box.minX;
const max = newAxis === 'x' ? box.maxY : box.maxX;
setPhantomPartition({ axis: newAxis, offset, min, max });
}
}
}
@@ -317,8 +315,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const realW = (x2 - x1) * drawerW;
const realD = (y2 - y1) * drawerD;
// Calculate limits for rendering
const limitMap = getFinalLimitsMap(parts);
const limitMap = solveWallLimits(parts); // SOLVER для отрисовки
if (parts.length === 0) {
const labelX = cellX + cellW / 2;
@@ -341,7 +338,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
{parts.map(p => {
const { min, max } = limitMap[p.id];
if (max - min < 0.001) return null; // Don't render collapsed
if (max - min < 0.001) return null;
let lx1, ly1, lx2, ly2;
let dist1 = 0, dist2 = 0;
@@ -354,9 +351,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
ly1 = cellY + (cellH * min); ly2 = cellY + (cellH * max);
const cy = (min + max) / 2;
// Use already calculated limits for neighbors
const box = getCursorBox(p.offset, cy, parts, limitMap);
dist1 = Math.abs((p.offset - box.minX) * realW) - wallThick;
dist2 = Math.abs((box.maxX - p.offset) * realW) - wallThick;
midX = px; midY = (ly1 + ly2) / 2;
@@ -367,7 +362,6 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const cx = (min + max) / 2;
const box = getCursorBox(cx, p.offset, parts, limitMap);
dist1 = Math.abs((p.offset - box.minY) * realD) - wallThick;
dist2 = Math.abs((box.maxY - p.offset) * realD) - wallThick;
midX = (lx1 + lx2) / 2; midY = py;
@@ -375,7 +369,7 @@ export const LayoutStep: React.FC<Props> = ({ config, splits, onChange }) => {
const isSel = selectedPartitionId === p.id;
const isHov = hoveredPartition?.id === p.id;
const textOffset = 10;
const textOffset = 12;
return (
<g key={p.id}>