From b40b7b247208f2935aba2fc9f3c520408c2e43c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Mon, 1 Jan 2024 21:03:27 -0600 Subject: [PATCH] feat: inputs --- src/app/tools/tankopedia/[id]/page.tsx | 64 +++++++++++++++++++++++++- src/core/math/normalizeAngle180.ts | 12 +++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/core/math/normalizeAngle180.ts diff --git a/src/app/tools/tankopedia/[id]/page.tsx b/src/app/tools/tankopedia/[id]/page.tsx index d89ffb27e..0878a2cea 100644 --- a/src/app/tools/tankopedia/[id]/page.tsx +++ b/src/app/tools/tankopedia/[id]/page.tsx @@ -39,6 +39,7 @@ import { tankDefinitions, tankNamesDiacritics, } from '../../../../core/blitzkrieg/tankDefinitions'; +import { normalizeAngle180 } from '../../../../core/math/normalizeAngle180'; import * as styles from '../page.css'; import { TankAlignment } from './components/tankAlignment'; @@ -73,6 +74,8 @@ export default function Page({ params }: { params: { id: string } }) { const gunOrigin = new Vector3(...turret.gunOrigin); const [turretRotation, setTurretRotation] = useState(0); const [orbitControlsEnabled, setOrbitControlsEnabled] = useState(true); + const turretRotationInput = useRef(null); + const gunRotationInput = useRef(null); useEffect(() => { if (!turret.guns.some(({ id }) => gun.id === id)) { @@ -89,6 +92,11 @@ export default function Page({ params }: { params: { id: string } }) { setVersusGun(versusTurret.guns.at(-1)!); } }, [versusTurret]); + useEffect(() => { + turretRotationInput.current!.value = `${Math.round( + turretRotation * (180 / Math.PI), + )}`; + }, [turretRotation]); return ( @@ -255,7 +263,9 @@ export default function Page({ params }: { params: { id: string } }) { } function handlePointerUp(event: PointerEvent) { setOrbitControlsEnabled(true); - setTurretRotation(draftTurretRotation); + setTurretRotation( + normalizeAngle180(draftTurretRotation), + ); window.removeEventListener( 'pointermove', @@ -302,6 +312,58 @@ export default function Page({ params }: { params: { id: string } }) { Enhanced armor )} + + + + Turret + { + setTurretRotation( + normalizeAngle180( + Number(turretRotationInput.current?.value) * + (Math.PI / 180), + ), + ); + }} + onKeyDown={(event) => { + if (event.key === 'Enter') { + turretRotationInput.current?.blur(); + } + }} + ref={turretRotationInput} + style={{ textAlign: 'right' }} + /> + ° + + + + Gun + + ° + + diff --git a/src/core/math/normalizeAngle180.ts b/src/core/math/normalizeAngle180.ts new file mode 100644 index 000000000..7764160d7 --- /dev/null +++ b/src/core/math/normalizeAngle180.ts @@ -0,0 +1,12 @@ +export function normalizeAngle180(angle: number) { + const newAngle = angle % (2 * Math.PI); + + if (newAngle > Math.PI) { + return newAngle - 2 * Math.PI; + } + if (newAngle < -Math.PI) { + return newAngle + 2 * Math.PI; + } + + return newAngle; +}