Preparing to support multiple sheets in r3f

This commit is contained in:
Aria Minaei 2021-09-05 23:03:34 +02:00
parent 3674f05487
commit 69a12440ca
5 changed files with 21 additions and 11 deletions

View file

@ -1,7 +1,8 @@
import {useCallback, useLayoutEffect} from 'react' import {useCallback, useLayoutEffect} from 'react'
import React from 'react' import React from 'react'
import {Canvas} from '@react-three/fiber' import {Canvas} from '@react-three/fiber'
import {useEditorStore} from '../store' import type {BaseSheetObjectType} from '../store'
import {allRegisteredObjects, useEditorStore} from '../store'
import shallow from 'zustand/shallow' import shallow from 'zustand/shallow'
import root from 'react-shadow/styled-components' import root from 'react-shadow/styled-components'
import ProxyManager from './ProxyManager' import ProxyManager from './ProxyManager'
@ -12,6 +13,7 @@ import {IoCameraReverseOutline} from 'react-icons/all'
import type {ISheet} from '@theatre/core' import type {ISheet} from '@theatre/core'
import useSnapshotEditorCamera from './useSnapshotEditorCamera' import useSnapshotEditorCamera from './useSnapshotEditorCamera'
import {getEditorSheet, getEditorSheetObject} from './editorStuff' import {getEditorSheet, getEditorSheetObject} from './editorStuff'
import type {$IntentionalAny} from '@theatre/shared/utils/types'
const GlobalStyle = createGlobalStyle` const GlobalStyle = createGlobalStyle`
:host { :host {
@ -99,8 +101,8 @@ const SnapshotEditor: React.FC<{paneId: string}> = (props) => {
const paneId = props.paneId const paneId = props.paneId
const editorObject = getEditorSheetObject() const editorObject = getEditorSheetObject()
const [sceneSnapshot, createSnapshot, sheet] = useEditorStore( const [sceneSnapshot, createSnapshot] = useEditorStore(
(state) => [state.sceneSnapshot, state.createSnapshot, state.sheet], (state) => [state.sceneSnapshot, state.createSnapshot],
shallow, shallow,
) )
@ -120,8 +122,18 @@ const SnapshotEditor: React.FC<{paneId: string}> = (props) => {
}, [editorOpen]) }, [editorOpen])
const onPointerMissed = useCallback(() => { const onPointerMissed = useCallback(() => {
if (sheet !== null) studio.setSelection([sheet]) // This callback runs when the user clicks in an empty space inside a SnapshotEditor.
}, [sheet]) // 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])
}
}, [])
if (!editorObject) return <></> if (!editorObject) return <></>

View file

@ -11,6 +11,7 @@ import type {
} from 'three' } from 'three'
import {Vector3} from 'three' import {Vector3} from 'three'
import type {EditableType} from '../store' import type {EditableType} from '../store'
import {allRegisteredObjects} from '../store'
import {baseSheetObjectType} from '../store' import {baseSheetObjectType} from '../store'
import {useEditorStore} from '../store' import {useEditorStore} from '../store'
import mergeRefs from 'react-merge-refs' import mergeRefs from 'react-merge-refs'
@ -60,6 +61,7 @@ const editable = <
useLayoutEffect(() => { useLayoutEffect(() => {
if (!sheet) return if (!sheet) return
const sheetObject = sheet.object(uniqueName, baseSheetObjectType) const sheetObject = sheet.object(uniqueName, baseSheetObjectType)
allRegisteredObjects.add(sheetObject)
setSheetObject(sheetObject) setSheetObject(sheetObject)
useEditorStore useEditorStore

View file

@ -7,16 +7,11 @@ let sheetObject: ISheetObject<typeof editorSheetObjectConfig> | undefined =
undefined undefined
const editorSheetObjectConfig = types.compound({ const editorSheetObjectConfig = types.compound({
isOpen: types.boolean(false, {label: 'Editor Open'}),
viewport: types.compound( viewport: types.compound(
{ {
showAxes: types.boolean(true, {label: 'Axes'}), showAxes: types.boolean(true, {label: 'Axes'}),
showGrid: types.boolean(true, {label: 'Grid'}), showGrid: types.boolean(true, {label: 'Grid'}),
showOverlayIcons: types.boolean(false, {label: 'Overlay Icons'}), showOverlayIcons: types.boolean(false, {label: 'Overlay Icons'}),
resolution: types.number(500, {
label: 'Resolution',
range: [0, 1000],
}),
shading: types.stringLiteral( shading: types.stringLiteral(
'rendered', 'rendered',
{ {

View file

@ -42,6 +42,8 @@ export const baseSheetObjectType = types.compound({
export type BaseSheetObjectType = ISheetObject<typeof baseSheetObjectType> export type BaseSheetObjectType = ISheetObject<typeof baseSheetObjectType>
export const allRegisteredObjects = new WeakSet<BaseSheetObjectType>()
export interface AbstractEditable<T extends EditableType> { export interface AbstractEditable<T extends EditableType> {
type: T type: T
role: 'active' | 'removed' role: 'active' | 'removed'

View file

@ -5,7 +5,6 @@ import {prism} from '@theatre/dataverse'
import SimpleCache from '@theatre/shared/utils/SimpleCache' import SimpleCache from '@theatre/shared/utils/SimpleCache'
import type {$FixMe, $IntentionalAny, VoidFn} from '@theatre/shared/utils/types' import type {$FixMe, $IntentionalAny, VoidFn} from '@theatre/shared/utils/types'
import type {IScrub} from '@theatre/studio/Scrub' import type {IScrub} from '@theatre/studio/Scrub'
import type {Studio} from '@theatre/studio/Studio' import type {Studio} from '@theatre/studio/Studio'
import { import {
isSheetObjectPublicAPI, isSheetObjectPublicAPI,