theatre/examples/basic-dom/Scene.tsx

136 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

2021-09-22 09:30:42 +02:00
import type {IScrub} from '@theatre/studio'
2021-06-18 13:05:06 +02:00
import studio from '@theatre/studio'
import React, {useLayoutEffect, useMemo, useState} from 'react'
import type {ISheet, ISheetObject, IProject} from '@theatre/core'
import type {UseDragOpts} from './useDrag'
import useDrag from './useDrag'
2021-09-01 10:36:44 +02:00
studio.initialize()
2021-09-06 11:26:00 +02:00
const boxObjectConfig = {
x: 0,
y: 0,
}
2021-06-18 13:05:06 +02:00
const Box: React.FC<{
id: string
sheet: ISheet
2021-09-22 09:30:42 +02:00
selectedObject: ISheetObject<any> | undefined
2021-06-18 13:05:06 +02:00
}> = ({id, sheet, selectedObject}) => {
// This is cheap to call and always returns the same value, so no need for useMemo()
const obj = sheet.object(id, boxObjectConfig)
2021-06-18 13:05:06 +02:00
const isSelected = selectedObject === obj
const [pos, setPos] = useState<{x: number; y: number}>({x: 0, y: 0})
useLayoutEffect(() => {
const unsubscribeFromChanges = obj.onValuesChange((newValues) => {
setPos(newValues)
})
return unsubscribeFromChanges
}, [id])
const [divRef, setDivRef] = useState<HTMLElement | null>(null)
const dragOpts = useMemo((): UseDragOpts => {
let scrub: IScrub | undefined
let initial: typeof obj.value
let firstOnDragCalled = false
return {
onDragStart() {
scrub = studio.scrub()
initial = obj.value
firstOnDragCalled = false
},
onDrag(x, y) {
if (!firstOnDragCalled) {
studio.setSelection([obj])
2021-06-18 13:05:06 +02:00
firstOnDragCalled = true
}
scrub!.capture(({set}) => {
set(obj.props, {x: x + initial.x, y: y + initial.y})
})
},
onDragEnd(dragHappened) {
if (dragHappened) {
scrub!.commit()
} else {
scrub!.discard()
}
},
lockCursorTo: 'move',
}
}, [])
useDrag(divRef, dragOpts)
return (
<div
onClick={() => {
studio.setSelection([obj])
2021-06-18 13:05:06 +02:00
}}
ref={setDivRef}
style={{
width: 100,
height: 100,
background: 'gray',
position: 'absolute',
left: pos.x + 'px',
top: pos.y + 'px',
boxSizing: 'border-box',
border: isSelected ? '1px solid #5a92fa' : '1px solid transparent',
}}
></div>
)
}
let lastBoxId = 1
export const Scene: React.FC<{project: IProject}> = ({project}) => {
const [boxes, setBoxes] = useState<Array<string>>(['0', '1'])
// This is cheap to call and always returns the same value, so no need for useMemo()
const sheet = project.sheet('Scene', 'default')
2021-09-22 09:30:42 +02:00
const [selection, _setSelection] = useState<Array<ISheetObject>>([])
2021-06-18 13:05:06 +02:00
useLayoutEffect(() => {
return studio.onSelectionChange((newSelection) => {
2021-09-22 09:30:42 +02:00
_setSelection(
newSelection.filter(
(s): s is ISheetObject => s.type === 'Theatre_SheetObject_PublicAPI',
),
)
2021-06-18 13:05:06 +02:00
})
})
return (
<div
style={{
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
background: '#575757',
}}
>
<button
onClick={() => {
setBoxes((boxes) => [...boxes, String(++lastBoxId)])
}}
>
Add
</button>
{boxes.map((id) => (
<Box
key={'box' + id}
id={id}
sheet={sheet}
selectedObject={selection[0]}
2021-06-18 13:05:06 +02:00
/>
))}
</div>
)
}