Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/core/src/events/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ export interface ThumbnailGenerateEvent {
* that should fire immediately from the current camera pose.
*/
snapLevels?: boolean
/**
* When true, keep the rendered alpha channel — emits a transparent PNG
* without baking the scene background into the output. Used by the
* preset capture flow so saved preset thumbnails composite cleanly on
* any palette background.
*/
transparent?: boolean
}

export interface CameraControlFitSceneEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ function makeScene(nodes: Record<string, AnyNode>): SceneApi {
markDirty: () => {},
pauseHistory: () => {},
resumeHistory: () => {},
getSubtreeSnapshot: () => null,
materializeSubtree: () => null,
}
}

Expand Down
39 changes: 24 additions & 15 deletions packages/core/src/registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
export type {
ArcResizeHandle,
Cursor,
EditorApi,
EndpointMoveHandle,
HandleAnchor,
HandleAxis,
HandleDescriptor,
HandleList,
HandlePlacement,
HandlePortal,
LinearResizeHandle,
RadialResizeHandle,
TapActionHandle,
} from './handles'
export {
discoverPlugins,
getSelectableKinds,
isPresettable,
isPresettableKind,
isRegistryMovable,
isRegistrySelectable,
kindsWithFloorplanScope,
Expand All @@ -17,22 +34,14 @@ export {
collectDescendants,
type SpatialQuery,
} from './relations-resolver'
export type {
ArcResizeHandle,
Cursor,
EditorApi,
EndpointMoveHandle,
HandleAnchor,
HandleAxis,
HandleDescriptor,
HandleList,
HandlePlacement,
HandlePortal,
LinearResizeHandle,
RadialResizeHandle,
TapActionHandle,
} from './handles'
export { createSceneApi, type SceneStoreLike } from './scene-api'
export {
buildSubtreeSnapshot,
type MaterializedSubtree,
materializeSubtree,
type NodeSubtree,
type SubtreeNode,
} from './subtree'
export type {
Affordance,
AnyNodeDefinition,
Expand Down
43 changes: 42 additions & 1 deletion packages/core/src/registry/registry.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { beforeEach, describe, expect, test } from 'bun:test'
import { z } from 'zod'
import { loadPlugin, nodeRegistry, registerNode } from './registry'
import {
isPresettable,
isPresettableKind,
loadPlugin,
nodeRegistry,
registerNode,
} from './registry'
import type { AnyNodeDefinition, Plugin } from './types'

function makeDefinition(
Expand Down Expand Up @@ -70,6 +76,41 @@ describe('nodeRegistry', () => {
})
})

describe('isPresettable', () => {
beforeEach(() => {
nodeRegistry._reset()
})

test('explicit true wins', () => {
const def = makeDefinition('explicit-true', { capabilities: { presettable: true } })
expect(isPresettable(def)).toBe(true)
})

test('explicit false wins even with parametrics', () => {
const def = makeDefinition('explicit-false', {
capabilities: { presettable: false },
parametrics: { groups: [] } as any,
})
expect(isPresettable(def)).toBe(false)
})

test('defaults to true when parametrics exists', () => {
const def = makeDefinition('param', { parametrics: { groups: [] } as any })
expect(isPresettable(def)).toBe(true)
})

test('defaults to false without parametrics', () => {
const def = makeDefinition('no-param')
expect(isPresettable(def)).toBe(false)
})

test('isPresettableKind looks up the registry', () => {
registerNode(makeDefinition('shelfy', { parametrics: { groups: [] } as any }))
expect(isPresettableKind('shelfy')).toBe(true)
expect(isPresettableKind('unknown')).toBe(false)
})
})

describe('loadPlugin', () => {
beforeEach(() => {
nodeRegistry._reset()
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/registry/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ export function isRegistryMovable(kind: string): boolean {
return false
}

/**
* Whether the kind can be saved as a reusable preset. Default: an
* explicit `capabilities.presettable` boolean wins; otherwise the kind
* is presettable iff it declares `def.parametrics`. Read by host apps
* (community shell) to gate "save as preset" UI on a selection.
*/
export function isPresettable(def: AnyNodeDefinition): boolean {
if (typeof def.capabilities.presettable === 'boolean') {
return def.capabilities.presettable
}
return def.parametrics !== undefined
}

export function isPresettableKind(kind: string): boolean {
const def = nodeRegistry.get(kind)
return def ? isPresettable(def) : false
}

export async function loadPlugin(plugin: Plugin): Promise<void> {
if (plugin.apiVersion !== HOST_API_VERSION) {
throw new Error(
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/registry/relations-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ function makeFakeScene(nodes: Record<string, AnyNode>): SceneApi {
markDirty: () => {},
pauseHistory: () => {},
resumeHistory: () => {},
getSubtreeSnapshot: () => null,
materializeSubtree: () => null,
}
}

Expand Down
36 changes: 36 additions & 0 deletions packages/core/src/registry/scene-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AnyNode, AnyNodeId } from '../schema/types'
import { pauseSceneHistory, resumeSceneHistory } from '../store/history-control'
import { buildSubtreeSnapshot, materializeSubtree as runMaterializeSubtree } from './subtree'
import type { SceneApi } from './types'

/**
Expand All @@ -14,6 +15,7 @@ export type SceneStoreLike = {
rootNodeIds: AnyNodeId[]
dirtyNodes: Set<AnyNodeId>
createNode: (node: AnyNode, parentId?: AnyNodeId) => void
createNodes?: (ops: { node: AnyNode; parentId?: AnyNodeId }[]) => void
updateNode: (id: AnyNodeId, data: Partial<AnyNode>) => void
deleteNode: (id: AnyNodeId) => void
markDirty: (id: AnyNodeId) => void
Expand Down Expand Up @@ -104,5 +106,39 @@ export function createSceneApi(store: SceneStoreLike): SceneApi {
resumeSceneHistory(store)
snapshot = null
},

getSubtreeSnapshot(rootId) {
return buildSubtreeSnapshot(store.getState().nodes, rootId)
},

materializeSubtree(subtree, position, parentId) {
const { rootId, nodes } = runMaterializeSubtree(subtree, position)
const state = store.getState()
// Prefer batched `createNodes` when the store exposes it — keeps
// children-array writes and dirty-marking in one tick, identical
// to how `applyNodeChanges` lands a multi-node paste. The
// minimal `SceneStoreLike` does not require it, so the test
// store can fall back to per-node `createNode` calls.
const root = nodes[0]
if (!root) return null
const ops: { node: AnyNode; parentId?: AnyNodeId }[] = []
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i]!
if (i === 0) {
ops.push(parentId ? { node, parentId } : { node })
} else {
ops.push({ node })
}
}
const createNodes = state.createNodes
if (createNodes) {
createNodes(ops)
} else {
for (const op of ops) {
state.createNode(op.node, op.parentId)
}
}
return rootId
},
}
}
164 changes: 164 additions & 0 deletions packages/core/src/registry/subtree.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { describe, expect, test } from 'bun:test'
import type { AnyNode, AnyNodeId } from '../schema/types'
import { buildSubtreeSnapshot, materializeSubtree } from './subtree'

function makeNode(id: string, type: string, extra: Record<string, unknown> = {}): AnyNode {
return {
object: 'node',
id,
type,
parentId: null,
visible: true,
metadata: {},
...extra,
} as unknown as AnyNode
}

describe('buildSubtreeSnapshot', () => {
test('returns null for missing root', () => {
expect(buildSubtreeSnapshot({}, 'missing' as AnyNodeId)).toBeNull()
})

test('strips id / parentId / position / wallId from the root', () => {
const nodes: Record<AnyNodeId, AnyNode> = {
['door_1' as AnyNodeId]: makeNode('door_1', 'door', {
parentId: 'level_1',
position: [1, 2, 3],
wallId: 'wall_x',
wallT: 0.4,
width: 0.9,
height: 2.1,
}),
}
const snap = buildSubtreeSnapshot(nodes, 'door_1' as AnyNodeId)
expect(snap).not.toBeNull()
expect((snap?.root as any).id).toBeUndefined()
expect((snap?.root as any).parentId).toBeUndefined()
expect((snap?.root as any).position).toBeUndefined()
expect((snap?.root as any).wallId).toBeUndefined()
expect((snap?.root as any).wallT).toBeUndefined()
expect((snap?.root as any).width).toBe(0.9)
expect((snap?.root as any).height).toBe(2.1)
expect(snap?.rootKind).toBe('door')
})

test('captures descendants via the children array', () => {
const nodes: Record<AnyNodeId, AnyNode> = {
['shelf_1' as AnyNodeId]: makeNode('shelf_1', 'shelf', {
position: [5, 0, 5],
children: ['item_a', 'item_b'],
width: 1,
}),
['item_a' as AnyNodeId]: makeNode('item_a', 'item', {
parentId: 'shelf_1',
position: [0, 0, 0],
}),
['item_b' as AnyNodeId]: makeNode('item_b', 'item', {
parentId: 'shelf_1',
position: [0.3, 0, 0],
}),
}
const snap = buildSubtreeSnapshot(nodes, 'shelf_1' as AnyNodeId)
expect(snap?.descendants).toHaveLength(2)
// Descendants keep their local positions.
expect((snap?.descendants[0] as any).position).toEqual([0, 0, 0])
expect((snap?.descendants[1] as any).position).toEqual([0.3, 0, 0])
// Internal child references rewritten to tokens, not original ids.
const childTokens = (snap?.root as any).children as string[]
expect(childTokens).toHaveLength(2)
for (const t of childTokens) {
expect(t.includes('::')).toBe(true)
expect(t.startsWith('item::')).toBe(true)
}
})
})

describe('materializeSubtree', () => {
test('round-trips a single-node snapshot at a new position with fresh ids', () => {
const original = makeNode('door_orig', 'door', {
position: [1, 2, 3],
wallId: 'wall_x',
width: 0.9,
height: 2.1,
})
const snap = buildSubtreeSnapshot(
{ ['door_orig' as AnyNodeId]: original },
'door_orig' as AnyNodeId,
)
if (!snap) throw new Error('snap')

const { rootId, nodes } = materializeSubtree(snap, [10, 0, -4])
expect(nodes).toHaveLength(1)
const newRoot = nodes[0] as any
expect(newRoot.id).toBe(rootId)
expect(newRoot.id).not.toBe('door_orig')
expect(newRoot.id.startsWith('door_')).toBe(true)
expect(newRoot.parentId).toBeNull()
expect(newRoot.position).toEqual([10, 0, -4])
// Host ref was stripped at snapshot time; materialize doesn't re-add it.
expect(newRoot.wallId).toBeUndefined()
// Parametric fields preserved verbatim.
expect(newRoot.width).toBe(0.9)
expect(newRoot.height).toBe(2.1)
})

test('preserves a parent/child subtree with remapped ids and relative positions', () => {
const nodes: Record<AnyNodeId, AnyNode> = {
['shelf_1' as AnyNodeId]: makeNode('shelf_1', 'shelf', {
position: [5, 0, 5],
children: ['item_a', 'item_b'],
width: 1,
}),
['item_a' as AnyNodeId]: makeNode('item_a', 'item', {
parentId: 'shelf_1',
position: [0, 0, 0],
}),
['item_b' as AnyNodeId]: makeNode('item_b', 'item', {
parentId: 'shelf_1',
position: [0.3, 0, 0],
}),
}
const snap = buildSubtreeSnapshot(nodes, 'shelf_1' as AnyNodeId)!
const { rootId, nodes: out } = materializeSubtree(snap, [99, 0, -99])
expect(out).toHaveLength(3)
const root = out[0] as any
expect(root.id).toBe(rootId)
expect(root.id).not.toBe('shelf_1')
expect(root.position).toEqual([99, 0, -99])
// Children point at fresh ids that exist in the output.
const ids = new Set(out.map((n) => (n as any).id))
expect(root.children).toHaveLength(2)
for (const cid of root.children) expect(ids.has(cid)).toBe(true)
// Descendant parentIds point at the new root id.
for (let i = 1; i < out.length; i += 1) {
const desc = out[i] as any
expect(desc.parentId).toBe(rootId)
// Position preserved.
expect(Array.isArray(desc.position)).toBe(true)
}
// Internal-token metadata is gone.
for (const node of out) {
const md = (node as any).metadata ?? {}
expect(md.__subtreeKey).toBeUndefined()
}
})

test('two materializations yield disjoint id sets', () => {
const nodes: Record<AnyNodeId, AnyNode> = {
['shelf_1' as AnyNodeId]: makeNode('shelf_1', 'shelf', {
position: [0, 0, 0],
children: ['item_a'],
}),
['item_a' as AnyNodeId]: makeNode('item_a', 'item', {
parentId: 'shelf_1',
position: [0, 0, 0],
}),
}
const snap = buildSubtreeSnapshot(nodes, 'shelf_1' as AnyNodeId)!
const first = materializeSubtree(snap, [0, 0, 0])
const second = materializeSubtree(snap, [1, 0, 0])
const idsA = new Set(first.nodes.map((n) => (n as any).id))
const idsB = new Set(second.nodes.map((n) => (n as any).id))
for (const id of idsA) expect(idsB.has(id)).toBe(false)
})
})
Loading
Loading