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: improve UI module type safety and eliminate magic numbers

esengine1de4e4ac944f
diff --git a/sdk/src/ui/DropdownPlugin.ts b/sdk/src/ui/DropdownPlugin.ts
index 83a90666f..2408a9bd2 100644
--- a/sdk/src/ui/DropdownPlugin.ts
+++ b/sdk/src/ui/DropdownPlugin.ts
@@ -21,7 +21,7 @@ import { Input } from '../input';
import type { InputState } from '../input';
import { isEditor, isPlayMode } from '../env';
import { ensureComponent, getEffectiveWidth, getEffectiveHeight } from './uiHelpers';
-import { DROPDOWN_ITEM_HEIGHT, DROPDOWN_FONT_SIZE, DROPDOWN_HIGHLIGHT_COLOR } from './uiConstants';
+import { DROPDOWN_ITEM_HEIGHT, DROPDOWN_FONT_SIZE, DROPDOWN_HIGHLIGHT_COLOR, DROPDOWN_DEFAULT_COLOR, DROPDOWN_OPTION_COLOR } from './uiConstants';
interface DropdownState {
optionEntities: Entity[];
@@ -157,7 +157,7 @@ export class DropdownPlugin implements Plugin {
if (world.has(entity, UIRenderer)) {
const r = world.get(entity, UIRenderer) as UIRendererData;
if (r.color.r === 1 && r.color.g === 1 && r.color.b === 1 && r.color.a === 1) {
- r.color = { r: 0.9, g: 0.9, b: 0.9, a: 1 };
+ r.color = { ...DROPDOWN_DEFAULT_COLOR };
world.insert(entity, UIRenderer, r);
}
} else if (world.has(entity, Sprite)) {
@@ -165,7 +165,7 @@ export class DropdownPlugin implements Plugin {
if (s.color.r === 1 && s.color.g === 1 && s.color.b === 1 && s.color.a === 1) {
world.insert(entity, Sprite, {
texture: s.texture,
- color: { r: 0.9, g: 0.9, b: 0.9, a: 1 },
+ color: { ...DROPDOWN_DEFAULT_COLOR },
size: { x: s.size.x, y: s.size.y },
uvOffset: { x: s.uvOffset.x, y: s.uvOffset.y },
uvScale: { x: s.uvScale.x, y: s.uvScale.y },
@@ -247,7 +247,7 @@ export class DropdownPlugin implements Plugin {
world.insert(optEntity, UIRenderer, {
visualType: UIVisualType.SolidColor,
texture: 0,
- color: { r: 0.95, g: 0.95, b: 0.95, a: 1 },
+ color: { ...DROPDOWN_OPTION_COLOR },
uvOffset: { x: 0, y: 0 },
uvScale: { x: 1, y: 1 },
sliceBorder: { x: 0, y: 0, z: 0, w: 0 },
@@ -383,7 +383,7 @@ export class DropdownPlugin implements Plugin {
if (!world.valid(optEntity)) continue;
const color = i === state.highlightIndex
? { ...DROPDOWN_HIGHLIGHT_COLOR }
- : { r: 0.95, g: 0.95, b: 0.95, a: 1 };
+ : { ...DROPDOWN_OPTION_COLOR };
if (world.has(optEntity, UIRenderer)) {
const r = world.get(optEntity, UIRenderer) as UIRendererData;
r.color = color;
diff --git a/sdk/src/ui/SafeAreaPlugin.ts b/sdk/src/ui/SafeAreaPlugin.ts
index a4b7719ed..af38b101b 100644
--- a/sdk/src/ui/SafeAreaPlugin.ts
+++ b/sdk/src/ui/SafeAreaPlugin.ts
@@ -17,8 +17,15 @@ export interface SafeAreaInsets {
right: number;
}
+interface WxGlobal {
+ wx?: {
+ getSystemInfoSync?(): { safeArea: { top: number; bottom: number; left: number; right: number }; screenWidth: number; screenHeight: number } | undefined;
+ onWindowResize?(cb: () => void): void;
+ };
+}
+
function getWeChatSafeAreaInsets(): SafeAreaInsets {
- const g = globalThis as any;
+ const g = globalThis as unknown as WxGlobal;
const info = g.wx?.getSystemInfoSync?.();
if (!info || !info.safeArea) {
return { top: 0, bottom: 0, left: 0, right: 0 };
@@ -63,7 +70,7 @@ export class SafeAreaPlugin implements Plugin {
let prevWorldH = 0;
if (isWeChat()) {
- const g = globalThis as any;
+ const g = globalThis as unknown as WxGlobal;
g.wx?.onWindowResize?.(() => {
cachedInsets = getSafeAreaInsets();
dirty = true;
diff --git a/sdk/src/ui/TextInputPlugin.ts b/sdk/src/ui/TextInputPlugin.ts
index 49eda9734..1543a5c56 100644
--- a/sdk/src/ui/TextInputPlugin.ts
+++ b/sdk/src/ui/TextInputPlugin.ts
@@ -32,17 +32,22 @@ export class TextInputPlugin implements Plugin {
if (isEditor() && !isPlayMode()) return;
- const module = app.wasmModule;
- if (!module) {
+ const moduleOrNull = app.wasmModule;
+ if (!moduleOrNull) {
console.warn('TextInputPlugin: No WASM module available');
return;
}
+ const module = moduleOrNull;
const world = app.world;
const textureCache = new Map<Entity, number>();
const canvas = platformCreateCanvas(RuntimeConfig.textCanvasSize, 64);
- const ctx = canvas.getContext('2d', { willReadFrequently: true })! as
- CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
+ const ctxOrNull = canvas.getContext('2d', { willReadFrequently: true });
+ if (!ctxOrNull) {
+ console.warn('TextInputPlugin: failed to create 2D canvas context');
+ return;
+ }
+ const ctx = ctxOrNull as CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
let wasmPixelPtr = 0;
let wasmPixelSize = 0;
@@ -142,13 +147,13 @@ export class TextInputPlugin implements Plugin {
textarea.removeEventListener('keydown', onKeyDown);
textarea.removeEventListener('blur', onBlur);
textarea.remove();
- const rm = module!.getResourceManager();
+ const rm = module.getResourceManager();
for (const tex of textureCache.values()) {
rm.releaseTexture(tex);
}
textureCache.clear();
if (wasmPixelPtr !== 0) {
- module!._free(wasmPixelPtr);
+ module._free(wasmPixelPtr);
wasmPixelPtr = 0;
wasmPixelSize = 0;
}
@@ -258,7 +263,7 @@ export class TextInputPlugin implements Plugin {
}
}
- const rm = module!.getResourceManager();
+ const rm = module.getResourceManager();
for (const [e, tex] of textureCache) {
if (!world.valid(e) || !world.has(e, TextInput)) {
rm.releaseTexture(tex);
@@ -388,16 +393,16 @@ export class TextInputPlugin implements Plugin {
const imageData = ctx.getImageData(0, 0, w, h);
const pixels = new Uint8Array(imageData.data.buffer);
- const rm = module!.getResourceManager();
+ const rm = module.getResourceManager();
if (pixels.length > wasmPixelSize) {
if (wasmPixelPtr !== 0) {
- module!._free(wasmPixelPtr);
+ module._free(wasmPixelPtr);
}
- wasmPixelPtr = module!._malloc(pixels.length);
+ wasmPixelPtr = module._malloc(pixels.length);
wasmPixelSize = pixels.length;
}
- module!.HEAPU8.set(pixels, wasmPixelPtr);
+ module.HEAPU8.set(pixels, wasmPixelPtr);
const existingTex = textureCache.get(entity);
if (existingTex !== undefined) {
diff --git a/sdk/src/ui/TextRenderer.ts b/sdk/src/ui/TextRenderer.ts
index d19b9e113..e12d291f5 100644
--- a/sdk/src/ui/TextRenderer.ts
+++ b/sdk/src/ui/TextRenderer.ts
@@ -41,7 +41,9 @@ export class TextRenderer {
constructor(module: ESEngineModule) {
this.module = module;
this.canvas = platformCreateCanvas(RuntimeConfig.textCanvasSize, RuntimeConfig.textCanvasSize);
- this.ctx = this.canvas.getContext('2d', { willReadFrequently: true })! as CanvasRenderingContext2D;
+ const ctx = this.canvas.getContext('2d', { willReadFrequently: true });
+ if (!ctx) throw new Error('TextRenderer: failed to create 2D canvas context');
+ this.ctx = ctx;
}
beginFrame(): void {
diff --git a/sdk/src/ui/UIInteractionPlugin.ts b/sdk/src/ui/UIInteractionPlugin.ts
index f0aea31eb..2906b5418 100644
--- a/sdk/src/ui/UIInteractionPlugin.ts
+++ b/sdk/src/ui/UIInteractionPlugin.ts
@@ -30,6 +30,7 @@ import { UILayoutGeneration } from './UILayoutGeneration';
import type { UILayoutGenerationData } from './UILayoutGeneration';
const vpCache = createInvVPCache();
+const CPP_NO_HIT_ENTITY = 0xFFFFFFFF;
function emitWithBubbling(
world: World,
@@ -122,7 +123,7 @@ export class UIInteractionPlugin implements Plugin {
);
const hitEntityRaw = module.uiHitTest_getHitEntity();
- hitEntity = hitEntityRaw === 0xFFFFFFFF ? null : hitEntityRaw;
+ hitEntity = hitEntityRaw === CPP_NO_HIT_ENTITY ? null : hitEntityRaw;
}
if (hoveredEntity !== null && !world.valid(hoveredEntity)) {
@@ -177,6 +178,9 @@ export class UIInteractionPlugin implements Plugin {
app.addSystemToSchedule(Schedule.Update, defineSystem(
[],
() => {
+ for (const e of buttonInitialized) {
+ if (!world.valid(e)) buttonInitialized.delete(e);
+ }
const buttonEntities = world.getEntitiesWithComponents([Button]);
for (const entity of buttonEntities) {
ensureComponent(world, entity, Interactable, { enabled: true });
diff --git a/sdk/src/ui/uiConstants.ts b/sdk/src/ui/uiConstants.ts
index 24eb1ca36..ebf1f1cfd 100644
--- a/sdk/src/ui/uiConstants.ts
+++ b/sdk/src/ui/uiConstants.ts
@@ -18,3 +18,5 @@ export const SCROLL_POSITION_EPSILON = 0.01;
export const DROPDOWN_ITEM_HEIGHT = 30;
export const DROPDOWN_FONT_SIZE = 16;
export const DROPDOWN_HIGHLIGHT_COLOR = { r: 0.3, g: 0.5, b: 0.8, a: 1 };
+export const DROPDOWN_DEFAULT_COLOR = { r: 0.9, g: 0.9, b: 0.9, a: 1 };
+export const DROPDOWN_OPTION_COLOR = { r: 0.95, g: 0.95, b: 0.95, a: 1 };