From 406b52b0d8e4e5d6c40855e1494b8689ddba445a Mon Sep 17 00:00:00 2001 From: Jerald-Golden-B Date: Wed, 21 May 2025 16:41:16 +0530 Subject: [PATCH] Enhance event managers with play, pause, and reset functionality; remove obsolete vehicle event manager --- .../eventManager/useConveyorEventManager.ts | 12 +++++++++++- .../machine/eventManager/useMachineEventManager.ts | 12 +++++++++++- .../eventManager/useArmBotEventManager.ts | 12 +++++++++++- .../instances/animator/roboticArmAnimator.tsx | 12 ++++++++---- .../instances/armInstance/roboticArmInstance.tsx | 6 ------ .../roboticArm/instances/ikInstance/ikInstance.tsx | 2 +- .../triggers/triggerHandler/useTriggerHandler.ts | 2 +- .../eventManager/useVehicleEventManager.ts | 14 ++++++++++++-- 8 files changed, 55 insertions(+), 17 deletions(-) rename app/src/modules/simulation/vehicle/{instances => }/eventManager/useVehicleEventManager.ts (80%) diff --git a/app/src/modules/simulation/conveyor/eventManager/useConveyorEventManager.ts b/app/src/modules/simulation/conveyor/eventManager/useConveyorEventManager.ts index bdc014c..4870613 100644 --- a/app/src/modules/simulation/conveyor/eventManager/useConveyorEventManager.ts +++ b/app/src/modules/simulation/conveyor/eventManager/useConveyorEventManager.ts @@ -1,6 +1,7 @@ import { useEffect, useRef } from 'react'; import { useFrame } from '@react-three/fiber'; import { useConveyorStore } from '../../../../store/simulation/useConveyorStore'; +import { usePauseButtonStore, usePlayButtonStore, useResetButtonStore } from '../../../../store/usePlayButtonStore'; type ConveyorCallback = { conveyorId: string; @@ -11,6 +12,15 @@ export function useConveyorEventManager() { const { getConveyorById } = useConveyorStore(); const callbacksRef = useRef([]); const isMonitoringRef = useRef(false); + const { isPlaying } = usePlayButtonStore(); + const { isPaused } = usePauseButtonStore(); + const { isReset } = useResetButtonStore(); + + useEffect(() => { + if (isReset) { + callbacksRef.current = []; + } + }, [isReset]) // Add a new conveyor to monitor const addConveyorToMonitor = (conveyorId: string, callback: () => void) => { @@ -39,7 +49,7 @@ export function useConveyorEventManager() { // Check conveyor states every frame useFrame(() => { - if (!isMonitoringRef.current || callbacksRef.current.length === 0) return; + if (!isMonitoringRef.current || callbacksRef.current.length === 0 || !isPlaying || isPaused) return; callbacksRef.current.forEach(({ conveyorId, callback }) => { const conveyor = getConveyorById(conveyorId); diff --git a/app/src/modules/simulation/machine/eventManager/useMachineEventManager.ts b/app/src/modules/simulation/machine/eventManager/useMachineEventManager.ts index e6edaf3..35c6418 100644 --- a/app/src/modules/simulation/machine/eventManager/useMachineEventManager.ts +++ b/app/src/modules/simulation/machine/eventManager/useMachineEventManager.ts @@ -1,6 +1,7 @@ import { useEffect, useRef } from 'react'; import { useFrame } from '@react-three/fiber'; import { useMachineStore } from '../../../../store/simulation/useMachineStore'; +import { usePauseButtonStore, usePlayButtonStore, useResetButtonStore } from '../../../../store/usePlayButtonStore'; type MachineCallback = { machineId: string; @@ -11,6 +12,15 @@ export function useMachineEventManager() { const { getMachineById } = useMachineStore(); const callbacksRef = useRef([]); const isMonitoringRef = useRef(false); + const { isPlaying } = usePlayButtonStore(); + const { isPaused } = usePauseButtonStore(); + const { isReset } = useResetButtonStore(); + + useEffect(() => { + if (isReset) { + callbacksRef.current = []; + } + }, [isReset]) // Add a new machine to monitor const addMachineToMonitor = (machineId: string, callback: () => void) => { @@ -39,7 +49,7 @@ export function useMachineEventManager() { // Check machine states every frame useFrame(() => { - if (!isMonitoringRef.current || callbacksRef.current.length === 0) return; + if (!isMonitoringRef.current || callbacksRef.current.length === 0 || !isPlaying || isPaused) return; callbacksRef.current.forEach(({ machineId, callback }) => { const machine = getMachineById(machineId); diff --git a/app/src/modules/simulation/roboticArm/eventManager/useArmBotEventManager.ts b/app/src/modules/simulation/roboticArm/eventManager/useArmBotEventManager.ts index 97593b4..59004fa 100644 --- a/app/src/modules/simulation/roboticArm/eventManager/useArmBotEventManager.ts +++ b/app/src/modules/simulation/roboticArm/eventManager/useArmBotEventManager.ts @@ -1,6 +1,7 @@ import { useEffect, useRef } from 'react'; import { useFrame } from '@react-three/fiber'; import { useArmBotStore } from '../../../../store/simulation/useArmBotStore'; +import { usePauseButtonStore, usePlayButtonStore, useResetButtonStore } from '../../../../store/usePlayButtonStore'; type ArmBotCallback = { armBotId: string; @@ -11,6 +12,15 @@ export function useArmBotEventManager() { const { getArmBotById } = useArmBotStore(); const callbacksRef = useRef([]); const isMonitoringRef = useRef(false); + const { isPlaying } = usePlayButtonStore(); + const { isPaused } = usePauseButtonStore(); + const { isReset } = useResetButtonStore(); + + useEffect(() => { + if (isReset) { + callbacksRef.current = []; + } + }, [isReset]) // Add a new armbot to monitor const addArmBotToMonitor = (armBotId: string, callback: () => void) => { @@ -39,7 +49,7 @@ export function useArmBotEventManager() { // Check armbot states every frame useFrame(() => { - if (!isMonitoringRef.current || callbacksRef.current.length === 0) return; + if (!isMonitoringRef.current || callbacksRef.current.length === 0 || !isPlaying || isPaused) return; callbacksRef.current.forEach(({ armBotId, callback }) => { const armBot = getArmBotById(armBotId); diff --git a/app/src/modules/simulation/roboticArm/instances/animator/roboticArmAnimator.tsx b/app/src/modules/simulation/roboticArm/instances/animator/roboticArmAnimator.tsx index b457acc..52213fe 100644 --- a/app/src/modules/simulation/roboticArm/instances/animator/roboticArmAnimator.tsx +++ b/app/src/modules/simulation/roboticArm/instances/animator/roboticArmAnimator.tsx @@ -1,5 +1,5 @@ import React, { useEffect, useMemo, useRef, useState } from 'react'; -import { useFrame } from '@react-three/fiber'; +import { useFrame, useThree } from '@react-three/fiber'; import * as THREE from 'three'; import { Line, Text } from '@react-three/drei'; import { useAnimationPlaySpeed, usePauseButtonStore, usePlayButtonStore, useResetButtonStore } from '../../../../../store/usePlayButtonStore'; @@ -10,7 +10,7 @@ type PointWithDegree = { }; function RoboticArmAnimator({ HandleCallback, restPosition, ikSolver, targetBone, armBot, path }: any) { - + const { scene } = useThree(); const progressRef = useRef(0); const curveRef = useRef(null); const totalDistanceRef = useRef(0); @@ -186,12 +186,12 @@ function RoboticArmAnimator({ HandleCallback, restPosition, ikSolver, targetBone new THREE.Vector3(end[0], curveHeight, end[2]), new THREE.Vector3(end[0], end[1], end[2]) ]; - + const pathSegments: [THREE.Vector3, THREE.Vector3][] = []; for (let i = 0; i < pathVectors.length - 1; i++) { pathSegments.push([pathVectors[i], pathVectors[i + 1]]); } - + const segmentDistances = pathSegments.map(([p1, p2]) => p1.distanceTo(p2)); segmentDistancesRef.current = segmentDistances; const totalDistance = segmentDistances.reduce((sum, d) => sum + d, 0); @@ -203,6 +203,10 @@ function RoboticArmAnimator({ HandleCallback, restPosition, ikSolver, targetBone // Frame update for animation useFrame((state, delta) => { + const targetMesh = scene?.getObjectByProperty("uuid", armBot.modelUuid); + if (targetMesh) { + targetMesh.visible = (!isPlaying) + } if (!ikSolver) return; const bone = ikSolver.mesh.skeleton.bones.find((b: any) => b.name === targetBone); diff --git a/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx b/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx index ce5a280..6568e1f 100644 --- a/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx +++ b/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx @@ -1,6 +1,5 @@ import React, { useEffect, useRef, useState } from 'react' import * as THREE from "three"; -import { useThree } from "@react-three/fiber"; import IKInstance from '../ikInstance/ikInstance'; import RoboticArmAnimator from '../animator/roboticArmAnimator'; import MaterialAnimator from '../animator/materialAnimator'; @@ -19,7 +18,6 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) { const [currentPhase, setCurrentPhase] = useState<(string)>("init"); const [path, setPath] = useState<[number, number, number][]>([]); const [ikSolver, setIkSolver] = useState(null); - const { scene } = useThree(); const restPosition = new THREE.Vector3(0, 1.75, -1.6); const targetBone = "Target"; const groupRef = useRef(null); @@ -273,10 +271,6 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) { useEffect(() => { - const targetMesh = scene?.getObjectByProperty("uuid", armBot.modelUuid); - if (targetMesh) { - targetMesh.visible = (!isPlaying) - } const targetBones = ikSolver?.mesh.skeleton.bones.find((b: any) => b.name === targetBone); if (!isReset && isPlaying) { //Moving armBot from initial point to rest position. diff --git a/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx b/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx index 4bd05a6..f448baf 100644 --- a/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx +++ b/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx @@ -80,7 +80,7 @@ function IKInstance({ modelUrl, setIkSolver, ikSolver, armBot, groupRef }: IKIns setSelectedArm(groupRef.current?.getObjectByName(targetBoneName)) }}> ([]); const isMonitoringRef = useRef(false); + const { isPlaying } = usePlayButtonStore(); + const { isPaused } = usePauseButtonStore(); + const { isReset } = useResetButtonStore(); + + useEffect(() => { + if (isReset) { + callbacksRef.current = []; + } + }, [isReset]) // Add a new vehicle to monitor const addVehicleToMonitor = (vehicleId: string, callback: () => void) => { @@ -39,7 +49,7 @@ export function useVehicleEventManager() { // Check vehicle states every frame useFrame(() => { - if (!isMonitoringRef.current || callbacksRef.current.length === 0) return; + if (!isMonitoringRef.current || callbacksRef.current.length === 0 || !isPlaying || isPaused) return; callbacksRef.current.forEach(({ vehicleId, callback }) => { const vehicle = getVehicleById(vehicleId);