import { type AnyNodeId, type FloorplanAffordance, type SpawnNode, useLiveNodeOverrides, useScene, } from '@pascal-app/core' import { isAngleSnapActive } from '@pascal-app/editor' import { rotateAffordanceDelta } from '../shared/rotate-affordance' export const spawnRotateAffordance: FloorplanAffordance = { start({ node, initialPlanPoint }) { const spawnId = node.id as AnyNodeId const initialRotation = node.rotation ?? 0 const cx = node.position[0] const cz = node.position[2] const initialAngle = Math.atan2(initialPlanPoint[1] - cz, initialPlanPoint[0] - cx) let lastRotation = initialRotation return { affectedIds: [spawnId], apply({ planPoint }) { const delta = rotateAffordanceDelta({ center: [cx, cz], initialAngle, planPoint, free: !isAngleSnapActive(), }) lastRotation = initialRotation - delta useLiveNodeOverrides.getState().set(spawnId, { rotation: lastRotation }) useScene.getState().markDirty(spawnId) }, canCommit() { return true }, commit() { useLiveNodeOverrides.getState().clear(spawnId) useScene.getState().updateNode(spawnId, { rotation: lastRotation }) }, } }, }