2025-03-28 13:43:20 +00:00
|
|
|
|
|
|
|
import { useThree } from "@react-three/fiber";
|
|
|
|
import React, { useState, useEffect } from "react";
|
2025-03-29 13:51:20 +00:00
|
|
|
import { useAsset3dWidget, useWidgetSubOption } from "../../../store/store";
|
2025-03-28 13:43:20 +00:00
|
|
|
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";
|
2025-03-29 13:51:20 +00:00
|
|
|
import { useSelectedZoneStore } from "../../../store/useZoneStore";
|
2025-03-28 13:43:20 +00:00
|
|
|
|
|
|
|
export default function Dropped3dWidgets() {
|
2025-03-29 13:51:20 +00:00
|
|
|
const { widgetSelect, setWidgetSelect } = useAsset3dWidget();
|
2025-03-28 13:43:20 +00:00
|
|
|
const { activeModule } = useModuleStore();
|
|
|
|
const { raycaster, gl, scene }: ThreeState = useThree();
|
2025-03-29 13:51:20 +00:00
|
|
|
const { selectedZone } = useSelectedZoneStore(); // Get currently selected zone
|
|
|
|
const { widgetSubOption, setWidgetSubOption } = useWidgetSubOption()
|
|
|
|
// 🔥 Store widget positions per zone
|
|
|
|
const [zoneWidgets, setZoneWidgets] = useState<Record<
|
|
|
|
string, // Zone ID
|
|
|
|
Record<string, [number, number, number][]> // Widget type -> Positions array
|
|
|
|
>>({});
|
2025-03-28 13:43:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-03-29 13:51:20 +00:00
|
|
|
if (widgetSubOption === "Floating") return
|
|
|
|
// if (activeModule !== "visualization") return;
|
2025-03-28 13:43:20 +00:00
|
|
|
const canvasElement = gl.domElement;
|
|
|
|
const onDrop = (event: DragEvent) => {
|
|
|
|
event.preventDefault(); // Prevent default browser behavior
|
2025-03-29 13:51:20 +00:00
|
|
|
if (widgetSubOption === "3D") {
|
|
|
|
if (selectedZone.zoneName === "") return
|
|
|
|
if (!widgetSelect?.startsWith("ui")) return;
|
|
|
|
const group1 = scene.getObjectByName("itemsGroup");
|
|
|
|
if (!group1) return;
|
|
|
|
const Assets = group1.children
|
|
|
|
.map((val) => scene.getObjectByProperty("uuid", val.uuid))
|
|
|
|
.filter(Boolean) as THREE.Object3D[];
|
|
|
|
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;
|
|
|
|
|
|
|
|
setZoneWidgets((prev) => ({
|
|
|
|
...prev,
|
|
|
|
[selectedZone.zoneId]: {
|
|
|
|
...(prev[selectedZone.zoneId] || {}),
|
|
|
|
[widgetSelect]: [...(prev[selectedZone.zoneId]?.[widgetSelect] || []), [x, y, z]],
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
2025-03-28 13:43:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
canvasElement.addEventListener("drop", onDrop);
|
|
|
|
return () => {
|
2025-03-29 13:51:20 +00:00
|
|
|
canvasElement.removeEventListener("drop", onDrop)
|
|
|
|
// setWidgetSelect()
|
2025-03-28 13:43:20 +00:00
|
|
|
};
|
2025-03-29 13:51:20 +00:00
|
|
|
}, [widgetSelect, activeModule, widgetSubOption]);
|
2025-03-28 13:43:20 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2025-03-29 13:51:20 +00:00
|
|
|
{zoneWidgets[selectedZone.zoneId]?.["ui-Widget 1"]?.map((pos, index) => (
|
2025-03-28 13:43:20 +00:00
|
|
|
<ProductionCapacity key={`Widget1-${index}`} position={pos} />
|
|
|
|
))}
|
2025-03-29 13:51:20 +00:00
|
|
|
{zoneWidgets[selectedZone.zoneId]?.["ui-Widget 2"]?.map((pos, index) => (
|
2025-03-28 13:43:20 +00:00
|
|
|
<ReturnOfInvestment key={`Widget2-${index}`} position={pos} />
|
|
|
|
))}
|
2025-03-29 13:51:20 +00:00
|
|
|
{zoneWidgets[selectedZone.zoneId]?.["ui-Widget 3"]?.map((pos, index) => (
|
2025-03-28 13:43:20 +00:00
|
|
|
<StateWorking key={`Widget3-${index}`} position={pos} />
|
|
|
|
))}
|
2025-03-29 13:51:20 +00:00
|
|
|
{zoneWidgets[selectedZone.zoneId]?.["ui-Widget 4"]?.map((pos, index) => (
|
2025-03-28 13:43:20 +00:00
|
|
|
<Throughput key={`Widget4-${index}`} position={pos} />
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-03-29 13:51:20 +00:00
|
|
|
|