Add commands and update brush face handling

This commit is contained in:
2026-03-31 02:33:18 +02:00
parent d1ae2499e5
commit 8760b210d4
9 changed files with 513 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
import { cloneEditorSelection, type EditorSelection } from "../core/selection";
import type { BoxBrush } from "../document/brushes";
import { cloneFaceUvState, type BoxBrush, type BoxFaceId, type BrushFace } from "../document/brushes";
import type { SceneDocument } from "../document/scene-document";
export function getBoxBrushOrThrow(document: SceneDocument, brushId: string): BoxBrush {
@@ -23,6 +23,14 @@ export function setSingleBrushSelection(brushId: string): EditorSelection {
};
}
export function setSingleBrushFaceSelection(brushId: string, faceId: BoxFaceId): EditorSelection {
return {
kind: "brushFace",
brushId,
faceId
};
}
export function cloneSelectionForCommand(selection: EditorSelection): EditorSelection {
return cloneEditorSelection(selection);
}
@@ -48,3 +56,29 @@ export function removeBrush(document: SceneDocument, brushId: string): SceneDocu
brushes: remainingBrushes
};
}
export function getBoxBrushFaceOrThrow(document: SceneDocument, brushId: string, faceId: BoxFaceId): BrushFace {
const brush = getBoxBrushOrThrow(document, brushId);
const face = brush.faces[faceId];
if (face === undefined) {
throw new Error(`Box brush ${brushId} does not contain face ${faceId}.`);
}
return face;
}
export function replaceBoxBrushFace(document: SceneDocument, brushId: string, faceId: BoxFaceId, face: BrushFace): SceneDocument {
const brush = getBoxBrushOrThrow(document, brushId);
return replaceBrush(document, {
...brush,
faces: {
...brush.faces,
[faceId]: {
materialId: face.materialId,
uv: cloneFaceUvState(face.uv)
}
}
});
}

View File

@@ -0,0 +1,81 @@
import type { ToolMode } from "../core/tool-mode";
import { createOpaqueId } from "../core/ids";
import type { EditorSelection } from "../core/selection";
import type { BoxFaceId } from "../document/brushes";
import {
cloneSelectionForCommand,
getBoxBrushFaceOrThrow,
replaceBoxBrushFace,
setSingleBrushFaceSelection
} from "./brush-command-helpers";
import type { EditorCommand } from "./command";
interface SetBoxBrushFaceMaterialCommandOptions {
brushId: string;
faceId: BoxFaceId;
materialId: string | null;
}
export function createSetBoxBrushFaceMaterialCommand(options: SetBoxBrushFaceMaterialCommandOptions): EditorCommand {
let previousMaterialId: string | null | undefined;
let previousSelection: EditorSelection | null = null;
let previousToolMode: ToolMode | null = null;
return {
id: createOpaqueId("command"),
label: options.materialId === null ? `Clear ${options.faceId} face material` : `Apply material to ${options.faceId} face`,
execute(context) {
const currentDocument = context.getDocument();
const currentFace = getBoxBrushFaceOrThrow(currentDocument, options.brushId, options.faceId);
if (options.materialId !== null && currentDocument.materials[options.materialId] === undefined) {
throw new Error(`Material ${options.materialId} does not exist in the document registry.`);
}
if (previousMaterialId === undefined) {
previousMaterialId = currentFace.materialId;
}
if (previousSelection === null) {
previousSelection = cloneSelectionForCommand(context.getSelection());
}
if (previousToolMode === null) {
previousToolMode = context.getToolMode();
}
context.setDocument(
replaceBoxBrushFace(currentDocument, options.brushId, options.faceId, {
...currentFace,
materialId: options.materialId
})
);
context.setSelection(setSingleBrushFaceSelection(options.brushId, options.faceId));
context.setToolMode("select");
},
undo(context) {
if (previousMaterialId === undefined) {
return;
}
const currentDocument = context.getDocument();
const currentFace = getBoxBrushFaceOrThrow(currentDocument, options.brushId, options.faceId);
context.setDocument(
replaceBoxBrushFace(currentDocument, options.brushId, options.faceId, {
...currentFace,
materialId: previousMaterialId
})
);
if (previousSelection !== null) {
context.setSelection(previousSelection);
}
if (previousToolMode !== null) {
context.setToolMode(previousToolMode);
}
}
};
}

View File

@@ -0,0 +1,78 @@
import type { ToolMode } from "../core/tool-mode";
import { createOpaqueId } from "../core/ids";
import type { EditorSelection } from "../core/selection";
import { cloneFaceUvState, type BoxFaceId, type FaceUvState } from "../document/brushes";
import {
cloneSelectionForCommand,
getBoxBrushFaceOrThrow,
replaceBoxBrushFace,
setSingleBrushFaceSelection
} from "./brush-command-helpers";
import type { EditorCommand } from "./command";
interface SetBoxBrushFaceUvStateCommandOptions {
brushId: string;
faceId: BoxFaceId;
uvState: FaceUvState;
label?: string;
}
export function createSetBoxBrushFaceUvStateCommand(options: SetBoxBrushFaceUvStateCommandOptions): EditorCommand {
let previousUvState: FaceUvState | null = null;
let previousSelection: EditorSelection | null = null;
let previousToolMode: ToolMode | null = null;
return {
id: createOpaqueId("command"),
label: options.label ?? `Update ${options.faceId} face UVs`,
execute(context) {
const currentDocument = context.getDocument();
const currentFace = getBoxBrushFaceOrThrow(currentDocument, options.brushId, options.faceId);
if (previousUvState === null) {
previousUvState = cloneFaceUvState(currentFace.uv);
}
if (previousSelection === null) {
previousSelection = cloneSelectionForCommand(context.getSelection());
}
if (previousToolMode === null) {
previousToolMode = context.getToolMode();
}
context.setDocument(
replaceBoxBrushFace(currentDocument, options.brushId, options.faceId, {
...currentFace,
uv: cloneFaceUvState(options.uvState)
})
);
context.setSelection(setSingleBrushFaceSelection(options.brushId, options.faceId));
context.setToolMode("select");
},
undo(context) {
if (previousUvState === null) {
return;
}
const currentDocument = context.getDocument();
const currentFace = getBoxBrushFaceOrThrow(currentDocument, options.brushId, options.faceId);
context.setDocument(
replaceBoxBrushFace(currentDocument, options.brushId, options.faceId, {
...currentFace,
uv: cloneFaceUvState(previousUvState)
})
);
if (previousSelection !== null) {
context.setSelection(previousSelection);
}
if (previousToolMode !== null) {
context.setToolMode(previousToolMode);
}
}
};
}