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(editor): make text and panels click-selectable + F-frame UI entities

yhhdfb2743d1971
diff --git a/desktop/src/engine/EditorControlSurface.ts b/desktop/src/engine/EditorControlSurface.ts
index 29f903a5c..8cb421121 100644
--- a/desktop/src/engine/EditorControlSurface.ts
+++ b/desktop/src/engine/EditorControlSurface.ts
@@ -26,6 +26,7 @@ import type {
import type { SceneData, SubsystemStatus } from 'esengine';
import { Material, Sprite } from 'esengine';
import { EngineHost } from './EngineHost';
+import { ViewportController } from './ViewportController';
import { PerfMonitor, type PerfSnapshot, type FrameSample, type SessionCapture } from './PerfMonitor';
import type { SceneCommandsImpl, EditorTransaction } from './SceneCommands';
import type { SceneQueryImpl } from './SceneQuery';
@@ -259,6 +260,16 @@ export class EditorControlSurfaceImpl {
return this.s.selection.subscribe(fn);
}
+ /** Viewport pick at a client position (what a click would select), or null. */
+ pick(clientX: number, clientY: number): EntityId | null {
+ return ViewportController.pickEntity(clientX, clientY);
+ }
+
+ /** Screen rect (CSS px rel. canvas) of an entity's selection bounds, or null. */
+ entityScreenRect(id: EntityId): { x: number; y: number; w: number; h: number } | null {
+ return ViewportController.getEntityScreenRect(id);
+ }
+
// =========================================================================
// Observation — runtime evidence for verification (the headless-gap closer)
// =========================================================================
diff --git a/desktop/src/engine/ViewportController.ts b/desktop/src/engine/ViewportController.ts
index c1116d0c0..c08101a62 100644
--- a/desktop/src/engine/ViewportController.ts
+++ b/desktop/src/engine/ViewportController.ts
@@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import {
Camera, CameraView, EditorView, Light2D, Sprite, Transform, Canvas, BoxCollider, CircleCollider,
- UINode, UICameraInfo, screenToUiWorld, uiWorldToScreen, uiHitTestWorld, type UICameraData,
+ UINode, UICameraInfo, screenToUiWorld, uiWorldToScreen, uiPickWorld, type UICameraData,
} from 'esengine';
import type { EntityId } from '@/types';
import { EngineHost } from './EngineHost';
@@ -117,18 +117,19 @@ export const ViewportController = {
};
},
- /** The UI entity under the pointer (engine UI hit-test), or null. */
+ /** The UI entity under the pointer (editor pick: any layout box, not just
+ * raycast targets — so plain text and panels are selectable), 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];
+ type Registry = Parameters<typeof uiPickWorld>[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);
+ const hit = uiPickWorld(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;
@@ -199,30 +200,36 @@ export const ViewportController = {
return { x: s.x / dpr, y: (canvas.height - s.y) / dpr };
},
- /**
- * Screen rect of a UI node's resolved layout box — its world OBB (Yoga-resolved
- * size × worldScale, pivot-centered on the world transform) projected through the
- * UI camera. Drives the selection outline for UI, which lives in screen space.
- */
- uiEntityScreenRect(id: EntityId): ClientRect | null {
+ /** World OBB of a UI node's resolved layout box (Yoga size × worldScale,
+ * pivot-centered on the world transform). */
+ uiEntityWorldOBB(id: EntityId): OBB | null {
const world = EngineHost.world;
const module = EngineHost.module;
- const cam = EngineHost.getResource(UICameraInfo) as UICameraData | undefined;
- if (!world || !module || !cam?.valid || !world.has(id, UINode) || !world.has(id, Transform)) return null;
- type Registry = Parameters<typeof uiHitTestWorld>[1];
+ if (!world || !module || !world.has(id, UINode) || !world.has(id, Transform)) return null;
+ type Registry = Parameters<typeof uiPickWorld>[1];
const reg = (world as unknown as { getCppRegistry(): Registry | null }).getCppRegistry();
if (!reg) return null;
const t = world.get(id, Transform);
const w = module.uiNode_computedWidth(reg, id) * t.worldScale.x;
const h = module.uiNode_computedHeight(reg, id) * t.worldScale.y;
if (!(w > 0) || !(h > 0)) return null;
- const obb: OBB = {
+ return {
cx: t.worldPosition.x,
cy: t.worldPosition.y,
hw: Math.abs(w) / 2,
hh: Math.abs(h) / 2,
rot: quatAngleZ(t.worldRotation as { w: number; x: number; y: number; z: number }),
};
+ },
+
+ /**
+ * Screen rect of a UI node's layout box — its world OBB projected through the
+ * UI camera. Drives the selection outline for UI, which lives in screen space.
+ */
+ uiEntityScreenRect(id: EntityId): ClientRect | null {
+ const cam = EngineHost.getResource(UICameraInfo) as UICameraData | undefined;
+ const obb = this.uiEntityWorldOBB(id);
+ if (!cam?.valid || !obb) return null;
return screenAABB(obbCorners(obb).map(([wx, wy]) => this.uiWorldToClient(cam, wx, wy)));
},
@@ -267,7 +274,8 @@ export const ViewportController = {
let maxX = -Infinity;
let maxY = -Infinity;
for (const id of ids) {
- const b = this.entityBounds(id);
+ // UI entities have no world OBB from entityBounds; frame their layout box.
+ const b = this.entityBounds(id) ?? this.uiEntityWorldOBB(id);
if (!b) continue;
for (const [wx, wy] of obbCorners(b)) {
minX = Math.min(minX, wx);
diff --git a/examples/ui-list/assets/scenes/main.esscene b/examples/ui-list/assets/scenes/main.esscene
index 10f0a99d1..6a74d4da4 100644
--- a/examples/ui-list/assets/scenes/main.esscene
+++ b/examples/ui-list/assets/scenes/main.esscene
@@ -66,7 +66,7 @@
"data": {
"direction": 1,
"justifyContent": 0,
- "alignItems": 0,
+ "alignItems": 3,
"gap": { "x": 0, "y": 10 },
"padding": { "left": 20, "top": 20, "right": 20, "bottom": 20 }
}
@@ -149,7 +149,7 @@
"data": {
"direction": 1,
"justifyContent": 0,
- "alignItems": 0,
+ "alignItems": 3,
"gap": { "x": 0, "y": 6 }
}
}
@@ -216,7 +216,7 @@
"data": {
"direction": 1,
"justifyContent": 0,
- "alignItems": 0,
+ "alignItems": 3,
"gap": { "x": 0, "y": 6 }
}
}
diff --git a/sdk/src/core-ui.ts b/sdk/src/core-ui.ts
index 1f18da877..423e50319 100644
--- a/sdk/src/core-ui.ts
+++ b/sdk/src/core-ui.ts
@@ -52,6 +52,7 @@ export {
screenToUiWorld,
uiWorldToScreen,
uiHitTestWorld,
+ uiPickWorld,
TextInput,
FillMethod,
FillOrigin,
diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.ts
index d8d222fbf..6c7365566 100644
--- a/sdk/src/ui/index.ts
+++ b/sdk/src/ui/index.ts
@@ -280,7 +280,7 @@ export {
type UICameraData,
} from './core/ui-camera-info';
-export { screenToUiWorld, uiWorldToScreen, uiHitTestWorld } from './util/ui-pick';
+export { screenToUiWorld, uiWorldToScreen, uiHitTestWorld, uiPickWorld } from './util/ui-pick';
export {
UILayoutGeneration,
diff --git a/sdk/src/ui/util/ui-pick.ts b/sdk/src/ui/util/ui-pick.ts
index a054d56b7..ccbdb232f 100644
--- a/sdk/src/ui/util/ui-pick.ts
+++ b/sdk/src/ui/util/ui-pick.ts
@@ -32,3 +32,19 @@ export function uiHitTestWorld(
const hit = module.uiHitTest_getHitEntity();
return hit === NO_HIT ? null : hit;
}
+
+/**
+ * Editor pick: topmost UI entity whose layout box contains the point,
+ * regardless of Interactable, without touching interaction state. Use this
+ * for editor selection; `uiHitTestWorld` is the runtime raycast.
+ */
+export function uiPickWorld(
+ module: ESEngineModule,
+ registry: CppRegistry,
+ worldX: number,
+ worldY: number,
+): Entity | null {
+ if (!module.uiHitTest_pick) return null;
+ const hit = module.uiHitTest_pick(registry, worldX, worldY);
+ return hit === NO_HIT ? null : hit;
+}
diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.ts
index faa754570..575891b05 100644
--- a/sdk/src/wasm.ts
+++ b/sdk/src/wasm.ts
@@ -355,6 +355,7 @@ export interface ESEngineModule {
uiHitTest_update(registry: CppRegistry, mouseWorldX: number, mouseWorldY: number, mouseDown: boolean, mousePressed: boolean, mouseReleased: boolean): void;
uiHitTest_getHitEntity(): number;
uiHitTest_getHitEntityPrev(): number;
+ uiHitTest_pick?(registry: CppRegistry, worldX: number, worldY: number): number;
uiNode_computedWidth(registry: CppRegistry, entity: number): number;
uiNode_computedHeight(registry: CppRegistry, entity: number): number;
uiRenderOrder_update(registry: CppRegistry): void;
diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cpp
index f181d2daf..b19b6a717 100644
--- a/src/esengine/bindings/WebSDKEntry.cpp
+++ b/src/esengine/bindings/WebSDKEntry.cpp
@@ -592,6 +592,12 @@ u32 uiHitTest_getHitEntityPrev() {
return ctx().require<ecs::UISystem>().getPrevHitEntity();
}
+// Editor pick: ignores Interactable and mutates no interaction state, so the
+// editor can select plain text / panels without polluting runtime hover.
+u32 uiHitTest_pick(ecs::Registry& registry, f32 worldX, f32 worldY) {
+ return ctx().require<ecs::UISystem>().pick(registry, worldX, worldY);
+}
+
// Resolved (Yoga-pass) pixel size of a UI node, for the editor's selection outline.
// The node's world box is this size, pivot-centered on its Transform.
f32 uiNode_computedWidth(ecs::Registry& r, u32 e) {
@@ -688,6 +694,7 @@ EMSCRIPTEN_BINDINGS(esengine_ui_systems) {
emscripten::function("uiHitTest_update", &esengine::uiHitTest_update);
emscripten::function("uiHitTest_getHitEntity", &esengine::uiHitTest_getHitEntity);
emscripten::function("uiHitTest_getHitEntityPrev", &esengine::uiHitTest_getHitEntityPrev);
+ emscripten::function("uiHitTest_pick", &esengine::uiHitTest_pick);
emscripten::function("uiNode_computedWidth", &esengine::uiNode_computedWidth);
emscripten::function("uiNode_computedHeight", &esengine::uiNode_computedHeight);
emscripten::function("uiRenderOrder_update", &esengine::uiRenderOrder_update);
diff --git a/src/esengine/ecs/UISystem.cpp b/src/esengine/ecs/UISystem.cpp
index 64977bc5f..17ffd7afb 100644
--- a/src/esengine/ecs/UISystem.cpp
+++ b/src/esengine/ecs/UISystem.cpp
@@ -14,6 +14,7 @@
#include "components/Transform.hpp"
#include "components/UIInteraction.hpp"
#include "components/UINode.hpp"
+#include "components/UIVisual.hpp"
namespace esengine::ecs {
@@ -69,4 +70,53 @@ void UISystem::hitTestUpdate(
}
}
+u32 UISystem::pick(Registry& registry, f32 worldX, f32 worldY) const {
+ const auto& nodes = tree.nodes_;
+
+ // Rank hits by hierarchy depth (the most specific element under the
+ // cursor — clicking a label picks the label, not its panel), breaking
+ // ties by draw order.
+ Entity best = INVALID_ENTITY;
+ i32 bestDepth = -1;
+ i32 bestOrder = INT32_MIN;
+
+ for (i32 i = static_cast<i32>(nodes.size()) - 1; i >= 0; i--) {
+ Entity entity = nodes[i].entity;
+ if (!registry.has<Transform>(entity)) continue;
+
+ auto* node = registry.tryGet<UINode>(entity);
+ if (!node || node->computed_size_.x <= 0.0f || node->computed_size_.y <= 0.0f) continue;
+
+ auto& t = registry.get<Transform>(entity);
+ t.ensureDecomposed();
+
+ if (!pointInOBB(
+ worldX, worldY,
+ t.worldPosition.x, t.worldPosition.y,
+ node->computed_size_.x * t.worldScale.x,
+ node->computed_size_.y * t.worldScale.y,
+ 0.5f, 0.5f,
+ t.worldRotation.z, t.worldRotation.w
+ )) continue;
+
+ if (isClippedByMask(registry, entity, worldX, worldY)) continue;
+
+ i32 depth = 0;
+ for (Entity a = entity; registry.has<Parent>(a);) {
+ Entity parent = registry.get<Parent>(a).entity;
+ if (!registry.valid(parent)) break;
+ depth++;
+ a = parent;
+ }
+ auto* vis = registry.tryGet<UIVisual>(entity);
+ const i32 order = vis ? vis->uiOrder : INT32_MIN;
+ if (depth > bestDepth || (depth == bestDepth && order > bestOrder)) {
+ bestDepth = depth;
+ bestOrder = order;
+ best = entity;
+ }
+ }
+ return best.id();
+}
+
} // namespace esengine::ecs
diff --git a/src/esengine/ecs/UISystem.hpp b/src/esengine/ecs/UISystem.hpp
index f41f0c7db..74a2f32fd 100644
--- a/src/esengine/ecs/UISystem.hpp
+++ b/src/esengine/ecs/UISystem.hpp
@@ -72,6 +72,14 @@ public:
f32 mouseWorldX, f32 mouseWorldY,
bool mouseDown, bool mousePressed, bool mouseReleased);
+ /**
+ * @brief Editor pick: topmost UI entity whose layout box contains the point.
+ *
+ * Unlike hitTestUpdate this ignores Interactable — plain text and panels
+ * are selectable — and mutates no interaction state.
+ */
+ u32 pick(Registry& registry, f32 worldX, f32 worldY) const;
+
/** @brief Entity hit by the most recent hitTestUpdate (or INVALID_ENTITY) */
u32 getHitEntity() const { return hitResult.hit_entity.id(); }