* Add roof surface placement support for items Items (e.g. solar panels) can now be placed on sloped roof surfaces. The placement system computes euler rotation from the roof surface normal so items sit flush on the slope instead of going inside. - Add roofStrategy to placement-strategies with enter/move/click/leave - Wire roof:enter/move/click/leave events in the placement coordinator - Add calculateRoofRotation in placement-math using surface normals - Support full 3D cursor rotation for sloped surfaces - Items on roofs are parented to the level with world-space rotation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fixed conflict * Fix spiral stair openings and fence handle arrows * Implement roof trim planes and ridge vent clipping * Fix mansard roof and ridge vent placement * Fix mansard merged roof cutouts * Fix Dutch roof gable overhang * Refactor roof segment, ridge vent, and surface geometry Remove Dutch ridge axis abstraction and rework roof edit system, ridge vent clipping geometry, and roof surface placement. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Simplify Dutch roof shape * Add Dutch roof gable top geometry controls * Fix Dutch roof slope material slots * Render dutch roof tops as double-sided faces * Add auto ridge vent toggle to roof segments Track ridge vent auto-generation via an `autoRidgeVent` metadata flag so geometry changes only regenerate default vents when enabled, treating legacy segments with generated vents as auto-enabled for back-compat. Expose a panel toggle to opt in/out per segment. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Snap new walls to the floor below Feed the walls of the level directly beneath the active one into the draft snap pipeline as extra references, so a new wall can align with the floor below. They share the same local XZ origin, and the list is kept separate from the current-level walls so the measurement HUD and wall splitting only act on the active level. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Set Dutch roof shape defaults on type switch Seed the Dutch shape parameters (waist width/height/length, top rake thickness/length) with sensible defaults whenever a segment is created as or switched to Dutch, so the gablet is well-formed regardless of leftover values from the previous roof type. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Use green accent for corner and endpoint snap markers Color the corner/endpoint snap markers and the vertical cursor pillar green across the 2D floorplan beacon, the 3D alignment guide dots, and the wall snap beacon so snap targets read as a consistent accent. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Add magnetic wall snapping to the roof tool Snap roof draft corners onto wall corners, midpoints, crossings, and bodies on the active level and the floor below, reusing the wall tool's snap pipeline so the beacon and coloring match. The cursor's ground dot/ring is hidden while a wall snap is active to avoid overlapping the beacon glyph. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Update auto-generated Next.js route types path Regenerated next-env.d.ts now references ./.next/dev/types/routes.d.ts. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Show cutaway outline while dragging roof trim Slice an untrimmed segment volume generated from the live node instead of the registry mesh, whose CSG rebuild lags a few frames behind the drag and may still hold placeholder geometry — so the section outline now renders deterministically. Use LineBasicNodeMaterial so the outline draws under the WebGPU pipeline, and export generateRoofSegmentGeometry for the slice source. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Fill and clip roof trim cutaway, gate it to active drag Add a violet silhouette fill behind the cutaway outline, extend the section slicing to angled diagonal/corner trims via a generic vertical cut plane, and clip each slice to its footprint span so the infinite plane no longer sprouts stray lines across the rest of the roof. The cutaway now renders only while a trim handle is being dragged. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Separate and extend Dutch roof end slopes Pull the Dutch hip end slopes out of the watertight shingle shell into their own slab wedge so they can be reshaped independently, and extend each end slope inward up its own hip plane until the top edge meets the gablet's inner triangle. Refactor roof-segment shape geometry into a shared roof-segment-shape module. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Render roof trim cutaway as a material-only section cut Replace the triangle-mesh slicer with a CSG intersection of a thin slab against the untrimmed roof shell, so the cutaway shows red only on real material (wall + deck bands) and leaves the hollow attic empty. Add an analytic surface-edge outline, style both solid red like a SketchUp section, and make the cutaway persist whenever a segment is trimmed. Keep the merged roof shell visible during trim editing (re-trimmed live from each segment's drag override) instead of swapping in the per-segment meshes, whose abutting end-cap faces showed as stray white planes the commit never had. Extend each slab past free cut-line ends only — trimmed ends clamp to the cut line — so the red section stays inside the trim box. Re-export INTERSECTION from the viewer CSG surface for the editor. * Outline roof cutaway by fill silhouette, restyle to destructive red Derive the section-cut outline from the fill geometry's edges (EdgesGeometry) so it traces the real cut shape — wall/deck band boundaries and the hollow-attic edge — instead of just the top surface line. Drop the fill to 85% opacity and recolor both fill and outline to the app's destructive red, matching the delete/destructive UI. * Include roof accessories in trim clipping and red cutaway Roof accessories (chimney, vents, skylight, dormer, gutter, downspout, solar-panel, cupola) now slice at the trim plane like the roof shell and appear in the red section-cut while dragging a trim handle: - Export clipGeometryBySegmentTrim from the viewer as a reusable segment-local trim-clip primitive. - Add a shared useSegmentTrimClippedGeometry hook + TrimClippedMesh wrapper (nodes) that slice accessory geometry by the host segment's live trim override, so the cut tracks the drag. - Wire the clip into all 11 accessory renderers, including skylight glass panes and dormer window glass/frame/sill. - Feed every hosted accessory mesh into the editor's red cutaway, welding triangle-soup geometry (e.g. ridge vent) so CSG INTERSECTION yields a cross-section. - Register skylight in the scene-graph tree-node map so it shows in the outliner when placed on a roof. Co-Authored-By: Claude <noreply@anthropic.com> * Add smooth spline fences with editable curve handles Fences can now be drawn as one continuous Catmull-Rom/Bezier curve via an optional `path` (+ per-point `tangents`), selectable in a Straight/Curved mode toggle. Selected spline fences expose draggable control-point dots (hexagon) and symmetric tangent handles (circle) joined by a violet line, editable in both 2D plan and 3D. Side-move arrows are dropped for splines. Co-Authored-By: Claude <noreply@anthropic.com> * Fix dutch roof ridge vent handling * Fix Dutch ridge vent placement and support * Fix Dutch roof trim artifacts * Fix Dutch roof trim preview geometry * Tag roof trim overlay meshes with EDITOR_LAYER Child meshes relied on a parent group's layer, which three.js does not propagate, so the trim section/rail/plane overlays rendered on the scene layer — getting inked/SSGI-darkened and leaking into thumbnail exports. Co-Authored-By: Claude <noreply@anthropic.com> * Apply Biome cleanup * fix(core): address Dutch roof review feedback * chore: apply biome check cleanup * fix(core): relax Dutch roof surface helper input * fix * Fix biome checks and dev verification * fixes * Remove unsupported Biome noShadow override * Improve roof interactions and fence editing * Fix fence drag and ridge vent default handling * editor: drop wall-snap debug log, gate curved-fence finish hint on draft start Remove the leftover TEMP DIAGNOSTIC console.log in the wall tool's onMove hot path. Curved fences commit on a closing gesture (double-click / Enter) rather than per-click, so surface a 'Finish curve' hint in the fence HUD — but only once a point has been placed and a curve is actually in flight. The draft point count is published from SplineFenceDraft into a small ephemeral editor store (useFenceCurveDraft) that the contextual helper reads, mirroring the existing useSegmentDraftChain pattern. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Wassim SAMAD <wass08@gmail.com>
566 lines
20 KiB
TypeScript
566 lines
20 KiB
TypeScript
import {
|
|
type AnyNodeId,
|
|
collectAlignmentAnchors,
|
|
emitter,
|
|
type FenceNode,
|
|
type GridEvent,
|
|
type LevelNode,
|
|
movingAlignmentAnchors,
|
|
nodeRegistry,
|
|
type RoofNode,
|
|
type RoofSegmentNode,
|
|
resolveAlignment,
|
|
type StairNode,
|
|
type StairSegmentNode,
|
|
sceneRegistry,
|
|
useLiveNodeOverrides,
|
|
useLiveTransforms,
|
|
useScene,
|
|
type WallNode,
|
|
} from '@pascal-app/core'
|
|
import {
|
|
CursorSphere,
|
|
commitFreshPlacementSubtree,
|
|
consumePlacementDragRelease,
|
|
DragBoundingBox,
|
|
getFloorStackPreviewPosition,
|
|
isMagneticSnapActive,
|
|
resolvePlanarCursorPosition,
|
|
snapFenceDraftPoint,
|
|
stripPlacementMetadataFlags,
|
|
triggerSFX,
|
|
useAlignmentGuides,
|
|
useEditor,
|
|
useFreshPlacementVisibility,
|
|
type WallPlanPoint,
|
|
} from '@pascal-app/editor'
|
|
import { useViewer } from '@pascal-app/viewer'
|
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
import * as THREE from 'three'
|
|
|
|
/** Figma-style alignment-snap threshold (meters), matching the other tools. */
|
|
const ALIGNMENT_THRESHOLD_M = 0.08
|
|
|
|
function disableRaycastDuringDrag(root: THREE.Object3D | undefined): () => void {
|
|
if (!root) return () => {}
|
|
|
|
const originals: Array<[THREE.Object3D, THREE.Object3D['raycast']]> = []
|
|
root.traverse((child) => {
|
|
originals.push([child, child.raycast])
|
|
child.raycast = () => {}
|
|
})
|
|
|
|
return () => {
|
|
for (const [child, raycast] of originals) {
|
|
child.raycast = raycast
|
|
}
|
|
}
|
|
}
|
|
|
|
function resolvePreviewRotationY(
|
|
node: RoofNode | RoofSegmentNode | StairNode | StairSegmentNode,
|
|
localRotation: number,
|
|
): number {
|
|
if ((node.type === 'roof-segment' || node.type === 'stair-segment') && node.parentId) {
|
|
const parentNode = useScene.getState().nodes[node.parentId as AnyNodeId]
|
|
const parentRotation =
|
|
parentNode && 'rotation' in parentNode && typeof parentNode.rotation === 'number'
|
|
? parentNode.rotation
|
|
: 0
|
|
return parentRotation + localRotation
|
|
}
|
|
|
|
return localRotation
|
|
}
|
|
|
|
export const MoveRoofTool: React.FC<{
|
|
node: RoofNode | RoofSegmentNode | StairNode | StairSegmentNode
|
|
}> = ({ node: movingNode }) => {
|
|
const {
|
|
isFreshPlacement,
|
|
previewVisible: cursorVisible,
|
|
revealFreshPlacement,
|
|
useAbsoluteCursorPlacement,
|
|
} = useFreshPlacementVisibility({
|
|
node: movingNode,
|
|
enabled: movingNode.type === 'roof' || movingNode.type === 'stair',
|
|
})
|
|
const exitMoveMode = useCallback(() => {
|
|
useEditor.getState().setMovingNode(null)
|
|
}, [])
|
|
|
|
const previousGridPosRef = useRef<[number, number] | null>(null)
|
|
const dragAnchorRef = useRef<[number, number] | null>(null)
|
|
|
|
const [previewRotation, setPreviewRotation] = useState<number>(() =>
|
|
resolvePreviewRotationY(movingNode, movingNode.rotation as number),
|
|
)
|
|
const [cursorWorldPos, setCursorWorldPos] = useState<[number, number, number]>(() => {
|
|
const obj = sceneRegistry.nodes.get(movingNode.id)
|
|
if (obj) {
|
|
const worldPos = obj.getWorldPosition(new THREE.Vector3())
|
|
// Cursor renders inside the building-local ToolManager group, so convert
|
|
// world → building-local to honor any building rotation.
|
|
const buildingId = useViewer.getState().selection.buildingId
|
|
const buildingObj = buildingId ? sceneRegistry.nodes.get(buildingId as AnyNodeId) : null
|
|
if (buildingObj) buildingObj.worldToLocal(worldPos)
|
|
return [worldPos.x, worldPos.y, worldPos.z]
|
|
}
|
|
// Fallback if not registered (e.g. newly created duplicate without mesh yet)
|
|
if (
|
|
(movingNode.type === 'roof-segment' || movingNode.type === 'stair-segment') &&
|
|
movingNode.parentId
|
|
) {
|
|
const parentNode = useScene.getState().nodes[movingNode.parentId as AnyNodeId]
|
|
if (parentNode && 'position' in parentNode && 'rotation' in parentNode) {
|
|
const parentAngle = parentNode.rotation as number
|
|
const px = parentNode.position[0] as number
|
|
const py = parentNode.position[1] as number
|
|
const pz = parentNode.position[2] as number
|
|
const lx = movingNode.position[0]
|
|
const ly = movingNode.position[1]
|
|
const lz = movingNode.position[2]
|
|
|
|
const wx = lx * Math.cos(parentAngle) - lz * Math.sin(parentAngle) + px
|
|
const wz = lx * Math.sin(parentAngle) + lz * Math.cos(parentAngle) + pz
|
|
return [wx, py + ly, wz]
|
|
}
|
|
}
|
|
return [movingNode.position[0], movingNode.position[1], movingNode.position[2]]
|
|
})
|
|
|
|
useEffect(() => {
|
|
useScene.temporal.getState().pause()
|
|
dragAnchorRef.current = null
|
|
previousGridPosRef.current = null
|
|
|
|
const isNew = isFreshPlacement
|
|
const committedMeta = stripPlacementMetadataFlags(movingNode.metadata) as RoofNode['metadata']
|
|
|
|
const original = {
|
|
position: [...movingNode.position] as [number, number, number],
|
|
rotation: movingNode.rotation,
|
|
parentId: movingNode.parentId,
|
|
metadata: movingNode.metadata,
|
|
}
|
|
|
|
// Track whether the move was committed so cleanup knows whether to revert.
|
|
// We avoid setting isTransient on the store to prevent RoofSystem from
|
|
// resetting the mesh position (it resets on dirty) and from triggering
|
|
// expensive merged-mesh CSG rebuilds on every frame.
|
|
let wasCommitted = false
|
|
let wasCancelled = false
|
|
let hasMoved = false
|
|
|
|
// Track pending rotation — no store updates during drag
|
|
let pendingRotation: number = movingNode.rotation as number
|
|
let lastLocalPosition: [number, number, number] = [
|
|
movingNode.position[0],
|
|
movingNode.position[1],
|
|
movingNode.position[2],
|
|
]
|
|
const movingObject = sceneRegistry.nodes.get(movingNode.id)
|
|
const restoreRaycasts = disableRaycastDuringDrag(movingObject)
|
|
|
|
const syncHostedPreview = (
|
|
patch: Pick<RoofSegmentNode | StairSegmentNode, 'position' | 'rotation'>,
|
|
) => {
|
|
if (movingNode.type !== 'roof-segment' && movingNode.type !== 'stair-segment') return
|
|
useLiveNodeOverrides.getState().set(movingNode.id, patch as Record<string, unknown>)
|
|
}
|
|
|
|
const clearHostedPreview = () => {
|
|
if (movingNode.type !== 'roof-segment' && movingNode.type !== 'stair-segment') return
|
|
useLiveNodeOverrides.getState().clear(movingNode.id)
|
|
}
|
|
|
|
const resolveLevelId = () => {
|
|
if (movingNode.type === 'roof' || movingNode.type === 'stair') {
|
|
return movingNode.parentId ?? null
|
|
}
|
|
|
|
if (
|
|
(movingNode.type === 'roof-segment' || movingNode.type === 'stair-segment') &&
|
|
movingNode.parentId
|
|
) {
|
|
const parentNode = useScene.getState().nodes[movingNode.parentId as AnyNodeId]
|
|
return parentNode && 'parentId' in parentNode ? (parentNode.parentId ?? null) : null
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
const levelId = resolveLevelId()
|
|
const isFloorPlaced = nodeRegistry.get(movingNode.type)?.capabilities?.floorPlaced !== undefined
|
|
const getPreviewPosition = (
|
|
position: [number, number, number],
|
|
rotation = pendingRotation,
|
|
): [number, number, number] => {
|
|
if (!isFloorPlaced) return position
|
|
return getFloorStackPreviewPosition({
|
|
node: movingNode,
|
|
position,
|
|
rotation,
|
|
levelId,
|
|
nodes: useScene.getState().nodes,
|
|
})
|
|
}
|
|
const levelNode =
|
|
levelId && useScene.getState().nodes[levelId as AnyNodeId]?.type === 'level'
|
|
? (useScene.getState().nodes[levelId as AnyNodeId] as LevelNode)
|
|
: null
|
|
const levelChildren = levelNode?.children ?? []
|
|
const levelWalls = levelChildren
|
|
.map((childId) => useScene.getState().nodes[childId as AnyNodeId])
|
|
.filter((node): node is WallNode => node?.type === 'wall')
|
|
const levelFences = levelChildren
|
|
.map((childId) => useScene.getState().nodes[childId as AnyNodeId])
|
|
.filter((node): node is FenceNode => node?.type === 'fence')
|
|
const buildingId = useViewer.getState().selection.buildingId
|
|
const buildingObj = buildingId ? sceneRegistry.nodes.get(buildingId as AnyNodeId) : null
|
|
|
|
// Alignment for top-level stair / roof only. Segments live in parent-local
|
|
// space (a different frame from the building-local candidate pool / guide
|
|
// layer), so we leave them on the plain grid+corner snap. Both stair and
|
|
// roof align by their footprint bounding-box corners.
|
|
const alignTopLevel = movingNode.type === 'stair' || movingNode.type === 'roof'
|
|
const alignmentCandidates = alignTopLevel
|
|
? collectAlignmentAnchors(
|
|
useScene.getState().nodes,
|
|
movingNode.id,
|
|
movingNode.type === 'stair' ? levelId : undefined,
|
|
)
|
|
: []
|
|
const alignLocalPoint = (lx: number, lz: number, bypass: boolean): [number, number] => {
|
|
if (!alignTopLevel || bypass || alignmentCandidates.length === 0) {
|
|
useAlignmentGuides.getState().clear()
|
|
return [lx, lz]
|
|
}
|
|
const moving =
|
|
movingNode.type === 'stair' || movingNode.type === 'roof'
|
|
? movingAlignmentAnchors(movingNode, useScene.getState().nodes, lx, lz, pendingRotation)
|
|
: []
|
|
const ar = resolveAlignment({
|
|
moving:
|
|
moving.length > 0 ? moving : [{ nodeId: movingNode.id, kind: 'corner', x: lx, z: lz }],
|
|
candidates: alignmentCandidates,
|
|
threshold: ALIGNMENT_THRESHOLD_M,
|
|
})
|
|
useAlignmentGuides.getState().set(ar.guides)
|
|
return ar.snap ? [lx + ar.snap.dx, lz + ar.snap.dz] : [lx, lz]
|
|
}
|
|
|
|
const localToWorldPoint = (localPoint: WallPlanPoint, y: number): [number, number, number] => {
|
|
if (buildingObj) {
|
|
const worldPoint = buildingObj.localToWorld(
|
|
new THREE.Vector3(localPoint[0], y, localPoint[1]),
|
|
)
|
|
return [worldPoint.x, worldPoint.y, worldPoint.z]
|
|
}
|
|
|
|
return [localPoint[0], y, localPoint[1]]
|
|
}
|
|
|
|
const computeLocal = (
|
|
gridX: number,
|
|
gridZ: number,
|
|
y: number,
|
|
buildingLocalX: number,
|
|
buildingLocalZ: number,
|
|
): [number, number] => {
|
|
// Segments have a transformed parent (stair/roof). Convert world → parent-local
|
|
// via Three.js hierarchy so the segment's stored position stays parent-relative.
|
|
if (
|
|
(movingNode.type === 'roof-segment' || movingNode.type === 'stair-segment') &&
|
|
movingNode.parentId
|
|
) {
|
|
const parentNode = useScene.getState().nodes[movingNode.parentId as AnyNodeId]
|
|
if (parentNode && 'position' in parentNode && 'rotation' in parentNode) {
|
|
const parentObj = sceneRegistry.nodes.get(movingNode.parentId)
|
|
if (parentObj) {
|
|
const worldVec = new THREE.Vector3(gridX, y, gridZ)
|
|
parentObj.worldToLocal(worldVec)
|
|
return [worldVec.x, worldVec.z]
|
|
}
|
|
const dx = gridX - (parentNode.position[0] as number)
|
|
const dz = gridZ - (parentNode.position[2] as number)
|
|
const angle = -(parentNode.rotation as number)
|
|
return [
|
|
dx * Math.cos(angle) - dz * Math.sin(angle),
|
|
dx * Math.sin(angle) + dz * Math.cos(angle),
|
|
]
|
|
}
|
|
}
|
|
|
|
// Stair/roof live directly in the level — their stored position is building-local.
|
|
// event.localPosition is already building-local, so using it handles building rotation.
|
|
return [buildingLocalX, buildingLocalZ]
|
|
}
|
|
|
|
const localPositionToToolLocal = (
|
|
position: [number, number, number],
|
|
): [number, number, number] => {
|
|
if (
|
|
(movingNode.type === 'roof-segment' || movingNode.type === 'stair-segment') &&
|
|
movingNode.parentId
|
|
) {
|
|
const parentObj = sceneRegistry.nodes.get(movingNode.parentId)
|
|
if (parentObj) {
|
|
const point = parentObj.localToWorld(new THREE.Vector3(...position))
|
|
if (buildingObj) buildingObj.worldToLocal(point)
|
|
return [point.x, point.y, point.z]
|
|
}
|
|
}
|
|
|
|
return position
|
|
}
|
|
|
|
const onGridMove = (event: GridEvent) => {
|
|
hasMoved = true
|
|
revealFreshPlacement()
|
|
|
|
const y = event.position[1]
|
|
|
|
const roofBypassSnap = event.nativeEvent?.shiftKey === true
|
|
const snappedLocal = snapFenceDraftPoint({
|
|
point: [event.localPosition[0], event.localPosition[2]],
|
|
walls: levelWalls,
|
|
fences: levelFences,
|
|
bypassSnap: roofBypassSnap,
|
|
magnetic: !roofBypassSnap && isMagneticSnapActive(),
|
|
})
|
|
const [rawGridX, , rawGridZ] = localToWorldPoint(snappedLocal, y)
|
|
const [rawLocalX, rawLocalZ] = computeLocal(
|
|
rawGridX,
|
|
rawGridZ,
|
|
y,
|
|
snappedLocal[0],
|
|
snappedLocal[1],
|
|
)
|
|
const resolved = resolvePlanarCursorPosition({
|
|
cursor: [rawLocalX, rawLocalZ],
|
|
original: [movingNode.position[0], movingNode.position[2]],
|
|
anchor: dragAnchorRef.current,
|
|
mode: useAbsoluteCursorPlacement ? 'absolute' : 'relative',
|
|
})
|
|
dragAnchorRef.current = resolved.anchor
|
|
let [localX, localZ] = resolved.point
|
|
|
|
if (alignTopLevel) {
|
|
const aligned = alignLocalPoint(
|
|
localX,
|
|
localZ,
|
|
event.nativeEvent?.altKey === true || event.nativeEvent?.shiftKey === true,
|
|
)
|
|
localX = aligned[0]
|
|
localZ = aligned[1]
|
|
}
|
|
|
|
if (
|
|
event.nativeEvent?.shiftKey !== true &&
|
|
previousGridPosRef.current &&
|
|
(localX !== previousGridPosRef.current[0] || localZ !== previousGridPosRef.current[1])
|
|
) {
|
|
triggerSFX('sfx:grid-snap')
|
|
}
|
|
|
|
previousGridPosRef.current = [localX, localZ]
|
|
|
|
lastLocalPosition = [localX, movingNode.position[1], localZ]
|
|
const previewPosition = getPreviewPosition(lastLocalPosition)
|
|
setCursorWorldPos(
|
|
isFloorPlaced ? previewPosition : localPositionToToolLocal(lastLocalPosition),
|
|
)
|
|
|
|
// Directly update the Three.js mesh — no store update during drag
|
|
const mesh = sceneRegistry.nodes.get(movingNode.id)
|
|
if (mesh) {
|
|
if (isFloorPlaced) {
|
|
mesh.position.set(...previewPosition)
|
|
} else {
|
|
mesh.position.x = localX
|
|
mesh.position.z = localZ
|
|
}
|
|
}
|
|
|
|
// Publish canonical position so the 2D floorplan can track the drag.
|
|
// Floor-placed parents (stairs) stay in their committed local frame;
|
|
// the lifted Y remains presentation-only in the 3D view.
|
|
useLiveTransforms.getState().set(movingNode.id, {
|
|
position: lastLocalPosition,
|
|
rotation: pendingRotation,
|
|
})
|
|
syncHostedPreview({
|
|
position: lastLocalPosition,
|
|
rotation: pendingRotation,
|
|
})
|
|
}
|
|
|
|
const onGridClick = (event: GridEvent) => {
|
|
if (wasCommitted) return
|
|
if (!hasMoved) return
|
|
const [localX, , localZ] = lastLocalPosition
|
|
|
|
useAlignmentGuides.getState().clear()
|
|
wasCommitted = true
|
|
|
|
let committedId = movingNode.id as AnyNodeId
|
|
if (isNew) {
|
|
committedId =
|
|
commitFreshPlacementSubtree(movingNode.id as AnyNodeId, {
|
|
position: [localX, movingNode.position[1], localZ],
|
|
rotation: pendingRotation,
|
|
metadata: committedMeta,
|
|
visible: true,
|
|
}) ?? committedId
|
|
} else {
|
|
// The store still holds the original values (we didn't update during drag).
|
|
// Resume temporal and apply the final state as a single undoable step.
|
|
useScene.temporal.getState().resume()
|
|
useScene.getState().updateNode(movingNode.id, {
|
|
position: [localX, movingNode.position[1], localZ],
|
|
rotation: pendingRotation,
|
|
metadata: committedMeta,
|
|
})
|
|
useScene.temporal.getState().pause()
|
|
}
|
|
|
|
triggerSFX('sfx:item-place')
|
|
useViewer.getState().setSelection({ selectedIds: [committedId] })
|
|
clearHostedPreview()
|
|
useLiveTransforms.getState().clear(movingNode.id)
|
|
useEditor.getState().setMovingNodeOrigin('3d')
|
|
exitMoveMode()
|
|
event.nativeEvent?.stopPropagation?.()
|
|
}
|
|
|
|
const onPlacementDragPointerUp = (event: PointerEvent) => {
|
|
if (!consumePlacementDragRelease(event)) return
|
|
onGridClick({ nativeEvent: event } as unknown as GridEvent)
|
|
}
|
|
|
|
const onCancel = () => {
|
|
wasCancelled = true
|
|
clearHostedPreview()
|
|
useLiveTransforms.getState().clear(movingNode.id)
|
|
useAlignmentGuides.getState().clear()
|
|
if (isNew) {
|
|
useScene.getState().deleteNode(movingNode.id)
|
|
} else {
|
|
useScene.getState().updateNode(movingNode.id, {
|
|
position: original.position,
|
|
rotation: original.rotation,
|
|
metadata: original.metadata,
|
|
})
|
|
}
|
|
useScene.temporal.getState().resume()
|
|
exitMoveMode()
|
|
}
|
|
|
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
if (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement) {
|
|
return
|
|
}
|
|
|
|
const ROTATION_STEP = Math.PI / 4
|
|
let rotationDelta = 0
|
|
if (event.key === 'r' || event.key === 'R') rotationDelta = ROTATION_STEP
|
|
else if (event.key === 't' || event.key === 'T') rotationDelta = -ROTATION_STEP
|
|
|
|
if (rotationDelta !== 0) {
|
|
event.preventDefault()
|
|
triggerSFX('sfx:item-rotate')
|
|
|
|
pendingRotation += rotationDelta
|
|
setPreviewRotation(resolvePreviewRotationY(movingNode, pendingRotation))
|
|
|
|
// Directly update the Three.js mesh — no store update during drag
|
|
const mesh = sceneRegistry.nodes.get(movingNode.id)
|
|
if (mesh) {
|
|
mesh.rotation.y = pendingRotation
|
|
if (isFloorPlaced) {
|
|
const previewPosition = getPreviewPosition(lastLocalPosition, pendingRotation)
|
|
mesh.position.set(...previewPosition)
|
|
setCursorWorldPos(previewPosition)
|
|
}
|
|
}
|
|
|
|
// Update live transform rotation for 2D floorplan
|
|
const currentLive = useLiveTransforms.getState().get(movingNode.id)
|
|
if (currentLive) {
|
|
useLiveTransforms.getState().set(movingNode.id, {
|
|
...currentLive,
|
|
rotation: pendingRotation,
|
|
})
|
|
}
|
|
syncHostedPreview({
|
|
position: lastLocalPosition,
|
|
rotation: pendingRotation,
|
|
})
|
|
}
|
|
}
|
|
|
|
emitter.on('grid:move', onGridMove)
|
|
emitter.on('grid:click', onGridClick)
|
|
emitter.on('tool:cancel', onCancel)
|
|
window.addEventListener('keydown', onKeyDown)
|
|
window.addEventListener('pointerup', onPlacementDragPointerUp)
|
|
|
|
return () => {
|
|
restoreRaycasts()
|
|
|
|
// Clear ephemeral live transform + any alignment guides
|
|
clearHostedPreview()
|
|
useLiveTransforms.getState().clear(movingNode.id)
|
|
useAlignmentGuides.getState().clear()
|
|
|
|
// Skip restore when the 2D floor-plan overlay claimed teardown
|
|
// ownership — same contract `FloorplanRegistryMoveOverlay` uses to
|
|
// decide whether to revert its own apply() writes. Without this,
|
|
// a stair / roof move committed in the floor plan unmounts this
|
|
// tool with `wasCommitted === false` (this tool's own grid-click
|
|
// never fired), and the restore below stomps the just-committed
|
|
// position back to the snapshot.
|
|
const finalisedBy2D = useEditor.getState().movingNodeOrigin === '2d'
|
|
|
|
if (!(wasCommitted || wasCancelled || isNew || finalisedBy2D)) {
|
|
useScene.getState().updateNode(movingNode.id, {
|
|
position: original.position,
|
|
rotation: original.rotation,
|
|
metadata: original.metadata,
|
|
})
|
|
}
|
|
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('pointerup', onPlacementDragPointerUp)
|
|
}
|
|
}, [movingNode, exitMoveMode, isFreshPlacement, revealFreshPlacement, useAbsoluteCursorPlacement])
|
|
|
|
// Show the same green drag box for both top-level roofs/stairs and their
|
|
// segments. Segment cursor positions are converted into the tool's
|
|
// building-local frame above, so the box can now ride the cursor correctly.
|
|
const showBoundingBox =
|
|
movingNode.type === 'stair' ||
|
|
movingNode.type === 'roof' ||
|
|
movingNode.type === 'roof-segment' ||
|
|
movingNode.type === 'stair-segment'
|
|
|
|
return (
|
|
<group visible={cursorVisible}>
|
|
<CursorSphere position={cursorWorldPos} showTooltip={false} />
|
|
{showBoundingBox && (
|
|
<DragBoundingBox
|
|
nodeId={movingNode.id}
|
|
position={cursorWorldPos}
|
|
rotationY={previewRotation}
|
|
/>
|
|
)}
|
|
</group>
|
|
)
|
|
}
|
|
|
|
export default MoveRoofTool
|