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): gated screenshot / visual-regression capture

esengine1cc3b8b79373
diff --git a/desktop/electron/main.ts b/desktop/electron/main.ts
index ae98c202b..1d59517f9 100644
--- a/desktop/electron/main.ts
+++ b/desktop/electron/main.ts
@@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import { app, BrowserWindow, shell, ipcMain, dialog, protocol } from 'electron';
import { fileURLToPath } from 'node:url';
-import { readFile } from 'node:fs/promises';
+import { readFile, writeFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import path from 'node:path';
import {
@@ -119,6 +119,52 @@ const VITE_PUBLIC = VITE_DEV_SERVER_URL
let win: BrowserWindow | null = null;
+/**
+ * Screenshot / visual-regression capture (gated on ESTELLA_SHOT=out.png). Drives the
+ * renderer's `?automation=1` hook to open ESTELLA_SHOT_PROJECT (+ optional
+ * ESTELLA_SHOT_SCENE), lets the panels + WebGL viewport settle, writes a PNG, and quits.
+ */
+async function runScreenshot(w: BrowserWindow, out: string): Promise<void> {
+ const exec = (code: string): Promise<unknown> =>
+ w.webContents.executeJavaScript(code, true).catch(() => undefined);
+ const sleep = (ms: number): Promise<void> => new Promise((r) => setTimeout(r, ms));
+ try {
+ let ready = false;
+ for (let i = 0; i < 100; i++) {
+ if (await exec('!!window.__estellaEditor')) { ready = true; break; }
+ await sleep(100);
+ }
+ if (!ready) throw new Error('automation hook never attached');
+
+ const project = process.env.ESTELLA_SHOT_PROJECT;
+ const scene = process.env.ESTELLA_SHOT_SCENE;
+ if (project) {
+ const ok = await exec(`window.__estellaEditor.open(${JSON.stringify(project)})`);
+ if (ok) await exec('window.__estellaEditor.enterEditor()');
+ await sleep(1500);
+ if (scene) { await exec(`window.__estellaEditor.openScene(${JSON.stringify(scene)})`); await sleep(1500); }
+ }
+ const selectName = process.env.ESTELLA_SHOT_SELECT;
+ if (selectName) {
+ await exec(`(async () => {
+ const s = window.__estellaEditor.surface;
+ const tree = await s.getSceneTree();
+ const hit = (tree || []).find((n) => n.name === ${JSON.stringify(selectName)});
+ if (hit) s.select(hit.id);
+ })()`);
+ await sleep(800);
+ }
+ await sleep(2500); // dockview panels + the WebGL viewport settle
+ const img = await w.webContents.capturePage();
+ await writeFile(out, img.toPNG());
+ console.log('[screenshot] wrote', out);
+ } catch (e) {
+ console.error('[screenshot] failed:', e);
+ } finally {
+ app.quit();
+ }
+}
+
function createWindow() {
win = new BrowserWindow({
width: 1480,
@@ -145,15 +191,22 @@ function createWindow() {
return { action: 'deny' };
});
+ // Screenshot/visual-regression mode (ESTELLA_SHOT=out.png): open ?automation=1 so
+ // the renderer hook is live, then drive the launcher→editor flow and capturePage.
+ const shotOut = process.env.ESTELLA_SHOT;
+ const automation = shotOut ? '?automation=1' : '';
+
if (VITE_DEV_SERVER_URL) {
- win.loadURL(VITE_DEV_SERVER_URL);
- win.webContents.openDevTools({ mode: 'detach' });
+ win.loadURL(VITE_DEV_SERVER_URL + automation);
+ if (!shotOut) win.webContents.openDevTools({ mode: 'detach' });
} else {
// Load over app:// (not file://) so the engine glue + play realm resolve from
// a real origin. handleApp serves dist/.
- win.loadURL(`${APP_ORIGIN}/index.html`);
+ win.loadURL(`${APP_ORIGIN}/index.html${automation}`);
}
+ if (shotOut) void runScreenshot(win, shotOut);
+
// Unsaved-changes quit guard: prompt before closing a window with a dirty scene.
// `sceneDirty` is pushed from the renderer (app:dirty); `quitting` lets the chosen
// action close past this handler.
diff --git a/desktop/package.json b/desktop/package.json
index 0be242d3f..966ea05f2 100644
--- a/desktop/package.json
+++ b/desktop/package.json
@@ -17,6 +17,7 @@
"editor:mcp": "electron scripts/editor-mcp.mjs",
"editor:mcp:build": "vite build && electron scripts/editor-mcp.mjs",
"editor:mcp:e2e": "vite build && node scripts/editor-mcp-e2e.mjs",
+ "editor:shot": "vite build && electron .",
"verify:render:spine": "vite build && ESTELLA_VERIFY_SCENE=/scenes/spine-test.esscene ESTELLA_VERIFY_MANIFEST=/scenes/spine-test.textures.json electron scripts/headless-verify.mjs",
"verify:render:particles": "vite build && ESTELLA_VERIFY_PLAY=1 ESTELLA_VERIFY_SCENE=/scenes/particle-demo.esscene ESTELLA_VERIFY_MANIFEST=/scenes/particle-demo.textures.json ESTELLA_VERIFY_STEPS=90 electron scripts/headless-verify.mjs",
"verify:render:particles:gradient": "vite build && ESTELLA_VERIFY_PLAY=1 ESTELLA_VERIFY_SCENE=/scenes/particle-gradient.esscene ESTELLA_VERIFY_MANIFEST=/scenes/particle-gradient.textures.json ESTELLA_VERIFY_W=256 ESTELLA_VERIFY_H=256 ESTELLA_VERIFY_STEPS=20 ESTELLA_VERIFY_EXPECT=\"[{\\\"x\\\":0.5,\\\"y\\\":0.5,\\\"rgb\\\":[0,255,0],\\\"tol\\\":90}]\" electron scripts/headless-verify.mjs",
diff --git a/desktop/src/main.tsx b/desktop/src/main.tsx
index f2ff54bd7..7224b27d3 100644
--- a/desktop/src/main.tsx
+++ b/desktop/src/main.tsx
@@ -30,6 +30,9 @@ import './theme/menus.css';
import './theme/settings.css';
import './theme/launcher.css';
import { App } from './App';
+import { ProjectStore } from './project/ProjectStore';
+import { useEditorStore } from './store/editorStore';
+import { EditorControlSurface } from './engine/EditorSession';
import { LogStore } from './store/LogStore';
import { initFsWatch } from './project/fsWatch';
// Register the built-in settings (side effect) and replay persisted ones.
@@ -44,6 +47,18 @@ applySettings();
// edits made outside the editor) via the main-process project watcher.
initFsWatch();
+// Automation hook (screenshots / visual-regression): with `?automation=1`, expose the
+// minimum to drive the launcher→editor flow from a headless driver. Gated so the normal
+// editor never carries it; mirrors the headless render host's `window.__estellaHeadless`.
+if (new URLSearchParams(location.search).has('automation')) {
+ (window as unknown as { __estellaEditor?: unknown }).__estellaEditor = {
+ open: (root: string) => ProjectStore.open(root),
+ enterEditor: () => useEditorStore.getState().enterEditor(),
+ openScene: (rel: string) => ProjectStore.openScene(rel),
+ surface: EditorControlSurface,
+ };
+}
+
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
diff --git a/examples/platformer/.esengine/workspace.json b/examples/platformer/.esengine/workspace.json
new file mode 100644
index 000000000..49baf3816
--- /dev/null
+++ b/examples/platformer/.esengine/workspace.json
@@ -0,0 +1,3 @@
+{
+ "lastOpenedScene": "assets/scenes/main.esscene"
+}