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): render-scoped visibility + single-undo multi add/remove

esengineee6df81c1e1d
diff --git a/desktop/src/engine/EditorHistory.ts b/desktop/src/engine/EditorHistory.ts
index 80b93f786..2144f68e5 100644
--- a/desktop/src/engine/EditorHistory.ts
+++ b/desktop/src/engine/EditorHistory.ts
@@ -31,6 +31,16 @@ export class EditorHistoryImpl {
this.bump();
}
+ /** Register several already-applied mutations as ONE undo step (e.g. a
+ * multi-selection add/remove). No-op on an empty op list. */
+ batch(label: string, ops: ReadonlyArray<{ forward: () => void; reverse: () => void }>) {
+ if (ops.length === 0) return;
+ const tx = this.tm.begin(label);
+ for (const op of ops) tx.addDeferred(op);
+ this.tm.commit(tx);
+ this.bump();
+ }
+
undo() {
if (this.tm.undo()) this.bump();
}
diff --git a/desktop/src/engine/SceneCommands.ts b/desktop/src/engine/SceneCommands.ts
index 768c8db4f..03806a276 100644
--- a/desktop/src/engine/SceneCommands.ts
+++ b/desktop/src/engine/SceneCommands.ts
@@ -9,6 +9,8 @@ import { expandInstance } from './PrefabInstance';
import {
componentByName,
componentDefaults,
+ componentEnable,
+ isRenderComponent,
userSchema,
angleZToQuat,
hexToRgb,
@@ -17,6 +19,8 @@ import {
type SceneEntity = SceneData['entities'][number];
type SceneComponent = SceneEntity['components'][number];
+/** A reversible model mutation: `forward` (re)applies it, `reverse` undoes it. */
+type UndoOp = { forward: () => void; reverse: () => void };
/** A single tile edit: set the tile at grid (x, y) to `tileId` (0 = erase). */
export interface TilePaint {
@@ -445,9 +449,11 @@ export class SceneCommandsImpl {
}
/** Add a component (with its registered/schema defaults) to an entity. Undoable. */
- addComponent(sourceId: EntityId, compName: string): void {
+ // Apply an add to the model and return its undo op, or null if it's a no-op
+ // (entity gone / component already present). Shared by the single + batch paths.
+ private addComponentOp(sourceId: EntityId, compName: string): UndoOp | null {
const entity = this.model.entityBySource(sourceId);
- if (!entity || entity.components.some((c) => c.type === compName)) return;
+ if (!entity || entity.components.some((c) => c.type === compName)) return null;
const def = componentByName(compName);
// Builtins default from the engine registry; user/script components from the
// schemas.json shape; an unknown-but-named component starts empty.
@@ -455,25 +461,45 @@ export class SceneCommandsImpl {
? structuredClone(componentDefaults(def))
: structuredClone(userSchema(compName)?.default ?? {});
this.model.setComponent(sourceId, compName, data);
- this.history.record(
- `Add ${prettyLabel(compName)}`,
- () => this.model.setComponent(sourceId, compName, structuredClone(data)),
- () => this.model.removeComponent(sourceId, compName),
- );
+ return {
+ forward: () => this.model.setComponent(sourceId, compName, structuredClone(data)),
+ reverse: () => this.model.removeComponent(sourceId, compName),
+ };
}
- /** Remove a component from an entity (Transform / Name are protected). Undoable. */
- removeComponent(sourceId: EntityId, compName: string): void {
- if (compName === 'Transform' || compName === 'Name') return;
+ private removeComponentOp(sourceId: EntityId, compName: string): UndoOp | null {
+ if (compName === 'Transform' || compName === 'Name') return null; // protected
const comp = this.model.entityBySource(sourceId)?.components.find((c) => c.type === compName);
- if (!comp) return;
+ if (!comp) return null;
const data = structuredClone(comp.data);
this.model.removeComponent(sourceId, compName);
- this.history.record(
- `Remove ${prettyLabel(compName)}`,
- () => this.model.removeComponent(sourceId, compName),
- () => this.model.setComponent(sourceId, compName, structuredClone(data)),
- );
+ return {
+ forward: () => this.model.removeComponent(sourceId, compName),
+ reverse: () => this.model.setComponent(sourceId, compName, structuredClone(data)),
+ };
+ }
+
+ addComponent(sourceId: EntityId, compName: string): void {
+ const op = this.addComponentOp(sourceId, compName);
+ if (op) this.history.record(`Add ${prettyLabel(compName)}`, op.forward, op.reverse);
+ }
+
+ /** Remove a component from an entity (Transform / Name are protected). Undoable. */
+ removeComponent(sourceId: EntityId, compName: string): void {
+ const op = this.removeComponentOp(sourceId, compName);
+ if (op) this.history.record(`Remove ${prettyLabel(compName)}`, op.forward, op.reverse);
+ }
+
+ /** Add a component to many entities (multi-select) as ONE undo step. */
+ addComponentMany(sourceIds: readonly EntityId[], compName: string): void {
+ const ops = sourceIds.map((id) => this.addComponentOp(id, compName)).filter((o): o is UndoOp => !!o);
+ this.history.batch(`Add ${prettyLabel(compName)}`, ops);
+ }
+
+ /** Remove a component from many entities (multi-select) as ONE undo step. */
+ removeComponentMany(sourceIds: readonly EntityId[], compName: string): void {
+ const ops = sourceIds.map((id) => this.removeComponentOp(id, compName)).filter((o): o is UndoOp => !!o);
+ this.history.batch(`Remove ${prettyLabel(compName)}`, ops);
}
/**
@@ -485,11 +511,12 @@ export class SceneCommandsImpl {
const entity = this.model.entityBySource(sourceId);
if (!entity) return;
this.beginGesture(visible ? 'Show' : 'Hide');
+ // Toggle only RENDER components' enable flag (each via its own key — Sprite
+ // uses `enabled`, TilemapLayer `visible`), so hiding never disables physics.
for (const comp of entity.components) {
- const data = comp.data as Record<string, unknown>;
- if (data && typeof data === 'object' && 'enabled' in data) {
- this.setField(sourceId, comp.type, 'enabled', 'bool', visible);
- }
+ if (!isRenderComponent(comp.type)) continue;
+ const en = componentEnable(comp.type, comp.data as Record<string, unknown>);
+ if (en) this.setField(sourceId, comp.type, en.key, 'bool', visible);
}
this.endGesture();
}
diff --git a/desktop/src/engine/schema.ts b/desktop/src/engine/schema.ts
index 435c42260..31a7bca81 100644
--- a/desktop/src/engine/schema.ts
+++ b/desktop/src/engine/schema.ts
@@ -34,6 +34,16 @@ export type AnyComp = Parameters<WorldT['has']>[1];
// Structural/relationship components that drive the tree, not the inspector.
const HIDDEN_COMPONENTS = new Set(['Parent', 'Children', 'Name']);
+// Components whose enable flag drives the entity's RENDER visibility (the Outliner
+// eye + hidden state). Disabling a non-render component (physics, audio, a script)
+// turns that behaviour off without hiding the entity.
+const RENDER_COMPONENTS = new Set([
+ 'Sprite', 'ShapeRenderer', 'SpineAnimation', 'BitmapText', 'TilemapLayer', 'ParticleEmitter',
+]);
+/** Whether a component's enable flag participates in the entity's render visibility. */
+export function isRenderComponent(name: string): boolean {
+ return RENDER_COMPONENTS.has(name);
+}
// Computed world-space mirrors on Transform — never editable.
const DERIVED_FIELDS = new Set(['worldPosition', 'worldRotation', 'worldScale']);
// Inspector display order; anything not listed follows in registration order.
@@ -418,11 +428,13 @@ export function modelNameOf(entity: SceneEntityLike, kind: NodeKind): string {
return entity.name || `${cap(kind)} ${entity.id}`;
}
-/** A source entity reads as hidden if any component is explicitly disabled. */
+/** Hidden iff a RENDER component is disabled — a disabled physics/audio/script
+ * component leaves the entity visible (only its behaviour is off). */
export function modelIsVisible(entity: SceneEntityLike): boolean {
for (const c of entity.components) {
- const d = c.data as Record<string, unknown>;
- if (d && typeof d === 'object' && 'enabled' in d && d.enabled === false) return false;
+ if (!RENDER_COMPONENTS.has(c.type)) continue;
+ const en = componentEnable(c.type, c.data as Record<string, unknown>);
+ if (en && !en.value) return false;
}
return true;
}
diff --git a/desktop/src/panels/Details.tsx b/desktop/src/panels/Details.tsx
index 0446a101b..cf5b97cf8 100644
--- a/desktop/src/panels/Details.tsx
+++ b/desktop/src/panels/Details.tsx
@@ -871,9 +871,6 @@ function EditorDetails() {
[selectedId, selectedIds],
);
const multi = ids.length > 1;
- // Apply a structural command to every selected entity. Field edits coalesce into
- // one undo step via the gesture; add/remove component record per entity.
- const fanOut = (action: (id: EntityId) => void) => ids.forEach(action);
const [query, setQuery] = useState('');
const [collapsed, setCollapsed] = useState<Set<string>>(new Set());
@@ -1040,7 +1037,7 @@ function EditorDetails() {
label: ids.length > 1 ? `Remove Component (${ids.length})` : 'Remove Component',
danger: true,
icon: <Trash2 size={13} strokeWidth={1.9} />,
- onClick: () => fanOut((id) => SceneCommands.removeComponent(id, compMenu.comp)),
+ onClick: () => SceneCommands.removeComponentMany(ids, compMenu.comp),
},
]}
onClose={() => setCompMenu(null)}
@@ -1049,7 +1046,7 @@ function EditorDetails() {
{addOpen && modelEntity && (
<AddComponentMenu
entries={modelAddableComponentEntries(modelEntity)}
- onAdd={(name) => fanOut((id) => SceneCommands.addComponent(id, name))}
+ onAdd={(name) => SceneCommands.addComponentMany(ids, name)}
onClose={() => setAddOpen(false)}
/>
)}
diff --git a/desktop/tests/inspector-model.test.ts b/desktop/tests/inspector-model.test.ts
index 1af8fcde8..fbc45d713 100644
--- a/desktop/tests/inspector-model.test.ts
+++ b/desktop/tests/inspector-model.test.ts
@@ -374,6 +374,61 @@ describe('Advanced fields (D5)', () => {
});
});
+describe('Polish: render-scoped visibility + batch undo', () => {
+ const TF = { position: { x: 0, y: 0, z: 0 }, rotation: { w: 1, x: 0, y: 0, z: 0 }, scale: { x: 1, y: 1, z: 1 } };
+
+ it('a disabled non-render component leaves the entity visible; a disabled renderer hides it', () => {
+ const S = EditorSession.create();
+ S.model.adopt(
+ {
+ version: '1.0',
+ name: 'v',
+ entities: [
+ {
+ id: 1,
+ name: 'A',
+ parent: null,
+ children: [],
+ components: [{ type: 'Sprite', data: { enabled: true } }, { type: 'RigidBody', data: { enabled: false } }],
+ },
+ { id: 2, name: 'B', parent: null, children: [], components: [{ type: 'Sprite', data: { enabled: false } }] },
+ ],
+ } as unknown as SceneData,
+ new Map([[1, 1], [2, 2]]),
+ );
+ const tree = S.query.readSceneTree();
+ expect(tree.find((n) => n.id === 1)!.visible).toBe(true); // physics off, still rendered
+ expect(tree.find((n) => n.id === 2)!.visible).toBe(false); // sprite off → hidden
+ });
+
+ it('multi-select add then remove component is one undo step each', () => {
+ const S = EditorSession.create();
+ S.model.adopt(
+ {
+ version: '1.0',
+ name: 'm',
+ entities: [
+ { id: 1, name: 'A', parent: null, children: [], components: [{ type: 'Transform', data: TF }] },
+ { id: 2, name: 'B', parent: null, children: [], components: [{ type: 'Transform', data: TF }] },
+ ],
+ } as unknown as SceneData,
+ new Map([[1, 1], [2, 2]]),
+ );
+ const hasSprite = (id: number) => S.query.readInspector(id).some((c) => c.name === 'Sprite');
+
+ S.commands.addComponentMany([1, 2], 'Sprite');
+ expect([hasSprite(1), hasSprite(2)]).toEqual([true, true]);
+ S.history.undo(); // ONE step removes it from both
+ expect([hasSprite(1), hasSprite(2)]).toEqual([false, false]);
+
+ S.commands.addComponentMany([1, 2], 'Sprite');
+ S.commands.removeComponentMany([1, 2], 'Sprite');
+ expect([hasSprite(1), hasSprite(2)]).toEqual([false, false]);
+ S.history.undo(); // ONE step restores it on both
+ expect([hasSprite(1), hasSprite(2)]).toEqual([true, true]);
+ });
+});
+
describe('Category grouping (D7)', () => {
it('groups builtin fields by category (ParticleEmitter)', () => {
const S = EditorSession.create();