@flighthq/input 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/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/inputManager.d.ts +175 -0
- package/dist/inputManager.d.ts.map +1 -0
- package/dist/inputManager.js +1031 -0
- package/dist/inputManager.js.map +1 -0
- package/package.json +38 -0
- package/src/inputManager.test.ts +1229 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import type { AttachInputOptions, GamepadAxisKind, GamepadButtonKind, GamepadMappingKind, InputKeyRepeatOptions, InputKeyRepeatTimer, InputManager, InputPointerData, InputSignals, InputState, MouseWheelMode } from '@flighthq/types';
|
|
2
|
+
/**
|
|
3
|
+
* Filters a single gamepad axis value through a simple dead zone.
|
|
4
|
+
* Values within `[-deadZone, deadZone]` are mapped to `0`; values outside
|
|
5
|
+
* are rescaled linearly to `[-1, 1]` so the live range is continuous.
|
|
6
|
+
* `deadZone` must be in `[0, 1)`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function applyGamepadAxisDeadZone(value: number, deadZone: number): number;
|
|
9
|
+
/**
|
|
10
|
+
* Filters a 2-D stick (left or right) through a **radial** dead zone.
|
|
11
|
+
* The magnitude of `(x, y)` is compared against `deadZone`; if within the
|
|
12
|
+
* dead zone the output is `(0, 0)`, otherwise the input direction is
|
|
13
|
+
* preserved and the magnitude is rescaled linearly to `[0, 1]`.
|
|
14
|
+
*
|
|
15
|
+
* Writes the filtered X and Y into `out.x` and `out.y`.
|
|
16
|
+
* Safe when `out` is the same object as the input (alias-safe).
|
|
17
|
+
*
|
|
18
|
+
* `deadZone` must be in `[0, 1)`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function applyGamepadStickDeadZone(out: {
|
|
21
|
+
x: number;
|
|
22
|
+
y: number;
|
|
23
|
+
}, x: number, y: number, deadZone: number): void;
|
|
24
|
+
export declare function attachGamepadInput(manager: InputManager, target: Window, options?: Readonly<AttachInputOptions>): void;
|
|
25
|
+
export declare function attachKeyboardInput(manager: InputManager, target: EventTarget, options?: Readonly<AttachInputOptions>): void;
|
|
26
|
+
export declare function attachPointerInput(manager: InputManager, element: HTMLElement, options?: Readonly<AttachInputOptions>): void;
|
|
27
|
+
export declare function attachRelativePointerInput(manager: InputManager, element: HTMLElement, options?: Readonly<AttachInputOptions>): void;
|
|
28
|
+
export declare function attachTextInput(manager: InputManager, element: HTMLElement, options?: Readonly<AttachInputOptions>): void;
|
|
29
|
+
export declare function attachWheelInput(manager: InputManager, element: HTMLElement, options?: Readonly<AttachInputOptions>): void;
|
|
30
|
+
/**
|
|
31
|
+
* Subscribes `state` to all signals on `manager` to maintain a live held-state snapshot.
|
|
32
|
+
* Also tracks per-frame edge sets (`justPressedKeys`, `justReleasedKeys`,
|
|
33
|
+
* `justPressedGamepadButtons`, `justReleasedGamepadButtons`) that accumulate
|
|
34
|
+
* until `endInputStateFrame` is called.
|
|
35
|
+
* Returns a disposer that disconnects the subscriptions.
|
|
36
|
+
*/
|
|
37
|
+
export declare function connectInputStateToInputManager(state: InputState, manager: InputManager): () => void;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a key-repeat timer for non-DOM sources (gamepad d-pad buttons,
|
|
40
|
+
* virtual on-screen keys, native backends) that do not generate their own
|
|
41
|
+
* auto-repeat events.
|
|
42
|
+
*
|
|
43
|
+
* Call `start(callback)` when a "key" is pressed. The `callback` is invoked
|
|
44
|
+
* immediately on press, then again after `options.delay` ms, then every
|
|
45
|
+
* `options.interval` ms until `stop()` is called.
|
|
46
|
+
*
|
|
47
|
+
* Returns a handle with `start(callback)` and `stop()` methods.
|
|
48
|
+
* The handle may be reused across multiple press/release cycles.
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* const timer = createInputKeyRepeatTimer({ delay: 500, interval: 33 });
|
|
52
|
+
* // on press:
|
|
53
|
+
* timer.start(() => emitSignal(manager.onKeyDown, dpadData));
|
|
54
|
+
* // on release:
|
|
55
|
+
* timer.stop();
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function createInputKeyRepeatTimer(options: Readonly<InputKeyRepeatOptions>): InputKeyRepeatTimer;
|
|
59
|
+
export declare function createInputManager(): InputManager;
|
|
60
|
+
export declare function createInputSignals(): InputSignals;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a fresh `InputState` with empty held-state maps/sets and empty
|
|
63
|
+
* frame-edge sets. Connect it to an `InputManager` via
|
|
64
|
+
* `connectInputStateToInputManager`, and call `endInputStateFrame` once per
|
|
65
|
+
* logical frame to roll the edge sets.
|
|
66
|
+
*/
|
|
67
|
+
export declare function createInputState(): InputState;
|
|
68
|
+
export declare function detachGamepadInput(manager: InputManager, target: Window): void;
|
|
69
|
+
export declare function detachKeyboardInput(manager: InputManager, target: EventTarget): void;
|
|
70
|
+
export declare function detachPointerInput(manager: InputManager, element: HTMLElement): void;
|
|
71
|
+
export declare function detachRelativePointerInput(manager: InputManager, element: HTMLElement): void;
|
|
72
|
+
export declare function detachTextInput(manager: InputManager, element: HTMLElement): void;
|
|
73
|
+
export declare function detachWheelInput(manager: InputManager, element: HTMLElement): void;
|
|
74
|
+
/**
|
|
75
|
+
* Rolls the per-frame edge sets on `state`, clearing `justPressedKeys`,
|
|
76
|
+
* `justReleasedKeys`, `justPressedGamepadButtons`, and
|
|
77
|
+
* `justReleasedGamepadButtons`. Call this once at the end of each logical
|
|
78
|
+
* frame (or input-poll cycle) to prepare the edge sets for the next frame.
|
|
79
|
+
*/
|
|
80
|
+
export declare function endInputStateFrame(state: InputState): void;
|
|
81
|
+
/**
|
|
82
|
+
* Exits pointer lock, returning the pointer to normal movement.
|
|
83
|
+
* No-op if pointer lock is not currently active.
|
|
84
|
+
*/
|
|
85
|
+
export declare function exitInputPointerLock(): void;
|
|
86
|
+
/**
|
|
87
|
+
* Returns coalesced pointer event data for a `pointermove` event, iterating
|
|
88
|
+
* over the high-frequency intermediate positions captured since the last
|
|
89
|
+
* delivered event. Falls back to a single entry with the event itself when
|
|
90
|
+
* `getCoalescedEvents` is unavailable (e.g. in jsdom).
|
|
91
|
+
*
|
|
92
|
+
* The callback receives each coalesced `InputPointerData` in order. The
|
|
93
|
+
* payload object is reused across calls — do not retain a reference to it.
|
|
94
|
+
*/
|
|
95
|
+
export declare function getCoalescedInputPointerEvents(event: PointerEvent, callback: (data: Readonly<InputPointerData>) => void): void;
|
|
96
|
+
/**
|
|
97
|
+
* Returns the semantic name string (a `GamepadAxisKind`) for `index` in the
|
|
98
|
+
* standard gamepad mapping, or `null` if `mapping` is not `'standard'` or
|
|
99
|
+
* `index` is out of the standard range.
|
|
100
|
+
*/
|
|
101
|
+
export declare function getGamepadAxisName(mapping: GamepadMappingKind, index: number): GamepadAxisKind | null;
|
|
102
|
+
/**
|
|
103
|
+
* Returns the semantic name string (a `GamepadButtonKind`) for `index` in the
|
|
104
|
+
* standard gamepad mapping, or `null` if `mapping` is not `'standard'` or
|
|
105
|
+
* `index` is out of the standard range.
|
|
106
|
+
*/
|
|
107
|
+
export declare function getGamepadButtonName(mapping: GamepadMappingKind, index: number): GamepadButtonKind | null;
|
|
108
|
+
/**
|
|
109
|
+
* Returns the current value of a gamepad axis from `state`, or `0` if not recorded.
|
|
110
|
+
* `gamepad` is the gamepad index; `axis` is the axis index.
|
|
111
|
+
*/
|
|
112
|
+
export declare function getInputGamepadAxis(state: Readonly<InputState>, gamepad: number, axis: number): number;
|
|
113
|
+
export declare function getKeyCodeFromDomKeyboardEvent(event: Readonly<KeyboardEvent>): number;
|
|
114
|
+
export declare function getKeyModifierFromDomKeyboardEvent(event: Readonly<KeyboardEvent>): number;
|
|
115
|
+
export declare function getMouseWheelModeFromDomWheelEvent(event: Readonly<WheelEvent>): MouseWheelMode;
|
|
116
|
+
/**
|
|
117
|
+
* Returns `true` if pointer lock is currently active on any element.
|
|
118
|
+
*/
|
|
119
|
+
export declare function hasInputPointerLock(): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Returns `true` if the given gamepad button is currently held.
|
|
122
|
+
* `gamepad` is the gamepad index; `button` is the button index.
|
|
123
|
+
*/
|
|
124
|
+
export declare function isInputGamepadButtonDown(state: Readonly<InputState>, gamepad: number, button: number): boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Returns `true` if the given `keyCode` (from `KeyCode`) is currently held.
|
|
127
|
+
*/
|
|
128
|
+
export declare function isInputKeyDown(state: Readonly<InputState>, keyCode: number): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Returns `true` if the given pointer button is currently held for the given `pointerId`.
|
|
131
|
+
* `button` corresponds to `MouseEvent.button` (0 = primary, 1 = middle, 2 = secondary, …).
|
|
132
|
+
*/
|
|
133
|
+
export declare function isInputPointerButtonDown(state: Readonly<InputState>, pointerId: number, button: number): boolean;
|
|
134
|
+
export declare function pollGamepadInput(manager: InputManager): void;
|
|
135
|
+
/**
|
|
136
|
+
* Releases pointer capture for `pointerId` from `element`, allowing pointer
|
|
137
|
+
* events to fire on the element under the pointer again.
|
|
138
|
+
* No-op if `element` does not have capture for this pointer.
|
|
139
|
+
*/
|
|
140
|
+
export declare function releaseInputPointerCapture(element: HTMLElement, pointerId: number): void;
|
|
141
|
+
/**
|
|
142
|
+
* Requests pointer lock on `element`. Returns a `Promise<boolean>` that
|
|
143
|
+
* resolves to `true` when lock is granted or `false` if the request is
|
|
144
|
+
* rejected (e.g. outside a user gesture, or the browser denies it).
|
|
145
|
+
*/
|
|
146
|
+
export declare function requestInputPointerLock(element: HTMLElement): Promise<boolean>;
|
|
147
|
+
/**
|
|
148
|
+
* Explicitly captures all pointer events for `pointerId` to `element`,
|
|
149
|
+
* regardless of where the pointer moves. Useful for drag operations.
|
|
150
|
+
* Automatically released on `pointerup` or `pointercancel` per the spec.
|
|
151
|
+
*/
|
|
152
|
+
export declare function setInputPointerCapture(element: HTMLElement, pointerId: number): void;
|
|
153
|
+
/**
|
|
154
|
+
* Returns `true` if the gamepad button at `gamepad`/`button` was pressed
|
|
155
|
+
* this frame (i.e. transitioned from up → down since the last
|
|
156
|
+
* `endInputStateFrame` call).
|
|
157
|
+
*/
|
|
158
|
+
export declare function wasInputGamepadButtonPressed(state: Readonly<InputState>, gamepad: number, button: number): boolean;
|
|
159
|
+
/**
|
|
160
|
+
* Returns `true` if the gamepad button at `gamepad`/`button` was released
|
|
161
|
+
* this frame (i.e. transitioned from down → up since the last
|
|
162
|
+
* `endInputStateFrame` call).
|
|
163
|
+
*/
|
|
164
|
+
export declare function wasInputGamepadButtonReleased(state: Readonly<InputState>, gamepad: number, button: number): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Returns `true` if the key with `keyCode` was pressed this frame (i.e.
|
|
167
|
+
* transitioned from up → down since the last `endInputStateFrame` call).
|
|
168
|
+
*/
|
|
169
|
+
export declare function wasInputKeyPressed(state: Readonly<InputState>, keyCode: number): boolean;
|
|
170
|
+
/**
|
|
171
|
+
* Returns `true` if the key with `keyCode` was released this frame (i.e.
|
|
172
|
+
* transitioned from down → up since the last `endInputStateFrame` call).
|
|
173
|
+
*/
|
|
174
|
+
export declare function wasInputKeyReleased(state: Readonly<InputState>, keyCode: number): boolean;
|
|
175
|
+
//# sourceMappingURL=inputManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inputManager.d.ts","sourceRoot":"","sources":["../src/inputManager.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EAC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