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

181 lines
4.7 KiB
TypeScript
Raw Normal View History

import {useCallback, useLayoutEffect} from 'react'
import React from 'react'
2021-06-18 13:05:06 +02:00
import {Canvas} from '@react-three/fiber'
import {useEditorStore} from '../store'
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 ProxyManager from './ProxyManager'
2021-07-22 21:26:34 +02:00
import studio, {ToolbarIconButton} from '@theatre/studio'
import {useVal} from '@theatre/dataverse-react'
2021-07-13 16:13:15 +02:00
import styled, {createGlobalStyle, StyleSheetManager} from 'styled-components'
2021-07-22 21:26:34 +02:00
import {IoCameraReverseOutline} from 'react-icons/all'
import type {ISheetObject, ISheet} from '@theatre/core'
import type {$FixMe} from '../types'
import useSnapshotEditorCamera from './useSnapshotEditorCamera'
2021-07-04 19:14:00 +02:00
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: React.FC<{snapshotEditorSheet: ISheet; paneId: string}> = ({
snapshotEditorSheet,
paneId,
}) => {
const [editorCamera, orbitControlsRef] = useSnapshotEditorCamera(
snapshotEditorSheet,
paneId,
)
2021-06-18 13:05:06 +02:00
const [editorObject, helpersRoot] = useEditorStore(
(state) => [state.editorObject, state.helpersRoot],
2021-06-18 13:05:06 +02:00
shallow,
)
const showGrid = useVal(editorObject?.props.viewport.showGrid) ?? true
const showAxes = useVal(editorObject?.props.viewport.showAxes) ?? true
2021-07-03 13:24:39 +02:00
2021-06-18 13:05:06 +02:00
return (
<>
2021-07-13 16:13:15 +02:00
{showGrid && <gridHelper args={[20, 20, '#6e6e6e', '#4a4b4b']} />}
2021-06-18 13:05:06 +02:00
{showAxes && <axesHelper args={[500]} />}
{editorCamera}
2021-06-18 13:05:06 +02:00
<primitive object={helpersRoot}></primitive>
<ProxyManager orbitControlsRef={orbitControlsRef} />
2021-07-13 16:13:15 +02:00
<color attach="background" args={[0.24, 0.24, 0.24]} />
2021-06-18 13:05:06 +02:00
</>
)
}
2021-07-14 18:37:32 +02:00
const Wrapper = styled.div`
2021-07-04 19:14:00 +02:00
tab-size: 4;
line-height: 1.15; /* 1 */
-webkit-text-size-adjust: 100%; /* 2 */
margin: 0;
2021-07-14 18:37:32 +02:00
position: absolute;
2021-07-04 19:14:00 +02:00
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
`
const CanvasWrapper = styled.div`
display: relative;
z-index: 0;
height: 100%;
`
2021-07-16 15:06:15 +02:00
const Overlay = styled.div`
position: absolute;
inset: 0;
z-index: 2;
pointer-events: none;
`
const Tools = styled.div`
position: absolute;
left: 8px;
top: 6px;
pointer-events: auto;
`
2021-07-14 18:37:32 +02:00
const SnapshotEditor: React.FC<{object: ISheetObject<$FixMe>; paneId: string}> =
(props) => {
const snapshotEditorSheet = props.object.sheet
const paneId = props.paneId
const [editorObject, sceneSnapshot, createSnapshot, sheet] = useEditorStore(
(state) => [
state.editorObject,
state.sceneSnapshot,
state.createSnapshot,
state.sheet,
],
shallow,
)
const editorOpen = true
useLayoutEffect(() => {
let timeout: NodeJS.Timeout | undefined
if (editorOpen) {
// a hack to make sure all the scene's props are
// applied before we take a snapshot
timeout = setTimeout(createSnapshot, 100)
2021-07-14 18:37:32 +02:00
}
return () => {
if (timeout !== undefined) {
clearTimeout(timeout)
}
}
}, [editorOpen])
const onPointerMissed = useCallback(() => {
if (sheet !== null) studio.__experimental_setSelection([sheet])
}, [sheet])
if (!editorObject) return <></>
return (
<root.div>
<StyleSheetManager disableVendorPrefixes>
<>
<GlobalStyle />
<Wrapper>
<Overlay>
<Tools>
<ToolbarIconButton
icon={<IoCameraReverseOutline />}
label="Refresh Snapshot"
onClick={createSnapshot}
></ToolbarIconButton>
</Tools>
</Overlay>
{sceneSnapshot ? (
<>
<CanvasWrapper>
<Canvas
// @ts-ignore
colorManagement
onCreated={({gl}) => {
gl.setClearColor('white')
}}
shadowMap
dpr={[1, 2]}
fog={'red'}
onPointerMissed={onPointerMissed}
>
<EditorScene
snapshotEditorSheet={snapshotEditorSheet}
paneId={paneId}
/>
</Canvas>
</CanvasWrapper>
</>
) : null}
</Wrapper>
{/* </PortalContext.Provider> */}
</>
</StyleSheetManager>
</root.div>
)
}
2021-06-18 13:05:06 +02:00
2021-07-14 18:37:32 +02:00
export default SnapshotEditor