Phase 5 Stage E: full kind migration into packages/nodes
Wholesale move of every remaining kind into its own subdirectory under
`packages/nodes/src/`, finishing the registry-driven migration. Each
kind now ships its definition, schema (re-exported from core), and any
of `geometry` / `renderer` / `system` / `floorplan` / `tool` /
`move-tool` / `panel` / `floorplan-move` / `floorplan-affordances` /
`parametrics` / `preview` it needs — no per-kind code remains under
`packages/editor/src/components/tools/` or
`packages/viewer/src/components/renderers/`.
Deleted (replaced by registry-driven equivalents):
- `tools/{ceiling,column,door,fence,item,slab,spawn,wall,window}/...`
(boundary editors, hole editors, placement tools, move tools,
endpoint movers, curve tools, helpers, math libs)
- `ui/helpers/{ceiling,slab,wall}-helper.tsx`
- `ui/panels/{column,door,elevator,item,roof,roof-segment,spawn,
stair,stair-segment,wall,window}-panel.tsx`
- `viewer/src/components/renderers/{building,ceiling,column,door,
elevator,fence,guide,item,level,roof,roof-segment,scan,site,slab,
spawn,stair,stair-segment,wall,window,zone}-renderer.tsx`
- `viewer/src/components/viewer/legacy-system.tsx`
Added under `packages/nodes/src/`:
- `building/`, `column/`, `elevator/`, `guide/`, `level/`, `roof/`,
`roof-segment/`, `scan/`, `shared/`, `site/`, `stair/`,
`stair-segment/` packages with definition + schema + renderer / system
/ floorplan / panel as appropriate.
- New `floorplan-move.ts` for every kind that supports 2D moves
(ceiling, door, item, shelf, slab, window) — single registry-driven
dispatch path via `def.floorplanMoveTarget`.
- New `floorplan-affordances.ts` for kinds with polygon / endpoint
drags (ceiling, fence, slab, wall) — using the shared
`polygon-vertex-affordance` factories.
- New per-kind `panel.tsx` for kinds with custom inspector content
(door, item, shelf, spawn, wall, window).
- New per-kind `tool.tsx` for placement (door, item, shelf, window).
- New per-kind `move-tool.tsx` for kinds with custom 3D move flows
(door, item, slab, window).
Coordinator + manager updates in `packages/editor/`:
- `tool-manager.tsx` resolves tools from the registry only — no
hardcoded type→component map.
- `panel-manager.tsx` resolves inspector panels the same way.
- `placement-{coordinator,strategies,types}.ts` extended with
shelf-surface placement.
- `selection-manager.tsx` adds the registry-selectable fallback.
- `floorplan-panel.tsx`, `floorplan-background-placement.ts`,
`floorplan-render-context.tsx` updated for the registry layer's new
contract (props, affordance dispatch, render context).
Viewer updates:
- `viewer/index.tsx` drops legacy renderer mounts.
- `node-renderer.tsx` resolves by registry only.
- `scene-bvh.tsx`, `use-node-events.ts`, `level-system.tsx`,
`wall-cutout.tsx`, `zone-system.tsx`, `materials.ts` adjusted for
the registry-only world.
Sidebar tree nodes for ceiling / fence / slab / shelf / tree-node
updated to read from the registered nodes instead of the deleted
legacy renderer trees.
Wiki: new `plugin-authoring.md` page, README index updated.
Tests in `packages/nodes/src/index.test.ts` validate every registered
kind has the required shape.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
11015ea1ed
commit
d747d2f0ea
@@ -1,5 +1,11 @@
|
||||
import type { NodeDefinition } from '@pascal-app/core'
|
||||
import { buildSlabFloorplan } from './floorplan'
|
||||
import {
|
||||
slabAddVertexAffordance,
|
||||
slabMoveEdgeAffordance,
|
||||
slabMoveVertexAffordance,
|
||||
} from './floorplan-affordances'
|
||||
import { slabFloorplanMoveTarget } from './floorplan-move'
|
||||
import { buildSlabGeometry } from './geometry'
|
||||
import { slabParametrics } from './parametrics'
|
||||
import { SlabNode } from './schema'
|
||||
@@ -73,6 +79,19 @@ export const slabDefinition: NodeDefinition<typeof SlabNode> = {
|
||||
// Stage C: floor-plan rendering. Legacy `slabPolygons` short-circuits
|
||||
// to [] when slab is registered (see floorplan-panel.tsx).
|
||||
floorplan: buildSlabFloorplan,
|
||||
// 2D move handler — translates polygon by cursor delta from first
|
||||
// pointer position. The 3D `MoveSlabTool` in `affordanceTools.move`
|
||||
// skips events sourced from the 2D scene so the two paths don't
|
||||
// double-write on commit.
|
||||
floorplanMoveTarget: slabFloorplanMoveTarget,
|
||||
// Sister to `affordanceTools['boundary-edit']` (the 3D `PolygonEditor`
|
||||
// wrapper). The 2D version edits the same `polygon` field via SVG
|
||||
// pointer events on the vertex handles emitted by `def.floorplan`.
|
||||
floorplanAffordances: {
|
||||
'move-vertex': slabMoveVertexAffordance,
|
||||
'add-vertex': slabAddVertexAffordance,
|
||||
'move-edge': slabMoveEdgeAffordance,
|
||||
},
|
||||
|
||||
toolHints: [
|
||||
{ key: 'Left click', label: 'Trace slab outline' },
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { SlabNode } from '@pascal-app/core'
|
||||
import {
|
||||
createPolygonAddVertexAffordance,
|
||||
createPolygonMoveEdgeAffordance,
|
||||
createPolygonVertexAffordance,
|
||||
} from '../shared/polygon-vertex-affordance'
|
||||
|
||||
/**
|
||||
* 2D drag affordances for slab. Three operations, each accepting an
|
||||
* optional `holeIndex` in the payload so they target the boundary
|
||||
* polygon or a specific hole:
|
||||
*
|
||||
* - `move-vertex` — drag an existing vertex.
|
||||
* - `add-vertex` — insert a new vertex at a midpoint then drag.
|
||||
* - `move-edge` — drag a whole edge perpendicular to itself.
|
||||
*
|
||||
* Holes are surfaced inline alongside the boundary in `def.floorplan`
|
||||
* (no separate "hole edit mode" state machine like the legacy) — when
|
||||
* the slab is selected, every hole's handles appear at the same time.
|
||||
* Simpler model, no UX downside in practice.
|
||||
*/
|
||||
export const slabMoveVertexAffordance = createPolygonVertexAffordance<SlabNode>('slab')
|
||||
export const slabAddVertexAffordance = createPolygonAddVertexAffordance<SlabNode>('slab')
|
||||
export const slabMoveEdgeAffordance = createPolygonMoveEdgeAffordance<SlabNode>('slab')
|
||||
@@ -0,0 +1,131 @@
|
||||
import {
|
||||
type AnyNodeId,
|
||||
type FloorplanMoveTarget,
|
||||
type FloorplanMoveTargetSession,
|
||||
type SlabNode,
|
||||
sceneRegistry,
|
||||
useLiveTransforms,
|
||||
useScene,
|
||||
} from '@pascal-app/core'
|
||||
import { snapPointToGrid, type WallPlanPoint } from '@pascal-app/editor'
|
||||
import type * as THREE from 'three'
|
||||
|
||||
/**
|
||||
* 2D floor-plan move handler for slab — mirrors the 3D `MoveSlabTool`
|
||||
* live-drag pattern so the visual stays smooth in split view.
|
||||
*
|
||||
* **Why not write the polygon every tick?** Per-tick `scene.update` on
|
||||
* `polygon` triggers a CSG geometry rebuild in `GeometrySystem` every
|
||||
* frame. Even with a synchronous `markDirty`, the rebuild dispose/add
|
||||
* pair flickers in the 3D viewer and the slab visibly catches up to
|
||||
* the cursor one frame late — the same regression `commit f4ea07e` was
|
||||
* fixed for in the 3D mover. The fix there: don't touch `scene` during
|
||||
* the drag at all. Translate the rendered `<group>` via the live-drag
|
||||
* exception (`mesh.position` + `useLiveTransforms.position = delta`).
|
||||
* On commit, write the polygon once.
|
||||
*
|
||||
* **Delta semantics** (see `wiki/architecture/tools.md` — "useLiveTransforms
|
||||
* contract is per-kind, not generic"): polygon-based kinds carry their
|
||||
* "position" in the polygon vertices, not a node.position field. The
|
||||
* `useLiveTransforms.position` must be a translation **delta**
|
||||
* (`[Δx, 0, Δz]`), which `ParametricNodeRenderer` consumes as the group
|
||||
* position. Visual = group.position + group.children-in-original-coords
|
||||
* = (delta) + (original polygon vertices) = translated, with no
|
||||
* geometry rebuild.
|
||||
*
|
||||
* **Commit path**: `canCommit` is the only side-effectful write to
|
||||
* `scene`. The dispatcher captured snapshots before the first apply,
|
||||
* so its snapshot-diff after `canCommit` returns will see one update
|
||||
* (the translated polygon) and run the single-undo dance against it.
|
||||
* `MoveSlabTool`'s cleanup (fires when `setMovingNode(null)` runs after
|
||||
* the commit) handles the `useLiveTransforms.clear` + the React-render
|
||||
* that resets `group.position` to (0,0,0) — by then `GeometrySystem`
|
||||
* has rebuilt with the new polygon, so the visual lands at the same
|
||||
* world position with no teleport.
|
||||
*/
|
||||
const GRID_STEP = 0.5
|
||||
|
||||
function translatePolygon(
|
||||
polygon: ReadonlyArray<readonly [number, number]>,
|
||||
dx: number,
|
||||
dz: number,
|
||||
): Array<[number, number]> {
|
||||
return polygon.map(([x, z]) => [x + dx, z + dz] as [number, number])
|
||||
}
|
||||
|
||||
export const slabFloorplanMoveTarget: FloorplanMoveTarget<SlabNode> = ({ node }) => {
|
||||
const slabId = node.id as AnyNodeId
|
||||
const originalPolygon = node.polygon.map(([x, z]) => [x, z] as [number, number])
|
||||
const originalHoles = (node.holes ?? []).map((hole) =>
|
||||
hole.map(([x, z]) => [x, z] as [number, number]),
|
||||
)
|
||||
let anchor: [number, number] | null = null
|
||||
let lastDelta: [number, number] = [0, 0]
|
||||
|
||||
const session: FloorplanMoveTargetSession = {
|
||||
affectedIds: [slabId],
|
||||
apply({ planPoint, modifiers }) {
|
||||
const snapped: WallPlanPoint = modifiers.shiftKey
|
||||
? ([planPoint[0], planPoint[1]] as WallPlanPoint)
|
||||
: snapPointToGrid([planPoint[0], planPoint[1]] as WallPlanPoint, GRID_STEP)
|
||||
if (!anchor) {
|
||||
anchor = [snapped[0], snapped[1]]
|
||||
return
|
||||
}
|
||||
const dx = snapped[0] - anchor[0]
|
||||
const dz = snapped[1] - anchor[1]
|
||||
lastDelta = [dx, dz]
|
||||
// Live-drag exception (wiki/architecture/tools.md): write the
|
||||
// delta to BOTH `mesh.position` (direct Three.js mutation) and
|
||||
// `useLiveTransforms.position` (React-bound source of truth).
|
||||
// They MUST match — `ParametricNodeRenderer` re-renders on every
|
||||
// useLiveTransforms change and reconciles `<group position={...}>`,
|
||||
// so a divergence makes the two writes fight every frame.
|
||||
useLiveTransforms.getState().set(slabId, {
|
||||
position: [dx, 0, dz],
|
||||
rotation: 0,
|
||||
})
|
||||
const mesh = sceneRegistry.nodes.get(slabId) as THREE.Object3D | undefined
|
||||
if (mesh) mesh.position.set(dx, 0, dz)
|
||||
},
|
||||
canCommit() {
|
||||
const live = useScene.getState().nodes[slabId] as SlabNode | undefined
|
||||
if (!live || live.type !== 'slab') return false
|
||||
const [dx, dz] = lastDelta
|
||||
if (dx === 0 && dz === 0) return false
|
||||
// Side-effect commit sequence — mirrors `MoveSlabTool.onGridClick`
|
||||
// so the React render that clears `group.position` (via the
|
||||
// useLiveTransforms.clear below) and the `GeometrySystem` rebuild
|
||||
// (via the sync `markDirty`) land in the same paint cycle. Order
|
||||
// matters:
|
||||
// 1. Write the translated polygon to `scene`. The dispatcher's
|
||||
// snapshot-diff right after `canCommit` returns will pick
|
||||
// this up as the single tracked change for undo.
|
||||
// 2. `markDirty` directly — bypasses the rAF-deferred batch in
|
||||
// `updateNodesAction`, so `GeometrySystem` sees the dirty
|
||||
// flag synchronously and can rebuild this frame (without
|
||||
// this the rebuild slides into the next frame and the slab
|
||||
// visually pops to its original position for one paint).
|
||||
// 3. Clear `useLiveTransforms` — `ParametricNodeRenderer` then
|
||||
// re-renders `<group position={[0,0,0]}>` instead of the
|
||||
// live delta. Without the rebuild from step 2 also landing
|
||||
// this frame, the group would render at (0,0,0) over the
|
||||
// *unrebuilt* (still-original) geometry → original-position
|
||||
// blink. With step 2 in place, the rebuild and the React
|
||||
// render commit together → smooth.
|
||||
useScene.getState().updateNodes([
|
||||
{
|
||||
id: slabId,
|
||||
data: {
|
||||
polygon: translatePolygon(originalPolygon, dx, dz),
|
||||
holes: originalHoles.map((h) => translatePolygon(h, dx, dz)),
|
||||
},
|
||||
},
|
||||
])
|
||||
useScene.getState().markDirty(slabId)
|
||||
useLiveTransforms.getState().clear(slabId)
|
||||
return true
|
||||
},
|
||||
}
|
||||
return session
|
||||
}
|
||||
@@ -64,6 +64,29 @@ function setMeshOffset(id: AnyNodeId, deltaX: number, deltaZ: number): void {
|
||||
if (mesh) mesh.position.set(deltaX, 0, deltaZ)
|
||||
}
|
||||
|
||||
/**
|
||||
* Distinguish 3D-canvas grid events (which this tool handles) from
|
||||
* 2D floor-plan grid events (which `slabFloorplanMoveTarget` +
|
||||
* `FloorplanRegistryMoveOverlay` Path 1 handle). The 2D scene wraps
|
||||
* everything in `[data-floorplan-scene]`; if the native event's target
|
||||
* is inside that subtree, the event belongs to the 2D mover. Without
|
||||
* this guard, both paths would write the polygon on commit and produce
|
||||
* two history entries / a double-translation.
|
||||
*/
|
||||
function isFloorplanSourcedEvent(event: GridEvent): boolean {
|
||||
// ThreeEvent (3D) wraps the DOM PointerEvent under `.nativeEvent`;
|
||||
// the 2D emitter passes the raw PointerEvent directly. Cover both.
|
||||
const native: unknown = event.nativeEvent
|
||||
const candidate =
|
||||
(native as { target?: unknown; nativeEvent?: { target?: unknown } } | null) ?? null
|
||||
const target =
|
||||
(candidate?.target as Element | null | undefined) ??
|
||||
(candidate?.nativeEvent as { target?: Element | null } | undefined)?.target ??
|
||||
null
|
||||
if (!target || typeof (target as Element).closest !== 'function') return false
|
||||
return (target as Element).closest('[data-floorplan-scene]') != null
|
||||
}
|
||||
|
||||
export const MoveSlabTool: React.FC<{ node: SlabNode }> = ({ node }) => {
|
||||
const activatedAtRef = useRef<number>(Date.now())
|
||||
const originalPolygonRef = useRef(node.polygon.map(([x, z]) => [x, z] as [number, number]))
|
||||
@@ -129,6 +152,7 @@ export const MoveSlabTool: React.FC<{ node: SlabNode }> = ({ node }) => {
|
||||
}
|
||||
|
||||
const onGridMove = (event: GridEvent) => {
|
||||
if (isFloorplanSourcedEvent(event)) return
|
||||
const [localX, localZ] = snapFenceDraftPoint({
|
||||
point: [event.localPosition[0], event.localPosition[2]],
|
||||
walls: levelWalls,
|
||||
@@ -150,6 +174,7 @@ export const MoveSlabTool: React.FC<{ node: SlabNode }> = ({ node }) => {
|
||||
}
|
||||
|
||||
const onGridClick = (event: GridEvent) => {
|
||||
if (isFloorplanSourcedEvent(event)) return
|
||||
if (Date.now() - activatedAtRef.current < 150) {
|
||||
event.nativeEvent?.stopPropagation?.()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user