2024-01-03 12:21:30 -06:00
|
|
|
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';
|
2024-01-04 13:06:40 -06:00
|
|
|
import { useTankopedia } from '../../../../../stores/tankopedia';
|
2024-01-03 12:21:30 -06:00
|
|
|
|
|
|
|
|
interface ControlsProps {
|
|
|
|
|
fit: RefObject<Object3D>;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-04 13:06:40 -06:00
|
|
|
export function Controls({ fit }: ControlsProps) {
|
2024-01-03 12:21:30 -06:00
|
|
|
const camera = useThree((state) => state.camera);
|
|
|
|
|
const orbitControls = useRef<OrbitControlsClass>(null);
|
2024-01-04 13:06:40 -06:00
|
|
|
const enabled = useTankopedia((state) => state.model.controlsEnabled);
|
2024-01-03 12:21:30 -06:00
|
|
|
|
|
|
|
|
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 <OrbitControls ref={orbitControls} enabled={enabled} />;
|
|
|
|
|
}
|