Files
blitzkit/src/app/tools/tankopedia/[id]/components/RotationInputs.tsx
T

129 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-01-04 13:06:40 -06:00
import { Flex, TextField } from '@radix-ui/themes';
import { use, useEffect, useRef } from 'react';
2024-01-06 13:21:11 -06:00
import { applyPitchYawLimits } from '../../../../../core/blitz/applyPitchYawLimits';
2024-01-04 13:06:40 -06:00
import { modelDefinitions } from '../../../../../core/blitzkrieg/modelDefinitions';
2024-01-13 18:50:13 -06:00
import { modelTransformEvent } from '../../../../../core/blitzkrieg/modelTransform';
2024-01-21 10:34:52 -06:00
import {
mutateTankopediaTemporary,
useTankopediaTemporary,
2024-01-04 13:06:40 -06:00
} from '../../../../../stores/tankopedia';
2024-01-15 15:02:04 -06:00
import { Duel } from '../page';
2024-01-04 13:06:40 -06:00
2024-01-15 15:02:04 -06:00
interface RotationInputsProps {
duel: Duel;
}
export function RotationInputs({ duel: { protagonist } }: RotationInputsProps) {
2024-01-21 22:11:01 -06:00
const physical = useTankopediaTemporary((state) => state.model.pose);
2024-01-04 13:06:40 -06:00
const awaitedModelDefinitions = use(modelDefinitions);
const turretYawInput = useRef<HTMLInputElement>(null);
const gunPitchInput = useRef<HTMLInputElement>(null);
useEffect(() => {
turretYawInput.current!.value = `${-Math.round(
2024-01-13 18:24:04 -06:00
physical.yaw * (180 / Math.PI),
2024-01-04 13:06:40 -06:00
)}`;
2024-01-13 18:24:04 -06:00
}, [physical.yaw]);
2024-01-04 13:06:40 -06:00
useEffect(() => {
gunPitchInput.current!.value = `${-Math.round(
2024-01-13 18:24:04 -06:00
physical.pitch * (180 / Math.PI),
2024-01-04 13:06:40 -06:00
)}`;
2024-01-13 18:24:04 -06:00
}, [physical.pitch]);
2024-01-04 13:06:40 -06:00
2024-01-11 21:44:44 -06:00
const tankModelDefinition = awaitedModelDefinitions[protagonist.tank.id];
const turretModelDefinition =
tankModelDefinition.turrets[protagonist.turret.id];
const gunModelDefinition = turretModelDefinition.guns[protagonist.gun.id];
2024-01-04 13:06:40 -06:00
return (
<Flex
2024-01-06 17:56:12 -06:00
justify="center"
2024-01-04 13:06:40 -06:00
align="center"
style={{
2024-01-06 17:56:12 -06:00
width: 'calc(100% - 16px)',
2024-01-04 13:06:40 -06:00
position: 'absolute',
2024-01-06 17:56:12 -06:00
left: 8,
bottom: 8,
2024-01-04 13:06:40 -06:00
}}
>
2024-01-06 17:56:12 -06:00
<Flex
gap="2"
2024-01-04 13:06:40 -06:00
style={{
2024-01-13 18:24:04 -06:00
width: 256,
2024-01-04 13:06:40 -06:00
}}
>
2024-01-06 17:56:12 -06:00
<TextField.Root
style={{
flex: 1,
2024-01-04 13:06:40 -06:00
}}
2024-01-06 17:56:12 -06:00
>
2024-01-19 22:08:05 -06:00
<TextField.Slot>Yaw</TextField.Slot>
2024-01-06 17:56:12 -06:00
<TextField.Input
2024-01-13 18:24:04 -06:00
defaultValue={Math.round(physical.yaw * (180 / Math.PI))}
2024-01-06 17:56:12 -06:00
onBlur={() => {
const [pitch, yaw] = applyPitchYawLimits(
2024-01-13 18:24:04 -06:00
physical.pitch,
2024-01-06 17:56:12 -06:00
-Number(turretYawInput.current?.value) * (Math.PI / 180),
gunModelDefinition.pitch,
turretModelDefinition.yaw,
);
2024-01-13 18:50:13 -06:00
modelTransformEvent.emit({ pitch, yaw });
2024-01-21 10:34:52 -06:00
mutateTankopediaTemporary((state) => {
2024-01-21 22:11:01 -06:00
state.model.pose.pitch = pitch;
state.model.pose.yaw = yaw;
2024-01-06 17:56:12 -06:00
});
turretYawInput.current!.value = `${Math.round(
yaw * (180 / Math.PI),
)}`;
}}
onKeyDown={(event) => {
if (event.key === 'Enter') {
turretYawInput.current?.blur();
}
}}
onFocus={() => turretYawInput.current?.focus()}
ref={turretYawInput}
style={{ textAlign: 'right' }}
/>
<TextField.Slot>°</TextField.Slot>
</TextField.Root>
2024-01-04 13:06:40 -06:00
2024-01-06 17:56:12 -06:00
<TextField.Root
style={{
flex: 1,
2024-01-04 13:06:40 -06:00
}}
2024-01-06 17:56:12 -06:00
>
2024-01-19 22:08:05 -06:00
<TextField.Slot>Pitch</TextField.Slot>
2024-01-06 17:56:12 -06:00
<TextField.Input
2024-01-13 18:24:04 -06:00
defaultValue={-Math.round(physical.pitch * (180 / Math.PI))}
2024-01-06 17:56:12 -06:00
onBlur={() => {
const [pitch, yaw] = applyPitchYawLimits(
-Number(gunPitchInput.current?.value) * (Math.PI / 180),
2024-01-13 18:24:04 -06:00
physical.yaw,
2024-01-06 17:56:12 -06:00
gunModelDefinition.pitch,
turretModelDefinition.yaw,
);
2024-01-13 18:50:13 -06:00
modelTransformEvent.emit({ pitch, yaw });
2024-01-21 10:34:52 -06:00
mutateTankopediaTemporary((state) => {
2024-01-21 22:11:01 -06:00
state.model.pose.pitch = pitch;
state.model.pose.yaw = yaw;
2024-01-06 17:56:12 -06:00
});
gunPitchInput.current!.value = `${Math.round(
pitch * (180 / Math.PI),
)}`;
}}
onKeyDown={(event) => {
if (event.key === 'Enter') {
gunPitchInput.current?.blur();
}
}}
onFocus={() => gunPitchInput.current?.focus()}
ref={gunPitchInput}
style={{ textAlign: 'right' }}
/>
<TextField.Slot>°</TextField.Slot>
</TextField.Root>
</Flex>
2024-01-04 13:06:40 -06:00
</Flex>
);
}