@mattebox/player-spatial-nav 0.1.2 → 0.3.0
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/README.md +22 -11
- package/dist/cdn/mattebox-player-spatial-nav-gamepad.min.js +1 -0
- package/dist/cdn/mattebox-player-spatial-nav.min.js +1 -1
- package/dist/component.js +40 -44
- package/dist/component.js.map +1 -1
- package/dist/es2015/component.js +2 -2
- package/dist/es2015/gamepad-help.js +139 -0
- package/dist/es2015/gamepad-index.js +17 -0
- package/dist/es2015/gamepad.js +321 -0
- package/dist/es2015/host.js +10 -1
- package/dist/es2015/index.js +3 -0
- package/dist/es2015/pad.js +157 -0
- package/dist/es2015/spatial-nav.js +2 -7
- package/dist/gamepad-help.d.ts +30 -0
- package/dist/gamepad-help.d.ts.map +1 -0
- package/dist/gamepad-help.js +137 -0
- package/dist/gamepad-help.js.map +1 -0
- package/dist/gamepad-index.d.ts +24 -0
- package/dist/gamepad-index.d.ts.map +1 -0
- package/dist/gamepad-index.js +19 -0
- package/dist/gamepad-index.js.map +1 -0
- package/dist/gamepad.d.ts +98 -0
- package/dist/gamepad.d.ts.map +1 -0
- package/dist/gamepad.js +316 -0
- package/dist/gamepad.js.map +1 -0
- package/dist/host.d.ts +2 -0
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +23 -14
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -11
- package/dist/index.js.map +1 -1
- package/dist/keys.js +31 -30
- package/dist/keys.js.map +1 -1
- package/dist/navigate.js +78 -85
- package/dist/navigate.js.map +1 -1
- package/dist/pad.d.ts +48 -0
- package/dist/pad.d.ts.map +1 -0
- package/dist/pad.js +157 -0
- package/dist/pad.js.map +1 -0
- package/dist/spatial-nav.d.ts.map +1 -1
- package/dist/spatial-nav.js +173 -201
- package/dist/spatial-nav.js.map +1 -1
- package/package.json +12 -6
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { Component } from "./component.js";
|
|
2
|
+
import { focusedIn } from "./host.js";
|
|
3
|
+
import { BUTTONS, kindOf, mapping, parseMap } from "./pad.js";
|
|
4
|
+
// src/gamepad.ts
|
|
5
|
+
/**
|
|
6
|
+
* <mbx-gamepad>: a game controller drives the player. Placed inside the
|
|
7
|
+
* player beside <mbx-spatial-nav>, it renders nothing.
|
|
8
|
+
*
|
|
9
|
+
* The Gamepad API has no button events, so the element reads the pads once
|
|
10
|
+
* a frame, on `requestAnimationFrame`. It reads them only while one is
|
|
11
|
+
* connected and the page is visible: a page without a pad pays nothing
|
|
12
|
+
* after `attach`.
|
|
13
|
+
*
|
|
14
|
+
* Each press of a button takes the action the mapping gives it (`pad.ts`).
|
|
15
|
+
* The element fires `gamepadaction` first, cancellable, so the page can
|
|
16
|
+
* take any action, or one of its own name. Nobody took it: the element
|
|
17
|
+
* sends the player the key a remote would send.
|
|
18
|
+
*
|
|
19
|
+
* - The directions are the arrows, which <mbx-spatial-nav> turns into
|
|
20
|
+
* moves of focus. They repeat while held.
|
|
21
|
+
* - `activate` is Enter on the focused control. With no control focused,
|
|
22
|
+
* or the bar asleep, it plays or pauses.
|
|
23
|
+
* - `back` is the remote's Back. With no control focused, it leaves
|
|
24
|
+
* fullscreen first; after that, Back is the application's.
|
|
25
|
+
* - `rewind` and `fast-forward` seek a step at each press, and again while
|
|
26
|
+
* held, faster the harder the trigger is pressed.
|
|
27
|
+
* - `previous` and `next` go to the chapter before or after, when the
|
|
28
|
+
* programme has chapters, else to the playlist's item.
|
|
29
|
+
*
|
|
30
|
+
* A press sets `focus-ring` on the player, and a pointer's press takes it
|
|
31
|
+
* off. The keys the element sends are synthetic, and after them the
|
|
32
|
+
* browser shows no `:focus-visible`, so without it the focus would not show.
|
|
33
|
+
*
|
|
34
|
+
* Edge on Xbox drives a mouse cursor with the pad unless the page asks for
|
|
35
|
+
* the pad itself, so the element asks, where the browser has the switch.
|
|
36
|
+
*/
|
|
37
|
+
/** The help bubble's tag: `help` opens it. */
|
|
38
|
+
const HELP = "mbx-gamepad-help";
|
|
39
|
+
/** The player's attribute that shows the focus a pad gave. */
|
|
40
|
+
const RING = "focus-ring";
|
|
41
|
+
/** The left stick leans a direction past this much. */
|
|
42
|
+
const DEAD_ZONE = .5;
|
|
43
|
+
/** A trigger counts as pressed past this much of its travel. */
|
|
44
|
+
const TRIGGER = .15;
|
|
45
|
+
/** A held direction repeats after this long, in ms, then every `STEP_MS`. */
|
|
46
|
+
const DELAY_MS = 400;
|
|
47
|
+
const STEP_MS = 120;
|
|
48
|
+
/** A held trigger seeks again after this long, in ms, then every `SCAN_MS` at full press. */
|
|
49
|
+
const SCAN_DELAY_MS = 300;
|
|
50
|
+
const SCAN_MS = 100;
|
|
51
|
+
/** Back to the start of the chapter, not the one before, this far into it, in seconds. */
|
|
52
|
+
const RESTART_S = 3;
|
|
53
|
+
/** The seconds a step seeks where no control bar answers the media keys. */
|
|
54
|
+
const STEP_S = 5;
|
|
55
|
+
/** The keys the directions send. */
|
|
56
|
+
const ARROWS = {
|
|
57
|
+
up: "ArrowUp",
|
|
58
|
+
down: "ArrowDown",
|
|
59
|
+
left: "ArrowLeft",
|
|
60
|
+
right: "ArrowRight"
|
|
61
|
+
};
|
|
62
|
+
/** The actions that repeat while their button is held. */
|
|
63
|
+
const REPEATING = /* @__PURE__ */ new Set([
|
|
64
|
+
"up",
|
|
65
|
+
"down",
|
|
66
|
+
"left",
|
|
67
|
+
"right",
|
|
68
|
+
"rewind",
|
|
69
|
+
"fast-forward"
|
|
70
|
+
]);
|
|
71
|
+
var MbxGamepad = class extends Component {
|
|
72
|
+
constructor() {
|
|
73
|
+
super();
|
|
74
|
+
this.held = /* @__PURE__ */ new Map();
|
|
75
|
+
this.frame = 0;
|
|
76
|
+
this.own = {};
|
|
77
|
+
this.last = "xbox";
|
|
78
|
+
}
|
|
79
|
+
/** The overrides set from script, over the defaults and the `map` attribute. Null unbinds a button. */
|
|
80
|
+
get map() {
|
|
81
|
+
return Object.assign({}, this.own);
|
|
82
|
+
}
|
|
83
|
+
set map(value) {
|
|
84
|
+
this.own = Object.assign({}, value);
|
|
85
|
+
}
|
|
86
|
+
/** The make of the pad used last, `xbox` before any. */
|
|
87
|
+
get kind() {
|
|
88
|
+
return this.last;
|
|
89
|
+
}
|
|
90
|
+
/** What each button does on `kind`: the defaults, the `map` attribute, then `map`. */
|
|
91
|
+
bindings(kind = this.last) {
|
|
92
|
+
return mapping(kind, parseMap(this.getAttribute("map")), this.own);
|
|
93
|
+
}
|
|
94
|
+
disconnectedCallback() {
|
|
95
|
+
var _this$player;
|
|
96
|
+
this.stop();
|
|
97
|
+
(_this$player = this.player) === null || _this$player === void 0 || _this$player.removeAttribute(RING);
|
|
98
|
+
super.disconnectedCallback();
|
|
99
|
+
}
|
|
100
|
+
attach(player) {
|
|
101
|
+
const nav = navigator;
|
|
102
|
+
if ("gamepadInputEmulation" in nav) nav.gamepadInputEmulation = "gamepad";
|
|
103
|
+
this.on(window, "gamepadconnected", (event) => {
|
|
104
|
+
const pad = event.gamepad;
|
|
105
|
+
this.last = kindOf(pad.id);
|
|
106
|
+
this.tell("gamepadconnect", pad);
|
|
107
|
+
this.start(player);
|
|
108
|
+
});
|
|
109
|
+
this.on(window, "gamepaddisconnected", (event) => {
|
|
110
|
+
const pad = event.gamepad;
|
|
111
|
+
this.held.delete(pad.index);
|
|
112
|
+
this.tell("gamepaddisconnect", pad);
|
|
113
|
+
});
|
|
114
|
+
this.on(document, "visibilitychange", () => this.start(player));
|
|
115
|
+
this.on(player, "pointerdown", () => player.removeAttribute(RING), { capture: true });
|
|
116
|
+
this.start(player);
|
|
117
|
+
}
|
|
118
|
+
/** Fires `name` on the element, bubbling and composed, with the pad's make and id. */
|
|
119
|
+
tell(name, pad) {
|
|
120
|
+
const detail = {
|
|
121
|
+
kind: kindOf(pad.id),
|
|
122
|
+
id: pad.id
|
|
123
|
+
};
|
|
124
|
+
this.dispatchEvent(new CustomEvent(name, {
|
|
125
|
+
detail,
|
|
126
|
+
bubbles: true,
|
|
127
|
+
composed: true
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
/** The connected pads. Chromium lists empty slots as null. */
|
|
131
|
+
pads() {
|
|
132
|
+
if (typeof navigator.getGamepads !== "function") return [];
|
|
133
|
+
const out = [];
|
|
134
|
+
for (const pad of navigator.getGamepads()) if ((pad === null || pad === void 0 ? void 0 : pad.connected) === true) out.push(pad);
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
/** Starts the loop, unless it runs, the page is hidden, or no pad is connected. */
|
|
138
|
+
start(player) {
|
|
139
|
+
if (this.frame !== 0 || document.hidden || this.pads().length === 0) return;
|
|
140
|
+
this.frame = requestAnimationFrame((now) => this.tick(player, now));
|
|
141
|
+
}
|
|
142
|
+
stop() {
|
|
143
|
+
if (this.frame !== 0) cancelAnimationFrame(this.frame);
|
|
144
|
+
this.frame = 0;
|
|
145
|
+
this.held.clear();
|
|
146
|
+
}
|
|
147
|
+
tick(player, now) {
|
|
148
|
+
this.frame = 0;
|
|
149
|
+
const pads = this.pads();
|
|
150
|
+
if (pads.length === 0 || document.hidden || this.player !== player) {
|
|
151
|
+
this.held.clear();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
for (const pad of pads) this.read(player, pad, now);
|
|
155
|
+
this.frame = requestAnimationFrame((next) => this.tick(player, next));
|
|
156
|
+
}
|
|
157
|
+
/** Compares one pad with the frame before, and acts on what went down or is held. */
|
|
158
|
+
read(player, pad, now) {
|
|
159
|
+
const kind = kindOf(pad.id);
|
|
160
|
+
let held = this.held.get(pad.index);
|
|
161
|
+
if (held === void 0) {
|
|
162
|
+
held = /* @__PURE__ */ new Map();
|
|
163
|
+
this.held.set(pad.index, held);
|
|
164
|
+
}
|
|
165
|
+
const map = this.bindings(kind);
|
|
166
|
+
for (const [index, button] of BUTTONS.entries()) {
|
|
167
|
+
const value = this.value(pad, index, button);
|
|
168
|
+
const was = held.get(button);
|
|
169
|
+
if (value === 0) {
|
|
170
|
+
held.delete(button);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const action = map[button];
|
|
174
|
+
if (was === void 0) {
|
|
175
|
+
this.last = kind;
|
|
176
|
+
held.set(button, { next: now + (action === "rewind" || action === "fast-forward" ? SCAN_DELAY_MS : DELAY_MS) });
|
|
177
|
+
if (action != null) this.act(player, action, button, kind);
|
|
178
|
+
} else if (action != null && REPEATING.has(action) && now >= was.next) {
|
|
179
|
+
was.next = now + (action === "rewind" || action === "fast-forward" ? SCAN_MS + (1 - value) * SCAN_DELAY_MS : STEP_MS);
|
|
180
|
+
this.act(player, action, button, kind);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* How far `button` is pressed, from 0 to 1, or 0 when it is not. The
|
|
186
|
+
* directions read the left stick too, past the dead zone.
|
|
187
|
+
*/
|
|
188
|
+
value(pad, index, button) {
|
|
189
|
+
var _pad$axes$, _pad$axes$2;
|
|
190
|
+
const own = pad.buttons[index];
|
|
191
|
+
const trigger = button === "lt" || button === "rt";
|
|
192
|
+
let value = own === void 0 ? 0 : own.pressed ? Math.max(own.value, .5) : own.value;
|
|
193
|
+
if (value < (trigger ? TRIGGER : .5)) value = 0;
|
|
194
|
+
const x = (_pad$axes$ = pad.axes[0]) !== null && _pad$axes$ !== void 0 ? _pad$axes$ : 0;
|
|
195
|
+
const y = (_pad$axes$2 = pad.axes[1]) !== null && _pad$axes$2 !== void 0 ? _pad$axes$2 : 0;
|
|
196
|
+
return (button === "left" ? -x : button === "right" ? x : button === "up" ? -y : button === "down" ? y : 0) > DEAD_ZONE ? Math.max(value, 1) : value;
|
|
197
|
+
}
|
|
198
|
+
/** Asks the page with `gamepadaction`, then does the action nobody took. */
|
|
199
|
+
act(player, action, button, kind) {
|
|
200
|
+
const asked = new CustomEvent("gamepadaction", {
|
|
201
|
+
detail: {
|
|
202
|
+
action,
|
|
203
|
+
button,
|
|
204
|
+
kind
|
|
205
|
+
},
|
|
206
|
+
bubbles: true,
|
|
207
|
+
composed: true,
|
|
208
|
+
cancelable: true
|
|
209
|
+
});
|
|
210
|
+
if (!this.dispatchEvent(asked)) return;
|
|
211
|
+
player.toggleAttribute(RING, true);
|
|
212
|
+
this.run(player, action);
|
|
213
|
+
}
|
|
214
|
+
run(player, action) {
|
|
215
|
+
const media = player.media;
|
|
216
|
+
const arrow = ARROWS[action];
|
|
217
|
+
if (arrow !== void 0) {
|
|
218
|
+
var _focusedIn;
|
|
219
|
+
this.key((_focusedIn = focusedIn(player)) !== null && _focusedIn !== void 0 ? _focusedIn : player, arrow);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
switch (action) {
|
|
223
|
+
case "activate":
|
|
224
|
+
this.activate(player);
|
|
225
|
+
break;
|
|
226
|
+
case "back":
|
|
227
|
+
this.back(player);
|
|
228
|
+
break;
|
|
229
|
+
case "play-pause":
|
|
230
|
+
if (this.key(player, "MediaPlayPause")) toggle(media);
|
|
231
|
+
break;
|
|
232
|
+
case "rewind":
|
|
233
|
+
if (this.key(player, "MediaRewind")) media.currentTime = Math.max(0, media.currentTime - STEP_S);
|
|
234
|
+
break;
|
|
235
|
+
case "fast-forward":
|
|
236
|
+
if (this.key(player, "MediaFastForward")) media.currentTime += STEP_S;
|
|
237
|
+
break;
|
|
238
|
+
case "previous":
|
|
239
|
+
this.skip(player, -1);
|
|
240
|
+
break;
|
|
241
|
+
case "next":
|
|
242
|
+
this.skip(player, 1);
|
|
243
|
+
break;
|
|
244
|
+
case "subtitles":
|
|
245
|
+
this.press(player, "mbx-subtitles-menu");
|
|
246
|
+
break;
|
|
247
|
+
case "mute":
|
|
248
|
+
if (this.key(player, "m")) media.muted = !media.muted;
|
|
249
|
+
break;
|
|
250
|
+
case "fullscreen":
|
|
251
|
+
this.key(player, "f");
|
|
252
|
+
break;
|
|
253
|
+
case "help":
|
|
254
|
+
var _player$querySelector;
|
|
255
|
+
(_player$querySelector = player.querySelector(HELP)) === null || _player$querySelector === void 0 || _player$querySelector.toggleAttribute("open");
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Enter on the focused control. A native button clicks itself only on a
|
|
260
|
+
* real Enter, so a button nobody took is clicked here. No control
|
|
261
|
+
* focused, or the bar asleep: play or pause, as on a console's player.
|
|
262
|
+
*/
|
|
263
|
+
activate(player) {
|
|
264
|
+
const current = focusedIn(player);
|
|
265
|
+
if (current === null || player.hasAttribute("idle")) {
|
|
266
|
+
if (this.key(player, "MediaPlayPause")) toggle(player.media);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (this.key(current, "Enter") && current instanceof HTMLButtonElement) current.click();
|
|
270
|
+
}
|
|
271
|
+
/** The remote's Back, which <mbx-spatial-nav> reads. With nothing focused, fullscreen ends first. */
|
|
272
|
+
back(player) {
|
|
273
|
+
const current = focusedIn(player);
|
|
274
|
+
if (current === null && player.hasAttribute("fullscreen")) {
|
|
275
|
+
this.key(player, "f");
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
this.key(current !== null && current !== void 0 ? current : player, "GoBack");
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* The chapter before or after, else the playlist's item. Previous goes
|
|
282
|
+
* to the start of the chapter that plays first, past its first seconds,
|
|
283
|
+
* as a remote's previous does.
|
|
284
|
+
*/
|
|
285
|
+
skip(player, by) {
|
|
286
|
+
var _player$chapters, _list$at$start, _list$at;
|
|
287
|
+
const media = player.media;
|
|
288
|
+
const list = (_player$chapters = player.chapters) !== null && _player$chapters !== void 0 ? _player$chapters : [];
|
|
289
|
+
const now = media.currentTime;
|
|
290
|
+
let at = -1;
|
|
291
|
+
for (const [i, chapter] of list.entries()) if (chapter.start <= now) at = i;
|
|
292
|
+
const target = by === 1 ? list[at + 1] : at >= 0 && now - ((_list$at$start = (_list$at = list[at]) === null || _list$at === void 0 ? void 0 : _list$at.start) !== null && _list$at$start !== void 0 ? _list$at$start : 0) > RESTART_S ? list[at] : list[at - 1];
|
|
293
|
+
if (target !== void 0) {
|
|
294
|
+
media.currentTime = target.start;
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
this.press(player, by === 1 ? "mbx-next-button" : "mbx-previous-button");
|
|
298
|
+
}
|
|
299
|
+
/** Clicks the button of the control `tag` in the player, when it shows. */
|
|
300
|
+
press(player, tag) {
|
|
301
|
+
var _player$querySelector2;
|
|
302
|
+
const button = (_player$querySelector2 = player.querySelector(tag)) === null || _player$querySelector2 === void 0 || (_player$querySelector2 = _player$querySelector2.shadowRoot) === null || _player$querySelector2 === void 0 ? void 0 : _player$querySelector2.querySelector("button");
|
|
303
|
+
if (button == null || button.getClientRects().length === 0) return;
|
|
304
|
+
button.click();
|
|
305
|
+
}
|
|
306
|
+
/** Sends `name` as a keydown on `target`; true when nobody took it. */
|
|
307
|
+
key(target, name) {
|
|
308
|
+
return target.dispatchEvent(new KeyboardEvent("keydown", {
|
|
309
|
+
key: name,
|
|
310
|
+
bubbles: true,
|
|
311
|
+
composed: true,
|
|
312
|
+
cancelable: true
|
|
313
|
+
}));
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
/** Plays a paused video, pauses a playing one. */
|
|
317
|
+
function toggle(media) {
|
|
318
|
+
if (media.paused) media.play().catch(() => void 0);
|
|
319
|
+
else media.pause();
|
|
320
|
+
}
|
|
321
|
+
export { MbxGamepad };
|
package/dist/es2015/host.js
CHANGED
|
@@ -15,4 +15,13 @@ function findPlayer(node) {
|
|
|
15
15
|
}
|
|
16
16
|
return null;
|
|
17
17
|
}
|
|
18
|
-
|
|
18
|
+
/** The element focused inside `player`, through shadow roots, or null when focus is elsewhere. */
|
|
19
|
+
function focusedIn(player) {
|
|
20
|
+
var _active$shadowRoot;
|
|
21
|
+
let active = document.activeElement;
|
|
22
|
+
while ((active === null || active === void 0 || (_active$shadowRoot = active.shadowRoot) === null || _active$shadowRoot === void 0 ? void 0 : _active$shadowRoot.activeElement) != null) active = active.shadowRoot.activeElement;
|
|
23
|
+
if (!(active instanceof HTMLElement) || active === player) return null;
|
|
24
|
+
for (let node = active; node !== null; node = above(node)) if (node === player) return active;
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
export { PLAYER, above, findPlayer, focusedIn };
|
package/dist/es2015/index.js
CHANGED
|
@@ -5,6 +5,9 @@ import { MbxSpatialNav } from "./spatial-nav.js";
|
|
|
5
5
|
* on a TV. Importing this module registers the element; that is the
|
|
6
6
|
* package's one side effect. It is a package of its own because most pages
|
|
7
7
|
* never run on a TV, and a page that does not pays nothing for it.
|
|
8
|
+
*
|
|
9
|
+
* The game controller is the `./gamepad` entry, so a page with a remote
|
|
10
|
+
* alone pays nothing for it.
|
|
8
11
|
*/
|
|
9
12
|
const SPATIAL_NAV = "mbx-spatial-nav";
|
|
10
13
|
if (customElements.get("mbx-spatial-nav") === void 0) customElements.define(SPATIAL_NAV, MbxSpatialNav);
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// src/pad.ts
|
|
2
|
+
/** Every button, in the standard layout's order: the index is the button's. */
|
|
3
|
+
const BUTTONS = [
|
|
4
|
+
"south",
|
|
5
|
+
"east",
|
|
6
|
+
"west",
|
|
7
|
+
"north",
|
|
8
|
+
"lb",
|
|
9
|
+
"rb",
|
|
10
|
+
"lt",
|
|
11
|
+
"rt",
|
|
12
|
+
"select",
|
|
13
|
+
"start",
|
|
14
|
+
"ls",
|
|
15
|
+
"rs",
|
|
16
|
+
"up",
|
|
17
|
+
"down",
|
|
18
|
+
"left",
|
|
19
|
+
"right"
|
|
20
|
+
];
|
|
21
|
+
/** The default mapping, for an Xbox or PlayStation pad. */
|
|
22
|
+
const DEFAULTS = {
|
|
23
|
+
up: "up",
|
|
24
|
+
down: "down",
|
|
25
|
+
left: "left",
|
|
26
|
+
right: "right",
|
|
27
|
+
south: "activate",
|
|
28
|
+
east: "back",
|
|
29
|
+
west: null,
|
|
30
|
+
north: "subtitles",
|
|
31
|
+
lb: "previous",
|
|
32
|
+
rb: "next",
|
|
33
|
+
lt: "rewind",
|
|
34
|
+
rt: "fast-forward",
|
|
35
|
+
select: "help",
|
|
36
|
+
start: "play-pause",
|
|
37
|
+
ls: null,
|
|
38
|
+
rs: null
|
|
39
|
+
};
|
|
40
|
+
/** The default mapping for `kind`: a Nintendo pad selects with the right face button, its A. */
|
|
41
|
+
function defaults(kind) {
|
|
42
|
+
const map = Object.assign({}, DEFAULTS);
|
|
43
|
+
if (kind === "nintendo") {
|
|
44
|
+
map.south = "back";
|
|
45
|
+
map.east = "activate";
|
|
46
|
+
}
|
|
47
|
+
return map;
|
|
48
|
+
}
|
|
49
|
+
/** The USB vendor ids of the makers whose pads are drawn differently, as four hex digits. */
|
|
50
|
+
const VENDORS = {
|
|
51
|
+
"054c": "playstation",
|
|
52
|
+
"057e": "nintendo"
|
|
53
|
+
};
|
|
54
|
+
/** Words in a pad's id that give its make away where no vendor id is reported. */
|
|
55
|
+
const NAMES = [[/dualsense|dualshock|playstation|\bps[345]\b/i, "playstation"], [/nintendo|switch|joy-?con|pro controller/i, "nintendo"]];
|
|
56
|
+
/**
|
|
57
|
+
* The make of a pad, from the id the browser reports: Chromium writes
|
|
58
|
+
* `Vendor: 054c`, Firefox starts the id with `054c-`. Any other pad is
|
|
59
|
+
* taken for an Xbox pad, the layout most pads follow.
|
|
60
|
+
*/
|
|
61
|
+
function kindOf(id) {
|
|
62
|
+
var _exec, _found$, _VENDORS$vendor;
|
|
63
|
+
const found = (_exec = /vendor:\s*([0-9a-f]{4})/i.exec(id)) !== null && _exec !== void 0 ? _exec : /^([0-9a-f]{4})-[0-9a-f]{4}-/i.exec(id);
|
|
64
|
+
const vendor = found === null || found === void 0 || (_found$ = found[1]) === null || _found$ === void 0 ? void 0 : _found$.toLowerCase();
|
|
65
|
+
if (vendor !== void 0) return (_VENDORS$vendor = VENDORS[vendor]) !== null && _VENDORS$vendor !== void 0 ? _VENDORS$vendor : "xbox";
|
|
66
|
+
for (const [words, kind] of NAMES) if (words.test(id)) return kind;
|
|
67
|
+
return "xbox";
|
|
68
|
+
}
|
|
69
|
+
/** What each make prints on each button, for the controls bubble. */
|
|
70
|
+
const GLYPHS = {
|
|
71
|
+
xbox: {
|
|
72
|
+
south: "A",
|
|
73
|
+
east: "B",
|
|
74
|
+
west: "X",
|
|
75
|
+
north: "Y",
|
|
76
|
+
lb: "LB",
|
|
77
|
+
rb: "RB",
|
|
78
|
+
lt: "LT",
|
|
79
|
+
rt: "RT",
|
|
80
|
+
select: "View",
|
|
81
|
+
start: "Menu",
|
|
82
|
+
ls: "LS",
|
|
83
|
+
rs: "RS",
|
|
84
|
+
up: "↑",
|
|
85
|
+
down: "↓",
|
|
86
|
+
left: "←",
|
|
87
|
+
right: "→"
|
|
88
|
+
},
|
|
89
|
+
playstation: {
|
|
90
|
+
south: "✕",
|
|
91
|
+
east: "○",
|
|
92
|
+
west: "□",
|
|
93
|
+
north: "△",
|
|
94
|
+
lb: "L1",
|
|
95
|
+
rb: "R1",
|
|
96
|
+
lt: "L2",
|
|
97
|
+
rt: "R2",
|
|
98
|
+
select: "Create",
|
|
99
|
+
start: "Options",
|
|
100
|
+
ls: "L3",
|
|
101
|
+
rs: "R3",
|
|
102
|
+
up: "↑",
|
|
103
|
+
down: "↓",
|
|
104
|
+
left: "←",
|
|
105
|
+
right: "→"
|
|
106
|
+
},
|
|
107
|
+
nintendo: {
|
|
108
|
+
south: "B",
|
|
109
|
+
east: "A",
|
|
110
|
+
west: "Y",
|
|
111
|
+
north: "X",
|
|
112
|
+
lb: "L",
|
|
113
|
+
rb: "R",
|
|
114
|
+
lt: "ZL",
|
|
115
|
+
rt: "ZR",
|
|
116
|
+
select: "−",
|
|
117
|
+
start: "+",
|
|
118
|
+
ls: "LS",
|
|
119
|
+
rs: "RS",
|
|
120
|
+
up: "↑",
|
|
121
|
+
down: "↓",
|
|
122
|
+
left: "←",
|
|
123
|
+
right: "→"
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
/** The PlayStation symbols by name, for a screen reader, which may not read the symbol. */
|
|
127
|
+
const SPOKEN = {
|
|
128
|
+
"✕": "Cross",
|
|
129
|
+
"○": "Circle",
|
|
130
|
+
"□": "Square",
|
|
131
|
+
"△": "Triangle"
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* The overrides in a `map` attribute: `button:action` pairs apart by spaces,
|
|
135
|
+
* as `rb:fast-forward west:mute select:none`. `none` unbinds the button. A
|
|
136
|
+
* pair with an unknown button is left out.
|
|
137
|
+
*/
|
|
138
|
+
function parseMap(text) {
|
|
139
|
+
const out = {};
|
|
140
|
+
for (const pair of (text !== null && text !== void 0 ? text : "").split(/\s+/)) {
|
|
141
|
+
const [button, action] = pair.split(":");
|
|
142
|
+
if (button === void 0 || action === void 0 || action === "") continue;
|
|
143
|
+
if (!BUTTONS.includes(button)) continue;
|
|
144
|
+
out[button] = action === "none" ? null : action;
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
/** The mapping for `kind` with the overrides on top. */
|
|
149
|
+
function mapping(kind, ...overrides) {
|
|
150
|
+
const out = defaults(kind);
|
|
151
|
+
for (const layer of overrides) for (const button of BUTTONS) {
|
|
152
|
+
const action = layer[button];
|
|
153
|
+
if (action !== void 0) out[button] = action;
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
export { BUTTONS, GLYPHS, SPOKEN, defaults, kindOf, mapping, parseMap };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { above } from "./host.js";
|
|
2
1
|
import { Component } from "./component.js";
|
|
2
|
+
import { above, focusedIn } from "./host.js";
|
|
3
3
|
import { isBack, keyOf } from "./keys.js";
|
|
4
4
|
import { DIRECTIONS, OPPOSITE, candidates, nearest } from "./navigate.js";
|
|
5
5
|
// src/spatial-nav.ts
|
|
@@ -66,12 +66,7 @@ var MbxSpatialNav = class extends Component {
|
|
|
66
66
|
}
|
|
67
67
|
/** The control focused inside the player, through shadow roots, or null. */
|
|
68
68
|
focused(player) {
|
|
69
|
-
|
|
70
|
-
let active = document.activeElement;
|
|
71
|
-
while ((active === null || active === void 0 || (_active$shadowRoot = active.shadowRoot) === null || _active$shadowRoot === void 0 ? void 0 : _active$shadowRoot.activeElement) != null) active = active.shadowRoot.activeElement;
|
|
72
|
-
if (!(active instanceof HTMLElement) || active === player) return null;
|
|
73
|
-
for (let node = active; node !== null; node = above(node)) if (node === player) return active;
|
|
74
|
-
return null;
|
|
69
|
+
return focusedIn(player);
|
|
75
70
|
}
|
|
76
71
|
/** Whether `element` sits inside something that carries `open`, below the player. */
|
|
77
72
|
inOpen(player, element) {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <mbx-gamepad-help>: a bubble over the video that lists what each button
|
|
3
|
+
* of the controller does. It reads the mapping from the player's
|
|
4
|
+
* <mbx-gamepad>, overrides included, and draws the buttons as the make of
|
|
5
|
+
* the pad in use prints them.
|
|
6
|
+
*
|
|
7
|
+
* - It opens when a pad connects, and closes by itself after `close-ms`
|
|
8
|
+
* (6000 by default). `manual` keeps it closed until the `help` action.
|
|
9
|
+
* - The `help` action opens and closes it. Back closes it.
|
|
10
|
+
* - `label` names the bubble ("Controller"). `label-<action>` names an
|
|
11
|
+
* action, such as `label-next="Next episode"`; an action of the page's
|
|
12
|
+
* own shows only with a label.
|
|
13
|
+
*/
|
|
14
|
+
import { Component } from './component.js';
|
|
15
|
+
import type { PlayerHost } from './host.js';
|
|
16
|
+
export declare class MbxGamepadHelp extends Component {
|
|
17
|
+
private list;
|
|
18
|
+
private heading;
|
|
19
|
+
private timer;
|
|
20
|
+
static get observedAttributes(): string[];
|
|
21
|
+
constructor();
|
|
22
|
+
attributeChangedCallback(): void;
|
|
23
|
+
disconnectedCallback(): void;
|
|
24
|
+
protected attach(player: PlayerHost): void;
|
|
25
|
+
/** The player's <mbx-gamepad>, or null. */
|
|
26
|
+
private pad;
|
|
27
|
+
/** Lists each action the mapping binds, with its buttons as the make in use prints them. */
|
|
28
|
+
private render;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=gamepad-help.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gamepad-help.d.ts","sourceRoot":"","sources":["../src/gamepad-help.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAG3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAwB5C,qBAAa,cAAe,SAAQ,SAAS;IAC3C,QAAgB,IAAI,CAAmB;IACvC,QAAgB,OAAO,CAAc;IACrC,QAAgB,KAAK,CAA4C;IAEjE,MAAM,KAAK,kBAAkB,IAAI,MAAM,EAAE,CAExC;;IAoBD,wBAAwB,IAAI,IAAI;IAKvB,oBAAoB,IAAI,IAAI;cAKlB,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI;IAgBnD,2CAA2C;IAC3C,OAAO,CAAC,GAAG;IAIX,4FAA4F;IAC5F,OAAO,CAAC,MAAM;CAqCf"}
|