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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(ui): bound hit caches to live pointer frames
yhh9889fdfdb958
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e43168181..17815756e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Touch hit caches release ended pointer IDs.** Long-running touch sessions retain only the pointers present in the current input frame, including release-frame results needed by quick taps, instead of accumulating every historical finger ID.+- **Pointer callbacks cancel invalid UI interactions immediately.** Disabling or deleting a control during hover, press or release stops later events from the same batch. Invalidated captures and interaction edges clear in the same frame, so re-enabling a button does not complete an old press.- **UI focus transitions recheck callback changes.** Removing or disabling a control and opening a modal during blur cannot focus a stale or excluded target. Focus callbacks that remove their control clear the manager in the same update. Blur handlers observe the cleared state, and single-target keyboard eligibility avoids sorting the entire Tab ring.diff --git a/sdk/src/ui/input/interaction.ts b/sdk/src/ui/input/interaction.tsindex 6502e2463..5ba5059ab 100644--- a/sdk/src/ui/input/interaction.ts+++ b/sdk/src/ui/input/interaction.ts@@ -24,7 +24,7 @@ import { ensureComponent, walkParentChain } from '../util/helpers';import type { CppRegistry } from '../../wasm';import { engineApi } from '../../ecs/bridge/engineApi';import { UILayoutGeneration } from '../layout/ui-layout-generation';-import { UiPointerBook, uiPointersOf, MOUSE_POINTER, type UiPointerSample } from './pointerBook';+import { UiPointerBook, UiPointerHitCache, uiPointersOf, MOUSE_POINTER, type UiPointerSample } from './pointerBook';import { fireGestureClick } from './gestureClick';import { inputRouter } from '../../input/inputRouter';import { SystemLabel, PluginName } from '../../ecs/systemLabels';@@ -70,7 +70,7 @@ export class UIInteractionPlugin implements Plugin {// imply. Gestures (drag, scroll, slider) stay on the primary pointer.const book = new UiPointerBook();/** Where each pointer last hit, so a settled pointer skips the raycast. */- const lastHit = new Map<number, { x: number; y: number; entity: Entity | null }>();+ const lastHit = new UiPointerHitCache();let lastLayoutGen = -1;let lastWorldVersion = -1;let lastTick = -1;@@ -113,6 +113,7 @@ export class UIInteractionPlugin implements Plugin {const dpr = platformDevicePixelRatio();const surfaceH = overlay.active ? overlay.surfaceH : camera.screenH;const pointers = uiPointersOf(input);+ lastHit.retain(pointers);const layoutChanged = layoutGen.generation !== lastLayoutGen;lastLayoutGen = layoutGen.generation;const worldVersion = world.getWorldVersion();diff --git a/sdk/src/ui/input/pointerBook.ts b/sdk/src/ui/input/pointerBook.tsindex 824b099fd..35ebe504e 100644--- a/sdk/src/ui/input/pointerBook.ts+++ b/sdk/src/ui/input/pointerBook.ts@@ -29,6 +29,15 @@ export interface UiPointerSample {hovers: boolean;}+/** Per-pointer raycast results, retained only while the input frame carries the pointer. */+export class UiPointerHitCache extends Map<number, { x: number; y: number; entity: Entity | null }> {+ retain(pointers: readonly UiPointerSample[]): void {+ for (const id of this.keys()) {+ if (!pointers.some(pointer => pointer.id === id)) this.delete(id);+ }+ }+}+export type UiPointerEventKind = 'hoverEnter' | 'hoverExit' | 'press' | 'release' | 'click';export interface UiPointerEvent {diff --git a/sdk/tests/ui-multi-pointer.test.ts b/sdk/tests/ui-multi-pointer.test.tsindex aedf062a0..3aa285e99 100644--- a/sdk/tests/ui-multi-pointer.test.ts+++ b/sdk/tests/ui-multi-pointer.test.ts@@ -7,7 +7,7 @@* two fingers hold stays pressed until the last one lifts.*/import { describe, it, expect } from 'vitest';-import { UiPointerBook, uiPointersOf, MOUSE_POINTER, type UiPointerSample } from '../src/ui/input/pointerBook';+import { UiPointerBook, UiPointerHitCache, uiPointersOf, MOUSE_POINTER, type UiPointerSample } from '../src/ui/input/pointerBook';import { InputState } from '../src/input/input';import type { Entity } from '../src/types';@@ -112,6 +112,30 @@ describe('a release inside the host event', () => {});describe('the pointer set a frame of input makes', () => {+ it('keeps touch hit caching bounded over repeated gestures, including the release frame', () => {+ const input = new InputState(), cache = new UiPointerHitCache();+ for (let id = 0; id < 10000; id++) {+ input.touches.set(id, { id, x: 10, y: 20 });+ cache.retain(uiPointersOf(input));+ cache.set(id, { x: 10, y: 20, entity: BUTTON_A });+ expect(cache.size).toBe(1);+ input.endTouch(id);+ cache.retain(uiPointersOf(input));+ expect(cache.get(id)?.entity).toBe(BUTTON_A);+ input.clearFrameState();+ cache.retain(uiPointersOf(input));+ expect(cache.size).toBe(0);+ }+ });+ it('expires ended touches while keeping another finger and the current mouse hit', () => {+ const cache = new UiPointerHitCache();+ for (const id of [1, 2, MOUSE_POINTER]) cache.set(id, { x: 0, y: 0, entity: BUTTON_A });+ cache.retain([finger(2, 0)]);+ expect([...cache.keys()]).toEqual([2]);+ cache.set(MOUSE_POINTER, { x: 20, y: 0, entity: BUTTON_B });+ cache.retain([finger(MOUSE_POINTER, 20)]);+ expect([...cache.keys()]).toEqual([MOUSE_POINTER]);+ });it('is the mouse when nothing is touching the glass', () => {const input = new InputState();input.mouseX = 42;diff --git a/sdk/tests/ui-pointer-callback-wasm.test.ts b/sdk/tests/ui-pointer-callback-wasm.test.tsindex 5c804df9d..0d86b9839 100644--- a/sdk/tests/ui-pointer-callback-wasm.test.ts+++ b/sdk/tests/ui-pointer-callback-wasm.test.ts@@ -10,6 +10,7 @@ import { ScreenOverlay } from '../src/ui/core/screen-overlay';import { UILayoutGeneration } from '../src/ui/layout/ui-layout-generation';import { Interactable, UIInteraction } from '../src/ui/input/interactable';import { uiInteractionPlugin } from '../src/ui/input/interaction';+import { UiPointerHitCache, MOUSE_POINTER } from '../src/ui/input/pointerBook';import { UIEvents } from '../src/ui/core/events';import type { CppRegistry, ESEngineModule } from '../src/wasm';import { HAS_WASM, loadWasmModule } from './helpers/loadWasm';@@ -35,9 +36,28 @@ describe.skipIf(!HAS_WASM)('pointer callback lifecycle on real WASM', () => {uiInteractionPlugin.build(app); flushPendingRegistrations(app);});afterEach(() => {+ vi.restoreAllMocks();for (const entity of app.world.getAllEntities()) app.world.despawn(entity);app.world.disconnectCpp(); registry.delete();});+ it('does not retain past touch IDs across 1000 real interaction updates', async () => {+ const entity = app.world.spawn(); hit.entity = entity;+ app.world.insert(entity, Interactable, { enabled: true });+ const input = app.getResource(Input);+ const retain = vi.spyOn(UiPointerHitCache.prototype, 'retain');+ for (let id = 0; id < 1000; id++) {+ input.touches.set(id, { id, x: 10, y: 20 });+ input.touchesStarted.set(id, { id, x: 10, y: 20 });+ input.endTouch(id);+ await app.tick(1 / 60);+ const cache = retain.mock.contexts[0] as UiPointerHitCache;+ expect([...cache.keys()]).toEqual([id]);+ input.clearFrameState();+ }+ await app.tick(1 / 60);+ const cache = retain.mock.contexts[0] as UiPointerHitCache;+ expect([...cache.keys()]).toEqual([MOUSE_POINTER]);+ });for (const mutation of ['disable', 'despawn'] as const) {it.each(['hover_enter', 'press', 'release'])(`${mutation} during %s stops later events`, async phase => {const entity = app.world.spawn(); hit.entity = entity;diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex e1ff3c753..4d67f4050 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(ui): bound hit caches to live pointer frames':+ { note: 'Touch hit caches release ended pointer IDs.' },'fix(ui): cancel pointer events invalidated by callbacks':{ note: 'Pointer callbacks cancel invalid UI interactions immediately.' },'fix(ui): revalidate focus targets after synchronous callbacks':