@typeonce/ritmo-browser 0.22.0 → 0.23.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/CHANGELOG.md +75 -0
- package/README.md +1 -1
- package/dist/BrowserHostClock.d.ts +5 -1
- package/dist/BrowserHostClock.d.ts.map +1 -1
- package/dist/BrowserHostClock.js.map +1 -1
- package/dist/BrowserTickSource.d.ts +1 -1
- package/dist/BrowserTickSource.js +1 -1
- package/dist/Controls.d.ts +2 -1
- package/dist/Controls.d.ts.map +1 -1
- package/dist/Controls.js.map +1 -1
- package/dist/Gamepad.d.ts +16 -3
- package/dist/Gamepad.d.ts.map +1 -1
- package/dist/Gamepad.js +13 -8
- package/dist/Gamepad.js.map +1 -1
- package/dist/InputCapture.d.ts +2 -1
- package/dist/InputCapture.d.ts.map +1 -1
- package/dist/InputCapture.js.map +1 -1
- package/dist/Keyboard.d.ts +35 -11
- package/dist/Keyboard.d.ts.map +1 -1
- package/dist/Keyboard.js +99 -24
- package/dist/Keyboard.js.map +1 -1
- package/dist/Pointer.d.ts +17 -5
- package/dist/Pointer.d.ts.map +1 -1
- package/dist/Pointer.js +90 -18
- package/dist/Pointer.js.map +1 -1
- package/package.json +2 -2
- package/src/BrowserHostClock.ts +5 -1
- package/src/BrowserTickSource.ts +1 -1
- package/src/Controls.ts +2 -1
- package/src/Gamepad.ts +30 -12
- package/src/InputCapture.ts +2 -1
- package/src/Keyboard.ts +121 -28
- package/src/Pointer.ts +98 -19
package/src/Keyboard.ts
CHANGED
|
@@ -11,11 +11,22 @@
|
|
|
11
11
|
* Bind by character (`"w"`) or by physical key (`Keyboard.code("KeyW")`);
|
|
12
12
|
* see {@link KeyBinding}. A key released while a modifier changed its
|
|
13
13
|
* character (W pressed, then Option, then W released as "∑") still releases
|
|
14
|
-
* its action.
|
|
14
|
+
* its action. Keys that type the same character (both Shift keys, a digit on
|
|
15
|
+
* the row and on the numpad) are one key to a character binding: it is
|
|
16
|
+
* pressed when the first goes down and released when the last comes up.
|
|
15
17
|
*
|
|
16
|
-
*
|
|
17
|
-
* Ctrl
|
|
18
|
-
*
|
|
18
|
+
* Browser shortcuts are left to the browser: a key pressed with Cmd held, or
|
|
19
|
+
* with Ctrl held, presses no action and is not `preventDefault`-ed, so Ctrl+R
|
|
20
|
+
* still reloads and Ctrl+F still finds while R and F are bound. AltGr reports
|
|
21
|
+
* Ctrl and Alt together and types characters on many layouts, so a key
|
|
22
|
+
* pressed with both is not a shortcut. A game that binds Control or Meta to
|
|
23
|
+
* an action (`"Control"`, `Keyboard.code("ControlLeft")`, ...) keeps every
|
|
24
|
+
* key pressed with them instead, for as long as its bindings hold one; some
|
|
25
|
+
* combinations still never reach the page (Ctrl+W / Cmd+W, Ctrl+T / Cmd+T,
|
|
26
|
+
* Cmd+Q). macOS sends no keyup for a key released while Cmd is down, so
|
|
27
|
+
* Cmd's own release lets go of every held key but the modifiers. A key still
|
|
28
|
+
* down after that, or after a window blur, is pressed again at its next
|
|
29
|
+
* auto-repeat.
|
|
19
30
|
*
|
|
20
31
|
* Keys typed into a form field are left to the field: a key that goes down
|
|
21
32
|
* in an `<input>` (any type), `<textarea>`, `<select>`, or contenteditable
|
|
@@ -88,9 +99,12 @@ import type { Direction, Vector } from "./internal/vectors.ts"
|
|
|
88
99
|
export type { Direction }
|
|
89
100
|
|
|
90
101
|
/**
|
|
91
|
-
* The subset of `KeyboardEvent` the module reads. `code` (the physical key)
|
|
92
|
-
* and `target` (the element the key was typed into) are
|
|
93
|
-
* test hosts can omit them; browsers always provide them.
|
|
102
|
+
* The subset of `KeyboardEvent` the module reads. `code` (the physical key),
|
|
103
|
+
* the modifiers, and `target` (the element the key was typed into) are
|
|
104
|
+
* optional so simple test hosts can omit them; browsers always provide them.
|
|
105
|
+
* `ctrlKey` and `metaKey` mark a browser shortcut, which {@link actions}
|
|
106
|
+
* leaves to the browser, and `altKey` tells AltGr (Ctrl and Alt together)
|
|
107
|
+
* from Ctrl.
|
|
94
108
|
*
|
|
95
109
|
* A target is a form field, whose key presses {@link actions} and
|
|
96
110
|
* {@link presses} leave to it, when its `tagName` is `INPUT` (any type,
|
|
@@ -103,6 +117,9 @@ export interface KeyEvent {
|
|
|
103
117
|
readonly key: string
|
|
104
118
|
readonly code?: string
|
|
105
119
|
readonly repeat: boolean
|
|
120
|
+
readonly ctrlKey?: boolean
|
|
121
|
+
readonly metaKey?: boolean
|
|
122
|
+
readonly altKey?: boolean
|
|
106
123
|
readonly target?: object | null
|
|
107
124
|
preventDefault(): void
|
|
108
125
|
}
|
|
@@ -119,6 +136,19 @@ const isEditableTarget = (target: object | null | undefined): boolean => {
|
|
|
119
136
|
return isContentEditable === true || (typeof tagName === "string" && editableTags.has(tagName.toUpperCase()))
|
|
120
137
|
}
|
|
121
138
|
|
|
139
|
+
const modifierKeys: ReadonlySet<string> = new Set(["Shift", "Control", "Alt", "AltGraph", "Meta"])
|
|
140
|
+
const modifierCodes: ReadonlySet<string> = new Set(["ShiftLeft", "ShiftRight", "ControlLeft", "ControlRight", "AltLeft", "AltRight", "MetaLeft", "MetaRight"])
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Whether a key goes down as a browser shortcut: with Cmd, or with Ctrl
|
|
144
|
+
* without Alt (AltGr reports both and types characters), and not a
|
|
145
|
+
* modifier itself.
|
|
146
|
+
*/
|
|
147
|
+
const isShortcut = (event: KeyEvent): boolean =>
|
|
148
|
+
(event.metaKey === true || (event.ctrlKey === true && event.altKey !== true)) && !modifierKeys.has(event.key)
|
|
149
|
+
|
|
150
|
+
const isMeta = (event: KeyEvent): boolean => event.key === "Meta" || event.code === "MetaLeft" || event.code === "MetaRight"
|
|
151
|
+
|
|
122
152
|
/**
|
|
123
153
|
* The event target the module listens on, usually `window`.
|
|
124
154
|
*/
|
|
@@ -146,7 +176,10 @@ export interface PhysicalKey {
|
|
|
146
176
|
* Character bindings follow the keyboard layout and are what a menu should
|
|
147
177
|
* show. Physical bindings keep a position fixed: WASD stays under the left
|
|
148
178
|
* hand on AZERTY keyboards, and modifiers cannot change the match (with
|
|
149
|
-
* Option held on macOS, W types "∑"; with Shift, 1 types "!").
|
|
179
|
+
* Option held on macOS, W types "∑"; with Shift, 1 types "!"). A character
|
|
180
|
+
* binding matches every key that types it: `"Shift"` is both Shift keys,
|
|
181
|
+
* held while either is down, and `"1"` is the digit row and the numpad;
|
|
182
|
+
* bind `Keyboard.code("ShiftLeft")` for one of them.
|
|
150
183
|
*/
|
|
151
184
|
export type KeyBinding = string | PhysicalKey
|
|
152
185
|
|
|
@@ -326,11 +359,25 @@ const bindingLabel = (binding: KeyBinding): string => typeof binding === "string
|
|
|
326
359
|
const labelsOf = (flat: Readonly<Record<string, Keys>>): ReadonlyArray<string> =>
|
|
327
360
|
[...new Set(Object.values(flat).flatMap((keys) => keys.map(bindingLabel)))]
|
|
328
361
|
|
|
362
|
+
const modifierTokens: ReadonlySet<string> = new Set([...[...modifierKeys].map(keyToken), ...[...modifierCodes].map(codeToken)])
|
|
363
|
+
|
|
364
|
+
/** The tokens of Ctrl and Cmd: a game that binds one keeps the keys pressed with them. */
|
|
365
|
+
const shortcutModifierTokens = [
|
|
366
|
+
keyToken("Control"),
|
|
367
|
+
keyToken("Meta"),
|
|
368
|
+
codeToken("ControlLeft"),
|
|
369
|
+
codeToken("ControlRight"),
|
|
370
|
+
codeToken("MetaLeft"),
|
|
371
|
+
codeToken("MetaRight")
|
|
372
|
+
]
|
|
373
|
+
|
|
329
374
|
/**
|
|
330
375
|
* Starts tracking bound keys on `host`, usually `window`, until `dispose()`.
|
|
331
376
|
* When the window loses focus every held action is released. A key pressed
|
|
332
|
-
* in a form field (see {@link KeyEvent}) is left to the field
|
|
333
|
-
*
|
|
377
|
+
* in a form field (see {@link KeyEvent}) is left to the field, and a browser
|
|
378
|
+
* shortcut (a key pressed with Cmd or Ctrl, unless the bindings use Control
|
|
379
|
+
* or Meta) to the browser: it presses no action and is not
|
|
380
|
+
* `preventDefault`-ed. Each call is
|
|
334
381
|
* its own device: build one per source (one `Controls` merge, one player
|
|
335
382
|
* of a pool). Use {@link scripted} for tests and headless runs, and
|
|
336
383
|
* {@link presses} to capture an unbound key on a settings screen.
|
|
@@ -346,6 +393,8 @@ export const actions = <const B extends Bindings>(
|
|
|
346
393
|
const names = Object.keys(flatten(initial))
|
|
347
394
|
let bindings = flatten(initial)
|
|
348
395
|
const actionsByToken = new Map<string, Array<string>>()
|
|
396
|
+
// Off while the bindings use Ctrl or Cmd as a button: then every key pressed with them is the game's.
|
|
397
|
+
let leavesShortcuts = true
|
|
349
398
|
const index = (): void => {
|
|
350
399
|
actionsByToken.clear()
|
|
351
400
|
for (const name of names) {
|
|
@@ -356,6 +405,7 @@ export const actions = <const B extends Bindings>(
|
|
|
356
405
|
else actionsByToken.set(token, [name])
|
|
357
406
|
}
|
|
358
407
|
}
|
|
408
|
+
leavesShortcuts = !shortcutModifierTokens.some((token) => actionsByToken.has(token))
|
|
359
409
|
}
|
|
360
410
|
index()
|
|
361
411
|
|
|
@@ -368,27 +418,68 @@ export const actions = <const B extends Bindings>(
|
|
|
368
418
|
/**
|
|
369
419
|
* The character token each physical key produced when it went down, so its
|
|
370
420
|
* release matches even if a modifier changed the character meanwhile
|
|
371
|
-
* (W pressed, Option pressed, W released as "∑").
|
|
421
|
+
* (W pressed, Option pressed, W released as "∑"). It also holds the keys
|
|
422
|
+
* known to be down, form fields and shortcuts included: their repeats
|
|
423
|
+
* press nothing.
|
|
372
424
|
*/
|
|
373
425
|
const keyTokenByCode = new Map<string, string>()
|
|
426
|
+
/**
|
|
427
|
+
* The physical keys holding each held character token, so a character two
|
|
428
|
+
* keys type (both Shifts, a digit on the row and on the numpad) stays held
|
|
429
|
+
* until the last of them comes up. A token held through a host that sends
|
|
430
|
+
* no `code` has no entry, and its first release lets go of it.
|
|
431
|
+
*/
|
|
432
|
+
const codesByToken = new Map<string, Set<string>>()
|
|
374
433
|
const pressed = new Set<string>()
|
|
375
434
|
const released = new Set<string>()
|
|
376
435
|
|
|
436
|
+
const release = (token: string): void => {
|
|
437
|
+
const holding = heldTokens.get(token)
|
|
438
|
+
if (!holding) return
|
|
439
|
+
heldTokens.delete(token)
|
|
440
|
+
codesByToken.delete(token)
|
|
441
|
+
for (const name of holding) released.add(name)
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** Lets go of `code`'s hold on a character token, releasing it when no other key holds it. */
|
|
445
|
+
const letGo = (token: string, code: string): void => {
|
|
446
|
+
const codes = codesByToken.get(token)
|
|
447
|
+
if (codes === undefined || !codes.delete(code) || codes.size > 0) return
|
|
448
|
+
release(token)
|
|
449
|
+
}
|
|
450
|
+
|
|
377
451
|
const onKeyDown = (event: KeyEvent): void => {
|
|
378
|
-
// Typed into a form field: the key belongs to the field. Its release is
|
|
379
|
-
// still processed by `onKeyUp`.
|
|
380
|
-
if (isEditableTarget(event.target)) return
|
|
381
452
|
const tokens = [keyToken(event.key)]
|
|
453
|
+
// A repeat of a key known to be down presses nothing; one whose release
|
|
454
|
+
// was missed (a blur, or Cmd on macOS) presses it again. Without `code`,
|
|
455
|
+
// a repeat presses any key that is not held.
|
|
456
|
+
const known = event.code !== undefined && keyTokenByCode.has(event.code)
|
|
382
457
|
if (event.code !== undefined) {
|
|
383
|
-
if (!
|
|
458
|
+
if (!event.repeat || !known) {
|
|
459
|
+
// A new press: the key went up unseen if it was known down, so it no
|
|
460
|
+
// longer holds what it typed then, and its release matches what it types now.
|
|
461
|
+
const before = keyTokenByCode.get(event.code)
|
|
462
|
+
if (before !== undefined && before !== tokens[0]) letGo(before, event.code)
|
|
463
|
+
keyTokenByCode.set(event.code, tokens[0]!)
|
|
464
|
+
}
|
|
384
465
|
tokens.push(codeToken(event.code))
|
|
385
466
|
}
|
|
467
|
+
// Typed into a form field, the key belongs to the field; a shortcut
|
|
468
|
+
// belongs to the browser. Their releases are still processed by `onKeyUp`.
|
|
469
|
+
if (isEditableTarget(event.target) || (leavesShortcuts && isShortcut(event))) return
|
|
386
470
|
let handled = false
|
|
387
471
|
for (const token of tokens) {
|
|
388
472
|
const bound = actionsByToken.get(token)
|
|
389
473
|
if (!bound) continue
|
|
390
474
|
handled = true
|
|
391
|
-
if (event.repeat
|
|
475
|
+
if (event.repeat && known) continue
|
|
476
|
+
// Another key typing the same character joins its hold: one press for both.
|
|
477
|
+
if (event.code !== undefined && token === tokens[0]) {
|
|
478
|
+
const codes = codesByToken.get(token)
|
|
479
|
+
if (codes) codes.add(event.code)
|
|
480
|
+
else if (!heldTokens.has(token)) codesByToken.set(token, new Set([event.code]))
|
|
481
|
+
}
|
|
482
|
+
if (heldTokens.has(token)) continue
|
|
392
483
|
heldTokens.set(token, new Set(bound))
|
|
393
484
|
for (const name of bound) pressed.add(name)
|
|
394
485
|
}
|
|
@@ -398,19 +489,18 @@ export const actions = <const B extends Bindings>(
|
|
|
398
489
|
// Releases are processed wherever the key was released, form fields
|
|
399
490
|
// included, so a key held before focus moved into a field still releases.
|
|
400
491
|
const onKeyUp = (event: KeyEvent): void => {
|
|
401
|
-
const
|
|
492
|
+
const character = event.code === undefined ? keyToken(event.key) : keyTokenByCode.get(event.code) ?? keyToken(event.key)
|
|
493
|
+
if (event.code === undefined || !codesByToken.has(character)) release(character)
|
|
494
|
+
else letGo(character, event.code)
|
|
402
495
|
if (event.code !== undefined) {
|
|
403
|
-
const downAs = keyTokenByCode.get(event.code)
|
|
404
496
|
keyTokenByCode.delete(event.code)
|
|
405
|
-
|
|
406
|
-
tokens.push(codeToken(event.code))
|
|
407
|
-
}
|
|
408
|
-
for (const token of tokens) {
|
|
409
|
-
const holding = heldTokens.get(token)
|
|
410
|
-
if (!holding) continue
|
|
411
|
-
heldTokens.delete(token)
|
|
412
|
-
for (const name of holding) released.add(name)
|
|
497
|
+
release(codeToken(event.code))
|
|
413
498
|
}
|
|
499
|
+
// macOS sends no keyup for a key released while Cmd is down: Cmd's own
|
|
500
|
+
// release lets go of every key but the modifiers, which do get theirs.
|
|
501
|
+
if (!isMeta(event)) return
|
|
502
|
+
for (const token of heldTokens.keys()) if (!modifierTokens.has(token)) release(token)
|
|
503
|
+
for (const code of keyTokenByCode.keys()) if (!modifierCodes.has(code)) keyTokenByCode.delete(code)
|
|
414
504
|
}
|
|
415
505
|
|
|
416
506
|
const onBlur = (): void => {
|
|
@@ -418,6 +508,7 @@ export const actions = <const B extends Bindings>(
|
|
|
418
508
|
for (const name of holding) released.add(name)
|
|
419
509
|
}
|
|
420
510
|
heldTokens.clear()
|
|
511
|
+
codesByToken.clear()
|
|
421
512
|
keyTokenByCode.clear()
|
|
422
513
|
}
|
|
423
514
|
|
|
@@ -490,7 +581,9 @@ export interface Presses extends System.Consumes<"snapshot"> {
|
|
|
490
581
|
* Starts collecting every key press on `host`, whatever the bindings, for a
|
|
491
582
|
* settings screen that waits for the key to bind. Use {@link actions} for
|
|
492
583
|
* gameplay. It never calls `preventDefault()`, and skips keys pressed in a
|
|
493
|
-
* form field (see {@link KeyEvent})
|
|
584
|
+
* form field (see {@link KeyEvent}) and browser shortcuts (a key pressed with
|
|
585
|
+
* Cmd or Ctrl), so Ctrl+R on the settings screen binds nothing; Control and
|
|
586
|
+
* Meta pressed alone are offered.
|
|
494
587
|
*
|
|
495
588
|
* @example
|
|
496
589
|
* ```ts
|
|
@@ -509,7 +602,7 @@ export interface Presses extends System.Consumes<"snapshot"> {
|
|
|
509
602
|
export const presses = (host: KeyboardHost): Presses => {
|
|
510
603
|
let pending: Array<KeyPress> = []
|
|
511
604
|
const onKeyDown = (event: KeyEvent): void => {
|
|
512
|
-
if (event.repeat || isEditableTarget(event.target)) return
|
|
605
|
+
if (event.repeat || isEditableTarget(event.target) || isShortcut(event)) return
|
|
513
606
|
pending.push({ key: event.key, code: event.code ?? null })
|
|
514
607
|
}
|
|
515
608
|
host.addEventListener("keydown", onKeyDown)
|
package/src/Pointer.ts
CHANGED
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
* as one device for `lastUsed` and `available`). Games that aim with the
|
|
10
10
|
* mouse read `position`. The module owns the browser plumbing:
|
|
11
11
|
* element-relative coordinates, press/release edges that survive clicks
|
|
12
|
-
* shorter than one frame,
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* shorter than one frame, buttons pressed and released while another is
|
|
13
|
+
* held (which browsers report as pointer moves), pointer capture so a
|
|
14
|
+
* release outside the element is still seen, suppressing the context menu,
|
|
15
|
+
* clearing buttons on window blur, and listener cleanup. It does not know about the ECS;
|
|
15
16
|
* `InputCapture.system(...)` copies snapshots into a resource.
|
|
16
17
|
*
|
|
17
18
|
* Positions are in CSS pixels relative to the element's top-left corner.
|
|
@@ -97,6 +98,14 @@ export const wheelStep = 100
|
|
|
97
98
|
|
|
98
99
|
const buttonOf = (code: number): Button | undefined => physicalButtons[code]
|
|
99
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Each physical button's bit in `PointerEvent.buttons`, in `PointerEvent.button`
|
|
103
|
+
* order: the two encodings swap the middle and secondary buttons.
|
|
104
|
+
*/
|
|
105
|
+
const buttonBits = [1, 4, 2, 8, 16] as const
|
|
106
|
+
|
|
107
|
+
const bitOf = (code: number): number => buttonBits[code] ?? 0
|
|
108
|
+
|
|
100
109
|
const buttonSet: ReadonlySet<string> = new Set(buttonNames)
|
|
101
110
|
|
|
102
111
|
/** Whether `name` is a pointer button, for validating stored bindings. */
|
|
@@ -190,7 +199,7 @@ export interface Actions<B extends Bindings> extends System.Consumes<"snapshot">
|
|
|
190
199
|
* the pointer was made with.
|
|
191
200
|
*/
|
|
192
201
|
rebind<const R extends Rebinding<B>>(bindings: R & OnlyActionsOf<R, B>): void
|
|
193
|
-
/** Removes the event listeners. */
|
|
202
|
+
/** Removes the event listeners and releases the pointer capture, if taken. */
|
|
194
203
|
dispose(): void
|
|
195
204
|
}
|
|
196
205
|
|
|
@@ -203,7 +212,17 @@ export interface PointerEventLike {
|
|
|
203
212
|
/** Raw motion since the previous event; events without it count as 0. */
|
|
204
213
|
readonly movementX?: number
|
|
205
214
|
readonly movementY?: number
|
|
215
|
+
/** The button that changed: 0 primary, 1 middle, 2 secondary, 3 back, 4 forward. */
|
|
206
216
|
readonly button: number
|
|
217
|
+
/**
|
|
218
|
+
* Every button down after this event, as a bitmask: 1 primary, 2
|
|
219
|
+
* secondary, 4 middle, 8 back, 16 forward. Browsers send `pointerdown`
|
|
220
|
+
* only for the first button down and `pointerup` only for the last one up;
|
|
221
|
+
* the buttons in between arrive as `pointermove`, so the module holds what
|
|
222
|
+
* this reports on every down, up, and move. Events without it are read by
|
|
223
|
+
* `button` alone, which misses those.
|
|
224
|
+
*/
|
|
225
|
+
readonly buttons?: number
|
|
207
226
|
readonly pointerId: number
|
|
208
227
|
/** `"mouse"`, `"pen"`, or `"touch"`; events without it count as mouse. */
|
|
209
228
|
readonly pointerType?: string
|
|
@@ -363,7 +382,8 @@ const tracker = <B extends Bindings>(initial: B, start: Position) => {
|
|
|
363
382
|
* Starts tracking the pointer over `target` with `bindings`, until
|
|
364
383
|
* `dispose()`. `blurHost` (usually `window`) releases held buttons when the
|
|
365
384
|
* page loses focus; a cancelled pointer and leaving the pointer lock release
|
|
366
|
-
* them too.
|
|
385
|
+
* them too. Each of these, the last button coming up, and `dispose()` also
|
|
386
|
+
* release the pointer capture the first press took. Use `Touch.actions` for on-screen touch controls.
|
|
367
387
|
*/
|
|
368
388
|
export const actions = <const B extends Bindings>(
|
|
369
389
|
target: PointerTarget,
|
|
@@ -379,6 +399,64 @@ export const actions = <const B extends Bindings>(
|
|
|
379
399
|
const state = tracker(bindings, { x: 0, y: 0 })
|
|
380
400
|
let over = false
|
|
381
401
|
const isLocked = (): boolean => lock !== undefined && lock.pointerLockElement === target
|
|
402
|
+
const awaitingLock = (): boolean => lock !== undefined && !isLocked()
|
|
403
|
+
// Buttons that went down while the pointer was not locked, ignored until they
|
|
404
|
+
// come up: the click that locks is not a press, even once the lock is taken.
|
|
405
|
+
let ignored = 0
|
|
406
|
+
// The pointer id capture was taken for, until it is released.
|
|
407
|
+
let captured: number | null = null
|
|
408
|
+
|
|
409
|
+
const capture = (pointerId: number): void => {
|
|
410
|
+
captured = pointerId
|
|
411
|
+
target.setPointerCapture?.(pointerId)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const release = (): void => {
|
|
415
|
+
if (captured === null) return
|
|
416
|
+
const pointerId = captured
|
|
417
|
+
captured = null
|
|
418
|
+
// Browsers throw for a pointer that is no longer active (after a cancel) or not captured.
|
|
419
|
+
try {
|
|
420
|
+
target.releasePointerCapture?.(pointerId)
|
|
421
|
+
} catch {
|
|
422
|
+
// The capture is already gone.
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// A blur, a cancelled pointer, and leaving the lock let go of every button and of the capture.
|
|
427
|
+
const letGo = (): void => {
|
|
428
|
+
state.buttons.upAll()
|
|
429
|
+
release()
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const heldMask = (): number => {
|
|
433
|
+
let mask = 0
|
|
434
|
+
for (let code = 0; code < physicalButtons.length; code++) {
|
|
435
|
+
if (state.buttons.held.has(physicalButtons[code]!)) mask |= buttonBits[code]!
|
|
436
|
+
}
|
|
437
|
+
return mask
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Holds exactly the buttons of `mask` (a `buttons` bitmask), pressing and
|
|
442
|
+
* releasing those that changed, and captures the pointer while any is held.
|
|
443
|
+
*/
|
|
444
|
+
const hold = (mask: number, pointerId: number, unlocked: boolean): void => {
|
|
445
|
+
const wasHeld = state.buttons.held.size > 0
|
|
446
|
+
ignored &= mask
|
|
447
|
+
for (let code = 0; code < physicalButtons.length; code++) {
|
|
448
|
+
const button = physicalButtons[code]!
|
|
449
|
+
const bit = buttonBits[code]!
|
|
450
|
+
if ((mask & bit) === 0) state.buttons.up(button)
|
|
451
|
+
else if ((ignored & bit) === 0 && !state.buttons.held.has(button)) {
|
|
452
|
+
if (unlocked) ignored |= bit
|
|
453
|
+
else state.buttons.down(button)
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
const isHeld = state.buttons.held.size > 0
|
|
457
|
+
if (!wasHeld && isHeld) capture(pointerId)
|
|
458
|
+
else if (wasHeld && !isHeld) release()
|
|
459
|
+
}
|
|
382
460
|
|
|
383
461
|
const locate = (event: PointerEventLike): void => {
|
|
384
462
|
const rect = target.getBoundingClientRect()
|
|
@@ -391,30 +469,30 @@ export const actions = <const B extends Bindings>(
|
|
|
391
469
|
locate(event)
|
|
392
470
|
over = true
|
|
393
471
|
const button = buttonOf(event.button)
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
472
|
+
const unlocked = awaitingLock()
|
|
473
|
+
if (button !== undefined) {
|
|
474
|
+
event.preventDefault()
|
|
397
475
|
// The click that locks is not a press: "click to resume" does not also fire.
|
|
398
|
-
target.requestPointerLock?.()
|
|
399
|
-
return
|
|
476
|
+
if (unlocked) target.requestPointerLock?.()
|
|
400
477
|
}
|
|
401
|
-
|
|
402
|
-
|
|
478
|
+
if (event.buttons !== undefined) hold(event.buttons, event.pointerId, unlocked)
|
|
479
|
+
else if (button !== undefined && !unlocked) hold(heldMask() | bitOf(event.button), event.pointerId, false)
|
|
403
480
|
}
|
|
404
481
|
|
|
405
482
|
const onUp = (event: PointerEventLike): void => {
|
|
406
483
|
if (!tracked(event)) return
|
|
407
484
|
locate(event)
|
|
408
485
|
const button = buttonOf(event.button)
|
|
409
|
-
if (
|
|
410
|
-
state.buttons.
|
|
411
|
-
if (state.buttons.held.size === 0) target.releasePointerCapture?.(event.pointerId)
|
|
486
|
+
if (event.buttons !== undefined) hold(event.buttons, event.pointerId, awaitingLock())
|
|
487
|
+
else if (button !== undefined && state.buttons.held.has(button)) hold(heldMask() & ~bitOf(event.button), event.pointerId, false)
|
|
412
488
|
}
|
|
413
489
|
|
|
414
490
|
const onMove = (event: PointerEventLike): void => {
|
|
415
491
|
if (!tracked(event)) return
|
|
416
492
|
locate(event)
|
|
417
493
|
over = true
|
|
494
|
+
// A button pressed or released while another is held arrives as a move.
|
|
495
|
+
if (event.buttons !== undefined) hold(event.buttons, event.pointerId, awaitingLock())
|
|
418
496
|
}
|
|
419
497
|
|
|
420
498
|
const onLeave = (event: PointerEventLike): void => {
|
|
@@ -422,7 +500,7 @@ export const actions = <const B extends Bindings>(
|
|
|
422
500
|
}
|
|
423
501
|
|
|
424
502
|
const onCancel = (event: PointerEventLike): void => {
|
|
425
|
-
if (tracked(event))
|
|
503
|
+
if (tracked(event)) letGo()
|
|
426
504
|
}
|
|
427
505
|
|
|
428
506
|
const onContextMenu = (event: { preventDefault(): void }): void => {
|
|
@@ -437,7 +515,7 @@ export const actions = <const B extends Bindings>(
|
|
|
437
515
|
|
|
438
516
|
// Leaving the lock (Escape) releases what was held, like a blur.
|
|
439
517
|
const onLockChange = (): void => {
|
|
440
|
-
if (!isLocked())
|
|
518
|
+
if (!isLocked()) letGo()
|
|
441
519
|
}
|
|
442
520
|
|
|
443
521
|
target.addEventListener("pointerdown", onDown)
|
|
@@ -448,7 +526,7 @@ export const actions = <const B extends Bindings>(
|
|
|
448
526
|
target.addEventListener("pointercancel", onCancel)
|
|
449
527
|
target.addEventListener("contextmenu", onContextMenu)
|
|
450
528
|
target.addEventListener("wheel", onWheel, { passive: false })
|
|
451
|
-
blurHost.addEventListener("blur",
|
|
529
|
+
blurHost.addEventListener("blur", letGo)
|
|
452
530
|
lock?.addEventListener("pointerlockchange", onLockChange)
|
|
453
531
|
|
|
454
532
|
return {
|
|
@@ -466,8 +544,9 @@ export const actions = <const B extends Bindings>(
|
|
|
466
544
|
target.removeEventListener("pointercancel", onCancel)
|
|
467
545
|
target.removeEventListener("contextmenu", onContextMenu)
|
|
468
546
|
target.removeEventListener("wheel", onWheel)
|
|
469
|
-
blurHost.removeEventListener("blur",
|
|
547
|
+
blurHost.removeEventListener("blur", letGo)
|
|
470
548
|
lock?.removeEventListener("pointerlockchange", onLockChange)
|
|
549
|
+
release()
|
|
471
550
|
}
|
|
472
551
|
}
|
|
473
552
|
}
|