@makefully/engagefully 0.2.0 → 0.2.1

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/package.json CHANGED
@@ -1,68 +1,68 @@
1
- {
2
- "name": "@makefully/engagefully",
3
- "version": "0.2.0",
4
- "description": "Device-agnostic input: keyboard/gamepad/pointer → Button/Joystick, plus ActionMap logical bindings.",
5
- "main": "./lib/engagefully.js",
6
- "module": "./src/index.js",
7
- "exports": {
8
- ".": {
9
- "import": "./src/index.js",
10
- "default": "./lib/engagefully.js"
11
- },
12
- "./package.json": "./package.json"
13
- },
14
- "files": [
15
- "lib",
16
- "src",
17
- "README.md",
18
- "CHANGELOG.md",
19
- "LICENSE"
20
- ],
21
- "scripts": {
22
- "start": "webpack-dev-server --env dev",
23
- "build": "webpack --env prod",
24
- "build:release": "webpack --env prod",
25
- "build:debug": "webpack --env dev",
26
- "test": "node -e \"console.log('no unit tests yet')\"",
27
- "prepublishOnly": "npm run build:release"
28
- },
29
- "publishConfig": {
30
- "access": "public",
31
- "provenance": true
32
- },
33
- "repository": {
34
- "type": "git",
35
- "url": "git+https://github.com/Makefully-Studios/engagefully.git"
36
- },
37
- "bugs": {
38
- "url": "https://github.com/Makefully-Studios/engagefully/issues"
39
- },
40
- "homepage": "https://github.com/Makefully-Studios/engagefully#readme",
41
- "keywords": [
42
- "engagefully",
43
- "makefully",
44
- "input",
45
- "keyboard",
46
- "gamepad",
47
- "controller",
48
- "gamedev"
49
- ],
50
- "contributors": [
51
- "Derek Detweiler <derek@makefullystudios.com>"
52
- ],
53
- "license": "MIT",
54
- "dependencies": {
55
- "broadcast-channel": "^4.10.0",
56
- "events": "^3.3.0"
57
- },
58
- "devDependencies": {
59
- "@babel/core": "^7.4.3",
60
- "@babel/polyfill": "^7.0.0",
61
- "@babel/preset-env": "^7.4.3",
62
- "babel-loader": "^8.0.2",
63
- "file-loader": "^6.2.0",
64
- "webpack": "^5.68.0",
65
- "webpack-cli": "^4.9.2",
66
- "webpack-dev-server": "^4.7.3"
67
- }
68
- }
1
+ {
2
+ "name": "@makefully/engagefully",
3
+ "version": "0.2.1",
4
+ "description": "Device-agnostic input: keyboard/gamepad/pointer → Button/Joystick, plus ActionMap logical bindings.",
5
+ "main": "./lib/engagefully.js",
6
+ "module": "./src/index.js",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./src/index.js",
10
+ "default": "./lib/engagefully.js"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "files": [
15
+ "lib",
16
+ "src",
17
+ "README.md",
18
+ "CHANGELOG.md",
19
+ "LICENSE"
20
+ ],
21
+ "scripts": {
22
+ "start": "webpack-dev-server --env dev",
23
+ "build": "webpack --env prod",
24
+ "build:release": "webpack --env prod",
25
+ "build:debug": "webpack --env dev",
26
+ "test": "node -e \"console.log('no unit tests yet')\"",
27
+ "prepublishOnly": "npm run build:release"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public",
31
+ "provenance": true
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/Makefully-Studios/engagefully.git"
36
+ },
37
+ "bugs": {
38
+ "url": "https://github.com/Makefully-Studios/engagefully/issues"
39
+ },
40
+ "homepage": "https://github.com/Makefully-Studios/engagefully#readme",
41
+ "keywords": [
42
+ "engagefully",
43
+ "makefully",
44
+ "input",
45
+ "keyboard",
46
+ "gamepad",
47
+ "controller",
48
+ "gamedev"
49
+ ],
50
+ "contributors": [
51
+ "Derek Detweiler <derek@makefullystudios.com>"
52
+ ],
53
+ "license": "MIT",
54
+ "dependencies": {
55
+ "broadcast-channel": "^4.10.0",
56
+ "events": "^3.3.0"
57
+ },
58
+ "devDependencies": {
59
+ "@babel/core": "^7.4.3",
60
+ "@babel/polyfill": "^7.0.0",
61
+ "@babel/preset-env": "^7.4.3",
62
+ "babel-loader": "^8.0.2",
63
+ "file-loader": "^6.2.0",
64
+ "webpack": "^5.68.0",
65
+ "webpack-cli": "^4.9.2",
66
+ "webpack-dev-server": "^4.7.3"
67
+ }
68
+ }
package/src/ActionMap.js CHANGED
@@ -1,261 +1,261 @@
1
- import EventEmitter from 'events';
2
-
3
- /**
4
- * Normalize a binding entry to { control, data }.
5
- * @param {string|{ control: string, data?: * }} entry
6
- */
7
- function normalizeBinding (entry) {
8
- if (typeof entry === 'string') {
9
- return { control: entry, data: undefined };
10
- }
11
- return { control: entry.control, data: entry.data };
12
- }
13
-
14
- /**
15
- * Keyboard-ish control ids use event.code shape (KeyW, Space, ArrowUp, Digit1, …).
16
- * Gamepad ids are named (AButton, LeftStick, UpDPad, …).
17
- */
18
- function isKeyboardControl (control) {
19
- return (
20
- control.startsWith('Key') ||
21
- control.startsWith('Arrow') ||
22
- control.startsWith('Digit') ||
23
- control.startsWith('Numpad') ||
24
- control === 'Space' ||
25
- control === 'Enter' ||
26
- control === 'Escape' ||
27
- control === 'Tab' ||
28
- control === 'Backspace' ||
29
- control === 'ShiftLeft' ||
30
- control === 'ShiftRight' ||
31
- control === 'ControlLeft' ||
32
- control === 'ControlRight' ||
33
- control === 'AltLeft' ||
34
- control === 'AltRight'
35
- );
36
- }
37
-
38
- /**
39
- * Map many physical controls onto named logical actions.
40
- *
41
- * @param {import('./Controller').default} controller
42
- * @param {Record<string, Array<string|{ control: string, data?: * }>>} bindings
43
- * @param {{
44
- * enabled?: () => boolean,
45
- * preventDefault?: boolean,
46
- * autoTick?: boolean,
47
- * }} [options]
48
- */
49
- class ActionMap extends EventEmitter {
50
- constructor (controller, bindings = {}, options = {}) {
51
- super();
52
-
53
- this.controller = controller;
54
- this.enabled = typeof options.enabled === 'function'
55
- ? options.enabled
56
- : () => true;
57
- this.preventDefault = options.preventDefault !== false;
58
- this.autoTick = options.autoTick !== false;
59
-
60
- /** @type {Map<string, Array<{ control: string, data?: * }>>} */
61
- this._bindings = new Map();
62
- /** control → [{ action, data }] */
63
- this._controlIndex = new Map();
64
- /** action → Set of currently held control ids */
65
- this._heldByAction = new Map();
66
- /** action → last event payload while held (for :held ticks) */
67
- this._lastEvent = new Map();
68
- this._listeners = [];
69
- this._disposed = false;
70
- this._wasEnabled = true;
71
- this._raf = 0;
72
-
73
- this._applyBindings(bindings);
74
-
75
- if (this.preventDefault) {
76
- this._onKeyDownCapture = (event) => {
77
- if (!this.enabled()) return;
78
- if (!this._keyboardCodes.has(event.code)) return;
79
- event.preventDefault();
80
- };
81
- window.addEventListener('keydown', this._onKeyDownCapture, true);
82
- }
83
-
84
- this._onBlur = () => {
85
- this.clearHeld();
86
- };
87
- window.addEventListener('blur', this._onBlur);
88
-
89
- if (this.autoTick) {
90
- const tick = () => {
91
- if (this._disposed) return;
92
- this._raf = requestAnimationFrame(tick);
93
- this._tick();
94
- };
95
- this._raf = requestAnimationFrame(tick);
96
- }
97
- }
98
-
99
- _applyBindings (bindings) {
100
- this._keyboardCodes = new Set();
101
-
102
- for (const [action, entries] of Object.entries(bindings)) {
103
- const list = (entries || []).map(normalizeBinding);
104
- this._bindings.set(action, list);
105
- this._heldByAction.set(action, new Set());
106
-
107
- for (const binding of list) {
108
- if (isKeyboardControl(binding.control)) {
109
- this._keyboardCodes.add(binding.control);
110
- }
111
- let bucket = this._controlIndex.get(binding.control);
112
- if (!bucket) {
113
- bucket = [];
114
- this._controlIndex.set(binding.control, bucket);
115
- }
116
- bucket.push({ action, data: binding.data });
117
-
118
- const onPressed = (raw) => this._onPhysicalPressed(binding.control, raw);
119
- const onReleased = (raw) => this._onPhysicalReleased(binding.control, raw);
120
-
121
- this.controller.on(`${binding.control}:pressed`, onPressed);
122
- this.controller.on(`${binding.control}:released`, onReleased);
123
- this._listeners.push(
124
- { event: `${binding.control}:pressed`, fn: onPressed },
125
- { event: `${binding.control}:released`, fn: onReleased }
126
- );
127
- }
128
- }
129
- }
130
-
131
- _isEnabled () {
132
- const on = this.enabled();
133
- if (this._wasEnabled && !on) {
134
- this.clearHeld();
135
- }
136
- this._wasEnabled = on;
137
- return on;
138
- }
139
-
140
- _onPhysicalPressed (control, raw) {
141
- if (!this._isEnabled()) return;
142
-
143
- const targets = this._controlIndex.get(control);
144
- if (!targets) return;
145
-
146
- for (const { action, data } of targets) {
147
- const held = this._heldByAction.get(action);
148
- const wasEmpty = held.size === 0;
149
- held.add(control);
150
-
151
- const event = {
152
- action,
153
- control,
154
- data,
155
- device: raw?.device,
156
- raw
157
- };
158
- this._lastEvent.set(action, event);
159
-
160
- if (wasEmpty) {
161
- this.emit(`${action}:pressed`, event);
162
- }
163
- this.emit(action, event);
164
- }
165
- }
166
-
167
- _onPhysicalReleased (control, raw) {
168
- const targets = this._controlIndex.get(control);
169
- if (!targets) return;
170
-
171
- for (const { action, data } of targets) {
172
- const held = this._heldByAction.get(action);
173
- if (!held.has(control)) continue;
174
- held.delete(control);
175
-
176
- const event = {
177
- action,
178
- control,
179
- data,
180
- device: raw?.device,
181
- raw
182
- };
183
-
184
- if (held.size === 0) {
185
- this._lastEvent.delete(action);
186
- this.emit(`${action}:released`, event);
187
- } else {
188
- // Prefer another still-held binding's data for subsequent held ticks.
189
- const still = this._bindings.get(action) || [];
190
- for (const b of still) {
191
- if (held.has(b.control)) {
192
- this._lastEvent.set(action, {
193
- action,
194
- control: b.control,
195
- data: b.data,
196
- device: raw?.device,
197
- raw
198
- });
199
- break;
200
- }
201
- }
202
- }
203
- }
204
- }
205
-
206
- _tick () {
207
- if (!this._isEnabled()) return;
208
- for (const [action, held] of this._heldByAction) {
209
- if (held.size === 0) continue;
210
- const event = this._lastEvent.get(action);
211
- if (!event) continue;
212
- this.emit(`${action}:held`, event);
213
- }
214
- }
215
-
216
- /**
217
- * @param {string} action
218
- * @returns {boolean}
219
- */
220
- held (action) {
221
- const set = this._heldByAction.get(action);
222
- return Boolean(set && set.size > 0);
223
- }
224
-
225
- /**
226
- * Last event payload for a held action (control + data), or null.
227
- * @param {string} action
228
- */
229
- heldEvent (action) {
230
- if (!this.held(action)) return null;
231
- return this._lastEvent.get(action) || null;
232
- }
233
-
234
- /** Release all logical actions (emits :released once per previously held action). */
235
- clearHeld () {
236
- for (const [action, held] of this._heldByAction) {
237
- if (held.size === 0) continue;
238
- const last = this._lastEvent.get(action);
239
- held.clear();
240
- this._lastEvent.delete(action);
241
- this.emit(`${action}:released`, last || { action, control: null, data: undefined });
242
- }
243
- }
244
-
245
- dispose () {
246
- if (this._disposed) return;
247
- this._disposed = true;
248
- cancelAnimationFrame(this._raf);
249
- for (const { event, fn } of this._listeners) {
250
- this.controller.off(event, fn);
251
- }
252
- this._listeners.length = 0;
253
- if (this._onKeyDownCapture) {
254
- window.removeEventListener('keydown', this._onKeyDownCapture, true);
255
- }
256
- window.removeEventListener('blur', this._onBlur);
257
- this.removeAllListeners();
258
- }
259
- }
260
-
261
- export default ActionMap;
1
+ import EventEmitter from 'events';
2
+
3
+ /**
4
+ * Normalize a binding entry to { control, data }.
5
+ * @param {string|{ control: string, data?: * }} entry
6
+ */
7
+ function normalizeBinding (entry) {
8
+ if (typeof entry === 'string') {
9
+ return { control: entry, data: undefined };
10
+ }
11
+ return { control: entry.control, data: entry.data };
12
+ }
13
+
14
+ /**
15
+ * Keyboard-ish control ids use event.code shape (KeyW, Space, ArrowUp, Digit1, …).
16
+ * Gamepad ids are named (AButton, LeftStick, UpDPad, …).
17
+ */
18
+ function isKeyboardControl (control) {
19
+ return (
20
+ control.startsWith('Key') ||
21
+ control.startsWith('Arrow') ||
22
+ control.startsWith('Digit') ||
23
+ control.startsWith('Numpad') ||
24
+ control === 'Space' ||
25
+ control === 'Enter' ||
26
+ control === 'Escape' ||
27
+ control === 'Tab' ||
28
+ control === 'Backspace' ||
29
+ control === 'ShiftLeft' ||
30
+ control === 'ShiftRight' ||
31
+ control === 'ControlLeft' ||
32
+ control === 'ControlRight' ||
33
+ control === 'AltLeft' ||
34
+ control === 'AltRight'
35
+ );
36
+ }
37
+
38
+ /**
39
+ * Map many physical controls onto named logical actions.
40
+ *
41
+ * @param {import('./Controller').default} controller
42
+ * @param {Record<string, Array<string|{ control: string, data?: * }>>} bindings
43
+ * @param {{
44
+ * enabled?: () => boolean,
45
+ * preventDefault?: boolean,
46
+ * autoTick?: boolean,
47
+ * }} [options]
48
+ */
49
+ class ActionMap extends EventEmitter {
50
+ constructor (controller, bindings = {}, options = {}) {
51
+ super();
52
+
53
+ this.controller = controller;
54
+ this.enabled = typeof options.enabled === 'function'
55
+ ? options.enabled
56
+ : () => true;
57
+ this.preventDefault = options.preventDefault !== false;
58
+ this.autoTick = options.autoTick !== false;
59
+
60
+ /** @type {Map<string, Array<{ control: string, data?: * }>>} */
61
+ this._bindings = new Map();
62
+ /** control → [{ action, data }] */
63
+ this._controlIndex = new Map();
64
+ /** action → Set of currently held control ids */
65
+ this._heldByAction = new Map();
66
+ /** action → last event payload while held (for :held ticks) */
67
+ this._lastEvent = new Map();
68
+ this._listeners = [];
69
+ this._disposed = false;
70
+ this._wasEnabled = true;
71
+ this._raf = 0;
72
+
73
+ this._applyBindings(bindings);
74
+
75
+ if (this.preventDefault) {
76
+ this._onKeyDownCapture = (event) => {
77
+ if (!this.enabled()) return;
78
+ if (!this._keyboardCodes.has(event.code)) return;
79
+ event.preventDefault();
80
+ };
81
+ window.addEventListener('keydown', this._onKeyDownCapture, true);
82
+ }
83
+
84
+ this._onBlur = () => {
85
+ this.clearHeld();
86
+ };
87
+ window.addEventListener('blur', this._onBlur);
88
+
89
+ if (this.autoTick) {
90
+ const tick = () => {
91
+ if (this._disposed) return;
92
+ this._raf = requestAnimationFrame(tick);
93
+ this._tick();
94
+ };
95
+ this._raf = requestAnimationFrame(tick);
96
+ }
97
+ }
98
+
99
+ _applyBindings (bindings) {
100
+ this._keyboardCodes = new Set();
101
+
102
+ for (const [action, entries] of Object.entries(bindings)) {
103
+ const list = (entries || []).map(normalizeBinding);
104
+ this._bindings.set(action, list);
105
+ this._heldByAction.set(action, new Set());
106
+
107
+ for (const binding of list) {
108
+ if (isKeyboardControl(binding.control)) {
109
+ this._keyboardCodes.add(binding.control);
110
+ }
111
+ let bucket = this._controlIndex.get(binding.control);
112
+ if (!bucket) {
113
+ bucket = [];
114
+ this._controlIndex.set(binding.control, bucket);
115
+ }
116
+ bucket.push({ action, data: binding.data });
117
+
118
+ const onPressed = (raw) => this._onPhysicalPressed(binding.control, raw);
119
+ const onReleased = (raw) => this._onPhysicalReleased(binding.control, raw);
120
+
121
+ this.controller.on(`${binding.control}:pressed`, onPressed);
122
+ this.controller.on(`${binding.control}:released`, onReleased);
123
+ this._listeners.push(
124
+ { event: `${binding.control}:pressed`, fn: onPressed },
125
+ { event: `${binding.control}:released`, fn: onReleased }
126
+ );
127
+ }
128
+ }
129
+ }
130
+
131
+ _isEnabled () {
132
+ const on = this.enabled();
133
+ if (this._wasEnabled && !on) {
134
+ this.clearHeld();
135
+ }
136
+ this._wasEnabled = on;
137
+ return on;
138
+ }
139
+
140
+ _onPhysicalPressed (control, raw) {
141
+ if (!this._isEnabled()) return;
142
+
143
+ const targets = this._controlIndex.get(control);
144
+ if (!targets) return;
145
+
146
+ for (const { action, data } of targets) {
147
+ const held = this._heldByAction.get(action);
148
+ const wasEmpty = held.size === 0;
149
+ held.add(control);
150
+
151
+ const event = {
152
+ action,
153
+ control,
154
+ data,
155
+ device: raw?.device,
156
+ raw
157
+ };
158
+ this._lastEvent.set(action, event);
159
+
160
+ if (wasEmpty) {
161
+ this.emit(`${action}:pressed`, event);
162
+ }
163
+ this.emit(action, event);
164
+ }
165
+ }
166
+
167
+ _onPhysicalReleased (control, raw) {
168
+ const targets = this._controlIndex.get(control);
169
+ if (!targets) return;
170
+
171
+ for (const { action, data } of targets) {
172
+ const held = this._heldByAction.get(action);
173
+ if (!held.has(control)) continue;
174
+ held.delete(control);
175
+
176
+ const event = {
177
+ action,
178
+ control,
179
+ data,
180
+ device: raw?.device,
181
+ raw
182
+ };
183
+
184
+ if (held.size === 0) {
185
+ this._lastEvent.delete(action);
186
+ this.emit(`${action}:released`, event);
187
+ } else {
188
+ // Prefer another still-held binding's data for subsequent held ticks.
189
+ const still = this._bindings.get(action) || [];
190
+ for (const b of still) {
191
+ if (held.has(b.control)) {
192
+ this._lastEvent.set(action, {
193
+ action,
194
+ control: b.control,
195
+ data: b.data,
196
+ device: raw?.device,
197
+ raw
198
+ });
199
+ break;
200
+ }
201
+ }
202
+ }
203
+ }
204
+ }
205
+
206
+ _tick () {
207
+ if (!this._isEnabled()) return;
208
+ for (const [action, held] of this._heldByAction) {
209
+ if (held.size === 0) continue;
210
+ const event = this._lastEvent.get(action);
211
+ if (!event) continue;
212
+ this.emit(`${action}:held`, event);
213
+ }
214
+ }
215
+
216
+ /**
217
+ * @param {string} action
218
+ * @returns {boolean}
219
+ */
220
+ held (action) {
221
+ const set = this._heldByAction.get(action);
222
+ return Boolean(set && set.size > 0);
223
+ }
224
+
225
+ /**
226
+ * Last event payload for a held action (control + data), or null.
227
+ * @param {string} action
228
+ */
229
+ heldEvent (action) {
230
+ if (!this.held(action)) return null;
231
+ return this._lastEvent.get(action) || null;
232
+ }
233
+
234
+ /** Release all logical actions (emits :released once per previously held action). */
235
+ clearHeld () {
236
+ for (const [action, held] of this._heldByAction) {
237
+ if (held.size === 0) continue;
238
+ const last = this._lastEvent.get(action);
239
+ held.clear();
240
+ this._lastEvent.delete(action);
241
+ this.emit(`${action}:released`, last || { action, control: null, data: undefined });
242
+ }
243
+ }
244
+
245
+ dispose () {
246
+ if (this._disposed) return;
247
+ this._disposed = true;
248
+ cancelAnimationFrame(this._raf);
249
+ for (const { event, fn } of this._listeners) {
250
+ this.controller.off(event, fn);
251
+ }
252
+ this._listeners.length = 0;
253
+ if (this._onKeyDownCapture) {
254
+ window.removeEventListener('keydown', this._onKeyDownCapture, true);
255
+ }
256
+ window.removeEventListener('blur', this._onBlur);
257
+ this.removeAllListeners();
258
+ }
259
+ }
260
+
261
+ export default ActionMap;