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.

fix(ui): safe-area insets are asked for again, not decided on frame one

esengine348e5426c780
diff --git a/desktop/scripts/launch-export.mjs b/desktop/scripts/launch-export.mjs
index 8ec935d47..ef737be13 100644
--- a/desktop/scripts/launch-export.mjs
+++ b/desktop/scripts/launch-export.mjs
@@ -24,6 +24,10 @@
* --touch present as a touch device (maxTouchPoints > 0), so a
* build that puts its on-screen controls up for one can
* be driven the way a phone would drive it
+ * --safe-area t,r,b,l a screen with insets (a notch, a home bar), via the
+ * CSS variables the web platform reads
+ * --probe a,b,c after settling, print where those named entities are
+ * (opens the package with ?headless)
* --log <regex> also print console lines matching this (the engine's own
* warnings say why a subsystem sat out; only `[engine]`
* lines are forwarded otherwise, which means diagnosing
@@ -77,14 +81,27 @@ const MIME = {
/** Serve the export directory. Range requests are not implemented: a build that
* needs them (video seek) would read as a broken asset here, not a broken server. */
-function serve(root) {
+function serve(root, safeArea) {
const server = http.createServer(async (req, res) => {
try {
let rel = decodeURIComponent(new URL(req.url, 'http://x').pathname).replace(/^\/+/, '');
if (rel === '') rel = 'index.html';
const abs = path.join(root, rel);
if (!abs.startsWith(root)) { res.writeHead(403).end(); return; }
- const bytes = await readFile(abs);
+ let bytes = await readFile(abs);
+ // Insets have to exist before the first script runs: the engine reads
+ // them once when its UI plugins build, and a variable set from the
+ // outside after that is a variable nobody ever asks about again.
+ if (safeArea && rel === 'index.html') {
+ const [t = 0, r = 0, b = 0, l = 0] = safeArea.split(',').map(Number);
+ const html = String(bytes);
+ const injected = html.replace(
+ '<head>',
+ `<head><style>:root{--sat:${t}px;--sar:${r}px;--sab:${b}px;--sal:${l}px}</style>`,
+ );
+ if (injected === html) console.log(' safe-area: no <head> to inject into');
+ bytes = Buffer.from(injected);
+ }
res.writeHead(200, { 'content-type': MIME[path.extname(abs).toLowerCase()] ?? 'application/octet-stream' })
.end(bytes);
} catch {
@@ -115,8 +132,9 @@ async function main() {
app.exit(2);
return;
}
- const server = await serve(DIR);
- const base = `http://127.0.0.1:${server.address().port}/`;
+ const PROBE = flag('probe', '');
+ const server = await serve(DIR, flag('safe-area', ''));
+ const base = `http://127.0.0.1:${server.address().port}/${PROBE ? '?headless' : ''}`;
const win = new BrowserWindow({
// Content size, not window size: with the frame counted in, the surface came
@@ -161,6 +179,14 @@ async function main() {
console.log(` input: ${ran} source(s) over ${Number(spec.frames ?? 40)} frames`);
}
+ if (PROBE) {
+ const names = PROBE.split(',').map((n) => n.trim()).filter(Boolean);
+ const seen = await win.webContents.executeJavaScript(
+ `window.__estellaCooked?.probe(${JSON.stringify(names)}) ?? null`,
+ ).catch((e) => ({ error: String(e) }));
+ console.log(` probe: ${JSON.stringify(seen)}`);
+ }
+
const image = await win.webContents.capturePage();
if (OUT) await writeFile(OUT, image.toPNG());
stop();
diff --git a/examples/celestial-heights/assets/scenes/hud.esscene b/examples/celestial-heights/assets/scenes/hud.esscene
index 551ae1171..d669cf1c6 100644
--- a/examples/celestial-heights/assets/scenes/hud.esscene
+++ b/examples/celestial-heights/assets/scenes/hud.esscene
@@ -8,12 +8,9 @@
"name": "Canvas",
"parent": null,
"children": [
- 51,
60,
- 70,
- 80,
84,
- 86
+ 95
],
"visible": true,
"components": [
@@ -40,7 +37,7 @@
{
"id": 51,
"name": "HUD",
- "parent": 50,
+ "parent": 95,
"children": [
52,
53,
@@ -391,7 +388,7 @@
{
"id": 70,
"name": "PackPanel",
- "parent": 50,
+ "parent": 95,
"children": [
71,
72
@@ -530,7 +527,7 @@
{
"id": 80,
"name": "BossPanel",
- "parent": 50,
+ "parent": 95,
"children": [
81,
82
@@ -834,7 +831,7 @@
{
"id": 86,
"name": "TouchLayer",
- "parent": 50,
+ "parent": 95,
"children": [
87,
89
@@ -1045,6 +1042,55 @@
}
}
]
+ },
+ {
+ "id": 95,
+ "name": "SafeFrame",
+ "parent": 50,
+ "children": [
+ 51,
+ 70,
+ 80,
+ 86
+ ],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {}
+ },
+ {
+ "type": "UINode",
+ "data": {
+ "position": 1,
+ "insetLeft": {
+ "value": 0,
+ "unit": 0
+ },
+ "insetRight": {
+ "value": 0,
+ "unit": 0
+ },
+ "insetTop": {
+ "value": 0,
+ "unit": 0
+ },
+ "insetBottom": {
+ "value": 0,
+ "unit": 0
+ }
+ }
+ },
+ {
+ "type": "SafeArea",
+ "data": {
+ "applyTop": true,
+ "applyBottom": true,
+ "applyLeft": true,
+ "applyRight": true
+ }
+ }
+ ]
}
]
}
diff --git a/examples/celestial-heights/src/systems/touch.ts b/examples/celestial-heights/src/systems/touch.ts
index f07ab8113..d59724736 100644
--- a/examples/celestial-heights/src/systems/touch.ts
+++ b/examples/celestial-heights/src/systems/touch.ts
@@ -67,3 +67,4 @@ export const touchButtonSystem = defineSystem(
},
{ name: 'TouchButtonSystem' },
);
+
diff --git a/sdk/src/ui/layout/safe-area.ts b/sdk/src/ui/layout/safe-area.ts
index 3ab441302..fb6c9b307 100644
--- a/sdk/src/ui/layout/safe-area.ts
+++ b/sdk/src/ui/layout/safe-area.ts
@@ -101,22 +101,19 @@ export class SafeAreaPlugin implements Plugin {
registerComponent('SafeArea', SafeArea);
const world = app.world;
- let cachedInsets: SafeAreaInsets = getSafeAreaInsets();
- let dirty = true;
+ // Read on demand rather than once at build: on iOS the env() values are
+ // only real after the first layout, and a plugin that built before that
+ // would hold zeros for the life of the game.
+ let cachedInsets: SafeAreaInsets = { top: 0, bottom: 0, left: 0, right: 0 };
+ let stale = true;
let prevScreenH = 0;
let prevWorldH = 0;
if (isWeChat()) {
const g = globalThis as unknown as WxGlobal;
- g.wx?.onWindowResize?.(() => {
- cachedInsets = getSafeAreaInsets();
- dirty = true;
- });
+ g.wx?.onWindowResize?.(() => { stale = true; });
} else if (typeof window !== 'undefined') {
- this.onResize_ = () => {
- cachedInsets = getSafeAreaInsets();
- dirty = true;
- };
+ this.onResize_ = () => { stale = true; };
window.addEventListener('resize', this.onResize_);
}
@@ -129,11 +126,16 @@ export class SafeAreaPlugin implements Plugin {
if (camera.screenH !== prevScreenH || worldH !== prevWorldH) {
prevScreenH = camera.screenH;
prevWorldH = worldH;
- dirty = true;
+ stale = true;
}
- if (!dirty) return;
- dirty = false;
+ // Only the READ is throttled; the nodes are visited every frame.
+ // A scene loaded later brings its own, and deciding once on
+ // frame one never looks at them.
+ if (stale) {
+ stale = false;
+ cachedInsets = getSafeAreaInsets();
+ }
const dpr = platformDevicePixelRatio();
const insetScale = isWeChat() ? (worldH / camera.screenH) : (dpr * worldH / camera.screenH);