@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.
@@ -0,0 +1,44 @@
1
+ import { Input, type Binding, type InputOptions } from './index.js';
2
+ /** Two scalar binding composites form one stick. Positive y is down, matching gamepads. */
3
+ export interface VectorBinding {
4
+ /** Bindings summed into x, before the radial processor. */
5
+ x: readonly Binding[];
6
+ /** Bindings summed into y, before the radial processor. */
7
+ y: readonly Binding[];
8
+ /** Inner radius, rescaled continuously to unit magnitude. Default 0.15. */
9
+ deadzone?: number;
10
+ /** Convert every nonzero result to unit length. Default false: preserve analog magnitude. */
11
+ normalize?: boolean;
12
+ }
13
+ /** Reused vector state, refreshed by VectorInput.update. */
14
+ export interface VectorActionState {
15
+ x: number;
16
+ y: number;
17
+ magnitude: number;
18
+ down: boolean;
19
+ pressed: boolean;
20
+ released: boolean;
21
+ }
22
+ /** Options for VectorInput. Axis deadzones are replaced by each vector's radial deadzone. */
23
+ export interface VectorInputOptions extends Omit<InputOptions, 'deadzone'> {
24
+ /** Additional scalar actions, sharing devices, contexts and replay. Default none. */
25
+ actions?: Readonly<Record<string, readonly Binding[]>>;
26
+ }
27
+ /** WASD composite with a downward-positive y axis. Opposite keys cancel. */
28
+ export declare function wasd(): VectorBinding;
29
+ /**
30
+ * Named vector actions on the standard Input device/context/rebinding/recording path.
31
+ * Scalar channels are named `<name>.x` and `<name>.y`; contexts and rebinding use those names.
32
+ * InputRecorder records this object directly and InputReplay plays into it unchanged.
33
+ */
34
+ export declare class VectorInput extends Input {
35
+ private readonly vectors;
36
+ /** @throws RangeError for an invalid deadzone; TypeError for empty names, axes or channel collisions. */
37
+ constructor(vectors: Readonly<Record<string, VectorBinding>>, { actions, ...options }?: VectorInputOptions);
38
+ /** @throws TypeError for an unknown name; define it in the constructor's vector map. */
39
+ vector(name: string): Readonly<VectorActionState>;
40
+ /** Sample devices once, then apply one radial deadzone and unit-circle clamp per vector. */
41
+ update(): void;
42
+ /** Reset devices and vector state immediately, including on window blur. */
43
+ reset(): void;
44
+ }
package/dist/vector.js ADDED
@@ -0,0 +1,66 @@
1
+ import { t as e } from "./src-eYqyrCA7.js";
2
+ //#region src/vector.ts
3
+ function t() {
4
+ return {
5
+ x: [{
6
+ type: "key",
7
+ code: "KeyD"
8
+ }, {
9
+ type: "key",
10
+ code: "KeyA",
11
+ scale: -1
12
+ }],
13
+ y: [{
14
+ type: "key",
15
+ code: "KeyS"
16
+ }, {
17
+ type: "key",
18
+ code: "KeyW",
19
+ scale: -1
20
+ }]
21
+ };
22
+ }
23
+ var n = class extends e {
24
+ constructor(e, { actions: t = {}, ...n } = {}) {
25
+ let r = { ...t };
26
+ for (let [t, n] of Object.entries(e)) {
27
+ if (!t || !n?.x?.length || !n?.y?.length || `${t}.x` in r || `${t}.y` in r) throw TypeError("Invalid vector action; use a name, two nonempty binding arrays and unique scalar channels");
28
+ let e = n.deadzone ?? .15;
29
+ if (!Number.isFinite(e) || e < 0 || e >= 1) throw RangeError("Invalid radial deadzone; use a finite radius in [0, 1)");
30
+ r[`${t}.x`] = n.x, r[`${t}.y`] = n.y;
31
+ }
32
+ super(r, {
33
+ ...n,
34
+ deadzone: 0
35
+ }), this.vectors = /* @__PURE__ */ new Map();
36
+ for (let [t, n] of Object.entries(e)) this.vectors.set(t, {
37
+ binding: { ...n },
38
+ state: {
39
+ x: 0,
40
+ y: 0,
41
+ magnitude: 0,
42
+ down: !1,
43
+ pressed: !1,
44
+ released: !1
45
+ }
46
+ });
47
+ }
48
+ vector(e) {
49
+ let t = this.vectors.get(e);
50
+ if (!t) throw TypeError("Unknown vector action; define it in the vector map first");
51
+ return t.state;
52
+ }
53
+ update() {
54
+ super.update();
55
+ for (let [e, { binding: t, state: n }] of this.vectors) {
56
+ let r = this.state(`${e}.x`).value, i = this.state(`${e}.y`).value, a = Math.hypot(r, i), o = t.deadzone ?? .15, s = a <= o ? 0 : t.normalize ? 1 : (Math.min(1, a) - o) / (1 - o), c = s > 1e-6;
57
+ n.pressed = c && !n.down, n.released = !c && n.down, n.down = c, n.x = s ? r / a * s : 0, n.y = s ? i / a * s : 0, n.magnitude = s;
58
+ }
59
+ }
60
+ reset() {
61
+ super.reset();
62
+ for (let { state: e } of this.vectors?.values() ?? []) e.x = e.y = e.magnitude = 0, e.released = e.down, e.down = e.pressed = !1;
63
+ }
64
+ };
65
+ //#endregion
66
+ export { n as VectorInput, t as wasd };
package/dist/xr.d.ts ADDED
@@ -0,0 +1,124 @@
1
+ import type { Binding } from './index.js';
2
+ import type { ControlHost } from './motion.js';
3
+ /** Structural WebXR source, compatible with native XRInputSource without an ambient XR dependency. */
4
+ export interface InputXRSource {
5
+ handedness: 'left' | 'right' | 'none';
6
+ gamepad?: {
7
+ readonly axes: readonly number[];
8
+ readonly buttons: readonly {
9
+ value: number;
10
+ pressed: boolean;
11
+ touched: boolean;
12
+ }[];
13
+ } | null;
14
+ hand?: {
15
+ get(name: string): object | undefined;
16
+ } | null;
17
+ gripSpace?: object | null;
18
+ }
19
+ /** Structural session exposing native input sources and select/squeeze lifecycle events. */
20
+ export interface InputXRSession extends EventTarget {
21
+ readonly inputSources: Iterable<InputXRSource>;
22
+ }
23
+ /** Structural frame used only for optional hand joints and grip pose channels. */
24
+ export interface InputXRFrame {
25
+ getJointPose?(joint: object, space: object): {
26
+ transform: {
27
+ position: {
28
+ x: number;
29
+ y: number;
30
+ z: number;
31
+ };
32
+ };
33
+ } | null;
34
+ getPose?(grip: object, space: object): {
35
+ transform: {
36
+ position: {
37
+ x: number;
38
+ y: number;
39
+ z: number;
40
+ };
41
+ orientation: {
42
+ x: number;
43
+ y: number;
44
+ z: number;
45
+ w: number;
46
+ };
47
+ };
48
+ } | null;
49
+ }
50
+ /** Source filter. Index selects the nth source matching handedness in session order (default 0). */
51
+ export interface XRBindingSource {
52
+ /** Default any hand. */
53
+ hand?: 'left' | 'right' | 'none';
54
+ /** Default 0. */
55
+ source?: number;
56
+ /** Default 1, applied by the ordinary Input binding. */
57
+ scale?: number;
58
+ }
59
+ /**
60
+ * XR-specific descriptors, converted by XRBindings.binding or map to standard replayable control bindings.
61
+ * An action sums and clamps to [-1, 1], so an `xr-grip-pose` position beyond 1 m reads 1 there; read metres with
62
+ * `input.bindingValue(action, index)`, which is not clamped.
63
+ */
64
+ export type XRBinding = XRBindingSource & ({
65
+ type: 'xr-button';
66
+ index: number;
67
+ value?: 'value' | 'pressed' | 'touched';
68
+ } | {
69
+ type: 'xr-axis';
70
+ index: number;
71
+ } | {
72
+ type: 'xr-select' | 'xr-grip' | 'xr-pinch';
73
+ } | {
74
+ type: 'xr-grip-pose';
75
+ component: 'x' | 'y' | 'z' | 'qx' | 'qy' | 'qz' | 'qw' | 'tracked';
76
+ });
77
+ /** Pinch hysteresis uses joint-centre distances in metres. */
78
+ export interface XRBindingsOptions {
79
+ /** Distance that starts a pinch. Default 0.02 m. */
80
+ pinchStart?: number;
81
+ /** Distance that ends a pinch. Default 0.03 m. Must exceed pinchStart. */
82
+ pinchEnd?: number;
83
+ /** Control id shared by binding() and attach(). Default 'xr'. Use distinct ids for multiple publishers. */
84
+ id?: string;
85
+ }
86
+ /**
87
+ * Publish XR buttons, axes, select, squeeze/grip and hysteretic hand pinch into any Input or VectorInput.
88
+ * Call update(frame, referenceSpace) before input.update(). No frame is needed for controllers or select events.
89
+ * Record the Input with InputRecorder, including binding-specific interactions; replay needs no XR session.
90
+ */
91
+ export declare class XRBindings {
92
+ private readonly session;
93
+ private readonly entries;
94
+ private readonly hosts;
95
+ private readonly selecting;
96
+ private readonly squeezing;
97
+ private readonly selectTaps;
98
+ private readonly gripTaps;
99
+ private readonly pinching;
100
+ private readonly cleanups;
101
+ private readonly start;
102
+ private readonly end;
103
+ private readonly id;
104
+ private disposed;
105
+ private ended;
106
+ /** @throws RangeError for invalid distances; TypeError for an empty control id. */
107
+ constructor(session: InputXRSession, { pinchStart, pinchEnd, id }?: XRBindingsOptions);
108
+ private listen;
109
+ private check;
110
+ /** Convert a typed XR descriptor to a standard Input binding. @throws TypeError for an invalid descriptor. */
111
+ binding(binding: XRBinding): Binding;
112
+ /** Convert a mixed native/XR action map, ready for the Input constructor or importBindings. */
113
+ map(actions: Readonly<Record<string, readonly (XRBinding | Binding)[]>>): Record<string, Binding[]>;
114
+ /** Publish to an Input's controls. Returns a detach function; a host may only attach once. */
115
+ attach(host: ControlHost): () => void;
116
+ /** Sample sources; missing/untracked/disconnected inputs read zero. @throws MargoError after disposal. */
117
+ update(frame?: InputXRFrame, space?: object): void;
118
+ private clear;
119
+ private publishZero;
120
+ /** Remove all listeners and published controls; safe to repeat. */
121
+ dispose(): void;
122
+ /** Deterministic disposal for using declarations. */
123
+ [Symbol.dispose](): void;
124
+ }
package/dist/xr.js ADDED
@@ -0,0 +1,154 @@
1
+ import { MargoError as e } from "@margoengine/math/errors";
2
+ //#region src/xr.ts
3
+ var t = class {
4
+ constructor(e, { pinchStart: t = .02, pinchEnd: n = .03, id: r = "xr" } = {}) {
5
+ if (this.session = e, this.entries = [], this.hosts = /* @__PURE__ */ new Map(), this.selecting = /* @__PURE__ */ new Set(), this.squeezing = /* @__PURE__ */ new Set(), this.selectTaps = /* @__PURE__ */ new Set(), this.gripTaps = /* @__PURE__ */ new Set(), this.pinching = /* @__PURE__ */ new Set(), this.cleanups = [], this.disposed = !1, this.ended = !1, !Number.isFinite(t + n) || t <= 0 || n <= t) throw RangeError("Invalid pinch distances; use 0 < pinchStart < pinchEnd in metres");
6
+ if (!r) throw TypeError("Empty XR control id; give the publisher a unique name");
7
+ this.start = t, this.end = n, this.id = r;
8
+ for (let [e, t, n] of [[
9
+ "select",
10
+ this.selecting,
11
+ this.selectTaps
12
+ ], [
13
+ "squeeze",
14
+ this.squeezing,
15
+ this.gripTaps
16
+ ]]) this.listen(`${e}start`, (e) => {
17
+ let r = e.inputSource;
18
+ t.add(r), n.add(r);
19
+ }), this.listen(`${e}end`, (e) => t.delete(e.inputSource));
20
+ this.listen("end", () => {
21
+ this.ended = !0, this.clear(), this.publishZero();
22
+ }), this.listen("inputsourceschange", () => {
23
+ let t = new Set(e.inputSources);
24
+ for (let e of [
25
+ this.selecting,
26
+ this.squeezing,
27
+ this.selectTaps,
28
+ this.gripTaps,
29
+ this.pinching
30
+ ]) for (let n of e) t.has(n) || e.delete(n);
31
+ });
32
+ }
33
+ listen(e, t) {
34
+ this.session.addEventListener(e, t), this.cleanups.push(() => this.session.removeEventListener(e, t));
35
+ }
36
+ check() {
37
+ if (this.disposed) throw new e("disposed", "XR bindings are disposed; create a new XRBindings");
38
+ }
39
+ binding(e) {
40
+ if (this.check(), !e || ![
41
+ "xr-button",
42
+ "xr-axis",
43
+ "xr-select",
44
+ "xr-grip",
45
+ "xr-pinch",
46
+ "xr-grip-pose"
47
+ ].includes(e.type) || !Number.isInteger(e.source ?? 0) || (e.source ?? 0) < 0 || !Number.isFinite(e.scale ?? 1) || e.hand !== void 0 && ![
48
+ "left",
49
+ "right",
50
+ "none"
51
+ ].includes(e.hand) || (e.type === "xr-button" || e.type === "xr-axis") && (!Number.isInteger(e.index) || e.index < 0) || e.type === "xr-button" && e.value !== void 0 && ![
52
+ "value",
53
+ "pressed",
54
+ "touched"
55
+ ].includes(e.value) || e.type === "xr-grip-pose" && ![
56
+ "x",
57
+ "y",
58
+ "z",
59
+ "qx",
60
+ "qy",
61
+ "qz",
62
+ "qw",
63
+ "tracked"
64
+ ].includes(e.component)) throw TypeError("Invalid XR binding; use a supported type, nonnegative integer source/index and finite scale");
65
+ let t = `b${this.entries.length}`;
66
+ return this.entries.push({
67
+ binding: { ...e },
68
+ axis: t
69
+ }), {
70
+ type: "control",
71
+ id: this.id,
72
+ axis: t,
73
+ scale: e.scale
74
+ };
75
+ }
76
+ map(e) {
77
+ return this.check(), Object.fromEntries(Object.entries(e).map(([e, t]) => [e, t.map((e) => e.type.startsWith("xr-") ? this.binding(e) : e)]));
78
+ }
79
+ attach(e) {
80
+ if (this.check(), this.hosts.has(e)) throw TypeError("XR host already attached; detach it before attaching again");
81
+ return this.hosts.set(e, e.control(this.id)), () => {
82
+ this.hosts.delete(e) && e.removeControl(this.id);
83
+ };
84
+ }
85
+ update(e, t) {
86
+ this.check();
87
+ let n = this.ended ? [] : Array.from(this.session.inputSources), r = new Set(n);
88
+ for (let e of [
89
+ this.selecting,
90
+ this.squeezing,
91
+ this.selectTaps,
92
+ this.gripTaps,
93
+ this.pinching
94
+ ]) for (let t of e) r.has(t) || e.delete(t);
95
+ for (let r of n) {
96
+ let n = r.hand?.get("thumb-tip"), i = r.hand?.get("index-finger-tip"), a = n && t ? e?.getJointPose?.(n, t)?.transform.position : void 0, o = i && t ? e?.getJointPose?.(i, t)?.transform.position : void 0;
97
+ (a && o ? Math.hypot(a.x - o.x, a.y - o.y, a.z - o.z) : Infinity) <= (this.pinching.has(r) ? this.end : this.start) ? this.pinching.add(r) : this.pinching.delete(r);
98
+ }
99
+ for (let { binding: r, axis: i } of this.entries) {
100
+ let a = n.filter((e) => r.hand === void 0 || e.handedness === r.hand)[r.source ?? 0], o = 0;
101
+ if (a) switch (r.type) {
102
+ case "xr-button": {
103
+ let e = a.gamepad?.buttons[r.index];
104
+ o = Number(e?.[r.value ?? "value"] ?? 0);
105
+ break;
106
+ }
107
+ case "xr-axis":
108
+ o = a.gamepad?.axes[r.index] ?? 0;
109
+ break;
110
+ case "xr-select":
111
+ o = +(this.selecting.has(a) || this.selectTaps.has(a));
112
+ break;
113
+ case "xr-grip":
114
+ o = +(this.squeezing.has(a) || this.gripTaps.has(a));
115
+ break;
116
+ case "xr-pinch":
117
+ o = +this.pinching.has(a);
118
+ break;
119
+ case "xr-grip-pose": {
120
+ let n = a.gripSpace && t ? e?.getPose?.(a.gripSpace, t) : null;
121
+ o = r.component === "tracked" ? +!!n : n ? r.component.startsWith("q") ? n.transform.orientation[r.component.slice(1)] : n.transform.position[r.component] : 0;
122
+ break;
123
+ }
124
+ }
125
+ for (let e of this.hosts.values()) e[i] = Number.isFinite(o) ? o : 0;
126
+ }
127
+ this.selectTaps.clear(), this.gripTaps.clear();
128
+ }
129
+ clear() {
130
+ for (let e of [
131
+ this.selecting,
132
+ this.squeezing,
133
+ this.selectTaps,
134
+ this.gripTaps,
135
+ this.pinching
136
+ ]) e.clear();
137
+ }
138
+ publishZero() {
139
+ for (let e of this.hosts.values()) for (let { axis: t } of this.entries) e[t] = 0;
140
+ }
141
+ dispose() {
142
+ if (!this.disposed) {
143
+ this.disposed = !0, this.clear();
144
+ for (let e of this.cleanups) e();
145
+ for (let e of this.hosts.keys()) e.removeControl(this.id);
146
+ this.hosts.clear();
147
+ }
148
+ }
149
+ [Symbol.dispose]() {
150
+ this.dispose();
151
+ }
152
+ };
153
+ //#endregion
154
+ export { t as XRBindings };
package/package.json CHANGED
@@ -1,6 +1,105 @@
1
- {
2
- "name": "@margoengine/input",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@margoengine/input",
3
+ "version": "0.1.0",
4
+ "description": "Semantic keyboard, pointer, gamepad, touch, gyro and pen actions with haptics, tap, hold, slow tap and double press rules, local multiplayer, and recording and replay.",
5
+ "keywords": [
6
+ "margoengine",
7
+ "webgpu",
8
+ "3d",
9
+ "input"
10
+ ],
11
+ "homepage": "https://margoengine.com",
12
+ "bugs": {
13
+ "url": "https://github.com/scottcampbelldata/margoengine/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/scottcampbelldata/margoengine.git",
18
+ "directory": "packages/input"
19
+ },
20
+ "license": "Apache-2.0",
21
+ "author": "Scott Campbell",
22
+ "type": "module",
23
+ "sideEffects": false,
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js"
28
+ },
29
+ "./interactions": {
30
+ "types": "./dist/interactions.d.ts",
31
+ "import": "./dist/interactions.js"
32
+ },
33
+ "./players": {
34
+ "types": "./dist/players.d.ts",
35
+ "import": "./dist/players.js"
36
+ },
37
+ "./motion": {
38
+ "types": "./dist/motion.d.ts",
39
+ "import": "./dist/motion.js"
40
+ },
41
+ "./replay": {
42
+ "types": "./dist/replay.d.ts",
43
+ "import": "./dist/replay.js"
44
+ },
45
+ "./pen": {
46
+ "types": "./dist/pen.d.ts",
47
+ "import": "./dist/pen.js"
48
+ },
49
+ "./vector": {
50
+ "types": "./dist/vector.d.ts",
51
+ "import": "./dist/vector.js"
52
+ },
53
+ "./xr": {
54
+ "types": "./dist/xr.d.ts",
55
+ "import": "./dist/xr.js"
56
+ },
57
+ "./lock": {
58
+ "types": "./dist/lock.d.ts",
59
+ "import": "./dist/lock.js"
60
+ },
61
+ "./layout": {
62
+ "types": "./dist/layout.d.ts",
63
+ "import": "./dist/layout.js"
64
+ },
65
+ "./haptics": {
66
+ "types": "./dist/haptics.d.ts",
67
+ "import": "./dist/haptics.js"
68
+ },
69
+ "./landmarks": {
70
+ "types": "./dist/landmarks.d.ts",
71
+ "import": "./dist/landmarks.js"
72
+ },
73
+ "./face": {
74
+ "types": "./dist/face.d.ts",
75
+ "import": "./dist/face.js"
76
+ },
77
+ "./body": {
78
+ "types": "./dist/body.d.ts",
79
+ "import": "./dist/body.js"
80
+ }
81
+ },
82
+ "files": [
83
+ "dist",
84
+ "LICENSE",
85
+ "NOTICE"
86
+ ],
87
+ "publishConfig": {
88
+ "access": "public"
89
+ },
90
+ "peerDependencies": {
91
+ "@margoengine/core": "^0.1.0"
92
+ },
93
+ "dependencies": {
94
+ "@margoengine/math": "^0.1.0"
95
+ },
96
+ "scripts": {
97
+ "build": "node ../../scripts/build-optional.js input",
98
+ "test": "npm run build && node --test test/*.test.js",
99
+ "prepack": "npm run build",
100
+ "test:browser": "cd ../.. && node packages/input/test/input-browser.mjs"
101
+ },
102
+ "margo": {
103
+ "gpuTestTimeoutMinutes": 2
104
+ }
105
+ }