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): the selection readout says the pose it has

esenginecf495d79dea1
diff --git a/desktop/src/engine/StatsStore.ts b/desktop/src/engine/StatsStore.ts
index d941283d0..2b85ef8f8 100644
--- a/desktop/src/engine/StatsStore.ts
+++ b/desktop/src/engine/StatsStore.ts
@@ -5,12 +5,18 @@ import { getResourceStats } from 'esengine';
import { EngineHost } from './EngineHost';
import { SceneModel } from './SceneModel';
import { useSelection } from '@/store/selectionStore';
+import { quatToEuler } from './schema';
/** The lone-selected entity's transform for the status bar (rot in degrees). */
export interface SelTransform {
x: number;
y: number;
+ /** Depth, or null when the entity is on the 2D plane and there is none to report. */
+ z: number | null;
+ /** Turn about Z, in degrees — the whole rotation of a 2D transform. */
rot: number;
+ /** The other two turns, or null when there are none. */
+ tilt: { x: number; y: number } | null;
}
/** Texture residency for the status bar (bytes; evictable = warm cache). */
@@ -31,22 +37,37 @@ export interface StatsSnapshot {
const round1 = (n: number) => Math.round(n * 10) / 10;
-/** Sample the lone selection's transform from the model, or null. */
-function sampleSelection(): SelTransform | null {
+/**
+ * Sample the lone selection's transform from the model, or null.
+ *
+ * A depth and a tilt are reported only when they are there: a flat, Z-turned
+ * transform reads as the two numbers and one angle it always did, and a posed one
+ * says so rather than showing a Z angle that is not its rotation.
+ */
+export function sampleSelection(): SelTransform | null {
const sel = useSelection.getState();
if (sel.selectedIds.size !== 1 || sel.selectedId == null) return null;
const e = SceneModel.entityBySource(sel.selectedId);
const tf = e?.components.find((c) => c.type === 'Transform')?.data as
- | { position?: { x: number; y: number }; rotation?: { w: number; z: number } }
+ | { position?: { x: number; y: number; z?: number }; rotation?: { x: number; y: number; z: number; w: number } }
| undefined;
if (!tf?.position) return null;
- // 2D rotation lives on Z; recover the angle from the (w, z) quaternion.
- const rot = tf.rotation ? 2 * Math.atan2(tf.rotation.z, tf.rotation.w) * (180 / Math.PI) : 0;
- return { x: round1(tf.position.x), y: round1(tf.position.y), rot: round1(rot) };
+ // Through the inspector's own conversion — a fourth transcription of "the Z angle
+ // of a quaternion" is a fourth chance for the readout to disagree with the field.
+ const [rx, ry, rz] = tf.rotation ? quatToEuler(tf.rotation) : [0, 0, 0];
+ const z = tf.position.z ?? 0;
+ return {
+ x: round1(tf.position.x),
+ y: round1(tf.position.y),
+ z: z === 0 ? null : round1(z),
+ rot: round1(rz),
+ tilt: rx === 0 && ry === 0 ? null : { x: round1(rx), y: round1(ry) },
+ };
}
const selEq = (a: SelTransform | null, b: SelTransform | null): boolean =>
- a === b || (!!a && !!b && a.x === b.x && a.y === b.y && a.rot === b.rot);
+ a === b || (!!a && !!b && a.x === b.x && a.y === b.y && a.z === b.z && a.rot === b.rot
+ && a.tilt?.x === b.tilt?.x && a.tilt?.y === b.tilt?.y);
/** Sample texture residency from the engine's resource stats, or null. */
function sampleVram(): VramReadout | null {
diff --git a/desktop/src/layout/StatusBar.tsx b/desktop/src/layout/StatusBar.tsx
index f45195881..a1227c91a 100644
--- a/desktop/src/layout/StatusBar.tsx
+++ b/desktop/src/layout/StatusBar.tsx
@@ -37,8 +37,12 @@ function SelectionReadout() {
if (!selection) return null;
return (
<span className="sitem mono" title={t('layout.status.selectionTooltip')}>
- {selection.x}, {selection.y}
- <span className="smute"> · {selection.rot}°</span>
+ {selection.x}, {selection.y}{selection.z !== null && `, ${selection.z}`}
+ <span className="smute">
+ {' · '}
+ {selection.tilt !== null && `${selection.tilt.x}, ${selection.tilt.y}, `}
+ {selection.rot}°
+ </span>
</span>
);
}
diff --git a/desktop/tests/selection-readout.test.ts b/desktop/tests/selection-readout.test.ts
new file mode 100644
index 000000000..01613497b
--- /dev/null
+++ b/desktop/tests/selection-readout.test.ts
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file The status bar's selection readout: what it reports about a transform.
+ * A flat, Z-turned one must read as the two numbers and one angle it always
+ * did; a posed one must not have its pose reported as a Z angle.
+ */
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+const h = vi.hoisted(() => ({
+ entity: null as { components: Array<{ type: string; data: unknown }> } | null,
+}));
+
+vi.mock('@/engine/SceneModel', () => ({
+ SceneModel: { entityBySource: () => h.entity, subscribe: () => () => {} },
+}));
+vi.mock('@/engine/EngineHost', () => ({ EngineHost: {} }));
+
+import { sampleSelection } from '@/engine/StatsStore';
+import { useSelection } from '@/store/selectionStore';
+import { eulerToQuat } from '@/engine/schema';
+
+const transform = (position: unknown, rotation?: unknown): void => {
+ h.entity = { components: [{ type: 'Transform', data: { position, rotation } }] };
+};
+
+beforeEach(() => {
+ h.entity = null;
+ useSelection.getState().select(null);
+});
+
+describe('the selection readout', () => {
+ it('reports nothing without exactly one selection', () => {
+ expect(sampleSelection()).toBeNull();
+ });
+
+ it('a flat, Z-turned transform reports two numbers and one angle', () => {
+ transform({ x: 12.34, y: -5 }, eulerToQuat([0, 0, 30]));
+ useSelection.getState().select(7);
+ const s = sampleSelection()!;
+ expect(s.x).toBe(12.3);
+ expect(s.y).toBe(-5);
+ expect(s.z).toBeNull();
+ expect(s.rot).toBeCloseTo(30, 1);
+ expect(s.tilt).toBeNull();
+ });
+
+ // 2*atan2(z, w) is the Z angle only while x and y are zero; for a posed transform
+ // it reports a number that is none of its turns.
+ it('a posed transform reports its depth and its other two turns', () => {
+ transform({ x: 0, y: 0, z: -40 }, eulerToQuat([25, -15, 30]));
+ useSelection.getState().select(7);
+ const s = sampleSelection()!;
+ expect(s.z).toBe(-40);
+ expect(s.tilt).not.toBeNull();
+ expect(s.tilt!.x).toBeCloseTo(25, 1);
+ expect(s.tilt!.y).toBeCloseTo(-15, 1);
+ expect(s.rot).toBeCloseTo(30, 1);
+ });
+});