moveControls and other controls altered
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import * as THREE from "three";
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import { SkeletonUtils } from "three-stdlib";
|
||||
import { useSelectedAssets, useSocketStore, useToggleView } from "../../../../../store/builder/store";
|
||||
@@ -10,20 +10,16 @@ import { getUserData } from "../../../../../functions/getUserData";
|
||||
import { useSceneContext } from "../../../sceneContext";
|
||||
import { useVersionContext } from "../../../../builder/version/versionContext";
|
||||
|
||||
// import { setAssetsApi } from '../../../../../services/factoryBuilder/asset/floorAsset/setAssetsApi';
|
||||
|
||||
const CopyPasteControls3D = ({
|
||||
copiedObjects,
|
||||
setCopiedObjects,
|
||||
pastedObjects,
|
||||
setpastedObjects,
|
||||
selectionGroup,
|
||||
setDuplicatedObjects,
|
||||
movedObjects,
|
||||
setMovedObjects,
|
||||
rotatedObjects,
|
||||
setRotatedObjects,
|
||||
boundingBoxRef
|
||||
}: any) => {
|
||||
const { camera, controls, gl, scene, pointer, raycaster } = useThree();
|
||||
const { toggleView } = useToggleView();
|
||||
@@ -33,31 +29,67 @@ const CopyPasteControls3D = ({
|
||||
const { assetStore, eventStore } = useSceneContext();
|
||||
const { addEvent } = eventStore();
|
||||
const { projectId } = useParams();
|
||||
const { assets, addAsset } = assetStore();
|
||||
const { assets, addAsset, setPosition, updateAsset, removeAsset, getAssetById } = assetStore();
|
||||
const { selectedVersionStore } = useVersionContext();
|
||||
const { selectedVersion } = selectedVersionStore();
|
||||
const { userId, organization } = getUserData();
|
||||
|
||||
const [isPasting, setIsPasting] = useState(false);
|
||||
const [relativePositions, setRelativePositions] = useState<THREE.Vector3[]>([]);
|
||||
const [centerOffset, setCenterOffset] = useState<THREE.Vector3 | null>(null);
|
||||
const mouseButtonsDown = useRef<{ left: boolean; right: boolean }>({
|
||||
left: false,
|
||||
right: false,
|
||||
});
|
||||
|
||||
const calculateRelativePositions = useCallback((objects: THREE.Object3D[]) => {
|
||||
if (objects.length === 0) return { center: new THREE.Vector3(), relatives: [] };
|
||||
|
||||
const box = new THREE.Box3();
|
||||
objects.forEach(obj => box.expandByObject(obj));
|
||||
const center = new THREE.Vector3();
|
||||
box.getCenter(center);
|
||||
|
||||
const relatives = objects.map(obj => {
|
||||
const relativePos = new THREE.Vector3().subVectors(obj.position, center);
|
||||
return relativePos;
|
||||
});
|
||||
|
||||
return { center, relatives };
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!camera || !scene || toggleView) return;
|
||||
const canvasElement = gl.domElement;
|
||||
canvasElement.tabIndex = 0;
|
||||
|
||||
let isMoving = false;
|
||||
let isPointerMoving = false;
|
||||
|
||||
const onPointerDown = () => {
|
||||
isMoving = false;
|
||||
const onPointerDown = (event: PointerEvent) => {
|
||||
isPointerMoving = false;
|
||||
if (event.button === 0) mouseButtonsDown.current.left = true;
|
||||
if (event.button === 2) mouseButtonsDown.current.right = true;
|
||||
};
|
||||
|
||||
const onPointerMove = () => {
|
||||
isMoving = true;
|
||||
isPointerMoving = true;
|
||||
};
|
||||
|
||||
const onPointerUp = (event: PointerEvent) => {
|
||||
if (!isMoving && pastedObjects.length > 0 && event.button === 0 && movedObjects.length === 0 && rotatedObjects.length === 0) {
|
||||
if (event.button === 0) mouseButtonsDown.current.left = false;
|
||||
if (event.button === 2) mouseButtonsDown.current.right = false;
|
||||
|
||||
if (!isPointerMoving && pastedObjects.length > 0 && event.button === 0) {
|
||||
event.preventDefault();
|
||||
addPastedObjects();
|
||||
}
|
||||
if (!isPointerMoving && pastedObjects.length > 0 && event.button === 2) {
|
||||
event.preventDefault();
|
||||
clearSelection();
|
||||
pastedObjects.forEach((obj: THREE.Object3D) => {
|
||||
removeAsset(obj.userData.modelUuid);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
@@ -69,6 +101,13 @@ const CopyPasteControls3D = ({
|
||||
if (keyCombination === "Ctrl+V" && copiedObjects.length > 0 && pastedObjects.length === 0 && movedObjects.length === 0 && rotatedObjects.length === 0) {
|
||||
pasteCopiedObjects();
|
||||
}
|
||||
if (keyCombination === "ESCAPE" && pastedObjects.length > 0) {
|
||||
event.preventDefault();
|
||||
clearSelection();
|
||||
pastedObjects.forEach((obj: THREE.Object3D) => {
|
||||
removeAsset(obj.userData.modelUuid);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (!toggleView) {
|
||||
@@ -84,27 +123,22 @@ const CopyPasteControls3D = ({
|
||||
canvasElement.removeEventListener("pointerup", onPointerUp);
|
||||
canvasElement.removeEventListener("keydown", onKeyDown);
|
||||
};
|
||||
|
||||
}, [assets, camera, controls, scene, toggleView, selectedAssets, copiedObjects, pastedObjects, movedObjects, socket, rotatedObjects]);
|
||||
|
||||
useFrame(() => {
|
||||
if (pastedObjects.length > 0) {
|
||||
const intersectionPoint = new THREE.Vector3();
|
||||
raycaster.setFromCamera(pointer, camera);
|
||||
const point = raycaster.ray.intersectPlane(plane, intersectionPoint);
|
||||
if (point) {
|
||||
const position = new THREE.Vector3();
|
||||
if (boundingBoxRef.current) {
|
||||
boundingBoxRef.current?.getWorldPosition(position)
|
||||
selectionGroup.current.position.set(point.x - (position.x - selectionGroup.current.position.x), selectionGroup.current.position.y, point.z - (position.z - selectionGroup.current.position.z));
|
||||
} else {
|
||||
const box = new THREE.Box3();
|
||||
pastedObjects.forEach((obj: THREE.Object3D) => box.expandByObject(obj.clone()));
|
||||
const center = new THREE.Vector3();
|
||||
box.getCenter(center);
|
||||
selectionGroup.current.position.set(point.x - (center.x - selectionGroup.current.position.x), selectionGroup.current.position.y, point.z - (center.z - selectionGroup.current.position.z));
|
||||
}
|
||||
}
|
||||
if (!isPasting || pastedObjects.length === 0) return;
|
||||
if (mouseButtonsDown.current.left || mouseButtonsDown.current.right) return;
|
||||
|
||||
const intersectionPoint = new THREE.Vector3();
|
||||
raycaster.setFromCamera(pointer, camera);
|
||||
const hit = raycaster.ray.intersectPlane(plane, intersectionPoint);
|
||||
|
||||
if (hit && centerOffset) {
|
||||
pastedObjects.forEach((pastedObject: THREE.Object3D, index: number) => {
|
||||
const newPos = new THREE.Vector3().addVectors(hit, relativePositions[index]);
|
||||
setPosition(pastedObject.userData.modelUuid, [newPos.x, 0, newPos.z]);
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
@@ -122,31 +156,41 @@ const CopyPasteControls3D = ({
|
||||
|
||||
const pasteCopiedObjects = () => {
|
||||
if (copiedObjects.length > 0 && pastedObjects.length === 0) {
|
||||
const newClones = copiedObjects.map((obj: THREE.Object3D) => {
|
||||
const clone = obj.clone();
|
||||
clone.position.copy(obj.position);
|
||||
const { center, relatives } = calculateRelativePositions(copiedObjects);
|
||||
setRelativePositions(relatives);
|
||||
|
||||
const newPastedObjects = copiedObjects.map((obj: any) => {
|
||||
const clone = SkeletonUtils.clone(obj);
|
||||
clone.userData.modelUuid = THREE.MathUtils.generateUUID();
|
||||
return clone;
|
||||
});
|
||||
selectionGroup.current.add(...newClones);
|
||||
setpastedObjects([...newClones]);
|
||||
setSelectedAssets([...newClones]);
|
||||
|
||||
const intersectionPoint = new THREE.Vector3();
|
||||
setpastedObjects(newPastedObjects);
|
||||
|
||||
raycaster.setFromCamera(pointer, camera);
|
||||
const point = raycaster.ray.intersectPlane(plane, intersectionPoint);
|
||||
const intersectionPoint = new THREE.Vector3();
|
||||
const hit = raycaster.ray.intersectPlane(plane, intersectionPoint);
|
||||
|
||||
if (point) {
|
||||
const position = new THREE.Vector3();
|
||||
if (boundingBoxRef.current) {
|
||||
boundingBoxRef.current?.getWorldPosition(position)
|
||||
selectionGroup.current.position.set(point.x - (position.x - selectionGroup.current.position.x), selectionGroup.current.position.y, point.z - (position.z - selectionGroup.current.position.z));
|
||||
} else {
|
||||
const box = new THREE.Box3();
|
||||
newClones.forEach((obj: THREE.Object3D) => box.expandByObject(obj.clone()));
|
||||
const center = new THREE.Vector3();
|
||||
box.getCenter(center);
|
||||
selectionGroup.current.position.set(point.x - (center.x - selectionGroup.current.position.x), selectionGroup.current.position.y, point.z - (center.z - selectionGroup.current.position.z));
|
||||
}
|
||||
if (hit) {
|
||||
const offset = new THREE.Vector3().subVectors(center, hit);
|
||||
setCenterOffset(offset);
|
||||
setIsPasting(true);
|
||||
|
||||
newPastedObjects.forEach((obj: THREE.Object3D, index: number) => {
|
||||
const initialPos = new THREE.Vector3().addVectors(hit, relatives[index]);
|
||||
const asset: Asset = {
|
||||
modelUuid: obj.userData.modelUuid,
|
||||
modelName: obj.userData.modelName,
|
||||
assetId: obj.userData.assetId,
|
||||
position: initialPos.toArray(),
|
||||
rotation: [obj.rotation.x, obj.rotation.y, obj.rotation.z],
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
isCollidable: false,
|
||||
opacity: 0.5,
|
||||
};
|
||||
addAsset(asset);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -154,33 +198,33 @@ const CopyPasteControls3D = ({
|
||||
const addPastedObjects = () => {
|
||||
if (pastedObjects.length === 0) return;
|
||||
|
||||
pastedObjects.forEach(async (obj: THREE.Object3D) => {
|
||||
if (obj) {
|
||||
const worldPosition = new THREE.Vector3();
|
||||
obj.getWorldPosition(worldPosition);
|
||||
obj.position.copy(worldPosition);
|
||||
pastedObjects.forEach(async (pastedAsset: THREE.Object3D) => {
|
||||
if (pastedAsset) {
|
||||
const assetUuid = pastedAsset.userData.modelUuid;
|
||||
const asset = getAssetById(assetUuid);
|
||||
if (!asset) return;
|
||||
|
||||
const newFloorItem: Types.FloorItemType = {
|
||||
modelUuid: THREE.MathUtils.generateUUID(),
|
||||
modelName: obj.userData.modelName,
|
||||
assetId: obj.userData.assetId,
|
||||
position: [worldPosition.x, worldPosition.y, worldPosition.z],
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z, },
|
||||
modelUuid: pastedAsset.userData.modelUuid,
|
||||
modelName: pastedAsset.userData.modelName,
|
||||
assetId: pastedAsset.userData.assetId,
|
||||
position: asset.position,
|
||||
rotation: { x: asset.rotation[0], y: asset.rotation[1], z: asset.rotation[2] },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
};
|
||||
|
||||
let updatedEventData = null;
|
||||
|
||||
if (obj.userData.eventData) {
|
||||
updatedEventData = JSON.parse(JSON.stringify(obj.userData.eventData));
|
||||
if (pastedAsset.userData.eventData) {
|
||||
updatedEventData = JSON.parse(JSON.stringify(pastedAsset.userData.eventData));
|
||||
updatedEventData.modelUuid = newFloorItem.modelUuid;
|
||||
|
||||
const eventData: any = {
|
||||
type: obj.userData.eventData.type,
|
||||
type: pastedAsset.userData.eventData.type,
|
||||
};
|
||||
|
||||
if (obj.userData.eventData.type === "Conveyor") {
|
||||
if (pastedAsset.userData.eventData.type === "Conveyor") {
|
||||
const ConveyorEvent: ConveyorEventSchema = {
|
||||
modelUuid: newFloorItem.modelUuid,
|
||||
modelName: newFloorItem.modelName,
|
||||
@@ -212,7 +256,7 @@ const CopyPasteControls3D = ({
|
||||
rotation: point.rotation
|
||||
}));
|
||||
|
||||
} else if (obj.userData.eventData.type === "Vehicle") {
|
||||
} else if (pastedAsset.userData.eventData.type === "Vehicle") {
|
||||
const vehicleEvent: VehicleEventSchema = {
|
||||
modelUuid: newFloorItem.modelUuid,
|
||||
modelName: newFloorItem.modelName,
|
||||
@@ -250,7 +294,7 @@ const CopyPasteControls3D = ({
|
||||
rotation: vehicleEvent.point.rotation
|
||||
};
|
||||
|
||||
} else if (obj.userData.eventData.type === "ArmBot") {
|
||||
} else if (pastedAsset.userData.eventData.type === "ArmBot") {
|
||||
const roboticArmEvent: RoboticArmEventSchema = {
|
||||
modelUuid: newFloorItem.modelUuid,
|
||||
modelName: newFloorItem.modelName,
|
||||
@@ -284,7 +328,7 @@ const CopyPasteControls3D = ({
|
||||
rotation: roboticArmEvent.point.rotation
|
||||
};
|
||||
|
||||
} else if (obj.userData.eventData.type === "StaticMachine") {
|
||||
} else if (pastedAsset.userData.eventData.type === "StaticMachine") {
|
||||
const machineEvent: MachineEventSchema = {
|
||||
modelUuid: newFloorItem.modelUuid,
|
||||
modelName: newFloorItem.modelName,
|
||||
@@ -312,7 +356,7 @@ const CopyPasteControls3D = ({
|
||||
position: machineEvent.point.position,
|
||||
rotation: machineEvent.point.rotation
|
||||
};
|
||||
} else if (obj.userData.eventData.type === "Storage") {
|
||||
} else if (pastedAsset.userData.eventData.type === "Storage") {
|
||||
const storageEvent: StorageEventSchema = {
|
||||
modelUuid: newFloorItem.modelUuid,
|
||||
modelName: newFloorItem.modelName,
|
||||
@@ -339,7 +383,7 @@ const CopyPasteControls3D = ({
|
||||
position: storageEvent.point.position,
|
||||
rotation: storageEvent.point.rotation
|
||||
};
|
||||
} else if (obj.userData.eventData.type === "Human") {
|
||||
} else if (pastedAsset.userData.eventData.type === "Human") {
|
||||
const humanEvent: HumanEventSchema = {
|
||||
modelUuid: newFloorItem.modelUuid,
|
||||
modelName: newFloorItem.modelName,
|
||||
@@ -374,15 +418,16 @@ const CopyPasteControls3D = ({
|
||||
}
|
||||
|
||||
newFloorItem.eventData = eventData;
|
||||
|
||||
//REST
|
||||
|
||||
// await setAssetsApi(
|
||||
// organization,
|
||||
// obj.uuid,
|
||||
// obj.userData.name,
|
||||
// pastedAsset.uuid,
|
||||
// pastedAsset.userData.name,
|
||||
// [worldPosition.x, worldPosition.y, worldPosition.z],
|
||||
// { "x": obj.rotation.x, "y": obj.rotation.y, "z": obj.rotation.z },
|
||||
// obj.userData.modelId,
|
||||
// { "x": pastedAsset.rotation.x, "y": pastedAsset.rotation.y, "z": pastedAsset.rotation.z },
|
||||
// pastedAsset.userData.modelId,
|
||||
// false,
|
||||
// true,
|
||||
// );
|
||||
@@ -395,7 +440,7 @@ const CopyPasteControls3D = ({
|
||||
modelName: newFloorItem.modelName,
|
||||
assetId: newFloorItem.assetId,
|
||||
position: newFloorItem.position,
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
||||
rotation: { x: pastedAsset.rotation.x, y: pastedAsset.rotation.y, z: pastedAsset.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
socketId: socket.id,
|
||||
@@ -405,16 +450,8 @@ const CopyPasteControls3D = ({
|
||||
projectId
|
||||
};
|
||||
|
||||
// console.log('data: ', data);
|
||||
socket.emit("v1:model-asset:add", data);
|
||||
|
||||
obj.userData = {
|
||||
name: newFloorItem.modelName,
|
||||
modelId: newFloorItem.assetId,
|
||||
modelUuid: newFloorItem.modelUuid,
|
||||
eventData: JSON.parse(JSON.stringify(eventData))
|
||||
};
|
||||
|
||||
const asset: Asset = {
|
||||
modelUuid: data.modelUuid,
|
||||
modelName: data.modelName,
|
||||
@@ -426,33 +463,17 @@ const CopyPasteControls3D = ({
|
||||
isVisible: data.isVisible,
|
||||
opacity: 1,
|
||||
eventData: data.eventData
|
||||
}
|
||||
|
||||
addAsset(asset);
|
||||
};
|
||||
|
||||
updateAsset(asset.modelUuid, asset);
|
||||
} else {
|
||||
|
||||
//REST
|
||||
|
||||
// await setAssetsApi(
|
||||
// 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,
|
||||
modelUuid: newFloorItem.modelUuid,
|
||||
modelName: newFloorItem.modelName,
|
||||
assetId: newFloorItem.assetId,
|
||||
position: newFloorItem.position,
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
||||
rotation: { x: pastedAsset.rotation.x, y: pastedAsset.rotation.y, z: pastedAsset.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
socketId: socket.id,
|
||||
@@ -461,7 +482,6 @@ const CopyPasteControls3D = ({
|
||||
userId
|
||||
};
|
||||
|
||||
// console.log('data: ', data);
|
||||
socket.emit("v1:model-asset:add", data);
|
||||
|
||||
const asset: Asset = {
|
||||
@@ -474,9 +494,9 @@ const CopyPasteControls3D = ({
|
||||
isCollidable: false,
|
||||
isVisible: data.isVisible,
|
||||
opacity: 1,
|
||||
}
|
||||
};
|
||||
|
||||
addAsset(asset);
|
||||
updateAsset(asset.modelUuid, asset);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -486,15 +506,14 @@ const CopyPasteControls3D = ({
|
||||
};
|
||||
|
||||
const clearSelection = () => {
|
||||
selectionGroup.current.children = [];
|
||||
selectionGroup.current.position.set(0, 0, 0);
|
||||
selectionGroup.current.rotation.set(0, 0, 0);
|
||||
setMovedObjects([]);
|
||||
setpastedObjects([]);
|
||||
setDuplicatedObjects([]);
|
||||
setRotatedObjects([]);
|
||||
setSelectedAssets([]);
|
||||
}
|
||||
setIsPasting(false);
|
||||
setCenterOffset(null);
|
||||
};
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user