From 2aab047b6dc4a62d81b6baaadb3d8b0bfa9f0b98 Mon Sep 17 00:00:00 2001 From: Jerald-Golden-B Date: Mon, 28 Jul 2025 15:05:28 +0530 Subject: [PATCH] Refactor useRetrieveHandler to remove unused human event manager and streamline action handling --- .../builder/asset/models/model/model.tsx | 73 +++---------------- .../actionHandler/useRetrieveHandler.ts | 23 ++---- 2 files changed, 14 insertions(+), 82 deletions(-) diff --git a/app/src/modules/builder/asset/models/model/model.tsx b/app/src/modules/builder/asset/models/model/model.tsx index 429a9c3..f92d34f 100644 --- a/app/src/modules/builder/asset/models/model/model.tsx +++ b/app/src/modules/builder/asset/models/model/model.tsx @@ -16,9 +16,9 @@ import { getUserData } from '../../../../../functions/getUserData'; import { useSceneContext } from '../../../../scene/sceneContext'; import { useVersionContext } from '../../../version/versionContext'; import { SkeletonUtils } from 'three-stdlib'; -import { useAnimationPlaySpeed, usePauseButtonStore } from '../../../../../store/usePlayButtonStore'; import { upsertProductOrEventApi } from '../../../../../services/simulation/products/UpsertProductOrEventApi'; import { getAssetIksApi } from '../../../../../services/simulation/ik/getAssetIKs'; +import { ModelAnimator } from './animator/modelAnimator'; function Model({ asset }: { readonly asset: Asset }) { const url_Backend_dwinzo = `http://${process.env.REACT_APP_SERVER_MARKETPLACE_URL}`; @@ -29,10 +29,8 @@ function Model({ asset }: { readonly asset: Asset }) { const { toggleView } = useToggleView(); const { subModule } = useSubModuleStore(); const { activeModule } = useModuleStore(); - const { speed } = useAnimationPlaySpeed(); - const { isPaused } = usePauseButtonStore(); const { assetStore, eventStore, productStore } = useSceneContext(); - const { removeAsset, setAnimations, resetAnimation, setAnimationComplete } = assetStore(); + const { removeAsset, resetAnimation } = assetStore(); const { setTop } = useTopData(); const { setLeft } = useLeftData(); const { getIsEventInProduct, addPoint } = productStore(); @@ -54,12 +52,7 @@ function Model({ asset }: { readonly asset: Asset }) { const [boundingBox, setBoundingBox] = useState(null); const [isSelected, setIsSelected] = useState(false); const groupRef = useRef(null); - const mixerRef = useRef(); - const actions = useRef<{ [name: string]: THREE.AnimationAction }>({}); - const [previousAnimation, setPreviousAnimation] = useState(null); const [ikData, setIkData] = useState(); - const blendFactor = useRef(0); - const blendDuration = 0.5; const { selectedVersionStore } = useVersionContext(); const { selectedVersion } = selectedVersionStore(); const { userId, organization } = getUserData(); @@ -122,16 +115,6 @@ function Model({ asset }: { readonly asset: Asset }) { clone.animations = cachedModel.animations || []; setGltfScene(clone); calculateBoundingBox(clone); - if (cachedModel.animations && clone.animations.length > 0) { - const animationName = clone.animations.map((clip: any) => clip.name); - setAnimations(asset.modelUuid, animationName) - mixerRef.current = new THREE.AnimationMixer(clone); - - clone.animations.forEach((animation: any) => { - const action = mixerRef.current!.clipAction(animation); - actions.current[animation.name] = action; - }); - } return; } @@ -371,50 +354,6 @@ function Model({ asset }: { readonly asset: Asset }) { } } - const handleAnimationComplete = useCallback(() => { - if (asset.animationState) { - setAnimationComplete(asset.modelUuid, true); - } - }, [asset.animationState]); - - useFrame((_, delta) => { - if (mixerRef.current) { - mixerRef.current.update(delta * (activeModule === 'simulation' ? speed : 1)); - } - }); - - useEffect(() => { - if (!asset.animationState || !mixerRef.current) return; - - const { current, loopAnimation, isPlaying } = asset.animationState; - const currentAction = actions.current[current]; - const previousAction = previousAnimation ? actions.current[previousAnimation] : null; - - if (isPlaying && currentAction && activeModule === 'simulation' && !isPaused) { - blendFactor.current = 0; - - currentAction.reset(); - currentAction.setLoop(loopAnimation ? THREE.LoopRepeat : THREE.LoopOnce, loopAnimation ? Infinity : 1); - currentAction.clampWhenFinished = true; - - if (previousAction && previousAction !== currentAction) { - previousAction.crossFadeTo(currentAction, blendDuration, false); - } - - currentAction.play(); - mixerRef.current.addEventListener('finished', handleAnimationComplete); - setPreviousAnimation(current); - } else { - Object.values(actions.current).forEach((action) => action.stop()); - } - - return () => { - if (mixerRef.current) { - mixerRef.current.removeEventListener('finished', handleAnimationComplete); - } - }; - }, [asset.animationState?.current, asset.animationState?.isCompleted, asset.animationState?.isPlaying, isPaused, activeModule]); - useEffect(() => { const canvasElement = gl.domElement; @@ -513,7 +452,13 @@ function Model({ asset }: { readonly asset: Asset }) { {gltfScene && ( <> {isRendered ? ( - + <> + + + + + + ) : ( )} diff --git a/app/src/modules/simulation/actions/storageUnit/actionHandler/useRetrieveHandler.ts b/app/src/modules/simulation/actions/storageUnit/actionHandler/useRetrieveHandler.ts index 02f33e1..e951289 100644 --- a/app/src/modules/simulation/actions/storageUnit/actionHandler/useRetrieveHandler.ts +++ b/app/src/modules/simulation/actions/storageUnit/actionHandler/useRetrieveHandler.ts @@ -3,17 +3,15 @@ import { useFrame } from "@react-three/fiber"; import { usePlayButtonStore, usePauseButtonStore, useResetButtonStore, useAnimationPlaySpeed } from "../../../../../store/usePlayButtonStore"; import { useSceneContext } from "../../../../scene/sceneContext"; import { useProductContext } from "../../../products/productContext"; -import { useHumanEventManager } from "../../../human/eventManager/useHumanEventManager"; export function useRetrieveHandler() { - const { materialStore, armBotStore, vehicleStore, storageUnitStore, productStore, humanStore, assetStore, humanEventManagerRef } = useSceneContext(); + const { materialStore, armBotStore, vehicleStore, storageUnitStore, productStore, humanStore, assetStore } = useSceneContext(); const { selectedProductStore } = useProductContext(); const { addMaterial } = materialStore(); const { getModelUuidByActionUuid, getPointUuidByActionUuid, getEventByModelUuid, getActionByUuid } = productStore(); const { getStorageUnitById, getLastMaterial, updateCurrentLoad, removeLastMaterial } = storageUnitStore(); const { getVehicleById, incrementVehicleLoad, addCurrentMaterial } = vehicleStore(); const { getHumanById, incrementHumanLoad, addCurrentMaterial: addCurrentMaterialToHuman } = humanStore(); - const { addHumanToMonitor } = useHumanEventManager(); const { getAssetById, setCurrentAnimation } = assetStore(); const { selectedProduct } = selectedProductStore(); const { getArmBotById, addCurrentAction } = armBotStore(); @@ -303,19 +301,10 @@ export function useRetrieveHandler() { const humanAsset = getAssetById(triggeredModel.modelUuid); const action = getActionByUuid(selectedProduct.productUuid, human?.currentAction?.actionUuid || ''); - if (!action || action.actionType !== 'worker' || !humanEventManagerRef.current) return; + if (!action || action.actionType !== 'worker') return; - let state = humanEventManagerRef.current.humanStates.find(h => h.humanId === triggeredModel.modelUuid); - console.log('state: ', state); - console.log('human: ', human); const currentCount = retrievalCountRef.current.get(actionUuid) ?? 0; - let conditionMet = false; - if (state) { - console.log('state.actionQueue: ', state.actionQueue); - // state.actionQueue[0].count - } - if (currentCount >= action.loadCount) { completedActions.push(actionUuid); hasChanges = true; @@ -337,12 +326,10 @@ export function useRetrieveHandler() { removeLastMaterial(storageUnit.modelUuid); updateCurrentLoad(storageUnit.modelUuid, -1); incrementHumanLoad(human.modelUuid, 1); - addHumanToMonitor(human.modelUuid, () => { - addCurrentMaterialToHuman(human.modelUuid, material.materialType, material.materialId); - retrieveLogStatus(material.materialName, `is picked by ${human.modelName}`); + addCurrentMaterialToHuman(human.modelUuid, material.materialType, material.materialId); + retrieveLogStatus(material.materialName, `is picked by ${human.modelName}`); - retrievalCountRef.current.set(actionUuid, currentCount + 1); - }, actionUuid) + retrievalCountRef.current.set(actionUuid, currentCount + 1); } } }