From b745ad2fa7f0e19ae5cc5f96b04f9ec6567ecfe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Wed, 3 Jan 2024 12:21:30 -0600 Subject: [PATCH] feat: controls --- .../tankopedia/[id]/components/Control.tsx | 24 +++++++++++++++++++ src/app/tools/tankopedia/[id]/page.tsx | 11 +++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/app/tools/tankopedia/[id]/components/Control.tsx diff --git a/src/app/tools/tankopedia/[id]/components/Control.tsx b/src/app/tools/tankopedia/[id]/components/Control.tsx new file mode 100644 index 000000000..e80d7044c --- /dev/null +++ b/src/app/tools/tankopedia/[id]/components/Control.tsx @@ -0,0 +1,24 @@ +import { OrbitControls } from '@react-three/drei'; +import { useThree } from '@react-three/fiber'; +import { RefObject, useEffect, useRef } from 'react'; +import { Box3, Object3D } from 'three'; +import { OrbitControls as OrbitControlsClass } from 'three-stdlib'; + +interface ControlsProps { + enabled: boolean; + fit: RefObject; +} + +export function Controls({ enabled, fit }: ControlsProps) { + const camera = useThree((state) => state.camera); + const orbitControls = useRef(null); + + useEffect(() => { + const box = new Box3().setFromObject(fit.current!); + + orbitControls.current?.target.set(0, (box.min.y + box.max.y) / 2, 0); + camera.position.set(-5, (box.min.y + box.max.y) / 2, -15); + }, [fit.current]); + + return ; +} diff --git a/src/app/tools/tankopedia/[id]/page.tsx b/src/app/tools/tankopedia/[id]/page.tsx index 54a839174..22b71485c 100644 --- a/src/app/tools/tankopedia/[id]/page.tsx +++ b/src/app/tools/tankopedia/[id]/page.tsx @@ -40,6 +40,7 @@ import { } from '../../../../core/blitzkrieg/tankDefinitions'; import { normalizeAnglePI } from '../../../../core/math/normalizeAngle180'; import * as styles from '../page.css'; +import { Controls } from './components/Control'; const X_AXIS = new Vector3(1, 0, 0); @@ -81,6 +82,7 @@ export default function Page({ params }: { params: { id: string } }) { const [turretYaw, setTurretYaw] = useState(0); const [hullYaw, setHullYaw] = useState(0); const [gunPitch, setGunPitch] = useState(0); + const [controlsEnabled, setControlsEnabled] = useState(true); const turretYawInput = useRef(null); const hullYawInput = useRef(null); const gunPitchInput = useRef(null); @@ -158,6 +160,7 @@ export default function Page({ params }: { params: { id: string } }) { distance={25} color={new Color('red')} /> + {/* I really like apartment, dawn, and sunset */} , ) { event.stopPropagation(); + if (isTurret) { + setControlsEnabled(false); draftTurretYaw = turretYaw; window.addEventListener( 'pointermove', @@ -285,6 +292,7 @@ export default function Page({ params }: { params: { id: string } }) { } function handlePointerUp() { setTurretYaw(normalizeAnglePI(draftTurretYaw)); + setControlsEnabled(true); window.removeEventListener( 'pointermove', handlePointerMove, @@ -342,6 +350,8 @@ export default function Page({ params }: { params: { id: string } }) { event: ThreeEvent, ) { event.stopPropagation(); + + setControlsEnabled(false); draftMantletPitch = gunPitch; draftTurretYaw = turretYaw; window.addEventListener( @@ -406,6 +416,7 @@ export default function Page({ params }: { params: { id: string } }) { } } function handlePointerUp() { + setControlsEnabled(true); setGunPitch(normalizeAnglePI(draftMantletPitch)); setTurretYaw(normalizeAnglePI(draftTurretYaw)); window.removeEventListener(