44 lines
2.5 KiB
TypeScript
44 lines
2.5 KiB
TypeScript
import type { WebEngineEditCommand } from "./web-engine";
|
|
|
|
type CoreCommand<T extends WebEngineEditCommand["type"]> = Extract<WebEngineEditCommand, { type: T }>;
|
|
type Vector3 = [number, number, number];
|
|
|
|
const vector3 = (value: readonly number[]): Vector3 => [value[0] ?? 0, value[1] ?? 0, value[2] ?? 0];
|
|
|
|
/** Typed constructors used by the workbench for the source-backed scene API. */
|
|
export const coreSceneCommands = Object.freeze({
|
|
createCube(location: readonly number[] = [0, 0, 0], name?: string): CoreCommand<"createPrimitive"> {
|
|
return { type: "createPrimitive", primitive: "CUBE", location: vector3(location), ...(name ? { name } : {}) };
|
|
},
|
|
duplicate(objectId: string, offset: readonly number[] = [0.25, 0.25, 0], linked = false): CoreCommand<"duplicateObject"> {
|
|
return { type: "duplicateObject", objectId, offset: vector3(offset), ...(linked ? { linked: true } : {}) };
|
|
},
|
|
delete(objectId: string): CoreCommand<"deleteObject"> {
|
|
return { type: "deleteObject", objectId };
|
|
},
|
|
visibility(objectId: string, visible: boolean): CoreCommand<"setObjectVisibility"> {
|
|
return { type: "setObjectVisibility", objectId, visible };
|
|
},
|
|
transform(objectId: string, translation: readonly number[], rotationEuler: readonly number[], scale: readonly number[]): CoreCommand<"setObjectTransform"> {
|
|
return { type: "setObjectTransform", objectId, translation: vector3(translation), rotationEuler: vector3(rotationEuler), scale: vector3(scale) };
|
|
},
|
|
parent(objectId: string, parentId: string | null, keepTransform = true): CoreCommand<"setParent"> {
|
|
return { type: "setParent", objectId, parentId, keepTransform };
|
|
},
|
|
createCollection(name: string, parentCollectionId?: string | null): CoreCommand<"createCollection"> {
|
|
return { type: "createCollection", name, ...(parentCollectionId === undefined ? {} : { parentCollectionId }) };
|
|
},
|
|
moveToCollection(objectId: string, collectionId: string): CoreCommand<"moveObjectToCollection"> {
|
|
return { type: "moveObjectToCollection", objectId, collectionId };
|
|
},
|
|
rename(id: string, name: string): CoreCommand<"renameId"> {
|
|
return { type: "renameId", id, name };
|
|
},
|
|
applyTransform(objectId: string): CoreCommand<"applyObjectTransform"> {
|
|
return { type: "applyObjectTransform", objectId };
|
|
},
|
|
setOrigin(objectId: string, mode: CoreCommand<"setObjectOrigin">["mode"] = "GEOMETRY"): CoreCommand<"setObjectOrigin"> {
|
|
return { type: "setObjectOrigin", objectId, mode };
|
|
},
|
|
} satisfies Record<string, (...args: never[]) => WebEngineEditCommand>);
|