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.

feat(viewport): select UI elements by clicking — reuse the engine UI hit-test

esengined90781f54779
diff --git a/desktop/src/engine/ViewportController.ts b/desktop/src/engine/ViewportController.ts
index 06965141a..7d7409a26 100644
--- a/desktop/src/engine/ViewportController.ts
+++ b/desktop/src/engine/ViewportController.ts
@@ -1,6 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
-import { Camera, CameraView, EditorView, Light2D, Sprite, Transform } from 'esengine';
+import {
+ Camera, CameraView, EditorView, Light2D, Sprite, Transform,
+ UINode, UICameraInfo, screenToUiWorld, uiHitTestWorld, type UICameraData,
+} from 'esengine';
import type { EntityId } from '@/types';
import { EngineHost } from './EngineHost';
import { SceneModel } from './SceneModel';
@@ -85,6 +88,8 @@ export const ViewportController = {
entityBounds(id: EntityId): OBB | null {
const world = EngineHost.world;
if (!world || !world.valid(id) || !world.has(id, Transform)) return null;
+ // UI nodes are screen-space; they're picked via the UI hit-test, not a world OBB.
+ if (world.has(id, UINode)) return null;
const t = world.get(id, Transform);
const rot = quatAngleZ(t.rotation as { w: number; x: number; y: number; z: number });
@@ -112,8 +117,29 @@ export const ViewportController = {
};
},
- /** Topmost entity whose oriented bounds contain the pointer, or null. */
+ /** The UI entity under the pointer (engine UI hit-test), or null. */
+ pickUIEntity(clientX: number, clientY: number): EntityId | null {
+ const world = EngineHost.world;
+ const module = EngineHost.module;
+ const cam = EngineHost.getResource(UICameraInfo) as UICameraData | undefined;
+ if (!world || !module || !cam?.valid) return null;
+ type Registry = Parameters<typeof uiHitTestWorld>[1];
+ const reg = (world as unknown as { getCppRegistry(): Registry | null }).getCppRegistry();
+ const s = clientToScreen(clientX, clientY);
+ if (!reg || !s) return null;
+ const wp = screenToUiWorld(cam, s.sx, s.sy);
+ const hit = uiHitTestWorld(module, reg, wp.x, wp.y);
+ if (hit == null) return null;
+ const src = SceneModel.sourceFor(hit);
+ if (src != null && (SceneModel.isLocked(src) || SceneModel.isHidden(src))) return null;
+ return hit;
+ },
+
+ /** Topmost entity under the pointer — UI (screen-space) first, then a world OBB. */
pickEntity(clientX: number, clientY: number): EntityId | null {
+ const ui = this.pickUIEntity(clientX, clientY);
+ if (ui != null) return ui;
+
const world = EngineHost.world;
const wp = this.canvasToWorld(clientX, clientY);
if (!world || !wp) return null;
diff --git a/sdk/src/core-ui.ts b/sdk/src/core-ui.ts
index 9106db81b..00b52db2c 100644
--- a/sdk/src/core-ui.ts
+++ b/sdk/src/core-ui.ts
@@ -18,6 +18,7 @@ export {
TextAlign,
TextVerticalAlign,
TextOverflow,
+ UINode,
UIVisual,
UIVisualType,
UILayoutGeneration,
@@ -45,6 +46,8 @@ export {
UIEventQueue,
makeInteractable,
UICameraInfo,
+ screenToUiWorld,
+ uiHitTestWorld,
/** @internal fill-sprite sizing helper used by widget composition, unstable */
syncFillSpriteSize,
TextInput,
diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.ts
index a74a075c0..34337fef0 100644
--- a/sdk/src/ui/index.ts
+++ b/sdk/src/ui/index.ts
@@ -261,6 +261,8 @@ export {
type UICameraData,
} from './UICameraInfo';
+export { screenToUiWorld, uiHitTestWorld } from './uiPick';
+
export {
UILayoutGeneration,
type UILayoutGenerationData,
diff --git a/sdk/src/ui/input/interaction.ts b/sdk/src/ui/input/interaction.ts
index 069cc31c5..4545a437e 100644
--- a/sdk/src/ui/input/interaction.ts
+++ b/sdk/src/ui/input/interaction.ts
@@ -16,7 +16,7 @@ import { UIEvents, UIEventQueue, UIEventType } from '../core/events';
import { UICameraInfo } from '../UICameraInfo';
import type { UICameraData } from '../UICameraInfo';
import type { InteractableData } from './interactable';
-import { screenToWorld, createInvVPCache } from '../uiMath';
+import { screenToUiWorld, uiHitTestWorld } from '../uiPick';
import { platformDevicePixelRatio } from '../../platform';
import { ensureComponent, walkParentChain } from '../uiHelpers';
import type { ESEngineModule, CppRegistry } from '../../wasm';
@@ -24,9 +24,6 @@ import { UILayoutGeneration } from '../UILayoutGeneration';
import { SystemLabel, PluginName } from '../../systemLabels';
import type { UILayoutGenerationData } from '../UILayoutGeneration';
-const vpCache = createInvVPCache();
-const CPP_NO_HIT_ENTITY = 0xFFFFFFFF;
-
function emitWithBubbling(
world: World,
events: UIEventQueue,
@@ -90,12 +87,7 @@ export class UIInteractionPlugin implements Plugin {
const mouseGLX = input.mouseX * dpr;
const mouseGLY = camera.screenH - input.mouseY * dpr;
- vpCache.update(camera.viewProjection);
- const invVP = vpCache.getInverse(camera.viewProjection);
- const worldMouse = screenToWorld(
- mouseGLX, mouseGLY, invVP,
- camera.vpX, camera.vpY, camera.vpW, camera.vpH,
- );
+ const worldMouse = screenToUiWorld(camera, mouseGLX, mouseGLY);
camera.worldMouseX = worldMouse.x;
camera.worldMouseY = worldMouse.y;
@@ -115,14 +107,11 @@ export class UIInteractionPlugin implements Plugin {
let hitEntity: Entity | null = hoveredEntity;
if (needsHitTest) {
- module.uiHitTest_update(
- registry,
+ hitEntity = uiHitTestWorld(
+ module, registry,
worldMouse.x, worldMouse.y,
mouseDown, mousePressed, mouseReleased,
);
-
- const hitEntityRaw = module.uiHitTest_getHitEntity();
- hitEntity = hitEntityRaw === CPP_NO_HIT_ENTITY ? null : hitEntityRaw;
}
if (hoveredEntity !== null && !world.valid(hoveredEntity)) {
diff --git a/sdk/src/ui/uiPick.ts b/sdk/src/ui/uiPick.ts
new file mode 100644
index 000000000..b4dfbf097
--- /dev/null
+++ b/sdk/src/ui/uiPick.ts
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import type { Entity } from '../types';
+import type { ESEngineModule, CppRegistry } from '../wasm';
+import type { UICameraData } from './UICameraInfo';
+import { screenToWorld, createInvVPCache } from './uiMath';
+
+const NO_HIT = 0xffffffff;
+const vpCache = createInvVPCache();
+
+export function screenToUiWorld(camera: UICameraData, screenGLX: number, screenGLY: number): { x: number; y: number } {
+ vpCache.update(camera.viewProjection);
+ const invVP = vpCache.getInverse(camera.viewProjection);
+ return screenToWorld(screenGLX, screenGLY, invVP, camera.vpX, camera.vpY, camera.vpW, camera.vpH);
+}
+
+export function uiHitTestWorld(
+ module: ESEngineModule,
+ registry: CppRegistry,
+ worldX: number,
+ worldY: number,
+ mouseDown = false,
+ mousePressed = false,
+ mouseReleased = false,
+): Entity | null {
+ module.uiHitTest_update(registry, worldX, worldY, mouseDown, mousePressed, mouseReleased);
+ const hit = module.uiHitTest_getHitEntity();
+ return hit === NO_HIT ? null : hit;
+}
diff --git a/sdk/tests/ui-pick.test.ts b/sdk/tests/ui-pick.test.ts
new file mode 100644
index 000000000..cf7ece839
--- /dev/null
+++ b/sdk/tests/ui-pick.test.ts
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { describe, it, expect, vi } from 'vitest';
+import { uiHitTestWorld } from '../src/ui/uiPick';
+
+const mod = (hit: number) =>
+ ({ uiHitTest_update: vi.fn(), uiHitTest_getHitEntity: () => hit }) as any;
+
+describe('uiHitTestWorld', () => {
+ it('returns the hit entity', () => {
+ const m = mod(42);
+ expect(uiHitTestWorld(m, {} as any, 10, 20)).toBe(42);
+ expect(m.uiHitTest_update).toHaveBeenCalledWith({}, 10, 20, false, false, false);
+ });
+
+ it('maps the no-hit sentinel to null', () => {
+ expect(uiHitTestWorld(mod(0xffffffff), {} as any, 0, 0)).toBeNull();
+ });
+
+ it('passes mouse flags through (the input path)', () => {
+ const m = mod(7);
+ uiHitTestWorld(m, {} as any, 1, 2, true, true, false);
+ expect(m.uiHitTest_update).toHaveBeenCalledWith({}, 1, 2, true, true, false);
+ });
+});