feat: live input updates

This commit is contained in:
TrèsAbhi committed 2024-05-22 13:47:35 -05:00
1 parent 6b63231194
commit a3cbe6d1e6
1 file changed
+37 -19
@@ -4,7 +4,10 @@ import { use, useEffect, useRef } from 'react';
import { degToRad, radToDeg } from 'three/src/math/MathUtils'; import { degToRad, radToDeg } from 'three/src/math/MathUtils';
import { applyPitchYawLimits } from '../../../../../../core/blitz/applyPitchYawLimits'; import { applyPitchYawLimits } from '../../../../../../core/blitz/applyPitchYawLimits';
import { modelDefinitions } from '../../../../../../core/blitzkit/modelDefinitions'; 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 { useEquipment } from '../../../../../../hooks/useEquipment';
import { useFullScreen } from '../../../../../../hooks/useFullScreen'; import { useFullScreen } from '../../../../../../hooks/useFullScreen';
import { mutateDuel, useDuel } from '../../../../../../stores/duel'; import { mutateDuel, useDuel } from '../../../../../../stores/duel';
@@ -13,8 +16,8 @@ import * as styles from './index.css';
export function RotationInputs() { export function RotationInputs() {
const protagonist = useDuel((state) => state.protagonist!); const protagonist = useDuel((state) => state.protagonist!);
const awaitedModelDefinitions = use(modelDefinitions); const awaitedModelDefinitions = use(modelDefinitions);
const turretYawInput = useRef<HTMLInputElement>(null); const yawInput = useRef<HTMLInputElement>(null);
const gunPitchInput = useRef<HTMLInputElement>(null); const pitchInput = useRef<HTMLInputElement>(null);
const tankModelDefinition = awaitedModelDefinitions[protagonist.tank.id]; const tankModelDefinition = awaitedModelDefinitions[protagonist.tank.id];
const turretModelDefinition = const turretModelDefinition =
tankModelDefinition.turrets[protagonist.turret.id]; tankModelDefinition.turrets[protagonist.turret.id];
@@ -24,13 +27,30 @@ export function RotationInputs() {
const isFullScreen = useFullScreen(); const isFullScreen = useFullScreen();
useEffect(() => { useEffect(() => {
turretYawInput.current!.value = radToDeg(protagonist.yaw).toFixed(1); yawInput.current!.value = radToDeg(protagonist.yaw).toFixed(1);
}, [protagonist.yaw]); }, [protagonist.yaw]);
useEffect(() => { useEffect(() => {
gunPitchInput.current!.value = ( pitchInput.current!.value = (
-radToDeg(protagonist.pitch) + initialGunPitch -radToDeg(protagonist.pitch) + initialGunPitch
).toFixed(1); ).toFixed(1);
}, [protagonist.pitch]); }, [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 ( return (
<Flex <Flex
@@ -53,11 +73,9 @@ export function RotationInputs() {
textAlign: 'right', textAlign: 'right',
}} }}
onBlur={() => { onBlur={() => {
const value = Number(turretYawInput.current!.value); const value = Number(yawInput.current!.value);
if (isNaN(value)) { if (isNaN(value)) {
turretYawInput.current!.value = radToDeg(protagonist.yaw).toFixed( yawInput.current!.value = radToDeg(protagonist.yaw).toFixed(1);
1,
);
return; return;
} }
const [pitch, yaw] = applyPitchYawLimits( const [pitch, yaw] = applyPitchYawLimits(
@@ -72,15 +90,15 @@ export function RotationInputs() {
state.protagonist!.pitch = pitch; state.protagonist!.pitch = pitch;
state.protagonist!.yaw = yaw; state.protagonist!.yaw = yaw;
}); });
turretYawInput.current!.value = radToDeg(protagonist.yaw).toFixed(1); yawInput.current!.value = radToDeg(protagonist.yaw).toFixed(1);
}} }}
onKeyDown={(event) => { onKeyDown={(event) => {
if (event.key === 'Enter') { if (event.key === 'Enter') {
turretYawInput.current?.blur(); yawInput.current?.blur();
} }
}} }}
onFocus={() => turretYawInput.current?.focus()} onFocus={() => yawInput.current?.focus()}
ref={turretYawInput} ref={yawInput}
> >
<TextField.Slot <TextField.Slot
style={{ style={{
@@ -100,9 +118,9 @@ export function RotationInputs() {
textAlign: 'right', textAlign: 'right',
}} }}
onBlur={() => { onBlur={() => {
const value = Number(gunPitchInput.current!.value); const value = Number(pitchInput.current!.value);
if (isNaN(value)) { if (isNaN(value)) {
gunPitchInput.current!.value = ( pitchInput.current!.value = (
-radToDeg(protagonist.pitch) + initialGunPitch -radToDeg(protagonist.pitch) + initialGunPitch
).toFixed(1); ).toFixed(1);
return; return;
@@ -119,17 +137,17 @@ export function RotationInputs() {
state.protagonist!.pitch = pitch; state.protagonist!.pitch = pitch;
state.protagonist!.yaw = yaw; state.protagonist!.yaw = yaw;
}); });
gunPitchInput.current!.value = ( pitchInput.current!.value = (
-radToDeg(protagonist.pitch) + initialGunPitch -radToDeg(protagonist.pitch) + initialGunPitch
).toFixed(1); ).toFixed(1);
}} }}
onKeyDown={(event) => { onKeyDown={(event) => {
if (event.key === 'Enter') { if (event.key === 'Enter') {
gunPitchInput.current?.blur(); pitchInput.current?.blur();
} }
}} }}
onFocus={() => gunPitchInput.current?.focus()} onFocus={() => pitchInput.current?.focus()}
ref={gunPitchInput} ref={pitchInput}
> >
<TextField.Slot <TextField.Slot
style={{ style={{