Dwinzo_dev/app/src/components/ui/componets/Dropped3dWidget.tsx

359 lines
15 KiB
TypeScript
Raw Normal View History

import { useThree } from "@react-three/fiber";
2025-04-04 09:47:38 +00:00
import React, { useEffect, useRef } from "react";
import { useAsset3dWidget, useSocketStore, useWidgetSubOption } from "../../../store/store";
import useModuleStore from "../../../store/useModuleStore";
import { ThreeState } from "../../../types/world/worldTypes";
import * as THREE from "three";
import Throughput from "../../layout/3D-cards/cards/Throughput";
import ProductionCapacity from "../../layout/3D-cards/cards/ProductionCapacity";
import ReturnOfInvestment from "../../layout/3D-cards/cards/ReturnOfInvestment";
import StateWorking from "../../layout/3D-cards/cards/StateWorking";
import { useSelectedZoneStore } from "../../../store/useZoneStore";
import { generateUniqueId } from "../../../functions/generateUniqueId";
import { adding3dWidgets } from "../../../services/realTimeVisulization/zoneData/add3dWidget";
import { get3dWidgetZoneData } from "../../../services/realTimeVisulization/zoneData/get3dWidgetData";
import { use3DWidget } from "../../../store/useDroppedObjectsStore";
2025-04-03 14:01:25 +00:00
import { useLeftData, useRightClickSelected, useRightSelected, useTopData, useZoneWidgetStore } from "../../../store/useZone3DWidgetStore";
import { useWidgetStore } from "../../../store/useWidgetStore";
import EditWidgetOption from "../menu/EditWidgetOption";
2025-04-04 09:47:38 +00:00
type WidgetData = {
id: string;
type: string;
position: [number, number, number];
rotation?: [number, number, number];
tempPosition?: [number, number, number];
};
export default function Dropped3dWidgets() {
const { widgetSelect } = useAsset3dWidget();
const { activeModule } = useModuleStore();
2025-04-03 14:01:25 +00:00
const { raycaster, gl, scene, mouse, camera }: ThreeState = useThree();
const { widgetSubOption } = useWidgetSubOption();
const { selectedZone } = useSelectedZoneStore();
2025-04-04 09:47:38 +00:00
const { top, setTop } = useTopData();
const { left, setLeft } = useLeftData();
const { rightSelect, setRightSelect } = useRightSelected();
2025-04-04 09:47:38 +00:00
const { zoneWidgetData, setZoneWidgetData, addWidget, updateWidgetPosition, updateWidgetRotation } = useZoneWidgetStore();
const { setWidgets3D } = use3DWidget();
const { visualizationSocket } = useSocketStore();
2025-04-04 09:47:38 +00:00
const { rightClickSelected, setRightClickSelected } = useRightClickSelected();
2025-04-03 14:01:25 +00:00
const plane = useRef(new THREE.Plane(new THREE.Vector3(0, 1, 0), 0)); // Floor plane for horizontal move
const verticalPlane = useRef(new THREE.Plane(new THREE.Vector3(0, 0, 1), 0)); // Vertical plane for vertical move
const planeIntersect = useRef(new THREE.Vector3());
2025-04-04 09:47:38 +00:00
// const plane = useRef(new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
// const verticalPlane = useRef(new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
// const planeIntersect = useRef(new THREE.Vector3());
const rotationStartRef = useRef<[number, number, number]>([0, 0, 0]);
const mouseStartRef = useRef<{ x: number; y: number }>({ x: 0, y: 0 });
useEffect(() => {
if (activeModule !== "visualization") return;
if (!selectedZone.zoneId) return;
2025-04-03 14:01:25 +00:00
const email = localStorage.getItem("email") || "";
const organization = email?.split("@")[1]?.split(".")[0];
async function get3dWidgetData() {
2025-04-04 09:47:38 +00:00
const result = await get3dWidgetZoneData(selectedZone.zoneId, organization);
setWidgets3D(result);
2025-04-04 09:47:38 +00:00
const formattedWidgets = result.map((widget: WidgetData) => ({
id: widget.id,
type: widget.type,
position: widget.position,
2025-04-04 09:47:38 +00:00
rotation: widget.rotation || [0, 0, 0],
}));
setZoneWidgetData(selectedZone.zoneId, formattedWidgets);
}
get3dWidgetData();
}, [selectedZone.zoneId, activeModule]);
useEffect(() => {
if (activeModule !== "visualization") return;
if (widgetSubOption === "Floating" || widgetSubOption === "2D") return;
if (selectedZone.zoneName === "") return;
const canvasElement = gl.domElement;
const onDrop = async (event: DragEvent) => {
event.preventDefault();
const email = localStorage.getItem("email") || "";
const organization = email?.split("@")[1]?.split(".")[0];
if (!widgetSelect.startsWith("ui")) return;
const group1 = scene.getObjectByName("itemsGroup");
if (!group1) return;
const intersects = raycaster.intersectObjects(scene.children, true).filter(
(intersect) =>
!intersect.object.name.includes("Roof") &&
!intersect.object.name.includes("agv-collider") &&
!intersect.object.name.includes("MeasurementReference") &&
!intersect.object.userData.isPathObject &&
!(intersect.object.type === "GridHelper")
);
if (intersects.length > 0) {
const { x, y, z } = intersects[0].point;
2025-04-04 09:47:38 +00:00
const newWidget: WidgetData = {
id: generateUniqueId(),
type: widgetSelect,
2025-04-04 09:47:38 +00:00
position: [x, y, z],
rotation: [0, 0, 0],
};
2025-04-04 09:47:38 +00:00
const add3dWidget = {
organization: organization,
widget: newWidget,
zoneId: selectedZone.zoneId
2025-04-04 09:47:38 +00:00
};
2025-04-03 14:01:25 +00:00
if (visualizationSocket) {
2025-04-04 09:47:38 +00:00
visualizationSocket.emit("v2:viz-3D-widget:add", add3dWidget);
}
addWidget(selectedZone.zoneId, newWidget);
}
};
canvasElement.addEventListener("drop", onDrop);
return () => {
canvasElement.removeEventListener("drop", onDrop);
};
}, [widgetSelect, activeModule, selectedZone.zoneId, widgetSubOption]);
const activeZoneWidgets = zoneWidgetData[selectedZone.zoneId] || [];
2025-04-03 14:01:25 +00:00
useEffect(() => {
if (!rightClickSelected) return;
const email = localStorage.getItem("email") || "";
const organization = email?.split("@")[1]?.split(".")[0];
2025-04-04 09:47:38 +00:00
2025-04-03 14:01:25 +00:00
if (rightSelect === "Duplicate") {
2025-04-04 09:47:38 +00:00
const widgetToDuplicate = activeZoneWidgets.find((w: WidgetData) => w.id === rightClickSelected);
2025-04-03 14:01:25 +00:00
if (!widgetToDuplicate) return;
2025-04-04 09:47:38 +00:00
const newWidget: WidgetData = {
2025-04-03 14:01:25 +00:00
id: generateUniqueId(),
type: widgetToDuplicate.type,
position: [
2025-04-04 09:47:38 +00:00
widgetToDuplicate.position[0] + 0.5,
2025-04-03 14:01:25 +00:00
widgetToDuplicate.position[1],
widgetToDuplicate.position[2] + 0.5,
2025-04-04 09:47:38 +00:00
],
rotation: widgetToDuplicate.rotation || [0, 0, 0],
2025-04-03 14:01:25 +00:00
};
2025-04-04 09:47:38 +00:00
const add3dWidget = {
2025-04-03 14:01:25 +00:00
organization,
widget: newWidget,
zoneId: selectedZone.zoneId
};
2025-04-04 09:47:38 +00:00
2025-04-03 14:01:25 +00:00
addWidget(selectedZone.zoneId, newWidget);
setRightSelect(null);
setRightClickSelected(null);
}
2025-04-04 09:47:38 +00:00
2025-04-03 14:01:25 +00:00
if (rightSelect === "Delete") {
2025-04-04 09:47:38 +00:00
const deleteWidget = {
2025-04-03 14:01:25 +00:00
organization,
widgetId: rightClickSelected,
zoneId: selectedZone.zoneId
};
2025-04-04 09:47:38 +00:00
setZoneWidgetData(
selectedZone.zoneId,
activeZoneWidgets.filter((w: WidgetData) => w.id !== rightClickSelected)
);
2025-04-03 14:01:25 +00:00
setRightClickSelected(null);
setRightSelect(null);
}
}, [rightSelect, rightClickSelected]);
useEffect(() => {
2025-04-04 09:47:38 +00:00
const handleMouseDown = (event: MouseEvent) => {
2025-04-03 14:01:25 +00:00
if (!rightClickSelected || !rightSelect) return;
2025-04-04 09:47:38 +00:00
if (rightSelect === "RotateX" || rightSelect === "RotateY") {
mouseStartRef.current = { x: event.clientX, y: event.clientY };
const selectedZone = Object.keys(zoneWidgetData).find((zoneId: string) =>
zoneWidgetData[zoneId].some((widget: WidgetData) => widget.id === rightClickSelected)
);
if (!selectedZone) return;
2025-04-03 14:01:25 +00:00
2025-04-04 09:47:38 +00:00
const selectedWidget = zoneWidgetData[selectedZone].find((widget: WidgetData) => widget.id === rightClickSelected);
if (selectedWidget) {
rotationStartRef.current = selectedWidget.rotation || [0, 0, 0];
}
}
};
const handleMouseMove = (event: MouseEvent) => {
if (!rightClickSelected || !rightSelect) return;
const selectedZone = Object.keys(zoneWidgetData).find((zoneId: string) =>
zoneWidgetData[zoneId].some((widget: WidgetData) => widget.id === rightClickSelected)
2025-04-03 14:01:25 +00:00
);
if (!selectedZone) return;
2025-04-04 09:47:38 +00:00
const selectedWidget = zoneWidgetData[selectedZone].find((widget: WidgetData) => widget.id === rightClickSelected);
2025-04-03 14:01:25 +00:00
if (!selectedWidget) return;
const rect = gl.domElement.getBoundingClientRect();
mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
if (rightSelect === "Horizontal Move" && raycaster.ray.intersectPlane(plane.current, planeIntersect.current)) {
2025-04-04 09:47:38 +00:00
const newPosition: [number, number, number] = [
2025-04-03 14:01:25 +00:00
planeIntersect.current.x,
selectedWidget.position[1],
planeIntersect.current.z
2025-04-04 09:47:38 +00:00
];
updateWidgetPosition(selectedZone, rightClickSelected, newPosition);
console.log('Horizontal Move - Final Position:', newPosition);
2025-04-03 14:01:25 +00:00
}
if (rightSelect === "Vertical Move") {
if (raycaster.ray.intersectPlane(verticalPlane.current, planeIntersect.current)) {
updateWidgetPosition(selectedZone, rightClickSelected, [
selectedWidget.position[0],
2025-04-04 09:47:38 +00:00
planeIntersect.current.y,
2025-04-03 14:01:25 +00:00
selectedWidget.position[2]
]);
}
}
2025-04-04 09:47:38 +00:00
if (rightSelect === "RotateX") {
const deltaX = event.clientX - mouseStartRef.current.x;
const rotationSpeed = 0.03;
const newRotation: [number, number, number] = [
rotationStartRef.current[0] + deltaX * rotationSpeed,
rotationStartRef.current[1],
rotationStartRef.current[2]
];
updateWidgetRotation(selectedZone, rightClickSelected, newRotation);
}
2025-04-03 14:01:25 +00:00
2025-04-04 09:47:38 +00:00
if (rightSelect === "RotateY") {
const deltaY = event.clientY - mouseStartRef.current.y;
const rotationSpeed = 0.03;
const newRotation: [number, number, number] = [
rotationStartRef.current[0],
rotationStartRef.current[1] + deltaY * rotationSpeed,
rotationStartRef.current[2]
];
updateWidgetRotation(selectedZone, rightClickSelected, newRotation);
}
if (rightSelect === "RotateZ") {
const deltaX = event.movementX;
const rotationSpeed = 0.03;
const currentRotation = selectedWidget.rotation || [0, 0, 0];
const newRotation: [number, number, number] = [
currentRotation[0],
currentRotation[1],
currentRotation[2] + deltaX * rotationSpeed
];
updateWidgetRotation(selectedZone, rightClickSelected, newRotation);
}
2025-04-03 14:01:25 +00:00
2025-04-04 09:47:38 +00:00
};
const handleMouseUp = () => {
if (rightClickSelected && (
rightSelect === "Horizontal Move" ||
rightSelect === "Vertical Move" ||
rightSelect === "RotateX" ||
rightSelect === "RotateY" || rightSelect === "RotateZ"
)) {
2025-04-03 14:01:25 +00:00
setTimeout(() => {
setRightClickSelected(null);
setRightSelect(null);
}, 50);
}
};
2025-04-04 09:47:38 +00:00
window.addEventListener("mousedown", handleMouseDown);
2025-04-03 14:01:25 +00:00
window.addEventListener("mousemove", handleMouseMove);
window.addEventListener("mouseup", handleMouseUp);
return () => {
2025-04-04 09:47:38 +00:00
window.removeEventListener("mousedown", handleMouseDown);
2025-04-03 14:01:25 +00:00
window.removeEventListener("mousemove", handleMouseMove);
window.removeEventListener("mouseup", handleMouseUp);
};
}, [rightClickSelected, rightSelect, zoneWidgetData, gl]);
return (
<>
2025-04-04 09:47:38 +00:00
{activeZoneWidgets.map(({ id, type, position, rotation = [0, 0, 0] }: WidgetData) => {
const handleRightClick = (event: React.MouseEvent, id: string) => {
2025-04-03 14:01:25 +00:00
event.preventDefault();
2025-04-04 09:47:38 +00:00
const canvasElement = document.getElementById("real-time-vis-canvas");
if (!canvasElement) throw new Error("Canvas element not found");
const canvasRect = canvasElement.getBoundingClientRect();
const relativeX = event.clientX - canvasRect.left;
const relativeY = event.clientY - canvasRect.top;
setRightClickSelected(id);
setTop(relativeY);
setLeft(relativeX);
2025-04-03 14:01:25 +00:00
};
2025-04-04 09:47:38 +00:00
switch (type) {
case "ui-Widget 1":
2025-04-04 09:47:38 +00:00
return (
<ProductionCapacity
key={id}
id={id}
type={type}
position={position}
rotation={rotation}
onContextMenu={(e) => handleRightClick(e, id)}
/>
);
case "ui-Widget 2":
2025-04-04 09:47:38 +00:00
return (
<ReturnOfInvestment
key={id}
id={id}
type={type}
position={position}
// rotation={rotation}
onContextMenu={(e) => handleRightClick(e, id)}
/>
);
case "ui-Widget 3":
2025-04-04 09:47:38 +00:00
return (
<StateWorking
key={id}
id={id}
type={type}
position={position}
// rotation={rotation}
onContextMenu={(e) => handleRightClick(e, id)}
/>
);
case "ui-Widget 4":
2025-04-04 09:47:38 +00:00
return (
<Throughput
key={id}
id={id}
type={type}
position={position}
// rotation={rotation}
onContextMenu={(e) => handleRightClick(e, id)}
/>
);
default:
return null;
}
})}
</>
);
2025-04-04 09:47:38 +00:00
}