|
| 1 | +import React, { useEffect, useRef } from 'react'; |
| 2 | +import type { Observer, WebXRAbstractMotionController, WebXRControllerComponent, WebXRInputSource } from '@babylonjs/core'; |
| 3 | +import { useXrExperience } from '../store'; |
| 4 | +import '@babylonjs/core/XR/motionController/webXROculusHandController.js'; |
| 5 | + |
| 6 | +type MicrogesturesProps = Partial<{ |
| 7 | + onSwipeLeft: () => void; |
| 8 | + onSwipeRight: () => void; |
| 9 | + onSwipeForward: () => void; |
| 10 | + onSwipeBackward: () => void; |
| 11 | + onTapThumb: () => void; |
| 12 | + onMenu: () => void; |
| 13 | +}>; |
| 14 | + |
| 15 | +export const Microgestures: React.FC<MicrogesturesProps> = ({ onSwipeLeft, onSwipeRight, onSwipeForward, onSwipeBackward, onTapThumb, onMenu }) => { |
| 16 | + const xrExperience = useXrExperience(); |
| 17 | + const observersRef = useRef<Array<Observer<WebXRInputSource> | Observer<WebXRAbstractMotionController> | Observer<WebXRControllerComponent>>>([]); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const onControllerAddedObserver = xrExperience.input.onControllerAddedObservable.add(xrController => { |
| 21 | + const onMotionControllerInitObserver = xrController.onMotionControllerInitObservable.add(motionController => { |
| 22 | + if (onSwipeLeft) { |
| 23 | + const swipeLeftComponent = motionController.getComponent('swipe-left'); |
| 24 | + const observer = swipeLeftComponent?.onButtonStateChangedObservable.add(() => { |
| 25 | + if (swipeLeftComponent.pressed) { |
| 26 | + onSwipeLeft(); |
| 27 | + } |
| 28 | + }); |
| 29 | + observersRef.current.push(observer); |
| 30 | + } |
| 31 | + if (onSwipeRight) { |
| 32 | + const swipeRightComponent = motionController.getComponent('swipe-right'); |
| 33 | + const observer = swipeRightComponent?.onButtonStateChangedObservable.add(() => { |
| 34 | + if (swipeRightComponent.pressed) { |
| 35 | + onSwipeRight(); |
| 36 | + } |
| 37 | + }); |
| 38 | + observersRef.current.push(observer); |
| 39 | + } |
| 40 | + if (onSwipeForward) { |
| 41 | + const swipeForwardComponent = motionController.getComponent('swipe-forward'); |
| 42 | + const observer = swipeForwardComponent?.onButtonStateChangedObservable.add(() => { |
| 43 | + if (swipeForwardComponent.pressed) { |
| 44 | + onSwipeForward(); |
| 45 | + } |
| 46 | + }); |
| 47 | + observersRef.current.push(observer); |
| 48 | + } |
| 49 | + if (onSwipeBackward) { |
| 50 | + const swipeBackwardComponent = motionController.getComponent('swipe-backward'); |
| 51 | + const observable = swipeBackwardComponent?.onButtonStateChangedObservable.add(() => { |
| 52 | + if (swipeBackwardComponent.pressed) { |
| 53 | + onSwipeBackward(); |
| 54 | + } |
| 55 | + }); |
| 56 | + observersRef.current.push(observable); |
| 57 | + } |
| 58 | + if (onTapThumb) { |
| 59 | + const tapThumbComponent = motionController.getComponent('tap-thumb'); |
| 60 | + const observer = tapThumbComponent?.onButtonStateChangedObservable.add(() => { |
| 61 | + if (tapThumbComponent.pressed) { |
| 62 | + onTapThumb(); |
| 63 | + } |
| 64 | + }); |
| 65 | + observersRef.current.push(observer); |
| 66 | + } |
| 67 | + if (onMenu) { |
| 68 | + const menu = motionController.getComponent('menu'); |
| 69 | + const observer = menu?.onButtonStateChangedObservable.add(() => { |
| 70 | + if (menu.pressed) { |
| 71 | + onMenu(); |
| 72 | + } |
| 73 | + }); |
| 74 | + observersRef.current.push(observer); |
| 75 | + } |
| 76 | + }); |
| 77 | + observersRef.current.push(onMotionControllerInitObserver); |
| 78 | + }); |
| 79 | + observersRef.current.push(onControllerAddedObserver); |
| 80 | + |
| 81 | + return () => { |
| 82 | + observersRef.current.forEach(observer => { |
| 83 | + observer.remove(); |
| 84 | + }); |
| 85 | + }; |
| 86 | + }, []); |
| 87 | + |
| 88 | + return null; |
| 89 | +}; |
0 commit comments