feat: Refactor point management in PointsCreator and update product store for better event handling
This commit is contained in:
@@ -18,8 +18,8 @@ function PointsCreator() {
|
||||
const { subModule } = useSubModuleStore();
|
||||
const { selectedProductStore } = useProductContext();
|
||||
const { eventStore, productStore } = useSceneContext();
|
||||
const { events, updatePoint, getPointByUuid, getEventByModelUuid } = eventStore();
|
||||
const { getEventByModelUuid: getEventByModelUuidFromProduct, updatePoint: updatePointFromProduct, getEventByModelUuid: getEventByModelUuidFromProduct2, getPointByUuid: getPointByUuidFromProduct } = productStore();
|
||||
const { events, getEventByModelUuid } = eventStore();
|
||||
const { getEventByModelUuid: getEventByModelUuidFromProduct, updatePoint: updatePointFromProduct, getEventByModelUuid: getEventByModelUuidFromProduct2, getPointByUuid: getPointByUuidFromProduct, getTriggersByTriggeredPointUuid, removeTrigger, removePoint } = productStore();
|
||||
const { selectedProduct } = selectedProductStore();
|
||||
const { activeModule } = useModuleStore();
|
||||
const transformRef = useRef<any>(null);
|
||||
@@ -75,65 +75,87 @@ function PointsCreator() {
|
||||
if (keyCombination === "R") {
|
||||
setTransformMode((prev) => (prev === "rotate" ? null : "rotate"));
|
||||
}
|
||||
if (keyCombination === 'DELETE') {
|
||||
deletePointfromConveyor(selectedEventSphere);
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("keydown", handleKeyDown);
|
||||
return () => window.removeEventListener("keydown", handleKeyDown);
|
||||
}, [selectedEventSphere]);
|
||||
|
||||
const updatePointToState = (selectedEventSphere: THREE.Mesh) => {
|
||||
let point: PointsScheme = JSON.parse(
|
||||
JSON.stringify(
|
||||
getPointByUuid(
|
||||
selectedEventSphere.userData.modelUuid,
|
||||
selectedEventSphere.userData.pointUuid
|
||||
)
|
||||
)
|
||||
);
|
||||
if (point) {
|
||||
point.position = [
|
||||
selectedEventSphere.position.x,
|
||||
selectedEventSphere.position.y,
|
||||
selectedEventSphere.position.z,
|
||||
];
|
||||
point.rotation = [
|
||||
selectedEventSphere.rotation.x,
|
||||
selectedEventSphere.rotation.y,
|
||||
selectedEventSphere.rotation.z,
|
||||
];
|
||||
const deletePointfromConveyor = (selectedEventSphere: THREE.Mesh) => {
|
||||
const eventModel = getEventByModelUuidFromProduct(selectedProduct.productUuid, selectedEventSphere.userData.modelUuid);
|
||||
if (!eventModel || eventModel.type !== 'transfer' || eventModel.points.length < 2) return;
|
||||
|
||||
const event = getEventByModelUuidFromProduct(selectedProduct.productUuid, selectedEventSphere.userData.modelUuid);
|
||||
const triggers = getTriggersByTriggeredPointUuid(selectedProduct.productUuid, selectedEventSphere.userData.pointUuid);
|
||||
|
||||
if (event && selectedProduct.productUuid !== '') {
|
||||
const updatedEvents: EventsSchema[] = [];
|
||||
if (triggers.length > 0) {
|
||||
triggers.map((trigger) => {
|
||||
const event = removeTrigger(selectedProduct.productUuid, trigger.triggerUuid);
|
||||
if (event) {
|
||||
updatedEvents.push(event);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const updatedPoint = JSON.parse(
|
||||
JSON.stringify(
|
||||
getPointByUuidFromProduct(selectedProduct.productUuid, selectedEventSphere.userData.modelUuid, selectedEventSphere.userData.pointUuid)
|
||||
)
|
||||
const event = removePoint(selectedProduct.productUuid, selectedEventSphere.userData.modelUuid, selectedEventSphere.userData.pointUuid)
|
||||
|
||||
if (event) {
|
||||
updatedEvents.push(event);
|
||||
}
|
||||
|
||||
if (updatedEvents.length > 0) {
|
||||
updatedEvents.map((updatedEvent) => {
|
||||
updateBackend(
|
||||
selectedProduct.productName,
|
||||
selectedProduct.productUuid,
|
||||
projectId || '',
|
||||
updatedEvent
|
||||
);
|
||||
if (updatedPoint) {
|
||||
updatedPoint.position = point.position;
|
||||
updatedPoint.rotation = point.rotation;
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const updatedEvent = updatePointFromProduct(
|
||||
const updatePointToState = (selectedEventSphere: THREE.Mesh) => {
|
||||
const position = [
|
||||
selectedEventSphere.position.x,
|
||||
selectedEventSphere.position.y,
|
||||
selectedEventSphere.position.z,
|
||||
];
|
||||
const rotation = [
|
||||
selectedEventSphere.rotation.x,
|
||||
selectedEventSphere.rotation.y,
|
||||
selectedEventSphere.rotation.z,
|
||||
];
|
||||
|
||||
const event = getEventByModelUuidFromProduct(selectedProduct.productUuid, selectedEventSphere.userData.modelUuid);
|
||||
|
||||
if (event && selectedProduct.productUuid !== '') {
|
||||
|
||||
const updatedPoint = JSON.parse(
|
||||
JSON.stringify(
|
||||
getPointByUuidFromProduct(selectedProduct.productUuid, selectedEventSphere.userData.modelUuid, selectedEventSphere.userData.pointUuid)
|
||||
)
|
||||
);
|
||||
if (updatedPoint) {
|
||||
updatedPoint.position = position;
|
||||
updatedPoint.rotation = rotation;
|
||||
|
||||
const updatedEvent = updatePointFromProduct(
|
||||
selectedProduct.productUuid,
|
||||
selectedEventSphere.userData.modelUuid,
|
||||
selectedEventSphere.userData.pointUuid,
|
||||
updatedPoint
|
||||
)
|
||||
if (updatedEvent) {
|
||||
updateBackend(
|
||||
selectedProduct.productName,
|
||||
selectedProduct.productUuid,
|
||||
selectedEventSphere.userData.modelUuid,
|
||||
selectedEventSphere.userData.pointUuid,
|
||||
updatedPoint
|
||||
)
|
||||
if (updatedEvent) {
|
||||
updatePoint(
|
||||
selectedEventSphere.userData.modelUuid,
|
||||
selectedEventSphere.userData.pointUuid,
|
||||
point
|
||||
)
|
||||
updateBackend(
|
||||
selectedProduct.productName,
|
||||
selectedProduct.productUuid,
|
||||
projectId || '',
|
||||
updatedEvent
|
||||
);
|
||||
}
|
||||
projectId || '',
|
||||
updatedEvent
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,9 +175,7 @@ function PointsCreator() {
|
||||
const onMouseUp = () => {
|
||||
if (selectedEventSphere && !drag) {
|
||||
raycaster.setFromCamera(pointer, camera);
|
||||
const intersects = raycaster
|
||||
.intersectObjects(scene.children, true)
|
||||
.filter((intersect) => intersect.object.name === "Event-Sphere");
|
||||
const intersects = raycaster.intersectObjects(scene.children, true).filter((intersect) => intersect.object.name === "Event-Sphere");
|
||||
if (intersects.length === 0) {
|
||||
clearSelectedEventSphere();
|
||||
setTransformMode(null);
|
||||
|
||||
Reference in New Issue
Block a user