Add snapToGrid option to create and move box brush commands
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user