2021-07-31 15:10:08 +02:00
|
|
|
import {useCallback, useLayoutEffect} from 'react'
|
2021-07-30 10:59:59 +02:00
|
|
|
import React from 'react'
|
2021-06-18 13:05:06 +02:00
|
|
|
import {Canvas} from '@react-three/fiber'
|
2021-09-05 23:03:34 +02:00
|
|
|
import type {BaseSheetObjectType} from '../store'
|
|
|
|
import {allRegisteredObjects, useEditorStore} from '../store'
|
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 ProxyManager from './ProxyManager'
|
2021-07-22 21:26:34 +02:00
|
|
|
import studio, {ToolbarIconButton} from '@theatre/studio'
|
2021-07-02 20:43:25 +02:00
|
|
|
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'
|
2021-08-08 21:33:10 +02:00
|
|
|
import type {ISheet} from '@theatre/core'
|
2021-07-30 10:59:59 +02:00
|
|
|
import useSnapshotEditorCamera from './useSnapshotEditorCamera'
|
2021-08-08 22:25:48 +02:00
|
|
|
import {getEditorSheet, getEditorSheetObject} from './editorStuff'
|
2021-09-05 23:03:34 +02:00
|
|
|
import type {$IntentionalAny} from '@theatre/shared/utils/types'
|
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
|
|
|
|
2021-07-30 10:59:59 +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
|
|
|
|
2021-08-08 22:25:48 +02:00
|
|
|
const editorObject = getEditorSheetObject()
|
|
|
|
|
|
|
|
const helpersRoot = useEditorStore((state) => state.helpersRoot, shallow)
|
2021-06-18 13:05:06 +02:00
|
|
|
|
2021-07-07 16:39:53 +02:00
|
|
|
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]} />}
|
2021-07-30 10:59:59 +02:00
|
|
|
{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
|
|
|
|
2021-08-08 21:33:10 +02:00
|
|
|
const SnapshotEditor: React.FC<{paneId: string}> = (props) => {
|
2021-08-08 22:25:48 +02:00
|
|
|
const snapshotEditorSheet = getEditorSheet()
|
2021-08-08 21:33:10 +02:00
|
|
|
const paneId = props.paneId
|
2021-08-08 22:25:48 +02:00
|
|
|
const editorObject = getEditorSheetObject()
|
2021-08-08 21:33:10 +02:00
|
|
|
|
2021-09-05 23:03:34 +02:00
|
|
|
const [sceneSnapshot, createSnapshot] = useEditorStore(
|
|
|
|
(state) => [state.sceneSnapshot, state.createSnapshot],
|
2021-08-08 21:33:10 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
if (timeout !== undefined) {
|
|
|
|
clearTimeout(timeout)
|
2021-07-30 10:59:59 +02:00
|
|
|
}
|
2021-08-08 21:33:10 +02:00
|
|
|
}
|
|
|
|
}, [editorOpen])
|
|
|
|
|
|
|
|
const onPointerMissed = useCallback(() => {
|
2021-09-05 23:03:34 +02:00
|
|
|
// This callback runs when the user clicks in an empty space inside a SnapshotEditor.
|
|
|
|
// We'll try to set the current selection to the nearest sheet _if_ at least one object
|
|
|
|
// belonging to R3F was selected previously.
|
|
|
|
const obj: undefined | BaseSheetObjectType = studio.selection.find(
|
|
|
|
(sheetOrObject) =>
|
|
|
|
allRegisteredObjects.has(sheetOrObject as $IntentionalAny),
|
|
|
|
) as $IntentionalAny
|
|
|
|
|
|
|
|
if (obj) {
|
|
|
|
studio.setSelection([obj.sheet])
|
|
|
|
}
|
|
|
|
}, [])
|
2021-08-08 21:33:10 +02:00
|
|
|
|
|
|
|
if (!editorObject) return <></>
|
|
|
|
|
|
|
|
return (
|
|
|
|
<root.div>
|
|
|
|
<StyleSheetManager disableVendorPrefixes>
|
|
|
|
<>
|
|
|
|
<GlobalStyle />
|
|
|
|
<Wrapper>
|
|
|
|
<Overlay>
|
|
|
|
<Tools>
|
|
|
|
<ToolbarIconButton
|
2021-08-12 11:52:23 +02:00
|
|
|
title="Refresh Snapshot"
|
2021-08-08 21:33:10 +02:00
|
|
|
onClick={createSnapshot}
|
2021-08-12 11:52:23 +02:00
|
|
|
>
|
|
|
|
<IoCameraReverseOutline />
|
|
|
|
</ToolbarIconButton>
|
2021-08-08 21:33:10 +02:00
|
|
|
</Tools>
|
|
|
|
</Overlay>
|
|
|
|
|
|
|
|
{sceneSnapshot ? (
|
|
|
|
<>
|
|
|
|
<CanvasWrapper>
|
|
|
|
<Canvas
|
|
|
|
// @ts-ignore
|
|
|
|
colorManagement
|
|
|
|
onCreated={({gl}) => {
|
|
|
|
gl.setClearColor('white')
|
|
|
|
}}
|
|
|
|
shadowMap
|
|
|
|
dpr={[1, 2]}
|
|
|
|
fog={'red'}
|
|
|
|
frameloop="demand"
|
|
|
|
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
|