From a3cbe6d1e67ff2565615210da07934c37bbabf2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Wed, 22 May 2024 13:47:35 -0500 Subject: [PATCH] feat: live input updates --- .../[id]/components/QuickInputs/index.tsx | 56 ++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/app/tools/tankopedia/[id]/components/QuickInputs/index.tsx b/src/app/tools/tankopedia/[id]/components/QuickInputs/index.tsx index fd7cd580c..ffeb8e8ef 100644 --- a/src/app/tools/tankopedia/[id]/components/QuickInputs/index.tsx +++ b/src/app/tools/tankopedia/[id]/components/QuickInputs/index.tsx @@ -4,7 +4,10 @@ import { use, useEffect, useRef } from 'react'; import { degToRad, radToDeg } from 'three/src/math/MathUtils'; import { applyPitchYawLimits } from '../../../../../../core/blitz/applyPitchYawLimits'; import { modelDefinitions } from '../../../../../../core/blitzkit/modelDefinitions'; -import { modelTransformEvent } from '../../../../../../core/blitzkit/modelTransform'; +import { + modelTransformEvent, + ModelTransformEventData, +} from '../../../../../../core/blitzkit/modelTransform'; import { useEquipment } from '../../../../../../hooks/useEquipment'; import { useFullScreen } from '../../../../../../hooks/useFullScreen'; import { mutateDuel, useDuel } from '../../../../../../stores/duel'; @@ -13,8 +16,8 @@ import * as styles from './index.css'; export function RotationInputs() { const protagonist = useDuel((state) => state.protagonist!); const awaitedModelDefinitions = use(modelDefinitions); - const turretYawInput = useRef(null); - const gunPitchInput = useRef(null); + const yawInput = useRef(null); + const pitchInput = useRef(null); const tankModelDefinition = awaitedModelDefinitions[protagonist.tank.id]; const turretModelDefinition = tankModelDefinition.turrets[protagonist.turret.id]; @@ -24,13 +27,30 @@ export function RotationInputs() { const isFullScreen = useFullScreen(); useEffect(() => { - turretYawInput.current!.value = radToDeg(protagonist.yaw).toFixed(1); + yawInput.current!.value = radToDeg(protagonist.yaw).toFixed(1); }, [protagonist.yaw]); useEffect(() => { - gunPitchInput.current!.value = ( + pitchInput.current!.value = ( -radToDeg(protagonist.pitch) + initialGunPitch ).toFixed(1); }, [protagonist.pitch]); + useEffect(() => { + function handleTransformEvent({ pitch, yaw }: ModelTransformEventData) { + if (!pitchInput.current || !yawInput.current) return; + + pitchInput.current.value = (-radToDeg(pitch)).toFixed(1); + + if (yaw === undefined) return; + + yawInput.current.value = radToDeg(yaw).toFixed(1); + } + + modelTransformEvent.on(handleTransformEvent); + + return () => { + modelTransformEvent.off(handleTransformEvent); + }; + }, []); return ( { - const value = Number(turretYawInput.current!.value); + const value = Number(yawInput.current!.value); if (isNaN(value)) { - turretYawInput.current!.value = radToDeg(protagonist.yaw).toFixed( - 1, - ); + yawInput.current!.value = radToDeg(protagonist.yaw).toFixed(1); return; } const [pitch, yaw] = applyPitchYawLimits( @@ -72,15 +90,15 @@ export function RotationInputs() { state.protagonist!.pitch = pitch; state.protagonist!.yaw = yaw; }); - turretYawInput.current!.value = radToDeg(protagonist.yaw).toFixed(1); + yawInput.current!.value = radToDeg(protagonist.yaw).toFixed(1); }} onKeyDown={(event) => { if (event.key === 'Enter') { - turretYawInput.current?.blur(); + yawInput.current?.blur(); } }} - onFocus={() => turretYawInput.current?.focus()} - ref={turretYawInput} + onFocus={() => yawInput.current?.focus()} + ref={yawInput} > { - const value = Number(gunPitchInput.current!.value); + const value = Number(pitchInput.current!.value); if (isNaN(value)) { - gunPitchInput.current!.value = ( + pitchInput.current!.value = ( -radToDeg(protagonist.pitch) + initialGunPitch ).toFixed(1); return; @@ -119,17 +137,17 @@ export function RotationInputs() { state.protagonist!.pitch = pitch; state.protagonist!.yaw = yaw; }); - gunPitchInput.current!.value = ( + pitchInput.current!.value = ( -radToDeg(protagonist.pitch) + initialGunPitch ).toFixed(1); }} onKeyDown={(event) => { if (event.key === 'Enter') { - gunPitchInput.current?.blur(); + pitchInput.current?.blur(); } }} - onFocus={() => gunPitchInput.current?.focus()} - ref={gunPitchInput} + onFocus={() => pitchInput.current?.focus()} + ref={pitchInput} >