2025-06-10 15:28:23 +05:30
|
|
|
import * as THREE from 'three';
|
2025-06-25 15:26:53 +05:30
|
|
|
import { useThree } from '@react-three/fiber';
|
2025-06-23 13:38:26 +05:30
|
|
|
import { useEffect, useMemo, useState } from "react";
|
|
|
|
|
import { DragControls, Tube } from '@react-three/drei';
|
2025-06-26 17:47:32 +05:30
|
|
|
import { useSocketStore, useToolMode } from '../../../store/builder/store';
|
2025-06-23 13:38:26 +05:30
|
|
|
import { useBuilderStore } from '../../../store/builder/useBuilderStore';
|
2025-06-25 15:26:53 +05:30
|
|
|
import { useSceneContext } from '../../scene/sceneContext';
|
|
|
|
|
import * as Constants from '../../../types/world/worldConstants';
|
2025-06-26 17:47:32 +05:30
|
|
|
import { deleteWallApi } from '../../../services/factoryBuilder/wall/deleteWallApi';
|
|
|
|
|
import { useVersionContext } from '../version/versionContext';
|
|
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
|
import { upsertWallApi } from '../../../services/factoryBuilder/wall/upsertWallApi';
|
|
|
|
|
import { getUserData } from '../../../functions/getUserData';
|
2025-06-10 15:28:23 +05:30
|
|
|
|
|
|
|
|
interface LineProps {
|
|
|
|
|
points: [Point, Point];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Line({ points }: Readonly<LineProps>) {
|
2025-06-23 13:38:26 +05:30
|
|
|
const [isHovered, setIsHovered] = useState(false);
|
|
|
|
|
const { raycaster, camera, pointer, gl } = useThree();
|
|
|
|
|
const plane = useMemo(() => new THREE.Plane(new THREE.Vector3(0, 1, 0), 0), []);
|
|
|
|
|
const [isDeletable, setIsDeletable] = useState(false);
|
2025-06-26 17:47:32 +05:30
|
|
|
const { socket } = useSocketStore();
|
2025-06-23 13:38:26 +05:30
|
|
|
const { toolMode } = useToolMode();
|
2025-06-26 17:47:32 +05:30
|
|
|
const { wallStore, floorStore } = useSceneContext();
|
|
|
|
|
const { removeWallByPoints, setPosition: setWallPosition, getWallsByPointId } = wallStore();
|
|
|
|
|
const { removeFloorByPoints, setPosition: setFloorPosition } = floorStore();
|
|
|
|
|
const { userId, organization } = getUserData();
|
|
|
|
|
const { selectedVersionStore } = useVersionContext();
|
|
|
|
|
const { selectedVersion } = selectedVersionStore();
|
|
|
|
|
const { projectId } = useParams();
|
2025-06-23 13:38:26 +05:30
|
|
|
const [dragOffset, setDragOffset] = useState<THREE.Vector3 | null>(null);
|
|
|
|
|
const { hoveredLine, setHoveredLine, hoveredPoint } = useBuilderStore();
|
|
|
|
|
|
2025-06-10 15:28:23 +05:30
|
|
|
const path = useMemo(() => {
|
|
|
|
|
const [start, end] = points.map(p => new THREE.Vector3(...p.position));
|
|
|
|
|
return new THREE.LineCurve3(start, end);
|
|
|
|
|
}, [points]);
|
|
|
|
|
|
|
|
|
|
const colors = getColor(points[0]);
|
|
|
|
|
|
|
|
|
|
function getColor(point: Point) {
|
|
|
|
|
if (point.pointType === 'Aisle') {
|
|
|
|
|
return {
|
|
|
|
|
defaultLineColor: Constants.lineConfig.aisleColor,
|
|
|
|
|
defaultDeleteColor: Constants.lineConfig.deleteColor,
|
|
|
|
|
}
|
|
|
|
|
} else if (point.pointType === 'Floor') {
|
|
|
|
|
return {
|
|
|
|
|
defaultLineColor: Constants.lineConfig.floorColor,
|
|
|
|
|
defaultDeleteColor: Constants.lineConfig.deleteColor,
|
|
|
|
|
}
|
|
|
|
|
} else if (point.pointType === 'Wall') {
|
|
|
|
|
return {
|
|
|
|
|
defaultLineColor: Constants.lineConfig.wallColor,
|
|
|
|
|
defaultDeleteColor: Constants.lineConfig.deleteColor,
|
|
|
|
|
}
|
|
|
|
|
} else if (point.pointType === 'Zone') {
|
|
|
|
|
return {
|
|
|
|
|
defaultLineColor: Constants.lineConfig.zoneColor,
|
|
|
|
|
defaultDeleteColor: Constants.lineConfig.deleteColor,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
defaultLineColor: Constants.lineConfig.defaultColor,
|
|
|
|
|
defaultDeleteColor: Constants.lineConfig.deleteColor,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-23 13:38:26 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (toolMode === '2D-Delete') {
|
|
|
|
|
if (isHovered && !hoveredPoint) {
|
|
|
|
|
setIsDeletable(true);
|
|
|
|
|
} else {
|
|
|
|
|
setIsDeletable(false);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
setIsDeletable(false);
|
|
|
|
|
}
|
|
|
|
|
}, [isHovered, colors.defaultLineColor, colors.defaultDeleteColor, toolMode, hoveredPoint]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (hoveredLine && (hoveredLine[0].pointUuid !== points[0].pointUuid || hoveredLine[1].pointUuid !== points[1].pointUuid)) {
|
|
|
|
|
setIsHovered(false);
|
|
|
|
|
}
|
|
|
|
|
}, [hoveredLine])
|
|
|
|
|
|
|
|
|
|
const handlePointClick = (points: [Point, Point]) => {
|
|
|
|
|
if (toolMode === '2D-Delete') {
|
|
|
|
|
if (points[0].pointType === 'Wall' && points[1].pointType === 'Wall') {
|
2025-06-26 17:47:32 +05:30
|
|
|
const removedWall = removeWallByPoints(points);
|
|
|
|
|
if (removedWall && projectId) {
|
|
|
|
|
|
|
|
|
|
// API
|
|
|
|
|
|
|
|
|
|
// deleteWallApi(projectId, selectedVersion?.versionId || '', removedWall.wallUuid);
|
|
|
|
|
|
|
|
|
|
// SOCKET
|
|
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
|
wallUuid: removedWall.wallUuid,
|
|
|
|
|
projectId: projectId,
|
|
|
|
|
versionId: selectedVersion?.versionId || '',
|
|
|
|
|
userId: userId,
|
|
|
|
|
organization: organization
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
socket.emit('v1:model-Wall:delete', data);
|
|
|
|
|
}
|
|
|
|
|
setHoveredLine(null);
|
|
|
|
|
}
|
|
|
|
|
if (points[0].pointType === 'Floor' && points[1].pointType === 'Floor') {
|
|
|
|
|
removeFloorByPoints(points);
|
2025-06-25 15:26:53 +05:30
|
|
|
setHoveredLine(null);
|
2025-06-23 13:38:26 +05:30
|
|
|
}
|
|
|
|
|
gl.domElement.style.cursor = 'default';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleDrag = (points: [Point, Point]) => {
|
|
|
|
|
if (toolMode === 'move' && isHovered && dragOffset) {
|
|
|
|
|
raycaster.setFromCamera(pointer, camera);
|
|
|
|
|
const intersectionPoint = new THREE.Vector3();
|
|
|
|
|
const hit = raycaster.ray.intersectPlane(plane, intersectionPoint);
|
|
|
|
|
|
|
|
|
|
if (hit) {
|
|
|
|
|
gl.domElement.style.cursor = 'move';
|
|
|
|
|
const positionWithOffset = new THREE.Vector3().addVectors(hit, dragOffset);
|
|
|
|
|
|
|
|
|
|
const start = new THREE.Vector3(...points[0].position);
|
|
|
|
|
const end = new THREE.Vector3(...points[1].position);
|
|
|
|
|
const midPoint = new THREE.Vector3().addVectors(start, end).multiplyScalar(0.5);
|
|
|
|
|
|
|
|
|
|
const delta = new THREE.Vector3().subVectors(positionWithOffset, midPoint);
|
|
|
|
|
|
|
|
|
|
const newStart = new THREE.Vector3().addVectors(start, delta);
|
|
|
|
|
const newEnd = new THREE.Vector3().addVectors(end, delta);
|
|
|
|
|
|
2025-06-26 17:47:32 +05:30
|
|
|
if (points[0].pointType === 'Wall' && points[1].pointType === 'Wall') {
|
|
|
|
|
setWallPosition(points[0].pointUuid, [newStart.x, newStart.y, newStart.z]);
|
|
|
|
|
setWallPosition(points[1].pointUuid, [newEnd.x, newEnd.y, newEnd.z]);
|
|
|
|
|
}
|
|
|
|
|
if (points[0].pointType === 'Floor' && points[1].pointType === 'Floor') {
|
|
|
|
|
setFloorPosition(points[0].pointUuid, [newStart.x, newStart.y, newStart.z]);
|
|
|
|
|
setFloorPosition(points[1].pointUuid, [newEnd.x, newEnd.y, newEnd.z]);
|
|
|
|
|
}
|
2025-06-23 13:38:26 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDragStart = (points: [Point, Point]) => {
|
|
|
|
|
raycaster.setFromCamera(pointer, camera);
|
|
|
|
|
const intersectionPoint = new THREE.Vector3();
|
|
|
|
|
const hit = raycaster.ray.intersectPlane(plane, intersectionPoint);
|
|
|
|
|
|
|
|
|
|
if (hit && !hoveredPoint) {
|
|
|
|
|
const start = new THREE.Vector3(...points[0].position);
|
|
|
|
|
const end = new THREE.Vector3(...points[1].position);
|
|
|
|
|
const midPoint = new THREE.Vector3().addVectors(start, end).multiplyScalar(0.5);
|
|
|
|
|
|
|
|
|
|
const offset = new THREE.Vector3().subVectors(midPoint, hit);
|
|
|
|
|
setDragOffset(offset);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDragEnd = (points: [Point, Point]) => {
|
2025-06-26 17:47:32 +05:30
|
|
|
if (toolMode !== 'move' || !dragOffset) return;
|
2025-06-23 13:38:26 +05:30
|
|
|
gl.domElement.style.cursor = 'default';
|
|
|
|
|
setDragOffset(null);
|
|
|
|
|
if (points[0].pointType === 'Wall' && points[1].pointType === 'Wall') {
|
2025-06-26 17:47:32 +05:30
|
|
|
const updatedWalls1 = getWallsByPointId(points[0].pointUuid);
|
|
|
|
|
const updatedWalls2 = getWallsByPointId(points[1].pointUuid);
|
|
|
|
|
const updatedWalls = [...updatedWalls1, ...updatedWalls2].filter((wall, index, self) => index === self.findIndex((w) => w.wallUuid === wall.wallUuid));
|
|
|
|
|
|
|
|
|
|
if (updatedWalls.length > 0 && projectId) {
|
|
|
|
|
updatedWalls.forEach(updatedWall => {
|
|
|
|
|
|
|
|
|
|
// API
|
|
|
|
|
|
|
|
|
|
// upsertWallApi(projectId, selectedVersion?.versionId || '', updatedWall).catch((error) => {
|
|
|
|
|
// console.error('Error updating wall:', error);
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// SOCKET
|
|
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
|
wallData: updatedWall,
|
|
|
|
|
projectId: projectId,
|
|
|
|
|
versionId: selectedVersion?.versionId || '',
|
|
|
|
|
userId: userId,
|
|
|
|
|
organization: organization
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
socket.emit('v1:model-Wall:add', data);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} else if (points[0].pointType === 'Floor' && points[1].pointType === 'Floor') {
|
|
|
|
|
// Handle floor update logic here if needed
|
2025-06-23 13:38:26 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-10 15:28:23 +05:30
|
|
|
return (
|
2025-06-23 13:38:26 +05:30
|
|
|
<DragControls
|
|
|
|
|
axisLock="y"
|
|
|
|
|
autoTransform={false}
|
|
|
|
|
onDragStart={() => handleDragStart(points)}
|
|
|
|
|
onDrag={() => handleDrag(points)}
|
|
|
|
|
onDragEnd={() => handleDragEnd(points)}
|
2025-06-10 15:28:23 +05:30
|
|
|
>
|
2025-06-23 13:38:26 +05:30
|
|
|
<Tube
|
|
|
|
|
name={`${points[0].pointType}-Line`}
|
|
|
|
|
key={`${points[0].pointUuid}-${points[1].pointUuid}`}
|
|
|
|
|
uuid={`${points[0].pointUuid}-${points[1].pointUuid}`}
|
|
|
|
|
userData={{ points, path }}
|
|
|
|
|
args={[path, Constants.lineConfig.tubularSegments, Constants.lineConfig.radius, Constants.lineConfig.radialSegments, false]}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
handlePointClick(points);
|
|
|
|
|
}}
|
|
|
|
|
onPointerOver={() => {
|
|
|
|
|
if (!hoveredLine) {
|
|
|
|
|
setHoveredLine(points);
|
|
|
|
|
setIsHovered(true)
|
|
|
|
|
if (toolMode === 'move' && !hoveredPoint) {
|
|
|
|
|
gl.domElement.style.cursor = 'pointer';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
onPointerOut={() => {
|
|
|
|
|
if (hoveredLine) {
|
|
|
|
|
setHoveredLine(null);
|
|
|
|
|
gl.domElement.style.cursor = 'default';
|
|
|
|
|
}
|
|
|
|
|
setIsHovered(false)
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<meshStandardMaterial color={isDeletable ? colors.defaultDeleteColor : colors.defaultLineColor} />
|
|
|
|
|
</Tube>
|
|
|
|
|
</DragControls >
|
2025-06-10 15:28:23 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Line;
|