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(text): a Text spawned in Update is ordered in the frame it appears, not drawn under its panels for one frame

yhh08fa8c44a5f5
diff --git a/sdk/src/ui/text/plugin.ts b/sdk/src/ui/text/plugin.ts
index e1ad4b28f..9ebdf83b7 100644
--- a/sdk/src/ui/text/plugin.ts
+++ b/sdk/src/ui/text/plugin.ts
@@ -12,6 +12,7 @@
import type { App, Plugin } from '../../app/app';
import { Transform, type TransformData, registerComponent } from '../../ecs/component';
import { defineSystem, Schedule } from '../../ecs/system';
+import { SystemLabel } from '../../ecs/systemLabels';
import type { ESEngineModule, CppRegistry } from '../../wasm';
import type { Entity } from '../../types';
import { SdfTextRenderer } from './text-renderer';
@@ -137,11 +138,20 @@ export class TextPlugin implements Plugin {
// UIVisual (visualType None — not drawn as a quad) so the UI
// render-order pass assigns it a uiOrder and the SDF glyphs sort with
// sibling UI elements. Idempotent; runs before the PostUpdate order pass.
- app.addSystemToSchedule(Schedule.PreUpdate, defineSystem([], () => {
+ const ensureTextRenderNodes = (): void => {
for (const e of world.getEntitiesWithComponents([Text, UINode])) {
ensureUIVisual(world, e as Entity);
}
- }, { name: 'TextRenderNodeSystem' }));
+ };
+ app.addSystemToSchedule(Schedule.PreUpdate, defineSystem([], ensureTextRenderNodes, { name: 'TextRenderNodeSystem' }));
+ // A Text spawned in Update is not seen by the PreUpdate pass, so it would
+ // reach the order pass with no UIVisual, take no uiOrder, and be drawn at
+ // the base layer — under every panel — for its first frame. The late pass
+ // gives it one before the order is assigned.
+ app.addSystemToSchedule(Schedule.PostUpdate, defineSystem([], ensureTextRenderNodes, {
+ name: 'TextRenderNodeLateSystem',
+ runBefore: [SystemLabel.UIRenderOrder],
+ }));
// Localized text: an `i18nKey` binds content to the Localization
// resource. Gated per frame on the resource so localizationPlugin
diff --git a/sdk/tests/text-first-frame-order.test.ts b/sdk/tests/text-first-frame-order.test.ts
new file mode 100644
index 000000000..85f594d29
--- /dev/null
+++ b/sdk/tests/text-first-frame-order.test.ts
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * A Text spawned in Update (after PreUpdate's TextRenderNodeSystem has run) must
+ * still be ordered in the frame it appears. Without a UIVisual the order pass
+ * skips it, ui_getRenderOrder answers -1, and the glyphs are drawn at the base
+ * layer — under every panel — for that one frame: the text of a rebuilt screen
+ * flashes out.
+ */
+import { describe, it, expect, beforeAll } from 'vitest';
+import { App } from '../src/app/app';
+import { Canvas, Transform } from '../src/ecs/component';
+import { defineSystem, Schedule } from '../src/ecs/system';
+import { UINode, type UINodeData } from '../src/ui/core/ui-node';
+import { Text } from '../src/ui/core/text';
+import { uiLayoutPlugin } from '../src/ui/layout/layout';
+import { uiRenderOrderPlugin } from '../src/ui/render/render-order';
+import { textPlugin } from '../src/ui/text/plugin';
+import { px, auto } from '../src/ui/core/dimension';
+import type { ESEngineModule, CppRegistry } from '../src/wasm';
+import { loadWasmModule, HAS_WASM } from './helpers/loadWasm';
+import { setScreenBox } from './helpers/screenBox';
+
+function box(over: Partial<UINodeData> = {}): UINodeData {
+ return {
+ display: 0, position: 0, opacity: 1, pointerEvents: 0,
+ width: auto(), height: auto(),
+ minWidth: auto(), minHeight: auto(), maxWidth: auto(), maxHeight: auto(),
+ flexGrow: 0, flexShrink: 1, flexBasis: auto(), alignSelf: 0,
+ marginLeft: px(0), marginTop: px(0), marginRight: px(0), marginBottom: px(0),
+ insetLeft: auto(), insetTop: auto(), insetRight: auto(), insetBottom: auto(),
+ ...over,
+ } as UINodeData;
+}
+
+describe.skipIf(!HAS_WASM)('a Text spawned in Update (WASM integration)', () => {
+ let module: ESEngineModule;
+ beforeAll(async () => { module = await loadWasmModule(); });
+
+ it('is given a render order in the frame it is spawned', async () => {
+ const app = App.new();
+ const registry = new module.Registry() as unknown as CppRegistry;
+ app.connectCpp(registry, module);
+ setScreenBox(app, -400, -300, 400, 300);
+ app.addPlugin(uiLayoutPlugin);
+ app.addPlugin(textPlugin);
+ app.addPlugin(uiRenderOrderPlugin);
+ const world = app.world;
+
+ const canvas = world.spawn();
+ world.insert(canvas, Canvas, {});
+ world.insert(canvas, UINode, box());
+ world.insert(canvas, Transform);
+
+ let label = 0;
+ let spawned = false;
+ app.addSystemToSchedule(Schedule.Update, defineSystem([], () => {
+ if (spawned) return;
+ spawned = true;
+ const e = world.spawn();
+ world.insert(e, Transform);
+ world.insert(e, UINode, box({ position: 1, insetLeft: px(0), insetTop: px(0), width: px(100), height: px(20) }));
+ world.insert(e, Text, { content: 'hello' });
+ world.setParent(e, canvas);
+ label = e as number;
+ }, { name: 'SpawnLabel' }));
+
+ await app.tick(1 / 60);
+
+ expect(label).not.toBe(0);
+ expect(module.ui_getRenderOrder!(registry, label)).toBeGreaterThanOrEqual(0);
+
+ for (const e of world.getAllEntities()) {
+ try { world.despawn(e); } catch { /* already gone */ }
+ }
+ world.disconnectCpp();
+ (registry as unknown as { delete(): void }).delete();
+ });
+});