Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(editor): multi-select edits keep per-entity data, in one undo step

esenginedb5c6db899b6
diff --git a/desktop/src/engine/SceneCommands.ts b/desktop/src/engine/SceneCommands.ts
index 9532c8ed0..eaf761253 100644
--- a/desktop/src/engine/SceneCommands.ts
+++ b/desktop/src/engine/SceneCommands.ts
@@ -92,12 +92,26 @@ export function toModelValue(
case 'string':
return String(value);
case 'vec2': {
+ // Only finite axes are written; a NaN slot means "keep this entity's own
+ // value" so a single-axis edit fanned across a multi-selection never
+ // clobbers the untouched axes of the non-primary entities.
const [x, y] = value as [number, number];
- return { ...(cur[key] as object), x, y };
+ const base = cur[key] as { x: number; y: number };
+ return {
+ ...base,
+ ...(Number.isFinite(x) ? { x } : {}),
+ ...(Number.isFinite(y) ? { y } : {}),
+ };
}
case 'vec3': {
const [x, y, z] = value as [number, number, number];
- return { ...(cur[key] as object), x, y, z };
+ const base = cur[key] as { x: number; y: number; z: number };
+ return {
+ ...base,
+ ...(Number.isFinite(x) ? { x } : {}),
+ ...(Number.isFinite(y) ? { y } : {}),
+ ...(Number.isFinite(z) ? { z } : {}),
+ };
}
case 'vec2list':
// A whole Vec2[] replacement (polygon vertices / chain points) — normalized to
@@ -642,32 +656,32 @@ export class SceneCommandsImpl {
});
}
- /** Duplicate an entity (offset slightly, as a sibling). Returns the new source id. */
+ /** Duplicate an entity WITH its subtree (offset slightly, as a sibling). Returns the new root source id. */
duplicateEntity(sourceId: EntityId): EntityId | null {
const src = this.model.entityBySource(sourceId);
if (!src) return null;
- // Clone the SOURCE record (preserves unknown components/fields + @uuid: refs
- // the World projection can't carry), with the standard paste offset.
- const components = structuredClone(src.components) as SceneComponent[];
- const pos = (components.find((c) => c.type === 'Transform')?.data as
- | { position?: { x: number; y: number } }
- | undefined)?.position;
- if (pos) {
- pos.x += 24;
- pos.y -= 24;
+ // Duplicate the whole subtree, re-keyed to fresh ids — the same deep path as
+ // copy+paste (remap preserves unknown components/fields + @uuid: refs and
+ // rewrites intra-subtree entity refs). A shallow clone silently drops children.
+ const payload: SceneEntity[] = [];
+ for (const sid of this.model.collectSubtree(sourceId)) {
+ const e = this.model.entityBySource(sid);
+ if (e) payload.push(e);
}
- const newSourceId = this.model.addEntity(src.name, components, src.parent ?? null);
- let record: SceneEntity | undefined;
+ const { entities, rootIds } = remapClipboardEntities(
+ payload,
+ () => this.model.allocateSourceId(),
+ src.parent ?? null,
+ { x: 24, y: -24 },
+ );
+ const apply = (): void => this.model.insertSubtree(entities);
+ apply();
this.history.record(
`Duplicate ${src.name || 'Entity'}`,
- () => {
- if (record) this.model.restoreEntity(record);
- },
- () => {
- record = this.model.removeEntityBySource(newSourceId);
- },
+ apply,
+ () => { for (const e of entities) this.model.removeEntityBySource((e as { id: EntityId }).id); },
);
- return newSourceId;
+ return rootIds[0] ?? null;
}
/**
@@ -1002,6 +1016,16 @@ export class SceneCommandsImpl {
});
}
+ /** Drag-reorder a whole selection as ONE undo step (per-id rules of {@link reorderEntity}). */
+ reorderEntities(ids: readonly EntityId[], targetId: EntityId, before: boolean): void {
+ const movable = ids.filter((id) => id !== targetId);
+ if (movable.length === 0) return;
+ if (movable.length === 1) return this.reorderEntity(movable[0], targetId, before);
+ this.history.group(`Reorder ${movable.length} Entities`, () => {
+ for (const id of movable) this.reorderEntity(id, targetId, before);
+ });
+ }
+
// The default data for a component: builtins from the engine registry; user/script
// components from the schemas.json shape; an unknown-but-named component is empty.
private defaultDataFor(compName: string): Record<string, unknown> {
diff --git a/desktop/src/panels/Details.tsx b/desktop/src/panels/Details.tsx
index 348692cbb..e3e46e15a 100644
--- a/desktop/src/panels/Details.tsx
+++ b/desktop/src/panels/Details.tsx
@@ -188,7 +188,10 @@ export function VecControl({
onBegin={onBegin}
onEnd={onEnd}
onCommit={(v) => {
- const next = value.slice();
+ // Write ONLY the edited axis — NaN on the others tells the model write
+ // to keep each (possibly multi-selected) entity's own value there,
+ // instead of stamping the primary's whole vector onto the selection.
+ const next = value.map(() => NaN);
next[i] = v;
onChange(next);
}}
diff --git a/desktop/src/panels/Outliner.tsx b/desktop/src/panels/Outliner.tsx
index 86a816cbb..e0eae1c16 100644
--- a/desktop/src/panels/Outliner.tsx
+++ b/desktop/src/panels/Outliner.tsx
@@ -477,7 +477,7 @@ export function Outliner() {
dragIds.current = null;
if (ids) {
const ordered = pos === 'before' ? ids : [...ids].reverse();
- for (const id of ordered) if (id !== item.id) SceneCommands.reorderEntity(id, item.id, pos === 'before');
+ SceneCommands.reorderEntities(ordered, item.id, pos === 'before');
}
};
diff --git a/desktop/tests/multi-select-batch.test.ts b/desktop/tests/multi-select-batch.test.ts
index 4844bba21..a0a4b6598 100644
--- a/desktop/tests/multi-select-batch.test.ts
+++ b/desktop/tests/multi-select-batch.test.ts
@@ -109,4 +109,42 @@ describe('multi-selection batching', () => {
expect(model.entityBySource(1)).toBeDefined();
expect(model.entityBySource(2)).toBeDefined();
});
+
+ it('duplicateEntity deep-copies the subtree — children come along (not just the root)', () => {
+ const dup = cmds.duplicateEntity(3); // 3 has child 4
+ expect(dup).not.toBeNull();
+ const root = model.entityBySource(dup!);
+ expect(root?.children.length).toBe(1); // the cloned child, NOT dropped
+ const childSrc = root!.children[0];
+ expect(childSrc).not.toBe(4); // a fresh source id
+ expect(model.entityBySource(childSrc)?.parent).toBe(dup);
+ // Undo/redo carry the whole subtree.
+ expect(undoDepth(history)).toBe(1);
+ history.undo();
+ expect(model.entityBySource(dup!)).toBeUndefined();
+ expect(model.entityBySource(childSrc)).toBeUndefined();
+ history.redo();
+ expect(model.entityBySource(dup!)?.children.length).toBe(1);
+ });
+
+ it('reorderEntities batches a multi-drag into one undo step', () => {
+ cmds.reorderEntities([1, 2], 3, false); // both move after 3 → real order change
+ expect(undoDepth(history)).toBe(1);
+ expect(history.undoLabel()).toBe('Reorder 2 Entities');
+ });
+
+ it('a single-axis edit fanned across a multi-selection keeps each entity\'s own other axes', () => {
+ // Give 1 and 2 distinct Y (their own values on the axis we will NOT touch).
+ cmds.setField(1, 'Transform', 'position', 'vec3', [0, 10, 0]);
+ cmds.setField(2, 'Transform', 'position', 'vec3', [0, 20, 0]);
+ // Edit only X across both — NaN on Y/Z is the "keep own value" sentinel the
+ // Inspector's VecControl emits for the untouched axes.
+ cmds.beginGesture('Edit position');
+ for (const e of [1, 2]) cmds.setField(e, 'Transform', 'position', 'vec3', [42, NaN, NaN]);
+ cmds.endGesture();
+ const pos = (id: number) =>
+ model.entityBySource(id)!.components.find((c) => c.type === 'Transform')!.data.position as { x: number; y: number };
+ expect(pos(1)).toMatchObject({ x: 42, y: 10 }); // kept its own Y
+ expect(pos(2)).toMatchObject({ x: 42, y: 20 }); // kept its own Y (not clobbered to 10)
+ });
});