import { IdeContext } from 'src/components/IdeToolbarNew'
import { useRef, useState, useEffect, useContext, useMemo } from 'react'
import { Canvas, extend, useFrame, useThree } from 'react-three-fiber'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { Vector3 } from 'three'
extend({ OrbitControls })
function Controls({ onCameraChange }) {
const controls = useRef()
const { scene, camera, gl } = useThree()
useEffect(() => {
// init camera position
camera.position.x = 12
camera.position.y = 12
if (controls.current) {
const callback = ({ target }) => {
// TODO figure out Three's rotations to make it match openscad's camera
const vector = new Vector3()
camera.getWorldDirection(vector)
console.log(Object.values(vector).map((val) => (val * 180) / Math.PI))
const { _x, _y, _z } = target.object.rotation
const rad2Deg = 180 / Math.PI
const [x, y, z] = [_x * rad2Deg, -_y * rad2Deg, _z * rad2Deg]
console.log({ y, z, x })
console.log(target, 'target')
onCameraChange({
position: target.object.position,
rotation: { x, y, z },
})
}
controls.current.addEventListener('end', callback)
const oldCurrent = controls.current
return () => oldCurrent.removeEventListener('end', callback)
}
}, [])
useFrame(() => controls.current.update())
return (