2025-04-21 06:23:42 +00:00
|
|
|
import * as THREE from "three";
|
2025-05-02 08:34:52 +00:00
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
2025-04-21 06:23:42 +00:00
|
|
|
import { useFrame, useThree } from "@react-three/fiber";
|
2025-05-02 08:34:52 +00:00
|
|
|
import { useFloorItems, useSelectedAssets, useSocketStore, useStartSimulation, useToggleView } from "../../../../store/store";
|
2025-04-21 06:23:42 +00:00
|
|
|
// import { setFloorItemApi } from '../../../../services/factoryBuilder/assest/floorAsset/setFloorItemApi';
|
|
|
|
import { toast } from "react-toastify";
|
|
|
|
import * as Types from "../../../../types/world/worldTypes";
|
|
|
|
import { detectModifierKeys } from "../../../../utils/shortcutkeys/detectModifierKeys";
|
2025-04-29 13:45:17 +00:00
|
|
|
import { useEventsStore } from "../../../../store/simulation/useEventsStore";
|
|
|
|
import { useProductStore } from "../../../../store/simulation/useProductStore";
|
|
|
|
import { useSelectedProduct } from "../../../../store/simulation/useSimulationStore";
|
2025-04-30 14:46:26 +00:00
|
|
|
import { upsertProductOrEventApi } from "../../../../services/simulation/UpsertProductOrEventApi";
|
2025-05-02 08:34:52 +00:00
|
|
|
import { snapControls } from "../../../../utils/handleSnap";
|
2025-05-09 13:53:08 +00:00
|
|
|
import DistanceFindingControls from "./distanceFindingControls";
|
2025-04-21 06:23:42 +00:00
|
|
|
|
|
|
|
function MoveControls({ movedObjects, setMovedObjects, itemsGroupRef, copiedObjects, setCopiedObjects, pastedObjects, setpastedObjects, duplicatedObjects, setDuplicatedObjects, selectionGroup, rotatedObjects, setRotatedObjects, boundingBoxRef }: any) {
|
2025-05-09 13:53:08 +00:00
|
|
|
|
2025-04-21 06:23:42 +00:00
|
|
|
const { camera, controls, gl, scene, pointer, raycaster } = useThree();
|
|
|
|
const plane = useMemo(() => new THREE.Plane(new THREE.Vector3(0, 1, 0), 0), []);
|
|
|
|
|
|
|
|
const { toggleView } = useToggleView();
|
|
|
|
const { selectedAssets, setSelectedAssets } = useSelectedAssets();
|
2025-04-30 14:46:26 +00:00
|
|
|
const { selectedProduct } = useSelectedProduct();
|
2025-04-21 06:23:42 +00:00
|
|
|
const { floorItems, setFloorItems } = useFloorItems();
|
|
|
|
const { socket } = useSocketStore();
|
|
|
|
const itemsData = useRef<Types.FloorItems>([]);
|
2025-05-02 08:34:52 +00:00
|
|
|
const [keyEvent, setKeyEvent] = useState<"Ctrl" | "Shift" | "Ctrl+Shift" | "">("")
|
2025-04-30 14:46:26 +00:00
|
|
|
const email = localStorage.getItem('email')
|
|
|
|
const organization = (email!.split("@")[1]).split(".")[0];
|
|
|
|
|
|
|
|
const updateBackend = (
|
|
|
|
productName: string,
|
|
|
|
productId: string,
|
|
|
|
organization: string,
|
|
|
|
eventData: EventsSchema
|
|
|
|
) => {
|
|
|
|
upsertProductOrEventApi({
|
|
|
|
productName: productName,
|
|
|
|
productId: productId,
|
|
|
|
organization: organization,
|
|
|
|
eventDatas: eventData
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-04-21 06:23:42 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!camera || !scene || toggleView || !itemsGroupRef.current) return;
|
|
|
|
|
|
|
|
const canvasElement = gl.domElement;
|
|
|
|
canvasElement.tabIndex = 0;
|
|
|
|
|
|
|
|
let isMoving = false;
|
|
|
|
|
|
|
|
const onPointerDown = () => {
|
|
|
|
isMoving = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const onPointerMove = () => {
|
|
|
|
isMoving = true;
|
|
|
|
};
|
2025-05-02 08:34:52 +00:00
|
|
|
const onKeyUp = (event: KeyboardEvent) => {
|
|
|
|
// When any modifier is released, reset snap
|
|
|
|
const isModifierKey =
|
|
|
|
event.key === "Control" || event.key === "Shift";
|
|
|
|
|
|
|
|
if (isModifierKey) {
|
|
|
|
setKeyEvent("");
|
|
|
|
}
|
|
|
|
};
|
2025-04-21 06:23:42 +00:00
|
|
|
|
|
|
|
const onPointerUp = (event: PointerEvent) => {
|
|
|
|
if (!isMoving && movedObjects.length > 0 && event.button === 0) {
|
|
|
|
event.preventDefault();
|
|
|
|
placeMovedAssets();
|
|
|
|
}
|
|
|
|
if (!isMoving && movedObjects.length > 0 && event.button === 2) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
clearSelection();
|
|
|
|
movedObjects.forEach((asset: any) => {
|
|
|
|
if (itemsGroupRef.current) {
|
|
|
|
itemsGroupRef.current.attach(asset);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
setFloorItems([...floorItems, ...itemsData.current]);
|
|
|
|
|
|
|
|
setMovedObjects([]);
|
|
|
|
itemsData.current = [];
|
|
|
|
}
|
2025-05-02 08:34:52 +00:00
|
|
|
setKeyEvent("")
|
2025-04-21 06:23:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
|
|
const keyCombination = detectModifierKeys(event);
|
|
|
|
|
|
|
|
if (pastedObjects.length > 0 || duplicatedObjects.length > 0 || rotatedObjects.length > 0) return;
|
2025-05-02 08:34:52 +00:00
|
|
|
|
|
|
|
if (keyCombination === "Ctrl" || keyCombination === "Ctrl+Shift" || keyCombination === "Shift") {
|
|
|
|
// update state here
|
|
|
|
setKeyEvent(keyCombination)
|
|
|
|
} else {
|
|
|
|
setKeyEvent("")
|
|
|
|
}
|
|
|
|
|
2025-04-21 06:23:42 +00:00
|
|
|
if (keyCombination === "G") {
|
|
|
|
if (selectedAssets.length > 0) {
|
|
|
|
moveAssets();
|
2025-04-30 06:16:20 +00:00
|
|
|
itemsData.current = floorItems.filter((item: { modelUuid: string }) => selectedAssets.some((asset: any) => asset.uuid === item.modelUuid));
|
2025-04-21 06:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
2025-05-02 08:34:52 +00:00
|
|
|
|
2025-04-21 06:23:42 +00:00
|
|
|
if (keyCombination === "ESCAPE") {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
clearSelection();
|
|
|
|
movedObjects.forEach((asset: any) => {
|
|
|
|
if (itemsGroupRef.current) {
|
|
|
|
itemsGroupRef.current.attach(asset);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
setFloorItems([...floorItems, ...itemsData.current]);
|
|
|
|
|
|
|
|
setMovedObjects([]);
|
|
|
|
itemsData.current = [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!toggleView) {
|
|
|
|
canvasElement.addEventListener("pointerdown", onPointerDown);
|
|
|
|
canvasElement.addEventListener("pointermove", onPointerMove);
|
|
|
|
canvasElement.addEventListener("pointerup", onPointerUp);
|
|
|
|
canvasElement.addEventListener("keydown", onKeyDown);
|
2025-05-02 08:34:52 +00:00
|
|
|
canvasElement?.addEventListener("keyup", onKeyUp);
|
2025-04-21 06:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
canvasElement.removeEventListener("pointerdown", onPointerDown);
|
|
|
|
canvasElement.removeEventListener("pointermove", onPointerMove);
|
|
|
|
canvasElement.removeEventListener("pointerup", onPointerUp);
|
|
|
|
canvasElement.removeEventListener("keydown", onKeyDown);
|
2025-05-02 08:34:52 +00:00
|
|
|
canvasElement?.removeEventListener("keyup", onKeyUp);
|
2025-04-21 06:23:42 +00:00
|
|
|
};
|
2025-05-02 08:34:52 +00:00
|
|
|
}, [camera, controls, scene, toggleView, selectedAssets, socket, floorItems, pastedObjects, duplicatedObjects, movedObjects, rotatedObjects, keyEvent]);
|
2025-04-21 06:23:42 +00:00
|
|
|
|
2025-05-02 08:34:52 +00:00
|
|
|
let moveSpeed = keyEvent === "Ctrl" || "Ctrl+Shift" ? 1 : 0.25;
|
2025-04-21 06:23:42 +00:00
|
|
|
|
|
|
|
useFrame(() => {
|
|
|
|
if (movedObjects.length > 0) {
|
|
|
|
const intersectionPoint = new THREE.Vector3();
|
|
|
|
raycaster.setFromCamera(pointer, camera);
|
|
|
|
const point = raycaster.ray.intersectPlane(plane, intersectionPoint);
|
|
|
|
|
|
|
|
if (point) {
|
|
|
|
let targetX = point.x;
|
|
|
|
let targetZ = point.z;
|
2025-05-02 08:34:52 +00:00
|
|
|
if (keyEvent === "Ctrl") {
|
|
|
|
targetX = snapControls(targetX, "Ctrl");
|
|
|
|
targetZ = snapControls(targetZ, "Ctrl");
|
2025-04-21 06:23:42 +00:00
|
|
|
}
|
2025-05-09 13:53:08 +00:00
|
|
|
// else if (keyEvent === "Ctrl+Shift") {
|
|
|
|
// targetX = snapControls(targetX, "Ctrl+Shift");
|
|
|
|
// targetZ = snapControls(targetZ, "Ctrl+Shift");
|
|
|
|
// } else if (keyEvent === "Shift") {
|
|
|
|
// targetX = snapControls(targetX, "Shift");
|
|
|
|
// targetZ = snapControls(targetZ, "Shift");
|
|
|
|
// } else {
|
|
|
|
// }
|
2025-04-21 06:23:42 +00:00
|
|
|
|
|
|
|
const position = new THREE.Vector3();
|
2025-05-09 13:53:08 +00:00
|
|
|
|
2025-04-21 06:23:42 +00:00
|
|
|
if (boundingBoxRef.current) {
|
|
|
|
boundingBoxRef.current.getWorldPosition(position);
|
|
|
|
selectionGroup.current.position.lerp(
|
|
|
|
new THREE.Vector3(
|
|
|
|
targetX - (position.x - selectionGroup.current.position.x),
|
|
|
|
selectionGroup.current.position.y,
|
|
|
|
targetZ - (position.z - selectionGroup.current.position.z)
|
|
|
|
),
|
|
|
|
moveSpeed
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const box = new THREE.Box3();
|
|
|
|
movedObjects.forEach((obj: THREE.Object3D) => box.expandByObject(obj));
|
|
|
|
const center = new THREE.Vector3();
|
|
|
|
box.getCenter(center);
|
|
|
|
|
|
|
|
selectionGroup.current.position.lerp(
|
|
|
|
new THREE.Vector3(
|
|
|
|
targetX - (center.x - selectionGroup.current.position.x),
|
|
|
|
selectionGroup.current.position.y,
|
|
|
|
targetZ - (center.z - selectionGroup.current.position.z)
|
|
|
|
),
|
|
|
|
moveSpeed
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-05-09 13:53:08 +00:00
|
|
|
|
2025-04-21 06:23:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const moveAssets = () => {
|
2025-04-30 06:16:20 +00:00
|
|
|
const updatedItems = floorItems.filter((item: { modelUuid: string }) => !selectedAssets.some((asset: any) => asset.uuid === item.modelUuid));
|
2025-04-21 06:23:42 +00:00
|
|
|
setFloorItems(updatedItems);
|
|
|
|
setMovedObjects(selectedAssets);
|
|
|
|
selectedAssets.forEach((asset: any) => { selectionGroup.current.attach(asset); });
|
|
|
|
}
|
|
|
|
|
|
|
|
const placeMovedAssets = () => {
|
|
|
|
if (movedObjects.length === 0) return;
|
|
|
|
|
|
|
|
movedObjects.forEach(async (obj: THREE.Object3D) => {
|
|
|
|
const worldPosition = new THREE.Vector3();
|
|
|
|
obj.getWorldPosition(worldPosition);
|
|
|
|
|
|
|
|
selectionGroup.current.remove(obj);
|
|
|
|
obj.position.copy(worldPosition);
|
|
|
|
|
|
|
|
if (itemsGroupRef.current) {
|
|
|
|
const newFloorItem: Types.FloorItemType = {
|
2025-04-30 06:16:20 +00:00
|
|
|
modelUuid: obj.uuid,
|
|
|
|
modelName: obj.userData.name,
|
2025-04-21 06:23:42 +00:00
|
|
|
modelfileID: obj.userData.modelId,
|
|
|
|
position: [worldPosition.x, worldPosition.y, worldPosition.z],
|
|
|
|
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z, },
|
|
|
|
isLocked: false,
|
|
|
|
isVisible: true
|
|
|
|
};
|
|
|
|
|
2025-04-29 13:45:17 +00:00
|
|
|
if (obj.userData.eventData) {
|
2025-04-30 06:16:20 +00:00
|
|
|
const eventData = useEventsStore.getState().getEventByModelUuid(obj.userData.modelUuid);
|
|
|
|
const productData = useProductStore.getState().getEventByModelUuid(useSelectedProduct.getState().selectedProduct.productId, obj.userData.modelUuid);
|
2025-04-29 13:45:17 +00:00
|
|
|
|
|
|
|
if (eventData) {
|
2025-04-30 06:16:20 +00:00
|
|
|
useEventsStore.getState().updateEvent(obj.userData.modelUuid, {
|
2025-04-29 13:45:17 +00:00
|
|
|
position: [worldPosition.x, worldPosition.y, worldPosition.z],
|
|
|
|
rotation: [obj.rotation.x, obj.rotation.y, obj.rotation.z],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (productData) {
|
2025-04-30 14:46:26 +00:00
|
|
|
const event = useProductStore.getState().updateEvent(useSelectedProduct.getState().selectedProduct.productId, obj.userData.modelUuid, {
|
2025-04-29 13:45:17 +00:00
|
|
|
position: [worldPosition.x, worldPosition.y, worldPosition.z],
|
|
|
|
rotation: [obj.rotation.x, obj.rotation.y, obj.rotation.z],
|
|
|
|
})
|
2025-04-30 14:46:26 +00:00
|
|
|
|
|
|
|
if (event) {
|
|
|
|
updateBackend(
|
|
|
|
selectedProduct.productName,
|
|
|
|
selectedProduct.productId,
|
|
|
|
organization,
|
|
|
|
event
|
|
|
|
);
|
|
|
|
}
|
2025-05-02 13:03:27 +00:00
|
|
|
|
|
|
|
newFloorItem.eventData = eventData;
|
2025-04-29 13:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-21 06:23:42 +00:00
|
|
|
setFloorItems((prevItems: Types.FloorItems) => {
|
|
|
|
const updatedItems = [...(prevItems || []), newFloorItem];
|
|
|
|
localStorage.setItem("FloorItems", JSON.stringify(updatedItems));
|
|
|
|
return updatedItems;
|
|
|
|
});
|
|
|
|
|
|
|
|
//REST
|
|
|
|
|
|
|
|
// await setFloorItemApi(
|
|
|
|
// organization,
|
|
|
|
// obj.uuid,
|
|
|
|
// obj.userData.name,
|
|
|
|
// [worldPosition.x, worldPosition.y, worldPosition.z],
|
|
|
|
// { "x": obj.rotation.x, "y": obj.rotation.y, "z": obj.rotation.z },
|
|
|
|
// obj.userData.modelId,
|
|
|
|
// false,
|
|
|
|
// true,
|
|
|
|
// );
|
|
|
|
|
|
|
|
//SOCKET
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
organization,
|
2025-04-30 06:16:20 +00:00
|
|
|
modelUuid: newFloorItem.modelUuid,
|
|
|
|
modelName: newFloorItem.modelName,
|
2025-04-21 06:23:42 +00:00
|
|
|
modelfileID: newFloorItem.modelfileID,
|
|
|
|
position: newFloorItem.position,
|
|
|
|
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
|
|
|
isLocked: false,
|
|
|
|
isVisible: true,
|
|
|
|
socketId: socket.id,
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.emit("v2:model-asset:add", data);
|
|
|
|
|
|
|
|
itemsGroupRef.current.add(obj);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
toast.success("Object moved!");
|
|
|
|
|
|
|
|
itemsData.current = [];
|
|
|
|
clearSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
const clearSelection = () => {
|
|
|
|
selectionGroup.current.children = [];
|
|
|
|
selectionGroup.current.position.set(0, 0, 0);
|
|
|
|
selectionGroup.current.rotation.set(0, 0, 0);
|
|
|
|
setpastedObjects([]);
|
|
|
|
setDuplicatedObjects([]);
|
|
|
|
setMovedObjects([]);
|
|
|
|
setRotatedObjects([]);
|
|
|
|
setSelectedAssets([]);
|
2025-05-02 08:34:52 +00:00
|
|
|
setKeyEvent("")
|
2025-04-21 06:23:42 +00:00
|
|
|
}
|
|
|
|
|
2025-05-09 13:53:08 +00:00
|
|
|
return <DistanceFindingControls boundingBoxRef={boundingBoxRef} />;
|
2025-04-21 06:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default MoveControls
|