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.

feat(editor): expose selection on the EditorControlSurface

esengine06b87d1fdc25
diff --git a/desktop/src/engine/EditorControlSurface.ts b/desktop/src/engine/EditorControlSurface.ts
index 7262f9772..f18a8eb5a 100644
--- a/desktop/src/engine/EditorControlSurface.ts
+++ b/desktop/src/engine/EditorControlSurface.ts
@@ -31,6 +31,7 @@ import type { SceneQueryImpl } from './SceneQuery';
import type { SceneModelImpl } from './SceneModel';
import type { EditorHistoryImpl } from './EditorHistory';
import type { ReconcilerImpl } from './Reconciler';
+import type { SelectionStore } from '@/store/selectionStore';
/** A captured viewport frame: raw RGBA pixels (GL order: bottom-up rows). */
export interface ViewportCapture {
@@ -46,6 +47,7 @@ export interface SurfaceSession {
commands: SceneCommandsImpl;
query: SceneQueryImpl;
reconciler: ReconcilerImpl;
+ selection: SelectionStore;
}
export class EditorControlSurfaceImpl {
@@ -229,6 +231,33 @@ export class EditorControlSurfaceImpl {
return this.s.model.serialize();
}
+ // =========================================================================
+ // Selection — the active entity(s). Stable source ids, self-healing on
+ // removal (the store drops a despawned id). The canonical select primitive
+ // for the headless host + MCP ("select, then inspect / modify").
+ // =========================================================================
+
+ /** The primary selected entity (drives Details + gizmo), or null. */
+ getSelection(): EntityId | null {
+ return this.s.selection.getState().selectedId;
+ }
+ /** The full multi-selection, primary last is not guaranteed; use getSelection() for the active one. */
+ getSelectionIds(): EntityId[] {
+ return [...this.s.selection.getState().selectedIds];
+ }
+ /** Replace the selection with one entity, or clear it with null. */
+ select(id: EntityId | null): void {
+ this.s.selection.getState().select(id);
+ }
+ /** Replace the selection with a set + a primary (box / shift-select). */
+ selectMany(ids: EntityId[], primary: EntityId): void {
+ this.s.selection.getState().selectMany(ids, primary);
+ }
+ /** Subscribe to selection changes. Returns an unsubscribe. */
+ subscribeSelection(fn: () => void): () => void {
+ return this.s.selection.subscribe(fn);
+ }
+
// =========================================================================
// Observation — runtime evidence for verification (the headless-gap closer)
// =========================================================================
diff --git a/desktop/tests/editor-control-surface.test.ts b/desktop/tests/editor-control-surface.test.ts
index 34731f7ce..323146d7c 100644
--- a/desktop/tests/editor-control-surface.test.ts
+++ b/desktop/tests/editor-control-surface.test.ts
@@ -125,4 +125,30 @@ describe.skipIf(!HAS_WASM)('EditorControlSurface (headless World)', () => {
S.surface.undo();
expect(S.model.entityOrder()).toEqual([a, b]);
});
+
+ it('selection — select / multi / clear, and self-heals when the selected entity is deleted', () => {
+ const a = S.surface.addEntity()!;
+ const b = S.surface.addEntity()!;
+ S.surface.select(a);
+ expect(S.surface.getSelection()).toBe(a);
+ S.surface.selectMany([a, b], b);
+ expect(S.surface.getSelection()).toBe(b);
+ expect([...S.surface.getSelectionIds()].sort((x, y) => x - y)).toEqual([a, b].sort((x, y) => x - y));
+ S.surface.deleteEntity(b); // the model drops b from the selection
+ expect(S.surface.getSelection()).not.toBe(b);
+ S.surface.select(null);
+ expect(S.surface.getSelection()).toBeNull();
+ });
+
+ it('subscribeSelection fires on change and stops after unsubscribe', () => {
+ const a = S.surface.addEntity()!;
+ let n = 0;
+ const off = S.surface.subscribeSelection(() => { n++; });
+ S.surface.select(a);
+ expect(n).toBeGreaterThan(0);
+ off();
+ const before = n;
+ S.surface.select(null);
+ expect(n).toBe(before);
+ });
});