Reference window fixes and improvements (#227)

This commit is contained in:
Andrew Prifer 2022-06-18 17:37:09 +02:00 committed by GitHub
parent 262d7d61d3
commit 0e724d631d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 32 deletions

View file

@ -12,20 +12,22 @@ import {useVal} from '@theatre/react'
import {getEditorSheetObject} from '../../editorStuff' import {getEditorSheetObject} from '../../editorStuff'
import studio from '@theatre/studio' import studio from '@theatre/studio'
const Container = styled.div` const Container = styled.div<{minified: boolean}>`
position: relative; position: relative;
border-radius: 4px; display: flex;
justify-content: center;
align-items: center;
pointer-events: auto; pointer-events: auto;
cursor: pointer; cursor: pointer;
overflow: hidden; overflow: hidden;
border-radius: ${({minified}) => (minified ? '2px' : '4px')};
&.hidden { box-shadow: 0 1px 1px rgba(0, 0, 0, 0.25), 0 2px 6px rgba(0, 0, 0, 0.15);
border-radius: 8px;
}
` `
const Canvas = styled.canvas` const Canvas = styled.canvas<{width: number; height: number}>`
display: block; display: block;
width: ${({width, height}) => (width > height ? 'auto' : '100%')};
height: ${({width, height}) => (height > width ? 'auto' : '100%')};
` `
const staticAnimation = keyframes` const staticAnimation = keyframes`
@ -71,15 +73,6 @@ const Warning = styled.div`
opacity: 0.8; opacity: 0.8;
` `
const Dot = styled.div`
width: 8px;
height: 8px;
border-radius: 50%;
background-color: red;
pointer-events: auto;
cursor: pointer;
`
interface ReferenceWindowProps { interface ReferenceWindowProps {
maxHeight: number maxHeight: number
maxWidth: number maxWidth: number
@ -88,11 +81,11 @@ interface ReferenceWindowProps {
const ReferenceWindow: VFC<ReferenceWindowProps> = ({maxHeight, maxWidth}) => { const ReferenceWindow: VFC<ReferenceWindowProps> = ({maxHeight, maxWidth}) => {
const canvasRef = useRef<HTMLCanvasElement>(null) const canvasRef = useRef<HTMLCanvasElement>(null)
const visible = const minified =
useVal(getEditorSheetObject()?.props.viewport.showReferenceWindow) ?? true useVal(getEditorSheetObject()?.props.viewport.showReferenceWindow) ?? true
const [gl] = useExtensionStore((state) => [state.gl], shallow) const [gl] = useExtensionStore((state) => [state.gl], shallow)
const [ref, bounds] = useMeasure() const [ref, {width: origWidth, height: origHeight}] = useMeasure()
const preserveDrawingBuffer = const preserveDrawingBuffer =
gl?.getContextAttributes()?.preserveDrawingBuffer ?? false gl?.getContextAttributes()?.preserveDrawingBuffer ?? false
@ -105,16 +98,15 @@ const ReferenceWindow: VFC<ReferenceWindowProps> = ({maxHeight, maxWidth}) => {
const [width, height] = useMemo(() => { const [width, height] = useMemo(() => {
if (!gl) return [0, 0] if (!gl) return [0, 0]
const aspectRatio = gl.domElement.width / gl.domElement.height const aspectRatio = origWidth / origHeight
const width = Math.min(aspectRatio * maxHeight, maxWidth) const width = Math.min(aspectRatio * maxHeight, maxWidth)
const height = width / aspectRatio const height = width / aspectRatio
return [width, height] return [width, height]
}, [gl, maxWidth, maxHeight]) }, [gl, maxWidth, maxHeight, origWidth, origHeight])
useEffect(() => { useEffect(() => {
// if (!visible) return
let animationHandle: number let animationHandle: number
const draw = (gl: WebGLRenderer) => () => { const draw = (gl: WebGLRenderer) => () => {
animationHandle = requestAnimationFrame(draw(gl)) animationHandle = requestAnimationFrame(draw(gl))
@ -146,24 +138,20 @@ const ReferenceWindow: VFC<ReferenceWindowProps> = ({maxHeight, maxWidth}) => {
const toggleVisibility = () => { const toggleVisibility = () => {
studio.transaction(({set}) => { studio.transaction(({set}) => {
set(getEditorSheetObject()!.props.viewport.showReferenceWindow, !visible) set(getEditorSheetObject()!.props.viewport.showReferenceWindow, !minified)
}) })
} }
// if (!visible) {
// return <Dot onClick={toggleVisibility} />
// }
return ( return (
<Container <Container
minified={!minified}
onClick={toggleVisibility} onClick={toggleVisibility}
className={`${visible ? 'visible' : 'hidden'}`}
style={{ style={{
width: (visible ? width : 12) + 'px', width: !minified ? 32 : preserveDrawingBuffer ? `${width}px` : 'auto',
height: (visible ? height : 12) + 'px', height: !minified ? 32 : preserveDrawingBuffer ? `${height}px` : 'auto',
}} }}
> >
{gl?.domElement && preserveDrawingBuffer ? ( {preserveDrawingBuffer ? (
<Canvas ref={canvasRef} width={width} height={height} /> <Canvas ref={canvasRef} width={width} height={height} />
) : ( ) : (
<Static> <Static>

View file

@ -194,8 +194,8 @@ const SnapshotEditor: React.FC<{paneId: string}> = (props) => {
<Tools ref={setToolsContainer} /> <Tools ref={setToolsContainer} />
<ReferenceWindowContainer> <ReferenceWindowContainer>
<ReferenceWindow <ReferenceWindow
maxHeight={Math.min(bounds.height * 0.3, 120)} maxHeight={Math.min(bounds.height * 0.3, 150)}
maxWidth={Math.min(bounds.width * 0.4, 200)} maxWidth={Math.min(bounds.width * 0.3, 250)}
/> />
</ReferenceWindowContainer> </ReferenceWindowContainer>
{!sceneSnapshot && ( {!sceneSnapshot && (