* feat: add polygon movement support to editor and implement dedicated move tools for walls, slabs, and ceilings * feat: add uv2 attribute to ceiling and slab geometries for lightmap support * Fix curved wall miters and align roof position sliders * Fix wall measurement label and extension anchors * Fix WebGPU renderer initialization order * fix(editor): scope wall movement to the selected level --------- Co-authored-by: Pascal <open@pascal.app>
158 lines
4.9 KiB
TypeScript
158 lines
4.9 KiB
TypeScript
'use client'
|
|
|
|
import {
|
|
type AnyNodeId,
|
|
emitter,
|
|
type GridEvent,
|
|
getClampedWallCurveOffset,
|
|
getMaxWallCurveOffset,
|
|
getWallChordFrame,
|
|
getWallMidpointHandlePoint,
|
|
normalizeWallCurveOffset,
|
|
useScene,
|
|
type WallNode,
|
|
} from '@pascal-app/core'
|
|
import { useViewer } from '@pascal-app/viewer'
|
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
import { markToolCancelConsumed } from '../../../hooks/use-keyboard'
|
|
import { sfxEmitter } from '../../../lib/sfx-bus'
|
|
import useEditor from '../../../store/use-editor'
|
|
import { CursorSphere } from '../shared/cursor-sphere'
|
|
|
|
function snap(value: number) {
|
|
return Math.round(value * 2) / 2
|
|
}
|
|
|
|
export const CurveWallTool: React.FC<{ node: WallNode }> = ({ node }) => {
|
|
const activatedAtRef = useRef<number>(Date.now())
|
|
const originalCurveOffsetRef = useRef(getClampedWallCurveOffset(node))
|
|
const previousCurveOffsetRef = useRef<number | null>(null)
|
|
const shiftPressedRef = useRef(false)
|
|
const previewOffsetRef = useRef<number>(originalCurveOffsetRef.current)
|
|
|
|
const initialHandle = getWallMidpointHandlePoint(node)
|
|
const [cursorLocalPos, setCursorLocalPos] = useState<[number, number, number]>([
|
|
initialHandle.x,
|
|
0,
|
|
initialHandle.y,
|
|
])
|
|
|
|
const exitCurveMode = useCallback(() => {
|
|
useEditor.getState().setCurvingWall(null)
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
const nodeId = node.id
|
|
const originalCurveOffset = originalCurveOffsetRef.current
|
|
const chord = getWallChordFrame(node)
|
|
const maxCurveOffset = getMaxWallCurveOffset(node)
|
|
|
|
useScene.temporal.getState().pause()
|
|
let wasCommitted = false
|
|
|
|
const applyPreview = (curveOffset: number) => {
|
|
previewOffsetRef.current = curveOffset
|
|
|
|
const nextNode = {
|
|
...node,
|
|
curveOffset,
|
|
}
|
|
const handlePoint = getWallMidpointHandlePoint(nextNode)
|
|
setCursorLocalPos([handlePoint.x, 0, handlePoint.y])
|
|
useScene.getState().updateNode(nodeId, { curveOffset })
|
|
useScene.getState().markDirty(nodeId as AnyNodeId)
|
|
}
|
|
|
|
const restoreOriginal = () => {
|
|
useScene.getState().updateNode(nodeId, { curveOffset: originalCurveOffset })
|
|
useScene.getState().markDirty(nodeId as AnyNodeId)
|
|
}
|
|
|
|
const onGridMove = (event: GridEvent) => {
|
|
const localX = shiftPressedRef.current ? event.localPosition[0] : snap(event.localPosition[0])
|
|
const localZ = shiftPressedRef.current ? event.localPosition[2] : snap(event.localPosition[2])
|
|
|
|
const offsetFromMidpoint =
|
|
-(
|
|
(localX - chord.midpoint.x) * chord.normal.x +
|
|
(localZ - chord.midpoint.y) * chord.normal.y
|
|
)
|
|
const snappedOffset = shiftPressedRef.current ? offsetFromMidpoint : snap(offsetFromMidpoint)
|
|
const nextCurveOffset = normalizeWallCurveOffset(node, Math.max(-maxCurveOffset, Math.min(maxCurveOffset, snappedOffset)))
|
|
|
|
if (
|
|
previousCurveOffsetRef.current !== null &&
|
|
nextCurveOffset !== previousCurveOffsetRef.current
|
|
) {
|
|
sfxEmitter.emit('sfx:grid-snap')
|
|
}
|
|
previousCurveOffsetRef.current = nextCurveOffset
|
|
|
|
applyPreview(nextCurveOffset)
|
|
}
|
|
|
|
const onGridClick = (event: GridEvent) => {
|
|
if (Date.now() - activatedAtRef.current < 150) {
|
|
event.nativeEvent?.stopPropagation?.()
|
|
return
|
|
}
|
|
|
|
const curveOffset = previewOffsetRef.current
|
|
wasCommitted = true
|
|
useScene.temporal.getState().resume()
|
|
useScene.getState().updateNode(nodeId, { curveOffset })
|
|
useScene.getState().markDirty(nodeId as AnyNodeId)
|
|
useScene.temporal.getState().pause()
|
|
|
|
sfxEmitter.emit('sfx:item-place')
|
|
useViewer.getState().setSelection({ selectedIds: [nodeId] })
|
|
exitCurveMode()
|
|
event.nativeEvent?.stopPropagation?.()
|
|
}
|
|
|
|
const onCancel = () => {
|
|
restoreOriginal()
|
|
useViewer.getState().setSelection({ selectedIds: [nodeId] })
|
|
useScene.temporal.getState().resume()
|
|
markToolCancelConsumed()
|
|
exitCurveMode()
|
|
}
|
|
|
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
if (event.key === 'Shift') {
|
|
shiftPressedRef.current = true
|
|
}
|
|
}
|
|
|
|
const onKeyUp = (event: KeyboardEvent) => {
|
|
if (event.key === 'Shift') {
|
|
shiftPressedRef.current = false
|
|
}
|
|
}
|
|
|
|
emitter.on('grid:move', onGridMove)
|
|
emitter.on('grid:click', onGridClick)
|
|
emitter.on('tool:cancel', onCancel)
|
|
window.addEventListener('keydown', onKeyDown)
|
|
window.addEventListener('keyup', onKeyUp)
|
|
|
|
return () => {
|
|
if (!wasCommitted) {
|
|
restoreOriginal()
|
|
}
|
|
useScene.temporal.getState().resume()
|
|
emitter.off('grid:move', onGridMove)
|
|
emitter.off('grid:click', onGridClick)
|
|
emitter.off('tool:cancel', onCancel)
|
|
window.removeEventListener('keydown', onKeyDown)
|
|
window.removeEventListener('keyup', onKeyUp)
|
|
}
|
|
}, [exitCurveMode, node])
|
|
|
|
return (
|
|
<group>
|
|
<CursorSphere position={cursorLocalPos} showTooltip={false} />
|
|
</group>
|
|
)
|
|
}
|