Add snapToGrid option to create and move box brush commands

This commit is contained in:
2026-04-04 19:26:18 +02:00
parent cc9b200bae
commit c19dae0df6
2 changed files with 16 additions and 5 deletions

View File

@@ -12,13 +12,21 @@ import type { EditorCommand } from "./command";
interface CreateBoxBrushCommandOptions { interface CreateBoxBrushCommandOptions {
center?: Vec3; center?: Vec3;
size?: Vec3; size?: Vec3;
snapToGrid?: boolean;
gridSize?: number; gridSize?: number;
} }
export function createCreateBoxBrushCommand(options: CreateBoxBrushCommandOptions = {}): EditorCommand { export function createCreateBoxBrushCommand(options: CreateBoxBrushCommandOptions = {}): EditorCommand {
const snapToGrid = options.snapToGrid ?? true;
const brush = createBoxBrush({ const brush = createBoxBrush({
center: snapVec3ToGrid(options.center ?? DEFAULT_BOX_BRUSH_CENTER, options.gridSize ?? DEFAULT_GRID_SIZE), center:
size: snapPositiveSizeToGrid(options.size ?? DEFAULT_BOX_BRUSH_SIZE, options.gridSize ?? DEFAULT_GRID_SIZE) snapToGrid === false
? options.center ?? DEFAULT_BOX_BRUSH_CENTER
: snapVec3ToGrid(options.center ?? DEFAULT_BOX_BRUSH_CENTER, options.gridSize ?? DEFAULT_GRID_SIZE),
size:
snapToGrid === false
? options.size ?? DEFAULT_BOX_BRUSH_SIZE
: snapPositiveSizeToGrid(options.size ?? DEFAULT_BOX_BRUSH_SIZE, options.gridSize ?? DEFAULT_GRID_SIZE)
}); });
let previousSelection: EditorSelection | null = null; let previousSelection: EditorSelection | null = null;

View File

@@ -11,11 +11,14 @@ import type { EditorCommand } from "./command";
interface MoveBoxBrushCommandOptions { interface MoveBoxBrushCommandOptions {
brushId: string; brushId: string;
center: Vec3; center: Vec3;
snapToGrid?: boolean;
gridSize?: number; gridSize?: number;
label?: string;
} }
export function createMoveBoxBrushCommand(options: MoveBoxBrushCommandOptions): EditorCommand { export function createMoveBoxBrushCommand(options: MoveBoxBrushCommandOptions): EditorCommand {
const snappedCenter = snapVec3ToGrid(options.center, options.gridSize ?? DEFAULT_GRID_SIZE); const resolvedCenter =
options.snapToGrid === false ? options.center : snapVec3ToGrid(options.center, options.gridSize ?? DEFAULT_GRID_SIZE);
let previousCenter: Vec3 | null = null; let previousCenter: Vec3 | null = null;
let previousSelection: EditorSelection | null = null; let previousSelection: EditorSelection | null = null;
@@ -23,7 +26,7 @@ export function createMoveBoxBrushCommand(options: MoveBoxBrushCommandOptions):
return { return {
id: createOpaqueId("command"), id: createOpaqueId("command"),
label: "Move box brush", label: options.label ?? "Move box brush",
execute(context) { execute(context) {
const currentDocument = context.getDocument(); const currentDocument = context.getDocument();
const brush = getBoxBrushOrThrow(currentDocument, options.brushId); const brush = getBoxBrushOrThrow(currentDocument, options.brushId);
@@ -46,7 +49,7 @@ export function createMoveBoxBrushCommand(options: MoveBoxBrushCommandOptions):
replaceBrush(currentDocument, { replaceBrush(currentDocument, {
...brush, ...brush,
center: { center: {
...snappedCenter ...resolvedCenter
} }
}) })
); );