@textadventures/quest-viva-wasmplayer 6.1.1 → 6.2.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/_framework/Common.wasm +0 -0
- package/_framework/Engine.wasm +0 -0
- package/_framework/ExtendedNumerics.BigDecimal.wasm +0 -0
- package/_framework/Legacy.wasm +0 -0
- package/_framework/Microsoft.Extensions.Logging.Abstractions.wasm +0 -0
- package/_framework/NCalc.Core.wasm +0 -0
- package/_framework/NCalc.Domain.wasm +0 -0
- package/_framework/NCalc.Parser.wasm +0 -0
- package/_framework/Parlot.wasm +0 -0
- package/_framework/PlayerCore.wasm +0 -0
- package/_framework/System.Collections.Immutable.wasm +0 -0
- package/_framework/System.Linq.wasm +0 -0
- package/_framework/System.Net.Http.wasm +0 -0
- package/_framework/System.Private.CoreLib.wasm +0 -0
- package/_framework/System.Private.Xml.Linq.wasm +0 -0
- package/_framework/System.Private.Xml.wasm +0 -0
- package/_framework/System.Runtime.Numerics.wasm +0 -0
- package/_framework/System.Text.RegularExpressions.wasm +0 -0
- package/_framework/WasmPlayer.wasm +0 -0
- package/_framework/dotnet.js +36 -42
- package/_framework/dotnet.native.wasm +0 -0
- package/chrome.css +1 -1
- package/export-manifest.json +1 -1
- package/game-saver.js +5 -1
- package/gamepad.js +668 -0
- package/index.html +53 -11
- package/legacy/adapter.js +14 -0
- package/legacy/grid.js +4 -0
- package/legacy/player.js +14 -0
- package/legacy/playercore.js +8 -1
- package/legacy/questui.js +5 -1
- package/package.json +1 -1
- package/v2/map.js +1 -1
- package/v2/player.htm +2 -0
- package/v2/v2.css +1 -1
- package/v2/v2.js +1 -1
- package/wasm-player.js +135 -13
- package/_framework/System.Linq.Expressions.wasm +0 -0
package/gamepad.js
ADDED
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
// Playing with a game controller (issue #3044), through the browser's Gamepad API. It belongs to
|
|
2
|
+
// the host, not to either player UI, so it works the same on legacy and v2, and in games that
|
|
3
|
+
// don't know about it. The controller mostly acts as a keyboard would: it moves focus between the
|
|
4
|
+
// choices on screen and uses the focused one, which screen readers already announce, and it sends
|
|
5
|
+
// commands through the UI's own sendCommand, which already ignores them while a turn is running.
|
|
6
|
+
//
|
|
7
|
+
// The standard layout (https://w3c.github.io/gamepad/#remapping):
|
|
8
|
+
//
|
|
9
|
+
// D-pad or left stick north / south / west / east, in the game's own words; two arrows
|
|
10
|
+
// together on the d-pad, or the stick pushed to a corner, the diagonals
|
|
11
|
+
// LT or RT held + d-pad up / down / out / in
|
|
12
|
+
// LB / RB previous / next choice (links, buttons, menu items)
|
|
13
|
+
// A use the focused choice, as Enter does
|
|
14
|
+
// B close a menu or popup, or cancel a menu that can be cancelled
|
|
15
|
+
// X / Y look / inventory
|
|
16
|
+
// LT or RT held + X wait
|
|
17
|
+
// LT or RT held + Y read the last output again (as does Back)
|
|
18
|
+
// Start open the controller menu: saved games, and a guide to the buttons
|
|
19
|
+
//
|
|
20
|
+
// While a menu, dialog or popup is open the d-pad moves between its choices instead, and while
|
|
21
|
+
// the game waits for a key press, any button continues. The guide names the buttons as the
|
|
22
|
+
// connected controller does (✕ and Options on a PlayStation controller, A and Menu on an Xbox one).
|
|
23
|
+
//
|
|
24
|
+
// wasm-player.js tells it what the game is doing (QuestGamepad.beginWait and so on) and gives it
|
|
25
|
+
// the host calls it needs (attach). A controller only reaches the page once a button has been
|
|
26
|
+
// pressed while the page has focus, which is when gamepadconnected fires.
|
|
27
|
+
|
|
28
|
+
(function () {
|
|
29
|
+
'use strict';
|
|
30
|
+
|
|
31
|
+
const BUTTONS = {
|
|
32
|
+
a: 0, b: 1, x: 2, y: 3, lb: 4, rb: 5, lt: 6, rt: 7,
|
|
33
|
+
back: 8, start: 9,
|
|
34
|
+
};
|
|
35
|
+
const DPAD = { up: 12, down: 13, left: 14, right: 15 };
|
|
36
|
+
// The stick counts as pushed past STICK_THRESHOLD, and as let go below STICK_RELEASE, so it
|
|
37
|
+
// doesn't flicker on and off at the edge.
|
|
38
|
+
const STICK_THRESHOLD = 0.6;
|
|
39
|
+
const STICK_RELEASE = 0.4;
|
|
40
|
+
// The stick has to stay in one of its eight directions this long before it moves, so pushing
|
|
41
|
+
// it to a corner doesn't go north on the way to northeast.
|
|
42
|
+
const STICK_SETTLE = 60;
|
|
43
|
+
// A thumb rolling onto a d-pad corner lands on one arrow a moment before the other. Where a
|
|
44
|
+
// diagonal exit could be what's meant, the first arrow waits this long for the second.
|
|
45
|
+
const DIAGONAL_WAIT = 80;
|
|
46
|
+
const TRIGGER_THRESHOLD = 0.5;
|
|
47
|
+
// LB and RB repeat while held, after a pause, so a long list can be walked through.
|
|
48
|
+
const REPEAT_DELAY = 450;
|
|
49
|
+
const REPEAT_INTERVAL = 180;
|
|
50
|
+
const REPEATING = new Set(['lb', 'rb']);
|
|
51
|
+
|
|
52
|
+
// Core's default compass words, in its order; the game replaces them with its own
|
|
53
|
+
// (setCompassDirections) when it starts.
|
|
54
|
+
const DEFAULT_DIRECTIONS = ['northwest', 'north', 'northeast', 'west', 'east', 'southwest', 'south',
|
|
55
|
+
'southeast', 'up', 'down', 'in', 'out'];
|
|
56
|
+
const DIRECTION_INDEX = {
|
|
57
|
+
northwest: 0, north: 1, northeast: 2, west: 3, east: 4, southwest: 5, south: 6, southeast: 7,
|
|
58
|
+
up: 8, down: 9, in: 10, out: 11,
|
|
59
|
+
};
|
|
60
|
+
// The stick's eight directions, anticlockwise from east, as Math.atan2 measures.
|
|
61
|
+
const STICK_POINTS = ['east', 'northeast', 'north', 'northwest', 'west', 'southwest', 'south', 'southeast'];
|
|
62
|
+
const DIAGONALS_BESIDE = {
|
|
63
|
+
north: ['northeast', 'northwest'], east: ['northeast', 'southeast'],
|
|
64
|
+
south: ['southeast', 'southwest'], west: ['northwest', 'southwest'],
|
|
65
|
+
};
|
|
66
|
+
// With a trigger held, the d-pad's arrows go up, down, out and in.
|
|
67
|
+
const WITH_TRIGGER = { north: 'up', south: 'down', west: 'out', east: 'in' };
|
|
68
|
+
const DEFAULT_COMMANDS = { look: 'look', inventory: 'inventory', wait: 'wait' };
|
|
69
|
+
|
|
70
|
+
let host = null;
|
|
71
|
+
let directions = DEFAULT_DIRECTIONS;
|
|
72
|
+
// The directions the player can go in now, from updateCompass; null until the game says, and
|
|
73
|
+
// then nothing is held back.
|
|
74
|
+
let availableDirections = null;
|
|
75
|
+
let waiting = false;
|
|
76
|
+
let turnStart = null;
|
|
77
|
+
const pads = new Map();
|
|
78
|
+
let polling = false;
|
|
79
|
+
let announcer = null;
|
|
80
|
+
// The kind of controller last used, for naming its buttons.
|
|
81
|
+
let kind = 'xbox';
|
|
82
|
+
let toastTimer;
|
|
83
|
+
|
|
84
|
+
// ── What the host tells it ──────────────────────────────────────────────────────────────────
|
|
85
|
+
|
|
86
|
+
const QuestGamepad = {
|
|
87
|
+
/**
|
|
88
|
+
* host: {
|
|
89
|
+
* sendCommand(text) sends a command as if the player had typed it
|
|
90
|
+
* commandWord(name) the game's word for "look", "inventory" or "wait", or ""
|
|
91
|
+
* outputElement() the element holding the game's output, or null
|
|
92
|
+
* savedGames() 'off' where there's no saving, 'busy' mid-turn, else 'ready'
|
|
93
|
+
* openSavedGames() opens the saved games dialog
|
|
94
|
+
* chromeString(key, text) a player string in the game's language
|
|
95
|
+
* }
|
|
96
|
+
*/
|
|
97
|
+
attach(newHost) {
|
|
98
|
+
host = newHost;
|
|
99
|
+
window.addEventListener('gamepadconnected', onConnected);
|
|
100
|
+
window.addEventListener('gamepaddisconnected', onDisconnected);
|
|
101
|
+
// One that was connected before this page loaded, and has been used on it already.
|
|
102
|
+
for (const pad of getPads()) addPad(pad, false);
|
|
103
|
+
startPolling();
|
|
104
|
+
},
|
|
105
|
+
reset() {
|
|
106
|
+
directions = DEFAULT_DIRECTIONS;
|
|
107
|
+
availableDirections = null;
|
|
108
|
+
waiting = false;
|
|
109
|
+
turnStart = null;
|
|
110
|
+
},
|
|
111
|
+
setCompassDirections(dirs) {
|
|
112
|
+
if (Array.isArray(dirs) && dirs.length >= DEFAULT_DIRECTIONS.length) directions = dirs;
|
|
113
|
+
},
|
|
114
|
+
updateCompass(data) {
|
|
115
|
+
availableDirections = typeof data === 'string' ? data.split('/').filter(Boolean) : null;
|
|
116
|
+
},
|
|
117
|
+
beginWait() {
|
|
118
|
+
waiting = true;
|
|
119
|
+
},
|
|
120
|
+
endWait() {
|
|
121
|
+
waiting = false;
|
|
122
|
+
},
|
|
123
|
+
/** The player has started a turn: what the game shows from now on is that turn's output. */
|
|
124
|
+
markTurnStart() {
|
|
125
|
+
const output = host?.outputElement();
|
|
126
|
+
turnStart = output ? { output, length: output.textContent.length } : null;
|
|
127
|
+
},
|
|
128
|
+
/** The text of the last turn's output, for "read it again". */
|
|
129
|
+
lastOutputText,
|
|
130
|
+
};
|
|
131
|
+
window.QuestGamepad = QuestGamepad;
|
|
132
|
+
|
|
133
|
+
// ── Controllers coming and going ────────────────────────────────────────────────────────────
|
|
134
|
+
|
|
135
|
+
function getPads() {
|
|
136
|
+
try {
|
|
137
|
+
return [...(navigator.getGamepads?.() ?? [])].filter(Boolean);
|
|
138
|
+
} catch {
|
|
139
|
+
// Blocked by the page's permissions policy (an iframe without allow="gamepad").
|
|
140
|
+
return [];
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function addPad(pad, announceIt) {
|
|
145
|
+
if (pads.has(pad.index)) return;
|
|
146
|
+
// What's already held down when it connects doesn't count as a press.
|
|
147
|
+
const stickPoint = readStick(pad, null);
|
|
148
|
+
pads.set(pad.index, {
|
|
149
|
+
pressed: readInputs(pad),
|
|
150
|
+
heldSince: new Map(),
|
|
151
|
+
dpad: { pending: null, done: !!readDpad(pad) },
|
|
152
|
+
stick: { point: stickPoint, since: 0, done: !!stickPoint },
|
|
153
|
+
});
|
|
154
|
+
if (pad.mapping === 'standard') kind = controllerKind(pad);
|
|
155
|
+
if (announceIt) {
|
|
156
|
+
const text = pad.mapping === 'standard'
|
|
157
|
+
? string('ControllerConnected', 'Controller connected. Press [button] for the controller menu.')
|
|
158
|
+
.replace('[button]', buttonName('start'))
|
|
159
|
+
: string('ControllerNotRecognised', "Controller connected, but its buttons aren't recognised.");
|
|
160
|
+
announce(text);
|
|
161
|
+
showToast(text);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function onConnected(e) {
|
|
166
|
+
const pad = e.gamepad ?? getPads().find(p => !pads.has(p.index));
|
|
167
|
+
if (pad) addPad(pad, true);
|
|
168
|
+
startPolling();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function onDisconnected(e) {
|
|
172
|
+
const index = e.gamepad?.index;
|
|
173
|
+
if (index === undefined || !pads.delete(index)) return;
|
|
174
|
+
announce(string('ControllerDisconnected', 'Controller disconnected.'));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function startPolling() {
|
|
178
|
+
if (polling || pads.size === 0) return;
|
|
179
|
+
polling = true;
|
|
180
|
+
requestAnimationFrame(poll);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Each frame, while a controller is connected. requestAnimationFrame stops in a background
|
|
184
|
+
// tab, which is right: a controller shouldn't play a game the player isn't looking at.
|
|
185
|
+
function poll(now) {
|
|
186
|
+
if (pads.size === 0) {
|
|
187
|
+
polling = false;
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const current = new Map(getPads().map(p => [p.index, p]));
|
|
191
|
+
for (const [index, padState] of pads) {
|
|
192
|
+
const pad = current.get(index);
|
|
193
|
+
if (pad && pad.mapping === 'standard') update(pad, padState, now);
|
|
194
|
+
}
|
|
195
|
+
requestAnimationFrame(poll);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ── Reading buttons ─────────────────────────────────────────────────────────────────────────
|
|
199
|
+
|
|
200
|
+
function readInputs(pad) {
|
|
201
|
+
const held = new Set();
|
|
202
|
+
const button = (i, threshold = 0.5) => {
|
|
203
|
+
const b = pad.buttons[i];
|
|
204
|
+
return !!b && (b.pressed || b.value > threshold);
|
|
205
|
+
};
|
|
206
|
+
for (const [name, i] of Object.entries(BUTTONS)) {
|
|
207
|
+
if (button(i, name === 'lt' || name === 'rt' ? TRIGGER_THRESHOLD : 0.5)) held.add(name);
|
|
208
|
+
}
|
|
209
|
+
return held;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const isHeld = (pad, i) => !!pad.buttons[i] && (pad.buttons[i].pressed || pad.buttons[i].value > 0.5);
|
|
213
|
+
|
|
214
|
+
// The compass point the d-pad's arrows make together, or null. Opposite arrows cancel out.
|
|
215
|
+
function readDpad(pad) {
|
|
216
|
+
const dx = isHeld(pad, DPAD.right) - isHeld(pad, DPAD.left);
|
|
217
|
+
const dy = isHeld(pad, DPAD.up) - isHeld(pad, DPAD.down);
|
|
218
|
+
if (!dx && !dy) return null;
|
|
219
|
+
return STICK_POINTS[(Math.round(Math.atan2(dy, dx) / (Math.PI / 4)) + 8) % 8];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// The compass point the left stick is pushed towards, or null; `previous` is the one it was
|
|
223
|
+
// pushed towards last time, which it keeps until it's let go further.
|
|
224
|
+
function readStick(pad, previous) {
|
|
225
|
+
const [x = 0, y = 0] = pad.axes;
|
|
226
|
+
if (Math.hypot(x, y) < (previous ? STICK_RELEASE : STICK_THRESHOLD)) return null;
|
|
227
|
+
// The stick's y is positive downwards.
|
|
228
|
+
return STICK_POINTS[(Math.round(Math.atan2(-y, x) / (Math.PI / 4)) + 8) % 8];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Whether the arrow just pressed could be the first half of a diagonal the player can take,
|
|
232
|
+
// so is worth waiting for the second. Not in a menu, while waiting for a key, or with a
|
|
233
|
+
// trigger held, where diagonals mean nothing.
|
|
234
|
+
function mightBeDiagonal(point, modifier) {
|
|
235
|
+
if (modifier || waiting || openScope() || !DIAGONALS_BESIDE[point]) return false;
|
|
236
|
+
if (!availableDirections) return true;
|
|
237
|
+
return DIAGONALS_BESIDE[point].some(p => availableDirections.includes(directions[DIRECTION_INDEX[p]]));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function updateDirections(pad, padState, now, modifier) {
|
|
241
|
+
// The d-pad: one move per press, waiting briefly for a second arrow where that matters.
|
|
242
|
+
const dpad = padState.dpad;
|
|
243
|
+
const point = readDpad(pad);
|
|
244
|
+
if (!point) {
|
|
245
|
+
// Let go before the wait was up: a quick tap of one arrow.
|
|
246
|
+
if (dpad.pending) direction(dpad.pending.point, dpad.pending.modifier);
|
|
247
|
+
dpad.pending = null;
|
|
248
|
+
dpad.done = false;
|
|
249
|
+
} else if (!dpad.done) {
|
|
250
|
+
if (dpad.pending) {
|
|
251
|
+
if (!DIAGONALS_BESIDE[point] || now >= dpad.pending.deadline) {
|
|
252
|
+
direction(DIAGONALS_BESIDE[point] ? dpad.pending.point : point, dpad.pending.modifier);
|
|
253
|
+
dpad.pending = null;
|
|
254
|
+
dpad.done = true;
|
|
255
|
+
}
|
|
256
|
+
} else if (mightBeDiagonal(point, modifier)) {
|
|
257
|
+
dpad.pending = { point, modifier, deadline: now + DIAGONAL_WAIT };
|
|
258
|
+
} else {
|
|
259
|
+
direction(point, modifier);
|
|
260
|
+
dpad.done = true;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// The left stick: one move per push, once it has settled on a direction.
|
|
265
|
+
const stick = padState.stick;
|
|
266
|
+
const stickPoint = readStick(pad, stick.point);
|
|
267
|
+
if (stickPoint !== stick.point) {
|
|
268
|
+
stick.point = stickPoint;
|
|
269
|
+
stick.since = now;
|
|
270
|
+
if (!stickPoint) stick.done = false;
|
|
271
|
+
} else if (stickPoint && !stick.done && now - stick.since >= STICK_SETTLE) {
|
|
272
|
+
direction(stickPoint, modifier);
|
|
273
|
+
stick.done = true;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function update(pad, padState, now) {
|
|
278
|
+
const held = readInputs(pad);
|
|
279
|
+
if (held.size > 0 && !padState.pressed.size) kind = controllerKind(pad);
|
|
280
|
+
const modifier = held.has('lt') || held.has('rt');
|
|
281
|
+
for (const name of held) {
|
|
282
|
+
if (name === 'lt' || name === 'rt') continue;
|
|
283
|
+
if (!padState.pressed.has(name)) {
|
|
284
|
+
padState.heldSince.set(name, now);
|
|
285
|
+
press(name, modifier);
|
|
286
|
+
} else if (REPEATING.has(name)) {
|
|
287
|
+
const since = padState.heldSince.get(name) ?? now;
|
|
288
|
+
if (now - since >= REPEAT_DELAY) {
|
|
289
|
+
padState.heldSince.set(name, now - REPEAT_DELAY + REPEAT_INTERVAL);
|
|
290
|
+
press(name, modifier);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
for (const name of padState.pressed) {
|
|
295
|
+
if (!held.has(name)) padState.heldSince.delete(name);
|
|
296
|
+
}
|
|
297
|
+
padState.pressed = held;
|
|
298
|
+
updateDirections(pad, padState, now, modifier);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// ── What each button does ───────────────────────────────────────────────────────────────────
|
|
302
|
+
|
|
303
|
+
function press(name, modifier) {
|
|
304
|
+
// Start opens and closes the controller menu, whatever else is going on.
|
|
305
|
+
if (name === 'start') {
|
|
306
|
+
toggleMenu();
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const scope = openScope();
|
|
310
|
+
if (waiting && !scope) {
|
|
311
|
+
continueWait();
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
// In a menu, dialog or popup, the buttons that send commands do nothing: the game isn't
|
|
315
|
+
// what the player is looking at.
|
|
316
|
+
if (scope && (name === 'x' || name === 'y')) return;
|
|
317
|
+
switch (name) {
|
|
318
|
+
case 'lb': return moveFocus(-1);
|
|
319
|
+
case 'rb': return moveFocus(1);
|
|
320
|
+
case 'a': return activate();
|
|
321
|
+
case 'b': return cancel();
|
|
322
|
+
case 'x': return command(modifier ? 'wait' : 'look');
|
|
323
|
+
case 'y': return modifier ? readAgain() : command('inventory');
|
|
324
|
+
case 'back': return readAgain();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// A compass point from the d-pad or the stick.
|
|
329
|
+
function direction(point, modifier) {
|
|
330
|
+
const scope = openScope();
|
|
331
|
+
if (waiting && !scope) {
|
|
332
|
+
continueWait();
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
// In a menu, dialog or popup, up and left go to the previous choice, down and right the next.
|
|
336
|
+
if (scope) {
|
|
337
|
+
if (point === 'north' || point === 'west') moveFocus(-1);
|
|
338
|
+
else if (point === 'south' || point === 'east') moveFocus(1);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (modifier) {
|
|
342
|
+
if (WITH_TRIGGER[point]) go(WITH_TRIGGER[point]);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
go(point);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function go(point) {
|
|
349
|
+
const word = directions[DIRECTION_INDEX[point]];
|
|
350
|
+
if (!word) return;
|
|
351
|
+
// Where the compass button would be disabled, say so rather than taking a turn over it.
|
|
352
|
+
// Not in the transcript: it's said, and shown for a moment.
|
|
353
|
+
if (availableDirections && !availableDirections.includes(word)) {
|
|
354
|
+
const text = string('ControllerNoExit', "You can't go [direction].").replace('[direction]', word);
|
|
355
|
+
announce(text);
|
|
356
|
+
showToast(text, 2500);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
host?.sendCommand(word);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function command(name) {
|
|
363
|
+
host?.sendCommand(host.commandWord(name) || DEFAULT_COMMANDS[name]);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// As any key does: both UIs listen for a key press on the page to end a wait.
|
|
367
|
+
function continueWait() {
|
|
368
|
+
document.body.dispatchEvent(new KeyboardEvent('keydown', {
|
|
369
|
+
key: 'Enter', code: 'Enter', bubbles: true, cancelable: true,
|
|
370
|
+
}));
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function readAgain() {
|
|
374
|
+
const text = lastOutputText();
|
|
375
|
+
if (text) announce(text);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function lastOutputText() {
|
|
379
|
+
const output = host?.outputElement();
|
|
380
|
+
if (!output) return '';
|
|
381
|
+
const text = output.textContent ?? '';
|
|
382
|
+
// A cleared screen, or a different game since: all of what's there now is the last output.
|
|
383
|
+
const sameOutput = turnStart && turnStart.output === output && turnStart.length <= text.length;
|
|
384
|
+
return (sameOutput ? text.slice(turnStart.length) : text).replace(/\s+/g, ' ').trim();
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// ── Choices and focus ───────────────────────────────────────────────────────────────────────
|
|
388
|
+
|
|
389
|
+
const CHOICE_SELECTOR = 'a[href], a.cmdlink, button, select, summary, [tabindex], [role="menuitem"], '
|
|
390
|
+
+ '[role="option"], input[type="checkbox"], input[type="radio"], input[type="button"], '
|
|
391
|
+
+ 'input[type="submit"]';
|
|
392
|
+
|
|
393
|
+
function isVisible(el) {
|
|
394
|
+
return el.getClientRects().length > 0 && getComputedStyle(el).visibility !== 'hidden';
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function isChoice(el) {
|
|
398
|
+
if (el.matches('input:not([type="checkbox"]):not([type="radio"]):not([type="button"]):not([type="submit"]), '
|
|
399
|
+
+ 'textarea, [contenteditable=""], [contenteditable="true"]')) {
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
if (el.disabled || el.getAttribute('aria-disabled') === 'true' || el.classList.contains('disabled')) return false;
|
|
403
|
+
// A roving item (a menu item or list option) is reachable even with tabindex -1, as the
|
|
404
|
+
// arrow keys reach it; anything else with tabindex -1 has been taken out of Tab's order.
|
|
405
|
+
if (el.tabIndex < 0 && el.hasAttribute('tabindex') && !el.matches('[role="menuitem"], [role="option"]')) {
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
if (el.closest('[inert], [aria-hidden="true"]')) return false;
|
|
409
|
+
return isVisible(el);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function lastOf(selector, test = isVisible) {
|
|
413
|
+
let nodes;
|
|
414
|
+
try {
|
|
415
|
+
nodes = [...document.querySelectorAll(selector)];
|
|
416
|
+
} catch {
|
|
417
|
+
return null; // :popover-open in a browser without popovers
|
|
418
|
+
}
|
|
419
|
+
return nodes.reverse().find(test) ?? null;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// What's open on top of the game, whose choices the controller moves between: a menu or
|
|
423
|
+
// popup first, then a modal dialog (v2's menus, the host's own), then legacy's jQuery UI
|
|
424
|
+
// dialog. Null when nothing is.
|
|
425
|
+
function openScope() {
|
|
426
|
+
return lastOf('div[id^="jjmenu"]')
|
|
427
|
+
?? lastOf(':popover-open')
|
|
428
|
+
?? lastOf('dialog[open]', (d) => d.matches(':modal'))
|
|
429
|
+
?? lastOf('.ui-dialog');
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function moveFocus(delta) {
|
|
433
|
+
const scope = openScope() ?? document.body;
|
|
434
|
+
const active = document.activeElement;
|
|
435
|
+
|
|
436
|
+
// Legacy's menu dialog is a list box: move through its options first.
|
|
437
|
+
if (active instanceof HTMLSelectElement && scope.contains(active)) {
|
|
438
|
+
const next = active.selectedIndex + delta;
|
|
439
|
+
if (next >= 0 && next < active.options.length) {
|
|
440
|
+
active.selectedIndex = next;
|
|
441
|
+
active.dispatchEvent(new Event('change', { bubbles: true }));
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const choices = [...scope.querySelectorAll(CHOICE_SELECTOR)].filter(isChoice);
|
|
447
|
+
if (choices.length === 0) return;
|
|
448
|
+
let target;
|
|
449
|
+
const index = choices.indexOf(active);
|
|
450
|
+
if (index >= 0) {
|
|
451
|
+
target = choices[index + delta];
|
|
452
|
+
} else if (active && active !== document.body && scope.contains(active)) {
|
|
453
|
+
// From the command box (or anything else that isn't a choice), the nearest choice
|
|
454
|
+
// that way, as Tab and Shift+Tab would: back from the command box is the newest output.
|
|
455
|
+
target = delta > 0
|
|
456
|
+
? choices.find(el => active.compareDocumentPosition(el) & Node.DOCUMENT_POSITION_FOLLOWING)
|
|
457
|
+
: choices.findLast(el => active.compareDocumentPosition(el) & Node.DOCUMENT_POSITION_PRECEDING);
|
|
458
|
+
} else {
|
|
459
|
+
target = delta > 0 ? choices[0] : choices[choices.length - 1];
|
|
460
|
+
}
|
|
461
|
+
if (!target) return;
|
|
462
|
+
target.focus();
|
|
463
|
+
// An old game's link that was never made focusable.
|
|
464
|
+
if (document.activeElement !== target) {
|
|
465
|
+
target.setAttribute('tabindex', '0');
|
|
466
|
+
target.focus();
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function activate() {
|
|
471
|
+
const el = document.activeElement;
|
|
472
|
+
if (!el || el === document.body || !isChoice(el)) return;
|
|
473
|
+
if (el instanceof HTMLSelectElement) {
|
|
474
|
+
// Legacy's menu dialog: its first button chooses the selected option.
|
|
475
|
+
el.closest('.ui-dialog')?.querySelector('.ui-dialog-buttonpane button')?.click();
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
if (el.matches('[role="option"]')) {
|
|
479
|
+
// A list option's Enter does more than a click (legacy's lists move on to the verbs).
|
|
480
|
+
el.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', bubbles: true, cancelable: true }));
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
el.click();
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function cancel() {
|
|
487
|
+
const active = document.activeElement;
|
|
488
|
+
const escape = new KeyboardEvent('keydown', { key: 'Escape', code: 'Escape', bubbles: true, cancelable: true });
|
|
489
|
+
(active && active !== document.body ? active : document.body).dispatchEvent(escape);
|
|
490
|
+
// A menu that handles Escape itself (legacy's verb menus, v2's).
|
|
491
|
+
if (escape.defaultPrevented) return;
|
|
492
|
+
|
|
493
|
+
const popover = lastOf(':popover-open');
|
|
494
|
+
if (popover) {
|
|
495
|
+
popover.hidePopover();
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
// What Escape does to a modal dialog, which a made-up key press doesn't: a cancel event,
|
|
499
|
+
// which a dialog that can't be cancelled refuses.
|
|
500
|
+
const dialog = lastOf('dialog[open]', (d) => d.matches(':modal'));
|
|
501
|
+
if (dialog) {
|
|
502
|
+
const cancelEvent = new Event('cancel', { cancelable: true });
|
|
503
|
+
dialog.dispatchEvent(cancelEvent);
|
|
504
|
+
if (!cancelEvent.defaultPrevented) dialog.close();
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
// Legacy's jQuery UI dialogs: only one that can be cancelled shows its close button.
|
|
508
|
+
const close = lastOf('.ui-dialog')?.querySelector('.ui-dialog-titlebar-close');
|
|
509
|
+
if (close && isVisible(close)) close.click();
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// ── The controller menu and its guide ──────────────────────────────────────────────────────
|
|
513
|
+
|
|
514
|
+
function controllerKind(pad) {
|
|
515
|
+
const id = pad.id ?? '';
|
|
516
|
+
if (/054c|playstation|dualshock|dualsense|wireless controller/i.test(id)) {
|
|
517
|
+
return /0ce6|0df2|dualsense/i.test(id) ? 'ps5' : 'ps4';
|
|
518
|
+
}
|
|
519
|
+
if (/057e|nintendo|pro controller|joy-con/i.test(id)) return 'nintendo';
|
|
520
|
+
return 'xbox';
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// Each kind's names for the buttons of the standard layout, which is by position: the bottom
|
|
524
|
+
// face button is A on an Xbox controller, ✕ on a PlayStation one and B on a Nintendo one. A
|
|
525
|
+
// shape is [symbol, string key, English name]: the symbol is shown, its name read out.
|
|
526
|
+
const SHAPES = {
|
|
527
|
+
a: ['✕', 'ControllerButtonCross', 'Cross'],
|
|
528
|
+
b: ['○', 'ControllerButtonCircle', 'Circle'],
|
|
529
|
+
x: ['□', 'ControllerButtonSquare', 'Square'],
|
|
530
|
+
y: ['△', 'ControllerButtonTriangle', 'Triangle'],
|
|
531
|
+
};
|
|
532
|
+
const BUTTON_NAMES = {
|
|
533
|
+
xbox: { a: 'A', b: 'B', x: 'X', y: 'Y', lb: 'LB', rb: 'RB', triggers: 'LT/RT', back: 'View', start: 'Menu' },
|
|
534
|
+
ps4: { ...SHAPES, lb: 'L1', rb: 'R1', triggers: 'L2/R2', back: 'Share', start: 'Options' },
|
|
535
|
+
ps5: { ...SHAPES, lb: 'L1', rb: 'R1', triggers: 'L2/R2', back: 'Create', start: 'Options' },
|
|
536
|
+
nintendo: { a: 'B', b: 'A', x: 'Y', y: 'X', lb: 'L', rb: 'R', triggers: 'ZL/ZR', back: '−', start: '+' },
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
function buttonName(button) {
|
|
540
|
+
const name = BUTTON_NAMES[kind][button];
|
|
541
|
+
return Array.isArray(name) ? `${name[0]} ${string(name[1], name[2])}` : name;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function buttonElement(button) {
|
|
545
|
+
if (button === 'dpad') return document.createTextNode(string('ControllerDpad', 'D-pad'));
|
|
546
|
+
if (button === 'dpadcorner') return document.createTextNode(string('ControllerDpadCorner', 'Two d-pad arrows'));
|
|
547
|
+
const name = BUTTON_NAMES[kind][button];
|
|
548
|
+
if (!Array.isArray(name)) return document.createTextNode(name);
|
|
549
|
+
const span = document.createElement('span');
|
|
550
|
+
const symbol = document.createElement('span');
|
|
551
|
+
symbol.setAttribute('aria-hidden', 'true');
|
|
552
|
+
symbol.textContent = name[0] + ' ';
|
|
553
|
+
span.append(symbol, string(name[1], name[2]));
|
|
554
|
+
return span;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// The guide's rows: the buttons (joined by + when held together, / for either), and what they do.
|
|
558
|
+
const HELP_ROWS = [
|
|
559
|
+
[['dpad'], 'ControllerHelpMove', 'Go north, south, west or east'],
|
|
560
|
+
[['dpadcorner'], 'ControllerHelpMoveDiagonal', 'Go northeast, northwest, southeast or southwest'],
|
|
561
|
+
[['triggers', '+', 'dpad'], 'ControllerHelpMoveVertical', 'Go up, down, out or in'],
|
|
562
|
+
[['lb', '/', 'rb'], 'ControllerHelpChoices', 'Previous or next link or button'],
|
|
563
|
+
[['a'], 'ControllerHelpUse', "Use the link or button you're on"],
|
|
564
|
+
[['b'], 'ControllerHelpCancel', 'Close a menu, or cancel'],
|
|
565
|
+
[['x'], 'ControllerHelpLook', 'Look'],
|
|
566
|
+
[['y'], 'ControllerHelpInventory', 'Inventory'],
|
|
567
|
+
[['triggers', '+', 'x'], 'ControllerHelpWait', 'Wait'],
|
|
568
|
+
[['back'], 'ControllerHelpReadAgain', 'Read the last text again'],
|
|
569
|
+
[['start'], 'ControllerHelpMenu', 'Open the controller menu'],
|
|
570
|
+
];
|
|
571
|
+
|
|
572
|
+
function renderHelp() {
|
|
573
|
+
const list = document.getElementById('qv-controller-help-list');
|
|
574
|
+
if (!list) return;
|
|
575
|
+
list.replaceChildren(...HELP_ROWS.map(([buttons, key, fallback]) => {
|
|
576
|
+
const row = document.createElement('li');
|
|
577
|
+
row.tabIndex = 0;
|
|
578
|
+
const names = document.createElement('span');
|
|
579
|
+
names.className = 'qv-controller-button';
|
|
580
|
+
for (const part of buttons) {
|
|
581
|
+
names.append(part === '+' || part === '/' ? ` ${part} ` : buttonElement(part));
|
|
582
|
+
}
|
|
583
|
+
const action = document.createElement('span');
|
|
584
|
+
action.textContent = string(key, fallback);
|
|
585
|
+
row.append(names, action);
|
|
586
|
+
return row;
|
|
587
|
+
}));
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
let dialogsWired = false;
|
|
591
|
+
|
|
592
|
+
function wireDialogs() {
|
|
593
|
+
if (dialogsWired) return;
|
|
594
|
+
dialogsWired = true;
|
|
595
|
+
for (const close of document.querySelectorAll('.qv-controller-close')) {
|
|
596
|
+
close.addEventListener('click', () => close.closest('dialog')?.close());
|
|
597
|
+
}
|
|
598
|
+
document.getElementById('qv-controller-menu-saves')?.addEventListener('click', () => {
|
|
599
|
+
document.getElementById('qv-controller-menu')?.close();
|
|
600
|
+
if (host?.savedGames() === 'ready') host.openSavedGames();
|
|
601
|
+
});
|
|
602
|
+
document.getElementById('qv-controller-menu-help')?.addEventListener('click', openHelp);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
function openHelp() {
|
|
606
|
+
const help = document.getElementById('qv-controller-help');
|
|
607
|
+
if (!help) return;
|
|
608
|
+
wireDialogs();
|
|
609
|
+
renderHelp();
|
|
610
|
+
if (!help.open) help.showModal();
|
|
611
|
+
help.querySelector('#qv-controller-help-list li')?.focus();
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function toggleMenu() {
|
|
615
|
+
const menu = document.getElementById('qv-controller-menu');
|
|
616
|
+
const help = document.getElementById('qv-controller-help');
|
|
617
|
+
if (!menu) return;
|
|
618
|
+
if (menu.open || help?.open) {
|
|
619
|
+
help?.close();
|
|
620
|
+
menu.close();
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
// Not over another dialog or menu, such as the saved games dialog: B closes that first.
|
|
624
|
+
if (openScope()) return;
|
|
625
|
+
wireDialogs();
|
|
626
|
+
const saves = document.getElementById('qv-controller-menu-saves');
|
|
627
|
+
if (saves) {
|
|
628
|
+
const state = host?.savedGames() ?? 'off';
|
|
629
|
+
saves.hidden = state === 'off';
|
|
630
|
+
saves.disabled = state !== 'ready';
|
|
631
|
+
}
|
|
632
|
+
menu.showModal();
|
|
633
|
+
menu.querySelector('.space-y-2 button:not(:disabled)')?.focus();
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function showToast(text, duration = 6000) {
|
|
637
|
+
const toast = document.getElementById('qv-controller-toast');
|
|
638
|
+
const card = toast?.firstElementChild;
|
|
639
|
+
if (!toast || !card) return;
|
|
640
|
+
card.textContent = text;
|
|
641
|
+
toast.classList.remove('hidden');
|
|
642
|
+
clearTimeout(toastTimer);
|
|
643
|
+
toastTimer = setTimeout(() => toast.classList.add('hidden'), duration);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// ── Saying things ───────────────────────────────────────────────────────────────────────────
|
|
647
|
+
|
|
648
|
+
function string(key, fallback) {
|
|
649
|
+
return host?.chromeString(key, fallback) ?? fallback;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function announce(text) {
|
|
653
|
+
if (!announcer) {
|
|
654
|
+
announcer = document.createElement('div');
|
|
655
|
+
announcer.id = 'qv-gamepad-announce';
|
|
656
|
+
announcer.setAttribute('role', 'status');
|
|
657
|
+
announcer.setAttribute('aria-live', 'polite');
|
|
658
|
+
Object.assign(announcer.style, {
|
|
659
|
+
position: 'absolute', width: '1px', height: '1px', overflow: 'hidden',
|
|
660
|
+
clip: 'rect(0 0 0 0)', clipPath: 'inset(50%)', whiteSpace: 'nowrap', margin: '-1px',
|
|
661
|
+
});
|
|
662
|
+
document.body.append(announcer);
|
|
663
|
+
}
|
|
664
|
+
// Emptied first, so the same text said twice is still read the second time.
|
|
665
|
+
announcer.textContent = '';
|
|
666
|
+
setTimeout(() => { announcer.textContent = text; }, 50);
|
|
667
|
+
}
|
|
668
|
+
})();
|