Anchor inspector resize edits

This commit is contained in:
Formsmith
2026-06-15 12:56:23 +03:00
parent 5d9d14c714
commit 66023aab7c
2 changed files with 106 additions and 26 deletions
+87 -10
View File
@@ -14,7 +14,7 @@ import gentilisBoldFontJson from "three/examples/fonts/gentilis_bold.typeface.js
import helvetikerBoldFontJson from "three/examples/fonts/helvetiker_bold.typeface.json";
import optimerBoldFontJson from "three/examples/fonts/optimer_bold.typeface.json";
import { AlignOverlay, MirrorOverlay, type AlignOverlayState, type MirrorOverlayState } from "@/components/workplane/ActionOverlays";
import { ShapeInspector, SnapGridControl } from "@/components/workplane/ShapeInspector";
import { ShapeInspector, SnapGridControl, type ShapeInspectorUpdateOptions } from "@/components/workplane/ShapeInspector";
import { WorkspaceSettingsModal } from "@/components/workplane/WorkspaceSettingsModal";
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, workplaneSettingsFingerprint } from "@/lib/workplaneSettings";
import { cleanNearZero, cleanRotationDegrees, fallbackSolidColor, mirroredAxisCount, mirrorSign, proportionalResizeScale, resizedShapeSize, shapeDepth, shapeWidth } from "@/lib/workplaneShapes";
@@ -212,6 +212,12 @@ type RotationHandleSide = "near" | "right" | "far" | "left";
type RotationHandleSides = Record<RotationAxis, RotationHandleSide>;
type ShapeUpdatePatch = Partial<WorkplaneShape> & { bakeTransform?: boolean };
type ResizeSigns = { x: number; z: number };
type ResizeAnchorMemory = {
shapeId: string;
handleKey: string;
signs: ResizeSigns;
pressedY: "top" | "bottom" | null;
};
type TransformDragState = {
id: string;
ids: string[];
@@ -710,7 +716,7 @@ function projectedWorldYForScreenY(state: ThreeState, shape: WorkplaneShape, tar
return nextWorldY;
}
function patchWithPreservedWorldBottom(shape: WorkplaneShape, patch: Partial<WorkplaneShape>) {
function patchWithPreservedWorldYEdge(shape: WorkplaneShape, patch: Partial<WorkplaneShape>, edge: "bottom" | "top") {
const startFrame = selectionFrameForShapes([shape], [shape.id]);
if (!startFrame) {
return patch;
@@ -722,12 +728,57 @@ function patchWithPreservedWorldBottom(shape: WorkplaneShape, patch: Partial<Wor
return patch;
}
const draftBounds = selectionWorldYBounds(draftFrame);
const delta = edge === "bottom" ? startBounds.min - draftBounds.min : startBounds.max - draftBounds.max;
return {
...patch,
elevation: cleanNearZero(clamp((draftShape.elevation ?? 0) + startBounds.min - draftBounds.min, MIN_ELEVATION, MAX_ELEVATION), 0.0005),
elevation: cleanNearZero(clamp((draftShape.elevation ?? 0) + delta, MIN_ELEVATION, MAX_ELEVATION), 0.0005),
};
}
function patchWithPreservedWorldBottom(shape: WorkplaneShape, patch: Partial<WorkplaneShape>) {
return patchWithPreservedWorldYEdge(shape, patch, "bottom");
}
function resizeSignsForDimension(signs: ResizeSigns, axis: "width" | "depth") {
return axis === "width" ? { x: signs.x, z: 0 } : { x: 0, z: signs.z };
}
function patchWithResizeAnchor(
shape: WorkplaneShape,
patch: Partial<WorkplaneShape>,
axis: ShapeInspectorUpdateOptions["resizeAxis"] | DimensionMark["axis"],
anchor: ResizeAnchorMemory | null,
) {
if (axis === "height") {
return patchWithPreservedWorldYEdge(shape, patch, anchor?.shapeId === shape.id && anchor.pressedY === "bottom" ? "top" : "bottom");
}
if (axis !== "width" && axis !== "depth") {
return patchWithPreservedWorldBottom(shape, patch);
}
if (!anchor || anchor.shapeId !== shape.id) {
return patchWithPreservedWorldBottom(shape, patch);
}
const signs = resizeSignsForDimension(anchor.signs, axis);
if (!signs.x && !signs.z) {
return patchWithPreservedWorldBottom(shape, patch);
}
const frame = selectionFrameForShapes([shape], [shape.id]);
if (!frame) {
return patchWithPreservedWorldBottom(shape, patch);
}
const width = Math.max(MIN_SHAPE_SIZE, patch.width ?? shapeWidth(shape));
const depth = Math.max(MIN_SHAPE_SIZE, patch.depth ?? shapeDepth(shape));
const center = resizeCenterFromAnchor(frame, resizeAnchorPointForFrame(frame, signs), signs, width, depth);
return patchWithPreservedWorldBottom(shape, {
...patch,
...resizedShapePatchFromFrame(shape, center, width, depth),
});
}
function resizeShapeFromFrameHandle(
transform: TransformDragState,
point: THREE.Vector3,
@@ -900,6 +951,7 @@ export function WorkplaneViewport({
const dragRef = useRef<DragState | null>(null);
const marqueeRef = useRef<MarqueeState | null>(null);
const transformRef = useRef<TransformDragState | null>(null);
const lastResizeAnchorRef = useRef<ResizeAnchorMemory | null>(null);
const suppressNextLiftEditRef = useRef(false);
const snapRef = useRef(snap);
const workspaceRef = useRef(workspace);
@@ -927,6 +979,24 @@ export function WorkplaneViewport({
const placementElevationRef = useRef(placementElevation);
const workplaneModeRef = useRef(workplaneMode);
const rememberResizeAnchor = useCallback((shapeId: string, kind: TransformHandleKind, handleKey: string) => {
if (kind === "scale") {
const signs = resizeSignsForHandle(handleKey);
if (signs.x || signs.z) {
lastResizeAnchorRef.current = { shapeId, handleKey, signs, pressedY: null };
}
return;
}
if (kind === "height") {
lastResizeAnchorRef.current = {
shapeId,
handleKey,
signs: { x: 0, z: 0 },
pressedY: handleKey === "bottom-height" ? "bottom" : "top",
};
}
}, []);
useEffect(() => {
const nextKey = workspaceSettingsKey ?? null;
if (workspaceSettingsKeyRef.current !== nextKey) {
@@ -985,6 +1055,7 @@ export function WorkplaneViewport({
const nextSelectedIdsKey = selectedIds.join("|");
if (nextSelectedIdsKey !== selectedIdsKeyRef.current) {
selectedIdsKeyRef.current = nextSelectedIdsKey;
lastResizeAnchorRef.current = null;
setHoverMeasureKey(null);
setPinnedMeasureKey(null);
setEditingDimension(null);
@@ -1256,6 +1327,7 @@ export function WorkplaneViewport({
if (kind === "scale" && !scaleStartPoint) {
return;
}
rememberResizeAnchor(shape.id, kind, resizeHandleKey);
event.preventDefault();
event.stopPropagation();
event.currentTarget.setPointerCapture(event.pointerId);
@@ -1335,7 +1407,7 @@ export function WorkplaneViewport({
}
onInteractionActiveChange?.(true);
},
[onInteractionActiveChange, toRawPlanePoint],
[onInteractionActiveChange, rememberResizeAnchor, toRawPlanePoint],
);
const updateTransform = useCallback(
@@ -1522,9 +1594,13 @@ export function WorkplaneViewport({
}, [onInteractionActiveChange, onUpdateShape, suppressLiftEditAfterDrag]);
const beginDimensionEdit = useCallback((mark: DimensionMark) => {
const id = selectedIdsRef.current[0];
if (id && (mark.axis === "width" || mark.axis === "depth" || mark.axis === "height")) {
rememberResizeAnchor(id, mark.axis === "height" ? "height" : "scale", mark.handleKey);
}
setPinnedMeasureKey(mark.handleKey);
setEditingDimension({ key: mark.key, axis: mark.axis, x: mark.labelX, y: mark.labelY, value: mark.label });
}, []);
}, [rememberResizeAnchor]);
const beginLiftEdit = useCallback((handleKey: string, x: number, y: number) => {
if (suppressNextLiftEditRef.current) {
@@ -1585,11 +1661,11 @@ export function WorkplaneViewport({
if (shape.kind === "cone") {
patch.baseRadius = nextValue / 2;
}
onUpdateShape(id, patchWithPreservedWorldBottom(shape, patch));
onUpdateShape(id, patchWithResizeAnchor(shape, patch, edit.axis, lastResizeAnchorRef.current));
} else if (edit.axis === "depth") {
onUpdateShape(id, patchWithPreservedWorldBottom(shape, { depth: nextValue, size: resizedShapeSize(shapeWidth(shape), nextValue) }));
onUpdateShape(id, patchWithResizeAnchor(shape, { depth: nextValue, size: resizedShapeSize(shapeWidth(shape), nextValue) }, edit.axis, lastResizeAnchorRef.current));
} else {
onUpdateShape(id, patchWithPreservedWorldBottom(shape, { height: nextValue }));
onUpdateShape(id, patchWithResizeAnchor(shape, { height: nextValue }, edit.axis, lastResizeAnchorRef.current));
}
}
setEditingDimension(null);
@@ -1752,6 +1828,7 @@ export function WorkplaneViewport({
const rotationCenter = handle.kind === "rotate" ? wheel ?? projectToScreen(pivot, state) : undefined;
const rotationStartPoint = handle.kind === "rotate" ? rayPointOnRotationPlane(state, event.clientX, event.clientY, rotationPlaneCenter, axisVector) : null;
const rotationStartVector = rotationStartPoint ? rotationStartPoint.sub(rotationPlaneCenter) : undefined;
rememberResizeAnchor(handle.id, handle.kind, resizeHandleKey);
event.preventDefault();
event.currentTarget.setPointerCapture(event.pointerId);
setEditingRotation(null);
@@ -2088,7 +2165,7 @@ export function WorkplaneViewport({
}
onInteractionActiveChange?.(false);
},
[onInteractionActiveChange, onSelectShape, onUpdateShape, setMarqueeFromState, shapesInMarquee, suppressLiftEditAfterDrag],
[onInteractionActiveChange, onSelectShape, onUpdateShape, rememberResizeAnchor, setMarqueeFromState, shapesInMarquee, suppressLiftEditAfterDrag],
);
const handleDrop = useCallback(
@@ -2247,7 +2324,7 @@ export function WorkplaneViewport({
shape={selectedShape}
snap={snap}
snapOpen={snapOpen}
onUpdate={(patch) => onUpdateShape(selectedShape.id, patch)}
onUpdate={(patch, options) => onUpdateShape(selectedShape.id, patchWithResizeAnchor(selectedShape, patch, options?.resizeAxis, lastResizeAnchorRef.current))}
onClose={() => onSelectShape(null)}
onSnapChange={setSnap}
onSnapOpenChange={setSnapOpen}
@@ -66,6 +66,8 @@ type SelectPropertyConfig = {
};
type ShapePropertyConfig = RangePropertyConfig | TextPropertyConfig | SelectPropertyConfig;
export type ShapeInspectorUpdateOptions = { resizeAxis?: "width" | "depth" | "height" };
type ShapeInspectorUpdate = (patch: Partial<WorkplaneShape>, options?: ShapeInspectorUpdateOptions) => void;
function clamp(value: number, min: number, max: number) {
return Math.min(max, Math.max(min, value));
@@ -75,16 +77,17 @@ function formatPanelNumber(value: number) {
return (Math.abs(value) < 0.005 ? 0 : value).toFixed(2);
}
function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<WorkplaneShape>) => void): ShapePropertyConfig[] {
function getShapeProperties(shape: WorkplaneShape, onUpdate: ShapeInspectorUpdate): ShapePropertyConfig[] {
const width = shapeWidth(shape);
const depth = shapeDepth(shape);
const setWidth = (value: number) => onUpdate({ width: value, size: resizedShapeSize(value, depth) });
const setDepth = (value: number) => onUpdate({ depth: value, size: resizedShapeSize(width, value) });
const setConeWidth = (value: number) => onUpdate({ width: value, baseRadius: value / 2, size: resizedShapeSize(value, depth) });
const setWidth = (value: number) => onUpdate({ width: value, size: resizedShapeSize(value, depth) }, { resizeAxis: "width" });
const setDepth = (value: number) => onUpdate({ depth: value, size: resizedShapeSize(width, value) }, { resizeAxis: "depth" });
const setConeWidth = (value: number) => onUpdate({ width: value, baseRadius: value / 2, size: resizedShapeSize(value, depth) }, { resizeAxis: "width" });
const setBaseRadius = (value: number) => {
const diameter = value * 2;
onUpdate({ baseRadius: value, width: diameter, size: resizedShapeSize(diameter, depth) });
onUpdate({ baseRadius: value, width: diameter, size: resizedShapeSize(diameter, depth) }, { resizeAxis: "width" });
};
const setHeight = (height: number) => onUpdate({ height }, { resizeAxis: "height" });
if (shape.kind === "box") {
return [
@@ -92,7 +95,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<Wor
{ label: "Steps", value: shape.steps ?? 10, min: 1, max: 64, step: 1, onChange: (steps) => onUpdate({ steps: Math.round(steps) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
];
}
@@ -103,7 +106,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<Wor
{ label: "Segments", value: shape.segments ?? 1, min: 1, max: 24, step: 1, onChange: (segments) => onUpdate({ segments: Math.round(segments) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
];
}
@@ -112,7 +115,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<Wor
{ label: "Steps", value: shape.steps ?? 24, min: 6, max: 64, step: 1, onChange: (steps) => onUpdate({ steps: Math.round(steps) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
];
}
@@ -121,7 +124,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<Wor
{ label: "Steps", value: shape.steps ?? 32, min: 6, max: 64, step: 1, onChange: (steps) => onUpdate({ steps: Math.round(steps) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
];
}
@@ -131,7 +134,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<Wor
{ label: "Base Radius", value: shape.baseRadius ?? width / 2, min: MIN_SHAPE_SIZE, max: 80, onChange: setBaseRadius },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setConeWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
{ label: "Sides", value: shape.sides ?? 96, min: 3, max: 128, step: 1, onChange: (sides) => onUpdate({ sides: Math.round(sides) }) },
];
}
@@ -141,7 +144,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<Wor
{ label: "Sides", value: shape.sides ?? 4, min: 3, max: 24, step: 1, onChange: (sides) => onUpdate({ sides: Math.round(sides) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
];
}
@@ -150,7 +153,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<Wor
{ label: "Sides", value: shape.sides ?? 64, min: 4, max: 128, step: 1, onChange: (sides) => onUpdate({ sides: Math.round(sides) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
];
}
@@ -159,7 +162,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<Wor
{ label: "Thickness", value: shape.bevel ?? 4, min: 0.5, max: 20, onChange: (bevel) => onUpdate({ bevel }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
];
}
@@ -176,7 +179,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<Wor
},
},
{ type: "select", label: "Font", value: shape.font ?? "Multilanguage", options: TEXT_FONT_OPTIONS, onChange: (font) => onUpdate({ font }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 40, onChange: (height) => onUpdate({ height }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 40, onChange: setHeight },
{ label: "Bevel", value: shape.bevel ?? 0, min: 0, max: 8, onChange: (bevel) => onUpdate({ bevel }) },
{ label: "Segments", value: shape.segments ?? 0, min: 0, max: 24, step: 1, onChange: (segments) => onUpdate({ segments: Math.round(segments) }) },
];
@@ -185,7 +188,7 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: (patch: Partial<Wor
return [
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: (height) => onUpdate({ height }) },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
];
}
@@ -201,7 +204,7 @@ export function ShapeInspector({
shape: WorkplaneShape;
snap: GridSize;
snapOpen: boolean;
onUpdate: (patch: Partial<WorkplaneShape>) => void;
onUpdate: ShapeInspectorUpdate;
onClose: () => void;
onSnapChange: Dispatch<SetStateAction<GridSize>>;
onSnapOpenChange: Dispatch<SetStateAction<boolean>>;