@dloizides/game-input 1.0.0 → 1.4.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 +36 -14
- package/README.md +76 -67
- package/dist/index.d.mts +87 -6
- package/dist/index.d.ts +87 -6
- package/dist/index.js +156 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +151 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,14 +1,36 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to `@dloizides/game-input` are documented here.
|
|
4
|
-
|
|
5
|
-
## 1.
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
- `createHaptics
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@dloizides/game-input` are documented here.
|
|
4
|
+
|
|
5
|
+
## 1.4.0
|
|
6
|
+
|
|
7
|
+
GI-HAPTICS-1 "Haptics vibrate only after a user activation".
|
|
8
|
+
|
|
9
|
+
- `createHaptics` no longer calls `navigator.vibrate` before a user gesture (Chrome blocks it and logs an
|
|
10
|
+
intervention). Gate: `navigator.userActivation.hasBeenActive` where present, else a capture-phase latch on
|
|
11
|
+
the first `pointerdown` / `keydown` / `touchstart` of `activationTarget` (new option, default `globalThis`).
|
|
12
|
+
- The enable-confirmation tick follows the same gate. Disabled and unsupported stay silent no-ops.
|
|
13
|
+
- New exports: `ActivationTarget` type, `USER_ACTIVATION_EVENTS`.
|
|
14
|
+
|
|
15
|
+
## 1.3.0
|
|
16
|
+
|
|
17
|
+
Standard Gamepad action poller (first consumer: NextGame console-dashboard UI).
|
|
18
|
+
|
|
19
|
+
- `stepPad(prev, next, repeat, now)` — pure per-frame reducer: face/shoulder buttons fire on the
|
|
20
|
+
rising edge by W3C Standard POSITION; D-pad or left stick (0.5 dead-zone, dominant axis) fires
|
|
21
|
+
once, repeats after 380 ms, then every 110 ms; release resets the repeat.
|
|
22
|
+
- `createGamepadPoller({ getPads, requestFrame, cancelFrame, onAction })` — injectable rAF loop over
|
|
23
|
+
the first connected pad; a disappearing pad resets state; `stop()` is safe from inside `onAction`.
|
|
24
|
+
- `PAD_ACTIONS`, `STANDARD_BUTTON`, `IDLE_REPEAT` + types `PadAction`, `PadSnapshot`, `RepeatState`,
|
|
25
|
+
`PadLike`, `GamepadPollerOptions`.
|
|
26
|
+
|
|
27
|
+
## 1.0.0
|
|
28
|
+
|
|
29
|
+
Initial release — the haptics primitive of the C4 games shared kit.
|
|
30
|
+
|
|
31
|
+
- `createHaptics(options)` — player-toggleable, always-safe Web Vibration API wrapper
|
|
32
|
+
(localStorage-gated, structural/injectable `navigator` + `storage`, never throws into the game loop).
|
|
33
|
+
- `HAPTIC_PATTERNS` — shared semantic vibration vocabulary (tap/select/lock/bump/navigate/success/error/loss/reveal).
|
|
34
|
+
- Zero runtime dependencies; isomorphic; dual CJS/ESM; 100% test coverage.
|
|
35
|
+
|
|
36
|
+
Extracted from Aurora's `src/input/haptics.ts`. First consumers: Aurora (re-export shim) and Morphe (new adoption).
|
package/README.md
CHANGED
|
@@ -1,67 +1,76 @@
|
|
|
1
|
-
# @dloizides/game-input
|
|
2
|
-
|
|
3
|
-
Framework-agnostic game input primitives. **v1 ships safe, player-toggleable haptics** — a thin wrapper
|
|
4
|
-
over the Web Vibration API that no-ops silently when disabled, unsupported, or blocked, so call sites can
|
|
5
|
-
fire haptics unconditionally alongside their SFX/visual feedback.
|
|
6
|
-
|
|
7
|
-
Zero runtime dependencies. Isomorphic (no DOM lib required — `navigator` and storage are structural and
|
|
8
|
-
injectable), so it works in browser games, React Native shells, and tests alike.
|
|
9
|
-
|
|
10
|
-
> **Scope note:** keyboard/gamepad normalization is intentionally *not* in v1. The games that were audited
|
|
11
|
-
> (Aurora, Morphe) implemented input with fundamentally different designs — there is no shared logic to
|
|
12
|
-
> extract yet. It will be added when a genuine 2nd same-stack consumer appears (extract-on-2nd-use).
|
|
13
|
-
|
|
14
|
-
## Install
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npm install @dloizides/game-input
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## Usage
|
|
21
|
-
|
|
22
|
-
```ts
|
|
23
|
-
import { createHaptics, HAPTIC_PATTERNS } from '@dloizides/game-input';
|
|
24
|
-
|
|
25
|
-
// One controller per game; the storage key namespaces the player's preference.
|
|
26
|
-
const haptics = createHaptics({ storageKey: 'aurora.haptics' });
|
|
27
|
-
|
|
28
|
-
// Fire unconditionally — disabled/unsupported just no-ops.
|
|
29
|
-
haptics.vibrate(HAPTIC_PATTERNS.tap); // menu navigation / selection
|
|
30
|
-
haptics.vibrate(HAPTIC_PATTERNS.success); // level cleared
|
|
31
|
-
haptics.vibrate([0, 14, 24, 14]); // or your own pattern
|
|
32
|
-
|
|
33
|
-
// Settings toggle — returns the new state, fires a confirming tick when enabling.
|
|
34
|
-
function onHapticsToggle(): boolean {
|
|
35
|
-
return haptics.toggle();
|
|
36
|
-
}
|
|
37
|
-
haptics.isEnabled();
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
### Semantic pattern vocabulary
|
|
41
|
-
|
|
42
|
-
`HAPTIC_PATTERNS` is a shared set of tuned vibration patterns keyed by intent, so every game uses the same
|
|
43
|
-
feel instead of re-picking magic durations:
|
|
44
|
-
|
|
45
|
-
| Intent | Pattern | Use |
|
|
46
|
-
|---|---|---|
|
|
47
|
-
| `tap` | `10` | menu nav / select / undo / restart |
|
|
48
|
-
| `select` | `12` | pick up a tool/charm/item |
|
|
49
|
-
| `lock` | `18` | something locks into place |
|
|
50
|
-
| `bump` | `24` | action blocked |
|
|
51
|
-
| `navigate` | `16` | portal / page turn |
|
|
52
|
-
| `success` | `[0,40,50,90]` | level/chamber cleared |
|
|
53
|
-
| `error` | `[0,14,24,14]` | fatal/destructive event |
|
|
54
|
-
| `loss` | `[0,40,50,40]` | player loses / falls |
|
|
55
|
-
| `reveal` | `[0,14,20,18,26,24]` | puzzle resolves / exit opens |
|
|
56
|
-
|
|
57
|
-
## API
|
|
58
|
-
|
|
59
|
-
- `createHaptics(options?)` → `Haptics` — `{ isEnabled, setEnabled, toggle, vibrate }`.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
- `options.
|
|
63
|
-
- `
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
1
|
+
# @dloizides/game-input
|
|
2
|
+
|
|
3
|
+
Framework-agnostic game input primitives. **v1 ships safe, player-toggleable haptics** — a thin wrapper
|
|
4
|
+
over the Web Vibration API that no-ops silently when disabled, unsupported, or blocked, so call sites can
|
|
5
|
+
fire haptics unconditionally alongside their SFX/visual feedback.
|
|
6
|
+
|
|
7
|
+
Zero runtime dependencies. Isomorphic (no DOM lib required — `navigator` and storage are structural and
|
|
8
|
+
injectable), so it works in browser games, React Native shells, and tests alike.
|
|
9
|
+
|
|
10
|
+
> **Scope note:** keyboard/gamepad normalization is intentionally *not* in v1. The games that were audited
|
|
11
|
+
> (Aurora, Morphe) implemented input with fundamentally different designs — there is no shared logic to
|
|
12
|
+
> extract yet. It will be added when a genuine 2nd same-stack consumer appears (extract-on-2nd-use).
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @dloizides/game-input
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { createHaptics, HAPTIC_PATTERNS } from '@dloizides/game-input';
|
|
24
|
+
|
|
25
|
+
// One controller per game; the storage key namespaces the player's preference.
|
|
26
|
+
const haptics = createHaptics({ storageKey: 'aurora.haptics' });
|
|
27
|
+
|
|
28
|
+
// Fire unconditionally — disabled/unsupported just no-ops.
|
|
29
|
+
haptics.vibrate(HAPTIC_PATTERNS.tap); // menu navigation / selection
|
|
30
|
+
haptics.vibrate(HAPTIC_PATTERNS.success); // level cleared
|
|
31
|
+
haptics.vibrate([0, 14, 24, 14]); // or your own pattern
|
|
32
|
+
|
|
33
|
+
// Settings toggle — returns the new state, fires a confirming tick when enabling.
|
|
34
|
+
function onHapticsToggle(): boolean {
|
|
35
|
+
return haptics.toggle();
|
|
36
|
+
}
|
|
37
|
+
haptics.isEnabled();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Semantic pattern vocabulary
|
|
41
|
+
|
|
42
|
+
`HAPTIC_PATTERNS` is a shared set of tuned vibration patterns keyed by intent, so every game uses the same
|
|
43
|
+
feel instead of re-picking magic durations:
|
|
44
|
+
|
|
45
|
+
| Intent | Pattern | Use |
|
|
46
|
+
|---|---|---|
|
|
47
|
+
| `tap` | `10` | menu nav / select / undo / restart |
|
|
48
|
+
| `select` | `12` | pick up a tool/charm/item |
|
|
49
|
+
| `lock` | `18` | something locks into place |
|
|
50
|
+
| `bump` | `24` | action blocked |
|
|
51
|
+
| `navigate` | `16` | portal / page turn |
|
|
52
|
+
| `success` | `[0,40,50,90]` | level/chamber cleared |
|
|
53
|
+
| `error` | `[0,14,24,14]` | fatal/destructive event |
|
|
54
|
+
| `loss` | `[0,40,50,40]` | player loses / falls |
|
|
55
|
+
| `reveal` | `[0,14,20,18,26,24]` | puzzle resolves / exit opens |
|
|
56
|
+
|
|
57
|
+
## API
|
|
58
|
+
|
|
59
|
+
- `createHaptics(options?)` → `Haptics` — `{ isEnabled, setEnabled, toggle, vibrate }`. Nothing vibrates
|
|
60
|
+
before a user gesture: `navigator.userActivation.hasBeenActive` where present, else the first
|
|
61
|
+
`pointerdown` / `keydown` / `touchstart` on `options.activationTarget` (default `globalThis`).
|
|
62
|
+
- `options.storageKey` (default `game.haptics`) — localStorage key for the on/off preference.
|
|
63
|
+
- `options.enableConfirmation` (default a `15`ms tick; pass `null` to disable) — fired when turning ON.
|
|
64
|
+
- `options.navigator` / `options.storage` — inject for tests/RN; default to `globalThis`.
|
|
65
|
+
- `HAPTIC_PATTERNS`, `DEFAULT_HAPTICS_STORAGE_KEY`, `DEFAULT_ENABLE_CONFIRMATION`.
|
|
66
|
+
- `createGamepadPoller({ getPads, requestFrame, cancelFrame, onAction })` → `{ start, stop }` — polls the
|
|
67
|
+
first connected W3C Standard Gamepad each frame and calls `onAction(PadAction)`. Pass
|
|
68
|
+
`() => navigator.getGamepads()`, `requestAnimationFrame`, `cancelAnimationFrame` in the browser.
|
|
69
|
+
- Buttons by POSITION: south → `confirm`, east → `back`, west → `view`, north → `mood`,
|
|
70
|
+
L/R shoulder → `pagePrev`/`pageNext` (rising edge only).
|
|
71
|
+
- D-pad or left stick (0.5 dead-zone) → `up`/`down`/`left`/`right`: fires once, repeats after 380 ms, then every 110 ms.
|
|
72
|
+
- `stepPad(prev, next, repeat, now)` — the pure per-frame reducer behind the poller; `IDLE_REPEAT`, `PAD_ACTIONS`, `STANDARD_BUTTON`.
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
+
/** The semantic actions a Standard Gamepad produces, independent of the device's labels. */
|
|
2
|
+
declare const PAD_ACTIONS: {
|
|
3
|
+
readonly up: "up";
|
|
4
|
+
readonly down: "down";
|
|
5
|
+
readonly left: "left";
|
|
6
|
+
readonly right: "right";
|
|
7
|
+
readonly confirm: "confirm";
|
|
8
|
+
readonly back: "back";
|
|
9
|
+
readonly mood: "mood";
|
|
10
|
+
readonly view: "view";
|
|
11
|
+
readonly pagePrev: "pagePrev";
|
|
12
|
+
readonly pageNext: "pageNext";
|
|
13
|
+
};
|
|
14
|
+
type PadAction = (typeof PAD_ACTIONS)[keyof typeof PAD_ACTIONS];
|
|
15
|
+
/** W3C Standard Gamepad button indices, named by POSITION (Nintendo swaps A/B labels). */
|
|
16
|
+
declare const STANDARD_BUTTON: {
|
|
17
|
+
readonly south: 0;
|
|
18
|
+
readonly east: 1;
|
|
19
|
+
readonly west: 2;
|
|
20
|
+
readonly north: 3;
|
|
21
|
+
readonly leftShoulder: 4;
|
|
22
|
+
readonly rightShoulder: 5;
|
|
23
|
+
readonly dpadUp: 12;
|
|
24
|
+
readonly dpadDown: 13;
|
|
25
|
+
readonly dpadLeft: 14;
|
|
26
|
+
readonly dpadRight: 15;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** The structural subset of the DOM `Gamepad` the poller reads. */
|
|
30
|
+
interface PadLike {
|
|
31
|
+
connected: boolean;
|
|
32
|
+
buttons: ReadonlyArray<{
|
|
33
|
+
pressed: boolean;
|
|
34
|
+
}>;
|
|
35
|
+
axes: readonly number[];
|
|
36
|
+
}
|
|
37
|
+
interface GamepadPollerOptions {
|
|
38
|
+
/** Usually `() => navigator.getGamepads()`. */
|
|
39
|
+
getPads: () => ReadonlyArray<PadLike | null>;
|
|
40
|
+
/** Usually `requestAnimationFrame`. */
|
|
41
|
+
requestFrame: (cb: (now: number) => void) => number;
|
|
42
|
+
/** Usually `cancelAnimationFrame`. */
|
|
43
|
+
cancelFrame: (id: number) => void;
|
|
44
|
+
onAction: (action: PadAction) => void;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* rAF-driven poller over the first connected pad. Starts idle; call start().
|
|
48
|
+
* Safe to stop() (or stop() then start()) from inside `onAction`.
|
|
49
|
+
*/
|
|
50
|
+
declare function createGamepadPoller(opts: GamepadPollerOptions): {
|
|
51
|
+
start: () => void;
|
|
52
|
+
stop: () => void;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** One frame of pad state: pressed flags by Standard button index, and raw axes. */
|
|
56
|
+
interface PadSnapshot {
|
|
57
|
+
buttons: readonly boolean[];
|
|
58
|
+
axes: readonly number[];
|
|
59
|
+
}
|
|
60
|
+
/** Which direction is held, when it started, and when it last fired. */
|
|
61
|
+
interface RepeatState {
|
|
62
|
+
held: PadAction | null;
|
|
63
|
+
since: number;
|
|
64
|
+
lastFire: number;
|
|
65
|
+
}
|
|
66
|
+
declare const IDLE_REPEAT: RepeatState;
|
|
67
|
+
/** One poll frame → the actions to dispatch and the next repeat state. Pure. */
|
|
68
|
+
declare function stepPad(prev: PadSnapshot | null, next: PadSnapshot, repeat: RepeatState, now: number): {
|
|
69
|
+
actions: PadAction[];
|
|
70
|
+
repeat: RepeatState;
|
|
71
|
+
};
|
|
72
|
+
|
|
1
73
|
/**
|
|
2
74
|
* A vibration pattern: a single duration in ms, or an on/off pattern
|
|
3
75
|
* (`[wait, on, off, on, ...]`) exactly as the Web Vibration API expects.
|
|
@@ -9,6 +81,14 @@ type VibrationPattern = number | number[];
|
|
|
9
81
|
*/
|
|
10
82
|
interface VibrationCapable {
|
|
11
83
|
vibrate?: (pattern: VibrationPattern) => boolean;
|
|
84
|
+
userActivation?: {
|
|
85
|
+
readonly hasBeenActive: boolean;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** Event source whose first pointer/key/touch gesture unlocks haptics. Defaults to `globalThis`. */
|
|
89
|
+
interface ActivationTarget {
|
|
90
|
+
addEventListener(type: string, listener: () => void, capture?: boolean): void;
|
|
91
|
+
removeEventListener(type: string, listener: () => void, capture?: boolean): void;
|
|
12
92
|
}
|
|
13
93
|
/**
|
|
14
94
|
* The slice of `localStorage` haptics needs to persist the player's toggle.
|
|
@@ -31,6 +111,8 @@ interface HapticsOptions {
|
|
|
31
111
|
navigator?: VibrationCapable;
|
|
32
112
|
/** Preference store. Defaults to `globalThis.localStorage`. Inject for tests. */
|
|
33
113
|
storage?: HapticsStorage;
|
|
114
|
+
/** Gesture source used when `navigator.userActivation` is unavailable. Defaults to `globalThis`. */
|
|
115
|
+
activationTarget?: ActivationTarget;
|
|
34
116
|
}
|
|
35
117
|
/**
|
|
36
118
|
* A player-toggleable, always-safe haptics controller. Every call no-ops silently
|
|
@@ -49,11 +131,8 @@ interface Haptics {
|
|
|
49
131
|
}
|
|
50
132
|
|
|
51
133
|
/**
|
|
52
|
-
* Create a player-toggleable, always-safe haptics controller
|
|
53
|
-
*
|
|
54
|
-
* The on/off preference is read from (and persisted to) the configured storage key
|
|
55
|
-
* so it survives reloads. Every vibration routes through one gated call that no-ops
|
|
56
|
-
* when disabled, when no Vibration API is present, or if the platform blocks it.
|
|
134
|
+
* Create a player-toggleable, always-safe haptics controller: it never vibrates before a
|
|
135
|
+
* user gesture, while disabled, or when the Vibration API is missing or blocked.
|
|
57
136
|
*/
|
|
58
137
|
declare function createHaptics(options?: HapticsOptions): Haptics;
|
|
59
138
|
|
|
@@ -61,6 +140,8 @@ declare function createHaptics(options?: HapticsOptions): Haptics;
|
|
|
61
140
|
declare const DEFAULT_HAPTICS_STORAGE_KEY = "game.haptics";
|
|
62
141
|
/** Default confirmation tick fired when haptics are turned ON (ms). */
|
|
63
142
|
declare const DEFAULT_ENABLE_CONFIRMATION = 15;
|
|
143
|
+
/** DOM events that count as a user activation before `navigator.userActivation` is available. */
|
|
144
|
+
declare const USER_ACTIVATION_EVENTS: readonly ["pointerdown", "keydown", "touchstart"];
|
|
64
145
|
/** The semantic intents games map their events onto. */
|
|
65
146
|
type HapticIntent = 'tap' | 'select' | 'lock' | 'bump' | 'navigate' | 'success' | 'error' | 'loss' | 'reveal';
|
|
66
147
|
/**
|
|
@@ -70,4 +151,4 @@ type HapticIntent = 'tap' | 'select' | 'lock' | 'bump' | 'navigate' | 'success'
|
|
|
70
151
|
*/
|
|
71
152
|
declare const HAPTIC_PATTERNS: Record<HapticIntent, VibrationPattern>;
|
|
72
153
|
|
|
73
|
-
export { DEFAULT_ENABLE_CONFIRMATION, DEFAULT_HAPTICS_STORAGE_KEY, HAPTIC_PATTERNS, type HapticIntent, type Haptics, type HapticsOptions, type HapticsStorage, type VibrationCapable, type VibrationPattern, createHaptics };
|
|
154
|
+
export { type ActivationTarget, DEFAULT_ENABLE_CONFIRMATION, DEFAULT_HAPTICS_STORAGE_KEY, type GamepadPollerOptions, HAPTIC_PATTERNS, type HapticIntent, type Haptics, type HapticsOptions, type HapticsStorage, IDLE_REPEAT, PAD_ACTIONS, type PadAction, type PadLike, type PadSnapshot, type RepeatState, STANDARD_BUTTON, USER_ACTIVATION_EVENTS, type VibrationCapable, type VibrationPattern, createGamepadPoller, createHaptics, stepPad };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
+
/** The semantic actions a Standard Gamepad produces, independent of the device's labels. */
|
|
2
|
+
declare const PAD_ACTIONS: {
|
|
3
|
+
readonly up: "up";
|
|
4
|
+
readonly down: "down";
|
|
5
|
+
readonly left: "left";
|
|
6
|
+
readonly right: "right";
|
|
7
|
+
readonly confirm: "confirm";
|
|
8
|
+
readonly back: "back";
|
|
9
|
+
readonly mood: "mood";
|
|
10
|
+
readonly view: "view";
|
|
11
|
+
readonly pagePrev: "pagePrev";
|
|
12
|
+
readonly pageNext: "pageNext";
|
|
13
|
+
};
|
|
14
|
+
type PadAction = (typeof PAD_ACTIONS)[keyof typeof PAD_ACTIONS];
|
|
15
|
+
/** W3C Standard Gamepad button indices, named by POSITION (Nintendo swaps A/B labels). */
|
|
16
|
+
declare const STANDARD_BUTTON: {
|
|
17
|
+
readonly south: 0;
|
|
18
|
+
readonly east: 1;
|
|
19
|
+
readonly west: 2;
|
|
20
|
+
readonly north: 3;
|
|
21
|
+
readonly leftShoulder: 4;
|
|
22
|
+
readonly rightShoulder: 5;
|
|
23
|
+
readonly dpadUp: 12;
|
|
24
|
+
readonly dpadDown: 13;
|
|
25
|
+
readonly dpadLeft: 14;
|
|
26
|
+
readonly dpadRight: 15;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** The structural subset of the DOM `Gamepad` the poller reads. */
|
|
30
|
+
interface PadLike {
|
|
31
|
+
connected: boolean;
|
|
32
|
+
buttons: ReadonlyArray<{
|
|
33
|
+
pressed: boolean;
|
|
34
|
+
}>;
|
|
35
|
+
axes: readonly number[];
|
|
36
|
+
}
|
|
37
|
+
interface GamepadPollerOptions {
|
|
38
|
+
/** Usually `() => navigator.getGamepads()`. */
|
|
39
|
+
getPads: () => ReadonlyArray<PadLike | null>;
|
|
40
|
+
/** Usually `requestAnimationFrame`. */
|
|
41
|
+
requestFrame: (cb: (now: number) => void) => number;
|
|
42
|
+
/** Usually `cancelAnimationFrame`. */
|
|
43
|
+
cancelFrame: (id: number) => void;
|
|
44
|
+
onAction: (action: PadAction) => void;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* rAF-driven poller over the first connected pad. Starts idle; call start().
|
|
48
|
+
* Safe to stop() (or stop() then start()) from inside `onAction`.
|
|
49
|
+
*/
|
|
50
|
+
declare function createGamepadPoller(opts: GamepadPollerOptions): {
|
|
51
|
+
start: () => void;
|
|
52
|
+
stop: () => void;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** One frame of pad state: pressed flags by Standard button index, and raw axes. */
|
|
56
|
+
interface PadSnapshot {
|
|
57
|
+
buttons: readonly boolean[];
|
|
58
|
+
axes: readonly number[];
|
|
59
|
+
}
|
|
60
|
+
/** Which direction is held, when it started, and when it last fired. */
|
|
61
|
+
interface RepeatState {
|
|
62
|
+
held: PadAction | null;
|
|
63
|
+
since: number;
|
|
64
|
+
lastFire: number;
|
|
65
|
+
}
|
|
66
|
+
declare const IDLE_REPEAT: RepeatState;
|
|
67
|
+
/** One poll frame → the actions to dispatch and the next repeat state. Pure. */
|
|
68
|
+
declare function stepPad(prev: PadSnapshot | null, next: PadSnapshot, repeat: RepeatState, now: number): {
|
|
69
|
+
actions: PadAction[];
|
|
70
|
+
repeat: RepeatState;
|
|
71
|
+
};
|
|
72
|
+
|
|
1
73
|
/**
|
|
2
74
|
* A vibration pattern: a single duration in ms, or an on/off pattern
|
|
3
75
|
* (`[wait, on, off, on, ...]`) exactly as the Web Vibration API expects.
|
|
@@ -9,6 +81,14 @@ type VibrationPattern = number | number[];
|
|
|
9
81
|
*/
|
|
10
82
|
interface VibrationCapable {
|
|
11
83
|
vibrate?: (pattern: VibrationPattern) => boolean;
|
|
84
|
+
userActivation?: {
|
|
85
|
+
readonly hasBeenActive: boolean;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** Event source whose first pointer/key/touch gesture unlocks haptics. Defaults to `globalThis`. */
|
|
89
|
+
interface ActivationTarget {
|
|
90
|
+
addEventListener(type: string, listener: () => void, capture?: boolean): void;
|
|
91
|
+
removeEventListener(type: string, listener: () => void, capture?: boolean): void;
|
|
12
92
|
}
|
|
13
93
|
/**
|
|
14
94
|
* The slice of `localStorage` haptics needs to persist the player's toggle.
|
|
@@ -31,6 +111,8 @@ interface HapticsOptions {
|
|
|
31
111
|
navigator?: VibrationCapable;
|
|
32
112
|
/** Preference store. Defaults to `globalThis.localStorage`. Inject for tests. */
|
|
33
113
|
storage?: HapticsStorage;
|
|
114
|
+
/** Gesture source used when `navigator.userActivation` is unavailable. Defaults to `globalThis`. */
|
|
115
|
+
activationTarget?: ActivationTarget;
|
|
34
116
|
}
|
|
35
117
|
/**
|
|
36
118
|
* A player-toggleable, always-safe haptics controller. Every call no-ops silently
|
|
@@ -49,11 +131,8 @@ interface Haptics {
|
|
|
49
131
|
}
|
|
50
132
|
|
|
51
133
|
/**
|
|
52
|
-
* Create a player-toggleable, always-safe haptics controller
|
|
53
|
-
*
|
|
54
|
-
* The on/off preference is read from (and persisted to) the configured storage key
|
|
55
|
-
* so it survives reloads. Every vibration routes through one gated call that no-ops
|
|
56
|
-
* when disabled, when no Vibration API is present, or if the platform blocks it.
|
|
134
|
+
* Create a player-toggleable, always-safe haptics controller: it never vibrates before a
|
|
135
|
+
* user gesture, while disabled, or when the Vibration API is missing or blocked.
|
|
57
136
|
*/
|
|
58
137
|
declare function createHaptics(options?: HapticsOptions): Haptics;
|
|
59
138
|
|
|
@@ -61,6 +140,8 @@ declare function createHaptics(options?: HapticsOptions): Haptics;
|
|
|
61
140
|
declare const DEFAULT_HAPTICS_STORAGE_KEY = "game.haptics";
|
|
62
141
|
/** Default confirmation tick fired when haptics are turned ON (ms). */
|
|
63
142
|
declare const DEFAULT_ENABLE_CONFIRMATION = 15;
|
|
143
|
+
/** DOM events that count as a user activation before `navigator.userActivation` is available. */
|
|
144
|
+
declare const USER_ACTIVATION_EVENTS: readonly ["pointerdown", "keydown", "touchstart"];
|
|
64
145
|
/** The semantic intents games map their events onto. */
|
|
65
146
|
type HapticIntent = 'tap' | 'select' | 'lock' | 'bump' | 'navigate' | 'success' | 'error' | 'loss' | 'reveal';
|
|
66
147
|
/**
|
|
@@ -70,4 +151,4 @@ type HapticIntent = 'tap' | 'select' | 'lock' | 'bump' | 'navigate' | 'success'
|
|
|
70
151
|
*/
|
|
71
152
|
declare const HAPTIC_PATTERNS: Record<HapticIntent, VibrationPattern>;
|
|
72
153
|
|
|
73
|
-
export { DEFAULT_ENABLE_CONFIRMATION, DEFAULT_HAPTICS_STORAGE_KEY, HAPTIC_PATTERNS, type HapticIntent, type Haptics, type HapticsOptions, type HapticsStorage, type VibrationCapable, type VibrationPattern, createHaptics };
|
|
154
|
+
export { type ActivationTarget, DEFAULT_ENABLE_CONFIRMATION, DEFAULT_HAPTICS_STORAGE_KEY, type GamepadPollerOptions, HAPTIC_PATTERNS, type HapticIntent, type Haptics, type HapticsOptions, type HapticsStorage, IDLE_REPEAT, PAD_ACTIONS, type PadAction, type PadLike, type PadSnapshot, type RepeatState, STANDARD_BUTTON, USER_ACTIVATION_EVENTS, type VibrationCapable, type VibrationPattern, createGamepadPoller, createHaptics, stepPad };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,133 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
// src/gamepad/padMap.ts
|
|
4
|
+
var PAD_ACTIONS = {
|
|
5
|
+
up: "up",
|
|
6
|
+
down: "down",
|
|
7
|
+
left: "left",
|
|
8
|
+
right: "right",
|
|
9
|
+
confirm: "confirm",
|
|
10
|
+
back: "back",
|
|
11
|
+
mood: "mood",
|
|
12
|
+
view: "view",
|
|
13
|
+
pagePrev: "pagePrev",
|
|
14
|
+
pageNext: "pageNext"
|
|
15
|
+
};
|
|
16
|
+
var STANDARD_BUTTON = {
|
|
17
|
+
south: 0,
|
|
18
|
+
east: 1,
|
|
19
|
+
west: 2,
|
|
20
|
+
north: 3,
|
|
21
|
+
leftShoulder: 4,
|
|
22
|
+
rightShoulder: 5,
|
|
23
|
+
dpadUp: 12,
|
|
24
|
+
dpadDown: 13,
|
|
25
|
+
dpadLeft: 14,
|
|
26
|
+
dpadRight: 15
|
|
27
|
+
};
|
|
28
|
+
var FACE_BUTTON_ACTIONS = [
|
|
29
|
+
[STANDARD_BUTTON.south, PAD_ACTIONS.confirm],
|
|
30
|
+
[STANDARD_BUTTON.east, PAD_ACTIONS.back],
|
|
31
|
+
[STANDARD_BUTTON.west, PAD_ACTIONS.view],
|
|
32
|
+
[STANDARD_BUTTON.north, PAD_ACTIONS.mood],
|
|
33
|
+
[STANDARD_BUTTON.leftShoulder, PAD_ACTIONS.pagePrev],
|
|
34
|
+
[STANDARD_BUTTON.rightShoulder, PAD_ACTIONS.pageNext]
|
|
35
|
+
];
|
|
36
|
+
var DPAD_ACTIONS = [
|
|
37
|
+
[STANDARD_BUTTON.dpadUp, PAD_ACTIONS.up],
|
|
38
|
+
[STANDARD_BUTTON.dpadDown, PAD_ACTIONS.down],
|
|
39
|
+
[STANDARD_BUTTON.dpadLeft, PAD_ACTIONS.left],
|
|
40
|
+
[STANDARD_BUTTON.dpadRight, PAD_ACTIONS.right]
|
|
41
|
+
];
|
|
42
|
+
var STICK_DEAD_ZONE = 0.5;
|
|
43
|
+
var REPEAT_DELAY_MS = 380;
|
|
44
|
+
var REPEAT_INTERVAL_MS = 110;
|
|
45
|
+
|
|
46
|
+
// src/gamepad/stepPad.ts
|
|
47
|
+
var IDLE_REPEAT = { held: null, since: 0, lastFire: 0 };
|
|
48
|
+
var STICK_X = 0;
|
|
49
|
+
var STICK_Y = 1;
|
|
50
|
+
function stickDirection(axes) {
|
|
51
|
+
const x = axes[STICK_X] ?? 0;
|
|
52
|
+
const y = axes[STICK_Y] ?? 0;
|
|
53
|
+
if (Math.max(Math.abs(x), Math.abs(y)) < STICK_DEAD_ZONE) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
if (Math.abs(x) > Math.abs(y)) {
|
|
57
|
+
return x > 0 ? PAD_ACTIONS.right : PAD_ACTIONS.left;
|
|
58
|
+
}
|
|
59
|
+
return y > 0 ? PAD_ACTIONS.down : PAD_ACTIONS.up;
|
|
60
|
+
}
|
|
61
|
+
function heldDirection(snapshot) {
|
|
62
|
+
const dpad = DPAD_ACTIONS.find(([index]) => snapshot.buttons[index] === true);
|
|
63
|
+
return dpad === void 0 ? stickDirection(snapshot.axes) : dpad[1];
|
|
64
|
+
}
|
|
65
|
+
function stepPad(prev, next, repeat, now) {
|
|
66
|
+
const actions = FACE_BUTTON_ACTIONS.filter(([index]) => next.buttons[index] === true && prev?.buttons[index] !== true).map(([, action]) => action);
|
|
67
|
+
const direction = heldDirection(next);
|
|
68
|
+
if (direction === null) {
|
|
69
|
+
return { actions, repeat: IDLE_REPEAT };
|
|
70
|
+
}
|
|
71
|
+
if (direction !== repeat.held) {
|
|
72
|
+
return { actions: [...actions, direction], repeat: { held: direction, since: now, lastFire: now } };
|
|
73
|
+
}
|
|
74
|
+
const firstRepeat = repeat.lastFire === repeat.since;
|
|
75
|
+
const wait = firstRepeat ? REPEAT_DELAY_MS : REPEAT_INTERVAL_MS;
|
|
76
|
+
if (now - repeat.lastFire < wait) {
|
|
77
|
+
return { actions, repeat };
|
|
78
|
+
}
|
|
79
|
+
return { actions: [...actions, direction], repeat: { ...repeat, lastFire: now } };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/gamepad/createGamepadPoller.ts
|
|
83
|
+
function createGamepadPoller(opts) {
|
|
84
|
+
let prev = null;
|
|
85
|
+
let repeat = IDLE_REPEAT;
|
|
86
|
+
let running = false;
|
|
87
|
+
let frame = null;
|
|
88
|
+
const tick = (now) => {
|
|
89
|
+
frame = null;
|
|
90
|
+
const pad = opts.getPads().find((p) => p !== null && p.connected);
|
|
91
|
+
if (pad === void 0) {
|
|
92
|
+
prev = null;
|
|
93
|
+
repeat = IDLE_REPEAT;
|
|
94
|
+
} else {
|
|
95
|
+
const snapshot = { buttons: pad.buttons.map((b) => b.pressed), axes: pad.axes };
|
|
96
|
+
const step = stepPad(prev, snapshot, repeat, now);
|
|
97
|
+
prev = snapshot;
|
|
98
|
+
repeat = step.repeat;
|
|
99
|
+
step.actions.forEach((action) => opts.onAction(action));
|
|
100
|
+
}
|
|
101
|
+
if (running && frame === null) {
|
|
102
|
+
frame = opts.requestFrame(tick);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
start: () => {
|
|
107
|
+
if (running) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
running = true;
|
|
111
|
+
if (frame === null) {
|
|
112
|
+
frame = opts.requestFrame(tick);
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
stop: () => {
|
|
116
|
+
running = false;
|
|
117
|
+
if (frame !== null) {
|
|
118
|
+
opts.cancelFrame(frame);
|
|
119
|
+
frame = null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
3
125
|
// src/constants.ts
|
|
4
126
|
var DEFAULT_HAPTICS_STORAGE_KEY = "game.haptics";
|
|
5
127
|
var DEFAULT_ENABLE_CONFIRMATION = 15;
|
|
6
128
|
var DISABLED_VALUE = "0";
|
|
7
129
|
var ENABLED_VALUE = "1";
|
|
130
|
+
var USER_ACTIVATION_EVENTS = ["pointerdown", "keydown", "touchstart"];
|
|
8
131
|
var HAPTIC_PATTERNS = {
|
|
9
132
|
/** Light tick for menu navigation / selection / undo / restart. */
|
|
10
133
|
tap: 10,
|
|
@@ -26,6 +149,29 @@ var HAPTIC_PATTERNS = {
|
|
|
26
149
|
reveal: [0, 14, 20, 18, 26, 24]
|
|
27
150
|
};
|
|
28
151
|
|
|
152
|
+
// src/userActivation.ts
|
|
153
|
+
function resolveActivationTarget(explicit) {
|
|
154
|
+
if (explicit) {
|
|
155
|
+
return explicit;
|
|
156
|
+
}
|
|
157
|
+
const candidate = globalThis;
|
|
158
|
+
return typeof candidate.addEventListener === "function" ? candidate : void 0;
|
|
159
|
+
}
|
|
160
|
+
function createActivationProbe(navigator, explicitTarget) {
|
|
161
|
+
let latched = false;
|
|
162
|
+
const target = typeof navigator?.vibrate === "function" ? resolveActivationTarget(explicitTarget) : void 0;
|
|
163
|
+
const onGesture = () => {
|
|
164
|
+
latched = true;
|
|
165
|
+
for (const type of USER_ACTIVATION_EVENTS) {
|
|
166
|
+
target?.removeEventListener(type, onGesture, true);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
for (const type of USER_ACTIVATION_EVENTS) {
|
|
170
|
+
target?.addEventListener(type, onGesture, true);
|
|
171
|
+
}
|
|
172
|
+
return () => latched || navigator?.userActivation?.hasBeenActive === true;
|
|
173
|
+
}
|
|
174
|
+
|
|
29
175
|
// src/haptics.ts
|
|
30
176
|
function resolveNavigator(explicit) {
|
|
31
177
|
if (explicit) {
|
|
@@ -44,6 +190,7 @@ function createHaptics(options = {}) {
|
|
|
44
190
|
const confirmation = options.enableConfirmation === void 0 ? DEFAULT_ENABLE_CONFIRMATION : options.enableConfirmation;
|
|
45
191
|
const navigator = resolveNavigator(options.navigator);
|
|
46
192
|
const storage = resolveStorage(options.storage);
|
|
193
|
+
const hasUserActivation = createActivationProbe(navigator, options.activationTarget);
|
|
47
194
|
const readPref = () => {
|
|
48
195
|
try {
|
|
49
196
|
return storage?.getItem(storageKey) !== DISABLED_VALUE;
|
|
@@ -53,18 +200,20 @@ function createHaptics(options = {}) {
|
|
|
53
200
|
};
|
|
54
201
|
let enabled = readPref();
|
|
55
202
|
const vibrate = (pattern) => {
|
|
56
|
-
if (!enabled) {
|
|
203
|
+
if (!enabled || !hasUserActivation()) {
|
|
57
204
|
return;
|
|
58
205
|
}
|
|
59
206
|
try {
|
|
60
207
|
navigator?.vibrate?.(pattern);
|
|
61
208
|
} catch {
|
|
209
|
+
return;
|
|
62
210
|
}
|
|
63
211
|
};
|
|
64
212
|
const persist = () => {
|
|
65
213
|
try {
|
|
66
214
|
storage?.setItem(storageKey, enabled ? ENABLED_VALUE : DISABLED_VALUE);
|
|
67
215
|
} catch {
|
|
216
|
+
return;
|
|
68
217
|
}
|
|
69
218
|
};
|
|
70
219
|
const setEnabled = (next) => {
|
|
@@ -86,6 +235,12 @@ function createHaptics(options = {}) {
|
|
|
86
235
|
exports.DEFAULT_ENABLE_CONFIRMATION = DEFAULT_ENABLE_CONFIRMATION;
|
|
87
236
|
exports.DEFAULT_HAPTICS_STORAGE_KEY = DEFAULT_HAPTICS_STORAGE_KEY;
|
|
88
237
|
exports.HAPTIC_PATTERNS = HAPTIC_PATTERNS;
|
|
238
|
+
exports.IDLE_REPEAT = IDLE_REPEAT;
|
|
239
|
+
exports.PAD_ACTIONS = PAD_ACTIONS;
|
|
240
|
+
exports.STANDARD_BUTTON = STANDARD_BUTTON;
|
|
241
|
+
exports.USER_ACTIVATION_EVENTS = USER_ACTIVATION_EVENTS;
|
|
242
|
+
exports.createGamepadPoller = createGamepadPoller;
|
|
89
243
|
exports.createHaptics = createHaptics;
|
|
244
|
+
exports.stepPad = stepPad;
|
|
90
245
|
//# sourceMappingURL=index.js.map
|
|
91
246
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/constants.ts","../src/haptics.ts"],"names":[],"mappings":";;;AAGO,IAAM,2BAAA,GAA8B;AAGpC,IAAM,2BAAA,GAA8B;AAGpC,IAAM,cAAA,GAAiB,GAAA;AAGvB,IAAM,aAAA,GAAgB,GAAA;AAmBtB,IAAM,eAAA,GAA0D;AAAA;AAAA,EAErE,GAAA,EAAK,EAAA;AAAA;AAAA,EAEL,MAAA,EAA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AA,OAAO,IAAA;AAAA,IACT;AAAA,EACF,CAAA;AAEA,EAAA,IAAI,UAAU,QAAA,EAAS;AAEvB,EAAA,MAAM,OAAA,GAAU,CAAC,OAAA,KAAoC;AACnD,IAAA,IAAI,CAAC,OAAA,EAAS;AACZ,MAAA;AAAA,IACF;AACA,IAAA,IAAI;AACF,MAAA,SAAA,EAAW,UAAU,OAAO,CAAA;AAAA,IAC9B,CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACF,CAAA;AAEA,EAAA,MAAM,UAAU,MAAY;AAC1B,IAAA,IAAI;AACF,MAAA,OAAA,EAAS,OAAA,CAAQ,UAAA,EAAY,OAAA,GAAU,aAAA,GAAgB,cAAc,CAAA;AAAA,IACvE,CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACF,CAAA;AAEA,EAAA,MAAM,UAAA,GAAa,CAAC,IAAA,KAA2B;AAC7C,IAAA,OAAA,GAAU,IAAA;AACV,IAAA,OAAA,EAAQ;AAER,IAAA,IAAI,OAAA,IAAW,iBAAiB,IAAA,EAAM;AACpC,MAAA,OAAA,CAAQ,YAAY,CAAA;AAAA,IACtB;AACA,IAAA,OAAO,OAAA;AAAA,EACT,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,WAAW,MAAe,OAAA;AAAA,IAC1B,UAAA;AAAA,IACA,MAAA,EAAQ,MAAe,UAAA,CAAW,CAAC,OAAO,CAAA;AAAA,IAC1C;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import type { VibrationPattern } from './types';\n\n/** Default localStorage key for the haptics on/off preference. */\nexport const DEFAULT_HAPTICS_STORAGE_KEY = 'game.haptics';\n\n/** Default confirmation tick fired when haptics are turned ON (ms). */\nexport const DEFAULT_ENABLE_CONFIRMATION = 15;\n\n/** Stored value meaning \"disabled\". Any other value (or absent) means enabled. */\nexport const DISABLED_VALUE = '0';\n\n/** Stored value meaning \"enabled\". */\nexport const ENABLED_VALUE = '1';\n\n/** The semantic intents games map their events onto. */\nexport type HapticIntent =\n | 'tap'\n | 'select'\n | 'lock'\n | 'bump'\n | 'navigate'\n | 'success'\n | 'error'\n | 'loss'\n | 'reveal';\n\n/**\n * A shared vocabulary of vibration patterns keyed by intent, so every game uses\n * the same tuned feel instead of re-picking magic durations. Pass any of these to\n * {@link Haptics.vibrate} (or supply your own pattern).\n */\nexport const HAPTIC_PATTERNS: Record<HapticIntent, VibrationPattern> = {\n /** Light tick for menu navigation / selection / undo / restart. */\n tap: 10,\n /** Light pulse when a tool/charm/item is picked up or selected. */\n select: 12,\n /** Short pulse when something locks into place. */\n lock: 18,\n /** Dull buzz when an action is blocked. */\n bump: 24,\n /** Quick pulse when stepping through a portal / page turn. */\n navigate: 16,\n /** Celebratory pattern when a level/chamber is cleared. */\n success: [0, 40, 50, 90],\n /** Sharp double crack for a fatal/destructive event. */\n error: [0, 14, 24, 14],\n /** Heavier buzz when the player loses / falls. */\n loss: [0, 40, 50, 40],\n /** Rising triple pulse the instant a puzzle resolves / an exit opens. */\n reveal: [0, 14, 20, 18, 26, 24],\n};\n","import {\n DEFAULT_ENABLE_CONFIRMATION,\n DEFAULT_HAPTICS_STORAGE_KEY,\n DISABLED_VALUE,\n ENABLED_VALUE,\n} from './constants';\nimport type { Haptics, HapticsOptions, HapticsStorage, VibrationCapable, VibrationPattern } from './types';\n\nfunction resolveNavigator(explicit?: VibrationCapable): VibrationCapable | undefined {\n if (explicit) {\n return explicit;\n }\n return (globalThis as { navigator?: VibrationCapable }).navigator;\n}\n\nfunction resolveStorage(explicit?: HapticsStorage): HapticsStorage | undefined {\n if (explicit) {\n return explicit;\n }\n return (globalThis as { localStorage?: HapticsStorage }).localStorage;\n}\n\n/**\n * Create a player-toggleable, always-safe haptics controller.\n *\n * The on/off preference is read from (and persisted to) the configured storage key\n * so it survives reloads. Every vibration routes through one gated call that no-ops\n * when disabled, when no Vibration API is present, or if the platform blocks it.\n */\nexport function createHaptics(options: HapticsOptions = {}): Haptics {\n const storageKey = options.storageKey ?? DEFAULT_HAPTICS_STORAGE_KEY;\n const confirmation =\n options.enableConfirmation === undefined ? DEFAULT_ENABLE_CONFIRMATION : options.enableConfirmation;\n const navigator = resolveNavigator(options.navigator);\n const storage = resolveStorage(options.storage);\n\n const readPref = (): boolean => {\n try {\n return storage?.getItem(storageKey) !== DISABLED_VALUE;\n } catch {\n // Reading storage can throw (blocked cookies / sandboxed iframe) — default ON.\n return true;\n }\n };\n\n let enabled = readPref();\n\n const vibrate = (pattern: VibrationPattern): void => {\n if (!enabled) {\n return;\n }\n try {\n navigator?.vibrate?.(pattern);\n } catch {\n // Vibration can throw if blocked by the platform — never let it break a move.\n }\n };\n\n const persist = (): void => {\n try {\n storage?.setItem(storageKey, enabled ? ENABLED_VALUE : DISABLED_VALUE);\n } catch {\n // Writing storage can throw (private mode / quota) — ignore.\n }\n };\n\n const setEnabled = (next: boolean): boolean => {\n enabled = next;\n persist();\n // A confirming tick when turning ON so the player feels it engage.\n if (enabled && confirmation !== null) {\n vibrate(confirmation);\n }\n return enabled;\n };\n\n return {\n isEnabled: (): boolean => enabled,\n setEnabled,\n toggle: (): boolean => setEnabled(!enabled),\n vibrate,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/gamepad/padMap.ts","../src/gamepad/stepPad.ts","../src/gamepad/createGamepadPoller.ts","../src/constants.ts","../src/userActivation.ts","../src/haptics.ts"],"names":[],"mappings":";;;AACO,IAAM,WAAA,GAAc;AAAA,EACzB,EAAA,EAAI,IAAA;AAAA,EACJ,IAAA,EAAM,MAAA;AAAA,EACN,IAAA,EAAM,MAAA;AAAA,EACN,KAAA,EAAO,OAAA;AAAA,EACP,OAAA,EAAS,SAAA;AAAA,EACT,IAAA,EAAM,MAAA;AAAA,EACN,IAAA,EAAM,MAAA;AAAA,EACN,IAAA,EAAM,MAAA;AAAA,EACN,QAAA,EAAU,UAAA;AAAA,EACV,QAAA,EAAU;AACZ;AAIO,IAAM,eAAA,GAAkB;AAAA,EAC7B,KAAA,EAAO,CAAA;AAAA,EAC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C,MAAA;AAAA,IACF;AACA,IAAA,IAAI;AACF,MAAA,SAAA,EAAW,UAAU,OAAO,CAAA;AAAA,IAC9B,CAAA,CAAA,MAAQ;AACN,MAAA;AAAA,IACF;AAAA,EACF,CAAA;AAEA,EAAA,MAAM,UAAU,MAAY;AAC1B,IAAA,IAAI;AACF,MAAA,OAAA,EAAS,OAAA,CAAQ,UAAA,EAAY,OAAA,GAAU,aAAA,GAAgB,cAAc,CAAA;AAAA,IACvE,CAAA,CAAA,MAAQ;AACN,MAAA;AAAA,IACF;AAAA,EACF,CAAA;AAEA,EAAA,MAAM,UAAA,GAAa,CAAC,IAAA,KAA2B;AAC7C,IAAA,OAAA,GAAU,IAAA;AACV,IAAA,OAAA,EAAQ;AACR,IAAA,IAAI,OAAA,IAAW,iBAAiB,IAAA,EAAM;AACpC,MAAA,OAAA,CAAQ,YAAY,CAAA;AAAA,IACtB;AACA,IAAA,OAAO,OAAA;AAAA,EACT,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,WAAW,MAAe,OAAA;AAAA,IAC1B,UAAA;AAAA,IACA,MAAA,EAAQ,MAAe,UAAA,CAAW,CAAC,OAAO,CAAA;AAAA,IAC1C;AAAA,GACF;AACF","file":"index.js","sourcesContent":["/** The semantic actions a Standard Gamepad produces, independent of the device's labels. */\nexport const PAD_ACTIONS = {\n up: 'up',\n down: 'down',\n left: 'left',\n right: 'right',\n confirm: 'confirm',\n back: 'back',\n mood: 'mood',\n view: 'view',\n pagePrev: 'pagePrev',\n pageNext: 'pageNext',\n} as const;\nexport type PadAction = (typeof PAD_ACTIONS)[keyof typeof PAD_ACTIONS];\n\n/** W3C Standard Gamepad button indices, named by POSITION (Nintendo swaps A/B labels). */\nexport const STANDARD_BUTTON = {\n south: 0,\n east: 1,\n west: 2,\n north: 3,\n leftShoulder: 4,\n rightShoulder: 5,\n dpadUp: 12,\n dpadDown: 13,\n dpadLeft: 14,\n dpadRight: 15,\n} as const;\n\n/** Edge-triggered buttons, in dispatch order. */\nexport const FACE_BUTTON_ACTIONS: ReadonlyArray<readonly [number, PadAction]> = [\n [STANDARD_BUTTON.south, PAD_ACTIONS.confirm],\n [STANDARD_BUTTON.east, PAD_ACTIONS.back],\n [STANDARD_BUTTON.west, PAD_ACTIONS.view],\n [STANDARD_BUTTON.north, PAD_ACTIONS.mood],\n [STANDARD_BUTTON.leftShoulder, PAD_ACTIONS.pagePrev],\n [STANDARD_BUTTON.rightShoulder, PAD_ACTIONS.pageNext],\n];\n\n/** Held (auto-repeating) buttons; the first pressed one wins. */\nexport const DPAD_ACTIONS: ReadonlyArray<readonly [number, PadAction]> = [\n [STANDARD_BUTTON.dpadUp, PAD_ACTIONS.up],\n [STANDARD_BUTTON.dpadDown, PAD_ACTIONS.down],\n [STANDARD_BUTTON.dpadLeft, PAD_ACTIONS.left],\n [STANDARD_BUTTON.dpadRight, PAD_ACTIONS.right],\n];\n\n/** Left-stick magnitude below which the stick counts as centred. */\nexport const STICK_DEAD_ZONE = 0.5;\n/** Delay before a held direction starts repeating (ms). */\nexport const REPEAT_DELAY_MS = 380;\n/** Interval between repeats once repeating (ms). */\nexport const REPEAT_INTERVAL_MS = 110;\n","import {\n DPAD_ACTIONS,\n FACE_BUTTON_ACTIONS,\n PAD_ACTIONS,\n REPEAT_DELAY_MS,\n REPEAT_INTERVAL_MS,\n STICK_DEAD_ZONE,\n type PadAction,\n} from './padMap';\n\n/** One frame of pad state: pressed flags by Standard button index, and raw axes. */\nexport interface PadSnapshot {\n buttons: readonly boolean[];\n axes: readonly number[];\n}\n\n/** Which direction is held, when it started, and when it last fired. */\nexport interface RepeatState {\n held: PadAction | null;\n since: number;\n lastFire: number;\n}\n\nexport const IDLE_REPEAT: RepeatState = { held: null, since: 0, lastFire: 0 };\n\nconst STICK_X = 0;\nconst STICK_Y = 1;\n\nfunction stickDirection(axes: readonly number[]): PadAction | null {\n const x = axes[STICK_X] ?? 0;\n const y = axes[STICK_Y] ?? 0;\n if (Math.max(Math.abs(x), Math.abs(y)) < STICK_DEAD_ZONE) {\n return null;\n }\n if (Math.abs(x) > Math.abs(y)) {\n return x > 0 ? PAD_ACTIONS.right : PAD_ACTIONS.left;\n }\n return y > 0 ? PAD_ACTIONS.down : PAD_ACTIONS.up;\n}\n\nfunction heldDirection(snapshot: PadSnapshot): PadAction | null {\n const dpad = DPAD_ACTIONS.find(([index]) => snapshot.buttons[index] === true);\n return dpad === undefined ? stickDirection(snapshot.axes) : dpad[1];\n}\n\n/** One poll frame → the actions to dispatch and the next repeat state. Pure. */\nexport function stepPad(\n prev: PadSnapshot | null,\n next: PadSnapshot,\n repeat: RepeatState,\n now: number,\n): { actions: PadAction[]; repeat: RepeatState } {\n const actions: PadAction[] = FACE_BUTTON_ACTIONS\n .filter(([index]) => next.buttons[index] === true && prev?.buttons[index] !== true)\n .map(([, action]) => action);\n const direction = heldDirection(next);\n if (direction === null) {\n return { actions, repeat: IDLE_REPEAT };\n }\n if (direction !== repeat.held) {\n return { actions: [...actions, direction], repeat: { held: direction, since: now, lastFire: now } };\n }\n const firstRepeat = repeat.lastFire === repeat.since;\n const wait = firstRepeat ? REPEAT_DELAY_MS : REPEAT_INTERVAL_MS;\n if (now - repeat.lastFire < wait) {\n return { actions, repeat };\n }\n return { actions: [...actions, direction], repeat: { ...repeat, lastFire: now } };\n}\n","import type { PadAction } from './padMap';\nimport { IDLE_REPEAT, stepPad, type PadSnapshot, type RepeatState } from './stepPad';\n\n/** The structural subset of the DOM `Gamepad` the poller reads. */\nexport interface PadLike {\n connected: boolean;\n buttons: ReadonlyArray<{ pressed: boolean }>;\n axes: readonly number[];\n}\n\nexport interface GamepadPollerOptions {\n /** Usually `() => navigator.getGamepads()`. */\n getPads: () => ReadonlyArray<PadLike | null>;\n /** Usually `requestAnimationFrame`. */\n requestFrame: (cb: (now: number) => void) => number;\n /** Usually `cancelAnimationFrame`. */\n cancelFrame: (id: number) => void;\n onAction: (action: PadAction) => void;\n}\n\n/**\n * rAF-driven poller over the first connected pad. Starts idle; call start().\n * Safe to stop() (or stop() then start()) from inside `onAction`.\n */\nexport function createGamepadPoller(opts: GamepadPollerOptions): { start: () => void; stop: () => void } {\n let prev: PadSnapshot | null = null;\n let repeat: RepeatState = IDLE_REPEAT;\n let running = false;\n let frame: number | null = null;\n\n const tick = (now: number): void => {\n frame = null;\n const pad = opts.getPads().find((p): p is PadLike => p !== null && p.connected);\n if (pad === undefined) {\n prev = null;\n repeat = IDLE_REPEAT;\n } else {\n const snapshot: PadSnapshot = { buttons: pad.buttons.map((b) => b.pressed), axes: pad.axes };\n const step = stepPad(prev, snapshot, repeat, now);\n prev = snapshot;\n repeat = step.repeat;\n step.actions.forEach((action) => opts.onAction(action));\n }\n if (running && frame === null) {\n frame = opts.requestFrame(tick);\n }\n };\n\n return {\n start: (): void => {\n if (running) {\n return;\n }\n running = true;\n if (frame === null) {\n frame = opts.requestFrame(tick);\n }\n },\n stop: (): void => {\n running = false;\n if (frame !== null) {\n opts.cancelFrame(frame);\n frame = null;\n }\n },\n };\n}\n","import type { VibrationPattern } from './types';\r\n\r\n/** Default localStorage key for the haptics on/off preference. */\r\nexport const DEFAULT_HAPTICS_STORAGE_KEY = 'game.haptics';\r\n\r\n/** Default confirmation tick fired when haptics are turned ON (ms). */\r\nexport const DEFAULT_ENABLE_CONFIRMATION = 15;\r\n\r\n/** Stored value meaning \"disabled\". Any other value (or absent) means enabled. */\r\nexport const DISABLED_VALUE = '0';\r\n\r\n/** Stored value meaning \"enabled\". */\r\nexport const ENABLED_VALUE = '1';\r\n\r\n/** DOM events that count as a user activation before `navigator.userActivation` is available. */\r\nexport const USER_ACTIVATION_EVENTS = ['pointerdown', 'keydown', 'touchstart'] as const;\r\n\r\n/** The semantic intents games map their events onto. */\r\nexport type HapticIntent =\r\n | 'tap'\r\n | 'select'\r\n | 'lock'\r\n | 'bump'\r\n | 'navigate'\r\n | 'success'\r\n | 'error'\r\n | 'loss'\r\n | 'reveal';\r\n\r\n/**\r\n * A shared vocabulary of vibration patterns keyed by intent, so every game uses\r\n * the same tuned feel instead of re-picking magic durations. Pass any of these to\r\n * {@link Haptics.vibrate} (or supply your own pattern).\r\n */\r\nexport const HAPTIC_PATTERNS: Record<HapticIntent, VibrationPattern> = {\r\n /** Light tick for menu navigation / selection / undo / restart. */\r\n tap: 10,\r\n /** Light pulse when a tool/charm/item is picked up or selected. */\r\n select: 12,\r\n /** Short pulse when something locks into place. */\r\n lock: 18,\r\n /** Dull buzz when an action is blocked. */\r\n bump: 24,\r\n /** Quick pulse when stepping through a portal / page turn. */\r\n navigate: 16,\r\n /** Celebratory pattern when a level/chamber is cleared. */\r\n success: [0, 40, 50, 90],\r\n /** Sharp double crack for a fatal/destructive event. */\r\n error: [0, 14, 24, 14],\r\n /** Heavier buzz when the player loses / falls. */\r\n loss: [0, 40, 50, 40],\r\n /** Rising triple pulse the instant a puzzle resolves / an exit opens. */\r\n reveal: [0, 14, 20, 18, 26, 24],\r\n};\r\n","import { USER_ACTIVATION_EVENTS } from './constants';\nimport type { ActivationTarget, VibrationCapable } from './types';\n\nfunction resolveActivationTarget(explicit?: ActivationTarget): ActivationTarget | undefined {\n if (explicit) {return explicit;}\n const candidate = globalThis as Partial<ActivationTarget>;\n return typeof candidate.addEventListener === 'function' ? (candidate as ActivationTarget) : undefined;\n}\n\n/** Returns a probe that is true once the page has received a user gesture. */\nexport function createActivationProbe(\n navigator: VibrationCapable | undefined,\n explicitTarget?: ActivationTarget,\n): () => boolean {\n let latched = false;\n const target = typeof navigator?.vibrate === 'function' ? resolveActivationTarget(explicitTarget) : undefined;\n\n const onGesture = (): void => {\n latched = true;\n for (const type of USER_ACTIVATION_EVENTS) {target?.removeEventListener(type, onGesture, true);}\n };\n\n for (const type of USER_ACTIVATION_EVENTS) {target?.addEventListener(type, onGesture, true);}\n\n return (): boolean => latched || navigator?.userActivation?.hasBeenActive === true;\n}\n","import {\r\n DEFAULT_ENABLE_CONFIRMATION,\r\n DEFAULT_HAPTICS_STORAGE_KEY,\r\n DISABLED_VALUE,\r\n ENABLED_VALUE,\r\n} from './constants';\r\nimport type { Haptics, HapticsOptions, HapticsStorage, VibrationCapable, VibrationPattern } from './types';\r\nimport { createActivationProbe } from './userActivation';\r\n\r\nfunction resolveNavigator(explicit?: VibrationCapable): VibrationCapable | undefined {\r\n if (explicit) {\r\n return explicit;\r\n }\r\n return (globalThis as { navigator?: VibrationCapable }).navigator;\r\n}\r\n\r\nfunction resolveStorage(explicit?: HapticsStorage): HapticsStorage | undefined {\r\n if (explicit) {\r\n return explicit;\r\n }\r\n return (globalThis as { localStorage?: HapticsStorage }).localStorage;\r\n}\r\n\r\n/**\r\n * Create a player-toggleable, always-safe haptics controller: it never vibrates before a\r\n * user gesture, while disabled, or when the Vibration API is missing or blocked.\r\n */\r\nexport function createHaptics(options: HapticsOptions = {}): Haptics {\r\n const storageKey = options.storageKey ?? DEFAULT_HAPTICS_STORAGE_KEY;\r\n const confirmation =\r\n options.enableConfirmation === undefined ? DEFAULT_ENABLE_CONFIRMATION : options.enableConfirmation;\r\n const navigator = resolveNavigator(options.navigator);\r\n const storage = resolveStorage(options.storage);\r\n const hasUserActivation = createActivationProbe(navigator, options.activationTarget);\r\n\r\n const readPref = (): boolean => {\r\n try {\r\n return storage?.getItem(storageKey) !== DISABLED_VALUE;\r\n } catch {\r\n return true;\r\n }\r\n };\r\n\r\n let enabled = readPref();\r\n\r\n const vibrate = (pattern: VibrationPattern): void => {\r\n if (!enabled || !hasUserActivation()) {\r\n return;\r\n }\r\n try {\r\n navigator?.vibrate?.(pattern);\r\n } catch {\r\n return;\r\n }\r\n };\r\n\r\n const persist = (): void => {\r\n try {\r\n storage?.setItem(storageKey, enabled ? ENABLED_VALUE : DISABLED_VALUE);\r\n } catch {\r\n return;\r\n }\r\n };\r\n\r\n const setEnabled = (next: boolean): boolean => {\r\n enabled = next;\r\n persist();\r\n if (enabled && confirmation !== null) {\r\n vibrate(confirmation);\r\n }\r\n return enabled;\r\n };\r\n\r\n return {\r\n isEnabled: (): boolean => enabled,\r\n setEnabled,\r\n toggle: (): boolean => setEnabled(!enabled),\r\n vibrate,\r\n };\r\n}\r\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,131 @@
|
|
|
1
|
+
// src/gamepad/padMap.ts
|
|
2
|
+
var PAD_ACTIONS = {
|
|
3
|
+
up: "up",
|
|
4
|
+
down: "down",
|
|
5
|
+
left: "left",
|
|
6
|
+
right: "right",
|
|
7
|
+
confirm: "confirm",
|
|
8
|
+
back: "back",
|
|
9
|
+
mood: "mood",
|
|
10
|
+
view: "view",
|
|
11
|
+
pagePrev: "pagePrev",
|
|
12
|
+
pageNext: "pageNext"
|
|
13
|
+
};
|
|
14
|
+
var STANDARD_BUTTON = {
|
|
15
|
+
south: 0,
|
|
16
|
+
east: 1,
|
|
17
|
+
west: 2,
|
|
18
|
+
north: 3,
|
|
19
|
+
leftShoulder: 4,
|
|
20
|
+
rightShoulder: 5,
|
|
21
|
+
dpadUp: 12,
|
|
22
|
+
dpadDown: 13,
|
|
23
|
+
dpadLeft: 14,
|
|
24
|
+
dpadRight: 15
|
|
25
|
+
};
|
|
26
|
+
var FACE_BUTTON_ACTIONS = [
|
|
27
|
+
[STANDARD_BUTTON.south, PAD_ACTIONS.confirm],
|
|
28
|
+
[STANDARD_BUTTON.east, PAD_ACTIONS.back],
|
|
29
|
+
[STANDARD_BUTTON.west, PAD_ACTIONS.view],
|
|
30
|
+
[STANDARD_BUTTON.north, PAD_ACTIONS.mood],
|
|
31
|
+
[STANDARD_BUTTON.leftShoulder, PAD_ACTIONS.pagePrev],
|
|
32
|
+
[STANDARD_BUTTON.rightShoulder, PAD_ACTIONS.pageNext]
|
|
33
|
+
];
|
|
34
|
+
var DPAD_ACTIONS = [
|
|
35
|
+
[STANDARD_BUTTON.dpadUp, PAD_ACTIONS.up],
|
|
36
|
+
[STANDARD_BUTTON.dpadDown, PAD_ACTIONS.down],
|
|
37
|
+
[STANDARD_BUTTON.dpadLeft, PAD_ACTIONS.left],
|
|
38
|
+
[STANDARD_BUTTON.dpadRight, PAD_ACTIONS.right]
|
|
39
|
+
];
|
|
40
|
+
var STICK_DEAD_ZONE = 0.5;
|
|
41
|
+
var REPEAT_DELAY_MS = 380;
|
|
42
|
+
var REPEAT_INTERVAL_MS = 110;
|
|
43
|
+
|
|
44
|
+
// src/gamepad/stepPad.ts
|
|
45
|
+
var IDLE_REPEAT = { held: null, since: 0, lastFire: 0 };
|
|
46
|
+
var STICK_X = 0;
|
|
47
|
+
var STICK_Y = 1;
|
|
48
|
+
function stickDirection(axes) {
|
|
49
|
+
const x = axes[STICK_X] ?? 0;
|
|
50
|
+
const y = axes[STICK_Y] ?? 0;
|
|
51
|
+
if (Math.max(Math.abs(x), Math.abs(y)) < STICK_DEAD_ZONE) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
if (Math.abs(x) > Math.abs(y)) {
|
|
55
|
+
return x > 0 ? PAD_ACTIONS.right : PAD_ACTIONS.left;
|
|
56
|
+
}
|
|
57
|
+
return y > 0 ? PAD_ACTIONS.down : PAD_ACTIONS.up;
|
|
58
|
+
}
|
|
59
|
+
function heldDirection(snapshot) {
|
|
60
|
+
const dpad = DPAD_ACTIONS.find(([index]) => snapshot.buttons[index] === true);
|
|
61
|
+
return dpad === void 0 ? stickDirection(snapshot.axes) : dpad[1];
|
|
62
|
+
}
|
|
63
|
+
function stepPad(prev, next, repeat, now) {
|
|
64
|
+
const actions = FACE_BUTTON_ACTIONS.filter(([index]) => next.buttons[index] === true && prev?.buttons[index] !== true).map(([, action]) => action);
|
|
65
|
+
const direction = heldDirection(next);
|
|
66
|
+
if (direction === null) {
|
|
67
|
+
return { actions, repeat: IDLE_REPEAT };
|
|
68
|
+
}
|
|
69
|
+
if (direction !== repeat.held) {
|
|
70
|
+
return { actions: [...actions, direction], repeat: { held: direction, since: now, lastFire: now } };
|
|
71
|
+
}
|
|
72
|
+
const firstRepeat = repeat.lastFire === repeat.since;
|
|
73
|
+
const wait = firstRepeat ? REPEAT_DELAY_MS : REPEAT_INTERVAL_MS;
|
|
74
|
+
if (now - repeat.lastFire < wait) {
|
|
75
|
+
return { actions, repeat };
|
|
76
|
+
}
|
|
77
|
+
return { actions: [...actions, direction], repeat: { ...repeat, lastFire: now } };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/gamepad/createGamepadPoller.ts
|
|
81
|
+
function createGamepadPoller(opts) {
|
|
82
|
+
let prev = null;
|
|
83
|
+
let repeat = IDLE_REPEAT;
|
|
84
|
+
let running = false;
|
|
85
|
+
let frame = null;
|
|
86
|
+
const tick = (now) => {
|
|
87
|
+
frame = null;
|
|
88
|
+
const pad = opts.getPads().find((p) => p !== null && p.connected);
|
|
89
|
+
if (pad === void 0) {
|
|
90
|
+
prev = null;
|
|
91
|
+
repeat = IDLE_REPEAT;
|
|
92
|
+
} else {
|
|
93
|
+
const snapshot = { buttons: pad.buttons.map((b) => b.pressed), axes: pad.axes };
|
|
94
|
+
const step = stepPad(prev, snapshot, repeat, now);
|
|
95
|
+
prev = snapshot;
|
|
96
|
+
repeat = step.repeat;
|
|
97
|
+
step.actions.forEach((action) => opts.onAction(action));
|
|
98
|
+
}
|
|
99
|
+
if (running && frame === null) {
|
|
100
|
+
frame = opts.requestFrame(tick);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
return {
|
|
104
|
+
start: () => {
|
|
105
|
+
if (running) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
running = true;
|
|
109
|
+
if (frame === null) {
|
|
110
|
+
frame = opts.requestFrame(tick);
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
stop: () => {
|
|
114
|
+
running = false;
|
|
115
|
+
if (frame !== null) {
|
|
116
|
+
opts.cancelFrame(frame);
|
|
117
|
+
frame = null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
1
123
|
// src/constants.ts
|
|
2
124
|
var DEFAULT_HAPTICS_STORAGE_KEY = "game.haptics";
|
|
3
125
|
var DEFAULT_ENABLE_CONFIRMATION = 15;
|
|
4
126
|
var DISABLED_VALUE = "0";
|
|
5
127
|
var ENABLED_VALUE = "1";
|
|
128
|
+
var USER_ACTIVATION_EVENTS = ["pointerdown", "keydown", "touchstart"];
|
|
6
129
|
var HAPTIC_PATTERNS = {
|
|
7
130
|
/** Light tick for menu navigation / selection / undo / restart. */
|
|
8
131
|
tap: 10,
|
|
@@ -24,6 +147,29 @@ var HAPTIC_PATTERNS = {
|
|
|
24
147
|
reveal: [0, 14, 20, 18, 26, 24]
|
|
25
148
|
};
|
|
26
149
|
|
|
150
|
+
// src/userActivation.ts
|
|
151
|
+
function resolveActivationTarget(explicit) {
|
|
152
|
+
if (explicit) {
|
|
153
|
+
return explicit;
|
|
154
|
+
}
|
|
155
|
+
const candidate = globalThis;
|
|
156
|
+
return typeof candidate.addEventListener === "function" ? candidate : void 0;
|
|
157
|
+
}
|
|
158
|
+
function createActivationProbe(navigator, explicitTarget) {
|
|
159
|
+
let latched = false;
|
|
160
|
+
const target = typeof navigator?.vibrate === "function" ? resolveActivationTarget(explicitTarget) : void 0;
|
|
161
|
+
const onGesture = () => {
|
|
162
|
+
latched = true;
|
|
163
|
+
for (const type of USER_ACTIVATION_EVENTS) {
|
|
164
|
+
target?.removeEventListener(type, onGesture, true);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
for (const type of USER_ACTIVATION_EVENTS) {
|
|
168
|
+
target?.addEventListener(type, onGesture, true);
|
|
169
|
+
}
|
|
170
|
+
return () => latched || navigator?.userActivation?.hasBeenActive === true;
|
|
171
|
+
}
|
|
172
|
+
|
|
27
173
|
// src/haptics.ts
|
|
28
174
|
function resolveNavigator(explicit) {
|
|
29
175
|
if (explicit) {
|
|
@@ -42,6 +188,7 @@ function createHaptics(options = {}) {
|
|
|
42
188
|
const confirmation = options.enableConfirmation === void 0 ? DEFAULT_ENABLE_CONFIRMATION : options.enableConfirmation;
|
|
43
189
|
const navigator = resolveNavigator(options.navigator);
|
|
44
190
|
const storage = resolveStorage(options.storage);
|
|
191
|
+
const hasUserActivation = createActivationProbe(navigator, options.activationTarget);
|
|
45
192
|
const readPref = () => {
|
|
46
193
|
try {
|
|
47
194
|
return storage?.getItem(storageKey) !== DISABLED_VALUE;
|
|
@@ -51,18 +198,20 @@ function createHaptics(options = {}) {
|
|
|
51
198
|
};
|
|
52
199
|
let enabled = readPref();
|
|
53
200
|
const vibrate = (pattern) => {
|
|
54
|
-
if (!enabled) {
|
|
201
|
+
if (!enabled || !hasUserActivation()) {
|
|
55
202
|
return;
|
|
56
203
|
}
|
|
57
204
|
try {
|
|
58
205
|
navigator?.vibrate?.(pattern);
|
|
59
206
|
} catch {
|
|
207
|
+
return;
|
|
60
208
|
}
|
|
61
209
|
};
|
|
62
210
|
const persist = () => {
|
|
63
211
|
try {
|
|
64
212
|
storage?.setItem(storageKey, enabled ? ENABLED_VALUE : DISABLED_VALUE);
|
|
65
213
|
} catch {
|
|
214
|
+
return;
|
|
66
215
|
}
|
|
67
216
|
};
|
|
68
217
|
const setEnabled = (next) => {
|
|
@@ -81,6 +230,6 @@ function createHaptics(options = {}) {
|
|
|
81
230
|
};
|
|
82
231
|
}
|
|
83
232
|
|
|
84
|
-
export { DEFAULT_ENABLE_CONFIRMATION, DEFAULT_HAPTICS_STORAGE_KEY, HAPTIC_PATTERNS, createHaptics };
|
|
233
|
+
export { DEFAULT_ENABLE_CONFIRMATION, DEFAULT_HAPTICS_STORAGE_KEY, HAPTIC_PATTERNS, IDLE_REPEAT, PAD_ACTIONS, STANDARD_BUTTON, USER_ACTIVATION_EVENTS, createGamepadPoller, createHaptics, stepPad };
|
|
85
234
|
//# sourceMappingURL=index.mjs.map
|
|
86
235
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/constants.ts","../src/haptics.ts"],"names":[],"mappings":";AAGO,IAAM,2BAAA,G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,CAAe,OAAA,CAAQ,OAAO,CAAA;AAE9C,EAAA,MAAM,WAAW,MAAe;AAC9B,IAAA,IAAI;AACF,MAAA,OAAO,OAAA,EAAS,OAAA,CAAQ,UAAU,CAAA,KAAM,cAAA;AAAA,IAC1C,CAAA,CAAA,MAAQ;AAEN,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,EACF,CAAA;AAEA,EAAA,IAAI,UAAU,QAAA,EAAS;AAEvB,EAAA,MAAM,OAAA,GAAU,CAAC,OAAA,KAAoC;AACnD,IAAA,IAAI,CAAC,OAAA,EAAS;AACZ,MAAA;AAAA,IACF;AACA,IAAA,IAAI;AACF,MAAA,SAAA,EAAW,UAAU,OAAO,CAAA;AAAA,IAC9B,CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACF,CAAA;AAEA,EAAA,MAAM,UAAU,MAAY;AAC1B,IAAA,IAAI;AACF,MAAA,OAAA,EAAS,OAAA,CAAQ,UAAA,EAAY,OAAA,GAAU,aAAA,GAAgB,cAAc,CAAA;AAAA,IACvE,CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACF,CAAA;AAEA,EAAA,MAAM,UAAA,GAAa,CAAC,IAAA,KAA2B;AAC7C,IAAA,OAAA,GAAU,IAAA;AACV,IAAA,OAAA,EAAQ;AAER,IAAA,IAAI,OAAA,IAAW,iBAAiB,IAAA,EAAM;AACpC,MAAA,OAAA,CAAQ,YAAY,CAAA;AAAA,IACtB;AACA,IAAA,OAAO,OAAA;AAAA,EACT,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,WAAW,MAAe,OAAA;AAAA,IAC1B,UAAA;AAAA,IACA,MAAA,EAAQ,MAAe,UAAA,CAAW,CAAC,OAAO,CAAA;AAAA,IAC1C;AAAA,GACF;AACF","file":"index.mjs","sourcesContent":["import type { VibrationPattern } from './types';\n\n/** Default localStorage key for the haptics on/off preference. */\nexport const DEFAULT_HAPTICS_STORAGE_KEY = 'game.haptics';\n\n/** Default confirmation tick fired when haptics are turned ON (ms). */\nexport const DEFAULT_ENABLE_CONFIRMATION = 15;\n\n/** Stored value meaning \"disabled\". Any other value (or absent) means enabled. */\nexport const DISABLED_VALUE = '0';\n\n/** Stored value meaning \"enabled\". */\nexport const ENABLED_VALUE = '1';\n\n/** The semantic intents games map their events onto. */\nexport type HapticIntent =\n | 'tap'\n | 'select'\n | 'lock'\n | 'bump'\n | 'navigate'\n | 'success'\n | 'error'\n | 'loss'\n | 'reveal';\n\n/**\n * A shared vocabulary of vibration patterns keyed by intent, so every game uses\n * the same tuned feel instead of re-picking magic durations. Pass any of these to\n * {@link Haptics.vibrate} (or supply your own pattern).\n */\nexport const HAPTIC_PATTERNS: Record<HapticIntent, VibrationPattern> = {\n /** Light tick for menu navigation / selection / undo / restart. */\n tap: 10,\n /** Light pulse when a tool/charm/item is picked up or selected. */\n select: 12,\n /** Short pulse when something locks into place. */\n lock: 18,\n /** Dull buzz when an action is blocked. */\n bump: 24,\n /** Quick pulse when stepping through a portal / page turn. */\n navigate: 16,\n /** Celebratory pattern when a level/chamber is cleared. */\n success: [0, 40, 50, 90],\n /** Sharp double crack for a fatal/destructive event. */\n error: [0, 14, 24, 14],\n /** Heavier buzz when the player loses / falls. */\n loss: [0, 40, 50, 40],\n /** Rising triple pulse the instant a puzzle resolves / an exit opens. */\n reveal: [0, 14, 20, 18, 26, 24],\n};\n","import {\n DEFAULT_ENABLE_CONFIRMATION,\n DEFAULT_HAPTICS_STORAGE_KEY,\n DISABLED_VALUE,\n ENABLED_VALUE,\n} from './constants';\nimport type { Haptics, HapticsOptions, HapticsStorage, VibrationCapable, VibrationPattern } from './types';\n\nfunction resolveNavigator(explicit?: VibrationCapable): VibrationCapable | undefined {\n if (explicit) {\n return explicit;\n }\n return (globalThis as { navigator?: VibrationCapable }).navigator;\n}\n\nfunction resolveStorage(explicit?: HapticsStorage): HapticsStorage | undefined {\n if (explicit) {\n return explicit;\n }\n return (globalThis as { localStorage?: HapticsStorage }).localStorage;\n}\n\n/**\n * Create a player-toggleable, always-safe haptics controller.\n *\n * The on/off preference is read from (and persisted to) the configured storage key\n * so it survives reloads. Every vibration routes through one gated call that no-ops\n * when disabled, when no Vibration API is present, or if the platform blocks it.\n */\nexport function createHaptics(options: HapticsOptions = {}): Haptics {\n const storageKey = options.storageKey ?? DEFAULT_HAPTICS_STORAGE_KEY;\n const confirmation =\n options.enableConfirmation === undefined ? DEFAULT_ENABLE_CONFIRMATION : options.enableConfirmation;\n const navigator = resolveNavigator(options.navigator);\n const storage = resolveStorage(options.storage);\n\n const readPref = (): boolean => {\n try {\n return storage?.getItem(storageKey) !== DISABLED_VALUE;\n } catch {\n // Reading storage can throw (blocked cookies / sandboxed iframe) — default ON.\n return true;\n }\n };\n\n let enabled = readPref();\n\n const vibrate = (pattern: VibrationPattern): void => {\n if (!enabled) {\n return;\n }\n try {\n navigator?.vibrate?.(pattern);\n } catch {\n // Vibration can throw if blocked by the platform — never let it break a move.\n }\n };\n\n const persist = (): void => {\n try {\n storage?.setItem(storageKey, enabled ? ENABLED_VALUE : DISABLED_VALUE);\n } catch {\n // Writing storage can throw (private mode / quota) — ignore.\n }\n };\n\n const setEnabled = (next: boolean): boolean => {\n enabled = next;\n persist();\n // A confirming tick when turning ON so the player feels it engage.\n if (enabled && confirmation !== null) {\n vibrate(confirmation);\n }\n return enabled;\n };\n\n return {\n isEnabled: (): boolean => enabled,\n setEnabled,\n toggle: (): boolean => setEnabled(!enabled),\n vibrate,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/gamepad/padMap.ts","../src/gamepad/stepPad.ts","../src/gamepad/createGamepadPoller.ts","../src/constants.ts","../src/userActivation.ts","../src/haptics.ts"],"names":[],"mappings":";AACO,IAAM,WAAA,GAAc;AAAA,EACzB,EAAA,EAAI,IAAA;AAAA,EACJ,IAAA,EAAM,MAAA;AAAA,EACN,IAAA,EAAM,MAAA;AAAA,EACN,KAAA,EAAO,OAAA;AAAA,EACP,OAAA,EAAS,SAAA;AAAA,EACT,IAAA,EA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AO,IAAA;AAAA,IACT;AAAA,EACF,CAAA;AAEA,EAAA,IAAI,UAAU,QAAA,EAAS;AAEvB,EAAA,MAAM,OAAA,GAAU,CAAC,OAAA,KAAoC;AACnD,IAAA,IAAI,CAAC,OAAA,IAAW,CAAC,iBAAA,EAAkB,EAAG;AACpC,MAAA;AAAA,IACF;AACA,IAAA,IAAI;AACF,MAAA,SAAA,EAAW,UAAU,OAAO,CAAA;AAAA,IAC9B,CAAA,CAAA,MAAQ;AACN,MAAA;AAAA,IACF;AAAA,EACF,CAAA;AAEA,EAAA,MAAM,UAAU,MAAY;AAC1B,IAAA,IAAI;AACF,MAAA,OAAA,EAAS,OAAA,CAAQ,UAAA,EAAY,OAAA,GAAU,aAAA,GAAgB,cAAc,CAAA;AAAA,IACvE,CAAA,CAAA,MAAQ;AACN,MAAA;AAAA,IACF;AAAA,EACF,CAAA;AAEA,EAAA,MAAM,UAAA,GAAa,CAAC,IAAA,KAA2B;AAC7C,IAAA,OAAA,GAAU,IAAA;AACV,IAAA,OAAA,EAAQ;AACR,IAAA,IAAI,OAAA,IAAW,iBAAiB,IAAA,EAAM;AACpC,MAAA,OAAA,CAAQ,YAAY,CAAA;AAAA,IACtB;AACA,IAAA,OAAO,OAAA;AAAA,EACT,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,WAAW,MAAe,OAAA;AAAA,IAC1B,UAAA;AAAA,IACA,MAAA,EAAQ,MAAe,UAAA,CAAW,CAAC,OAAO,CAAA;AAAA,IAC1C;AAAA,GACF;AACF","file":"index.mjs","sourcesContent":["/** The semantic actions a Standard Gamepad produces, independent of the device's labels. */\nexport const PAD_ACTIONS = {\n up: 'up',\n down: 'down',\n left: 'left',\n right: 'right',\n confirm: 'confirm',\n back: 'back',\n mood: 'mood',\n view: 'view',\n pagePrev: 'pagePrev',\n pageNext: 'pageNext',\n} as const;\nexport type PadAction = (typeof PAD_ACTIONS)[keyof typeof PAD_ACTIONS];\n\n/** W3C Standard Gamepad button indices, named by POSITION (Nintendo swaps A/B labels). */\nexport const STANDARD_BUTTON = {\n south: 0,\n east: 1,\n west: 2,\n north: 3,\n leftShoulder: 4,\n rightShoulder: 5,\n dpadUp: 12,\n dpadDown: 13,\n dpadLeft: 14,\n dpadRight: 15,\n} as const;\n\n/** Edge-triggered buttons, in dispatch order. */\nexport const FACE_BUTTON_ACTIONS: ReadonlyArray<readonly [number, PadAction]> = [\n [STANDARD_BUTTON.south, PAD_ACTIONS.confirm],\n [STANDARD_BUTTON.east, PAD_ACTIONS.back],\n [STANDARD_BUTTON.west, PAD_ACTIONS.view],\n [STANDARD_BUTTON.north, PAD_ACTIONS.mood],\n [STANDARD_BUTTON.leftShoulder, PAD_ACTIONS.pagePrev],\n [STANDARD_BUTTON.rightShoulder, PAD_ACTIONS.pageNext],\n];\n\n/** Held (auto-repeating) buttons; the first pressed one wins. */\nexport const DPAD_ACTIONS: ReadonlyArray<readonly [number, PadAction]> = [\n [STANDARD_BUTTON.dpadUp, PAD_ACTIONS.up],\n [STANDARD_BUTTON.dpadDown, PAD_ACTIONS.down],\n [STANDARD_BUTTON.dpadLeft, PAD_ACTIONS.left],\n [STANDARD_BUTTON.dpadRight, PAD_ACTIONS.right],\n];\n\n/** Left-stick magnitude below which the stick counts as centred. */\nexport const STICK_DEAD_ZONE = 0.5;\n/** Delay before a held direction starts repeating (ms). */\nexport const REPEAT_DELAY_MS = 380;\n/** Interval between repeats once repeating (ms). */\nexport const REPEAT_INTERVAL_MS = 110;\n","import {\n DPAD_ACTIONS,\n FACE_BUTTON_ACTIONS,\n PAD_ACTIONS,\n REPEAT_DELAY_MS,\n REPEAT_INTERVAL_MS,\n STICK_DEAD_ZONE,\n type PadAction,\n} from './padMap';\n\n/** One frame of pad state: pressed flags by Standard button index, and raw axes. */\nexport interface PadSnapshot {\n buttons: readonly boolean[];\n axes: readonly number[];\n}\n\n/** Which direction is held, when it started, and when it last fired. */\nexport interface RepeatState {\n held: PadAction | null;\n since: number;\n lastFire: number;\n}\n\nexport const IDLE_REPEAT: RepeatState = { held: null, since: 0, lastFire: 0 };\n\nconst STICK_X = 0;\nconst STICK_Y = 1;\n\nfunction stickDirection(axes: readonly number[]): PadAction | null {\n const x = axes[STICK_X] ?? 0;\n const y = axes[STICK_Y] ?? 0;\n if (Math.max(Math.abs(x), Math.abs(y)) < STICK_DEAD_ZONE) {\n return null;\n }\n if (Math.abs(x) > Math.abs(y)) {\n return x > 0 ? PAD_ACTIONS.right : PAD_ACTIONS.left;\n }\n return y > 0 ? PAD_ACTIONS.down : PAD_ACTIONS.up;\n}\n\nfunction heldDirection(snapshot: PadSnapshot): PadAction | null {\n const dpad = DPAD_ACTIONS.find(([index]) => snapshot.buttons[index] === true);\n return dpad === undefined ? stickDirection(snapshot.axes) : dpad[1];\n}\n\n/** One poll frame → the actions to dispatch and the next repeat state. Pure. */\nexport function stepPad(\n prev: PadSnapshot | null,\n next: PadSnapshot,\n repeat: RepeatState,\n now: number,\n): { actions: PadAction[]; repeat: RepeatState } {\n const actions: PadAction[] = FACE_BUTTON_ACTIONS\n .filter(([index]) => next.buttons[index] === true && prev?.buttons[index] !== true)\n .map(([, action]) => action);\n const direction = heldDirection(next);\n if (direction === null) {\n return { actions, repeat: IDLE_REPEAT };\n }\n if (direction !== repeat.held) {\n return { actions: [...actions, direction], repeat: { held: direction, since: now, lastFire: now } };\n }\n const firstRepeat = repeat.lastFire === repeat.since;\n const wait = firstRepeat ? REPEAT_DELAY_MS : REPEAT_INTERVAL_MS;\n if (now - repeat.lastFire < wait) {\n return { actions, repeat };\n }\n return { actions: [...actions, direction], repeat: { ...repeat, lastFire: now } };\n}\n","import type { PadAction } from './padMap';\nimport { IDLE_REPEAT, stepPad, type PadSnapshot, type RepeatState } from './stepPad';\n\n/** The structural subset of the DOM `Gamepad` the poller reads. */\nexport interface PadLike {\n connected: boolean;\n buttons: ReadonlyArray<{ pressed: boolean }>;\n axes: readonly number[];\n}\n\nexport interface GamepadPollerOptions {\n /** Usually `() => navigator.getGamepads()`. */\n getPads: () => ReadonlyArray<PadLike | null>;\n /** Usually `requestAnimationFrame`. */\n requestFrame: (cb: (now: number) => void) => number;\n /** Usually `cancelAnimationFrame`. */\n cancelFrame: (id: number) => void;\n onAction: (action: PadAction) => void;\n}\n\n/**\n * rAF-driven poller over the first connected pad. Starts idle; call start().\n * Safe to stop() (or stop() then start()) from inside `onAction`.\n */\nexport function createGamepadPoller(opts: GamepadPollerOptions): { start: () => void; stop: () => void } {\n let prev: PadSnapshot | null = null;\n let repeat: RepeatState = IDLE_REPEAT;\n let running = false;\n let frame: number | null = null;\n\n const tick = (now: number): void => {\n frame = null;\n const pad = opts.getPads().find((p): p is PadLike => p !== null && p.connected);\n if (pad === undefined) {\n prev = null;\n repeat = IDLE_REPEAT;\n } else {\n const snapshot: PadSnapshot = { buttons: pad.buttons.map((b) => b.pressed), axes: pad.axes };\n const step = stepPad(prev, snapshot, repeat, now);\n prev = snapshot;\n repeat = step.repeat;\n step.actions.forEach((action) => opts.onAction(action));\n }\n if (running && frame === null) {\n frame = opts.requestFrame(tick);\n }\n };\n\n return {\n start: (): void => {\n if (running) {\n return;\n }\n running = true;\n if (frame === null) {\n frame = opts.requestFrame(tick);\n }\n },\n stop: (): void => {\n running = false;\n if (frame !== null) {\n opts.cancelFrame(frame);\n frame = null;\n }\n },\n };\n}\n","import type { VibrationPattern } from './types';\r\n\r\n/** Default localStorage key for the haptics on/off preference. */\r\nexport const DEFAULT_HAPTICS_STORAGE_KEY = 'game.haptics';\r\n\r\n/** Default confirmation tick fired when haptics are turned ON (ms). */\r\nexport const DEFAULT_ENABLE_CONFIRMATION = 15;\r\n\r\n/** Stored value meaning \"disabled\". Any other value (or absent) means enabled. */\r\nexport const DISABLED_VALUE = '0';\r\n\r\n/** Stored value meaning \"enabled\". */\r\nexport const ENABLED_VALUE = '1';\r\n\r\n/** DOM events that count as a user activation before `navigator.userActivation` is available. */\r\nexport const USER_ACTIVATION_EVENTS = ['pointerdown', 'keydown', 'touchstart'] as const;\r\n\r\n/** The semantic intents games map their events onto. */\r\nexport type HapticIntent =\r\n | 'tap'\r\n | 'select'\r\n | 'lock'\r\n | 'bump'\r\n | 'navigate'\r\n | 'success'\r\n | 'error'\r\n | 'loss'\r\n | 'reveal';\r\n\r\n/**\r\n * A shared vocabulary of vibration patterns keyed by intent, so every game uses\r\n * the same tuned feel instead of re-picking magic durations. Pass any of these to\r\n * {@link Haptics.vibrate} (or supply your own pattern).\r\n */\r\nexport const HAPTIC_PATTERNS: Record<HapticIntent, VibrationPattern> = {\r\n /** Light tick for menu navigation / selection / undo / restart. */\r\n tap: 10,\r\n /** Light pulse when a tool/charm/item is picked up or selected. */\r\n select: 12,\r\n /** Short pulse when something locks into place. */\r\n lock: 18,\r\n /** Dull buzz when an action is blocked. */\r\n bump: 24,\r\n /** Quick pulse when stepping through a portal / page turn. */\r\n navigate: 16,\r\n /** Celebratory pattern when a level/chamber is cleared. */\r\n success: [0, 40, 50, 90],\r\n /** Sharp double crack for a fatal/destructive event. */\r\n error: [0, 14, 24, 14],\r\n /** Heavier buzz when the player loses / falls. */\r\n loss: [0, 40, 50, 40],\r\n /** Rising triple pulse the instant a puzzle resolves / an exit opens. */\r\n reveal: [0, 14, 20, 18, 26, 24],\r\n};\r\n","import { USER_ACTIVATION_EVENTS } from './constants';\nimport type { ActivationTarget, VibrationCapable } from './types';\n\nfunction resolveActivationTarget(explicit?: ActivationTarget): ActivationTarget | undefined {\n if (explicit) {return explicit;}\n const candidate = globalThis as Partial<ActivationTarget>;\n return typeof candidate.addEventListener === 'function' ? (candidate as ActivationTarget) : undefined;\n}\n\n/** Returns a probe that is true once the page has received a user gesture. */\nexport function createActivationProbe(\n navigator: VibrationCapable | undefined,\n explicitTarget?: ActivationTarget,\n): () => boolean {\n let latched = false;\n const target = typeof navigator?.vibrate === 'function' ? resolveActivationTarget(explicitTarget) : undefined;\n\n const onGesture = (): void => {\n latched = true;\n for (const type of USER_ACTIVATION_EVENTS) {target?.removeEventListener(type, onGesture, true);}\n };\n\n for (const type of USER_ACTIVATION_EVENTS) {target?.addEventListener(type, onGesture, true);}\n\n return (): boolean => latched || navigator?.userActivation?.hasBeenActive === true;\n}\n","import {\r\n DEFAULT_ENABLE_CONFIRMATION,\r\n DEFAULT_HAPTICS_STORAGE_KEY,\r\n DISABLED_VALUE,\r\n ENABLED_VALUE,\r\n} from './constants';\r\nimport type { Haptics, HapticsOptions, HapticsStorage, VibrationCapable, VibrationPattern } from './types';\r\nimport { createActivationProbe } from './userActivation';\r\n\r\nfunction resolveNavigator(explicit?: VibrationCapable): VibrationCapable | undefined {\r\n if (explicit) {\r\n return explicit;\r\n }\r\n return (globalThis as { navigator?: VibrationCapable }).navigator;\r\n}\r\n\r\nfunction resolveStorage(explicit?: HapticsStorage): HapticsStorage | undefined {\r\n if (explicit) {\r\n return explicit;\r\n }\r\n return (globalThis as { localStorage?: HapticsStorage }).localStorage;\r\n}\r\n\r\n/**\r\n * Create a player-toggleable, always-safe haptics controller: it never vibrates before a\r\n * user gesture, while disabled, or when the Vibration API is missing or blocked.\r\n */\r\nexport function createHaptics(options: HapticsOptions = {}): Haptics {\r\n const storageKey = options.storageKey ?? DEFAULT_HAPTICS_STORAGE_KEY;\r\n const confirmation =\r\n options.enableConfirmation === undefined ? DEFAULT_ENABLE_CONFIRMATION : options.enableConfirmation;\r\n const navigator = resolveNavigator(options.navigator);\r\n const storage = resolveStorage(options.storage);\r\n const hasUserActivation = createActivationProbe(navigator, options.activationTarget);\r\n\r\n const readPref = (): boolean => {\r\n try {\r\n return storage?.getItem(storageKey) !== DISABLED_VALUE;\r\n } catch {\r\n return true;\r\n }\r\n };\r\n\r\n let enabled = readPref();\r\n\r\n const vibrate = (pattern: VibrationPattern): void => {\r\n if (!enabled || !hasUserActivation()) {\r\n return;\r\n }\r\n try {\r\n navigator?.vibrate?.(pattern);\r\n } catch {\r\n return;\r\n }\r\n };\r\n\r\n const persist = (): void => {\r\n try {\r\n storage?.setItem(storageKey, enabled ? ENABLED_VALUE : DISABLED_VALUE);\r\n } catch {\r\n return;\r\n }\r\n };\r\n\r\n const setEnabled = (next: boolean): boolean => {\r\n enabled = next;\r\n persist();\r\n if (enabled && confirmation !== null) {\r\n vibrate(confirmation);\r\n }\r\n return enabled;\r\n };\r\n\r\n return {\r\n isEnabled: (): boolean => enabled,\r\n setEnabled,\r\n toggle: (): boolean => setEnabled(!enabled),\r\n vibrate,\r\n };\r\n}\r\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dloizides/game-input",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Framework-agnostic game input primitives. v1 ships safe, player-toggleable haptics (Vibration API wrapper, localStorage-gated, zero deps). Extracted from Aurora; consumed across EisaiPollis games.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"haptics",
|