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(editor): drive UI layout from the UI-mode device selection

esengine7509da85a442
diff --git a/desktop/src/App.tsx b/desktop/src/App.tsx
index d6b29fe16..072e76b28 100644
--- a/desktop/src/App.tsx
+++ b/desktop/src/App.tsx
@@ -21,6 +21,8 @@ import { commands } from '@/commands';
import { handleTilePaintKey } from '@/tools/tileMode';
import { suggestedMode } from '@/mode/activeMode';
import { useEditorMode } from '@/store/editorModeStore';
+import { uiPreviewAspect } from '@/mode/resolutionPresets';
+import { EngineHost } from '@/engine/EngineHost';
import type { EditorModeId } from '@/mode/editorModes';
import { useSelection } from '@/store/selectionStore';
import { PlayRealms } from '@/engine/PlayRealm';
@@ -85,6 +87,21 @@ export function App() {
});
}, []);
+ // Feed the UI-mode device selection into the editor UI layout: a chosen device preset
+ // lays UI out at its aspect (previewing adaptation), 'design' keeps the design box. Re-sync
+ // on any device/orientation change and on each engine (re)boot, since a fresh EditorView
+ // resets uiPreviewAspect to the design default.
+ useEffect(() => {
+ const apply = () => {
+ const { device, orientation } = useEditorMode.getState();
+ EngineHost.setUiPreviewAspect(uiPreviewAspect(device, orientation));
+ };
+ apply();
+ const unsubMode = useEditorMode.subscribe(apply);
+ const unsubEngine = EngineHost.subscribe(apply);
+ return () => { unsubMode(); unsubEngine(); };
+ }, []);
+
// Wire the Sequencer's edit-mode live preview (timeline document → World) and
// record-mode auto-key once.
useEffect(() => {
diff --git a/desktop/src/engine/EngineHost.ts b/desktop/src/engine/EngineHost.ts
index 9819d9c13..423d4eb37 100644
--- a/desktop/src/engine/EngineHost.ts
+++ b/desktop/src/engine/EngineHost.ts
@@ -294,6 +294,16 @@ class EngineHostImpl {
if (spacing != null && spacing > 0) grid.spacing = spacing;
}
+ /**
+ * The aspect the editor lays UI out against — a selected device preset's aspect, or 0
+ * for the design resolution (WYSIWYG). Drives uiLayoutRect via EditorView so a simulated
+ * device previews how the UI adapts. No-op pre-boot (the resource installs at plugin build).
+ */
+ setUiPreviewAspect(aspect: number): void {
+ const view = this.getResource(EditorView);
+ if (view) view.uiPreviewAspect = aspect;
+ }
+
/** The active (or first) scene camera's center + ortho half-height, for seeding. */
private readSceneCamera(): { x: number; y: number; orthoSize: number } | null {
const world = this.world;
diff --git a/desktop/src/mode/resolutionPresets.ts b/desktop/src/mode/resolutionPresets.ts
index 6da0347ab..1a46b526e 100644
--- a/desktop/src/mode/resolutionPresets.ts
+++ b/desktop/src/mode/resolutionPresets.ts
@@ -40,6 +40,30 @@ export const RESOLUTION_PRESET_BY_ID: Record<DevicePresetId, DevicePreset> = Obj
RESOLUTION_PRESETS.map((p) => [p.id, p]),
) as Record<DevicePresetId, DevicePreset>;
+export type Orientation = 'portrait' | 'landscape';
+
+/**
+ * A device preset's pixel dimensions with the orientation applied (portrait dims are
+ * swapped for landscape). Null for the `design` sentinel (no simulated device). The one
+ * source of the device's oriented size — the design-frame overlay and the UI-layout-aspect
+ * sync both read it, so the previewed frame and the actual UI layout can't drift apart.
+ */
+export function deviceDims(device: DevicePresetId, orientation: Orientation): { w: number; h: number } | null {
+ const p = RESOLUTION_PRESET_BY_ID[device];
+ if (p.w <= 0 || p.h <= 0) return null;
+ return orientation === 'landscape' ? { w: p.h, h: p.w } : { w: p.w, h: p.h };
+}
+
+/**
+ * The aspect the editor lays UI out against for the current device selection: the device's
+ * aspect (previewing adaptation), or 0 for the `design` sentinel (WYSIWYG at the authored
+ * resolution). Fed to EditorView.uiPreviewAspect → uiLayoutRect.
+ */
+export function uiPreviewAspect(device: DevicePresetId, orientation: Orientation): number {
+ const d = deviceDims(device, orientation);
+ return d ? d.w / d.h : 0;
+}
+
/**
* Common authored design resolutions for the viewport Design control. Unlike the
* device presets above (a transient preview simulation), picking one WRITES the
diff --git a/desktop/src/panels/Viewport.tsx b/desktop/src/panels/Viewport.tsx
index 48125daf1..07e2ea77b 100644
--- a/desktop/src/panels/Viewport.tsx
+++ b/desktop/src/panels/Viewport.tsx
@@ -12,7 +12,7 @@ import { useTilemapPaint, type PaintTool } from '@/store/tilemapPaintStore';
import { exitTilePaint, isTilePaintMode, selectedTilemapCellSize } from '@/tools/tileMode';
import { activeMode, activeModeOverlays } from '@/mode/activeMode';
import { useEditorMode } from '@/store/editorModeStore';
-import { RESOLUTION_PRESETS, RESOLUTION_PRESET_BY_ID, DESIGN_RESOLUTION_PRESETS } from '@/mode/resolutionPresets';
+import { RESOLUTION_PRESETS, RESOLUTION_PRESET_BY_ID, DESIGN_RESOLUTION_PRESETS, deviceDims } from '@/mode/resolutionPresets';
import { buildStampGhost } from '@/tools/tileStampGhost';
import { TilemapAPI, tileIdOf, UINode, DimensionUnit, computeEffectiveOrthoSize } from 'esengine';
import { commands } from '@/commands';
@@ -737,11 +737,13 @@ export function Viewport() {
const ms = useEditorMode.getState();
const preset = RESOLUTION_PRESET_BY_ID[ms.device];
// Device visible frame: the design resolution fit into the simulated device's
- // aspect per the Canvas scaleMode. The 'design' sentinel (w/h 0) = no device.
+ // aspect per the Canvas scaleMode. `dd` is the oriented device size (null for the
+ // 'design' sentinel) — the SAME source App.tsx feeds to uiLayoutRect, so this
+ // frame and the actual UI layout share one aspect and can't drift.
+ const dd = deviceDims(ms.device, ms.orientation);
let dev = des;
- if (preset.w > 0 && preset.h > 0) {
- const [dw, dh] = ms.orientation === 'landscape' ? [preset.h, preset.w] : [preset.w, preset.h];
- const deviceAspect = dw / dh;
+ if (dd) {
+ const deviceAspect = dd.w / dd.h;
const designAspect = ci.designResolution.x / ci.designResolution.y;
const halfH = computeEffectiveOrthoSize(
ci.designResolution.y / 2, designAspect, deviceAspect, ci.scaleMode, ci.matchWidthOrHeight,
@@ -762,7 +764,7 @@ export function Viewport() {
if (lb) {
const punch = (o: { x: number; y: number; w: number; h: number }): string =>
`M${o.x},${o.y}h${o.w}v${o.h}h${-o.w}Z M${des.x},${des.y}h${des.w}v${des.h}h${-des.w}Z`;
- const hasDevice = preset.w > 0 && preset.h > 0;
+ const hasDevice = dd != null;
const deviceContains =
dev.w >= des.w - 0.5 && dev.h >= des.h - 0.5 && (dev.w > des.w + 0.5 || dev.h > des.h + 0.5);
if (hasDevice && deviceContains) {
@@ -781,10 +783,9 @@ export function Viewport() {
// Safe-area inset within the device frame (device px → screen px by the frame ratio).
const safe = dsvg.querySelector('.df-safe') as SVGRectElement | null;
if (safe) {
- if (ms.showSafeArea && preset.safe && preset.w > 0) {
- const [dwPx, dhPx] = ms.orientation === 'landscape' ? [preset.h, preset.w] : [preset.w, preset.h];
- const sx = dev.w / dwPx;
- const sy = dev.h / dhPx;
+ if (ms.showSafeArea && preset.safe && dd) {
+ const sx = dev.w / dd.w;
+ const sy = dev.h / dd.h;
setRectAttrs(safe, {
x: dev.x + preset.safe.left * sx,
y: dev.y + preset.safe.top * sy,