'use client' import { type AnyNode, type AnyNodeId, type MaterialSchema, type StairNode, type StairRailingMode, type StairTopLandingMode, type StairType, StairNode as StairNodeSchema, type StairSegmentNode, StairSegmentNode as StairSegmentNodeSchema, useScene, } from '@pascal-app/core' import { useViewer } from '@pascal-app/viewer' import { Copy, Move, Plus, Trash2 } from 'lucide-react' import { useCallback } from 'react' import { sfxEmitter } from '../../../lib/sfx-bus' import useEditor from '../../../store/use-editor' import { DEFAULT_SPIRAL_STAIR_SWEEP_ANGLE } from '../../tools/stair/stair-defaults' import { ActionButton, ActionGroup } from '../controls/action-button' import { MaterialPicker } from '../controls/material-picker' import { PanelSection } from '../controls/panel-section' import { SegmentedControl } from '../controls/segmented-control' import { SliderControl } from '../controls/slider-control' import { ToggleControl } from '../controls/toggle-control' import { PanelWrapper } from './panel-wrapper' const RAILING_MODE_OPTIONS: { label: string; value: StairRailingMode }[] = [ { label: 'None', value: 'none' }, { label: 'Left', value: 'left' }, { label: 'Right', value: 'right' }, { label: 'Both', value: 'both' }, ] const STAIR_TYPE_OPTIONS: { label: string; value: StairType }[] = [ { label: 'Straight', value: 'straight' }, { label: 'Curved', value: 'curved' }, { label: 'Spiral', value: 'spiral' }, ] const TOP_LANDING_MODE_OPTIONS: { label: string; value: StairTopLandingMode }[] = [ { label: 'None', value: 'none' }, { label: 'Integrated', value: 'integrated' }, ] export function StairPanel() { const selectedIds = useViewer((s) => s.selection.selectedIds) const setSelection = useViewer((s) => s.setSelection) const nodes = useScene((s) => s.nodes) const updateNode = useScene((s) => s.updateNode) const createNode = useScene((s) => s.createNode) const createNodes = useScene((s) => s.createNodes) const setMovingNode = useEditor((s) => s.setMovingNode) const selectedId = selectedIds[0] const node = selectedId ? (nodes[selectedId as AnyNode['id']] as StairNode | undefined) : undefined const handleUpdate = useCallback( (updates: Partial) => { if (!selectedId) return updateNode(selectedId as AnyNode['id'], updates) }, [selectedId, updateNode], ) const handleMaterialChange = useCallback( (material: MaterialSchema) => { handleUpdate({ material, materialPreset: undefined }) }, [handleUpdate], ) const handleMaterialPresetChange = useCallback( (materialPreset: string) => { handleUpdate({ materialPreset, material: undefined }) }, [handleUpdate], ) const handleClose = useCallback(() => { setSelection({ selectedIds: [] }) }, [setSelection]) const getLastSegmentFillDefaults = useCallback(() => { if (!node) return { fillToFloor: true } const children = node.children ?? [] const lastChildId = children[children.length - 1] if (lastChildId) { const lastChild = nodes[lastChildId as AnyNodeId] as StairSegmentNode | undefined if (lastChild?.type === 'stair-segment') { return { fillToFloor: lastChild.fillToFloor } } } return { fillToFloor: true } }, [node, nodes]) const handleAddFlight = useCallback(() => { if (!node) return const { fillToFloor } = getLastSegmentFillDefaults() const segment = StairSegmentNodeSchema.parse({ segmentType: 'stair', width: 1.0, length: 3.0, height: 2.5, stepCount: 10, attachmentSide: 'front', fillToFloor, thickness: 0.25, position: [0, 0, 0], }) createNode(segment, node.id as AnyNodeId) }, [node, createNode, getLastSegmentFillDefaults]) const handleAddLanding = useCallback(() => { if (!node) return const { fillToFloor } = getLastSegmentFillDefaults() const segment = StairSegmentNodeSchema.parse({ segmentType: 'landing', width: 1.0, length: 1.0, height: 0, stepCount: 0, attachmentSide: 'front', fillToFloor, thickness: 0.32, position: [0, 0, 0], }) createNode(segment, node.id as AnyNodeId) }, [node, createNode, getLastSegmentFillDefaults]) const handleSelectSegment = useCallback( (segmentId: string) => { setSelection({ selectedIds: [segmentId as AnyNode['id']] }) }, [setSelection], ) const handleDuplicate = useCallback(() => { if (!node?.parentId) return sfxEmitter.emit('sfx:item-pick') let duplicateInfo = structuredClone(node) as any delete duplicateInfo.id duplicateInfo.metadata = { ...duplicateInfo.metadata } duplicateInfo.children = [] duplicateInfo.position = [ duplicateInfo.position[0] + 1, duplicateInfo.position[1], duplicateInfo.position[2] + 1, ] try { const duplicate = StairNodeSchema.parse(duplicateInfo) const nodesState = useScene.getState().nodes const children = node.children || [] const createOps: { node: AnyNode; parentId?: AnyNodeId }[] = [ { node: duplicate, parentId: duplicate.parentId as AnyNodeId }, ] for (const childId of children) { const childNode = nodesState[childId] if (childNode && childNode.type === 'stair-segment') { let childDuplicateInfo = structuredClone(childNode) as any delete childDuplicateInfo.id childDuplicateInfo.metadata = { ...childDuplicateInfo.metadata } const childDuplicate = StairSegmentNodeSchema.parse(childDuplicateInfo) createOps.push({ node: childDuplicate, parentId: duplicate.id as AnyNodeId }) } } createNodes(createOps) setSelection({ selectedIds: [duplicate.id as AnyNode['id']] }) } catch (e) { console.error('Failed to duplicate stair', e) } }, [createNodes, node, setSelection]) const handleMove = useCallback(() => { if (node) { sfxEmitter.emit('sfx:item-pick') setMovingNode(node) setSelection({ selectedIds: [] }) } }, [node, setMovingNode, setSelection]) const handleDelete = useCallback(() => { if (!(selectedId && node)) return sfxEmitter.emit('sfx:item-delete') const parentId = node.parentId useScene.getState().deleteNode(selectedId as AnyNodeId) if (parentId) { useScene.getState().dirtyNodes.add(parentId as AnyNodeId) } setSelection({ selectedIds: [] }) }, [selectedId, node, setSelection]) if (!node || node.type !== 'stair' || selectedIds.length !== 1) return null const segments = (node.children ?? []) .map((childId) => nodes[childId as AnyNodeId] as StairSegmentNode | undefined) .filter((n): n is StairSegmentNode => n?.type === 'stair-segment') return ( handleUpdate( value === 'spiral' && node.stairType !== 'spiral' ? { stairType: value, sweepAngle: DEFAULT_SPIRAL_STAIR_SWEEP_ANGLE, position: [node.position[0], 0, node.position[2]], } : { stairType: value }, ) } options={STAIR_TYPE_OPTIONS} value={node.stairType ?? 'straight'} /> {node.stairType === 'straight' && (
{segments.map((seg, i) => ( ))}
} label="Add flight" onClick={handleAddFlight} /> } label="Add landing" onClick={handleAddLanding} />
)} {(node.stairType === 'curved' || node.stairType === 'spiral') && ( handleUpdate({ width: value })} precision={2} step={0.05} unit="m" value={Math.round((node.width ?? 1) * 100) / 100} /> handleUpdate({ totalRise: value })} precision={2} step={0.05} unit="m" value={Math.round((node.totalRise ?? 2.5) * 100) / 100} /> handleUpdate({ stepCount: Math.max(2, Math.round(value)) })} precision={0} step={1} unit="" value={Math.max(2, Math.round(node.stepCount ?? 10))} /> {node.stairType !== 'spiral' && ( handleUpdate({ fillToFloor: checked })} /> )} {(node.stairType === 'spiral' || !(node.fillToFloor ?? true)) && ( handleUpdate({ thickness: value })} precision={2} step={0.01} unit="m" value={Math.round((node.thickness ?? 0.25) * 100) / 100} /> )} handleUpdate({ innerRadius: value })} precision={2} step={0.05} unit="m" value={Math.round((node.innerRadius ?? 0.9) * 100) / 100} /> handleUpdate({ sweepAngle: (degrees * Math.PI) / 180 })} precision={0} step={1} unit="°" value={Math.round(((node.sweepAngle ?? Math.PI / 2) * 180) / Math.PI)} /> {node.stairType === 'spiral' && ( <> handleUpdate({ topLandingMode: value })} options={TOP_LANDING_MODE_OPTIONS} value={node.topLandingMode ?? 'none'} /> {(node.topLandingMode ?? 'none') === 'integrated' && ( handleUpdate({ topLandingDepth: value })} precision={2} step={0.05} unit="m" value={Math.round((node.topLandingDepth ?? 0.9) * 100) / 100} /> )} handleUpdate({ showCenterColumn: checked })} /> handleUpdate({ showStepSupports: checked })} /> )} )} { const pos = [...node.position] as [number, number, number] pos[0] = v handleUpdate({ position: pos }) }} precision={2} step={0.05} unit="m" value={Math.round(node.position[0] * 100) / 100} /> { const pos = [...node.position] as [number, number, number] pos[1] = v handleUpdate({ position: pos }) }} precision={2} step={0.05} unit="m" value={Math.round(node.position[1] * 100) / 100} /> { const pos = [...node.position] as [number, number, number] pos[2] = v handleUpdate({ position: pos }) }} precision={2} step={0.05} unit="m" value={Math.round(node.position[2] * 100) / 100} /> { handleUpdate({ rotation: (degrees * Math.PI) / 180 }) }} precision={0} step={1} unit="°" value={Math.round((node.rotation * 180) / Math.PI)} />
{ sfxEmitter.emit('sfx:item-rotate') handleUpdate({ rotation: node.rotation - Math.PI / 4 }) }} /> { sfxEmitter.emit('sfx:item-rotate') handleUpdate({ rotation: node.rotation + Math.PI / 4 }) }} />
handleUpdate({ railingMode: value })} options={RAILING_MODE_OPTIONS} value={node.railingMode ?? 'none'} /> {(node.railingMode ?? 'none') !== 'none' && ( handleUpdate({ railingHeight: value })} precision={2} step={0.02} unit="m" value={Math.round((node.railingHeight ?? 0.92) * 100) / 100} /> )} } label="Move" onClick={handleMove} /> } label="Duplicate" onClick={handleDuplicate} /> } label="Delete" onClick={handleDelete} />
) }