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

162 lines
5.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';
import { normalizeAnglePI } from '../../../../../core/math/normalizeAngle180';
import mutateTankopedia, {
useTankopedia,
} from '../../../../../stores/tankopedia';
2024-01-07 13:13:27 -06:00
export function RotationInputs() {
2024-01-04 13:06:40 -06:00
const model = useTankopedia((state) => state.model);
const awaitedModelDefinitions = use(modelDefinitions);
const turretYawInput = useRef<HTMLInputElement>(null);
const hullYawInput = useRef<HTMLInputElement>(null);
const gunPitchInput = useRef<HTMLInputElement>(null);
2024-01-07 13:13:27 -06:00
const protagonist = useTankopedia((state) => {
if (!state.areTanksAssigned) return;
return state.protagonist;
});
2024-01-04 13:06:40 -06:00
2024-01-07 13:13:27 -06:00
if (!protagonist) return null;
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
useEffect(() => {
hullYawInput.current!.value = `${-Math.round(
model.hullYaw * (180 / Math.PI),
)}`;
}, [model.hullYaw]);
useEffect(() => {
turretYawInput.current!.value = `${-Math.round(
model.turretYaw * (180 / Math.PI),
)}`;
}, [model.turretYaw]);
useEffect(() => {
gunPitchInput.current!.value = `${-Math.round(
model.gunPitch * (180 / Math.PI),
)}`;
}, [model.gunPitch]);
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-06 17:56:12 -06:00
width: 352,
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
>
<TextField.Slot>Hull</TextField.Slot>
<TextField.Input
defaultValue={Math.round(model.hullYaw * (180 / Math.PI))}
onBlur={() => {
const normalized = normalizeAnglePI(
-Number(hullYawInput.current?.value) * (Math.PI / 180),
);
mutateTankopedia((state) => {
state.model.hullYaw = normalized;
});
hullYawInput.current!.value = `${Math.round(normalized)}`;
}}
onKeyDown={(event) => {
if (event.key === 'Enter') {
hullYawInput.current?.blur();
}
}}
onFocus={() => hullYawInput.current?.focus()}
ref={hullYawInput}
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
>
<TextField.Slot>Turret</TextField.Slot>
<TextField.Input
defaultValue={Math.round(model.turretYaw * (180 / Math.PI))}
onBlur={() => {
const [pitch, yaw] = applyPitchYawLimits(
model.gunPitch,
-Number(turretYawInput.current?.value) * (Math.PI / 180),
gunModelDefinition.pitch,
turretModelDefinition.yaw,
);
mutateTankopedia((state) => {
state.model.gunPitch = pitch;
state.model.turretYaw = yaw;
});
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
>
<TextField.Slot>Gun</TextField.Slot>
<TextField.Input
defaultValue={-Math.round(model.gunPitch * (180 / Math.PI))}
onBlur={() => {
const [pitch, yaw] = applyPitchYawLimits(
-Number(gunPitchInput.current?.value) * (Math.PI / 180),
model.turretYaw,
gunModelDefinition.pitch,
turretModelDefinition.yaw,
);
mutateTankopedia((state) => {
state.model.gunPitch = pitch;
state.model.turretYaw = yaw;
});
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>
);
}