2026-03-31 02:03:30 +02:00
|
|
|
import type { ToolMode } from "../core/tool-mode";
|
|
|
|
|
import { DEFAULT_GRID_SIZE, snapPositiveSizeToGrid } from "../geometry/grid-snapping";
|
2026-04-05 02:38:04 +02:00
|
|
|
import { cloneBoxBrushGeometry, scaleBoxBrushGeometryToSize } from "../document/brushes";
|
2026-03-31 02:03:30 +02:00
|
|
|
|
|
|
|
|
import { createOpaqueId } from "../core/ids";
|
|
|
|
|
import type { EditorSelection } from "../core/selection";
|
|
|
|
|
import type { Vec3 } from "../core/vector";
|
|
|
|
|
|
|
|
|
|
import { cloneSelectionForCommand, getBoxBrushOrThrow, replaceBrush, setSingleBrushSelection } from "./brush-command-helpers";
|
|
|
|
|
import type { EditorCommand } from "./command";
|
|
|
|
|
|
|
|
|
|
interface ResizeBoxBrushCommandOptions {
|
|
|
|
|
brushId: string;
|
|
|
|
|
size: Vec3;
|
2026-04-04 19:26:26 +02:00
|
|
|
snapToGrid?: boolean;
|
2026-03-31 02:03:30 +02:00
|
|
|
gridSize?: number;
|
2026-04-04 19:26:26 +02:00
|
|
|
label?: string;
|
2026-03-31 02:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createResizeBoxBrushCommand(options: ResizeBoxBrushCommandOptions): EditorCommand {
|
2026-04-04 19:26:26 +02:00
|
|
|
const resolvedSize =
|
|
|
|
|
options.snapToGrid === false ? options.size : snapPositiveSizeToGrid(options.size, options.gridSize ?? DEFAULT_GRID_SIZE);
|
2026-03-31 02:03:30 +02:00
|
|
|
|
|
|
|
|
let previousSize: Vec3 | null = null;
|
2026-04-05 02:37:46 +02:00
|
|
|
let previousGeometry: ReturnType<typeof cloneBoxBrushGeometry> | null = null;
|
2026-03-31 02:03:30 +02:00
|
|
|
let previousSelection: EditorSelection | null = null;
|
|
|
|
|
let previousToolMode: ToolMode | null = null;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: createOpaqueId("command"),
|
2026-04-04 19:26:26 +02:00
|
|
|
label: options.label ?? "Resize box brush",
|
2026-03-31 02:03:30 +02:00
|
|
|
execute(context) {
|
|
|
|
|
const currentDocument = context.getDocument();
|
|
|
|
|
const brush = getBoxBrushOrThrow(currentDocument, options.brushId);
|
|
|
|
|
|
|
|
|
|
if (previousSize === null) {
|
|
|
|
|
previousSize = {
|
|
|
|
|
...brush.size
|
|
|
|
|
};
|
2026-04-05 02:37:46 +02:00
|
|
|
previousGeometry = cloneBoxBrushGeometry(brush.geometry);
|
2026-03-31 02:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (previousSelection === null) {
|
|
|
|
|
previousSelection = cloneSelectionForCommand(context.getSelection());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (previousToolMode === null) {
|
|
|
|
|
previousToolMode = context.getToolMode();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 02:37:46 +02:00
|
|
|
const nextGeometry = scaleBoxBrushGeometryToSize(brush.geometry, resolvedSize);
|
|
|
|
|
|
2026-03-31 02:03:30 +02:00
|
|
|
context.setDocument(
|
|
|
|
|
replaceBrush(currentDocument, {
|
|
|
|
|
...brush,
|
|
|
|
|
size: {
|
2026-04-04 19:26:26 +02:00
|
|
|
...resolvedSize
|
2026-04-05 02:37:46 +02:00
|
|
|
},
|
|
|
|
|
geometry: nextGeometry
|
2026-03-31 02:03:30 +02:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
context.setSelection(setSingleBrushSelection(options.brushId));
|
|
|
|
|
context.setToolMode("select");
|
|
|
|
|
},
|
|
|
|
|
undo(context) {
|
2026-04-05 02:37:46 +02:00
|
|
|
if (previousSize === null || previousGeometry === null) {
|
2026-03-31 02:03:30 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const currentDocument = context.getDocument();
|
|
|
|
|
const brush = getBoxBrushOrThrow(currentDocument, options.brushId);
|
|
|
|
|
|
|
|
|
|
context.setDocument(
|
|
|
|
|
replaceBrush(currentDocument, {
|
|
|
|
|
...brush,
|
|
|
|
|
size: {
|
|
|
|
|
...previousSize
|
2026-04-05 02:37:46 +02:00
|
|
|
},
|
|
|
|
|
geometry: cloneBoxBrushGeometry(previousGeometry)
|
2026-03-31 02:03:30 +02:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (previousSelection !== null) {
|
|
|
|
|
context.setSelection(previousSelection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (previousToolMode !== null) {
|
|
|
|
|
context.setToolMode(previousToolMode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|