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): page switches project gear values into the MODEL, not just the world

esengine74ed749c8b2f
diff --git a/desktop/src/engine/SceneCommands.ts b/desktop/src/engine/SceneCommands.ts
index 13b8753c7..ce199af1d 100644
--- a/desktop/src/engine/SceneCommands.ts
+++ b/desktop/src/engine/SceneCommands.ts
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import type { SceneData, PrefabData, ControllerState, UIControllerData, GearBinding, GearTween, UIGearData } from 'esengine';
-import { TilemapAPI, TilemapLiveSync, UIPositionType, DimensionUnit, anchorPresetFields, type AnchorPreset } from 'esengine';
+import { TilemapAPI, TilemapLiveSync, UIPositionType, DimensionUnit, anchorPresetFields, writeFieldPath, type AnchorPreset } from 'esengine';
import type { EntityId, InspectorFieldType, InspectorFieldValue } from '@/types';
import { EditorHistory, EditorHistoryImpl } from './EditorHistory';
import { SceneModel, SceneModelImpl } from './SceneModel';
@@ -1260,7 +1260,40 @@ export class SceneCommandsImpl {
const cur = this.controllersOf(id);
const ctrl = cur.find((c) => c.name === name);
if (!ctrl || ctrl.current === page || !ctrl.pages.includes(page)) return;
- this.writeControllers(id, 'Set Page', cur.map((c) => (c.name === name ? { ...c, current: page } : c)));
+ const next = cur.map((c) => (c.name === name ? { ...c, current: page } : c));
+
+ // Project the page's authored gear values into the MODEL, not just the world:
+ // the model is the document — Details and the saved scene must show the
+ // selected page's values, while the runtime gear-apply system mirrors them
+ // (with tween) in the world. Raw model.setField: bypassing the command door
+ // keeps an armed ControllerRecorder from re-capturing its own projection.
+ const edits: Array<{ id: EntityId; comp: string; key: string; before: unknown; after: unknown }> = [];
+ for (const gid of this.subtreeWithGears(id)) {
+ if (!this.resolvesTo(gid, id, name)) continue;
+ for (const b of this.gearBindingsOf(gid)) {
+ if (b.controller !== name) continue;
+ const target = b.pages[page];
+ if (target === undefined) continue;
+ const data = this.model.entityBySource(gid)?.components.find((c) => c.type === b.component)?.data as
+ Record<string, unknown> | undefined;
+ if (!data) continue;
+ const key = b.property.split('.')[0]!;
+ const scratch = structuredClone(data);
+ if (!writeFieldPath(scratch, b.property, target)) continue;
+ if (valueEqual(scratch[key], data[key])) continue;
+ edits.push({
+ id: gid, comp: b.component, key,
+ before: structuredClone(data[key]), after: structuredClone(scratch[key]),
+ });
+ }
+ }
+
+ const apply = (ctrls: ControllerState[], pick: 'before' | 'after') => {
+ this.model.setField(id, 'UIController', 'controllers', structuredClone(ctrls));
+ for (const e of edits) this.model.setField(e.id, e.comp, e.key, structuredClone(pick === 'after' ? e.after : e.before));
+ };
+ apply(next, 'after');
+ this.history.record('Set Page', () => apply(next, 'after'), () => apply(cur, 'before'));
}
addControllerPage(id: EntityId, name: string, page: string): void {
diff --git a/desktop/tests/controller-commands.test.ts b/desktop/tests/controller-commands.test.ts
index e18736e6a..034f41d8e 100644
--- a/desktop/tests/controller-commands.test.ts
+++ b/desktop/tests/controller-commands.test.ts
@@ -122,6 +122,23 @@ describe.skipIf(!HAS_WASM)('controller + gear commands (cascades)', () => {
expect(gearsOf(leaf)[0]!.pages.b).toEqual({ r: 0, g: 1, b: 0, a: 1 });
});
+ it('setControllerPage projects the page values into the MODEL fields (undo restores both)', () => {
+ const { root, leaf } = makeTree();
+ S.commands.addComponent(leaf, 'UIVisual');
+ const colorOf = () =>
+ (S.model.entityBySource(leaf)?.components.find((c) => c.type === 'UIVisual')?.data as
+ { color: { r: number; g: number } }).color;
+ const white = structuredClone(colorOf());
+
+ S.commands.setControllerPage(root, 'tab', 'b');
+ expect(controllersOf(root)[0]!.current).toBe('b');
+ expect(colorOf()).toEqual({ r: 0, g: 1, b: 0, a: 1 }); // page b's authored green
+
+ S.history.undo();
+ expect(controllersOf(root)[0]!.current).toBe('a');
+ expect(colorOf()).toEqual(white); // the field write undoes with the page switch
+ });
+
it('refuses to remove the last page', () => {
const { root } = makeTree();
S.commands.removeControllerPage(root, 'tab', 'b');
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 176b1222b..387c341e7 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/index
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 1204 stable · 17 beta · 1 deprecated
+Symbols: 1206 stable · 17 beta · 1 deprecated
## ABI_LAYOUT_HASH — const
```
@@ -8742,6 +8742,11 @@ PrefabsPlugin
(world: World, entity: number): ColliderInstance[]
```
+## readFieldPath — function
+```
+(data: Record<string, any>, path: string): unknown
+```
+
## readPtrField — function
```
(f32: Float32Array, u32: Uint32Array, u8: Uint8Array, ptr: number, field: PtrFieldDesc): unknown
@@ -9412,6 +9417,11 @@ VideoPlugin
(app: App, sceneName: string, system: SystemDef): SystemDef
```
+## writeFieldPath — function
+```
+(data: Record<string, any>, path: string, value: unknown): boolean
+```
+
## writePtrField — function
```
(f32: Float32Array, u32: Uint32Array, u8: Uint8Array, ptr: number, field: PtrFieldDesc, value: PtrFieldValue | unknown): void
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 5ac6601d1..cef77ba55 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/index.node
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 1203 stable · 19 beta · 1 deprecated
+Symbols: 1205 stable · 19 beta · 1 deprecated
## ANCHOR_AXES — const
```
@@ -8747,6 +8747,11 @@ PrefabsPlugin
(world: World, entity: number): ColliderInstance[]
```
+## readFieldPath — function
+```
+(data: Record<string, any>, path: string): unknown
+```
+
## readPtrField — function
```
(f32: Float32Array, u32: Uint32Array, u8: Uint8Array, ptr: number, field: PtrFieldDesc): unknown
@@ -9417,6 +9422,11 @@ VideoPlugin
(app: App, sceneName: string, system: SystemDef): SystemDef
```
+## writeFieldPath — function
+```
+(data: Record<string, any>, path: string, value: unknown): boolean
+```
+
## writePtrField — function
```
(f32: Float32Array, u32: Uint32Array, u8: Uint8Array, ptr: number, field: PtrFieldDesc, value: PtrFieldValue | unknown): void
diff --git a/sdk/src/core-ui.ts b/sdk/src/core-ui.ts
index a292f0adb..a94237e29 100644
--- a/sdk/src/core-ui.ts
+++ b/sdk/src/core-ui.ts
@@ -123,6 +123,8 @@ export {
getControllerPage,
findControllerOwner,
bindControllerPage,
+ readFieldPath,
+ writeFieldPath,
type UIControllerData,
type ControllerState,
type UIGearData,