@margoengine/input 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +370 -3
- package/dist/body.d.ts +117 -0
- package/dist/body.js +446 -0
- package/dist/face.d.ts +136 -0
- package/dist/face.js +344 -0
- package/dist/gestures.d.ts +68 -0
- package/dist/haptics.d.ts +52 -0
- package/dist/haptics.js +59 -0
- package/dist/index.d.ts +205 -0
- package/dist/index.js +3 -0
- package/dist/interactions.d.ts +92 -0
- package/dist/interactions.js +93 -0
- package/dist/labels-DZOEJBkX.js +70 -0
- package/dist/labels.d.ts +8 -0
- package/dist/landmarks.d.ts +197 -0
- package/dist/landmarks.js +183 -0
- package/dist/layout.d.ts +62 -0
- package/dist/layout.js +76 -0
- package/dist/lock.d.ts +72 -0
- package/dist/lock.js +91 -0
- package/dist/motion.d.ts +97 -0
- package/dist/motion.js +98 -0
- package/dist/pen.d.ts +86 -0
- package/dist/pen.js +111 -0
- package/dist/players.d.ts +129 -0
- package/dist/players.js +161 -0
- package/dist/replay.d.ts +109 -0
- package/dist/replay.js +99 -0
- package/dist/src-eYqyrCA7.js +459 -0
- package/dist/vector.d.ts +44 -0
- package/dist/vector.js +66 -0
- package/dist/xr.d.ts +124 -0
- package/dist/xr.js +154 -0
- package/package.json +105 -6
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
export { bindingLabel, type GlyphLayout } from './labels.js';
|
|
3
|
+
export { Gestures, type GestureEvent, type GestureOptions, type GestureType } from './gestures.js';
|
|
4
|
+
/** Modifier keys a `key` binding can require; the binding only fires while all of them are held. */
|
|
5
|
+
export type Modifier = 'shift' | 'ctrl' | 'alt' | 'meta';
|
|
6
|
+
/**
|
|
7
|
+
* One source feeding an action. Each sample is multiplied by `scale` (finite, default 1) and summed into the action.
|
|
8
|
+
* - `key`: `KeyboardEvent.code` (non-empty); 1 while held. With `modifiers` it is a chord (for example Ctrl+S):
|
|
9
|
+
* the key counts only while every listed modifier is also held.
|
|
10
|
+
* - `mouse`: button 0..4; 1 while held.
|
|
11
|
+
* - `pointer`: movement on `x`/`y` (pixels) or `wheel` (`WheelEvent.deltaY` in pixels: a line counts 16 and a page 800) since the last update.
|
|
12
|
+
* - `gamepad-axis` / `gamepad-button`: axis or button `index` (integer >= 0) of gamepad `gamepad` (default 0);
|
|
13
|
+
* axes get the deadzone applied.
|
|
14
|
+
* - `touch`: a control from `createStick`/`bindTouchButton` by `id`; `axis` reads a stick in [-1, 1], otherwise 1 while down.
|
|
15
|
+
* - `control`: a number published by code through {@link Input.control}: `axis` of control `id`, default `'value'`.
|
|
16
|
+
* `Gyro` and `Pen` publish this way.
|
|
17
|
+
*/
|
|
18
|
+
export type Binding = {
|
|
19
|
+
type: 'key';
|
|
20
|
+
code: string;
|
|
21
|
+
modifiers?: readonly Modifier[];
|
|
22
|
+
scale?: number;
|
|
23
|
+
} | {
|
|
24
|
+
type: 'mouse';
|
|
25
|
+
button: number;
|
|
26
|
+
scale?: number;
|
|
27
|
+
} | {
|
|
28
|
+
type: 'pointer';
|
|
29
|
+
axis: 'x' | 'y' | 'wheel';
|
|
30
|
+
scale?: number;
|
|
31
|
+
} | {
|
|
32
|
+
type: 'gamepad-axis' | 'gamepad-button';
|
|
33
|
+
index: number;
|
|
34
|
+
gamepad?: number;
|
|
35
|
+
scale?: number;
|
|
36
|
+
} | {
|
|
37
|
+
type: 'touch';
|
|
38
|
+
id: string;
|
|
39
|
+
axis?: 'x' | 'y';
|
|
40
|
+
scale?: number;
|
|
41
|
+
} | {
|
|
42
|
+
type: 'control';
|
|
43
|
+
id: string;
|
|
44
|
+
axis?: string;
|
|
45
|
+
scale?: number;
|
|
46
|
+
};
|
|
47
|
+
/** Per-action result of the last {@link Input.update}. The object is reused across updates. */
|
|
48
|
+
export interface ActionState {
|
|
49
|
+
/** Sum of the scaled binding samples, clamped to [-1, 1]. */
|
|
50
|
+
value: number;
|
|
51
|
+
/** True while `value` is non-zero (magnitude above 1e-6). */
|
|
52
|
+
down: boolean;
|
|
53
|
+
/** True on the update where `down` became true. */
|
|
54
|
+
pressed: boolean;
|
|
55
|
+
/** True on the update where `down` became false. */
|
|
56
|
+
released: boolean;
|
|
57
|
+
}
|
|
58
|
+
/** Options for the {@link Input} constructor. */
|
|
59
|
+
export interface InputOptions {
|
|
60
|
+
/** Where keyboard, pointer, wheel and blur listeners attach. Default `window`. */
|
|
61
|
+
target?: EventTarget;
|
|
62
|
+
/** Gamepad source. Default `navigator.getGamepads()`, or an empty list where unsupported. */
|
|
63
|
+
gamepads?: () => readonly (Gamepad | null)[];
|
|
64
|
+
/** Gamepad axis deadzone in [0, 1); axes rescale to fill the rest of the range. Default 0.15. */
|
|
65
|
+
deadzone?: number;
|
|
66
|
+
/**
|
|
67
|
+
* Input contexts: context name to the actions it owns. An action listed in any context reads 0 unless one of its
|
|
68
|
+
* contexts is enabled (see {@link Input.setContext}); unlisted actions are always active. All contexts start enabled.
|
|
69
|
+
*/
|
|
70
|
+
contexts?: Readonly<Record<string, readonly string[]>>;
|
|
71
|
+
}
|
|
72
|
+
/** Takes the place of the devices while set with {@link Input.setFeed}. */
|
|
73
|
+
export interface InputFeed {
|
|
74
|
+
/** Called at the start of every {@link Input.update}. Calling `setFeed(null)` here gives this update to the devices. */
|
|
75
|
+
begin(): void;
|
|
76
|
+
/** What one binding of `action` gives this update, as {@link Input.bindingValue} would report it. */
|
|
77
|
+
read(action: string, binding: number): number;
|
|
78
|
+
}
|
|
79
|
+
/** Plain, JSON-safe snapshot of every action's bindings, as written by {@link Input.exportBindings}. */
|
|
80
|
+
export type BindingMap = Record<string, Binding[]>;
|
|
81
|
+
/** Semantic action state is reused; sample once per simulation tick. */
|
|
82
|
+
export declare class Input {
|
|
83
|
+
private readonly states;
|
|
84
|
+
private readonly samples;
|
|
85
|
+
private readonly keys;
|
|
86
|
+
private buttons;
|
|
87
|
+
private readonly tapped;
|
|
88
|
+
private tappedButtons;
|
|
89
|
+
private readonly motion;
|
|
90
|
+
private readonly touch;
|
|
91
|
+
private readonly controls;
|
|
92
|
+
private feed;
|
|
93
|
+
private readonly cleanups;
|
|
94
|
+
private readonly gamepads;
|
|
95
|
+
private readonly deadzone;
|
|
96
|
+
private disposed;
|
|
97
|
+
private readonly target;
|
|
98
|
+
private readonly initial;
|
|
99
|
+
private bindingsByAction;
|
|
100
|
+
private readonly owners;
|
|
101
|
+
private readonly enabled;
|
|
102
|
+
/** Current bindings per action. Replaced (not mutated) by {@link Input.rebind} and {@link Input.importBindings}. */
|
|
103
|
+
get actions(): Readonly<Record<string, readonly Binding[]>>;
|
|
104
|
+
/**
|
|
105
|
+
* Map named actions to bindings and start listening on `target`. Key presses inside form fields are ignored.
|
|
106
|
+
* @throws RangeError for an invalid deadzone or a non-function `gamepads`.
|
|
107
|
+
* @throws TypeError for an empty action name, an action without bindings, or an invalid binding.
|
|
108
|
+
*/
|
|
109
|
+
constructor(actions: Readonly<Record<string, readonly Binding[]>>, { target, gamepads, deadzone, contexts, }?: InputOptions);
|
|
110
|
+
/**
|
|
111
|
+
* Pointer Events 3, 4.1.1.1: a button pressed or released while another is held fires no pointerdown or pointerup,
|
|
112
|
+
* only a pointermove whose `button` names the changed button. `buttons` holds them all, so read it on every event.
|
|
113
|
+
*/
|
|
114
|
+
private pointerButtons;
|
|
115
|
+
private listen;
|
|
116
|
+
/**
|
|
117
|
+
* Live state of `action`, refreshed by each {@link Input.update}.
|
|
118
|
+
* @throws TypeError for an unknown action.
|
|
119
|
+
*/
|
|
120
|
+
state(action: string): Readonly<ActionState>;
|
|
121
|
+
/**
|
|
122
|
+
* What one binding of `action` gave at the last {@link Input.update}, with its `scale` applied and before the action
|
|
123
|
+
* sums and clamps. Reads 0 for an index past the end, and while the action's contexts are off.
|
|
124
|
+
* @throws TypeError for an unknown action.
|
|
125
|
+
*/
|
|
126
|
+
bindingValue(action: string, binding: number): number;
|
|
127
|
+
/** Read bindings from `feed` instead of the devices, or from the devices again with `null`. See `InputReplay`. */
|
|
128
|
+
setFeed(feed: InputFeed | null): void;
|
|
129
|
+
/**
|
|
130
|
+
* Sample every binding into its action state and clear the accumulated pointer motion. Call once per tick.
|
|
131
|
+
* @throws MargoError `'disposed'` after {@link Input.dispose}.
|
|
132
|
+
*/
|
|
133
|
+
update(): void;
|
|
134
|
+
private held;
|
|
135
|
+
private clearTapped;
|
|
136
|
+
/**
|
|
137
|
+
* The numbers of control `id`, made on first use. Write to them and `{ type: 'control', id, axis }` bindings read
|
|
138
|
+
* them at each update, so any code can feed an action. Values go to 0 on {@link Input.reset}.
|
|
139
|
+
* @throws TypeError for an empty id.
|
|
140
|
+
* @throws MargoError `'disposed'` after {@link Input.dispose}.
|
|
141
|
+
*/
|
|
142
|
+
control(id: string): Record<string, number>;
|
|
143
|
+
/** Remove control `id`; bindings to it read 0. */
|
|
144
|
+
removeControl(id: string): void;
|
|
145
|
+
/**
|
|
146
|
+
* Append a 120 px on-screen touch stick to `parent`, readable through `touch` bindings with this `id`.
|
|
147
|
+
* Removed on dispose.
|
|
148
|
+
* @throws TypeError when disposed, `id` is empty, or `id` is already used by a touch control.
|
|
149
|
+
*/
|
|
150
|
+
createStick(parent: HTMLElement, id: string): HTMLDivElement;
|
|
151
|
+
/**
|
|
152
|
+
* Make an existing element a touch button for `touch` bindings with this `id`; pointer, Space and Enter press it.
|
|
153
|
+
* @throws TypeError when disposed, `id` is empty, or `id` is already used by a touch control.
|
|
154
|
+
*/
|
|
155
|
+
bindTouchButton(element: HTMLElement, id: string): void;
|
|
156
|
+
/**
|
|
157
|
+
* Play a dual-rumble effect; resolves false when the gamepad has no vibration actuator.
|
|
158
|
+
* @param duration Milliseconds, in [0, 10000].
|
|
159
|
+
* @param strength Magnitude in [0, 1] for both motors. Default 1.
|
|
160
|
+
* @param gamepad Gamepad index, an integer >= 0. Default 0.
|
|
161
|
+
* @throws MargoError `'disposed'` after {@link Input.dispose}.
|
|
162
|
+
* @throws RangeError for an out-of-range argument.
|
|
163
|
+
*/
|
|
164
|
+
rumble(duration: number, strength?: number, gamepad?: number): Promise<boolean>;
|
|
165
|
+
/**
|
|
166
|
+
* Enable or disable an input context from the constructor's `contexts` option (for example mute gameplay actions
|
|
167
|
+
* while a menu is open). Actions owned by no enabled context read 0, and held actions release on the next update.
|
|
168
|
+
* @throws TypeError for an unknown context.
|
|
169
|
+
*/
|
|
170
|
+
setContext(context: string, enabled: boolean): void;
|
|
171
|
+
/** Whether `context` is currently enabled. Unknown contexts are not. */
|
|
172
|
+
hasContext(context: string): boolean;
|
|
173
|
+
/**
|
|
174
|
+
* Replace the bindings of one action (for example from a rebinding screen).
|
|
175
|
+
* @throws TypeError for an unknown action, no bindings, or an invalid binding.
|
|
176
|
+
*/
|
|
177
|
+
rebind(action: string, bindings: readonly Binding[]): void;
|
|
178
|
+
/** Copy of every action's bindings, safe to `JSON.stringify` into localStorage or a settings file. */
|
|
179
|
+
exportBindings(): BindingMap;
|
|
180
|
+
/**
|
|
181
|
+
* Apply bindings from {@link Input.exportBindings} (or its JSON text). Unknown actions in the data are ignored so
|
|
182
|
+
* saved files survive action renames; actions missing from the data keep their bindings. Nothing changes if any
|
|
183
|
+
* entry is invalid.
|
|
184
|
+
* @throws TypeError for malformed JSON or an invalid binding.
|
|
185
|
+
*/
|
|
186
|
+
importBindings(data: BindingMap | string): void;
|
|
187
|
+
/** Restore the bindings given to the constructor. */
|
|
188
|
+
resetBindings(): void;
|
|
189
|
+
/**
|
|
190
|
+
* Wait for the next key, mouse button or gamepad button press and resolve with it as a {@link Binding}, for a
|
|
191
|
+
* "press a key to rebind" prompt. Modifiers held with a key become `modifiers`; a modifier alone is taken when it is
|
|
192
|
+
* released with no other key. A gamepad button already held at the call counts once released and pressed again.
|
|
193
|
+
* Escape cancels.
|
|
194
|
+
* @throws DOMException `AbortError` when `signal` aborts or the user presses Escape.
|
|
195
|
+
* @throws MargoError `'disposed'` after {@link Input.dispose}.
|
|
196
|
+
*/
|
|
197
|
+
captureBinding({ signal }?: {
|
|
198
|
+
signal?: AbortSignal;
|
|
199
|
+
}): Promise<Binding>;
|
|
200
|
+
/** Release all keys, buttons and touch controls and clear pending motion; runs on blur and pointercancel. */
|
|
201
|
+
reset(): void;
|
|
202
|
+
/** Remove all listeners and created sticks. Idempotent. */
|
|
203
|
+
dispose(): void;
|
|
204
|
+
[Symbol.dispose]: () => void;
|
|
205
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { ActionState } from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* How a press counts:
|
|
4
|
+
* - `tap`: pressed and released within `time`. Performed on release; held past `time` it cancels.
|
|
5
|
+
* - `hold`: held for `time`. Performed while still held; released early it cancels.
|
|
6
|
+
* - `slow-tap`: held for at least `time`, then released. Performed on release; released early it cancels.
|
|
7
|
+
* - `double-press`: `count` presses, each shorter than `time` and each starting within `gap` of the last release.
|
|
8
|
+
* Performed on the last press; a press held too long or a gap too long cancels.
|
|
9
|
+
*/
|
|
10
|
+
export type InteractionType = 'tap' | 'hold' | 'slow-tap' | 'double-press';
|
|
11
|
+
/** One rule of {@link Interactions}, reading an action or one binding of it. */
|
|
12
|
+
export interface InteractionRule {
|
|
13
|
+
type: InteractionType;
|
|
14
|
+
/** The action to read. */
|
|
15
|
+
action: string;
|
|
16
|
+
/**
|
|
17
|
+
* Read only this binding of the action, by its index in `input.actions[action]`, so one action can tap on the
|
|
18
|
+
* keyboard and hold on the gamepad. A rebind keeps working: the index reads whatever is bound there. Default: the
|
|
19
|
+
* whole action.
|
|
20
|
+
*/
|
|
21
|
+
binding?: number;
|
|
22
|
+
/** Milliseconds, as the type describes. Default 200 for `tap` and `double-press`, 400 for `hold`, 500 for `slow-tap`. */
|
|
23
|
+
time?: number;
|
|
24
|
+
/** `double-press`: longest gap in milliseconds between a release and the next press. Default 300. */
|
|
25
|
+
gap?: number;
|
|
26
|
+
/** `double-press`: presses needed, an integer of at least 2. Default 2. */
|
|
27
|
+
count?: number;
|
|
28
|
+
/** Magnitude that counts as pressed, above 0 and up to 1. It releases at three quarters of this. Default 0.5. */
|
|
29
|
+
threshold?: number;
|
|
30
|
+
}
|
|
31
|
+
/** Result of one rule at the last {@link Interactions.update}. The object is reused across updates. */
|
|
32
|
+
export interface InteractionState {
|
|
33
|
+
/** True on the update where a press began the rule. */
|
|
34
|
+
started: boolean;
|
|
35
|
+
/** True on the update where the rule succeeded. */
|
|
36
|
+
performed: boolean;
|
|
37
|
+
/** True on the update where the rule failed. */
|
|
38
|
+
canceled: boolean;
|
|
39
|
+
/** True from `started` until `performed` or `canceled`. */
|
|
40
|
+
active: boolean;
|
|
41
|
+
/** Share of `time` held so far, 0 to 1, for a charge bar; for `double-press`, the share of `count` pressed. */
|
|
42
|
+
progress: number;
|
|
43
|
+
/** Milliseconds since `started`. Keeps its last value once the rule ends. */
|
|
44
|
+
duration: number;
|
|
45
|
+
}
|
|
46
|
+
/** What {@link Interactions} reads: an `Input`, or anything with the same two methods. */
|
|
47
|
+
export interface InteractionSource {
|
|
48
|
+
state(action: string): Readonly<ActionState>;
|
|
49
|
+
bindingValue(action: string, binding: number): number;
|
|
50
|
+
}
|
|
51
|
+
/** Options for {@link Interactions}. */
|
|
52
|
+
export interface InteractionOptions {
|
|
53
|
+
/** Clock in milliseconds. Default `performance.now`. */
|
|
54
|
+
now?: () => number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Tap, hold, slow tap and double press rules over the actions of an `Input`. Rules are named, like actions, and read
|
|
58
|
+
* after `input.update()`:
|
|
59
|
+
*
|
|
60
|
+
* ```ts
|
|
61
|
+
* const rules = new Interactions(input, { open: { type: 'tap', action: 'use' }, inspect: { type: 'hold', action: 'use' } });
|
|
62
|
+
* input.update();
|
|
63
|
+
* rules.update();
|
|
64
|
+
* if (rules.state('inspect').performed) inspect();
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* Rules are independent: one press can start several, and each ends by its own rule.
|
|
68
|
+
*/
|
|
69
|
+
export declare class Interactions {
|
|
70
|
+
private readonly tracks;
|
|
71
|
+
private readonly list;
|
|
72
|
+
private readonly source;
|
|
73
|
+
private readonly now;
|
|
74
|
+
/**
|
|
75
|
+
* @throws TypeError for an empty rule name, an unknown type or action, or a binding that is not an integer >= 0.
|
|
76
|
+
* @throws RangeError for a `time` below 0 (0 only for `hold` and `slow-tap`), a `gap` not above 0, a `count` below 2,
|
|
77
|
+
* or a `threshold` outside (0, 1].
|
|
78
|
+
*/
|
|
79
|
+
constructor(source: InteractionSource, rules: Readonly<Record<string, InteractionRule>>, { now }?: InteractionOptions);
|
|
80
|
+
/**
|
|
81
|
+
* Live state of a rule, refreshed by each {@link Interactions.update}.
|
|
82
|
+
* @throws TypeError for an unknown rule.
|
|
83
|
+
*/
|
|
84
|
+
state(rule: string): Readonly<InteractionState>;
|
|
85
|
+
/** Advance every rule. Call once per tick, after `input.update()`. */
|
|
86
|
+
update(): void;
|
|
87
|
+
/** Drop every press in progress, without a `canceled`: all rules are idle until the next press. */
|
|
88
|
+
reset(): void;
|
|
89
|
+
private step;
|
|
90
|
+
private stepPresses;
|
|
91
|
+
private end;
|
|
92
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
//#region src/interactions.ts
|
|
2
|
+
var e = {
|
|
3
|
+
tap: 200,
|
|
4
|
+
hold: 400,
|
|
5
|
+
"slow-tap": 500,
|
|
6
|
+
"double-press": 200
|
|
7
|
+
}, t = class {
|
|
8
|
+
constructor(e, t, n, r, i, a, o) {
|
|
9
|
+
this.type = e, this.action = t, this.binding = n, this.time = r, this.gap = i, this.count = a, this.threshold = o, this.state = {
|
|
10
|
+
started: !1,
|
|
11
|
+
performed: !1,
|
|
12
|
+
canceled: !1,
|
|
13
|
+
active: !1,
|
|
14
|
+
progress: 0,
|
|
15
|
+
duration: 0
|
|
16
|
+
}, this.held = !1, this.spent = !1, this.start = 0, this.pressAt = 0, this.releaseAt = 0, this.presses = 0;
|
|
17
|
+
}
|
|
18
|
+
}, n = class {
|
|
19
|
+
constructor(n, r, { now: i = () => performance.now() } = {}) {
|
|
20
|
+
if (this.tracks = /* @__PURE__ */ new Map(), this.list = [], typeof n?.state != "function" || typeof n.bindingValue != "function") throw TypeError("Interactions reads an Input; pass one as the first argument");
|
|
21
|
+
if (typeof i != "function") throw TypeError("Interactions now must be a function returning milliseconds");
|
|
22
|
+
this.source = n, this.now = i;
|
|
23
|
+
for (let [i, a] of Object.entries(r)) {
|
|
24
|
+
if (!i || !a || !Object.hasOwn(e, a.type)) throw TypeError("Invalid interaction; use a named rule of type 'tap', 'hold', 'slow-tap' or 'double-press'");
|
|
25
|
+
n.state(a.action);
|
|
26
|
+
let { binding: r, time: o = e[a.type], gap: s = 300, count: c = 2, threshold: l = .5 } = a;
|
|
27
|
+
if (r !== void 0 && (!Number.isInteger(r) || r < 0)) throw TypeError("Interaction binding must be an index into the bindings of the action, an integer >= 0");
|
|
28
|
+
let u = a.type === "hold" || a.type === "slow-tap";
|
|
29
|
+
if (!Number.isFinite(o) || o < 0 || !u && o === 0) throw RangeError("Interaction time is in milliseconds; only hold and slow-tap may use 0");
|
|
30
|
+
if (!Number.isFinite(s) || s <= 0) throw RangeError("Interaction gap must be in milliseconds, above 0");
|
|
31
|
+
if (!Number.isInteger(c) || c < 2) throw RangeError("Interaction count must be an integer of at least 2");
|
|
32
|
+
if (!(l > 0 && l <= 1)) throw RangeError("Interaction threshold must be above 0 and up to 1");
|
|
33
|
+
let d = new t(a.type, a.action, r, o, s, c, l);
|
|
34
|
+
this.tracks.set(i, d), this.list.push(d);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
state(e) {
|
|
38
|
+
let t = this.tracks.get(e);
|
|
39
|
+
if (!t) throw TypeError("Unknown interaction; add it to the rules given to the constructor");
|
|
40
|
+
return t.state;
|
|
41
|
+
}
|
|
42
|
+
update() {
|
|
43
|
+
let e = this.now();
|
|
44
|
+
for (let t = 0; t < this.list.length; t++) this.step(this.list[t], e);
|
|
45
|
+
}
|
|
46
|
+
reset() {
|
|
47
|
+
for (let e of this.list) {
|
|
48
|
+
let t = e.state;
|
|
49
|
+
t.started = t.performed = t.canceled = t.active = !1, t.progress = t.duration = 0, e.spent = e.held;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
step(e, t) {
|
|
53
|
+
let n = e.state, r = Math.abs(e.binding === void 0 ? this.source.state(e.action).value : this.source.bindingValue(e.action, e.binding)), i = e.held, a = r >= (i ? e.threshold * .75 : e.threshold), o = i && !a;
|
|
54
|
+
if (e.held = a, n.started = n.performed = n.canceled = !1, e.spent) {
|
|
55
|
+
a || (e.spent = !1);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (!n.active) {
|
|
59
|
+
if (!a) return;
|
|
60
|
+
n.active = n.started = !0, n.progress = n.duration = 0, e.start = e.pressAt = t, e.presses = 1;
|
|
61
|
+
}
|
|
62
|
+
let s = t - e.start;
|
|
63
|
+
switch (n.duration = s, n.progress = e.time > 0 ? Math.min(1, s / e.time) : 1, e.type) {
|
|
64
|
+
case "hold":
|
|
65
|
+
a ? s >= e.time && this.end(e, !0) : this.end(e, !1);
|
|
66
|
+
break;
|
|
67
|
+
case "tap":
|
|
68
|
+
o ? this.end(e, s <= e.time) : s > e.time && this.end(e, !1);
|
|
69
|
+
break;
|
|
70
|
+
case "slow-tap":
|
|
71
|
+
o && this.end(e, s >= e.time);
|
|
72
|
+
break;
|
|
73
|
+
case "double-press": this.stepPresses(e, t, a, !i && a && !n.started, o);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
stepPresses(e, t, n, r, i) {
|
|
77
|
+
let a = e.state;
|
|
78
|
+
if (r) {
|
|
79
|
+
if (t - e.releaseAt > e.gap) {
|
|
80
|
+
this.end(e, !1), e.spent = !1;
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
e.presses++, e.pressAt = t;
|
|
84
|
+
}
|
|
85
|
+
a.progress = e.presses / e.count, n && e.presses >= e.count ? this.end(e, !0) : n || i ? t - e.pressAt > e.time ? this.end(e, !1) : i && (e.releaseAt = t) : t - e.releaseAt > e.gap && this.end(e, !1);
|
|
86
|
+
}
|
|
87
|
+
end(e, t) {
|
|
88
|
+
let n = e.state;
|
|
89
|
+
n.active = !1, n.performed = t, n.canceled = !t, t && (n.progress = 1), e.spent = e.held;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
//#endregion
|
|
93
|
+
export { n as Interactions };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
//#region src/labels.ts
|
|
2
|
+
var e = [
|
|
3
|
+
"A",
|
|
4
|
+
"B",
|
|
5
|
+
"X",
|
|
6
|
+
"Y",
|
|
7
|
+
"LB",
|
|
8
|
+
"RB",
|
|
9
|
+
"LT",
|
|
10
|
+
"RT",
|
|
11
|
+
"View",
|
|
12
|
+
"Menu",
|
|
13
|
+
"L3",
|
|
14
|
+
"R3",
|
|
15
|
+
"D-pad Up",
|
|
16
|
+
"D-pad Down",
|
|
17
|
+
"D-pad Left",
|
|
18
|
+
"D-pad Right",
|
|
19
|
+
"Guide"
|
|
20
|
+
], t = [
|
|
21
|
+
"Cross",
|
|
22
|
+
"Circle",
|
|
23
|
+
"Square",
|
|
24
|
+
"Triangle",
|
|
25
|
+
"L1",
|
|
26
|
+
"R1",
|
|
27
|
+
"L2",
|
|
28
|
+
"R2",
|
|
29
|
+
"Create",
|
|
30
|
+
"Options",
|
|
31
|
+
"L3",
|
|
32
|
+
"R3",
|
|
33
|
+
"D-pad Up",
|
|
34
|
+
"D-pad Down",
|
|
35
|
+
"D-pad Left",
|
|
36
|
+
"D-pad Right",
|
|
37
|
+
"PS"
|
|
38
|
+
], n = [
|
|
39
|
+
"Left Stick X",
|
|
40
|
+
"Left Stick Y",
|
|
41
|
+
"Right Stick X",
|
|
42
|
+
"Right Stick Y"
|
|
43
|
+
], r = {
|
|
44
|
+
shift: "Shift",
|
|
45
|
+
ctrl: "Ctrl",
|
|
46
|
+
alt: "Alt",
|
|
47
|
+
meta: "Meta"
|
|
48
|
+
}, i = [
|
|
49
|
+
"Left Mouse",
|
|
50
|
+
"Middle Mouse",
|
|
51
|
+
"Right Mouse",
|
|
52
|
+
"Mouse Back",
|
|
53
|
+
"Mouse Forward"
|
|
54
|
+
];
|
|
55
|
+
function a(e) {
|
|
56
|
+
return /^Key[A-Z]$/.test(e) ? e.slice(3) : /^Digit\d$/.test(e) ? e.slice(5) : /^Numpad\d$/.test(e) ? `Num ${e.slice(6)}` : e.startsWith("Arrow") ? e.slice(5) : e.replace(/(Left|Right)$/, (e) => ` ${e}`).replace(/([a-z])([A-Z])/g, "$1 $2");
|
|
57
|
+
}
|
|
58
|
+
function o(o, s = "xbox") {
|
|
59
|
+
switch (o.type) {
|
|
60
|
+
case "key": return [...(o.modifiers ?? []).map((e) => r[e]), a(o.code)].join("+");
|
|
61
|
+
case "mouse": return i[o.button] ?? `Mouse ${o.button + 1}`;
|
|
62
|
+
case "pointer": return o.axis === "wheel" ? "Mouse Wheel" : `Mouse ${o.axis.toUpperCase()}`;
|
|
63
|
+
case "gamepad-button": return (s === "xbox" ? e : s === "playstation" ? t : [])[o.index] ?? `Button ${o.index}`;
|
|
64
|
+
case "gamepad-axis": return n[o.index] ?? `Axis ${o.index}`;
|
|
65
|
+
case "touch": return o.axis ? `${o.id} ${o.axis.toUpperCase()}` : o.id;
|
|
66
|
+
case "control": return o.axis ? `${o.id} ${o.axis}` : o.id;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
export { o as t };
|
package/dist/labels.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Binding } from './index.js';
|
|
2
|
+
/** Button naming for {@link bindingLabel}: Xbox (A/B/X/Y), PlayStation (Cross/Circle/Square/Triangle) or generic numbers. */
|
|
3
|
+
export type GlyphLayout = 'xbox' | 'playstation' | 'generic';
|
|
4
|
+
/**
|
|
5
|
+
* Short on-screen prompt text for a binding, for "Press [Space] to jump" labels and rebinding menus.
|
|
6
|
+
* Chords join with `+` (`Ctrl+S`). Gamepad buttons use the names of `layout` (default `'xbox'`).
|
|
7
|
+
*/
|
|
8
|
+
export declare function bindingLabel(binding: Binding, layout?: GlyphLayout): string;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/** One tracked point. Image landmarks are normalised to the frame (x right, y down, 0..1); world ones are metres. */
|
|
2
|
+
export interface Landmark {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
z: number;
|
|
6
|
+
/** Likelihood the point is visible, 0..1, when the detector reports it. */
|
|
7
|
+
visibility?: number;
|
|
8
|
+
/** Likelihood the point is in the frame, 0..1, when the detector reports it. */
|
|
9
|
+
presence?: number;
|
|
10
|
+
}
|
|
11
|
+
/** One detected face. */
|
|
12
|
+
export interface FaceTrack {
|
|
13
|
+
/** Face mesh points in the MediaPipe Face Landmarker order (468 or 478). */
|
|
14
|
+
landmarks: Landmark[];
|
|
15
|
+
/** Detector blendshape scores by ARKit name (`jawOpen`, `eyeBlinkLeft`, ...), when it gives them. */
|
|
16
|
+
blendshapes?: Record<string, number>;
|
|
17
|
+
/** Column-major 4x4 face transform, when the detector gives one. */
|
|
18
|
+
transform?: ArrayLike<number>;
|
|
19
|
+
/** Detection confidence 0..1. Default 1. */
|
|
20
|
+
score?: number;
|
|
21
|
+
}
|
|
22
|
+
/** One detected hand, in the MediaPipe Hand Landmarker order (21 points, 0 the wrist). */
|
|
23
|
+
export interface HandTrack {
|
|
24
|
+
landmarks: Landmark[];
|
|
25
|
+
/** Metric points about the hand's centre, when the detector gives them. */
|
|
26
|
+
worldLandmarks?: Landmark[];
|
|
27
|
+
/** The subject's hand, as the detector labels it. */
|
|
28
|
+
handedness: 'left' | 'right';
|
|
29
|
+
/** Default 1. */
|
|
30
|
+
score?: number;
|
|
31
|
+
}
|
|
32
|
+
/** One detected body, in the MediaPipe Pose Landmarker order (33 points, 0 the nose, 23 and 24 the hips). */
|
|
33
|
+
export interface PoseTrack {
|
|
34
|
+
landmarks: Landmark[];
|
|
35
|
+
/** Metric points about the hips' centre, when the detector gives them. */
|
|
36
|
+
worldLandmarks?: Landmark[];
|
|
37
|
+
/** Default 1. */
|
|
38
|
+
score?: number;
|
|
39
|
+
}
|
|
40
|
+
/** Everything one detector pass saw. `time` is milliseconds on one clock (the video frame time). */
|
|
41
|
+
export interface LandmarkFrame {
|
|
42
|
+
time: number;
|
|
43
|
+
faces: FaceTrack[];
|
|
44
|
+
hands: HandTrack[];
|
|
45
|
+
poses: PoseTrack[];
|
|
46
|
+
}
|
|
47
|
+
/** An async iterator of frames that the consumer ends with `break`, the signal or `dispose`. */
|
|
48
|
+
export interface LandmarkSource extends AsyncIterable<LandmarkFrame>, Disposable {
|
|
49
|
+
/** End the source; a waiting `next()` resolves done. */
|
|
50
|
+
dispose(): void;
|
|
51
|
+
}
|
|
52
|
+
/** A pushable {@link LandmarkSource}. */
|
|
53
|
+
export interface LandmarkStream extends LandmarkSource {
|
|
54
|
+
/** Queue a frame; frames past `buffer` drop the oldest. @throws MargoError `disposed` after the stream ended. */
|
|
55
|
+
push(frame: LandmarkFrame): void;
|
|
56
|
+
/** End the stream after the queued frames are read. */
|
|
57
|
+
close(): void;
|
|
58
|
+
}
|
|
59
|
+
/** {@link landmarkStream} options. */
|
|
60
|
+
export interface LandmarkStreamOptions {
|
|
61
|
+
/** Frames held for a slow reader; older ones are dropped, so a reader always sees the newest. Default 1. */
|
|
62
|
+
buffer?: number;
|
|
63
|
+
/** Ends the stream. */
|
|
64
|
+
signal?: AbortSignal;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A source the app pushes frames into, from a detector callback or a recording; `for await` reads them.
|
|
68
|
+
* @throws RangeError unless `buffer` is a whole number of 1 or more.
|
|
69
|
+
*/
|
|
70
|
+
export declare function landmarkStream({ buffer, signal }?: LandmarkStreamOptions): LandmarkStream;
|
|
71
|
+
interface MPCategory {
|
|
72
|
+
categoryName?: string;
|
|
73
|
+
score?: number;
|
|
74
|
+
}
|
|
75
|
+
/** The fields `fromMediaPipe` reads from MediaPipe Tasks Vision results (FaceLandmarker, HandLandmarker, PoseLandmarker). */
|
|
76
|
+
export interface MediaPipeResults {
|
|
77
|
+
face?: {
|
|
78
|
+
faceLandmarks: Landmark[][];
|
|
79
|
+
faceBlendshapes?: {
|
|
80
|
+
categories: MPCategory[];
|
|
81
|
+
}[];
|
|
82
|
+
facialTransformationMatrixes?: {
|
|
83
|
+
data: ArrayLike<number>;
|
|
84
|
+
}[];
|
|
85
|
+
};
|
|
86
|
+
hands?: {
|
|
87
|
+
landmarks: Landmark[][];
|
|
88
|
+
worldLandmarks?: Landmark[][];
|
|
89
|
+
/** Newer releases name it `handedness`, older ones `handednesses`. */
|
|
90
|
+
handedness?: MPCategory[][];
|
|
91
|
+
handednesses?: MPCategory[][];
|
|
92
|
+
};
|
|
93
|
+
pose?: {
|
|
94
|
+
landmarks: Landmark[][];
|
|
95
|
+
worldLandmarks?: Landmark[][];
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/** {@link fromMediaPipe} options. */
|
|
99
|
+
export interface FromMediaPipeOptions {
|
|
100
|
+
/**
|
|
101
|
+
* Swap the hand labels. MediaPipe labels handedness as if the image were mirrored (a selfie view), so the default
|
|
102
|
+
* true turns its `Left` into the subject's right for an unmirrored webcam frame. Default true.
|
|
103
|
+
*/
|
|
104
|
+
mirroredHandedness?: boolean;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* One {@link LandmarkFrame} from MediaPipe Tasks results, so `detectForVideo` output plugs straight in.
|
|
108
|
+
* @param time The same ms timestamp passed to `detectForVideo`.
|
|
109
|
+
* @throws TypeError for a non-finite time or a result without its landmark arrays.
|
|
110
|
+
*/
|
|
111
|
+
export declare function fromMediaPipe(results: MediaPipeResults, time: number, { mirroredHandedness }?: FromMediaPipeOptions): LandmarkFrame;
|
|
112
|
+
/** The parts of a video element the webcam helper uses; an HTMLVideoElement fits. */
|
|
113
|
+
export interface WebcamVideo {
|
|
114
|
+
srcObject: unknown;
|
|
115
|
+
muted: boolean;
|
|
116
|
+
playsInline: boolean;
|
|
117
|
+
readonly currentTime: number;
|
|
118
|
+
play(): Promise<void>;
|
|
119
|
+
pause(): void;
|
|
120
|
+
requestVideoFrameCallback?(callback: (now: number, metadata: {
|
|
121
|
+
mediaTime: number;
|
|
122
|
+
}) => void): number;
|
|
123
|
+
cancelVideoFrameCallback?(handle: number): void;
|
|
124
|
+
}
|
|
125
|
+
/** A running webcam. */
|
|
126
|
+
export interface Webcam extends Disposable {
|
|
127
|
+
readonly video: WebcamVideo;
|
|
128
|
+
readonly stream: MediaStream;
|
|
129
|
+
/** Stop the frame loop and every track; idempotent. */
|
|
130
|
+
stop(): void;
|
|
131
|
+
}
|
|
132
|
+
/** {@link startWebcam} options. */
|
|
133
|
+
export interface WebcamOptions {
|
|
134
|
+
/** Called once per new video frame with its time in ms (`requestVideoFrameCallback`, else animation frames). */
|
|
135
|
+
onFrame?: (video: WebcamVideo, time: number) => void;
|
|
136
|
+
/** Video element to play into. Default a new detached `<video>`. */
|
|
137
|
+
video?: WebcamVideo;
|
|
138
|
+
/** Video constraints. Default `{ facingMode: 'user', width: 640, height: 480 }`. */
|
|
139
|
+
constraints?: MediaTrackConstraints;
|
|
140
|
+
/** Cancels the start and, later, stops the webcam. */
|
|
141
|
+
signal?: AbortSignal;
|
|
142
|
+
/** Media devices to ask. Default `navigator.mediaDevices`. */
|
|
143
|
+
mediaDevices?: Pick<MediaDevices, 'getUserMedia'>;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Open the camera, play it into a video element and call `onFrame` once per new frame until stopped.
|
|
147
|
+
* @throws MargoError `unsupported` without getUserMedia; DOMException from the browser (NotAllowedError) or AbortError.
|
|
148
|
+
*/
|
|
149
|
+
export declare function startWebcam({ onFrame, video, constraints, signal, mediaDevices, }?: WebcamOptions): Promise<Webcam>;
|
|
150
|
+
/** {@link webcamLandmarks} options. */
|
|
151
|
+
export interface WebcamLandmarksOptions extends Omit<WebcamOptions, 'onFrame'> {
|
|
152
|
+
/**
|
|
153
|
+
* The app's detector, run on each new frame: return a frame (for MediaPipe, `fromMediaPipe({ face:
|
|
154
|
+
* landmarker.detectForVideo(video, time) }, time)`), or null to skip it.
|
|
155
|
+
*/
|
|
156
|
+
detect: (video: WebcamVideo, time: number) => LandmarkFrame | null | undefined;
|
|
157
|
+
/** See {@link LandmarkStreamOptions.buffer}. Default 1. */
|
|
158
|
+
buffer?: number;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The webcam as a {@link LandmarkSource}: each new video frame goes through the app's detector. Ending the source stops
|
|
162
|
+
* the camera. A detector that throws ends the source and the error comes out of the next read.
|
|
163
|
+
*/
|
|
164
|
+
export declare function webcamLandmarks({ detect, buffer, ...options }: WebcamLandmarksOptions): Promise<LandmarkSource & {
|
|
165
|
+
readonly webcam: Webcam;
|
|
166
|
+
}>;
|
|
167
|
+
/** {@link OneEuroFilter} settings. */
|
|
168
|
+
export interface OneEuroOptions {
|
|
169
|
+
/** Cutoff in Hz at rest: lower removes more jitter when still. Default 1. */
|
|
170
|
+
minCutoff?: number;
|
|
171
|
+
/** How fast the cutoff rises with speed: higher removes more lag when moving. Default 0.007. */
|
|
172
|
+
beta?: number;
|
|
173
|
+
/** Cutoff in Hz for the speed estimate. Default 1. */
|
|
174
|
+
dCutoff?: number;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The one-euro filter (Casiez, Roussel and Vogel, CHI 2012): a low-pass filter whose cutoff rises with the signal's
|
|
178
|
+
* speed, so it removes jitter when still and lag when moving. One instance filters one scalar.
|
|
179
|
+
* @throws RangeError for a cutoff that is not above 0 or a negative beta.
|
|
180
|
+
*/
|
|
181
|
+
export declare class OneEuroFilter {
|
|
182
|
+
readonly minCutoff: number;
|
|
183
|
+
readonly beta: number;
|
|
184
|
+
readonly dCutoff: number;
|
|
185
|
+
private x;
|
|
186
|
+
private dx;
|
|
187
|
+
private t;
|
|
188
|
+
constructor({ minCutoff, beta, dCutoff }?: OneEuroOptions);
|
|
189
|
+
/**
|
|
190
|
+
* Filter `value` sampled at `time` seconds; the first sample (or one at or before the last time) passes through.
|
|
191
|
+
* @throws RangeError for a non-finite value or time.
|
|
192
|
+
*/
|
|
193
|
+
filter(value: number, time: number): number;
|
|
194
|
+
/** Forget the history; the next sample passes through. */
|
|
195
|
+
reset(): void;
|
|
196
|
+
}
|
|
197
|
+
export {};
|