Files
Dwinzo_Demo/app/src/modules/builder/line/line.tsx

70 lines
2.7 KiB
TypeScript
Raw Normal View History

import * as THREE from "three";
2025-09-15 10:51:29 +05:30
import { useMemo } from "react";
2025-09-15 11:30:02 +05:30
import { DragControls, Line as DreiLine } from "@react-three/drei";
import * as Constants from "../../../types/world/worldConstants";
2025-09-15 10:51:29 +05:30
import { useLineEventHandler } from "./eventHandler/useLineEventHandler";
2025-06-10 15:28:23 +05:30
interface LineProps {
points: [Point, Point];
}
function Line({ points }: Readonly<LineProps>) {
const path = useMemo(() => {
const [start, end] = points.map((p) => new THREE.Vector3(...p.position));
2025-06-10 15:28:23 +05:30
return new THREE.LineCurve3(start, end);
}, [points]);
2025-09-15 11:30:02 +05:30
const positions = useMemo(() => points.map((p) => new THREE.Vector3(...p.position)), [points]);
2025-06-10 15:28:23 +05:30
const colors = getColor(points[0]);
function getColor(point: Point) {
if (point.pointType === "Aisle") {
2025-06-10 15:28:23 +05:30
return {
defaultLineColor: Constants.lineConfig.aisleColor,
defaultDeleteColor: Constants.lineConfig.deleteColor,
};
} else if (point.pointType === "Floor") {
2025-06-10 15:28:23 +05:30
return {
defaultLineColor: Constants.lineConfig.floorColor,
defaultDeleteColor: Constants.lineConfig.deleteColor,
};
} else if (point.pointType === "Wall") {
2025-06-10 15:28:23 +05:30
return {
defaultLineColor: Constants.lineConfig.wallColor,
defaultDeleteColor: Constants.lineConfig.deleteColor,
};
} else if (point.pointType === "Zone") {
2025-06-10 15:28:23 +05:30
return {
defaultLineColor: Constants.lineConfig.zoneColor,
defaultDeleteColor: Constants.lineConfig.deleteColor,
};
2025-06-10 15:28:23 +05:30
} else {
return {
defaultLineColor: Constants.lineConfig.defaultColor,
defaultDeleteColor: Constants.lineConfig.deleteColor,
};
2025-06-10 15:28:23 +05:30
}
}
2025-09-15 10:51:29 +05:30
const { isDeletable, handleDrag, handleDragStart, handleDragEnd, handleLineClick, handlePointerOver, handlePointerOut } = useLineEventHandler({ points });
2025-06-10 15:28:23 +05:30
return (
2025-09-15 10:51:29 +05:30
<DragControls axisLock="y" autoTransform={false} onDragStart={handleDragStart} onDrag={handleDrag} onDragEnd={handleDragEnd}>
2025-09-15 11:30:02 +05:30
<DreiLine
name={`${points[0].pointType}-Line`}
key={`${points[0].pointUuid}-${points[1].pointUuid}`}
uuid={`${points[0].pointUuid}-${points[1].pointUuid}`}
userData={{ points, path }}
2025-09-15 11:30:02 +05:30
points={positions}
2025-09-15 11:31:49 +05:30
lineWidth={Constants.lineConfig.width}
2025-09-15 11:30:02 +05:30
color={isDeletable ? colors.defaultDeleteColor : colors.defaultLineColor}
2025-09-15 10:51:29 +05:30
onClick={handleLineClick}
onPointerOver={handlePointerOver}
onPointerOut={handlePointerOut}
2025-09-15 11:30:02 +05:30
/>
</DragControls>
2025-06-10 15:28:23 +05:30
);
}
export default Line;