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): UI selection outline — expose the resolved UI rect from the engine

esengineff76bcab5621
diff --git a/desktop/src/engine/ViewportController.ts b/desktop/src/engine/ViewportController.ts
index 7d7409a26..3448bcc01 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,
- UINode, UICameraInfo, screenToUiWorld, uiHitTestWorld, type UICameraData,
+ UINode, UICameraInfo, screenToUiWorld, uiWorldToScreen, uiHitTestWorld, type UICameraData,
} from 'esengine';
import type { EntityId } from '@/types';
import { EngineHost } from './EngineHost';
@@ -184,8 +184,45 @@ export const ViewportController = {
return { x: t.position.x, y: t.position.y };
},
+ /** A UI-camera world point → CSS px relative to the canvas (UI is screen-space). */
+ uiWorldToClient(cam: UICameraData, wx: number, wy: number): { x: number; y: number } | null {
+ const canvas = EngineHost.canvas;
+ if (!canvas) return null;
+ const s = uiWorldToScreen(cam, wx, wy);
+ const dpr = window.devicePixelRatio || 1;
+ 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 {
+ 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];
+ 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 = {
+ 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 }),
+ };
+ return screenAABB(obbCorners(obb).map(([wx, wy]) => this.uiWorldToClient(cam, wx, wy)));
+ },
+
/** Screen-space bounding rect (CSS px rel. canvas) of an entity, for the selection outline. */
getEntityScreenRect(id: EntityId): ClientRect | null {
+ if (EngineHost.world?.has(id, UINode)) return this.uiEntityScreenRect(id);
const b = this.entityBounds(id);
if (!b) return null;
return screenAABB(obbCorners(b).map(([wx, wy]) => this.worldToClient(wx, wy)));
diff --git a/sdk/src/core-ui.ts b/sdk/src/core-ui.ts
index 00b52db2c..70bf0416d 100644
--- a/sdk/src/core-ui.ts
+++ b/sdk/src/core-ui.ts
@@ -47,6 +47,7 @@ export {
makeInteractable,
UICameraInfo,
screenToUiWorld,
+ uiWorldToScreen,
uiHitTestWorld,
/** @internal fill-sprite sizing helper used by widget composition, unstable */
syncFillSpriteSize,
diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.ts
index 34337fef0..49d3d8f43 100644
--- a/sdk/src/ui/index.ts
+++ b/sdk/src/ui/index.ts
@@ -261,7 +261,7 @@ export {
type UICameraData,
} from './UICameraInfo';
-export { screenToUiWorld, uiHitTestWorld } from './uiPick';
+export { screenToUiWorld, uiWorldToScreen, uiHitTestWorld } from './uiPick';
export {
UILayoutGeneration,
diff --git a/sdk/src/ui/uiPick.ts b/sdk/src/ui/uiPick.ts
index b4dfbf097..9bf511284 100644
--- a/sdk/src/ui/uiPick.ts
+++ b/sdk/src/ui/uiPick.ts
@@ -3,7 +3,7 @@
import type { Entity } from '../types';
import type { ESEngineModule, CppRegistry } from '../wasm';
import type { UICameraData } from './UICameraInfo';
-import { screenToWorld, createInvVPCache } from './uiMath';
+import { screenToWorld, worldToScreen, createInvVPCache } from './uiMath';
const NO_HIT = 0xffffffff;
const vpCache = createInvVPCache();
@@ -14,6 +14,11 @@ export function screenToUiWorld(camera: UICameraData, screenGLX: number, screenG
return screenToWorld(screenGLX, screenGLY, invVP, camera.vpX, camera.vpY, camera.vpW, camera.vpH);
}
+export function uiWorldToScreen(camera: UICameraData, worldX: number, worldY: number): { x: number; y: number } {
+ const [x, y] = worldToScreen(worldX, worldY, camera.viewProjection, camera.vpX, camera.vpY, camera.vpW, camera.vpH);
+ return { x, y };
+}
+
export function uiHitTestWorld(
module: ESEngineModule,
registry: CppRegistry,
diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.ts
index cef9f0dff..6c6e0958f 100644
--- a/sdk/src/wasm.ts
+++ b/sdk/src/wasm.ts
@@ -341,6 +341,8 @@ export interface ESEngineModule {
uiHitTest_update(registry: CppRegistry, mouseWorldX: number, mouseWorldY: number, mouseDown: boolean, mousePressed: boolean, mouseReleased: boolean): void;
uiHitTest_getHitEntity(): number;
uiHitTest_getHitEntityPrev(): number;
+ uiNode_computedWidth(registry: CppRegistry, entity: number): number;
+ uiNode_computedHeight(registry: CppRegistry, entity: number): number;
uiRenderOrder_update(registry: CppRegistry): void;
uiFlexLayout_update(registry: CppRegistry): void;
getUINodeComputedWidth?(registry: CppRegistry, entity: number): number;
diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cpp
index 86724479f..2c4f29f52 100644
--- a/src/esengine/bindings/WebSDKEntry.cpp
+++ b/src/esengine/bindings/WebSDKEntry.cpp
@@ -584,6 +584,17 @@ u32 uiHitTest_getHitEntityPrev() {
return ctx().require<ecs::UISystem>().getPrevHitEntity();
}
+// 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) {
+ auto* n = r.tryGet<ecs::UINode>(Entity::fromRaw(e));
+ return n ? n->computed_size_.x : 0.0f;
+}
+f32 uiNode_computedHeight(ecs::Registry& r, u32 e) {
+ auto* n = r.tryGet<ecs::UINode>(Entity::fromRaw(e));
+ return n ? n->computed_size_.y : 0.0f;
+}
+
void uiRenderOrder_update(ecs::Registry& registry) {
ecs::uiRenderOrderUpdate(registry);
}
@@ -669,6 +680,8 @@ 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("uiNode_computedWidth", &esengine::uiNode_computedWidth);
+ emscripten::function("uiNode_computedHeight", &esengine::uiNode_computedHeight);
emscripten::function("uiRenderOrder_update", &esengine::uiRenderOrder_update);
emscripten::function("ui_getRenderOrder", &esengine::ui_getRenderOrder);
emscripten::function("uiFlexLayout_update", &esengine::uiFlexLayout_update);