@asyra/input-system 0.2.5 → 0.5.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 +21 -0
- package/README.md +42 -48
- package/dist/event-mappings.d.ts +37 -2
- package/dist/event-mappings.js +1 -78
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -3
- package/dist/input-system.d.ts +11 -0
- package/dist/input-system.js +133 -11
- package/dist/registry.d.ts +39 -0
- package/dist/registry.js +57 -0
- package/dist/subscribe.js +14 -2
- package/package.json +22 -4
- package/.turbo/turbo-test$colon$ci.log +0 -132
- package/.turbo/turbo-test$colon$local.log +0 -52
- package/.turbo/turbo-test.log +0 -0
- package/CHANGELOG.md +0 -46
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/clover.xml +0 -573
- package/coverage/constants.ts.html +0 -94
- package/coverage/coverage-final.json +0 -8963
- package/coverage/event-mappings.ts.html +0 -370
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +0 -206
- package/coverage/index.ts.html +0 -106
- package/coverage/input-system.ts.html +0 -1216
- package/coverage/keymap.ts.html +0 -616
- package/coverage/matcher.ts.html +0 -187
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -196
- package/coverage/subscribe.ts.html +0 -109
- package/dist/__tests__/input-system.test.d.ts +0 -1
- package/dist/__tests__/input-system.test.js +0 -279
- package/dist/matcher.d.ts +0 -16
- package/dist/matcher.js +0 -32
- package/src/__tests__/input-system.test.ts +0 -398
- package/src/constants.ts +0 -3
- package/src/event-mappings.ts +0 -95
- package/src/index.ts +0 -7
- package/src/input-system.ts +0 -377
- package/src/keymap.ts +0 -177
- package/src/matcher.ts +0 -34
- package/src/subscribe.ts +0 -8
- package/test-setup.ts +0 -6
- package/tsconfig.json +0 -22
- package/vitest.config.ts +0 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Asa Tsai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -27,61 +27,55 @@ yarn add @asyra/input-system
|
|
|
27
27
|
|
|
28
28
|
## Usage
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
Create an instance of the Input System with your defined combinations:
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
const inputSystem = new InputSystem(combinations)
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
4. **Attach Event Listeners**:
|
|
66
|
-
|
|
67
|
-
Use the `on` method to listen for specific actions:
|
|
30
|
+
The default browser singleton listens for raw input. Apps define named events by
|
|
31
|
+
registering typed combinations, then attach and release listeners by callback
|
|
32
|
+
identity:
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import inputSystem, { keyMap } from '@asyra/input-system'
|
|
36
|
+
import { InputType, ModifierKey, PointerKey } from '@asyra/utils'
|
|
37
|
+
|
|
38
|
+
inputSystem.registry.registerKeyCombinations({
|
|
39
|
+
UNDO: [
|
|
40
|
+
{
|
|
41
|
+
type: InputType.KEYBOARD,
|
|
42
|
+
keys: [keyMap.keys.KeyZ],
|
|
43
|
+
modifiers: [ModifierKey.META]
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
DRAG_START: [
|
|
47
|
+
{
|
|
48
|
+
type: InputType.POINTER,
|
|
49
|
+
keys: [PointerKey.LEFT_MOUSE_DOWN]
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const handleUndo = () => console.log('Undo triggered')
|
|
55
|
+
|
|
56
|
+
inputSystem.on('UNDO', handleUndo)
|
|
57
|
+
inputSystem.off('UNDO', handleUndo)
|
|
58
|
+
```
|
|
68
59
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
inputSystem.on('DRAG_UPDATE', () => console.log('Drag Update'))
|
|
73
|
-
inputSystem.on('DRAG_END', () => console.log('Drag End'))
|
|
74
|
-
inputSystem.on('UNDO', () => console.log('Undo triggered'))
|
|
75
|
-
inputSystem.on('REDO', () => console.log('Redo triggered'))
|
|
76
|
-
```
|
|
60
|
+
Event names and combinations are app-owned. Register each event name once for a
|
|
61
|
+
given registry. `off(...)` removes only the supplied listener and returns
|
|
62
|
+
`false` when that listener is not registered.
|
|
77
63
|
|
|
78
64
|
## Contributing
|
|
79
65
|
|
|
80
|
-
|
|
66
|
+
This repository is not accepting external issues or pull requests at this time.
|
|
67
|
+
You are welcome to fork the package and adapt it for your own application; see
|
|
68
|
+
the repository root README for the current contribution policy.
|
|
81
69
|
|
|
82
70
|
## License
|
|
83
71
|
|
|
84
|
-
This project is licensed under the MIT License.
|
|
72
|
+
This project is licensed under the MIT License.
|
|
73
|
+
|
|
74
|
+
## Release support
|
|
75
|
+
|
|
76
|
+
The `@asyra/input-system` `0.2.5` ESM artifact supports Node.js 24.x. Use only
|
|
77
|
+
package-root exports. See the
|
|
78
|
+
[Framework release support contract](../../docs/ai/framework/RELEASE_SUPPORT.md).
|
|
85
79
|
|
|
86
80
|
## Acknowledgments
|
|
87
81
|
|
package/dist/event-mappings.d.ts
CHANGED
|
@@ -1,8 +1,43 @@
|
|
|
1
|
-
import { InputType, KeyboardKey, ModifierKey,
|
|
1
|
+
import { InputType, KeyboardKey, ModifierKey, DetailType } from '@asyra/utils';
|
|
2
|
+
import type { RawInputEvent } from '@asyra/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Defines a single input combination that can trigger an event.
|
|
5
|
+
*
|
|
6
|
+
* This interface is part of the framework's type system. Users create instances
|
|
7
|
+
* of this type and register them with the InputSystemRegistry.
|
|
8
|
+
*
|
|
9
|
+
* @example Keyboard shortcut
|
|
10
|
+
* const combo: InputEventCombo = {
|
|
11
|
+
* type: InputType.KEYBOARD,
|
|
12
|
+
* keys: [keyMap.keys.KeyZ],
|
|
13
|
+
* modifiers: [ModifierKey.META]
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* @example Mouse drag
|
|
17
|
+
* const combo: InputEventCombo = {
|
|
18
|
+
* type: InputType.POINTER,
|
|
19
|
+
* keys: [PointerKey.LEFT_MOUSE_DOWN]
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* @example With callback to update system context
|
|
23
|
+
* const combo: InputEventCombo = {
|
|
24
|
+
* type: InputType.POINTER,
|
|
25
|
+
* keys: [PointerKey.LEFT_MOUSE_DOWN],
|
|
26
|
+
* callback: (raw: RawInputEvent) => {
|
|
27
|
+
* core.setSystemProperty('mousePosition', { x: raw.pointer.clientX, y: raw.pointer.clientY })
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* @example Custom device
|
|
32
|
+
* const combo: InputEventCombo = {
|
|
33
|
+
* type: 'voice.command', // Custom InputType
|
|
34
|
+
* keys: ['create rectangle']
|
|
35
|
+
* }
|
|
36
|
+
*/
|
|
2
37
|
export interface InputEventCombo {
|
|
3
38
|
type: InputType;
|
|
4
39
|
keys: KeyboardKey[];
|
|
5
40
|
modifiers?: ModifierKey[];
|
|
6
41
|
detail?: DetailType;
|
|
42
|
+
callback?: (raw: RawInputEvent) => void;
|
|
7
43
|
}
|
|
8
|
-
export declare const InputEventMappings: Record<InputSystemEvents, InputEventCombo[]>;
|
package/dist/event-mappings.js
CHANGED
|
@@ -1,78 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import keyMap from './keymap';
|
|
3
|
-
export const InputEventMappings = {
|
|
4
|
-
[InputSystemEvents.INPUT_DRAG_START]: [
|
|
5
|
-
{ type: InputType.POINTER, keys: [PointerKey.LEFT_MOUSE_DOWN] }
|
|
6
|
-
],
|
|
7
|
-
[InputSystemEvents.INPUT_DRAG_UPDATE]: [
|
|
8
|
-
{
|
|
9
|
-
type: InputType.POINTER,
|
|
10
|
-
keys: [PointerKey.LEFT_MOUSE_DOWN, PointerKey.LEFT_MOUSE_MOVE]
|
|
11
|
-
}
|
|
12
|
-
],
|
|
13
|
-
[InputSystemEvents.INPUT_DRAG_END]: [
|
|
14
|
-
{ type: InputType.POINTER, keys: [PointerKey.LEFT_MOUSE_UP] }
|
|
15
|
-
],
|
|
16
|
-
[InputSystemEvents.INPUT_MOUSE_MOVE]: [
|
|
17
|
-
{
|
|
18
|
-
type: InputType.POINTER,
|
|
19
|
-
keys: [PointerKey.LEFT_MOUSE_MOVE]
|
|
20
|
-
}
|
|
21
|
-
],
|
|
22
|
-
[InputSystemEvents.INPUT_WHEEL_SCROLL]: [
|
|
23
|
-
{ type: InputType.WHEEL, keys: [PointerKey.WHEEL] }
|
|
24
|
-
],
|
|
25
|
-
[InputSystemEvents.INPUT_SHORTCUT_ARROW]: [
|
|
26
|
-
{
|
|
27
|
-
type: InputType.KEYBOARD,
|
|
28
|
-
keys: [keyMap.keys.ArrowUp]
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
type: InputType.KEYBOARD,
|
|
32
|
-
keys: [keyMap.keys.ArrowDown]
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
type: InputType.KEYBOARD,
|
|
36
|
-
keys: [keyMap.keys.ArrowLeft]
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
type: InputType.KEYBOARD,
|
|
40
|
-
keys: [keyMap.keys.ArrowRight]
|
|
41
|
-
}
|
|
42
|
-
],
|
|
43
|
-
[InputSystemEvents.INPUT_SHORTCUT_UNDOREDO]: [
|
|
44
|
-
{
|
|
45
|
-
type: InputType.KEYBOARD,
|
|
46
|
-
keys: [keyMap.keys.KeyZ],
|
|
47
|
-
modifiers: [ModifierKey.META]
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
type: InputType.KEYBOARD,
|
|
51
|
-
keys: [keyMap.keys.KeyZ],
|
|
52
|
-
modifiers: [ModifierKey.CTRL]
|
|
53
|
-
}
|
|
54
|
-
],
|
|
55
|
-
[InputSystemEvents.INPUT_SHORTCUT_ZOOM_PRESET]: [
|
|
56
|
-
{
|
|
57
|
-
type: InputType.KEYBOARD,
|
|
58
|
-
keys: [keyMap.keys.Digit1],
|
|
59
|
-
modifiers: [ModifierKey.META]
|
|
60
|
-
}
|
|
61
|
-
],
|
|
62
|
-
[InputSystemEvents.INPUT_SHORTCUT_SWITCH_PRIMARY_TOOL]: [
|
|
63
|
-
{
|
|
64
|
-
type: InputType.KEYBOARD,
|
|
65
|
-
keys: [keyMap.keys.KeyR],
|
|
66
|
-
detail: {
|
|
67
|
-
primaryTool: PrimaryToolType.RECTANGLE
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
type: InputType.KEYBOARD,
|
|
72
|
-
keys: [keyMap.keys.KeyV],
|
|
73
|
-
detail: {
|
|
74
|
-
primaryTool: PrimaryToolType.SELECT
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
]
|
|
78
|
-
};
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import inputSystem, { InputSystem } from './input-system';
|
|
2
|
-
|
|
1
|
+
import inputSystem, { InputSystem } from './input-system.js';
|
|
2
|
+
import { InputSystemRegistry } from './registry.js';
|
|
3
|
+
import { InputEventCombo } from './event-mappings.js';
|
|
4
|
+
import keyMap from './keymap.js';
|
|
5
|
+
export { InputSystem, InputSystemRegistry, InputEventCombo, keyMap };
|
|
3
6
|
export default inputSystem;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import inputSystem, { InputSystem } from './input-system';
|
|
2
|
-
import {
|
|
1
|
+
import inputSystem, { InputSystem } from './input-system.js';
|
|
2
|
+
import { InputSystemRegistry } from './registry.js';
|
|
3
|
+
import keyMap from './keymap.js';
|
|
4
|
+
import { initInputSystemSubscribe } from './subscribe.js';
|
|
3
5
|
initInputSystemSubscribe();
|
|
4
|
-
export { InputSystem };
|
|
6
|
+
export { InputSystem, InputSystemRegistry, keyMap };
|
|
5
7
|
export default inputSystem;
|
package/dist/input-system.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ModifierKeys, RawInputEvent, ModifierKey } from '@asyra/utils';
|
|
2
|
+
import { InputSystemRegistry } from './registry.js';
|
|
2
3
|
type Callback = (raw: RawInputEvent) => void | Promise<void>;
|
|
3
4
|
type Combinations = Record<string, string[]>;
|
|
4
5
|
declare class InputSystem {
|
|
@@ -9,10 +10,14 @@ declare class InputSystem {
|
|
|
9
10
|
private listeners;
|
|
10
11
|
private timers;
|
|
11
12
|
private _startPos;
|
|
13
|
+
private pointerInputBlocked;
|
|
14
|
+
private pointerCaptureId;
|
|
15
|
+
registry: InputSystemRegistry;
|
|
12
16
|
constructor();
|
|
13
17
|
private setupListeners;
|
|
14
18
|
setCombinations(combinations: Combinations): void;
|
|
15
19
|
on(action: string, callback: Callback): this;
|
|
20
|
+
off(action: string, callback: Callback): boolean;
|
|
16
21
|
switchWatchedElement(watchedElement: HTMLElement): void;
|
|
17
22
|
private startTimer;
|
|
18
23
|
private clearTimer;
|
|
@@ -23,13 +28,19 @@ declare class InputSystem {
|
|
|
23
28
|
private handleMouseDown;
|
|
24
29
|
private handleMouseUp;
|
|
25
30
|
private handleMouseMove;
|
|
31
|
+
private handleDoubleClick;
|
|
26
32
|
private getMouseEventKey;
|
|
27
33
|
private checkCombinations;
|
|
28
34
|
private isExactMatch;
|
|
29
35
|
private triggerAction;
|
|
30
36
|
private handleWheel;
|
|
31
37
|
getActiveModifiers(keys: Set<string>): ModifierKey[];
|
|
38
|
+
setPointerCaptureBlock(active: boolean, captureId?: string | null): void;
|
|
39
|
+
private clearPointerState;
|
|
40
|
+
private isPointerKey;
|
|
32
41
|
getAllModifiers(modifiers: ModifierKey[]): ModifierKeys;
|
|
42
|
+
dispose(): void;
|
|
43
|
+
reset(): void;
|
|
33
44
|
}
|
|
34
45
|
export { InputSystem };
|
|
35
46
|
declare const inputSystem: InputSystem;
|
package/dist/input-system.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { MouseButton, SpecialEvent, InputType, arrEqual, DefaultPointerEventData } from '@asyra/utils';
|
|
1
|
+
import { MouseButton, SpecialEvent, InputType, arrEqual, DefaultPointerEventData, measureBrowserDragPhase } from '@asyra/utils';
|
|
2
2
|
import { InputFieldsList } from '@asyra/utils';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
3
|
+
import { PointerKey } from '@asyra/utils';
|
|
4
|
+
import { CLICK_THRESHOLD, CLEAR_KEY_TIME } from './constants.js';
|
|
5
|
+
import { InputSystemRegistry } from './registry.js';
|
|
6
|
+
import keymap from './keymap.js';
|
|
6
7
|
const WHEEL_EVENT_OPTIONS = { passive: false };
|
|
8
|
+
const POINTER_KEYS = new Set(Object.values(PointerKey));
|
|
7
9
|
const getMouseButton = (button) => {
|
|
8
10
|
switch (button) {
|
|
9
11
|
case 0:
|
|
@@ -16,6 +18,18 @@ const getMouseButton = (button) => {
|
|
|
16
18
|
return MouseButton.NONE;
|
|
17
19
|
}
|
|
18
20
|
};
|
|
21
|
+
const getMouseButtonFromButtonsMask = (buttons) => {
|
|
22
|
+
if ((buttons & 1) === 1) {
|
|
23
|
+
return MouseButton.LEFT;
|
|
24
|
+
}
|
|
25
|
+
if ((buttons & 4) === 4) {
|
|
26
|
+
return MouseButton.MIDDLE;
|
|
27
|
+
}
|
|
28
|
+
if ((buttons & 2) === 2) {
|
|
29
|
+
return MouseButton.RIGHT;
|
|
30
|
+
}
|
|
31
|
+
return MouseButton.NONE;
|
|
32
|
+
};
|
|
19
33
|
class InputSystem {
|
|
20
34
|
_previousWatchedElement;
|
|
21
35
|
combinations = {};
|
|
@@ -24,6 +38,9 @@ class InputSystem {
|
|
|
24
38
|
listeners;
|
|
25
39
|
timers;
|
|
26
40
|
_startPos;
|
|
41
|
+
pointerInputBlocked;
|
|
42
|
+
pointerCaptureId;
|
|
43
|
+
registry;
|
|
27
44
|
constructor() {
|
|
28
45
|
this._previousWatchedElement = window;
|
|
29
46
|
this.keyMap = keymap;
|
|
@@ -31,6 +48,9 @@ class InputSystem {
|
|
|
31
48
|
this.listeners = new Map();
|
|
32
49
|
this.timers = new Map();
|
|
33
50
|
this._startPos = null;
|
|
51
|
+
this.pointerInputBlocked = false;
|
|
52
|
+
this.pointerCaptureId = null;
|
|
53
|
+
this.registry = new InputSystemRegistry();
|
|
34
54
|
this.setupListeners();
|
|
35
55
|
}
|
|
36
56
|
setupListeners() {
|
|
@@ -39,8 +59,8 @@ class InputSystem {
|
|
|
39
59
|
window.addEventListener('mousedown', this.handleMouseDown);
|
|
40
60
|
window.addEventListener('mouseup', this.handleMouseUp);
|
|
41
61
|
window.addEventListener('mousemove', this.handleMouseMove);
|
|
62
|
+
window.addEventListener('dblclick', this.handleDoubleClick);
|
|
42
63
|
window.addEventListener('wheel', this.handleWheel, WHEEL_EVENT_OPTIONS);
|
|
43
|
-
window.addEventListener('contextmenu', (e) => e.preventDefault());
|
|
44
64
|
}
|
|
45
65
|
setCombinations(combinations) {
|
|
46
66
|
this.combinations = combinations;
|
|
@@ -52,14 +72,31 @@ class InputSystem {
|
|
|
52
72
|
this.listeners.get(action)?.push(callback);
|
|
53
73
|
return this;
|
|
54
74
|
}
|
|
75
|
+
off(action, callback) {
|
|
76
|
+
const callbacks = this.listeners.get(action);
|
|
77
|
+
if (!callbacks) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const index = callbacks.indexOf(callback);
|
|
81
|
+
if (index < 0) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
callbacks.splice(index, 1);
|
|
85
|
+
if (callbacks.length === 0) {
|
|
86
|
+
this.listeners.delete(action);
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
55
90
|
switchWatchedElement(watchedElement) {
|
|
56
91
|
this._previousWatchedElement.removeEventListener('mousedown', this.handleMouseDown);
|
|
57
92
|
this._previousWatchedElement.removeEventListener('mouseup', this.handleMouseUp);
|
|
58
93
|
this._previousWatchedElement.removeEventListener('mousemove', this.handleMouseMove);
|
|
94
|
+
this._previousWatchedElement.removeEventListener('dblclick', this.handleDoubleClick);
|
|
59
95
|
this._previousWatchedElement.removeEventListener('wheel', this.handleWheel, WHEEL_EVENT_OPTIONS);
|
|
60
96
|
watchedElement.addEventListener('mousedown', this.handleMouseDown);
|
|
61
97
|
watchedElement.addEventListener('mouseup', this.handleMouseUp);
|
|
62
98
|
watchedElement.addEventListener('mousemove', this.handleMouseMove);
|
|
99
|
+
watchedElement.addEventListener('dblclick', this.handleDoubleClick);
|
|
63
100
|
watchedElement.addEventListener('wheel', this.handleWheel, {
|
|
64
101
|
passive: false
|
|
65
102
|
});
|
|
@@ -121,6 +158,9 @@ class InputSystem {
|
|
|
121
158
|
}
|
|
122
159
|
};
|
|
123
160
|
handleMouseDown = (event) => {
|
|
161
|
+
if (this.pointerInputBlocked) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
124
164
|
const button = getMouseButton(event.button);
|
|
125
165
|
const key = this.getMouseEventKey(button, 'Down');
|
|
126
166
|
if (key) {
|
|
@@ -137,6 +177,9 @@ class InputSystem {
|
|
|
137
177
|
}
|
|
138
178
|
};
|
|
139
179
|
handleMouseUp = (event) => {
|
|
180
|
+
if (this.pointerInputBlocked) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
140
183
|
const button = getMouseButton(event.button);
|
|
141
184
|
const key = this.getMouseEventKey(button, 'Up');
|
|
142
185
|
if (key) {
|
|
@@ -153,7 +196,13 @@ class InputSystem {
|
|
|
153
196
|
}
|
|
154
197
|
};
|
|
155
198
|
handleMouseMove = (event) => {
|
|
156
|
-
|
|
199
|
+
if (this.pointerInputBlocked) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const buttonFromMask = getMouseButtonFromButtonsMask(event.buttons);
|
|
203
|
+
const button = buttonFromMask !== MouseButton.NONE
|
|
204
|
+
? buttonFromMask
|
|
205
|
+
: getMouseButton(event.button);
|
|
157
206
|
const key = this.getMouseEventKey(button, 'Move');
|
|
158
207
|
if (key) {
|
|
159
208
|
let canMove = true;
|
|
@@ -167,27 +216,55 @@ class InputSystem {
|
|
|
167
216
|
}
|
|
168
217
|
if (canMove) {
|
|
169
218
|
this.activeKeys.add(key);
|
|
170
|
-
this.checkCombinations(InputType.POINTER, {
|
|
219
|
+
measureBrowserDragPhase('input:pointer-move', () => this.checkCombinations(InputType.POINTER, {
|
|
171
220
|
...DefaultPointerEventData,
|
|
172
221
|
clientX: event.clientX,
|
|
173
222
|
clientY: event.clientY,
|
|
174
223
|
button
|
|
175
|
-
});
|
|
224
|
+
}));
|
|
176
225
|
// No need to keep mouse up key after trigger action
|
|
177
226
|
this.activeKeys.delete(key);
|
|
178
227
|
}
|
|
179
228
|
}
|
|
180
229
|
};
|
|
230
|
+
handleDoubleClick = (event) => {
|
|
231
|
+
if (this.pointerInputBlocked) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const button = getMouseButton(event.button);
|
|
235
|
+
this.activeKeys.add(PointerKey.MOUSE_DOUBLE_CLICK);
|
|
236
|
+
this.checkCombinations(InputType.POINTER, {
|
|
237
|
+
...DefaultPointerEventData,
|
|
238
|
+
clientX: event.clientX,
|
|
239
|
+
clientY: event.clientY,
|
|
240
|
+
button
|
|
241
|
+
});
|
|
242
|
+
this.activeKeys.delete(PointerKey.MOUSE_DOUBLE_CLICK);
|
|
243
|
+
};
|
|
181
244
|
getMouseEventKey(button, state) {
|
|
182
245
|
if (button === MouseButton.NONE)
|
|
183
246
|
return;
|
|
184
247
|
return `${button}Mouse${state}`;
|
|
185
248
|
}
|
|
186
249
|
checkCombinations(type, pointerData = DefaultPointerEventData) {
|
|
187
|
-
const currentKeys = Array.from(this.activeKeys).filter((key) =>
|
|
250
|
+
const currentKeys = Array.from(this.activeKeys).filter((key) => {
|
|
251
|
+
if (this.keyMap.isModifierKeys(key)) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
if (type === InputType.KEYBOARD) {
|
|
255
|
+
return !POINTER_KEYS.has(key);
|
|
256
|
+
}
|
|
257
|
+
if (type === InputType.WHEEL) {
|
|
258
|
+
return key === SpecialEvent.WHEEL;
|
|
259
|
+
}
|
|
260
|
+
return POINTER_KEYS.has(key) && key !== SpecialEvent.WHEEL;
|
|
261
|
+
});
|
|
188
262
|
const activeModifiers = this.getActiveModifiers(this.activeKeys);
|
|
189
263
|
const allModifiers = this.getAllModifiers(activeModifiers);
|
|
190
|
-
for (const
|
|
264
|
+
for (const eventName of this.registry.getEventNames()) {
|
|
265
|
+
const combos = this.registry.getCombinations(eventName);
|
|
266
|
+
if (!combos)
|
|
267
|
+
continue;
|
|
191
268
|
for (const combo of combos) {
|
|
192
269
|
if (this.isExactMatch(type, currentKeys, combo, activeModifiers)) {
|
|
193
270
|
const raw = {
|
|
@@ -199,6 +276,10 @@ class InputSystem {
|
|
|
199
276
|
if (combo.detail) {
|
|
200
277
|
raw.detail = combo.detail;
|
|
201
278
|
}
|
|
279
|
+
// Call callback before triggering action (if defined)
|
|
280
|
+
if (combo.callback) {
|
|
281
|
+
combo.callback(raw);
|
|
282
|
+
}
|
|
202
283
|
this.triggerAction(eventName, raw);
|
|
203
284
|
}
|
|
204
285
|
}
|
|
@@ -219,7 +300,7 @@ class InputSystem {
|
|
|
219
300
|
const callbacks = this.listeners.get(event);
|
|
220
301
|
if (callbacks) {
|
|
221
302
|
callbacks.forEach((cb) => {
|
|
222
|
-
const result = cb(raw);
|
|
303
|
+
const result = measureBrowserDragPhase(`input:trigger:${event}`, () => cb(raw));
|
|
223
304
|
// If callback returns a promise, catch any errors
|
|
224
305
|
if (result instanceof Promise) {
|
|
225
306
|
result.catch((error) => {
|
|
@@ -258,6 +339,37 @@ class InputSystem {
|
|
|
258
339
|
});
|
|
259
340
|
return modifiers;
|
|
260
341
|
}
|
|
342
|
+
setPointerCaptureBlock(active, captureId) {
|
|
343
|
+
if (active) {
|
|
344
|
+
this.pointerInputBlocked = true;
|
|
345
|
+
this.pointerCaptureId = captureId ?? null;
|
|
346
|
+
this.clearPointerState();
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (this.pointerCaptureId &&
|
|
350
|
+
captureId &&
|
|
351
|
+
this.pointerCaptureId !== captureId) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
this.pointerInputBlocked = false;
|
|
355
|
+
this.pointerCaptureId = null;
|
|
356
|
+
this.clearPointerState();
|
|
357
|
+
}
|
|
358
|
+
clearPointerState() {
|
|
359
|
+
this._startPos = null;
|
|
360
|
+
for (const key of Array.from(this.activeKeys)) {
|
|
361
|
+
if (this.isPointerKey(key)) {
|
|
362
|
+
this.activeKeys.delete(key);
|
|
363
|
+
this.clearTimer(key);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
isPointerKey(key) {
|
|
368
|
+
if (key === PointerKey.WHEEL) {
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
return key.toLowerCase().includes('mouse');
|
|
372
|
+
}
|
|
261
373
|
getAllModifiers(modifiers) {
|
|
262
374
|
const allModifiers = {
|
|
263
375
|
meta: false,
|
|
@@ -270,6 +382,16 @@ class InputSystem {
|
|
|
270
382
|
});
|
|
271
383
|
return allModifiers;
|
|
272
384
|
}
|
|
385
|
+
dispose() {
|
|
386
|
+
this.listeners.clear();
|
|
387
|
+
this.timers.forEach((timer) => clearTimeout(timer));
|
|
388
|
+
this.timers.clear();
|
|
389
|
+
this.activeKeys.clear();
|
|
390
|
+
this.combinations = {};
|
|
391
|
+
}
|
|
392
|
+
reset() {
|
|
393
|
+
this.dispose();
|
|
394
|
+
}
|
|
273
395
|
}
|
|
274
396
|
export { InputSystem };
|
|
275
397
|
const inputSystem = new InputSystem();
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { InputEventCombo } from './event-mappings.js';
|
|
2
|
+
export declare class InputSystemRegistry {
|
|
3
|
+
private mappings;
|
|
4
|
+
/**
|
|
5
|
+
* Register input event combinations for a given event name
|
|
6
|
+
* @param eventName - The event name to trigger
|
|
7
|
+
* @param combos - Array of input combinations that trigger this event
|
|
8
|
+
*/
|
|
9
|
+
register(eventName: string, combos: InputEventCombo[]): void;
|
|
10
|
+
/**
|
|
11
|
+
* Register multiple events from a config object
|
|
12
|
+
* @param combinations - Config object with event names as keys
|
|
13
|
+
*/
|
|
14
|
+
registerKeyCombinations(combinations: Record<string, InputEventCombo[]>): void;
|
|
15
|
+
/**
|
|
16
|
+
* Get all registered event names
|
|
17
|
+
*/
|
|
18
|
+
getEventNames(): string[];
|
|
19
|
+
/**
|
|
20
|
+
* Get combinations for a specific event
|
|
21
|
+
* @param eventName - The event name
|
|
22
|
+
* @returns Array of input combinations, or undefined if not found
|
|
23
|
+
*/
|
|
24
|
+
getCombinations(eventName: string): InputEventCombo[] | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Check if an event is registered
|
|
27
|
+
* @param eventName - The event name to check
|
|
28
|
+
*/
|
|
29
|
+
hasEvent(eventName: string): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Remove an event registration
|
|
32
|
+
* @param eventName - The event name to remove
|
|
33
|
+
*/
|
|
34
|
+
unregister(eventName: string): void;
|
|
35
|
+
/**
|
|
36
|
+
* Clear all registered events
|
|
37
|
+
*/
|
|
38
|
+
clear(): void;
|
|
39
|
+
}
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { MapRegistry } from '@asyra/utils';
|
|
2
|
+
export class InputSystemRegistry {
|
|
3
|
+
mappings = new MapRegistry();
|
|
4
|
+
/**
|
|
5
|
+
* Register input event combinations for a given event name
|
|
6
|
+
* @param eventName - The event name to trigger
|
|
7
|
+
* @param combos - Array of input combinations that trigger this event
|
|
8
|
+
*/
|
|
9
|
+
register(eventName, combos) {
|
|
10
|
+
this.mappings.register(eventName, combos, {
|
|
11
|
+
duplicateErrorMessage: `Input event "${eventName}" is already registered`
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Register multiple events from a config object
|
|
16
|
+
* @param combinations - Config object with event names as keys
|
|
17
|
+
*/
|
|
18
|
+
registerKeyCombinations(combinations) {
|
|
19
|
+
for (const [eventName, combos] of Object.entries(combinations)) {
|
|
20
|
+
this.register(eventName, combos);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get all registered event names
|
|
25
|
+
*/
|
|
26
|
+
getEventNames() {
|
|
27
|
+
return this.mappings.keys();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get combinations for a specific event
|
|
31
|
+
* @param eventName - The event name
|
|
32
|
+
* @returns Array of input combinations, or undefined if not found
|
|
33
|
+
*/
|
|
34
|
+
getCombinations(eventName) {
|
|
35
|
+
return this.mappings.get(eventName);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Check if an event is registered
|
|
39
|
+
* @param eventName - The event name to check
|
|
40
|
+
*/
|
|
41
|
+
hasEvent(eventName) {
|
|
42
|
+
return this.mappings.has(eventName);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Remove an event registration
|
|
46
|
+
* @param eventName - The event name to remove
|
|
47
|
+
*/
|
|
48
|
+
unregister(eventName) {
|
|
49
|
+
this.mappings.delete(eventName);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Clear all registered events
|
|
53
|
+
*/
|
|
54
|
+
clear() {
|
|
55
|
+
this.mappings.clear();
|
|
56
|
+
}
|
|
57
|
+
}
|