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): Play In Viewport (UE5 PIE) + play-mode toggle (IM3)

esengine88c3aceb1987
diff --git a/desktop/src/App.tsx b/desktop/src/App.tsx
index 4dfba7e45..64b2bd6f1 100644
--- a/desktop/src/App.tsx
+++ b/desktop/src/App.tsx
@@ -59,8 +59,9 @@ export function App() {
useEditorStore.getState().stop();
return;
}
- dockApi.openGame();
void PlayRealm.start(payload);
+ // 'window' → a Game dock tab; 'viewport' → the Viewport mounts it (PIE).
+ if (useEditorStore.getState().playTarget === 'window') dockApi.openGame();
} else {
PlayRealm.stop();
dockApi.closeGame();
diff --git a/desktop/src/layout/Toolbar.tsx b/desktop/src/layout/Toolbar.tsx
index e9aae401b..055c40bed 100644
--- a/desktop/src/layout/Toolbar.tsx
+++ b/desktop/src/layout/Toolbar.tsx
@@ -14,6 +14,8 @@ import {
Square as Stop,
Eye,
Hammer,
+ Monitor,
+ AppWindow,
type LucideIcon,
} from 'lucide-react';
import { useEditorStore } from '@/store/editorStore';
@@ -65,6 +67,8 @@ function TBtn({
export function Toolbar() {
// Reactive display state (tool / toggles / play); actions dispatch through the
// command registry so menu, toolbar, and keyboard share one implementation.
+ const playTarget = useEditorStore((s) => s.playTarget);
+ const setPlayTarget = useEditorStore((s) => s.setPlayTarget);
const { tool, snapping, showGrid, showGizmos, isPlaying, isPaused, togglePause, stop } =
useEditorStore();
@@ -140,6 +144,15 @@ export function Toolbar() {
<button type="button" className="play-side" title="Stop" disabled={!isPlaying} onClick={stop}>
<Stop size={12} strokeWidth={1.9} fill="currentColor" />
</button>
+ <button
+ type="button"
+ className="play-side play-mode"
+ title={`Play in ${playTarget === 'viewport' ? 'Viewport' : 'New Window'} — click to switch`}
+ disabled={isPlaying}
+ onClick={() => setPlayTarget(playTarget === 'viewport' ? 'window' : 'viewport')}
+ >
+ {playTarget === 'viewport' ? <Monitor size={13} strokeWidth={1.9} /> : <AppWindow size={13} strokeWidth={1.9} />}
+ </button>
</div>
<span className="tspacer" />
diff --git a/desktop/src/panels/GamePanel.tsx b/desktop/src/panels/GamePanel.tsx
index e2076888d..d1c5b9faa 100644
--- a/desktop/src/panels/GamePanel.tsx
+++ b/desktop/src/panels/GamePanel.tsx
@@ -1,5 +1,6 @@
import { useEffect, useRef, useSyncExternalStore } from 'react';
import { PlayRealm } from '@/engine/PlayRealm';
+import { useEditorStore } from '@/store/editorStore';
// The "Game" dock panel: hosts the isolated play-realm iframe (PlayRealm owns the
// element + re-parents it here, so the realm survives panel remounts). The host
@@ -8,12 +9,17 @@ import { PlayRealm } from '@/engine/PlayRealm';
export function GamePanel() {
const hostRef = useRef<HTMLDivElement>(null);
const { playing, ready, error } = useSyncExternalStore(PlayRealm.subscribe, PlayRealm.getSnapshot);
+ const playTarget = useEditorStore((s) => s.playTarget);
+ // Only host the realm iframe in 'window' mode — in 'viewport' mode the Viewport
+ // owns it (one iframe, one mount). Guards against a stale Game tab stealing it.
useEffect(() => {
const el = hostRef.current;
- if (el) PlayRealm.attach(el);
- return () => PlayRealm.detach();
- }, []);
+ if (el && playTarget === 'window') PlayRealm.attach(el);
+ return () => {
+ if (playTarget === 'window') PlayRealm.detach();
+ };
+ }, [playTarget]);
const overlay = error
? `Play failed: ${error}`
diff --git a/desktop/src/panels/Viewport.tsx b/desktop/src/panels/Viewport.tsx
index 13db3d199..e13fc91f4 100644
--- a/desktop/src/panels/Viewport.tsx
+++ b/desktop/src/panels/Viewport.tsx
@@ -8,6 +8,7 @@ import { useEditorStore } from '@/store/editorStore';
import { useSelection } from '@/store/selectionStore';
import { commands } from '@/commands';
import { EngineHost } from '@/engine/EngineHost';
+import { PlayRealm } from '@/engine/PlayRealm';
import { ViewportController } from '@/engine/ViewportController';
import { SceneCommands } from '@/engine/SceneCommands';
import { SceneQuery } from '@/engine/SceneQuery';
@@ -121,12 +122,14 @@ function OvTool({
export function Viewport() {
const isPlaying = useEditorStore((s) => s.isPlaying);
+ const playTarget = useEditorStore((s) => s.playTarget);
const tool = useEditorStore((s) => s.tool);
const showGrid = useEditorStore((s) => s.showGrid);
const showGizmos = useEditorStore((s) => s.showGizmos);
const snapping = useEditorStore((s) => s.snapping);
const stageRef = useRef<HTMLDivElement>(null);
+ const playHostRef = useRef<HTMLDivElement>(null);
const gizmoRef = useRef<HTMLDivElement>(null);
const selectionRef = useRef<HTMLDivElement>(null);
const dragRef = useRef<Drag | null>(null);
@@ -154,6 +157,16 @@ export function Viewport() {
return () => EngineHost.detach();
}, []);
+ // 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';
+ useEffect(() => {
+ if (!playInViewport) return;
+ const host = playHostRef.current;
+ if (host) PlayRealm.attach(host);
+ return () => PlayRealm.detach();
+ }, [playInViewport]);
+
// Wheel = zoom about the view (native non-passive listener so we can preventDefault).
useEffect(() => {
const stage = stageRef.current;
@@ -398,6 +411,21 @@ export function Viewport() {
</div>
))}
+ {/* Play In Viewport: the realm iframe fills the stage; a thin badge marks PIE. */}
+ {playInViewport && (
+ <div className="viewport__play">
+ <div className="viewport__play-host" ref={playHostRef} />
+ <button
+ type="button"
+ className="viewport__play-stop"
+ title="Stop (Esc)"
+ onClick={() => useEditorStore.getState().stop()}
+ >
+ ● Playing · Stop
+ </button>
+ </div>
+ )}
+
<div ref={selectionRef} className="viewport__selection" aria-hidden="true" />
<div ref={gizmoRef} className="viewport__gizmo" aria-hidden="true">
diff --git a/desktop/src/store/editorStore.ts b/desktop/src/store/editorStore.ts
index d8cda8aa8..91e72a310 100644
--- a/desktop/src/store/editorStore.ts
+++ b/desktop/src/store/editorStore.ts
@@ -19,6 +19,9 @@ interface EditorState {
togglePlay: () => void;
togglePause: () => void;
stop: () => void;
+ // Where Play runs: in the Viewport (UE5 PIE, default) or a separate Game tab.
+ playTarget: 'viewport' | 'window';
+ setPlayTarget: (t: 'viewport' | 'window') => void;
// Launcher (project browser) vs editor shell. The editor opens on the
// launcher until a project is opened/created; `enterEditor` dismisses it.
@@ -65,6 +68,15 @@ export const useEditorStore = create<EditorState>((set) => ({
set((s) => ({ isPlaying: !s.isPlaying, isPaused: false })),
togglePause: () => set((s) => ({ isPaused: !s.isPaused })),
stop: () => set({ isPlaying: false, isPaused: false }),
+ // Guarded: this store is imported in pure-node tests where localStorage is absent.
+ playTarget:
+ (typeof localStorage !== 'undefined'
+ ? (localStorage.getItem('estella.playTarget') as 'viewport' | 'window' | null)
+ : null) || 'viewport',
+ setPlayTarget: (playTarget) => {
+ if (typeof localStorage !== 'undefined') localStorage.setItem('estella.playTarget', playTarget);
+ set({ playTarget });
+ },
showLauncher: true,
enterEditor: () => set({ showLauncher: false }),
diff --git a/desktop/src/theme/viewport.css b/desktop/src/theme/viewport.css
index 01584e5eb..e32fd7ea1 100644
--- a/desktop/src/theme/viewport.css
+++ b/desktop/src/theme/viewport.css
@@ -107,6 +107,31 @@
.vp-coord .ro strong { color: var(--text); }
.vp-coord .hint { color: var(--text-mute); font-family: var(--sans); padding-left: 2px; }
+/* Play In Viewport (UE5 PIE): the realm iframe overlays the stage while playing. */
+.viewport__play { position: absolute; inset: 0; z-index: 6; background: #0e121b; }
+.viewport__play-host { position: absolute; inset: 0; }
+.viewport__play-host iframe { width: 100%; height: 100%; border: 0; display: block; }
+.viewport__play-stop {
+ position: absolute;
+ top: 10px;
+ left: 50%;
+ transform: translateX(-50%);
+ z-index: 7;
+ height: 24px;
+ padding: 0 12px;
+ display: inline-flex;
+ align-items: center;
+ gap: 6px;
+ font-size: var(--fs-xs);
+ font-weight: 600;
+ color: #fff;
+ background: var(--run);
+ border-radius: var(--r-pill);
+ box-shadow: var(--shadow-2);
+ transition: background var(--t-fast);
+}
+.viewport__play-stop:hover { background: var(--run-hi); }
+
/* Game view (isolated play realm iframe) — REARCH_EDITOR_REALM Phase R. */
.game-panel { position: relative; width: 100%; height: 100%; background: #0e121b; overflow: hidden; }
.game-panel__host { position: absolute; inset: 0; }