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(ui): every Interactable carries its UIInteraction from the frame it appears; one exported game hit test

yhh2bb7aefcacc2
diff --git a/desktop b/desktop
index cf6aca325..aee8562d2 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit cf6aca32587b371f0e311c1ddeffd9c625b0f02e
+Subproject commit aee8562d2738d4fb3a16a12412c96f2e8f930cf3
diff --git a/sdk/src/core-ui.ts b/sdk/src/core-ui.ts
index da87cdf4e..a8afe12cc 100644
--- a/sdk/src/core-ui.ts
+++ b/sdk/src/core-ui.ts
@@ -68,6 +68,7 @@ export {
screenToUiWorld,
uiWorldToScreen,
uiHitTestWorld,
+ uiHitAtScreen,
uiPickWorld,
uiPickAllWorld,
type PickableWorld,
diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.ts
index ca86cc525..479e6df8a 100644
--- a/sdk/src/ui/index.ts
+++ b/sdk/src/ui/index.ts
@@ -389,7 +389,7 @@ export {
type UICameraData,
} from './core/ui-camera-info';
-export { screenToUiWorld, uiWorldToScreen, screenToUiLayout, uiLayoutToScreen, isScreenEntity, uiHitTestWorld, uiPickWorld, uiPickAllWorld } from './util/ui-pick';
+export { screenToUiWorld, uiWorldToScreen, screenToUiLayout, uiLayoutToScreen, isScreenEntity, uiHitTestWorld, uiHitAtScreen, uiPickWorld, uiPickAllWorld } from './util/ui-pick';
export type { PickableWorld } from './util/ui-pick';
export {
diff --git a/sdk/src/ui/input/interaction.ts b/sdk/src/ui/input/interaction.ts
index bae676b1c..a8bf2f616 100644
--- a/sdk/src/ui/input/interaction.ts
+++ b/sdk/src/ui/input/interaction.ts
@@ -17,7 +17,7 @@ import { ensureEntityEvents } from '../../ecs/entityEvents';
import { UICameraInfo } from '../core/ui-camera-info';
import type { UICameraData } from '../core/ui-camera-info';
import type { InteractableData } from './interactable';
-import { screenToUiWorld, screenToUiLayout, uiPointerRay, uiLayoutRay, uiHitTestWorld } from '../util/ui-pick';
+import { screenToUiWorld, screenToUiLayout, uiHitAtScreen } from '../util/ui-pick';
import { pointerToScreen } from '../util/math';
import { ScreenOverlay, type ScreenOverlayData } from '../core/screen-overlay';
import { platformDevicePixelRatio } from '../../platform';
@@ -138,12 +138,25 @@ export class UIInteractionPlugin implements Plugin {
expireGestureFired(world);
});
+ let ensuredAtVersion = -1;
app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
[Res(Input), Res(UICameraInfo), Res(ScreenOverlay), Res(UILayoutGeneration)],
(input: InputState, camera: UICameraData, overlay: ScreenOverlayData,
layoutGen: UILayoutGenerationData) => {
events.drain();
+ // Every Interactable carries its pointer state from the frame it
+ // appears, not from the first time a pointer reaches it — a query
+ // over UIInteraction otherwise missed every control nobody had
+ // touched yet. Checked only when the world's structure changed.
+ const structure = world.getWorldVersion();
+ if (structure !== ensuredAtVersion) {
+ ensuredAtVersion = structure;
+ for (const entity of world.getEntitiesWithComponents([Interactable])) {
+ if (!world.has(entity, UIInteraction)) ensureComponent(world, entity, UIInteraction);
+ }
+ }
+
const interactionEntities = world.getEntitiesWithComponents([UIInteraction]);
for (const entity of interactionEntities) {
const interaction = world.get(entity, UIInteraction) as UIInteractionData;
@@ -205,12 +218,7 @@ export class UIInteractionPlugin implements Plugin {
&& (cached.entity === null || eligible(cached.entity))) {
entity = cached.entity;
} else {
- // With no camera there is no world ray: inverting a projection
- // never written yields NaN, which answers "no" by accident.
- // The screen's ray stands in — nothing is in the world.
- const screenRay = overlay.active ? uiLayoutRay(overlay, glX, glY) : undefined;
- const worldRay = camera.valid ? uiPointerRay(camera, glX, glY) : screenRay!;
- entity = uiHitTestWorld(world, worldRay, screenRay);
+ entity = uiHitAtScreen(world, camera, overlay, glX, glY);
if (entity !== null && !eligible(entity)) entity = null;
lastHit.set(pointer.id, { x: layoutPointer.x, y: layoutPointer.y, entity });
}
diff --git a/sdk/src/ui/util/ui-pick.ts b/sdk/src/ui/util/ui-pick.ts
index 2f197fa6f..86e6d29b7 100644
--- a/sdk/src/ui/util/ui-pick.ts
+++ b/sdk/src/ui/util/ui-pick.ts
@@ -143,6 +143,25 @@ export function uiHitTestWorld(
/** Editor pick: topmost UI entity under the point, regardless of Interactable;
* {@link uiHitTestWorld} is the runtime raycast. */
+/**
+ * What the GAME's pointer would hit at framebuffer pixel (@p screenGLX, @p screenGLY)
+ * — the interaction system's own question, so anything that synthesises a click
+ * (an automation driver) is refused exactly where a player's would land
+ * elsewhere: a subtree under `pointerEvents: None` passes the pointer through.
+ * Unlike {@link uiPickWorld}, the editor's selection pick, which sees everything.
+ */
+export function uiHitAtScreen(
+ world: PickableWorld, camera: UICameraData, overlay: ScreenOverlayData,
+ screenGLX: number, screenGLY: number,
+): Entity | null {
+ if (!camera.valid && !overlay.active) return null;
+ // With no camera there is no world ray: inverting a projection never written
+ // yields NaN, which answers "no" by accident. The screen's ray stands in.
+ const screenRay = overlay.active ? uiLayoutRay(overlay, screenGLX, screenGLY) : undefined;
+ const worldRay = camera.valid ? uiPointerRay(camera, screenGLX, screenGLY) : screenRay!;
+ return uiHitTestWorld(world, worldRay, screenRay);
+}
+
export function uiPickWorld(world: PickableWorld, worldX: number, worldY: number): Entity | null {
const c = core(world);
if (!c?.engine.uiHitTest_pick) return null;
diff --git a/sdk/tests/ui-pointer-events-wasm.test.ts b/sdk/tests/ui-pointer-events-wasm.test.ts
index 597e050ad..639f52584 100644
--- a/sdk/tests/ui-pointer-events-wasm.test.ts
+++ b/sdk/tests/ui-pointer-events-wasm.test.ts
@@ -17,7 +17,7 @@ import { ContentRect } from '../src/ui/core/content-rect';
import { UICameraInfo } from '../src/ui/core/ui-camera-info';
import { UIEventType } from '../src/ui/core/events';
import { UILayoutGeneration } from '../src/ui/layout/ui-layout-generation';
-import { Interactable } from '../src/ui/input/interactable';
+import { Interactable, UIInteraction } from '../src/ui/input/interactable';
import { UIControlPointers, type UiPointerEventData } from '../src/ui/input/control-pointers';
import { uiInteractionPlugin } from '../src/ui/input/interaction';
import { registerComponent } from '../src/ecs/component';
@@ -77,6 +77,14 @@ describe.skipIf(!HAS_WASM)('pointer events (WASM integration)', () => {
await a.tick(1 / 60);
}
+ it('gives every Interactable its UIInteraction before a pointer reaches it', async () => {
+ const { app: a, input } = boot();
+ const untouched = area(a, 200, 200, 10, 10);
+ await frame(a, input, 0, 0);
+ expect(a.world.has(untouched, UIInteraction)).toBe(true);
+ expect(a.world.get(untouched, UIInteraction)).toMatchObject({ hovered: false, pressed: false });
+ });
+
it('lets an entity that is only a rectangle be the one pressed', async () => {
const { app: a, input, events } = boot();
const hit = area(a, 0, 0, 40, 20);