Files
adminandClaude Opus 5.5 d7d0a6e7d4 Let Tab reach the game in the Game tab (#261)
The game did get Tab, but the editor moved the keyboard focus to a
button at the same time. The next Space then pressed that button
instead of reaching the game. The key field of On key down could not
capture Tab at all, so an inventory on Tab could not be built.

- FocusManager asks tabBelongsElsewhere before it moves the focus.
  The editor answers yes while the Game tab is in front, so Tab stays
  with the game as long as no field has the focus.
- A widget can claim Tab through takesTab. The key capture button
  does so while it is capturing, so On key down accepts Tab.
- The harness knows the key Tab.

Found on the way:

- The first version of check 20g left the Behavior tab in front, and
  module 21 then failed in the chained run. 20g now goes back.

Tests:

- ./check.sh: 2746 tests, CHECK EXIT 0.
- e2e 20 has two new checks: Tab reaches the game and leaves the
  keyboard with it, the key field captures Tab. Both failed against
  the old bundle.
- GM_STEPS=19,20: all passed, 44 checks. The full local run with all
  three topics of this push passed with 1543 checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 06:01:26 +02:00

169 lines
7.0 KiB
JavaScript

import { edge, node } from '../lib/builders.mjs';
import { LANES, scaffoldBehavior } from './lib/scaffold.mjs';
/**
* The auto-repeat of `On key down`. A plain and a repeating node on the same key are measured in one
* hold, so "it repeated" cannot be confused with "the key arrived more often than expected".
*/
const PLAIN = 'plain hit';
const REPEATING = 'repeating hit';
const TAB = 'tab hit';
const DELAY = 300;
const INTERVAL = 50;
const repeatGraph = {
nodes: [
node('r-plain', 'OnKeyDown', 0, 0, { key: 'ArrowRight' }),
node('r-plain-log', 'Log', 340, 0, { message: PLAIN }),
node('r-repeat', 'OnKeyDown', 0, 220, {
key: 'ArrowRight',
repeatAfter: DELAY,
repeatInterval: INTERVAL,
}),
node('r-repeat-log', 'Log', 340, 220, { message: REPEATING }),
node('r-tab', 'OnKeyDown', 0, 440, { key: 'Tab' }),
node('r-tab-log', 'Log', 340, 440, { message: TAB }),
],
edges: [
edge('r-e1', 'r-plain', 'exec', 'r-plain-log', 'exec'),
edge('r-e2', 'r-repeat', 'exec', 'r-repeat-log', 'exec'),
edge('r-e3', 'r-tab', 'exec', 'r-tab-log', 'exec'),
],
};
export async function keyRepeat(t) {
const { check, sleep, json, shot, click, press, holdKey, world, stop, select, typeText } = t;
/** Both counts out of one read of the world, so the two are always of the same moment. */
const hits = async () => {
const log = (await world()).log;
const count = (message) => log.filter((line) => line.includes(message)).length;
return { plain: count(PLAIN), repeating: count(REPEATING) };
};
// 20a. A box with the two-node behavior below it, written into the model directly: what is under
// test is the repeat, not another way of building a graph.
const rig = await scaffoldBehavior(t, {
assetId: 'repeater',
name: 'Repeater',
graph: repeatGraph,
boxId: 'r-box',
boxName: 'Repeat box',
logicId: 'r-logic',
y: LANES.keyRepeat,
});
await t.play();
// 20b. A hold shorter than the delay is still just a press. Without this the check below would
// also pass on a node that simply fires every frame the key is down.
const beforeShort = await hits();
await holdKey('ArrowRight', 120);
await sleep(200);
const afterShort = await hits();
check(
'a hold shorter than the delay fires once, repeat or not',
afterShort.plain === beforeShort.plain + 1 && afterShort.repeating === beforeShort.repeating + 1,
`plain ${beforeShort.plain} → ${afterShort.plain}, repeating ${beforeShort.repeating} → ${afterShort.repeating} over 120 ms with a ${DELAY} ms delay`,
);
// 20c. Hold it past the delay: the plain node still counts one press, the repeating one runs.
await holdKey('ArrowRight', 800);
await sleep(200);
const afterLong = await hits();
const gained = afterLong.repeating - afterShort.repeating;
check(
'holding past the delay keeps the repeating node firing, while the plain one counts one press',
afterLong.plain === afterShort.plain + 1 && gained > 4 && gained < 25,
`plain +${afterLong.plain - afterShort.plain}, repeating +${gained} over 800 ms (press + ~${Math.round((800 - DELAY) / INTERVAL)} repeats expected)`,
);
await stop();
// 20d. Both settings are on the node itself, so a graph can be tuned where it is built.
await select({ kind: 'node', assetId: 'repeater', nodeId: 'r-repeat' });
const afterField = await json(`__gm.inspectorField('Repeat after (ms)')`);
const intervalField = await json(`__gm.inspectorField('Repeat interval (ms)')`);
check(
'the node inspector offers the delay and the interval',
afterField !== null && intervalField !== null,
JSON.stringify({ afterField, intervalField }),
);
// A switched-off repeat is worth no rows: two lines of "0" on every key node would make each
// one taller for nothing. The repeating node next to it shows both, so this is not just hiding.
const plainRows = await json(`__gm.graph.nodeSettings('r-plain')`);
const repeatRows = await json(`__gm.graph.nodeSettings('r-repeat')`);
check(
'a node with no repeat set carries no repeat rows, the repeating one carries both',
plainRows.length === 1 &&
repeatRows.length === 3 &&
repeatRows.some((r) => r.startsWith('Repeat after')) &&
repeatRows.some((r) => r.startsWith('Repeat interval')),
JSON.stringify({ plainRows, repeatRows }),
);
await shot('40-key-repeat-node');
await click(afterField.x, afterField.y);
await typeText('750');
await click(intervalField.x, intervalField.y);
const params = await json(
`__gm.state.project.assets.find((a) => a.id === 'repeater').graph.nodes.find((n) => n.id === 'r-repeat').params`,
);
check(
'typing a new delay lands on the node',
params.repeatAfter === 750 && params.repeatInterval === INTERVAL,
JSON.stringify(params),
);
// 20e. The typed value is what the next run obeys: 750 ms is longer than this hold, so the
// repeating node falls silent although nothing else about the graph changed.
await t.play();
const beforeTuned = await hits();
await holdKey('ArrowRight', 500);
await sleep(200);
const afterTuned = await hits();
check(
'the delay typed into the inspector is the one the run uses',
afterTuned.repeating === beforeTuned.repeating + 1,
`repeating ${beforeTuned.repeating} → ${afterTuned.repeating} over 500 ms with the delay raised to 750 ms`,
);
await stop();
// 20f. Tab is a game key like any other while the game has the keyboard. The editor used to move
// its focus on it, so the next key went to a widget and no longer reached the game.
await t.play();
const beforeTab = await hits();
const tabsBefore = (await world()).log.filter((line) => line.includes(TAB)).length;
await press('Tab');
await holdKey('ArrowRight', 120);
await sleep(200);
const afterTab = await hits();
const tabsAfter = (await world()).log.filter((line) => line.includes(TAB)).length;
const focusAfterTab = await json('__gm.focusedRect()');
check(
'Tab reaches the game and leaves the keyboard with it',
tabsAfter === tabsBefore + 1 && afterTab.plain === beforeTab.plain + 1 && focusAfterTab === null,
JSON.stringify({ tabs: tabsAfter - tabsBefore, plain: afterTab.plain - beforeTab.plain, focusAfterTab }),
);
await stop();
// 20g. The key field takes Tab too, or a graph could never be set to it by hand.
await select({ kind: 'node', assetId: 'repeater', nodeId: 'r-plain' });
const keyField = await json(`__gm.inspectorField('Key')`);
await click(keyField.x, keyField.y);
await press('Tab');
const pickedKey = await json(
`__gm.state.project.assets.find((a) => a.id === 'repeater').graph.nodes.find((n) => n.id === 'r-plain').params.key`,
);
check('the key field captures Tab', pickedKey === 'Tab', JSON.stringify({ pickedKey }));
// Selecting the node brought its graph to the front; the steps after this one start in the scene.
await t.showTab('scene');
const left = await rig.remove();
check(
'the step leaves nothing of its own behind',
left.objects === 0 && left.assets === 0,
JSON.stringify(left),
);
}