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(editor): a driven click is a press and a release, a frame apart

yhh969660531dcf
diff --git a/desktop/electron/surfaceDriver.ts b/desktop/electron/surfaceDriver.ts
index 8aabb1e59..8f8ed7115 100644
--- a/desktop/electron/surfaceDriver.ts
+++ b/desktop/electron/surfaceDriver.ts
@@ -325,19 +325,23 @@ export function createSurfaceDriver(
const button = Number(input.button ?? 0);
const code = JSON.stringify(String(input.code ?? ''));
const id = 1;
+ // Every injection takes the frame it needs to be sampled, and the wrapper
+ // takes one first so the boundary is ours: an edge sent into a frame past
+ // its PreUpdate is cleared before anything reads it, and still says 'ok'.
const call = {
- click: `i.down(${x},${y},${button}); i.up(${button});`,
- move: `i.move(${x},${y});`,
- down: `i.down(${x},${y},${button});`,
- up: `i.up(${button});`,
- wheel: `i.wheel(${x},${y});`,
- key_down: `return i.keyDown(${code});`,
- key_up: `i.keyUp(${code});`,
- tap: `i.touchStart(${id},${x},${y}); i.touchEnd(${id});`,
+ click: `i.move(${x},${y}); await f(1); i.down(${x},${y},${button}); await f(1); `
+ + `i.up(${button}); await f(2);`,
+ move: `i.move(${x},${y}); await f(1);`,
+ down: `i.down(${x},${y},${button}); await f(1);`,
+ up: `i.up(${button}); await f(1);`,
+ wheel: `i.wheel(${x},${y}); await f(1);`,
+ key_down: `const r = i.keyDown(${code}); await f(1); return r;`,
+ key_up: `i.keyUp(${code}); await f(1);`,
+ tap: `i.touchStart(${id},${x},${y}); await f(1); i.touchEnd(${id}); await f(2);`,
// A pad is HELD until released — a game reads it every frame, so a
// one-shot press would be gone before the frame that reads it.
gamepad: `i.gamepad(${Number(input.pad ?? 0)},${JSON.stringify(input.buttons ?? [])},`
- + `${JSON.stringify(input.axes ?? [])});`,
+ + `${JSON.stringify(input.axes ?? [])}); await f(1);`,
gamepad_release: `i.releaseGamepad(${input.pad === undefined ? '' : Number(input.pad)});`,
}[kind ?? ''];
// Clicking a NAMED element answers with where it went, so it is not one
@@ -354,11 +358,12 @@ export function createSurfaceDriver(
);
}
return unwrap(await frame.executeJavaScript(carryError(
- `(() => { const p = window.__estellaPlay; if (!p || !p.input) `
+ `(async () => { const p = window.__estellaPlay; if (!p || !p.input) `
+ `throw new Error('this play realm publishes no input door — it predates play_input'); `
+ + `const i = p.input; const f = (n) => (p.step ? p.step(n) : undefined); await f(1); `
// A kind that has something to say answers with it; the rest, and any
// realm older than the one that started answering, keep 'ok'.
- + `const i = p.input; const r = (() => { ${call} })(); `
+ + `const r = await (async () => { ${call} })(); `
+ `return r === undefined ? 'ok' : r; })()`,
)));
}
diff --git a/desktop/shared/toolCatalog.mjs b/desktop/shared/toolCatalog.mjs
index 644e97db5..c4617081a 100644
--- a/desktop/shared/toolCatalog.mjs
+++ b/desktop/shared/toolCatalog.mjs
@@ -683,7 +683,8 @@ const ATOMS = [
{ name: 'click_ui', effect: 'ephemeral',
description: 'Click a UI element BY NAME in the running game, or refuse. '
+ 'Computing where a button is and clicking there is the arithmetic that lands beside it and reports success — so the point is put to the ENGINE\'S OWN hit test first, and unless it answers with that element (or a child of it, which is what a label on a button is) nothing is sent and the reply says what is there instead. '
- + 'Answers { entity, name, at, hit }. Two entities of the same name is refused rather than picked between. Use play_input with x/y for anything that is not a UI element.',
+ + 'The click LANDS before this returns: it is spread over the four frames a press-then-release needs (an edge is dropped at the end of every frame, so a click sent inside one is cleared before anything samples it), and what it caused has settled by the time you read. '
+ + 'Answers { entity, name, at, hit, frames }. Two entities of the same name is refused rather than picked between. Use play_input with x/y for anything that is not a UI element.',
schema: obj({
target: { type: 'string', description: 'the entity name of the UI element' },
frame: { type: 'number' },
@@ -724,7 +725,9 @@ const ATOMS = [
+ 'x/y are SCREEN pixels, canvas-relative, y DOWN — the same numbers a real pointer event carries and what `Input.mouseX/mouseY` then read; '
+ 'ask the camera (CameraView.screenToWorld / UICameraInfo.worldMouseX) for where that is in the world rather than converting by hand. '
+ '`code` is a KeyboardEvent code for the key kinds. It goes through the platform binding\'s own callbacks, so UI gets first refusal exactly as it would for a real event. '
- + 'A pressed EDGE (isMouseButtonPressed / isKeyPressed) lasts one frame: call step() right after, then read the result. '
+ + 'The event ARRIVES before this returns: it carries the frame it needs to be sampled (a click, four — move, press, release, settle), '
+ + 'because a pressed edge (isMouseButtonPressed / isKeyPressed) is dropped at the end of every frame and an injection from outside the game can land mid-frame. '
+ + 'So read the result straight after; step() is for letting the game RUN on, not for delivering the input. '
+ 'A key stays DOWN until you send key_up for it, and `key_down` on one already held makes no new edge — it answers `pressEdge: false` and the game does not react, while everything you read back afterwards looks perfectly normal. '
+ 'Calling the game\'s own handler instead proves only that the handler works — not that a click reaches it.',
schema: obj({
diff --git a/desktop/src/playHost.ts b/desktop/src/playHost.ts
index 8f2e63d4d..e958d936d 100644
--- a/desktop/src/playHost.ts
+++ b/desktop/src/playHost.ts
@@ -82,6 +82,11 @@ function inputState(): InputState | null {
* own pick is asked what is at the point, and a click is sent only when the
* answer is that element or a descendant of it (a label on a button is one, and
* the interaction system bubbles from it anyway).
+ *
+ * A click is press THEN release, which is two frames however it is delivered, so
+ * the gesture is spread over stepped frames here rather than left to the caller:
+ * move, press, release, and one more for what the click caused to settle. What
+ * it returns on is therefore a game that has already reacted.
*/
async function clickUiByName(app_: App, name: string): Promise<unknown> {
const world = app_.world;
@@ -134,10 +139,16 @@ async function clickUiByName(app_: App, name: string): Promise<unknown> {
const dpr = window.devicePixelRatio || 1;
const screen = { x: gl.x / dpr, y: (camera.screenH - gl.y) / dpr };
const cb = inputEventCallbacks(app_.getResource(Input));
+ const frames = 4;
cb.onPointerMove?.(screen.x, screen.y);
+ await app_.stepFrames(1); // hover, and the boundary is ours from here
cb.onPointerDown?.(0, screen.x, screen.y);
+ await app_.stepFrames(1); // the press edge is sampled
cb.onPointerUp?.(0);
- return { entity: target as never as number, name, at: screen, hit: hit as never as number };
+ // Two: the release frame emits `click` (handlers run inside it), the next one
+ // is where what they did — a page switch, a gear, a relayout — has settled.
+ await app_.stepFrames(2);
+ return { entity: target as never as number, name, at: screen, hit: hit as never as number, frames };
}
/** Whether `hit` is `target` or under it — a label on a button is a child, and