theatre/packages/plugin-r3f/src/components/Editor.tsx

138 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-06-18 13:05:06 +02:00
import type {VFC} from 'react'
import {useLayoutEffect} from 'react'
2021-07-03 15:16:18 +02:00
import React, {useEffect, useRef} from 'react'
2021-06-18 13:05:06 +02:00
import {Canvas} from '@react-three/fiber'
import {useEditorStore} from '../store'
2021-07-03 15:16:18 +02:00
import {OrbitControls} from '@react-three/drei'
2021-06-18 13:05:06 +02:00
import shallow from 'zustand/shallow'
2021-07-04 19:14:00 +02:00
import root from 'react-shadow/styled-components'
2021-06-18 13:05:06 +02:00
import UI from './UI'
import ProxyManager from './ProxyManager'
import studio from '@theatre/studio'
import {useVal} from '@theatre/dataverse-react'
2021-07-04 19:14:00 +02:00
import styled, {createGlobalStyle} from 'styled-components'
const GlobalStyle = createGlobalStyle`
:host {
contain: strict;
all: initial;
color: white;
font: 11px -apple-system, BlinkMacSystemFont, Segoe WPC, Segoe Editor,
HelveticaNeue-Light, Ubuntu, Droid Sans, sans-serif;
}
* {
padding: 0;
margin: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
list-style: none;
}
`
2021-06-18 13:05:06 +02:00
const EditorScene = () => {
const orbitControlsRef = useRef<typeof OrbitControls>()
2021-07-03 15:16:18 +02:00
const [editorObject, helpersRoot, setOrbitControlsRef] = useEditorStore(
2021-06-18 13:05:06 +02:00
(state) => [
2021-07-03 13:24:39 +02:00
state.editorObject,
2021-06-18 13:05:06 +02:00
state.helpersRoot,
state.setOrbitControlsRef,
],
shallow,
)
2021-07-03 13:24:39 +02:00
const showGrid = useVal(editorObject?.props.showGrid) ?? true
const showAxes = useVal(editorObject?.props.showAxes) ?? true
2021-06-18 13:05:06 +02:00
useEffect(() => {
setOrbitControlsRef(orbitControlsRef)
}, [setOrbitControlsRef])
return (
<>
{showGrid && <gridHelper args={[1000, 1000, 0x444444, 0x888888]} />}
{showAxes && <axesHelper args={[500]} />}
{/* @ts-ignore */}
2021-07-05 18:39:57 +02:00
<OrbitControls ref={orbitControlsRef} enableDamping={false} />
2021-06-18 13:05:06 +02:00
<primitive object={helpersRoot}></primitive>
<ProxyManager orbitControlsRef={orbitControlsRef} />
</>
)
}
2021-07-04 19:14:00 +02:00
const Wrapper = styled.div<{editorOpen: boolean}>`
tab-size: 4;
line-height: 1.15; /* 1 */
-webkit-text-size-adjust: 100%; /* 2 */
margin: 0;
position: fixed;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
z-index: 50;
display: ${(props) => (props.editorOpen ? 'block' : 'none')};
`
const CanvasWrapper = styled.div`
display: relative;
z-index: 0;
height: 100%;
`
2021-06-18 13:05:06 +02:00
const Editor: VFC = () => {
const [editorObject, sceneSnapshot, initialEditorCamera, createSnapshot] =
useEditorStore(
(state) => [
state.editorObject,
state.sceneSnapshot,
state.initialEditorCamera,
state.createSnapshot,
],
shallow,
)
2021-07-04 19:14:00 +02:00
const editorOpen = !!useVal(editorObject?.props.isOpen)
useLayoutEffect(() => {
if (editorOpen) {
createSnapshot()
}
}, [editorOpen])
if (!editorObject) return <></>
2021-06-18 13:05:06 +02:00
return (
<root.div>
2021-07-04 19:14:00 +02:00
<GlobalStyle />
<Wrapper id="theatre-plugin-r3f-root" editorOpen={editorOpen}>
{sceneSnapshot ? (
2021-07-04 18:23:27 +02:00
<>
2021-07-04 19:14:00 +02:00
<CanvasWrapper>
<Canvas
// @ts-ignore
colorManagement
camera={initialEditorCamera}
onCreated={({gl}) => {
gl.setClearColor('white')
}}
shadowMap
pixelRatio={window.devicePixelRatio}
onPointerMissed={() => studio.__experimental_setSelection([])}
2021-06-18 13:05:06 +02:00
>
2021-07-04 19:14:00 +02:00
<EditorScene />
</Canvas>
</CanvasWrapper>
<UI />
2021-07-04 18:23:27 +02:00
</>
2021-07-04 19:14:00 +02:00
) : null}
</Wrapper>
2021-06-18 13:05:06 +02:00
</root.div>
)
}
export default Editor