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): align viewport overlays to the design + drive the grid

esengine9a9b542b205a
diff --git a/desktop/src/engine/EngineHost.ts b/desktop/src/engine/EngineHost.ts
index c9afe30a4..7abc99394 100644
--- a/desktop/src/engine/EngineHost.ts
+++ b/desktop/src/engine/EngineHost.ts
@@ -8,6 +8,8 @@ import {
Sprite,
Camera,
EditorView,
+ EditorGrid,
+ installEditorGrid,
setEditorMode,
setPlayMode,
} from 'esengine';
@@ -173,6 +175,19 @@ class EngineHostImpl {
view.active = !this.playing_;
}
+ /**
+ * Drive the editor reference grid from the editor's Show-Flags + Snap state.
+ * The renderer only paints when the EditorView is active (edit mode), so this
+ * just mirrors the user's intent; play/edit gating is handled there. No-op
+ * before the grid resource is installed (pre-boot).
+ */
+ setGrid(enabled: boolean, spacing?: number): void {
+ const grid = this.getResource(EditorGrid);
+ if (!grid) return;
+ grid.enabled = enabled;
+ if (spacing != null && spacing > 0) grid.spacing = spacing;
+ }
+
/** 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;
@@ -330,6 +345,12 @@ class EngineHostImpl {
setEditorMode(true);
setPlayMode(false);
+ // Editor-only world-space reference grid (drawn through the editor camera,
+ // occluded by scene entities). Inserts the EditorGrid resource + registers
+ // the pre-scene draw pass; the UI flips enabled/spacing via setGrid(). The
+ // renderer self-gates on EditorView.active, so it never draws in play mode.
+ installEditorGrid(app);
+
// Model-authoritative wiring (Reconciler model→World projection + SceneStore
// reactivity) is owned by the EditorSession (constructed at app/headless
// entry), not here — EngineHost is a pure engine host (REARCH_EDITOR_MODEL.md
diff --git a/desktop/src/panels/Viewport.tsx b/desktop/src/panels/Viewport.tsx
index 4fc00648a..2038a6d58 100644
--- a/desktop/src/panels/Viewport.tsx
+++ b/desktop/src/panels/Viewport.tsx
@@ -1,8 +1,8 @@
import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';
-import type { PointerEvent as ReactPointerEvent, DragEvent as ReactDragEvent } from 'react';
+import type { PointerEvent as ReactPointerEvent, DragEvent as ReactDragEvent, ReactNode } from 'react';
import {
- MousePointer2, Move, RotateCw, Scale3d, Grid3x3, Eye, Magnet, Frame,
- Camera, Loader2, TriangleAlert, type LucideIcon,
+ MousePointer2, Move, RotateCw, Scale3d, Grid3x3, Eye, Frame,
+ Camera, Check, ChevronDown, Loader2, TriangleAlert, type LucideIcon,
} from 'lucide-react';
import { useEditorStore } from '@/store/editorStore';
import { useSelection } from '@/store/selectionStore';
@@ -74,13 +74,22 @@ const TOOLS: { mode: ToolMode; icon: LucideIcon; label: string; key: string }[]
{ mode: 'scale', icon: Scale3d, label: 'Scale', key: 'R' },
];
-// Grid/angle/scale increments applied while the Snapping toggle is on. Fixed for
-// now — a future Preferences panel will make these user-configurable.
-const SNAP_MOVE = 10; // world units per grid step
+// Angle/scale snap increments applied while Snapping is on. Fixed for now — a
+// future Preferences panel will make these user-configurable. The Move step is
+// the user-chosen `snapStep` (viewport Snap dropdown), not a constant here.
const SNAP_ROTATE = 15; // degrees
const SNAP_SCALE = 0.1; // uniform scale step
+const SNAP_STEPS = [16, 32, 64]; // world units offered by the Snap dropdown
const snap = (v: number, step: number) => Math.round(v / step) * step;
+// One-line hint shown under the coord readout, reflecting the active tool.
+const TOOL_HINT: Record<ToolMode, string> = {
+ select: 'Click to select · drag empty to box-select',
+ move: 'Drag the entity to move · Shift to multi-select',
+ rotate: 'Drag around the entity to rotate',
+ scale: 'Drag out from the entity to scale',
+};
+
// Entity's screen-center in viewport (client) coordinates.
function entityClientCenter(id: number): { cx: number; cy: number } | null {
const pos = ViewportController.getEntityXY(id);
@@ -120,6 +129,74 @@ function OvTool({
);
}
+// A viewport overlay dropdown (UE5 "show flags" / "snap" menus): an .ovbtn
+// trigger with an icon, a label, and a chevron, plus a floating .dd-menu.
+// Closes on outside-click or after an item is chosen (the menu's onClick).
+function OvDropdown({
+ icon: Icon,
+ label,
+ align,
+ title,
+ children,
+}: {
+ icon: LucideIcon;
+ label: ReactNode;
+ align?: 'r';
+ title?: string;
+ children: ReactNode;
+}) {
+ const [open, setOpen] = useState(false);
+ const ref = useRef<HTMLDivElement>(null);
+ useEffect(() => {
+ if (!open) return;
+ const onDown = (e: MouseEvent) => {
+ if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
+ };
+ window.addEventListener('mousedown', onDown);
+ return () => window.removeEventListener('mousedown', onDown);
+ }, [open]);
+ return (
+ <div className={`dd${open ? ' open' : ''}`} ref={ref}>
+ <button
+ type="button"
+ className={`ovbtn${open ? ' open' : ''}`}
+ title={title}
+ aria-haspopup="menu"
+ aria-expanded={open}
+ onClick={() => setOpen((o) => !o)}
+ >
+ <Icon className="ic" size={13} strokeWidth={1.9} />
+ {label}
+ <ChevronDown className="cv" size={9} strokeWidth={2.5} />
+ </button>
+ {/* Item clicks bubble here to dismiss; each item runs its own handler. */}
+ <div className={`dd-menu${align === 'r' ? ' r' : ''}`} role="menu" onClick={() => setOpen(false)}>
+ {children}
+ </div>
+ </div>
+ );
+}
+
+// Multi-toggle menu row (checkbox box) — for the Show Flags menu.
+function DdCheck({ on, label, onClick }: { on: boolean; label: string; onClick: () => void }) {
+ return (
+ <div className={`dd-item${on ? ' on' : ''}`} role="menuitemcheckbox" aria-checked={on} onClick={onClick}>
+ <span className="chk">{on && <Check size={8} strokeWidth={3.5} />}</span>
+ <span className="l">{label}</span>
+ </div>
+ );
+}
+
+// Single-select menu row (tick mark, shown when active) — for the Snap menu.
+function DdRadio({ on, label, onClick }: { on: boolean; label: string; onClick: () => void }) {
+ return (
+ <div className={`dd-item${on ? ' on' : ''}`} role="menuitemradio" aria-checked={on} onClick={onClick}>
+ <span className="tk"><Check size={11} strokeWidth={3} /></span>
+ <span className="l">{label}</span>
+ </div>
+ );
+}
+
export function Viewport() {
const isPlaying = useEditorStore((s) => s.isPlaying);
const playTarget = useEditorStore((s) => s.playTarget);
@@ -127,6 +204,8 @@ export function Viewport() {
const showGrid = useEditorStore((s) => s.showGrid);
const showGizmos = useEditorStore((s) => s.showGizmos);
const snapping = useEditorStore((s) => s.snapping);
+ const snapStep = useEditorStore((s) => s.snapStep);
+ const selCount = useSelection((s) => s.selectedIds.size);
const stageRef = useRef<HTMLDivElement>(null);
const playHostRef = useRef<HTMLDivElement>(null);
@@ -158,6 +237,14 @@ export function Viewport() {
return () => EngineHost.detach();
}, []);
+ // Drive the engine's world-space editor grid from Show-Flags (Grid) + Snap
+ // step. Re-applied when the engine becomes ready, since the grid resource
+ // exists only after boot. Play/edit gating lives in the renderer (EditorView).
+ useEffect(() => {
+ if (engine.status !== 'ready') return;
+ EngineHost.setGrid(showGrid, snapStep);
+ }, [showGrid, snapStep, engine.status]);
+
// Play In Viewport (UE5 PIE): host the realm iframe over the stage while playing
// here; App.start() already booted the realm — we just re-parent its iframe.
const playInViewport = isPlaying && playTarget === 'viewport';
@@ -308,7 +395,11 @@ export function Viewport() {
if (wp) {
let x = wp.x + drag.dx;
let y = wp.y + drag.dy;
- if (snapOn) { x = snap(x, SNAP_MOVE); y = snap(y, SNAP_MOVE); }
+ if (snapOn) {
+ const step = useEditorStore.getState().snapStep;
+ x = snap(x, step);
+ y = snap(y, step);
+ }
SceneCommands.setEntityXY(drag.id, x, y);
}
} else if (drag.kind === 'rotate') {
@@ -360,7 +451,21 @@ export function Viewport() {
return (
<div className="viewport">
+ {/* Top-left: view menus (UE5 layout) — Show Flags dropdown + Frame. */}
<div className="ov ov-tl">
+ <div className="ov-cluster">
+ <OvDropdown icon={Eye} label="Show" title="Show Flags">
+ <div className="dd-lbl">Show Flags</div>
+ <DdCheck on={showGrid} label="Grid" onClick={() => commands.run('view.toggleGrid')} />
+ <DdCheck on={showGizmos} label="Gizmos" onClick={() => commands.run('view.toggleGizmos')} />
+ </OvDropdown>
+ <span className="ov-divider" />
+ <OvTool icon={Frame} label="Frame Selected (F)" kbd="F" onClick={() => commands.run('view.frameSelected')} />
+ </div>
+ </div>
+
+ {/* Top-right: transform tools (UE5 moved gizmo tools here) + Snap. */}
+ <div className="ov ov-tr">
<div className="ov-cluster">
{TOOLS.map((t) => (
<OvTool
@@ -373,11 +478,23 @@ export function Viewport() {
/>
))}
<span className="ov-divider" />
- <OvTool icon={Grid3x3} label="Show Grid" active={showGrid} onClick={() => commands.run('view.toggleGrid')} />
- <OvTool icon={Eye} label="Show Gizmos" active={showGizmos} onClick={() => commands.run('view.toggleGizmos')} />
- <OvTool icon={Magnet} label="Snapping" active={snapping} onClick={() => commands.run('view.toggleSnapping')} />
- <span className="ov-divider" />
- <OvTool icon={Frame} label="Frame Selected (F)" kbd="F" onClick={() => commands.run('view.frameSelected')} />
+ <OvDropdown
+ icon={Grid3x3}
+ label={<span className="val">{snapping ? snapStep : 'Off'}</span>}
+ align="r"
+ title="Grid Snap"
+ >
+ <div className="dd-lbl">Snap (units)</div>
+ <DdRadio on={!snapping} label="Off" onClick={() => useEditorStore.setState({ snapping: false })} />
+ {SNAP_STEPS.map((s) => (
+ <DdRadio
+ key={s}
+ on={snapping && snapStep === s}
+ label={String(s)}
+ onClick={() => useEditorStore.getState().setSnapStep(s)}
+ />
+ ))}
+ </OvDropdown>
</div>
</div>
@@ -464,6 +581,10 @@ export function Viewport() {
<span className="v">{stats.fps}</span>
</div>
<div className="ps" />
+ <div className="pr">
+ <span className="k">Frame</span>
+ <span className="v">{stats.fps > 0 ? (1000 / stats.fps).toFixed(1) : '—'} ms</span>
+ </div>
<div className="pr">
<span className="k">Entities</span>
<span className="v">{stats.entities}</span>
@@ -473,17 +594,17 @@ export function Viewport() {
<div className="vp-coord">
<div className="ro">
- {stats.cursor ? (
+ {stats.cursor && (
<>
<strong>
{stats.cursor.x}, {stats.cursor.y}
</strong>{' '}
- · {zoomPct}%
+ ·{' '}
</>
- ) : (
- `${zoomPct}%`
)}
+ Sel <strong>{selCount}</strong> · {zoomPct}%
</div>
+ <div className="hint">{TOOL_HINT[tool]}</div>
</div>
{isPlaying && <div className="viewport__playflag">● PLAY</div>}
diff --git a/desktop/src/store/editorStore.ts b/desktop/src/store/editorStore.ts
index 2a775e649..6c9252ed6 100644
--- a/desktop/src/store/editorStore.ts
+++ b/desktop/src/store/editorStore.ts
@@ -37,9 +37,14 @@ interface EditorState {
showGrid: boolean;
showGizmos: boolean;
snapping: boolean;
+ // Grid-snap increment (world units) applied to Move while `snapping` is on. The
+ // viewport snap dropdown picks from a fixed set (16 / 32 / 64); "off" flips
+ // `snapping` rather than changing the step, so the last step survives a re-enable.
+ snapStep: number;
toggleGrid: () => void;
toggleGizmos: () => void;
toggleSnapping: () => void;
+ setSnapStep: (step: number) => void;
// Content Drawer — a quick-access overlay: the Content Browser slides up over
// the workspace (Ctrl+Space), dismissing on outside click / Esc. It sits ON
@@ -91,9 +96,11 @@ export const useEditorStore = create<EditorState>((set) => ({
showGrid: true,
showGizmos: true,
snapping: false,
+ snapStep: 32,
toggleGrid: () => set((s) => ({ showGrid: !s.showGrid })),
toggleGizmos: () => set((s) => ({ showGizmos: !s.showGizmos })),
toggleSnapping: () => set((s) => ({ snapping: !s.snapping })),
+ setSnapStep: (snapStep) => set({ snapStep, snapping: true }),
contentDrawer: false,
toggleContentDrawer: () => set((s) => ({ contentDrawer: !s.contentDrawer })),
diff --git a/desktop/src/theme/viewport.css b/desktop/src/theme/viewport.css
index b3529b88b..2120d8b2d 100644
--- a/desktop/src/theme/viewport.css
+++ b/desktop/src/theme/viewport.css
@@ -57,6 +57,73 @@
.ov-tool:hover kbd { opacity: 1; }
.ov-divider { width: 1px; height: 16px; background: var(--border-line); margin: 0 3px; }
+/* overlay dropdown (view-menu / snap) — adopted from ue5/shell.css (.dd block) */
+.dd { position: relative; }
+.dd-menu {
+ position: absolute;
+ top: calc(100% + 6px);
+ min-width: 188px;
+ background: rgba(24, 24, 28, 0.96);
+ -webkit-backdrop-filter: blur(20px);
+ backdrop-filter: blur(20px);
+ border: 1px solid var(--border-line);
+ border-radius: var(--r-lg);
+ box-shadow: var(--shadow-3);
+ padding: 4px;
+ z-index: 8;
+ opacity: 0;
+ transform: translateY(-6px) scale(0.98);
+ transform-origin: top;
+ pointer-events: none;
+ transition: opacity var(--t-fast) var(--e-out), transform var(--t-fast) var(--e-out);
+}
+.dd.open .dd-menu { opacity: 1; transform: translateY(0) scale(1); pointer-events: auto; }
+.dd-menu.r { right: 0; }
+.dd-lbl {
+ font-size: var(--fs-2xs);
+ text-transform: uppercase;
+ letter-spacing: 0.6px;
+ color: var(--text-mute);
+ font-weight: 700;
+ padding: 6px 8px 4px;
+}
+.dd-item {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ height: 28px;
+ padding: 0 8px;
+ border-radius: var(--r-sm);
+ color: var(--text-dim);
+ font-size: var(--fs-sm);
+ cursor: pointer;
+ transition: color var(--t-fast), background var(--t-fast);
+}
+.dd-item:hover { color: var(--text); background: rgba(255, 255, 255, 0.06); }
+.dd-item .tk { width: 14px; display: inline-flex; color: var(--acc); opacity: 0; }
+.dd-item.on { color: var(--text); }
+.dd-item.on .tk { opacity: 1; }
+.dd-item .chk {
+ width: 13px;
+ height: 13px;
+ border: 1px solid var(--border-line);
+ border-radius: 3px;
+ display: grid;
+ place-items: center;
+ color: transparent;
+}
+.dd-item.on .chk { background: var(--acc); border-color: var(--acc); color: #fff; }
+.dd-item .l { flex: 1; }
+.dd-item .k {
+ margin-left: auto;
+ font-size: var(--fs-2xs);
+ color: var(--text-mute);
+ border: 1px solid var(--border-line);
+ border-radius: 2px;
+ padding: 1px 4px;
+}
+.dd-sep { height: 1px; background: var(--border-line); margin: 4px 6px; }
+
/* perf widget (bottom-right) */
.vp-perf {
position: absolute;