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): an Absolute UI widget can actually be dragged

yhhc8addc73ecb1
diff --git a/desktop/src/engine/SceneCommands.ts b/desktop/src/engine/SceneCommands.ts
index 225f08d33..9b8e79c7e 100644
--- a/desktop/src/engine/SceneCommands.ts
+++ b/desktop/src/engine/SceneCommands.ts
@@ -227,10 +227,23 @@ export class SceneCommandsImpl {
return () => this.editHooks.delete(fn);
}
- /** The current model value of one field, or undefined. */
+ /**
+ * The current model value of one field: the stored value, else the component's
+ * registered default. `undefined` means the ENTITY DOES NOT HAVE THE COMPONENT —
+ * which several callers rely on ("is this a UINode?").
+ *
+ * The default matters because scene data stores only what was authored: a field
+ * left alone is simply absent, not empty. Reading the raw data made every such
+ * field look unset, and a caller that needs a value gave up — an Absolute UINode
+ * could not be dragged in the viewport at all, because the inset shift bails
+ * unless it can read BOTH sides of the axis and the far side is almost never
+ * authored (it sits at `auto`).
+ */
private modelFieldValue(sourceId: number, comp: string, key: string): unknown {
const c = this.model.entityBySource(sourceId)?.components.find((c) => c.type === comp);
- return c ? (c.data as Record<string, unknown>)[key] : undefined;
+ if (!c) return undefined;
+ const data = c.data as Record<string, unknown>;
+ return key in data ? data[key] : defaultDataFor(comp)[key];
}
/** Whether re-parenting every id in `ids` under `target` is valid — false when
diff --git a/desktop/src/engine/SceneQuery.ts b/desktop/src/engine/SceneQuery.ts
index 0935a5743..7e2953fd5 100644
--- a/desktop/src/engine/SceneQuery.ts
+++ b/desktop/src/engine/SceneQuery.ts
@@ -8,6 +8,7 @@ import {
inspectorFields,
inferField,
isColorKey,
+ defaultDataFor,
componentEnable,
componentNotice,
modelInspectableComponents,
@@ -226,7 +227,12 @@ export class SceneQueryImpl {
const comp = this.model.entityBySource(id)?.components.find((c) => c.type === compName);
if (!comp) return null;
const data = comp.data as Record<string, unknown>;
- const f = inferField(key, data[key], isColorKey(compName, key));
+ // Stored value, else the registered default — the same effective view the
+ // inspector renders. Scene data holds only what was authored, so reading the
+ // raw data reports "no value" for every field still at its default, and a
+ // caller asking a yes/no question about one gets the wrong answer.
+ const raw = key in data ? data[key] : defaultDataFor(compName)[key];
+ const f = inferField(key, raw, isColorKey(compName, key));
return f ? f.value : null;
}
}
diff --git a/desktop/tests/anchor-preset.test.ts b/desktop/tests/anchor-preset.test.ts
index e5e36b341..3f84abf78 100644
--- a/desktop/tests/anchor-preset.test.ts
+++ b/desktop/tests/anchor-preset.test.ts
@@ -98,6 +98,30 @@ describe.skipIf(!HAS_WASM)('SceneCommands.setUINodeAnchor (headless World)', ()
}
});
+ // A field a scene never authored still HAS a value — its default. Reading the
+ // raw stored data reported "unset" for those, and the UI-move path is built out
+ // of exactly such reads: it asks whether the entity is a UINode, whether it is
+ // Absolute, and what both insets of an axis are. Almost none of that is
+ // authored, so an Absolute widget could not be dragged in the viewport at all.
+ it('reads a field the scene never stored as its default, not as absent', () => {
+ const { id } = withUINode(); // a bare UINode: nothing authored on it
+ // "Is this a UINode, and how is it positioned?" — the question the move tool
+ // and setEntityXY both ask. Absent meant "not a UINode" / "no answer".
+ expect(S.query.getFieldValue(id, 'UINode', 'position')).toBe(UIPositionType.Relative);
+ expect(S.query.getFieldValue(id, 'UINode', 'insetRight')).toEqual({ value: 0, unit: AUTO });
+ // Still null for a component the entity genuinely does not have.
+ expect(S.query.getFieldValue(id, 'Sprite', 'layer')).toBeNull();
+ });
+
+ it('leaves a flow UINode`s Transform alone instead of writing a position layout owns', () => {
+ // An unauthored `position` reads as Relative (flow), so the move is dropped
+ // where it used to fall through and write Transform.position — a value the
+ // next layout pass overwrites.
+ const { id } = withUINode();
+ S.commands.setEntityXY(id, 123, 456);
+ expect(S.query.getFieldValue(id, 'Transform', 'position')).toEqual([0, 0, 0]);
+ });
+
it('applies to a multi-selection in one undo step', () => {
const a = withUINode();
const b = withUINode();