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): edit design resolution from the viewport

esengine0225b42b9ccc
diff --git a/desktop/src/mode/resolutionPresets.ts b/desktop/src/mode/resolutionPresets.ts
index 223d4bef6..6da0347ab 100644
--- a/desktop/src/mode/resolutionPresets.ts
+++ b/desktop/src/mode/resolutionPresets.ts
@@ -39,3 +39,24 @@ export const RESOLUTION_PRESETS: DevicePreset[] = [
export const RESOLUTION_PRESET_BY_ID: Record<DevicePresetId, DevicePreset> = Object.fromEntries(
RESOLUTION_PRESETS.map((p) => [p.id, p]),
) as Record<DevicePresetId, DevicePreset>;
+
+/**
+ * Common authored design resolutions for the viewport Design control. Unlike the
+ * device presets above (a transient preview simulation), picking one WRITES the
+ * Canvas' `designResolution` — the authoritative value the scene is designed against.
+ * Exact/custom numbers are edited on the Canvas component in the Inspector.
+ */
+export interface DesignResolutionPreset {
+ label: string;
+ x: number;
+ y: number;
+}
+
+export const DESIGN_RESOLUTION_PRESETS: DesignResolutionPreset[] = [
+ { label: '1920 × 1080', x: 1920, y: 1080 },
+ { label: '1280 × 720', x: 1280, y: 720 },
+ { label: '1080 × 1920', x: 1080, y: 1920 },
+ { label: '750 × 1334', x: 750, y: 1334 },
+ { label: '1170 × 2532', x: 1170, y: 2532 },
+ { label: '2048 × 1536', x: 2048, y: 1536 },
+];
diff --git a/desktop/src/panels/Viewport.tsx b/desktop/src/panels/Viewport.tsx
index 989fa266b..138046340 100644
--- a/desktop/src/panels/Viewport.tsx
+++ b/desktop/src/panels/Viewport.tsx
@@ -4,7 +4,7 @@ import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'reac
import type { PointerEvent as ReactPointerEvent, DragEvent as ReactDragEvent, ReactNode } from 'react';
import {
MousePointer2, Move, RotateCw, Scale3d, Grid3x3, Eye, Frame,
- Camera, Check, ChevronDown, Loader2, TriangleAlert, Lightbulb, Sparkles, Globe, Crosshair, Smartphone, type LucideIcon,
+ Camera, Check, ChevronDown, Loader2, TriangleAlert, Lightbulb, Sparkles, Globe, Crosshair, Smartphone, Monitor, type LucideIcon,
} from 'lucide-react';
import { useEditorStore } from '@/store/editorStore';
import { useSelection } from '@/store/selectionStore';
@@ -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 } from '@/mode/resolutionPresets';
+import { RESOLUTION_PRESETS, RESOLUTION_PRESET_BY_ID, DESIGN_RESOLUTION_PRESETS } from '@/mode/resolutionPresets';
import { buildStampGhost } from '@/tools/tileStampGhost';
import { TilemapAPI, tileIdOf, UINode, DimensionUnit, computeEffectiveOrthoSize } from 'esengine';
import { commands } from '@/commands';
@@ -487,6 +487,18 @@ export function Viewport() {
const device = useEditorMode((s) => s.device);
const orientation = useEditorMode((s) => s.orientation);
const showSafeArea = useEditorMode((s) => s.showSafeArea);
+ // The scene's Canvas + its authored design resolution — the value the Design control
+ // edits (the single source of truth). Re-read on any model change so the label tracks.
+ const dataRev = useSyncExternalStore(SceneStore.subscribe, SceneStore.getRevision);
+ const sceneCanvas = useMemo(() => {
+ void dataRev;
+ const id = SceneCommands.findCanvas();
+ if (id == null) return null;
+ const d = SceneModel.entityBySource(id)?.components.find((c) => c.type === 'Canvas')?.data as
+ | { designResolution?: { x: number; y: number } }
+ | undefined;
+ return { id, x: d?.designResolution?.x ?? 0, y: d?.designResolution?.y ?? 0 };
+ }, [dataRev]);
// WYSIWYG brush ghost: the actual stamp tiles, built once per stamp/atlas change and
// laid out at natural tile pixels; the rAF scales the container to the hovered
// footprint each frame (see the tile-preview block). Empty (null) → the plain box shows.
@@ -508,6 +520,7 @@ export function Viewport() {
const gizmoRef = useRef<HTMLDivElement>(null);
const uiGizmoRef = useRef<HTMLDivElement>(null);
const designSvgRef = useRef<SVGSVGElement>(null);
+ const designLabelRef = useRef<HTMLDivElement>(null);
// One outline div per selected entity, keyed by source id and positioned by the rAF.
const selRefs = useRef(new Map<number, HTMLDivElement | null>());
const marqueeRef = useRef<HTMLDivElement>(null);
@@ -726,8 +739,17 @@ export function Viewport() {
safe.style.opacity = '0';
}
}
+ // The authored resolution, pinned to the frame's top-left corner.
+ const dlabel = designLabelRef.current;
+ if (dlabel) {
+ dlabel.textContent = `${ci.designResolution.x} × ${ci.designResolution.y}`;
+ dlabel.style.transform = `translate(${des.x + 4}px, ${des.y + 4}px)`;
+ dlabel.style.opacity = '1';
+ }
} else {
dsvg.style.opacity = '0';
+ const dlabel = designLabelRef.current;
+ if (dlabel) dlabel.style.opacity = '0';
}
}
@@ -1230,10 +1252,28 @@ export function Viewport() {
{mode.overlays?.designFrame && (
<>
<span className="ov-divider" />
+ {sceneCanvas && (
+ <OvDropdown
+ icon={Monitor}
+ label={<span className="val">{sceneCanvas.x}×{sceneCanvas.y}</span>}
+ title="Design resolution — the canvas you author against. Picking a preset writes Canvas.designResolution (undoable)."
+ >
+ <div className="ovmenu-lbl">Design Resolution</div>
+ {DESIGN_RESOLUTION_PRESETS.map((p) => (
+ <DdRadio
+ key={p.label}
+ on={sceneCanvas.x === p.x && sceneCanvas.y === p.y}
+ label={p.label}
+ onClick={() => SceneCommands.setField(sceneCanvas.id, 'Canvas', 'designResolution', 'vec2', [p.x, p.y])}
+ />
+ ))}
+ <div className="ovmenu-lbl">Exact values: select the Canvas → Inspector</div>
+ </OvDropdown>
+ )}
<OvDropdown
icon={Smartphone}
label={<span className="val">{RESOLUTION_PRESET_BY_ID[device].label}</span>}
- title="Preview Device (design-resolution fit)"
+ title="Preview device — simulates a target screen (does NOT change the design resolution)"
>
<div className="ovmenu-lbl">Device</div>
{RESOLUTION_PRESETS.map((p) => (
@@ -1539,6 +1579,7 @@ export function Viewport() {
<rect className="df-safe" />
<rect className="df-design" />
</svg>
+ <div ref={designLabelRef} className="viewport__design-label" style={{ opacity: 0 }} aria-hidden="true" />
<div ref={uiGizmoRef} className="viewport__ui-gizmo" style={{ opacity: 0 }}>
<span
diff --git a/desktop/src/theme/viewport.css b/desktop/src/theme/viewport.css
index 0b2421b0b..33aabf8dc 100644
--- a/desktop/src/theme/viewport.css
+++ b/desktop/src/theme/viewport.css
@@ -120,6 +120,8 @@
.viewport__design-svg .df-design { fill: none; stroke: var(--acc); stroke-width: 1.5; }
.viewport__design-svg .df-device { fill: none; stroke: var(--text-mute); stroke-width: 1; stroke-dasharray: 5 4; }
.viewport__design-svg .df-safe { fill: none; stroke: var(--acc); stroke-width: 1; stroke-dasharray: 2 3; opacity: 0.6; }
+/* Design-resolution readout pinned to the frame's top-left corner (rAF-positioned). */
+.viewport__design-label { position: absolute; top: 0; left: 0; z-index: 6; pointer-events: none; padding: 1px 5px; font-size: var(--fs-2xs); font-variant-numeric: tabular-nums; color: var(--on-accent); background: var(--acc); border-radius: var(--r-sm); white-space: nowrap; }
/* perf widget (bottom-right) */
.vp-perf {
diff --git a/desktop/tests/engine-model-sync.test.ts b/desktop/tests/engine-model-sync.test.ts
index adb8c9612..05fd58bc6 100644
--- a/desktop/tests/engine-model-sync.test.ts
+++ b/desktop/tests/engine-model-sync.test.ts
@@ -11,7 +11,7 @@
* the scene into the (mocked) headless World; commands flow model→World through it.
*/
import { describe, it, expect, beforeAll, beforeEach, afterEach, vi } from 'vitest';
-import { App, Transform, Parent, Sprite } from 'esengine';
+import { App, Transform, Parent, Sprite, Canvas } from 'esengine';
import type { ESEngineModule, SceneData } from 'esengine';
import { loadWasmModule, HAS_WASM } from './helpers/loadWasm';
@@ -148,6 +148,19 @@ describe.skipIf(!HAS_WASM)('Model-authoritative projection + lossless save', ()
expect(u.parent ?? null).toBeNull();
});
+ it('setField writes a Canvas designResolution to the model AND the World (the viewport Design control path)', () => {
+ const canvas = S.model.addEntity('Canvas', [{ type: 'Transform', data: {} }, { type: 'Canvas', data: {} }] as never);
+ S.commands.setField(canvas, 'Canvas', 'designResolution', 'vec2', [1080, 1920]);
+
+ const d = S.model.entityBySource(canvas)!.components.find((c) => c.type === 'Canvas')!.data as {
+ designResolution: { x: number; y: number };
+ };
+ expect(d.designResolution).toMatchObject({ x: 1080, y: 1920 });
+
+ const cw = host.world.get(S.model.runtimeFor(canvas)!, Canvas) as { designResolution: { x: number; y: number } };
+ expect(cw.designResolution).toMatchObject({ x: 1080, y: 1920 });
+ });
+
it('attachUINodeBox is a no-op when the Text already has a UINode', () => {
const canvas = S.model.addEntity('Canvas', [{ type: 'Transform', data: {} }, { type: 'Canvas', data: {} }] as never);
const text = S.model.addEntity('Label', [{ type: 'Transform', data: {} }, { type: 'UINode', data: {} }, { type: 'Text', data: {} }] as never);