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: shared scene/game view with offscreen WebGL canvas

esenginecf1151ccdac0
diff --git a/editor/src/panels/game-view/GameInstanceManager.ts b/editor/src/panels/game-view/GameInstanceManager.ts
index c67bf02ac..177126851 100644
--- a/editor/src/panels/game-view/GameInstanceManager.ts
+++ b/editor/src/panels/game-view/GameInstanceManager.ts
@@ -1,5 +1,3 @@
-import { getEditorInstance } from '../../context/EditorContext';
-
export type GameState = 'stopped' | 'playing' | 'paused';
export interface GameInstanceCallbacks {
@@ -10,7 +8,6 @@ export interface GameInstanceCallbacks {
export class GameInstanceManager {
private state_: GameState = 'stopped';
private callbacks_: GameInstanceCallbacks;
- private previewUrl_: string | null = null;
constructor(callbacks: GameInstanceCallbacks) {
this.callbacks_ = callbacks;
@@ -20,34 +17,6 @@ export class GameInstanceManager {
return this.state_;
}
- get previewUrl(): string | null {
- return this.previewUrl_;
- }
-
- async play(): Promise<string | null> {
- if (this.state_ === 'playing') return null;
-
- const editor = getEditorInstance();
- if (!editor) {
- this.callbacks_.onError(new Error('Editor not available'));
- return null;
- }
-
- try {
- const url = await editor.startPreviewServer();
- if (!url) {
- this.callbacks_.onError(new Error('Failed to start preview server'));
- return null;
- }
- this.previewUrl_ = url;
- this.setState('playing');
- return url;
- } catch (e) {
- this.callbacks_.onError(e instanceof Error ? e : new Error(String(e)));
- return null;
- }
- }
-
pause(): void {
if (this.state_ === 'playing') {
this.setState('paused');
@@ -61,7 +30,6 @@ export class GameInstanceManager {
}
async stop(): Promise<void> {
- this.previewUrl_ = null;
this.setState('stopped');
}
@@ -69,7 +37,7 @@ export class GameInstanceManager {
await this.stop();
}
- private setState(state: GameState): void {
+ setState(state: GameState): void {
this.state_ = state;
this.callbacks_.onStateChange(state);
}
diff --git a/editor/src/panels/game-view/GameViewPanel.ts b/editor/src/panels/game-view/GameViewPanel.ts
index 162106fdf..6208b42f6 100644
--- a/editor/src/panels/game-view/GameViewPanel.ts
+++ b/editor/src/panels/game-view/GameViewPanel.ts
@@ -2,7 +2,8 @@ import type { EditorStore } from '../../store/EditorStore';
import type { PanelInstance, Resizable } from '../PanelRegistry';
import { GameInstanceManager, type GameState } from './GameInstanceManager';
import { GameViewToolbar } from './GameViewToolbar';
-import { GameViewBridge } from './GameViewBridge';
+import { GameViewRenderer } from '../../renderer/GameViewRenderer';
+import { getSharedRenderContext } from '../../renderer/SharedRenderContext';
import { getPlayModeService } from '../../services/PlayModeService';
export interface GameViewPanelOptions {
@@ -11,13 +12,15 @@ export interface GameViewPanelOptions {
export class GameViewPanel implements PanelInstance, Resizable {
private container_: HTMLElement;
- private iframe_: HTMLIFrameElement | null = null;
private toolbar_: GameViewToolbar;
private gameManager_: GameInstanceManager;
- private bridge_: GameViewBridge | null = null;
private canvasArea_: HTMLElement | null = null;
private placeholder_: HTMLElement | null = null;
- private eventCleanups_: (() => void)[] = [];
+ private canvas2d_: HTMLCanvasElement | null = null;
+ private gameRenderer_: GameViewRenderer | null = null;
+ private inputCleanup_: (() => void) | null = null;
+ private resizeObserver_: ResizeObserver | null = null;
+ private scale_ = 1;
constructor(container: HTMLElement, _store: EditorStore, _options?: GameViewPanelOptions) {
this.container_ = container;
@@ -33,19 +36,29 @@ export class GameViewPanel implements PanelInstance, Resizable {
onPause: () => this.pause(),
onResume: () => this.resume(),
onStepFrame: () => this.stepFrame(),
- onSpeedChange: (speed) => this.bridge_?.setSpeed(speed),
- onResolutionChange: () => this.updateIframeSize(),
+ onSpeedChange: (speed) => this.onSpeedChange(speed),
+ onResolutionChange: () => this.updateCanvasSize(),
+ onScaleChange: (scale) => {
+ this.scale_ = scale;
+ this.updateCanvasSize();
+ },
});
this.buildUI();
+ this.initGameRenderer();
}
resize(): void {
- this.updateIframeSize();
+ this.updateCanvasSize();
}
dispose(): void {
- this.disposeBridge();
+ if (this.resizeObserver_) {
+ this.resizeObserver_.disconnect();
+ this.resizeObserver_ = null;
+ }
+ this.cleanupInput();
+ this.detachRenderer();
this.gameManager_.dispose();
this.toolbar_.dispose();
}
@@ -60,137 +73,260 @@ export class GameViewPanel implements PanelInstance, Resizable {
</div>
<div class="es-gameview-viewport">
<div class="es-gameview-canvas-area">
- <iframe class="es-gameview-iframe"></iframe>
+ <canvas class="es-gameview-canvas"></canvas>
<div class="es-gameview-placeholder">
- <span>Click Play to start</span>
+ <span>Game Camera Preview</span>
</div>
</div>
</div>
`;
- this.iframe_ = this.container_.querySelector('.es-gameview-iframe')!;
+ this.canvas2d_ = this.container_.querySelector('.es-gameview-canvas')!;
this.canvasArea_ = this.container_.querySelector('.es-gameview-canvas-area')!;
this.placeholder_ = this.container_.querySelector('.es-gameview-placeholder')!;
this.toolbar_.setup();
- this.updateIframeSize();
+
+ if (this.canvasArea_) {
+ this.resizeObserver_ = new ResizeObserver(() => this.updateCanvasSize());
+ this.resizeObserver_.observe(this.canvasArea_);
+ }
+
+ this.updateCanvasSize();
+ }
+
+ private initGameRenderer(): void {
+ const ctx = getSharedRenderContext();
+ if (!ctx.initialized || !ctx.webglCanvas_ || !this.canvas2d_) {
+ ctx.onceInitialized(() => this.initGameRenderer());
+ return;
+ }
+
+ if (this.gameRenderer_) return;
+ this.gameRenderer_ = new GameViewRenderer(this.canvas2d_, ctx.webglCanvas_);
+ ctx.setGameViewRenderer(this.gameRenderer_);
+ this.updateCanvasSize();
+ }
+
+ tryInitRenderer(): void {
+ if (this.gameRenderer_) return;
+ this.initGameRenderer();
+ }
+
+ private detachRenderer(): void {
+ if (this.gameRenderer_) {
+ const ctx = getSharedRenderContext();
+ ctx.setGameViewRenderer(null);
+ ctx.setGameViewportSize(0, 0);
+ this.gameRenderer_.dispose();
+ this.gameRenderer_ = null;
+ }
}
private async play(): Promise<void> {
- const url = await this.gameManager_.play();
- if (url && this.iframe_) {
- this.iframe_.src = url + '?t=' + Date.now();
- this.toolbar_.setPreviewUrl(url);
- this.createBridge();
+ const service = getPlayModeService();
+ service.enterShared();
+ this.gameManager_.setState('playing');
+ if (this.gameRenderer_) {
+ this.gameRenderer_.setVisible(true);
}
+ this.updateCanvasSize();
+ this.setupInput();
}
- private async pause(): Promise<void> {
- if (!this.bridge_?.isReady) return;
- try {
- await this.bridge_.pause();
- this.gameManager_.pause();
- } catch { /* ignore timeout */ }
+ private pause(): void {
+ getSharedRenderContext().pausePlay();
+ this.gameManager_.pause();
}
- private async resume(): Promise<void> {
- if (!this.bridge_?.isReady) return;
- try {
- await this.bridge_.resume();
- this.gameManager_.resume();
- } catch { /* ignore timeout */ }
+ private resume(): void {
+ getSharedRenderContext().resumePlay();
+ this.gameManager_.resume();
}
- private async stepFrame(): Promise<void> {
- if (!this.bridge_?.isReady) return;
- try {
- await this.bridge_.step();
- } catch { /* ignore timeout */ }
+ private stepFrame(): void {
+ const ctx = getSharedRenderContext();
+ if (ctx.app_) {
+ ctx.app_.tick(1 / 60);
+ ctx.requestRender();
+ }
}
private async stop(): Promise<void> {
- this.disposeBridge();
- this.toolbar_.setPreviewUrl(null);
- if (this.iframe_) {
- this.iframe_.src = 'about:blank';
+ this.cleanupInput();
+ if (this.gameRenderer_) {
+ this.gameRenderer_.setVisible(false);
}
+ getPlayModeService().exitShared();
await this.gameManager_.stop();
}
- private createBridge(): void {
- if (!this.iframe_) return;
- this.disposeBridge();
-
- this.bridge_ = new GameViewBridge(this.iframe_);
-
- this.eventCleanups_.push(
- this.bridge_.on('stats', (data) => {
- this.toolbar_.updateFps(data.fps ?? 0);
- }),
- this.bridge_.on('console:log', (data) => {
- console.log('[Preview]', data.message);
- }),
- this.bridge_.on('console:warn', (data) => {
- console.warn('[Preview]', data.message);
- }),
- this.bridge_.on('console:error', (data) => {
- console.error('[Preview]', data.message);
- }),
- this.bridge_.on('error', (data) => {
- console.error('[Preview Error]', data.message);
- }),
- this.bridge_.on('ready', () => {
- getPlayModeService().enter(this.bridge_!);
- }),
- );
- }
-
- private disposeBridge(): void {
- for (const cleanup of this.eventCleanups_) cleanup();
- this.eventCleanups_ = [];
- getPlayModeService().exit();
- this.bridge_?.dispose();
- this.bridge_ = null;
- this.toolbar_.updateFps(0);
+ private onSpeedChange(speed: number): void {
+ getSharedRenderContext().setPlaySpeed(speed);
}
private onStateChange(state: GameState): void {
this.toolbar_.updateState(state);
+ const stopped = state === 'stopped';
if (this.placeholder_) {
- this.placeholder_.style.display = state === 'stopped' ? 'flex' : 'none';
+ this.placeholder_.style.display = stopped ? 'flex' : 'none';
}
- if (this.iframe_) {
- this.iframe_.style.display = state === 'stopped' ? 'none' : 'block';
+ if (this.canvas2d_) {
+ this.canvas2d_.style.display = stopped ? 'none' : 'block';
}
}
- private updateIframeSize(): void {
- if (!this.iframe_ || !this.canvasArea_) return;
+ private updateCanvasSize(): void {
+ if (!this.canvas2d_ || !this.canvasArea_) return;
const rect = this.canvasArea_.getBoundingClientRect();
- const preset = this.toolbar_.currentPreset;
+ if (rect.width <= 0 || rect.height <= 0) return;
- if (preset.width > 0 && preset.height > 0) {
- const targetAspect = preset.width / preset.height;
- const viewportAspect = rect.width / rect.height;
+ const preset = this.toolbar_.currentPreset;
+ const ctx = getSharedRenderContext();
- let displayW: number;
- let displayH: number;
+ let renderW: number;
+ let renderH: number;
- if (viewportAspect > targetAspect) {
- displayH = rect.height;
- displayW = displayH * targetAspect;
+ if (preset.width > 0 && preset.height > 0) {
+ renderW = preset.width;
+ renderH = preset.height;
+ } else {
+ const designRes = this.gameRenderer_?.getDesignResolution(ctx);
+ if (designRes) {
+ renderW = designRes.x;
+ renderH = designRes.y;
} else {
- displayW = rect.width;
- displayH = displayW / targetAspect;
+ renderW = Math.round(rect.width);
+ renderH = Math.round(rect.height);
}
+ }
+
+ const targetAspect = renderW / renderH;
+ const viewportAspect = rect.width / rect.height;
+ let displayW: number;
+ let displayH: number;
- this.iframe_.style.width = `${displayW}px`;
- this.iframe_.style.height = `${displayH}px`;
+ if (viewportAspect > targetAspect) {
+ displayH = rect.height;
+ displayW = displayH * targetAspect;
} else {
- this.iframe_.style.width = '100%';
- this.iframe_.style.height = '100%';
+ displayW = rect.width;
+ displayH = displayW / targetAspect;
+ }
+
+ const scaledW = displayW * this.scale_;
+ const scaledH = displayH * this.scale_;
+
+ this.canvas2d_.style.width = `${scaledW}px`;
+ this.canvas2d_.style.height = `${scaledH}px`;
+
+ const dpr = window.devicePixelRatio || 1;
+ const deviceW = Math.round(scaledW * dpr);
+ const deviceH = Math.round(scaledH * dpr);
+
+ if (this.gameRenderer_) {
+ this.gameRenderer_.setSize(deviceW, deviceH);
+ ctx.setGameViewportSize(deviceW, deviceH);
+ ctx.requestRender();
+ }
+ }
+
+ private setupInput(): void {
+ this.cleanupInput();
+ const canvas = this.canvas2d_;
+ if (!canvas) return;
+
+ const getInput = () => getSharedRenderContext().inputState;
+
+ const toRenderCoords = (cssX: number, cssY: number): [number, number] => {
+ const sx = canvas.width / (canvas.clientWidth || 1);
+ const sy = canvas.height / (canvas.clientHeight || 1);
+ return [cssX * sx, cssY * sy];
+ };
+
+ const onMouseMove = (e: MouseEvent) => {
+ const input = getInput();
+ if (input) {
+ const [mx, my] = toRenderCoords(e.offsetX, e.offsetY);
+ input.mouseX = mx;
+ input.mouseY = my;
+ }
+ };
+ const onMouseDown = (e: MouseEvent) => {
+ const input = getInput();
+ if (input) {
+ const [mx, my] = toRenderCoords(e.offsetX, e.offsetY);
+ input.mouseX = mx;
+ input.mouseY = my;
+ input.mouseButtons.add(e.button);
+ input.mouseButtonsPressed.add(e.button);
+ }
+ };
+ const onMouseUp = (e: MouseEvent) => {
+ const input = getInput();
+ if (input) {
+ input.mouseButtons.delete(e.button);
+ input.mouseButtonsReleased.add(e.button);
+ }
+ };
+ const onTouchStart = (e: TouchEvent) => {
+ const touch = e.touches[0];
+ if (!touch) return;
+ const rect = canvas.getBoundingClientRect();
+ const input = getInput();
+ if (input) {
+ const [mx, my] = toRenderCoords(touch.clientX - rect.left, touch.clientY - rect.top);
+ input.mouseX = mx;
+ input.mouseY = my;
+ input.mouseButtons.add(0);
+ input.mouseButtonsPressed.add(0);
+ }
+ e.preventDefault();
+ };
+ const onTouchMove = (e: TouchEvent) => {
+ const touch = e.touches[0];
+ if (!touch) return;
+ const rect = canvas.getBoundingClientRect();
+ const input = getInput();
+ if (input) {
+ const [mx, my] = toRenderCoords(touch.clientX - rect.left, touch.clientY - rect.top);
+ input.mouseX = mx;
+ input.mouseY = my;
+ }
+ e.preventDefault();
+ };
+ const onTouchEnd = (e: TouchEvent) => {
+ const input = getInput();
+ if (input) {
+ input.mouseButtons.delete(0);
+ input.mouseButtonsReleased.add(0);
+ }
+ e.preventDefault();
+ };
+
+ canvas.addEventListener('mousemove', onMouseMove);
+ canvas.addEventListener('mousedown', onMouseDown);
+ document.addEventListener('mouseup', onMouseUp);
+ canvas.addEventListener('touchstart', onTouchStart, { passive: false });
+ canvas.addEventListener('touchmove', onTouchMove, { passive: false });
+ canvas.addEventListener('touchend', onTouchEnd, { passive: false });
+
+ this.inputCleanup_ = () => {
+ canvas.removeEventListener('mousemove', onMouseMove);
+ canvas.removeEventListener('mousedown', onMouseDown);
+ document.removeEventListener('mouseup', onMouseUp);
+ canvas.removeEventListener('touchstart', onTouchStart);
+ canvas.removeEventListener('touchmove', onTouchMove);
+ canvas.removeEventListener('touchend', onTouchEnd);
+ };
+ }
+
+ private cleanupInput(): void {
+ if (this.inputCleanup_) {
+ this.inputCleanup_();
+ this.inputCleanup_ = null;
}
}
}
diff --git a/editor/src/panels/game-view/GameViewToolbar.ts b/editor/src/panels/game-view/GameViewToolbar.ts
index 6f6f88e8d..387b1f1f0 100644
--- a/editor/src/panels/game-view/GameViewToolbar.ts
+++ b/editor/src/panels/game-view/GameViewToolbar.ts
@@ -23,6 +23,7 @@ export interface GameViewToolbarCallbacks {
onStepFrame(): void;
onSpeedChange(speed: number): void;
onResolutionChange(preset: ResolutionPreset): void;
+ onScaleChange(scale: number): void;
}
export class GameViewToolbar {
@@ -36,6 +37,7 @@ export class GameViewToolbar {
private speedSelect_: HTMLSelectElement | null = null;
private fpsDisplay_: HTMLSpanElement | null = null;
private resolutionSelect_: HTMLSelectElement | null = null;
+ private scaleSelect_: HTMLSelectElement | null = null;
private customSizeEl_: HTMLElement | null = null;
private customWidthInput_: HTMLInputElement | null = null;
private customHeightInput_: HTMLInputElement | null = null;
@@ -76,6 +78,16 @@ export class GameViewToolbar {
<span class="es-gameview-custom-x">&times;</span>
<input type="number" class="es-gameview-custom-h" placeholder="H" min="1" max="4320" />
</div>
+ <div class="es-toolbar-divider"></div>
+ <span class="es-gameview-scale-label">Scale</span>
+ <select class="es-gameview-scale" title="Display Scale">
+ <option value="0.5">0.5x</option>
+ <option value="0.75">0.75x</option>
+ <option value="1" selected>1x</option>
+ <option value="1.5">1.5x</option>
+ <option value="2">2x</option>
+ <option value="3">3x</option>
+ </select>
<span class="es-gameview-fps"></span>
<span class="es-gameview-preview-url" style="display:none" title="Click to copy preview URL"></span>
</div>
@@ -94,6 +106,7 @@ export class GameViewToolbar {
this.customSizeEl_ = this.container_.querySelector('.es-gameview-custom-size');
this.customWidthInput_ = this.container_.querySelector('.es-gameview-custom-w');
this.customHeightInput_ = this.container_.querySelector('.es-gameview-custom-h');
+ this.scaleSelect_ = this.container_.querySelector('.es-gameview-scale');
this.playBtn_?.addEventListener('click', () => this.callbacks_.onPlay());
this.pauseBtn_?.addEventListener('click', () => this.callbacks_.onPause());
@@ -133,6 +146,11 @@ export class GameViewToolbar {
this.customWidthInput_?.addEventListener('change', () => this.applyCustomResolution());
this.customHeightInput_?.addEventListener('change', () => this.applyCustomResolution());
+
+ this.scaleSelect_?.addEventListener('change', () => {
+ const scale = parseFloat(this.scaleSelect_!.value);
+ this.callbacks_.onScaleChange(scale);
+ });
}
private applyCustomResolution(): void {
@@ -192,6 +210,7 @@ export class GameViewToolbar {
this.speedSelect_ = null;
this.fpsDisplay_ = null;
this.resolutionSelect_ = null;
+ this.scaleSelect_ = null;
this.customSizeEl_ = null;
this.customWidthInput_ = null;
this.customHeightInput_ = null;
diff --git a/editor/src/panels/game-view/index.ts b/editor/src/panels/game-view/index.ts
index c95cd514d..467634ea6 100644
--- a/editor/src/panels/game-view/index.ts
+++ b/editor/src/panels/game-view/index.ts
@@ -1,4 +1,4 @@
export { GameViewPanel, type GameViewPanelOptions } from './GameViewPanel';
-export { GameInstanceManager, type GameState, type GameInstanceCallbacks } from './GameInstanceManager';
+export { GameInstanceManager, type GameState } from './GameInstanceManager';
export { GameViewToolbar, type ResolutionPreset, type GameViewToolbarCallbacks } from './GameViewToolbar';
export { GameViewBridge, type RuntimeEntityData, type RuntimeStats, type EntityComponentData } from './GameViewBridge';
diff --git a/editor/src/panels/scene-view/SceneViewPanel.ts b/editor/src/panels/scene-view/SceneViewPanel.ts
index 119b5fe84..29bfbed5c 100644
--- a/editor/src/panels/scene-view/SceneViewPanel.ts
+++ b/editor/src/panels/scene-view/SceneViewPanel.ts
@@ -7,6 +7,7 @@ import { getPlatformAdapter } from '../../platform/PlatformAdapter';
import { isUUID, getAssetLibrary } from '../../asset/AssetDatabase';
import { icons } from '../../utils/icons';
import { EditorSceneRenderer } from '../../renderer/EditorSceneRenderer';
+import { getSharedRenderContext } from '../../renderer/SharedRenderContext';
import { getEntityBounds } from '../../bounds';
import { GizmoManager } from '../../gizmos';
import type { GizmoContext } from '../../gizmos';
@@ -37,8 +38,10 @@ export class SceneViewPanel {
private store_: EditorStore;
private bridge_: EditorBridge | null = null;
private canvas_: HTMLCanvasElement;
- private webglCanvas_: HTMLCanvasElement | null = null;
+ private displayCanvas_: HTMLCanvasElement | null = null;
private overlayCanvas_: HTMLCanvasElement | null = null;
+ private sceneViewportW_ = 0;
+ private sceneViewportH_ = 0;
private unsubscribe_: (() => void) | null = null;
private unsubscribeSceneSync_: (() => void) | null = null;
private unsubscribeFocus_: (() => void) | null = null;
@@ -98,12 +101,12 @@ export class SceneViewPanel {
</div>
</div>
<div class="es-sceneview-viewport">
- <canvas class="es-sceneview-webgl"></canvas>
+ <canvas class="es-sceneview-display"></canvas>
<canvas class="es-sceneview-overlay"></canvas>
</div>
`;
- this.webglCanvas_ = this.container_.querySelector('.es-sceneview-webgl')!;
+ this.displayCanvas_ = this.container_.querySelector('.es-sceneview-display')!;
this.overlayCanvas_ = this.container_.querySelector('.es-sceneview-overlay')!;
this.canvas_ = this.overlayCanvas_;
@@ -279,10 +282,15 @@ export class SceneViewPanel {
this.sceneRenderer_.dispose();
this.sceneRenderer_ = null;
}
+ getSharedRenderContext().setSceneViewportSize(0, 0);
this.input_.dispose();
this.toolbar_.dispose();
}
+ get sharedContext() {
+ return this.useWebGL_ ? getSharedRenderContext() : null;
+ }
+
setBridge(bridge: EditorBridge): void {
this.bridge_ = bridge;
this.requestRender();
@@ -336,11 +344,16 @@ export class SceneViewPanel {
const rect = viewport.getBoundingClientRect();
const dpr = window.devicePixelRatio || 1;
- if (this.webglCanvas_) {
- this.webglCanvas_.width = rect.width * dpr;
- this.webglCanvas_.height = rect.height * dpr;
- this.webglCanvas_.style.width = `${rect.width}px`;
- this.webglCanvas_.style.height = `${rect.height}px`;
+ if (this.displayCanvas_) {
+ const pixelW = Math.round(rect.width * dpr);
+ const pixelH = Math.round(rect.height * dpr);
+ this.displayCanvas_.width = pixelW;
+ this.displayCanvas_.height = pixelH;
+ this.displayCanvas_.style.width = `${rect.width}px`;
+ this.displayCanvas_.style.height = `${rect.height}px`;
+ this.sceneViewportW_ = pixelW;
+ this.sceneViewportH_ = pixelH;
+ getSharedRenderContext().setSceneViewportSize(pixelW, pixelH);
}
if (this.overlayCanvas_) {
@@ -375,16 +388,19 @@ export class SceneViewPanel {
private async initWebGLRenderer(): Promise<void> {
if (this.webglInitPending_ || this.webglInitialized_) return;
- if (!this.app_?.wasmModule || !this.webglCanvas_) return;
+ if (!this.app_?.wasmModule) return;
this.webglInitPending_ = true;
- this.sceneRenderer_ = new EditorSceneRenderer();
- this.sceneRenderer_.setStore(this.store_);
- this.sceneRenderer_.setRenderCallback(() => this.requestRender());
- const success = await this.sceneRenderer_.init(this.app_.wasmModule, this.webglCanvas_);
+ const sharedCtx = getSharedRenderContext();
+ const success = sharedCtx.initialized || await sharedCtx.init(this.app_.wasmModule);
if (success) {
+ this.sceneRenderer_ = new EditorSceneRenderer(sharedCtx);
+ this.sceneRenderer_.setStore(this.store_);
+ this.sceneRenderer_.setRenderCallback(() => this.requestRender());
+ sharedCtx.setRenderCallback(() => this.requestRender());
+
this.webglInitialized_ = true;
this.useWebGL_ = true;
@@ -413,6 +429,7 @@ export class SceneViewPanel {
try {
if (this.sceneRenderer_ && this.useWebGL_) {
await this.syncSceneToRenderer();
+ this.requestRender();
}
} catch (e) {
console.warn('[SceneViewPanel] Scene sync error:', e);
@@ -526,7 +543,7 @@ export class SceneViewPanel {
if (this.animationId_ !== null) return;
this.animationId_ = requestAnimationFrame(() => {
this.animationId_ = null;
- if (this.livePreview_) this.isDirty_ = true;
+ if (this.livePreview_ || getSharedRenderContext().isPlayMode) this.isDirty_ = true;
if (this.isDirty_) {
this.isDirty_ = false;
try {
@@ -542,9 +559,9 @@ export class SceneViewPanel {
}
private render(): void {
- if (this.useWebGL_ && this.sceneRenderer_ && this.webglCanvas_) {
- const w = this.webglCanvas_.width;
- const h = this.webglCanvas_.height;
+ if (this.useWebGL_ && this.sceneRenderer_) {
+ const w = this.sceneViewportW_;
+ const h = this.sceneViewportH_;
if (w === 0 || h === 0) return;
this.sceneRenderer_.camera.panX = this.camera_.panX;
@@ -553,6 +570,18 @@ export class SceneViewPanel {
this.sceneRenderer_.render(w, h);
this.camera_.orthoHalfHeight = this.sceneRenderer_.camera.orthoHalfHeight;
+
+ const sharedCtx = getSharedRenderContext();
+ const webglCanvas = sharedCtx.webglCanvas_;
+ if (this.displayCanvas_ && webglCanvas) {
+ const dCtx = this.displayCanvas_.getContext('2d');
+ if (dCtx) {
+ const srcY = webglCanvas.height - h;
+ dCtx.clearRect(0, 0, this.displayCanvas_.width, this.displayCanvas_.height);
+ dCtx.drawImage(webglCanvas, 0, srcY, w, h, 0, 0, w, h);
+ }
+ }
+
this.renderOverlay();
} else if (this.bridge_) {
const w = this.canvas_.width;
diff --git a/editor/src/renderer/EditorSceneRenderer.ts b/editor/src/renderer/EditorSceneRenderer.ts
index 781df644d..cd4863f7e 100644
--- a/editor/src/renderer/EditorSceneRenderer.ts
+++ b/editor/src/renderer/EditorSceneRenderer.ts
@@ -1,32 +1,10 @@
-/**
- * @file EditorSceneRenderer.ts
- * @brief WebGL scene renderer for editor scene view
- */
-
-import type { ESEngineModule, CppRegistry, LayoutRect } from 'esengine';
+import type { ESEngineModule, LayoutRect } from 'esengine';
import {
App,
UICameraInfo,
- UIEvents,
- UIEventQueue,
- Input,
- InputState,
- setEditorMode,
- uiPlugins,
ButtonState,
RenderPipeline,
Renderer,
- createMaskProcessor,
- initDrawAPI,
- shutdownDrawAPI,
- initGeometryAPI,
- shutdownGeometryAPI,
- initMaterialAPI,
- shutdownMaterialAPI,
- initPostProcessAPI,
- shutdownPostProcessAPI,
- initRendererAPI,
- shutdownRendererAPI,
} from 'esengine';
import type { SpineModuleController } from 'esengine/spine';
import type { SceneData, ComponentData } from '../types/SceneTypes';
@@ -37,112 +15,50 @@ import { AssetPathResolver } from '../asset';
import { getAssetEventBus } from '../events/AssetEventBus';
import { getDependencyGraph } from '../asset/AssetDependencyGraph';
import type { TransformValue } from '../math/Transform';
-
-// =============================================================================
-// EditorSceneRenderer
-// =============================================================================
+import type { SharedRenderContext } from './SharedRenderContext';
export class EditorSceneRenderer {
- private module_: ESEngineModule | null = null;
- private app_: App | null = null;
- private sceneManager_: EditorSceneManager | null = null;
- private pipeline_: RenderPipeline | null = null;
+ private context_: SharedRenderContext;
private camera_: EditorCamera;
- private pathResolver_: AssetPathResolver;
private store_: EditorStore | null = null;
- private initialized_ = false;
- private startTime_ = 0;
- private lastElapsed_ = 0;
private unsubscribes_: (() => void)[] = [];
private dirtyEntities_ = new Set<number>();
private rafId_: number | null = null;
private renderCallback_: (() => void) | null = null;
+ private lastElapsed_ = 0;
- constructor() {
+ constructor(context: SharedRenderContext) {
+ this.context_ = context;
this.camera_ = new EditorCamera();
- this.pathResolver_ = new AssetPathResolver();
}
- setStore(store: EditorStore): void {
- this.store_ = store;
- this.setupSyncService();
+ get camera(): EditorCamera {
+ return this.camera_;
}
- setRenderCallback(callback: (() => void) | null): void {
- this.renderCallback_ = callback;
+ private get sceneManager_(): EditorSceneManager | null {
+ return this.context_.sceneManager_;
}
- async init(module: ESEngineModule, canvas: HTMLCanvasElement): Promise<boolean> {
- if (this.initialized_) return true;
-
- this.module_ = module;
-
- const gl = canvas.getContext('webgl2', {
- alpha: true,
- depth: true,
- stencil: true,
- antialias: true,
- premultipliedAlpha: true,
- preserveDrawingBuffer: false,
- });
- if (!gl) {
- console.error('[EditorSceneRenderer] Failed to create WebGL2 context');
- return false;
- }
-
- const handle = module.GL.registerContext(gl, { majorVersion: 2, minorVersion: 0 });
- if (handle <= 0) {
- console.error('[EditorSceneRenderer] Failed to register WebGL context');
- return false;
- }
-
- const success = module.initRendererWithContext(handle);
- if (!success) {
- console.error('[EditorSceneRenderer] Failed to initialize renderer');
- return false;
- }
-
- initDrawAPI(module);
- initGeometryAPI(module);
- initMaterialAPI(module);
- initPostProcessAPI(module);
- initRendererAPI(module);
-
- this.pipeline_ = new RenderPipeline();
- this.startTime_ = performance.now();
-
- const app = App.new();
- const cppRegistry = new module.Registry() as unknown as CppRegistry;
- app.connectCpp(cppRegistry, module);
-
- app.insertResource(UICameraInfo, {
- viewProjection: new Float32Array(16),
- vpX: 0, vpY: 0, vpW: 0, vpH: 0,
- screenW: 0, screenH: 0,
- worldLeft: 0, worldBottom: 0, worldRight: 0, worldTop: 0,
- worldMouseX: 0, worldMouseY: 0,
- valid: false,
- });
-
- app.insertResource(Input, new InputState());
- app.insertResource(UIEvents, new UIEventQueue());
+ private get pipeline_(): RenderPipeline | null {
+ return this.context_.pipeline_;
+ }
- setEditorMode(true);
- for (const plugin of uiPlugins) {
- app.addPlugin(plugin);
- }
- this.app_ = app;
+ private get app_(): App | null {
+ return this.context_.app_;
+ }
- this.sceneManager_ = new EditorSceneManager(module, this.pathResolver_, app.world);
- this.sceneManager_.registerSystems(app);
- this.pipeline_.setMaskProcessor(
- createMaskProcessor(module, this.sceneManager_.world)
- );
- this.initialized_ = true;
+ private get module_(): ESEngineModule | null {
+ return this.context_.module_;
+ }
+ setStore(store: EditorStore): void {
+ this.store_ = store;
this.setupSyncService();
+ }
- return true;
+ setRenderCallback(callback: (() => void) | null): void {
+ this.renderCallback_ = callback;
}
private setupSyncService(): void {
@@ -215,10 +131,6 @@ export class EditorSceneRenderer {
this.scheduleEntityUpdate(event.entity);
}
- // =========================================================================
- // Sync Helpers
- // =========================================================================
-
private syncCanvasToCamera(canvasEntityId: number): void {
const canvasData = this.store_!.getEntityData(canvasEntityId);
if (!canvasData) return;
@@ -270,10 +182,6 @@ export class EditorSceneRenderer {
this.scheduleEntityUpdate(entityId);
}
- // =========================================================================
- // Dirty Entity Scheduling
- // =========================================================================
-
private scheduleDescendantUpdates(entityId: number): void {
const entityData = this.store_!.getEntityData(entityId);
if (!entityData) return;
@@ -315,33 +223,17 @@ export class EditorSceneRenderer {
}
setProjectDir(projectDir: string): void {
- this.pathResolver_.setProjectDir(projectDir);
- this.sceneManager_?.setProjectDir(projectDir);
+ this.context_.setProjectDir(projectDir);
}
setSpineController(controller: SpineModuleController | null): void {
- this.sceneManager_?.setSpineController(controller);
-
- if (controller && this.pipeline_ && this.module_) {
- const module = this.module_;
- this.pipeline_.setSpineRenderer((_registry, elapsed) => {
- const dt = elapsed - this.lastElapsed_;
- this.lastElapsed_ = elapsed;
- this.sceneManager_?.updateAndSubmitSpine(module, dt);
- });
- } else {
- this.pipeline_?.setSpineRenderer(null);
- }
+ this.context_.setSpineController(controller);
}
get pathResolver(): AssetPathResolver {
- return this.pathResolver_;
+ return this.context_.pathResolver_;
}
- // =========================================================================
- // Scene Sync (Delegated to EditorSceneManager)
- // =========================================================================
-
async syncScene(scene: SceneData): Promise<void> {
await this.sceneManager_?.loadScene(scene);
}
@@ -354,14 +246,6 @@ export class EditorSceneRenderer {
this.sceneManager_?.removeEntity(entityId);
}
- // =========================================================================
- // Editor-specific Features
- // =========================================================================
-
- get camera(): EditorCamera {
- return this.camera_;
- }
-
get textureManager() {
return this.sceneManager_?.assetServer.textureManager ?? null;
}
@@ -434,12 +318,8 @@ export class EditorSceneRenderer {
return this.getComputedLayoutSize(entityId);
}
- // =========================================================================
- // Rendering
- // =========================================================================
-
render(width: number, height: number): void {
- if (!this.pipeline_ || !this.sceneManager_ || !this.initialized_) return;
+ if (!this.pipeline_ || !this.sceneManager_ || !this.context_.initialized) return;
if (width <= 0 || height <= 0) return;
if (this.sceneManager_.isBusy) return;
@@ -456,26 +336,40 @@ export class EditorSceneRenderer {
}
const matrix = this.camera_.getViewProjection(width, height);
- const elapsed = (performance.now() - this.startTime_) / 1000;
+ const elapsed = this.context_.elapsed;
const cppReg = this.sceneManager_.registry;
const registry = { _cpp: cppReg };
try {
if (this.app_) {
- const uiCamera = this.app_.getResource(UICameraInfo);
- if (canvasRect) {
- uiCamera.worldLeft = canvasRect.left;
- uiCamera.worldBottom = canvasRect.bottom;
- uiCamera.worldRight = canvasRect.right;
- uiCamera.worldTop = canvasRect.top;
- uiCamera.valid = true;
+ if (this.context_.isPlayMode) {
+ const gameRenderer = this.context_.gameViewRenderer;
+ if (gameRenderer && gameRenderer.visible) {
+ gameRenderer.updateUICameraInfo(this.context_);
+ }
} else {
- uiCamera.valid = false;
+ const uiCamera = this.app_.getResource(UICameraInfo);
+ if (canvasRect) {
+ uiCamera.worldLeft = canvasRect.left;
+ uiCamera.worldBottom = canvasRect.bottom;
+ uiCamera.worldRight = canvasRect.right;
+ uiCamera.worldTop = canvasRect.top;
+ uiCamera.valid = true;
+ } else {
+ uiCamera.valid = false;
+ }
}
- this.app_.tick(1 / 60);
+
+ this.context_.tickApp();
+ }
+
+ const gameRenderer = this.context_.gameViewRenderer;
+ if (gameRenderer && gameRenderer.visible) {
+ gameRenderer.renderAndCapture(this.context_);
}
+ Renderer.setClearColor(bg.r, bg.g, bg.b, bg.a);
Renderer.setViewport(0, 0, width, height);
Renderer.clearBuffers(3);
Renderer.begin(matrix);
@@ -500,6 +394,40 @@ export class EditorSceneRenderer {
}
}
+ renderGameCamera(width: number, height: number, vpMatrix: Float32Array): void {
+ if (!this.pipeline_ || !this.sceneManager_ || !this.context_.initialized) return;
+ if (width <= 0 || height <= 0) return;
+ if (this.sceneManager_.isBusy) return;
+
+ const elapsed = this.context_.elapsed;
+ const cppReg = this.sceneManager_.registry;
+ const registry = { _cpp: cppReg };
+
+ try {
+ Renderer.setViewport(0, 0, width, height);
+ Renderer.clearBuffers(3);
+ Renderer.begin(vpMatrix);
+ if (this.pipeline_.maskProcessor) {
+ this.pipeline_.maskProcessor(registry._cpp, vpMatrix, 0, 0, width, height);
+ }
+ Renderer.submitSprites(registry);
+ Renderer.submitBitmapText(registry);
+ if (this.pipeline_.spineRenderer) {
+ this.pipeline_.spineRenderer(registry, elapsed);
+ } else {
+ Renderer.submitSpine(registry);
+ }
+ Renderer.flush();
+ Renderer.end();
+ } catch (e) {
+ if (e instanceof WebAssembly.RuntimeError) {
+ console.warn('[EditorSceneRenderer] WASM error during game camera render:', (e as Error).message);
+ } else {
+ throw e;
+ }
+ }
+ }
+
private findCanvasBackgroundColor(): { r: number; g: number; b: number; a: number } {
if (this.store_) {
for (const entity of this.store_.scene.entities) {
@@ -513,10 +441,6 @@ export class EditorSceneRenderer {
return { r: 0.1, g: 0.1, b: 0.1, a: 1.0 };
}
- // =========================================================================
- // Cleanup
- // =========================================================================
-
dispose(): void {
if (this.rafId_ !== null) {
cancelAnimationFrame(this.rafId_);
@@ -525,31 +449,5 @@ export class EditorSceneRenderer {
this.dirtyEntities_.clear();
for (const unsub of this.unsubscribes_) unsub();
this.unsubscribes_ = [];
-
- if (this.sceneManager_) {
- this.sceneManager_.dispose();
- this.sceneManager_ = null;
- }
-
- if (this.app_) {
- const world = this.app_.world;
- const reg = world.getCppRegistry();
- world.disconnectCpp();
- if (reg) (reg as any).delete();
- this.app_ = null;
- }
-
- if (this.module_ && this.initialized_) {
- shutdownDrawAPI();
- shutdownGeometryAPI();
- shutdownMaterialAPI();
- shutdownPostProcessAPI();
- shutdownRendererAPI();
- this.module_.shutdownRenderer();
- }
-
- this.pipeline_ = null;
- this.initialized_ = false;
- this.module_ = null;
}
}
diff --git a/editor/src/renderer/GameViewRenderer.ts b/editor/src/renderer/GameViewRenderer.ts
new file mode 100644
index 000000000..612cb70c2
--- /dev/null
+++ b/editor/src/renderer/GameViewRenderer.ts
@@ -0,0 +1,300 @@
+import type { ESEngineModule, CppRegistry, UICameraData } from 'esengine';
+import { Renderer, UICameraInfo } from 'esengine';
+import type { SharedRenderContext } from './SharedRenderContext';
+
+const PROJECTION_ORTHOGRAPHIC = 1;
+
+const _orthoM = new Float32Array(16);
+function ortho(left: number, right: number, bottom: number, top: number, near: number, far: number): Float32Array {
+ const m = _orthoM;
+ m.fill(0);
+ const rl = right - left;
+ const tb = top - bottom;
+ const fn = far - near;
+ m[0] = 2 / rl;
+ m[5] = 2 / tb;
+ m[10] = -2 / fn;
+ m[12] = -(right + left) / rl;
+ m[13] = -(top + bottom) / tb;
+ m[14] = -(far + near) / fn;
+ m[15] = 1;
+ return m;
+}
+
+const _perspM = new Float32Array(16);
+function perspective(fovRad: number, aspect: number, near: number, far: number): Float32Array {
+ const m = _perspM;
+ m.fill(0);
+ const f = 1.0 / Math.tan(fovRad / 2);
+ const nf = near - far;
+ m[0] = f / aspect;
+ m[5] = f;
+ m[10] = (far + near) / nf;
+ m[11] = -1;
+ m[14] = (2 * far * near) / nf;
+ return m;
+}
+
+const _invTransM = new Float32Array(16);
+function invertTranslation(x: number, y: number, z: number): Float32Array {
+ const m = _invTransM;
+ m[0] = 1; m[1] = 0; m[2] = 0; m[3] = 0;
+ m[4] = 0; m[5] = 1; m[6] = 0; m[7] = 0;
+ m[8] = 0; m[9] = 0; m[10] = 1; m[11] = 0;
+ m[12] = -x; m[13] = -y; m[14] = -z; m[15] = 1;
+ return m;
+}
+
+const _mulM = new Float32Array(16);
+function multiply(a: Float32Array, b: Float32Array): Float32Array {
+ const m = _mulM;
+ for (let i = 0; i < 4; i++) {
+ for (let j = 0; j < 4; j++) {
+ m[j * 4 + i] =
+ a[0 * 4 + i] * b[j * 4 + 0] +
+ a[1 * 4 + i] * b[j * 4 + 1] +
+ a[2 * 4 + i] * b[j * 4 + 2] +
+ a[3 * 4 + i] * b[j * 4 + 3];
+ }
+ }
+ return m;
+}
+
+const SCALE_FIXED_WIDTH = 0;
+const SCALE_EXPAND = 2;
+const SCALE_SHRINK = 3;
+const SCALE_MATCH = 4;
+
+function computeEffectiveOrthoSize(
+ baseOrthoSize: number,
+ designAspect: number,
+ actualAspect: number,
+ scaleMode: number,
+ matchWidthOrHeight: number,
+): number {
+ const orthoForWidth = baseOrthoSize * designAspect / actualAspect;
+ const orthoForHeight = baseOrthoSize;
+
+ switch (scaleMode) {
+ case SCALE_FIXED_WIDTH: return orthoForWidth;
+ case SCALE_EXPAND: return Math.max(orthoForWidth, orthoForHeight);
+ case SCALE_SHRINK: return Math.min(orthoForWidth, orthoForHeight);
+ case SCALE_MATCH: {
+ const t = matchWidthOrHeight;
+ return Math.pow(orthoForWidth, 1 - t) * Math.pow(orthoForHeight, t);
+ }
+ default: return orthoForHeight;
+ }
+}
+
+export class GameViewRenderer {
+ private canvas2d_: HTMLCanvasElement;
+ private ctx2d_: CanvasRenderingContext2D;
+ private webglCanvas_: HTMLCanvasElement;
+ private visible_ = false;
+ private width_ = 0;
+ private height_ = 0;
+ private vpMatrix_ = new Float32Array(16);
+ private worldLeft_ = 0;
+ private worldRight_ = 0;
+ private worldBottom_ = 0;
+ private worldTop_ = 0;
+
+ constructor(canvas2d: HTMLCanvasElement, webglCanvas: HTMLCanvasElement) {
+ this.canvas2d_ = canvas2d;
+ this.webglCanvas_ = webglCanvas;
+ const ctx = canvas2d.getContext('2d');
+ if (!ctx) throw new Error('Failed to create 2D context for GameViewRenderer');
+ this.ctx2d_ = ctx;
+ }
+
+ get visible(): boolean {
+ return this.visible_;
+ }
+
+ get width(): number {
+ return this.width_;
+ }
+
+ get height(): number {
+ return this.height_;
+ }
+
+ setSize(w: number, h: number): void {
+ this.width_ = w;
+ this.height_ = h;
+ this.canvas2d_.width = w;
+ this.canvas2d_.height = h;
+ }
+
+ setVisible(visible: boolean): void {
+ this.visible_ = visible;
+ }
+
+ computeGameCameraVP(context: SharedRenderContext): boolean {
+ const module = context.module_;
+ const sceneManager = context.sceneManager_;
+ if (!module || !sceneManager) return false;
+ if (this.width_ <= 0 || this.height_ <= 0) return false;
+
+ const registry = sceneManager.registry;
+ const cameraEntities = module.registry_getCameraEntities(registry);
+ if (cameraEntities.length === 0) return false;
+
+ const e = cameraEntities[0];
+ const camera = registry.getCamera(e);
+ const transform = registry.getTransform(e);
+
+ const width = this.width_;
+ const height = this.height_;
+ const aspect = width / height;
+
+ let projection: Float32Array;
+
+ const camX = transform.position.x;
+ const camY = transform.position.y;
+
+ if (camera.projectionType === PROJECTION_ORTHOGRAPHIC) {
+ let halfH = camera.orthoSize;
+
+ const canvasEntity = module.registry_getCanvasEntity(registry);
+ if (canvasEntity >= 0) {
+ const canvas = registry.getCanvas(canvasEntity);
+ const baseOrthoSize = canvas.designResolution.y / 2;
+ const designAspect = canvas.designResolution.x / canvas.designResolution.y;
+ halfH = computeEffectiveOrthoSize(
+ baseOrthoSize, designAspect, aspect,
+ canvas.scaleMode, canvas.matchWidthOrHeight,
+ );
+ }
+
+ const halfW = halfH * aspect;
+ projection = ortho(-halfW, halfW, -halfH, halfH, -camera.farPlane, camera.farPlane);
+
+ this.worldLeft_ = camX - halfW;
+ this.worldRight_ = camX + halfW;
+ this.worldBottom_ = camY - halfH;
+ this.worldTop_ = camY + halfH;
+ } else {
+ projection = perspective(
+ camera.fov * Math.PI / 180,
+ aspect,
+ camera.nearPlane,
+ camera.farPlane,
+ );
+ this.worldLeft_ = 0;
+ this.worldRight_ = 0;
+ this.worldBottom_ = 0;
+ this.worldTop_ = 0;
+ }
+
+ const view = invertTranslation(camX, camY, transform.position.z);
+ this.vpMatrix_.set(multiply(projection, view));
+ return true;
+ }
+
+ updateUICameraInfo(context: SharedRenderContext): void {
+ if (!this.visible_ || this.width_ <= 0 || this.height_ <= 0) return;
+ if (!this.computeGameCameraVP(context)) return;
+
+ const cam = context.getUICameraInfo();
+ if (!cam) return;
+
+ cam.viewProjection.set(this.vpMatrix_);
+ cam.vpX = 0;
+ cam.vpY = 0;
+ cam.vpW = this.width_;
+ cam.vpH = this.height_;
+ cam.screenW = this.width_;
+ cam.screenH = this.height_;
+ cam.worldLeft = this.worldLeft_;
+ cam.worldRight = this.worldRight_;
+ cam.worldBottom = this.worldBottom_;
+ cam.worldTop = this.worldTop_;
+ cam.valid = true;
+ }
+
+ renderAndCapture(context: SharedRenderContext): void {
+ if (!this.visible_ || this.width_ <= 0 || this.height_ <= 0) return;
+ if (!this.computeGameCameraVP(context)) return;
+
+ const sceneManager = context.sceneManager_;
+ if (!sceneManager || sceneManager.isBusy) return;
+
+ const renderW = this.width_;
+ const renderH = this.height_;
+
+ const bg = this.findCanvasBackgroundColor(context);
+ Renderer.setClearColor(bg.r, bg.g, bg.b, bg.a);
+
+ const pipeline = context.pipeline_;
+ const elapsed = context.elapsed;
+ const cppReg = sceneManager.registry;
+ const registry = { _cpp: cppReg };
+
+ try {
+ Renderer.setViewport(0, 0, renderW, renderH);
+ Renderer.clearBuffers(3);
+ Renderer.begin(this.vpMatrix_);
+ if (pipeline?.maskProcessor) {
+ pipeline.maskProcessor(registry._cpp, this.vpMatrix_, 0, 0, renderW, renderH);
+ }
+ Renderer.submitSprites(registry);
+ Renderer.submitBitmapText(registry);
+ if (pipeline?.spineRenderer) {
+ pipeline.spineRenderer(registry, elapsed);
+ } else {
+ Renderer.submitSpine(registry);
+ }
+ Renderer.flush();
+ Renderer.end();
+
+ const srcY = this.webglCanvas_.height - renderH;
+ this.ctx2d_.clearRect(0, 0, this.width_, this.height_);
+ this.ctx2d_.drawImage(
+ this.webglCanvas_,
+ 0, srcY, renderW, renderH,
+ 0, 0, this.width_, this.height_,
+ );
+ } catch (e) {
+ if (e instanceof WebAssembly.RuntimeError) {
+ console.warn('[GameViewRenderer] WASM error during render:', (e as Error).message);
+ } else {
+ throw e;
+ }
+ }
+ }
+
+ private findCanvasBackgroundColor(context: SharedRenderContext): { r: number; g: number; b: number; a: number } {
+ const module = context.module_;
+ const sceneManager = context.sceneManager_;
+ if (module && sceneManager) {
+ const registry = sceneManager.registry;
+ const canvasEntity = module.registry_getCanvasEntity(registry);
+ if (canvasEntity >= 0) {
+ const canvas = registry.getCanvas(canvasEntity);
+ const bg = canvas.backgroundColor;
+ return { r: bg.x, g: bg.y, b: bg.z, a: bg.w };
+ }
+ }
+ return { r: 0.1, g: 0.1, b: 0.1, a: 1.0 };
+ }
+
+ getDesignResolution(context: SharedRenderContext): { x: number; y: number } | null {
+ const module = context.module_;
+ const sceneManager = context.sceneManager_;
+ if (!module || !sceneManager) return null;
+ const registry = sceneManager.registry;
+ const canvasEntity = module.registry_getCanvasEntity(registry);
+ if (canvasEntity < 0) return null;
+ const canvas = registry.getCanvas(canvasEntity);
+ const x = canvas.designResolution.x;
+ const y = canvas.designResolution.y;
+ if (x <= 0 || y <= 0) return null;
+ return { x, y };
+ }
+
+ dispose(): void {
+ this.visible_ = false;
+ }
+}
diff --git a/editor/src/renderer/SharedRenderContext.ts b/editor/src/renderer/SharedRenderContext.ts
new file mode 100644
index 000000000..a17d78958
--- /dev/null
+++ b/editor/src/renderer/SharedRenderContext.ts
@@ -0,0 +1,373 @@
+import type { ESEngineModule, CppRegistry, UICameraData } from 'esengine';
+import {
+ App,
+ UICameraInfo,
+ UIEvents,
+ UIEventQueue,
+ Input,
+ InputState,
+ setEditorMode,
+ setPlayMode,
+ defineSystem,
+ Schedule,
+ uiPlugins,
+ RenderPipeline,
+ Renderer,
+ createMaskProcessor,
+ initDrawAPI,
+ shutdownDrawAPI,
+ initGeometryAPI,
+ shutdownGeometryAPI,
+ initMaterialAPI,
+ shutdownMaterialAPI,
+ initPostProcessAPI,
+ shutdownPostProcessAPI,
+ initRendererAPI,
+ shutdownRendererAPI,
+} from 'esengine';
+import type { SpineModuleController } from 'esengine/spine';
+import { EditorSceneManager } from '../scene/EditorSceneManager';
+import { AssetPathResolver } from '../asset';
+import type { GameViewRenderer } from './GameViewRenderer';
+
+export class SharedRenderContext {
+ module_: ESEngineModule | null = null;
+ gl_: WebGL2RenderingContext | null = null;
+ app_: App | null = null;
+ sceneManager_: EditorSceneManager | null = null;
+ pipeline_: RenderPipeline | null = null;
+ webglCanvas_: HTMLCanvasElement | null = null;
+ pathResolver_: AssetPathResolver;
+
+ private initialized_ = false;
+ private gameViewRenderer_: GameViewRenderer | null = null;
+ private inputState_: InputState | null = null;
+ private startTime_ = 0;
+ private lastElapsed_ = 0;
+
+ private playMode_ = false;
+ private paused_ = false;
+ private playSpeed_ = 1;
+ private lastFrameTime_ = 0;
+
+ private animationId_: number | null = null;
+ private continuousRender_ = false;
+ private isDirty_ = true;
+ private sceneViewportW_ = 0;
+ private sceneViewportH_ = 0;
+ private gameViewportW_ = 0;
+ private gameViewportH_ = 0;
+ private renderCallback_: (() => void) | null = null;
+ private onInitCallbacks_: (() => void)[] = [];
+
+ constructor() {
+ this.pathResolver_ = new AssetPathResolver();
+ }
+
+ get initialized(): boolean {
+ return this.initialized_;
+ }
+
+ get isPlayMode(): boolean {
+ return this.playMode_;
+ }
+
+ async init(module: ESEngineModule): Promise<boolean> {
+ if (this.initialized_) return true;
+
+ this.module_ = module;
+
+ const canvas = document.createElement('canvas');
+ canvas.style.position = 'fixed';
+ canvas.style.left = '-9999px';
+ canvas.style.top = '-9999px';
+ canvas.style.pointerEvents = 'none';
+ document.body.appendChild(canvas);
+ this.webglCanvas_ = canvas;
+
+ const gl = canvas.getContext('webgl2', {
+ alpha: true,
+ depth: true,
+ stencil: true,
+ antialias: true,
+ premultipliedAlpha: true,
+ preserveDrawingBuffer: false,
+ });
+ if (!gl) {
+ console.error('[SharedRenderContext] Failed to create WebGL2 context');
+ return false;
+ }
+ this.gl_ = gl;
+
+ const handle = module.GL.registerContext(gl, { majorVersion: 2, minorVersion: 0 });
+ if (handle <= 0) {
+ console.error('[SharedRenderContext] Failed to register WebGL context');
+ return false;
+ }
+
+ const success = module.initRendererWithContext(handle);
+ if (!success) {
+ console.error('[SharedRenderContext] Failed to initialize renderer');
+ return false;
+ }
+
+ initDrawAPI(module);
+ initGeometryAPI(module);
+ initMaterialAPI(module);
+ initPostProcessAPI(module);
+ initRendererAPI(module);
+
+ this.pipeline_ = new RenderPipeline();
+ this.startTime_ = performance.now();
+
+ const app = App.new();
+ const cppRegistry = new module.Registry() as unknown as CppRegistry;
+ app.connectCpp(cppRegistry, module);
+
+ app.insertResource(UICameraInfo, {
+ viewProjection: new Float32Array(16),
+ vpX: 0, vpY: 0, vpW: 0, vpH: 0,
+ screenW: 0, screenH: 0,
+ worldLeft: 0, worldBottom: 0, worldRight: 0, worldTop: 0,
+ worldMouseX: 0, worldMouseY: 0,
+ valid: false,
+ });
+
+ this.inputState_ = new InputState();
+ app.insertResource(Input, this.inputState_);
+ app.insertResource(UIEvents, new UIEventQueue());
+
+ app.addSystemToSchedule(Schedule.Last, defineSystem([], () => {
+ if (!this.inputState_) return;
+ this.inputState_.keysPressed.clear();
+ this.inputState_.keysReleased.clear();
+ this.inputState_.mouseButtonsPressed.clear();
+ this.inputState_.mouseButtonsReleased.clear();
+ this.inputState_.scrollDeltaX = 0;
+ this.inputState_.scrollDeltaY = 0;
+ }, { name: 'EditorInputClearSystem' }));
+
+ setEditorMode(true);
+ for (const plugin of uiPlugins) {
+ app.addPlugin(plugin);
+ }
+ this.app_ = app;
+
+ this.sceneManager_ = new EditorSceneManager(module, this.pathResolver_, app.world);
+ this.sceneManager_.registerSystems(app);
+ this.pipeline_.setMaskProcessor(
+ createMaskProcessor(module, this.sceneManager_.world)
+ );
+ this.initialized_ = true;
+
+ for (const cb of this.onInitCallbacks_) cb();
+ this.onInitCallbacks_ = [];
+
+ return true;
+ }
+
+ onceInitialized(cb: () => void): void {
+ if (this.initialized_) {
+ cb();
+ } else {
+ this.onInitCallbacks_.push(cb);
+ }
+ }
+
+ setRenderCallback(callback: (() => void) | null): void {
+ this.renderCallback_ = callback;
+ }
+
+ setGameViewRenderer(renderer: GameViewRenderer | null): void {
+ this.gameViewRenderer_ = renderer;
+ if (renderer) {
+ this.requestRender();
+ }
+ }
+
+ get gameViewRenderer(): GameViewRenderer | null {
+ return this.gameViewRenderer_;
+ }
+
+ get inputState(): InputState | null {
+ return this.inputState_;
+ }
+
+ getUICameraInfo(): UICameraData | null {
+ return this.app_?.getResource(UICameraInfo) as UICameraData | null ?? null;
+ }
+
+ private clearInputState(): void {
+ if (!this.inputState_) return;
+ this.inputState_.keysDown.clear();
+ this.inputState_.keysPressed.clear();
+ this.inputState_.keysReleased.clear();
+ this.inputState_.mouseButtons.clear();
+ this.inputState_.mouseButtonsPressed.clear();
+ this.inputState_.mouseButtonsReleased.clear();
+ this.inputState_.scrollDeltaX = 0;
+ this.inputState_.scrollDeltaY = 0;
+ }
+
+ setProjectDir(projectDir: string): void {
+ this.pathResolver_.setProjectDir(projectDir);
+ this.sceneManager_?.setProjectDir(projectDir);
+ }
+
+ setSpineController(controller: SpineModuleController | null): void {
+ this.sceneManager_?.setSpineController(controller);
+
+ if (controller && this.pipeline_ && this.module_) {
+ const module = this.module_;
+ this.pipeline_.setSpineRenderer((_registry, elapsed) => {
+ const dt = elapsed - this.lastElapsed_;
+ this.lastElapsed_ = elapsed;
+ this.sceneManager_?.updateAndSubmitSpine(module, dt);
+ });
+ } else {
+ this.pipeline_?.setSpineRenderer(null);
+ }
+ }
+
+ get elapsed(): number {
+ return (performance.now() - this.startTime_) / 1000;
+ }
+
+ requestRender(): void {
+ this.isDirty_ = true;
+ this.renderCallback_?.();
+ }
+
+ startContinuousRender(): void {
+ this.continuousRender_ = true;
+ }
+
+ stopContinuousRender(): void {
+ this.continuousRender_ = false;
+ }
+
+ get isContinuousRender(): boolean {
+ return this.continuousRender_;
+ }
+
+ setSceneViewportSize(w: number, h: number): void {
+ this.sceneViewportW_ = w;
+ this.sceneViewportH_ = h;
+ this.syncCanvasSize_();
+ }
+
+ setGameViewportSize(w: number, h: number): void {
+ this.gameViewportW_ = w;
+ this.gameViewportH_ = h;
+ this.syncCanvasSize_();
+ }
+
+ private syncCanvasSize_(): void {
+ if (!this.webglCanvas_) return;
+ const w = Math.max(this.sceneViewportW_, this.gameViewportW_);
+ const h = Math.max(this.sceneViewportH_, this.gameViewportH_);
+ if (w <= 0 || h <= 0) return;
+ if (this.webglCanvas_.width !== w || this.webglCanvas_.height !== h) {
+ this.webglCanvas_.width = w;
+ this.webglCanvas_.height = h;
+ }
+ }
+
+ tickApp(): void {
+ if (!this.app_) return;
+ if (this.playMode_ && !this.paused_) {
+ const now = performance.now();
+ const dt = (now - this.lastFrameTime_) / 1000;
+ this.lastFrameTime_ = now;
+ const clampedDt = Math.min(dt, 0.1);
+ this.app_.tick(clampedDt * this.playSpeed_);
+ } else {
+ this.app_.tick(1 / 60);
+ }
+ }
+
+ enterPlayMode(): void {
+ this.playMode_ = true;
+ this.paused_ = false;
+ this.lastFrameTime_ = performance.now();
+ setPlayMode(true);
+ this.startContinuousRender();
+ this.requestRender();
+ }
+
+ exitPlayMode(): void {
+ this.playMode_ = false;
+ this.paused_ = false;
+ setPlayMode(false);
+ this.clearInputState();
+ const cam = this.getUICameraInfo();
+ if (cam) cam.valid = false;
+ this.stopContinuousRender();
+ }
+
+ pausePlay(): void {
+ this.paused_ = true;
+ }
+
+ resumePlay(): void {
+ this.paused_ = false;
+ this.lastFrameTime_ = performance.now();
+ }
+
+ setPlaySpeed(speed: number): void {
+ this.playSpeed_ = speed;
+ }
+
+ dispose(): void {
+ if (this.animationId_ !== null) {
+ cancelAnimationFrame(this.animationId_);
+ this.animationId_ = null;
+ }
+
+ if (this.sceneManager_) {
+ this.sceneManager_.dispose();
+ this.sceneManager_ = null;
+ }
+
+ if (this.app_) {
+ const world = this.app_.world;
+ const reg = world.getCppRegistry();
+ world.disconnectCpp();
+ if (reg) (reg as any).delete();
+ this.app_ = null;
+ }
+
+ if (this.module_ && this.initialized_) {
+ shutdownDrawAPI();
+ shutdownGeometryAPI();
+ shutdownMaterialAPI();
+ shutdownPostProcessAPI();
+ shutdownRendererAPI();
+ this.module_.shutdownRenderer();
+ }
+
+ this.pipeline_ = null;
+ this.gl_ = null;
+ if (this.webglCanvas_) {
+ this.webglCanvas_.remove();
+ this.webglCanvas_ = null;
+ }
+ this.initialized_ = false;
+ this.module_ = null;
+ this.gameViewRenderer_ = null;
+ }
+}
+
+let sharedInstance: SharedRenderContext | null = null;
+
+export function getSharedRenderContext(): SharedRenderContext {
+ if (!sharedInstance) sharedInstance = new SharedRenderContext();
+ return sharedInstance;
+}
+
+export function resetSharedRenderContext(): void {
+ if (sharedInstance) {
+ sharedInstance.dispose();
+ sharedInstance = null;
+ }
+}
diff --git a/editor/src/renderer/index.ts b/editor/src/renderer/index.ts
index 2387e06cc..341c5b201 100644
--- a/editor/src/renderer/index.ts
+++ b/editor/src/renderer/index.ts
@@ -6,3 +6,5 @@
export { EditorSceneRenderer } from './EditorSceneRenderer';
export { EditorTextureManager } from './EditorTextureManager';
export { EditorCamera } from './EditorCamera';
+export { SharedRenderContext, getSharedRenderContext, resetSharedRenderContext } from './SharedRenderContext';
+export { GameViewRenderer } from './GameViewRenderer';
diff --git a/editor/src/services/PlayModeService.ts b/editor/src/services/PlayModeService.ts
index 17b82be45..69f55e9d7 100644
--- a/editor/src/services/PlayModeService.ts
+++ b/editor/src/services/PlayModeService.ts
@@ -1,6 +1,7 @@
import type { GameViewBridge, RuntimeEntityData } from '../panels/game-view/GameViewBridge';
import type { EntityData } from '../types/SceneTypes';
import { getEditorStore, type SceneSnapshot } from '../store/EditorStore';
+import { getSharedRenderContext } from '../renderer/SharedRenderContext';
export type PlayState = 'stopped' | 'playing';
@@ -22,6 +23,7 @@ export function runtimeToEntityData(re: RuntimeEntityData): EntityData {
class PlayModeService {
private state_: PlayState = 'stopped';
private bridge_: GameViewBridge | null = null;
+ private sharedMode_ = false;
private selectedEntityId_: number | null = null;
private cachedEntities_: RuntimeEntityData[] = [];
private pollTimer_: ReturnType<typeof setTimeout> | null = null;
@@ -36,6 +38,7 @@ class PlayModeService {
get bridge(): GameViewBridge | null { return this.bridge_; }
get runtimeEntities(): RuntimeEntityData[] { return this.cachedEntities_; }
get selectedEntityId(): number | null { return this.selectedEntityId_; }
+ get isSharedMode(): boolean { return this.sharedMode_; }
getRuntimeEntityData(entityId: number): EntityData | null {
const re = this.cachedEntities_.find(e => e.entityId === entityId);
@@ -43,6 +46,37 @@ class PlayModeService {
return null;
}
+ enterShared(): void {
+ this.snapshot_ = getEditorStore().takeSnapshot();
+ this.sharedMode_ = true;
+ this.bridge_ = null;
+ this.state_ = 'playing';
+ this.cachedEntities_ = [];
+ this.selectedEntityId_ = null;
+
+ getSharedRenderContext().enterPlayMode();
+ this.emitStateChange();
+ }
+
+ exitShared(): void {
+ if (!this.sharedMode_) return;
+
+ getSharedRenderContext().exitPlayMode();
+
+ this.sharedMode_ = false;
+ this.state_ = 'stopped';
+ this.cachedEntities_ = [];
+ this.selectedEntityId_ = null;
+
+ if (this.snapshot_) {
+ getEditorStore().restoreSnapshot(this.snapshot_);
+ this.snapshot_ = null;
+ }
+
+ this.emitStateChange();
+ this.emitSelectionChange();
+ }
+
enter(bridge: GameViewBridge): void {
if (this.state_ === 'playing') {
this.stopPolling();
@@ -55,6 +89,7 @@ class PlayModeService {
}
this.snapshot_ = getEditorStore().takeSnapshot();
this.bridge_ = bridge;
+ this.sharedMode_ = false;
this.state_ = 'playing';
this.cachedEntities_ = [];
this.selectedEntityId_ = null;
@@ -63,6 +98,10 @@ class PlayModeService {
}
exit(): void {
+ if (this.sharedMode_) {
+ this.exitShared();
+ return;
+ }
this.stopPolling();
this.bridge_ = null;
this.state_ = 'stopped';
diff --git a/editor/src/store/EditorStore.ts b/editor/src/store/EditorStore.ts
index 66605fbd4..070b95dcc 100644
--- a/editor/src/store/EditorStore.ts
+++ b/editor/src/store/EditorStore.ts
@@ -246,6 +246,7 @@ export class EditorStore {
this.state_.selectedEntities = new Set(snapshot.selectedEntities);
this.state_.isDirty = snapshot.isDirty;
this.state_.filePath = snapshot.filePath;
+ this.nextEntityId_ = this.computeNextEntityId(this.state_.scene);
this.rebuildEntityMap();
this.worldTransforms_.setScene(this.state_.scene);
this.notifySceneSync();
diff --git a/editor/src/styles/game-view.css b/editor/src/styles/game-view.css
index 8e86e820f..bac2c0a00 100644
--- a/editor/src/styles/game-view.css
+++ b/editor/src/styles/game-view.css
@@ -155,9 +155,33 @@
/* Viewport layout */
.es-gameview-canvas-area {
flex: 1;
- position: relative;
- overflow: hidden;
+ overflow: auto;
display: flex;
- align-items: center;
- justify-content: center;
+}
+
+.es-gameview-canvas {
+ margin: auto;
+ flex-shrink: 0;
+ display: none;
+}
+
+.es-gameview-scale-label {
+ font-size: 11px;
+ color: var(--es-text-secondary);
+ user-select: none;
+}
+
+.es-gameview-scale {
+ background: var(--es-input-bg);
+ color: var(--es-text-primary);
+ border: 1px solid var(--es-border);
+ border-radius: 3px;
+ font-size: 11px;
+ padding: 2px 4px;
+ height: 22px;
+ outline: none;
+}
+
+.es-gameview-scale:focus {
+ border-color: var(--es-accent);
}
diff --git a/editor/src/styles/scene-view.css b/editor/src/styles/scene-view.css
index 6c141ace7..4c6ca2adb 100644
--- a/editor/src/styles/scene-view.css
+++ b/editor/src/styles/scene-view.css
@@ -27,7 +27,7 @@
background: #1a1a1a;
}
-.es-sceneview-webgl {
+.es-sceneview-display {
position: absolute;
top: 0;
left: 0;
diff --git a/sdk/src/env.ts b/sdk/src/env.ts
index e601fcb2a..27a494791 100644
--- a/sdk/src/env.ts
+++ b/sdk/src/env.ts
@@ -1,4 +1,5 @@
let editorMode = false;
+let playMode = false;
export function setEditorMode(active: boolean): void {
editorMode = active;
@@ -11,3 +12,11 @@ export function isEditor(): boolean {
export function isRuntime(): boolean {
return !editorMode;
}
+
+export function setPlayMode(active: boolean): void {
+ playMode = active;
+}
+
+export function isPlayMode(): boolean {
+ return playMode;
+}
diff --git a/sdk/src/ui/DragPlugin.ts b/sdk/src/ui/DragPlugin.ts
index 6efce3dc6..ee835a36a 100644
--- a/sdk/src/ui/DragPlugin.ts
+++ b/sdk/src/ui/DragPlugin.ts
@@ -14,7 +14,7 @@ import type { UIInteractionData } from './UIInteraction';
import { UIEvents, UIEventQueue } from './UIEvents';
import { UICameraInfo } from './UICameraInfo';
import type { UICameraData } from './UICameraInfo';
-import { isEditor } from '../env';
+import { isEditor, isPlayMode } from '../env';
import { getEntityDepth } from './uiHelpers';
import { quaternionToAngle2D } from './uiMath';
@@ -73,7 +73,6 @@ export class DragPlugin implements Plugin {
registerComponent('DragState', DragState);
const world = app.world;
- const editorMode = isEditor();
const events = app.getResource(UIEvents) as UIEventQueue;
let pendingEntity: Entity | null = null;
@@ -83,7 +82,7 @@ export class DragPlugin implements Plugin {
app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
[Res(Input), Res(UICameraInfo)],
(input: InputState, camera: UICameraData) => {
- if (editorMode) return;
+ if (isEditor() && !isPlayMode()) return;
if (!camera.valid) return;
const worldMouse = { x: camera.worldMouseX, y: camera.worldMouseY };
diff --git a/sdk/src/ui/DropdownPlugin.ts b/sdk/src/ui/DropdownPlugin.ts
index fd54525f0..628ec7d33 100644
--- a/sdk/src/ui/DropdownPlugin.ts
+++ b/sdk/src/ui/DropdownPlugin.ts
@@ -1,6 +1,6 @@
import type { App, Plugin } from '../app';
import { registerComponent, Transform, Name, Sprite } from '../component';
-import type { SpriteData } from '../component';
+import type { SpriteData, TransformData } from '../component';
import { defineSystem, Schedule } from '../system';
import { Res } from '../resource';
import type { Entity } from '../types';
@@ -15,10 +15,9 @@ import { Text, TextAlign, TextVerticalAlign, TextOverflow } from './text';
import type { TextData } from './text';
import { UIRect } from './UIRect';
import type { UIRectData } from './UIRect';
-import { UIMask, MaskMode } from './UIMask';
import { Input } from '../input';
import type { InputState } from '../input';
-import { isEditor } from '../env';
+import { isEditor, isPlayMode } from '../env';
import { INVALID_TEXTURE } from '../types';
import { ensureComponent } from './uiHelpers';
import { DROPDOWN_ITEM_HEIGHT, DROPDOWN_FONT_SIZE, DROPDOWN_HIGHLIGHT_COLOR } from './uiConstants';
@@ -27,6 +26,8 @@ interface DropdownState {
optionEntities: Entity[];
textEntities: Entity[];
highlightIndex: number;
+ ddWidth: number;
+ ddLayer: number;
}
export class DropdownPlugin implements Plugin {
@@ -34,35 +35,39 @@ export class DropdownPlugin implements Plugin {
registerComponent('Dropdown', Dropdown);
const world = app.world;
- const editorMode = isEditor();
const dropdownStates = new Map<Entity, DropdownState>();
app.addSystemToSchedule(Schedule.Update, defineSystem(
[Res(Input), Res(UIEvents)],
(input: InputState, events: UIEventQueue) => {
- if (editorMode) return;
for (const [e] of dropdownStates) {
if (!world.valid(e)) dropdownStates.delete(e);
}
+ const editorSceneView = isEditor() && !isPlayMode();
const entities = world.getEntitiesWithComponents([Dropdown]);
for (const entity of entities) {
- ensureComponent(world, entity, Interactable, { enabled: true, blockRaycast: true });
-
const dropdown = world.get(entity, Dropdown) as DropdownData;
- const interaction = world.has(entity, UIInteraction)
- ? world.get(entity, UIInteraction) as UIInteractionData
- : null;
let state = dropdownStates.get(entity);
if (!state) {
- state = { optionEntities: [], textEntities: [], highlightIndex: -1 };
+ state = { optionEntities: [], textEntities: [], highlightIndex: -1, ddWidth: 0, ddLayer: 0 };
dropdownStates.set(entity, state);
+ normalizeListTransform(dropdown.listEntity);
if (!dropdown.isOpen) {
setListVisible(dropdown.listEntity, false);
}
+ initDropdownAppearance(entity);
}
+ if (editorSceneView) continue;
+
+ ensureComponent(world, entity, Interactable, { enabled: true, blockRaycast: true });
+
+ const interaction = world.has(entity, UIInteraction)
+ ? world.get(entity, UIInteraction) as UIInteractionData
+ : null;
+
if (interaction?.justPressed) {
if (dropdown.isOpen) {
closeDropdown(entity, dropdown, state);
@@ -82,6 +87,21 @@ export class DropdownPlugin implements Plugin {
}
}
+ let hoveredIndex = -1;
+ for (let i = 0; i < state.optionEntities.length; i++) {
+ const optEntity = state.optionEntities[i];
+ if (!world.valid(optEntity) || !world.has(optEntity, UIInteraction)) continue;
+ const optInteraction = world.get(optEntity, UIInteraction) as UIInteractionData;
+ if (optInteraction.hovered) {
+ hoveredIndex = i;
+ break;
+ }
+ }
+ if (hoveredIndex >= 0 && hoveredIndex !== state.highlightIndex) {
+ state.highlightIndex = hoveredIndex;
+ updateHighlight(state);
+ }
+
if (input.isKeyPressed('ArrowDown')) {
if (state.highlightIndex >= dropdown.options.length - 1) {
state.highlightIndex = 0;
@@ -115,6 +135,71 @@ export class DropdownPlugin implements Plugin {
{ name: 'DropdownSystem' }
), { runAfter: ['UIInteractionSystem'] });
+ app.addSystemToSchedule(Schedule.PostUpdate, defineSystem(
+ [],
+ () => {
+ for (const [entity, state] of dropdownStates) {
+ if (!world.valid(entity)) continue;
+ const dropdown = world.get(entity, Dropdown) as DropdownData;
+ if (!dropdown.isOpen) continue;
+
+ const textLayer = state.ddLayer + 3;
+ for (const te of state.textEntities) {
+ if (!world.valid(te) || !world.has(te, Sprite)) continue;
+ const s = world.get(te, Sprite) as SpriteData;
+ if (s.layer !== textLayer) {
+ world.insert(te, Sprite, {
+ texture: s.texture,
+ color: { r: s.color.r, g: s.color.g, b: s.color.b, a: s.color.a },
+ size: { x: s.size.x, y: s.size.y },
+ uvOffset: { x: s.uvOffset.x, y: s.uvOffset.y },
+ uvScale: { x: s.uvScale.x, y: s.uvScale.y },
+ layer: textLayer,
+ flipX: s.flipX,
+ flipY: s.flipY,
+ material: s.material,
+ enabled: s.enabled,
+ });
+ }
+ }
+ }
+ },
+ { name: 'DropdownPostSystem' }
+ ), { runAfter: ['UIRenderOrderSystem'] });
+
+ function normalizeListTransform(listEntity: Entity): void {
+ if (listEntity === 0 || !world.valid(listEntity)) return;
+ if (!world.has(listEntity, Transform)) return;
+ const t = world.get(listEntity, Transform) as TransformData;
+ if (t.scale.x === 0 && t.scale.y === 0 && t.scale.z === 0) {
+ world.insert(listEntity, Transform, {
+ position: { x: t.position.x, y: t.position.y, z: t.position.z },
+ rotation: { x: t.rotation.x, y: t.rotation.y, z: t.rotation.z, w: t.rotation.w },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+ }
+ }
+
+ function initDropdownAppearance(entity: Entity): void {
+ if (world.has(entity, Sprite)) {
+ const s = world.get(entity, Sprite) as SpriteData;
+ if (s.color.r === 1 && s.color.g === 1 && s.color.b === 1 && s.color.a === 1) {
+ world.insert(entity, Sprite, {
+ texture: s.texture,
+ color: { r: 0.9, g: 0.9, b: 0.9, a: 1 },
+ size: { x: s.size.x, y: s.size.y },
+ uvOffset: { x: s.uvOffset.x, y: s.uvOffset.y },
+ uvScale: { x: s.uvScale.x, y: s.uvScale.y },
+ layer: s.layer,
+ flipX: s.flipX,
+ flipY: s.flipY,
+ material: s.material,
+ enabled: s.enabled,
+ });
+ }
+ }
+ }
+
function openDropdown(entity: Entity, dropdown: DropdownData, state: DropdownState): void {
dropdown.isOpen = true;
state.highlightIndex = dropdown.selectedIndex;
@@ -123,39 +208,66 @@ export class DropdownPlugin implements Plugin {
if (listEntity === 0 || !world.valid(listEntity)) return;
setListVisible(listEntity, true);
- if (!world.has(listEntity, UIMask)) {
- world.insert(listEntity, UIMask, { enabled: true, mode: MaskMode.Scissor });
- }
destroyOptions(state);
- const listRect = world.has(listEntity, UIRect)
- ? world.get(listEntity, UIRect) as UIRectData
- : null;
- const listWidth = listRect ? listRect.size.x : 160;
+ const ddSprite = world.get(entity, Sprite) as SpriteData;
+ const ddWidth = ddSprite.size.x;
+ const ddHeight = ddSprite.size.y;
+ const ddLayer = ddSprite.layer;
+ state.ddWidth = ddWidth;
+ state.ddLayer = ddLayer;
+ const totalHeight = dropdown.options.length * DROPDOWN_ITEM_HEIGHT;
+
+ world.insert(listEntity, UIRect, {
+ anchorMin: { x: 0, y: 0 },
+ anchorMax: { x: 1, y: 0 },
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 0, y: 0 },
+ size: { x: ddWidth, y: totalHeight },
+ pivot: { x: 0.5, y: 0.5 },
+ });
+ world.insert(listEntity, Transform, {
+ position: { x: 0, y: -(ddHeight + totalHeight) / 2, z: 0 },
+ rotation: { w: 1, x: 0, y: 0, z: 0 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+ world.insert(listEntity, Sprite, {
+ texture: INVALID_TEXTURE,
+ color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: ddWidth, y: totalHeight },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer: ddLayer + 1,
+ flipX: false,
+ flipY: false,
+ material: 0,
+ enabled: true,
+ });
for (let i = 0; i < dropdown.options.length; i++) {
const optEntity = world.spawn();
+ const optY = totalHeight / 2 - (i + 0.5) * DROPDOWN_ITEM_HEIGHT;
world.insert(optEntity, Name, { value: `Option_${i}` });
world.insert(optEntity, UIRect, {
anchorMin: { x: 0, y: 1 },
anchorMax: { x: 1, y: 1 },
offsetMin: { x: 0, y: -i * DROPDOWN_ITEM_HEIGHT },
offsetMax: { x: 0, y: 0 },
- size: { x: listWidth, y: DROPDOWN_ITEM_HEIGHT },
- pivot: { x: 0.5, y: 1 },
+ size: { x: ddWidth, y: DROPDOWN_ITEM_HEIGHT },
+ pivot: { x: 0.5, y: 0.5 },
});
world.insert(optEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
+ position: { x: 0, y: optY, z: 0 },
rotation: { w: 1, x: 0, y: 0, z: 0 },
scale: { x: 1, y: 1, z: 1 },
});
world.insert(optEntity, Sprite, {
texture: INVALID_TEXTURE,
color: { r: 0.95, g: 0.95, b: 0.95, a: 1 },
- size: { x: listWidth, y: DROPDOWN_ITEM_HEIGHT },
+ size: { x: ddWidth, y: DROPDOWN_ITEM_HEIGHT },
uvOffset: { x: 0, y: 0 },
uvScale: { x: 1, y: 1 },
- layer: 0,
+ layer: ddLayer + 2,
flipX: false,
flipY: false,
material: 0,
@@ -187,18 +299,13 @@ export class DropdownPlugin implements Plugin {
anchorMax: { x: 1, y: 1 },
offsetMin: { x: 0, y: 0 },
offsetMax: { x: 0, y: 0 },
- size: { x: listWidth, y: DROPDOWN_ITEM_HEIGHT },
+ size: { x: ddWidth, y: DROPDOWN_ITEM_HEIGHT },
pivot: { x: 0.5, y: 0.5 },
});
world.setParent(textEntity, optEntity);
state.textEntities.push(textEntity);
}
- if (listRect) {
- listRect.size.y = dropdown.options.length * DROPDOWN_ITEM_HEIGHT;
- world.insert(listEntity, UIRect, listRect);
- }
-
updateHighlight(state);
}
@@ -225,22 +332,36 @@ export class DropdownPlugin implements Plugin {
if (labelEntity === 0 || !world.valid(labelEntity)) return;
if (!world.has(labelEntity, Text)) return;
- if (dropdown.selectedIndex < 0 || dropdown.selectedIndex >= dropdown.options.length) return;
-
- const text = world.get(labelEntity, Text) as TextData;
- const selectedText = dropdown.options[dropdown.selectedIndex];
-
+ const t = world.get(labelEntity, Text) as TextData;
+ let newColor = { r: t.color.r, g: t.color.g, b: t.color.b, a: t.color.a };
+ let newContent = t.content;
let changed = false;
- if (text.content !== selectedText) {
- text.content = selectedText;
+
+ if (newColor.r === 1 && newColor.g === 1 && newColor.b === 1 && newColor.a === 1) {
+ newColor = { r: 0, g: 0, b: 0, a: 1 };
changed = true;
}
- if (text.color.r === 1 && text.color.g === 1 && text.color.b === 1 && text.color.a === 1) {
- text.color = { r: 0, g: 0, b: 0, a: 1 };
- changed = true;
+
+ if (dropdown.selectedIndex >= 0 && dropdown.selectedIndex < dropdown.options.length) {
+ const selectedText = dropdown.options[dropdown.selectedIndex];
+ if (newContent !== selectedText) {
+ newContent = selectedText;
+ changed = true;
+ }
}
+
if (changed) {
- world.insert(labelEntity, Text, text);
+ world.insert(labelEntity, Text, {
+ content: newContent,
+ fontSize: t.fontSize,
+ fontFamily: t.fontFamily,
+ color: newColor,
+ align: t.align,
+ verticalAlign: t.verticalAlign,
+ wordWrap: t.wordWrap,
+ overflow: t.overflow,
+ lineHeight: t.lineHeight,
+ });
}
}
@@ -262,14 +383,22 @@ export class DropdownPlugin implements Plugin {
function setListVisible(entity: Entity, visible: boolean): void {
if (entity === 0 || !world.valid(entity)) return;
if (world.has(entity, Interactable)) {
- const inter = world.get(entity, Interactable) as InteractableData;
- inter.enabled = visible;
- world.insert(entity, Interactable, inter);
+ world.insert(entity, Interactable, { enabled: visible, blockRaycast: true, raycastTarget: true });
}
if (world.has(entity, Sprite)) {
- const sprite = world.get(entity, Sprite) as SpriteData;
- sprite.enabled = visible;
- world.insert(entity, Sprite, sprite);
+ const s = world.get(entity, Sprite) as SpriteData;
+ world.insert(entity, Sprite, {
+ texture: s.texture,
+ color: { r: s.color.r, g: s.color.g, b: s.color.b, a: s.color.a },
+ size: { x: s.size.x, y: s.size.y },
+ uvOffset: { x: s.uvOffset.x, y: s.uvOffset.y },
+ uvScale: { x: s.uvScale.x, y: s.uvScale.y },
+ layer: s.layer,
+ flipX: s.flipX,
+ flipY: s.flipY,
+ material: s.material,
+ enabled: visible,
+ });
}
}
@@ -277,13 +406,21 @@ export class DropdownPlugin implements Plugin {
for (let i = 0; i < state.optionEntities.length; i++) {
const optEntity = state.optionEntities[i];
if (!world.valid(optEntity) || !world.has(optEntity, Sprite)) continue;
- const sprite = world.get(optEntity, Sprite) as SpriteData;
- if (i === state.highlightIndex) {
- sprite.color = { ...DROPDOWN_HIGHLIGHT_COLOR };
- } else {
- sprite.color = { r: 0.95, g: 0.95, b: 0.95, a: 1 };
- }
- world.insert(optEntity, Sprite, sprite);
+ const color = i === state.highlightIndex
+ ? { ...DROPDOWN_HIGHLIGHT_COLOR }
+ : { r: 0.95, g: 0.95, b: 0.95, a: 1 };
+ world.insert(optEntity, Sprite, {
+ texture: INVALID_TEXTURE,
+ color,
+ size: { x: state.ddWidth, y: DROPDOWN_ITEM_HEIGHT },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer: state.ddLayer + 2,
+ flipX: false,
+ flipY: false,
+ material: 0,
+ enabled: true,
+ });
}
}
diff --git a/sdk/src/ui/FocusPlugin.ts b/sdk/src/ui/FocusPlugin.ts
index ea3af36c3..253e00f21 100644
--- a/sdk/src/ui/FocusPlugin.ts
+++ b/sdk/src/ui/FocusPlugin.ts
@@ -11,7 +11,7 @@ import { Interactable } from './Interactable';
import type { InteractableData } from './Interactable';
import { UIInteraction } from './UIInteraction';
import type { UIInteractionData } from './UIInteraction';
-import { isEditor } from '../env';
+import { isEditor, isPlayMode } from '../env';
import { UIEvents, UIEventQueue } from './UIEvents';
export class FocusPlugin implements Plugin {
@@ -19,14 +19,13 @@ export class FocusPlugin implements Plugin {
registerComponent('Focusable', Focusable);
const world = app.world;
- const editorMode = isEditor();
const focusManager = new FocusManagerState();
app.insertResource(FocusManager, focusManager);
app.addSystemToSchedule(Schedule.Update, defineSystem(
[Res(Input), Res(UIEvents)],
(input: InputState, events: UIEventQueue) => {
- if (editorMode) return;
+ if (isEditor() && !isPlayMode()) return;
if (focusManager.focusedEntity !== null && !world.valid(focusManager.focusedEntity)) {
focusManager.focusedEntity = null;
}
diff --git a/sdk/src/ui/ListViewPlugin.ts b/sdk/src/ui/ListViewPlugin.ts
index 91249bed0..472d9c186 100644
--- a/sdk/src/ui/ListViewPlugin.ts
+++ b/sdk/src/ui/ListViewPlugin.ts
@@ -15,7 +15,7 @@ import { Interactable } from './Interactable';
import { UIMask, MaskMode } from './UIMask';
import { UIInteraction } from './UIInteraction';
import type { UIInteractionData } from './UIInteraction';
-import { isEditor } from '../env';
+import { isEditor, isPlayMode } from '../env';
import { ensureComponent } from './uiHelpers';
import { SCROLL_WHEEL_SENSITIVITY } from './uiConstants';
@@ -52,14 +52,13 @@ export class ListViewPlugin implements Plugin {
registerComponent('ListView', ListView);
const world = app.world;
- const editorMode = isEditor();
const listViewStates = new Map<Entity, ListViewState>();
activeListViewStates = listViewStates;
app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
[Res(Input)],
(input: InputState) => {
- if (editorMode) return;
+ if (isEditor() && !isPlayMode()) return;
for (const [e, st] of listViewStates) {
if (!world.valid(e)) {
for (const itemEntity of st.itemEntities.values()) {
diff --git a/sdk/src/ui/ScrollViewPlugin.ts b/sdk/src/ui/ScrollViewPlugin.ts
index b717f568e..4708ab612 100644
--- a/sdk/src/ui/ScrollViewPlugin.ts
+++ b/sdk/src/ui/ScrollViewPlugin.ts
@@ -16,7 +16,7 @@ import { UIInteraction } from './UIInteraction';
import type { UIInteractionData } from './UIInteraction';
import { UICameraInfo } from './UICameraInfo';
import type { UICameraData } from './UICameraInfo';
-import { isEditor } from '../env';
+import { isEditor, isPlayMode } from '../env';
import { getEffectiveWidth, getEffectiveHeight, ensureComponent } from './uiHelpers';
import {
SCROLL_VELOCITY_SMOOTHING, SCROLL_VELOCITY_NEW_WEIGHT,
@@ -37,7 +37,6 @@ export class ScrollViewPlugin implements Plugin {
registerComponent('ScrollView', ScrollView);
const world = app.world;
- const editorMode = isEditor();
const states = new Map<Entity, ScrollState>();
let lastTime = 0;
const worldMouse = { x: 0, y: 0 };
@@ -45,7 +44,7 @@ export class ScrollViewPlugin implements Plugin {
app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
[Res(Input), Res(UICameraInfo)],
(input: InputState, camera: UICameraData) => {
- if (editorMode) return;
+ if (isEditor() && !isPlayMode()) return;
const now = performance.now() / 1000;
const dt = lastTime > 0 ? Math.min(now - lastTime, SCROLL_MAX_DT) : 0;
diff --git a/sdk/src/ui/SliderPlugin.ts b/sdk/src/ui/SliderPlugin.ts
index a89449df2..911c246c7 100644
--- a/sdk/src/ui/SliderPlugin.ts
+++ b/sdk/src/ui/SliderPlugin.ts
@@ -1,12 +1,11 @@
import type { App, Plugin } from '../app';
-import { registerComponent, Transform, Sprite } from '../component';
-import type { TransformData, SpriteData } from '../component';
+import { registerComponent, Transform } from '../component';
+import type { TransformData } from '../component';
import { defineSystem, Schedule } from '../system';
import { Res } from '../resource';
import { Input } from '../input';
import type { InputState } from '../input';
import type { Entity } from '../types';
-import type { World } from '../world';
import { Slider, FillDirection } from './Slider';
import type { SliderData } from './Slider';
import { UIRect } from './UIRect';
@@ -17,9 +16,9 @@ import type { UIInteractionData } from './UIInteraction';
import { UICameraInfo } from './UICameraInfo';
import type { UICameraData } from './UICameraInfo';
import { UIEvents, UIEventQueue } from './UIEvents';
-import { isEditor } from '../env';
-import { applyDirectionalFill, getEffectiveWidth, getEffectiveHeight, ensureComponent } from './uiHelpers';
-import { computeUIRectLayout } from './uiLayout';
+import { isEditor, isPlayMode } from '../env';
+import { applyDirectionalFill, getEffectiveWidth, getEffectiveHeight, ensureComponent, layoutChildEntity } from './uiHelpers';
+import type { LayoutRect } from './uiLayout';
import { quaternionToAngle2D } from './uiMath';
function computeSliderValue(
@@ -80,46 +79,11 @@ function syncHandleRect(
}
}
-function syncFillLayout(
- world: World, fillEntity: Entity,
- sliderW: number, sliderH: number,
-): void {
- if (!world.has(fillEntity, UIRect)) return;
- const fillRect = world.get(fillEntity, UIRect) as UIRectData;
- const halfW = sliderW / 2;
- const halfH = sliderH / 2;
- const parentRect = { left: -halfW, bottom: -halfH, right: halfW, top: halfH };
- const result = computeUIRectLayout(
- fillRect.anchorMin, fillRect.anchorMax,
- fillRect.offsetMin, fillRect.offsetMax,
- fillRect.size, parentRect, fillRect.pivot,
- );
-
- if (world.has(fillEntity, Sprite)) {
- const sprite = world.get(fillEntity, Sprite) as SpriteData;
- if (sprite.size.x !== result.width || sprite.size.y !== result.height) {
- sprite.size.x = result.width;
- sprite.size.y = result.height;
- world.insert(fillEntity, Sprite, sprite);
- }
- }
-
- if (world.has(fillEntity, Transform)) {
- const t = world.get(fillEntity, Transform) as TransformData;
- const curPos = t.position;
- if (curPos.x !== result.originX || curPos.y !== result.originY) {
- t.position = { x: result.originX, y: result.originY, z: curPos.z };
- world.insert(fillEntity, Transform, t);
- }
- }
-}
-
export class SliderPlugin implements Plugin {
build(app: App): void {
registerComponent('Slider', Slider);
const world = app.world;
- const editorMode = isEditor();
let draggingSlider: Entity | null = null;
app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
@@ -136,7 +100,7 @@ export class SliderPlugin implements Plugin {
const slider = world.get(entity, Slider) as SliderData;
const rect = world.get(entity, UIRect) as UIRectData;
- if (!editorMode) {
+ if (!isEditor() || isPlayMode()) {
const hasWT = world.has(entity, Transform);
if (hasWT) {
ensureComponent(world, entity, Interactable, { enabled: true, blockRaycast: true });
@@ -187,20 +151,23 @@ export class SliderPlugin implements Plugin {
const range = slider.maxValue - slider.minValue;
const normalizedValue = range > 0 ? (slider.value - slider.minValue) / range : 0;
+ const sw = getEffectiveWidth(rect, entity);
+ const sh = getEffectiveHeight(rect, entity);
+ const parentRect: LayoutRect = {
+ left: -sw / 2, bottom: -sh / 2,
+ right: sw / 2, top: sh / 2,
+ };
+
if (slider.fillEntity !== 0 && world.valid(slider.fillEntity)) {
applyDirectionalFill(world, slider.fillEntity, slider.direction, normalizedValue);
- const sw = getEffectiveWidth(rect, entity);
- const sh = getEffectiveHeight(rect, entity);
- syncFillLayout(world, slider.fillEntity, sw, sh);
+ layoutChildEntity(world, slider.fillEntity, parentRect, 0, 0);
}
if (slider.handleEntity !== 0 && world.valid(slider.handleEntity) && world.has(slider.handleEntity, UIRect)) {
const handleRect = world.get(slider.handleEntity, UIRect) as UIRectData;
syncHandleRect(handleRect, slider.direction, normalizedValue);
world.insert(slider.handleEntity, UIRect, handleRect);
- const sw = getEffectiveWidth(rect, entity);
- const sh = getEffectiveHeight(rect, entity);
- syncFillLayout(world, slider.handleEntity, sw, sh);
+ layoutChildEntity(world, slider.handleEntity, parentRect, 0, 0);
}
}
},
diff --git a/sdk/src/ui/TextInputPlugin.ts b/sdk/src/ui/TextInputPlugin.ts
index fe494916f..d67b14908 100644
--- a/sdk/src/ui/TextInputPlugin.ts
+++ b/sdk/src/ui/TextInputPlugin.ts
@@ -11,7 +11,7 @@ import { FocusManager, FocusManagerState } from './Focusable';
import { UIEvents, UIEventQueue } from './UIEvents';
import { Res } from '../resource';
import { platformCreateCanvas } from '../platform';
-import { isEditor } from '../env';
+import { isEditor, isPlayMode } from '../env';
import { ensureSprite, wrapText, nextPowerOf2, ensureComponent } from './uiHelpers';
import { CURSOR_BLINK_INTERVAL, TEXT_INPUT_LINE_HEIGHT_RATIO } from './uiConstants';
@@ -28,8 +28,7 @@ export class TextInputPlugin implements Plugin {
build(app: App): void {
registerComponent('TextInput', TextInput);
- const editorMode = isEditor();
- if (editorMode) return;
+ if (isEditor() && !isPlayMode()) return;
const module = app.wasmModule;
if (!module) {
diff --git a/sdk/src/ui/TogglePlugin.ts b/sdk/src/ui/TogglePlugin.ts
index 6b1c9ac4c..9e8a0b7b4 100644
--- a/sdk/src/ui/TogglePlugin.ts
+++ b/sdk/src/ui/TogglePlugin.ts
@@ -13,7 +13,7 @@ import type { ToggleData } from './Toggle';
import { ToggleGroup } from './ToggleGroup';
import type { ToggleGroupData } from './ToggleGroup';
import { UIEvents, UIEventQueue } from './UIEvents';
-import { isEditor } from '../env';
+import { isEditor, isPlayMode } from '../env';
import { applyColorTransition, ensureComponent } from './uiHelpers';
export class TogglePlugin implements Plugin {
@@ -22,7 +22,6 @@ export class TogglePlugin implements Plugin {
registerComponent('ToggleGroup', ToggleGroup);
const world = app.world;
- const editorMode = isEditor();
const initializedEntities = new Set<Entity>();
app.addSystemToSchedule(Schedule.Update, defineSystem(
@@ -30,7 +29,7 @@ export class TogglePlugin implements Plugin {
(events: UIEventQueue) => {
const toggleEntities = world.getEntitiesWithComponents([Toggle]);
- if (!editorMode) {
+ if (!isEditor() || isPlayMode()) {
const groupFirstOn = new Map<Entity, Entity>();
for (const entity of toggleEntities) {
if (initializedEntities.has(entity)) continue;
diff --git a/sdk/src/ui/UIInteractionPlugin.ts b/sdk/src/ui/UIInteractionPlugin.ts
index 5e5c9d59d..a4f866900 100644
--- a/sdk/src/ui/UIInteractionPlugin.ts
+++ b/sdk/src/ui/UIInteractionPlugin.ts
@@ -7,7 +7,7 @@ import { Input } from '../input';
import type { InputState } from '../input';
import type { Entity } from '../types';
import type { World } from '../world';
-import { isEditor } from '../env';
+import { isEditor, isPlayMode } from '../env';
import { Interactable } from './Interactable';
import { UIInteraction } from './UIInteraction';
import type { UIInteractionData } from './UIInteraction';
@@ -54,8 +54,6 @@ export class UIInteractionPlugin implements Plugin {
const events = new UIEventQueue();
app.insertResource(UIEvents, events);
- const editorMode = isEditor();
-
let hoveredEntity: Entity | null = null;
let pressedEntity: Entity | null = null;
@@ -64,7 +62,7 @@ export class UIInteractionPlugin implements Plugin {
(input: InputState, camera: UICameraData) => {
events.drain();
- if (editorMode) return;
+ if (isEditor() && !isPlayMode()) return;
const interactionEntities = world.getEntitiesWithComponents([UIInteraction]);
for (const entity of interactionEntities) {