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
feat(picking): a sprite outside the layout tree can be picked
esenginec08d24e73362
diff --git a/sdk/tests/ui-drag-plugin.test.ts b/sdk/tests/ui-drag-plugin.test.tsindex 439c039d0..49e9cb37b 100644--- a/sdk/tests/ui-drag-plugin.test.ts+++ b/sdk/tests/ui-drag-plugin.test.ts@@ -51,6 +51,9 @@ function draggable(opts: Partial<{ enabled: boolean; dragThreshold: number; lockconstraintMin: null,constraintMax: null,});+ // hovered is set by hand here, which makes every test below about the drag+ // LOGIC and none of them about whether such an entity can be hovered at all.+ // That question is ui-pick-world.test.ts, against the real raycast.app.world.insert(e, UIInteraction, { hovered: opts.hovered ?? true });app.world.insert(e, Transform, { position: { x: 0, y: 0, z: 0 }, worldPosition: { x: 0, y: 0, z: 0 } });return e;diff --git a/sdk/tests/ui-pick-world.test.ts b/sdk/tests/ui-pick-world.test.tsnew file mode 100644index 000000000..81a1df9b0--- /dev/null+++ b/sdk/tests/ui-pick-world.test.ts@@ -0,0 +1,132 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file Picking an entity that is NOT in the UI layout tree, via the real raycast.+ *+ * ui-drag-plugin.test.ts sets `hovered` by hand, so it covers the drag logic and+ * not whether such an entity can be hovered at all.+ * Requires pre-built WASM at build/wasm/web/esengine.wasm.+ */+import { describe, it, expect, beforeAll } from 'vitest';+import { World } from '../src/ecs/world';+import { Transform, Sprite } from '../src/ecs/component';+import { Interactable } from '../src/ui/input/interactable';+import type { Entity } from '../src/types';+import type { ESEngineModule, CppRegistry } from '../src/wasm';+import { loadWasmModule, HAS_WASM } from './helpers/loadWasm';++const NO_HIT = 0xffffffff;++describe.skipIf(!HAS_WASM)('world-space picking (WASM integration)', () => {+ let module: ESEngineModule;++ beforeAll(async () => {+ module = await loadWasmModule();+ });++ function createWorld(): { world: World; registry: CppRegistry } {+ const registry = new module.Registry() as unknown as CppRegistry;+ const world = new World();+ world.connectCpp(registry, module);+ return { world, registry };+ }++ function disposeWorld(world: World, registry: CppRegistry): void {+ for (const e of world.getAllEntities()) {+ try { world.despawn(e); } catch { /* already gone */ }+ }+ world.disconnectCpp();+ (registry as unknown as { delete(): void }).delete();+ }++ /** A plain world-space sprite: no UINode, so the layout tree never sees it. */+ function sprite(+ world: World, x: number, y: number,+ opts: { size?: number; layer?: number; raycastTarget?: boolean; scale?: number } = {},+ ): Entity {+ const e = world.spawn();+ const s = opts.size ?? 100;+ const k = opts.scale ?? 1;+ world.insert(e, Transform, { position: { x, y, z: 0 }, scale: { x: k, y: k, z: 1 } });+ world.insert(e, Sprite, { size: { x: s, y: s }, layer: opts.layer ?? 0 });+ world.insert(e, Interactable, { raycastTarget: opts.raycastTarget ?? true });+ return e;+ }++ let frame = 0;+ function pickAt(registry: CppRegistry, x: number, y: number): number {+ // transforms_updated is memoised per frame, so each pick needs its own.+ module.renderer_beginFrame(frame++);+ module.renderer_updateTransforms(registry as never);+ module.uiHitTest_update(registry as never, x, y, false, false, false);+ return module.uiHitTest_getHitEntity();+ }++ it('hits a sprite that has no UINode', () => {+ const { world, registry } = createWorld();+ const e = sprite(world, 0, 0);++ expect(pickAt(registry, 0, 0)).toBe(e);++ disposeWorld(world, registry);+ });++ it('misses outside the sprite, so the box is the sprite and not the world', () => {+ const { world, registry } = createWorld();+ sprite(world, 0, 0, { size: 100 });++ // Just inside, then well outside: a pass that always hits would fail here.+ expect(pickAt(registry, 49, 49)).not.toBe(NO_HIT);+ expect(pickAt(registry, 400, 400)).toBe(NO_HIT);++ disposeWorld(world, registry);+ });++ it('sets UIInteraction, which is what the drag system gates on', () => {+ const { world, registry } = createWorld();+ const e = sprite(world, 0, 0);++ pickAt(registry, 0, 0);++ // The engine writes it, so ask the engine: this is the component the drag+ // system reads `hovered` from, and nothing else creates it here.+ const reg = registry as unknown as { hasUIInteraction(e: number): boolean };+ expect(reg.hasUIInteraction(e)).toBe(true);++ disposeWorld(world, registry);+ });++ it('honours raycastTarget: false', () => {+ const { world, registry } = createWorld();+ const opaque = sprite(world, 0, 0);++ // Prove the point IS pickable first: asserting only the miss would pass+ // just as well with no world-space pass at all.+ expect(pickAt(registry, 0, 0)).toBe(opaque);++ world.insert(opaque, Interactable, { raycastTarget: false });+ expect(pickAt(registry, 0, 0)).toBe(NO_HIT);++ disposeWorld(world, registry);+ });++ it('takes the higher layer when two overlap', () => {+ const { world, registry } = createWorld();+ sprite(world, 0, 0, { layer: 0 });+ const top = sprite(world, 0, 0, { layer: 5 });++ expect(pickAt(registry, 0, 0)).toBe(top);++ disposeWorld(world, registry);+ });++ it('scales the box with the transform', () => {+ const { world, registry } = createWorld();+ const e = sprite(world, 0, 0, { size: 100, scale: 3 });++ // 140 is outside the unscaled box (±50) and inside the scaled one (±150).+ expect(pickAt(registry, 140, 0)).toBe(e);++ disposeWorld(world, registry);+ });+});diff --git a/src/esengine/ui/UISystem.cpp b/src/esengine/ui/UISystem.cppindex f13c8cd2e..e1fac70ce 100644--- a/src/esengine/ui/UISystem.cpp+++ b/src/esengine/ui/UISystem.cpp@@ -15,6 +15,7 @@#include "../ecs/components/UIInteraction.hpp"#include "../ecs/components/UINode.hpp"#include "../ecs/components/UIVisual.hpp"+#include "../ecs/components/Sprite.hpp"#include <algorithm>#include <cstdint>@@ -74,6 +75,53 @@ void UISystem::hitTestUpdate(return;}}++ hitWorldSprites(registry, mouseWorldX, mouseWorldY);+}++/**+ * World-space pick, for entities the layout tree does not contain.+ *+ * The loop above takes its geometry from UINode, so a Sprite without one can+ * never be hit there. Its size and pivot are its visible extent, so that is the+ * box; UI wins any overlap, hence running only after a miss.+ */+void UISystem::hitWorldSprites(Registry& registry, f32 mouseWorldX, f32 mouseWorldY) {+ Entity best = INVALID_ENTITY;+ i32 bestLayer = 0;+ bool haveBest = false;++ registry.eachLive<Sprite, Interactable>(+ [&](Entity entity, Sprite& sprite, Interactable& interactable) {+ if (!interactable.enabled || !interactable.raycastTarget) return;+ // The layout tree owns anything with a UINode; this pass is for the rest.+ if (registry.has<UINode>(entity)) return;+ if (!registry.has<Transform>(entity)) return;++ auto& t = registry.get<Transform>(entity);+ t.ensureDecomposed();++ if (!pointInOBB(+ mouseWorldX, mouseWorldY,+ t.worldPosition.x, t.worldPosition.y,+ sprite.size.x * t.worldScale.x, sprite.size.y * t.worldScale.y,+ sprite.pivot.x, sprite.pivot.y,+ t.worldRotation.z, t.worldRotation.w+ )) return;++ // Draw order decides who is on top, and layer is what carries it. A tie+ // goes to the later entity, which is the order the renderer batches in.+ const i32 layer = static_cast<i32>(sprite.layer);+ if (!haveBest || layer >= bestLayer) {+ haveBest = true;+ bestLayer = layer;+ best = entity;+ }+ });++ if (!haveBest) return;+ registry.getOrEmplace<UIInteraction>(best);+ hitResult.hit_entity = best;}u32 UISystem::pickAll(Registry& registry, f32 worldX, f32 worldY) {diff --git a/src/esengine/ui/UISystem.hpp b/src/esengine/ui/UISystem.hppindex 4de9b099d..42b733080 100644--- a/src/esengine/ui/UISystem.hpp+++ b/src/esengine/ui/UISystem.hpp@@ -102,6 +102,9 @@ public:u32 getPrevHitEntity() const { return hitResult.prev_hit_entity.id(); }private:+ /** @brief World-space pick for entities outside the layout tree (see .cpp) */+ void hitWorldSprites(Registry& registry, f32 mouseWorldX, f32 mouseWorldY);+std::vector<Entity> pickResults_;// ---- Layout skip-when-clean gate (see layoutUpdate) ----