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(sdk): preview UI at a simulated device's aspect in the editor
esengine639e2952f1ee
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex 83b6350d2..2d0090f74 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -2176,6 +2176,7 @@ ResourceDef<EditorViewData>```active: booleanorthoSize: number+uiPreviewAspect: numberx: numbery: number```@@ -8656,7 +8657,7 @@ UILayoutPlugin## uiLayoutRect — function```-(cam: CameraExtents, canvas: CanvasScale | null, width: number, height: number): WorldRect+(cam: CameraExtents, canvas: CanvasScale | null, width: number, height: number, previewAspect?: number): WorldRect```## uiMaskPlugin — constdiff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.mdindex 34b347cf2..7b6ace18d 100644--- a/sdk/etc/index.node.api.md+++ b/sdk/etc/index.node.api.md@@ -2171,6 +2171,7 @@ ResourceDef<EditorViewData>```active: booleanorthoSize: number+uiPreviewAspect: numberx: numbery: number```@@ -8661,7 +8662,7 @@ UILayoutPlugin## uiLayoutRect — function```-(cam: CameraExtents, canvas: CanvasScale | null, width: number, height: number): WorldRect+(cam: CameraExtents, canvas: CanvasScale | null, width: number, height: number, previewAspect?: number): WorldRect```## uiMaskPlugin — constdiff --git a/sdk/src/camera/CameraPlugin.ts b/sdk/src/camera/CameraPlugin.tsindex 13c932e08..b64ed5085 100644--- a/sdk/src/camera/CameraPlugin.ts+++ b/sdk/src/camera/CameraPlugin.ts@@ -377,8 +377,11 @@ function syncUICameraInfo(uiCam.screenH = height;// The box UI lays out within. Scene cameras carry design-scaled extents;// the free-zoom editor view gets the fixed design box from the canvas so UI- // doesn't reflow with the zoom (see uiLayoutRect).- const rect = uiLayoutRect(cam, findCanvasData(module, cppRegistry), width, height);+ // doesn't reflow with the zoom (see uiLayoutRect). In the editor a selected+ // device preset overrides the box aspect (uiPreviewAspect) so UI previews how it+ // adapts on that device; 0 (default / shipped games) keeps the design aspect.+ const previewAspect = app.hasResource(EditorView) ? app.getResource(EditorView).uiPreviewAspect : 0;+ const rect = uiLayoutRect(cam, findCanvasData(module, cppRegistry), width, height, previewAspect);uiCam.worldLeft = rect.left;uiCam.worldRight = rect.right;uiCam.worldBottom = rect.bottom;diff --git a/sdk/src/camera/EditorView.ts b/sdk/src/camera/EditorView.tsindex e2cb34fb4..fd4e03d09 100644--- a/sdk/src/camera/EditorView.ts+++ b/sdk/src/camera/EditorView.ts@@ -26,8 +26,15 @@ export interface EditorViewData {y: number;/** Half-height of the view in world units (zoom; smaller = more zoomed in). */orthoSize: number;+ /**+ * Aspect ratio the editor lays UI out against, so the editor previews how UI adapts+ * on a simulated device: `> 0` fits the design resolution into this aspect (the device+ * simulator), `0` uses the authored design aspect — WYSIWYG at the design resolution.+ * Editor-only; scene cameras (shipped games) ignore it. See uiLayoutRect.+ */+ uiPreviewAspect: number;}-export const DEFAULT_EDITOR_VIEW: EditorViewData = { active: false, x: 0, y: 0, orthoSize: 360 };+export const DEFAULT_EDITOR_VIEW: EditorViewData = { active: false, x: 0, y: 0, orthoSize: 360, uiPreviewAspect: 0 };export const EditorView = defineResource<EditorViewData>({ ...DEFAULT_EDITOR_VIEW }, 'EditorView');diff --git a/sdk/src/camera/uiLayoutRect.ts b/sdk/src/camera/uiLayoutRect.tsindex 93dd2febe..3a40942b3 100644--- a/sdk/src/camera/uiLayoutRect.ts+++ b/sdk/src/camera/uiLayoutRect.ts@@ -90,19 +90,25 @@ export function uiLayoutRect(canvas: CanvasScale | null,width: number,height: number,+ previewAspect = 0,): WorldRect {let halfW = cam.halfW;let halfH = cam.halfH;if (cam.entity === EDITOR_VIEW_ENTITY && canvas && width > 0 && height > 0) {- // Lay UI out in the authored design box (1 unit = 1 design px), independent of the- // editor panel's aspect — the editor is then WYSIWYG with the design-frame overlay:- // edge-anchored UI sits at the design-resolution corners, exactly as it ships at- // that resolution. The free editor camera renders this fixed box through its zoomed- // viewProjection, so UI scales with zoom without reflowing. Previewing adaptation to- // a *different* screen aspect is the device simulator's concern, not the base box.- halfW = canvas.designResolution.x / 2;- halfH = canvas.designResolution.y / 2;+ // Lay UI out against the *preview screen*, independent of the editor panel's aspect —+ // so the editor is WYSIWYG with the design-frame overlay. `previewAspect > 0` fits the+ // design resolution into a simulated device's aspect (the device simulator: UI adapts+ // per scaleMode, exactly as the letterbox/device frame shows); `0` uses the design+ // aspect, where every scaleMode collapses to the exact authored box (edge-anchored UI+ // at the design-resolution corners). The free editor camera renders this fixed box+ // through its zoomed viewProjection, so UI scales with zoom without reflowing.+ const designAspect = canvas.designResolution.x / canvas.designResolution.y;+ const aspect = previewAspect > 0 ? previewAspect : designAspect;+ halfH = computeEffectiveOrthoSize(+ canvas.designResolution.y / 2, designAspect, aspect, canvas.scaleMode, canvas.matchWidthOrHeight,+ );+ halfW = halfH * aspect;}return {diff --git a/sdk/tests/ui-layout-rect.test.ts b/sdk/tests/ui-layout-rect.test.tsindex 9eb5969d3..b39ee6ab0 100644--- a/sdk/tests/ui-layout-rect.test.ts+++ b/sdk/tests/ui-layout-rect.test.ts@@ -47,6 +47,16 @@ describe('uiLayoutRect', () => {});});+ it('a selected device previews UI at the device aspect (device simulator)', () => {+ // A portrait device (aspect 0.5) on a 16:9 Expand canvas: Expand keeps the design+ // width and reveals height, so the UI box grows tall (letterbox top/bottom) — the+ // UI adapts exactly as the device/letterbox frame shows, not the design box.+ const cam = { entity: EDITOR_VIEW_ENTITY, cameraX: 0, cameraY: 0, halfW: 160, halfH: 90 };+ expect(uiLayoutRect(cam, canvas, 1000, 1000, 0.5)).toEqual({+ left: -640, right: 640, bottom: -1280, top: 1280,+ });+ });+it('the editor falls back to its own extents when the scene has no canvas', () => {const cam = { entity: EDITOR_VIEW_ENTITY, cameraX: 0, cameraY: 0, halfW: 160, halfH: 90 };expect(uiLayoutRect(cam, null, W, H)).toEqual({