WIP: Move r3f's UI state to Theatre - 1

This commit is contained in:
Aria Minaei 2021-06-29 16:00:27 +02:00
parent 430275704b
commit 06ff052797
2 changed files with 12 additions and 29 deletions

View file

@ -17,8 +17,6 @@ import mergeRefs from 'react-merge-refs'
import type {$FixMe} from '@theatre/shared/utils/types' import type {$FixMe} from '@theatre/shared/utils/types'
import type {ISheetObject} from '@theatre/core' import type {ISheetObject} from '@theatre/core'
const log = console.log
interface Elements { interface Elements {
group: Group group: Group
mesh: Mesh mesh: Mesh

View file

@ -7,7 +7,7 @@ import type {OrbitControls} from '@react-three/drei'
// @ts-ignore TODO // @ts-ignore TODO
import type {ContainerProps} from '@react-three/fiber' import type {ContainerProps} from '@react-three/fiber'
import type {ISheet, ISheetObject} from '@theatre/core' import type {ISheet, ISheetObject} from '@theatre/core'
import {types} from '@theatre/core' import {types, getProject} from '@theatre/core'
export type EditableType = export type EditableType =
| 'group' | 'group'
@ -137,6 +137,7 @@ export interface EditableState {
export type EditorStore = { export type EditorStore = {
sheet: ISheet | null sheet: ISheet | null
uiSheet: ISheet | null
sheetObjects: {[uniqueName in string]?: BaseSheetObjectType} sheetObjects: {[uniqueName in string]?: BaseSheetObjectType}
scene: Scene | null scene: Scene | null
gl: WebGLRenderer | null gl: WebGLRenderer | null
@ -169,7 +170,7 @@ export type EditorStore = {
allowImplicitInstancing: boolean, allowImplicitInstancing: boolean,
editorCamera: ContainerProps['camera'], editorCamera: ContainerProps['camera'],
sheet: ISheet, sheet: ISheet,
initialState?: EditableState, uiSheet: null | ISheet,
) => void ) => void
setOrbitControlsRef: ( setOrbitControlsRef: (
@ -213,6 +214,7 @@ const config: StateCreator<EditorStore> = (set, get) => {
return { return {
sheet: null, sheet: null,
uiSheet: null,
sheetObjects: {}, sheetObjects: {},
scene: null, scene: null,
gl: null, gl: null,
@ -244,35 +246,15 @@ const config: StateCreator<EditorStore> = (set, get) => {
allowImplicitInstancing, allowImplicitInstancing,
editorCamera, editorCamera,
sheet, sheet,
initialState, uiSheet,
) => { ) => {
const editables = get().editables
const newEditables: Record<string, Editable> = initialState
? Object.fromEntries(
Object.entries(initialState.editables).map(
([name, initialEditable]) => {
const originalEditable = editables[name]
return [
name,
{
type: initialEditable.type,
role: originalEditable?.role ?? 'removed',
},
]
},
),
)
: editables
set({ set({
scene, scene,
gl, gl,
allowImplicitInstancing, allowImplicitInstancing,
editables: newEditables,
initialEditorCamera: editorCamera, initialEditorCamera: editorCamera,
initialState: initialState ?? null,
sheet, sheet,
uiSheet,
}) })
}, },
@ -384,17 +366,20 @@ export const useEditorStore = create<EditorStore>(config)
export type BindFunction = (options: { export type BindFunction = (options: {
allowImplicitInstancing?: boolean allowImplicitInstancing?: boolean
state?: EditableState
editorCamera?: ContainerProps['camera'] editorCamera?: ContainerProps['camera']
sheet: ISheet sheet: ISheet
}) => (options: {gl: WebGLRenderer; scene: Scene}) => void }) => (options: {gl: WebGLRenderer; scene: Scene}) => void
export const bindToCanvas: BindFunction = ({ export const bindToCanvas: BindFunction = ({
allowImplicitInstancing = false, allowImplicitInstancing = false,
state,
editorCamera = {}, editorCamera = {},
sheet, sheet,
}) => { }) => {
const uiSheet: null | ISheet =
process.env.NODE_ENV === 'development'
? getProject('R3F Plugin').sheet('UI')
: null
return ({gl, scene}) => { return ({gl, scene}) => {
const init = useEditorStore.getState().init const init = useEditorStore.getState().init
init( init(
@ -403,7 +388,7 @@ export const bindToCanvas: BindFunction = ({
allowImplicitInstancing, allowImplicitInstancing,
{...{position: [20, 20, 20]}, ...editorCamera}, {...{position: [20, 20, 20]}, ...editorCamera},
sheet, sheet,
state, uiSheet,
) )
} }
} }