@makefully/engagefully 0.0.0-stage → 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,6 +1,68 @@
1
1
  {
2
2
  "name": "@makefully/engagefully",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
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
+ }
@@ -0,0 +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;
@@ -0,0 +1,74 @@
1
+ import EventEmitter from 'events';
2
+ import controlMap from "./actions/index";
3
+
4
+ class Controller extends EventEmitter {
5
+ constructor (devices = [], controlClasses = {}) {
6
+ super();
7
+
8
+ this.devices = [];
9
+ this.controls = {};
10
+
11
+ // Set up control map with type to class conversions
12
+ this.controlMap = {
13
+ ...controlMap,
14
+ ...controlClasses
15
+ };
16
+
17
+ devices.forEach((device) => this.addDevice(device));
18
+ }
19
+
20
+ addControl (id, type) {
21
+ const
22
+ Control = this.controlMap[type];
23
+
24
+ if (Control) {
25
+ const
26
+ control = this.controls[id] = new Control(id);
27
+
28
+ return control;
29
+ } else {
30
+ const
31
+ control = this.controls[id] = new this.controlMap.Data(id);
32
+
33
+ console.warn(`${id}: "${type}" has no associated control class. Using 'Data'.`);
34
+
35
+ return control;
36
+ }
37
+ }
38
+
39
+ addDevice (device) {
40
+ this.devices.push(device);
41
+
42
+ device.on('updated', (event) => {
43
+ const
44
+ key = event.control;
45
+ let control = this.controls[key];
46
+
47
+ if (!control) {
48
+ control = this.addControl(key, event.type);
49
+
50
+ if (!control) {
51
+ return;
52
+ }
53
+ }
54
+
55
+ control.update(event, this);
56
+ });
57
+ }
58
+
59
+ getControl (id, type) {
60
+ let control = this.controls[id];
61
+
62
+ if (!control && type) {
63
+ control = this.addControl(id, type);
64
+ }
65
+
66
+ return control;
67
+ }
68
+
69
+ poll (id) {
70
+ return this.controls[id]?.poll?.();
71
+ }
72
+ };
73
+
74
+ export default Controller;
@@ -0,0 +1,21 @@
1
+ class Amount {
2
+ constructor (control) {
3
+ this.control = control;
4
+ this.value = 0;
5
+ }
6
+
7
+ update (event, controller) {
8
+ let updated = false;
9
+
10
+ if (event.value !== this.value) {
11
+ updated = true;
12
+ this.value = event.value;
13
+ }
14
+
15
+ if (updated) {
16
+ controller.emit(this.control, event, this);
17
+ }
18
+ }
19
+ }
20
+
21
+ export default Amount;
@@ -0,0 +1,33 @@
1
+ class Button {
2
+ constructor (control) {
3
+ this.control = control;
4
+ this.value = 0;
5
+ }
6
+
7
+ update (event, controller) {
8
+ if (event.value !== this.value) {
9
+ this.value = event.value;
10
+ if (this.value === 1) {
11
+ controller.emit(`${this.control}:pressed`, event, this);
12
+ }
13
+ if (this.value === 0) {
14
+ controller.emit(`${this.control}:released`, event, this);
15
+ }
16
+ controller.emit(this.control, event, this);
17
+ }
18
+ }
19
+
20
+ poll () {
21
+ return this.value;
22
+ }
23
+
24
+ toJSON () {
25
+ return {
26
+ control: this.control,
27
+ type: 'Button',
28
+ value: this.value
29
+ };
30
+ }
31
+ }
32
+
33
+ export default Button;
@@ -0,0 +1,13 @@
1
+ class Data {
2
+ constructor (control) {
3
+ this.control = control;
4
+ this.value = null;
5
+ }
6
+
7
+ update (event, controller) {
8
+ this.value = event.value;
9
+ controller.emit(this.control, event, this);
10
+ }
11
+ }
12
+
13
+ export default Data;
@@ -0,0 +1,15 @@
1
+ class Dial {
2
+ constructor (control) {
3
+ this.control = control;
4
+ this.value = 0;
5
+ }
6
+
7
+ update (event, controller) {
8
+ if (event.value !== this.value) {
9
+ this.value = event.value;
10
+ controller.emit(this.control, event, this);
11
+ }
12
+ }
13
+ }
14
+
15
+ export default Dial;
@@ -0,0 +1,19 @@
1
+ class Drop {
2
+ constructor (control) {
3
+ this.control = control;
4
+ this.value = '';
5
+ this.x = 0;
6
+ this.y = 0;
7
+ }
8
+
9
+ update (event, controller) {
10
+ if ((event.value !== this.value) || (event.x !== this.x) || (event.y !== this.y)) {
11
+ this.value = event.value;
12
+ this.x = event.x;
13
+ this.y = event.y;
14
+ controller.emit(this.control, event, this);
15
+ }
16
+ }
17
+ }
18
+
19
+ export default Drop;
@@ -0,0 +1,17 @@
1
+ class Joystick {
2
+ constructor (control) {
3
+ this.control = control;
4
+ this.x = 0;
5
+ this.y = 0;
6
+ }
7
+
8
+ update (event, controller) {
9
+ if ((event.x !== this.x) || (event.y !== this.y)) {
10
+ this.x = event.x;
11
+ this.y = event.y;
12
+ controller.emit(this.control, event, this);
13
+ }
14
+ }
15
+ }
16
+
17
+ export default Joystick;
@@ -0,0 +1,30 @@
1
+ import {BroadcastChannel} from 'broadcast-channel';
2
+
3
+ class PageAction {
4
+ constructor (controllerOrDeviceList, id = 'PageDevice') {
5
+ const
6
+ devices = controllerOrDeviceList.devices || controllerOrDeviceList;
7
+
8
+ this.channel = new BroadcastChannel(id);
9
+
10
+ devices.forEach((device) => {
11
+ device.on('updated', (event) => {
12
+ const
13
+ newEvent = {
14
+ ...event
15
+ };
16
+
17
+ newEvent.device = `${id}-${event.device}`;
18
+ newEvent.control = `Page${event.control}`;
19
+
20
+ // These aren't necessarily serializable.
21
+ delete newEvent.target;
22
+ delete newEvent.event;
23
+
24
+ this.channel.postMessage(newEvent);
25
+ });
26
+ });
27
+ }
28
+ }
29
+
30
+ export default PageAction;
@@ -0,0 +1,19 @@
1
+ class Pointer {
2
+ constructor (control) {
3
+ this.control = control;
4
+ this.value = 0;
5
+ this.x = 0;
6
+ this.y = 0;
7
+ }
8
+
9
+ update (event, controller) {
10
+ if ((event.value !== this.value) || (event.x !== this.x) || (event.y !== this.y)) {
11
+ this.value = event.value;
12
+ this.x = event.x;
13
+ this.y = event.y;
14
+ controller.emit(this.control, event, this);
15
+ }
16
+ }
17
+ }
18
+
19
+ export default Pointer;
@@ -0,0 +1,6 @@
1
+ class Throttle {
2
+ constructor () {
3
+ }
4
+ }
5
+
6
+ export default Throttle;
@@ -0,0 +1,6 @@
1
+ class Wheel {
2
+ constructor () {
3
+ }
4
+ }
5
+
6
+ export default Wheel;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @namespace actions
3
+ * @memberof engagefully
4
+ */
5
+ import Amount from './Amount';
6
+ import Button from './Button';
7
+ import Data from './Data';
8
+ import Dial from './Dial';
9
+ import Drop from './Drop';
10
+ import PageAction from './PageAction';
11
+ import Pointer from './Pointer';
12
+ import Joystick from './Joystick';
13
+
14
+ export default {
15
+ Amount,
16
+ Button,
17
+ Data,
18
+ Dial,
19
+ Drop,
20
+ PageAction,
21
+ Pointer,
22
+ Joystick
23
+ };
24
+