like2d 2.9.0 → 2.10.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.
Files changed (103) hide show
  1. package/README.md +21 -17
  2. package/dist/__benchmarks__/vector2.bench.d.ts +2 -0
  3. package/dist/__benchmarks__/vector2.bench.d.ts.map +1 -0
  4. package/dist/__benchmarks__/vector2.bench.js +74 -0
  5. package/dist/{core → audio}/audio.d.ts +12 -3
  6. package/dist/audio/audio.d.ts.map +1 -0
  7. package/dist/{core → audio}/audio.js +10 -2
  8. package/dist/audio/index.d.ts +2 -0
  9. package/dist/audio/index.d.ts.map +1 -0
  10. package/dist/audio/index.js +1 -0
  11. package/dist/engine.d.ts +12 -42
  12. package/dist/engine.d.ts.map +1 -1
  13. package/dist/engine.js +34 -76
  14. package/dist/{core/events.d.ts → events.d.ts} +27 -50
  15. package/dist/events.d.ts.map +1 -0
  16. package/dist/events.js +5 -0
  17. package/dist/gamecontrollerdb.txt +2222 -0
  18. package/dist/{core → graphics}/canvas.d.ts +18 -11
  19. package/dist/graphics/canvas.d.ts.map +1 -0
  20. package/dist/{core → graphics}/canvas.js +73 -58
  21. package/dist/{core/graphics.d.ts → graphics/drawing.d.ts} +25 -25
  22. package/dist/graphics/drawing.d.ts.map +1 -0
  23. package/dist/{core/graphics.js → graphics/drawing.js} +59 -52
  24. package/dist/graphics/index.d.ts +19 -0
  25. package/dist/graphics/index.d.ts.map +1 -0
  26. package/dist/graphics/index.js +13 -0
  27. package/dist/index.d.ts +3 -30
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +0 -21
  30. package/dist/input/gamepad-mapping.d.ts +134 -0
  31. package/dist/input/gamepad-mapping.d.ts.map +1 -0
  32. package/dist/input/gamepad-mapping.js +146 -0
  33. package/dist/input/gamepad.d.ts +74 -0
  34. package/dist/input/gamepad.d.ts.map +1 -0
  35. package/dist/input/gamepad.js +288 -0
  36. package/dist/input/index.d.ts +6 -0
  37. package/dist/input/index.d.ts.map +1 -0
  38. package/dist/input/index.js +1 -0
  39. package/dist/input/input.d.ts +76 -0
  40. package/dist/input/input.d.ts.map +1 -0
  41. package/dist/input/input.js +164 -0
  42. package/dist/input/keyboard.d.ts +15 -0
  43. package/dist/input/keyboard.d.ts.map +1 -0
  44. package/dist/{core → input}/keyboard.js +11 -21
  45. package/dist/input/mouse.d.ts +108 -0
  46. package/dist/input/mouse.d.ts.map +1 -0
  47. package/dist/input/mouse.js +241 -0
  48. package/dist/like.d.ts +80 -0
  49. package/dist/like.d.ts.map +1 -0
  50. package/dist/like.js +5 -0
  51. package/dist/math/index.d.ts +16 -0
  52. package/dist/math/index.d.ts.map +1 -1
  53. package/dist/math/index.js +16 -0
  54. package/dist/math/rect.d.ts +24 -27
  55. package/dist/math/rect.d.ts.map +1 -1
  56. package/dist/math/rect.js +47 -73
  57. package/dist/math/vector2.d.ts +87 -32
  58. package/dist/math/vector2.d.ts.map +1 -1
  59. package/dist/math/vector2.js +92 -110
  60. package/dist/prefab-scenes/index.d.ts +1 -0
  61. package/dist/prefab-scenes/index.d.ts.map +1 -1
  62. package/dist/prefab-scenes/index.js +1 -0
  63. package/dist/prefab-scenes/mapGamepad.d.ts +28 -0
  64. package/dist/prefab-scenes/mapGamepad.d.ts.map +1 -0
  65. package/dist/prefab-scenes/mapGamepad.js +181 -0
  66. package/dist/prefab-scenes/startScreen.d.ts +2 -2
  67. package/dist/prefab-scenes/startScreen.js +2 -2
  68. package/dist/scene.d.ts +2 -2
  69. package/dist/scene.d.ts.map +1 -1
  70. package/dist/timer/index.d.ts +2 -0
  71. package/dist/timer/index.d.ts.map +1 -0
  72. package/dist/timer/index.js +1 -0
  73. package/dist/timer/timer.d.ts +32 -0
  74. package/dist/timer/timer.d.ts.map +1 -0
  75. package/dist/{core → timer}/timer.js +20 -3
  76. package/package.json +22 -13
  77. package/dist/core/audio.d.ts.map +0 -1
  78. package/dist/core/canvas.d.ts.map +0 -1
  79. package/dist/core/events.d.ts.map +0 -1
  80. package/dist/core/events.js +0 -21
  81. package/dist/core/gamepad-mapping.d.ts +0 -58
  82. package/dist/core/gamepad-mapping.d.ts.map +0 -1
  83. package/dist/core/gamepad-mapping.js +0 -23
  84. package/dist/core/gamepad.d.ts +0 -37
  85. package/dist/core/gamepad.d.ts.map +0 -1
  86. package/dist/core/gamepad.js +0 -103
  87. package/dist/core/graphics.d.ts.map +0 -1
  88. package/dist/core/input-state.d.ts +0 -14
  89. package/dist/core/input-state.d.ts.map +0 -1
  90. package/dist/core/input-state.js +0 -50
  91. package/dist/core/input.d.ts +0 -40
  92. package/dist/core/input.d.ts.map +0 -1
  93. package/dist/core/input.js +0 -105
  94. package/dist/core/keyboard.d.ts +0 -15
  95. package/dist/core/keyboard.d.ts.map +0 -1
  96. package/dist/core/like.d.ts +0 -113
  97. package/dist/core/like.d.ts.map +0 -1
  98. package/dist/core/like.js +0 -9
  99. package/dist/core/mouse.d.ts +0 -52
  100. package/dist/core/mouse.d.ts.map +0 -1
  101. package/dist/core/mouse.js +0 -142
  102. package/dist/core/timer.d.ts +0 -15
  103. package/dist/core/timer.d.ts.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAExC,YAAY,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACxC,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACpE,YAAY,EAAE,UAAU,EAAE,iBAAiB,IAAI,eAAe,EAAE,MAAM,eAAe,CAAC;AACtF,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,MAAM,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,UAAU,CAAC,SAAS,EAAE,WAAW,GAAG,IAAI,CAGvD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC,YAAY,EAAE,IAAI,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AACzD,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAE/D;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,SAAS,EAAE,WAAW,GAAG,IAAI,CAGvD"}
package/dist/index.js CHANGED
@@ -1,11 +1,8 @@
1
1
  /**
2
2
  * @module like2d
3
3
  * @description A cozy web-native 2D game framework.
4
- *
5
- * See main like/README.md file an for overview of Like2D.
6
4
  */
7
5
  import { Engine } from './engine';
8
- export { ImageHandle } from './core/graphics';
9
6
  /**
10
7
  * Create a new Like2D game instance attached to a DOM container.
11
8
  *
@@ -13,26 +10,8 @@ export { ImageHandle } from './core/graphics';
13
10
  * initializes all subsystems (graphics, audio, input), and returns an object
14
11
  * where you can assign game callbacks.
15
12
  *
16
- * ### How to bind callbacks
17
- *
18
- * ```ts
19
- * export const like = createLike();
20
- *
21
- * like.start();
22
- *
23
- * like.draw = () => { like.gfx.clear('yellow') }
24
- *
25
- * like.update = function (dt: number) {
26
- * if (dt === Math.random()) {
27
- * console.log("You just won the Powerball!")
28
- * }
29
- * }
30
- * ```
31
- *
32
13
  * @param container - The HTML element to attach the game canvas to.
33
- * Must be in the DOM.
34
14
  * @returns A {@link Like} instance ready for callback assignment
35
- *
36
15
  */
37
16
  export function createLike(container) {
38
17
  const engine = new Engine(container);
@@ -0,0 +1,134 @@
1
+ /**
2
+ * @module GamepadMapping
3
+ *
4
+ * A database, generated on module load,
5
+ * which uses SDL's database to coerce
6
+ * browser APIs into physical gamepad button
7
+ * mappings.
8
+ *
9
+ * Browser API shortcomings:
10
+ * - No standard way of exposing vendor and product
11
+ * - Almost nothing is standard
12
+ * - Vendor and product alone doesn't suffice for GUID -- Different controllers have the same.
13
+ * - D-pads either get mapped to an axis (last pair of axes in Chromium) or buttons (Firefox).
14
+ * - Analog axes get mapped differently in Firefox and Chromium.
15
+ *
16
+ * How we overcome them:
17
+ * - We parse out vendor and product based on currently known formats.
18
+ * - We go with best-match and always fall back on manual mapping.
19
+ */
20
+ import type { Vector2 } from "../math";
21
+ /**
22
+ * ref: https://www.w3.org/TR/gamepad/#dfn-standard-gamepad
23
+ * note: `num` is only the corresponding number on standard mapping above.
24
+ *
25
+ * The point of the mapping system is to apply that _or_ non-standard mappings,
26
+ * Which are exceedingly common.
27
+ */
28
+ declare const buttonMap: readonly [{
29
+ readonly sdl: "a";
30
+ readonly like: "BBottom";
31
+ readonly num: number;
32
+ readonly name: "Bottom Face Button";
33
+ }, {
34
+ readonly sdl: "b";
35
+ readonly like: "BRight";
36
+ readonly num: 1;
37
+ readonly name: "Right Face Button";
38
+ }, {
39
+ readonly sdl: "x";
40
+ readonly like: "BLeft";
41
+ readonly num: 2;
42
+ readonly name: "Left Face Button";
43
+ }, {
44
+ readonly sdl: "y";
45
+ readonly like: "BTop";
46
+ readonly num: 3;
47
+ readonly name: "Top Face Button";
48
+ }, {
49
+ readonly sdl: "leftshoulder";
50
+ readonly like: "L1";
51
+ readonly num: 4;
52
+ readonly name: "Left shoulder (front)";
53
+ }, {
54
+ readonly sdl: "rightshoulder";
55
+ readonly like: "R1";
56
+ readonly num: 5;
57
+ readonly name: "Right shoulder (front)";
58
+ }, {
59
+ readonly sdl: "lefttrigger";
60
+ readonly like: "L2";
61
+ readonly num: 6;
62
+ readonly name: "Left shoulder (rear)";
63
+ }, {
64
+ readonly sdl: "righttrigger";
65
+ readonly like: "R2";
66
+ readonly num: 7;
67
+ readonly name: "Right shoulder (rear)";
68
+ }, {
69
+ readonly sdl: "back";
70
+ readonly like: "MenuLeft";
71
+ readonly num: 8;
72
+ readonly name: "Left Menu Button";
73
+ }, {
74
+ readonly sdl: "start";
75
+ readonly like: "MenuRight";
76
+ readonly num: 9;
77
+ readonly name: "Right Menu Button";
78
+ }, {
79
+ readonly sdl: "leftstick";
80
+ readonly like: "LeftStick";
81
+ readonly num: 10;
82
+ readonly name: "Left Stick Button";
83
+ }, {
84
+ readonly sdl: "rightstick";
85
+ readonly like: "RightStick";
86
+ readonly num: 11;
87
+ readonly name: "Right Stick Button";
88
+ }, {
89
+ readonly sdl: "dpup";
90
+ readonly like: "Up";
91
+ readonly num: 12;
92
+ readonly name: "D-Pad Up";
93
+ }, {
94
+ readonly sdl: "dpdown";
95
+ readonly like: "Down";
96
+ readonly num: 13;
97
+ readonly name: "D-Pad Down";
98
+ }, {
99
+ readonly sdl: "dpleft";
100
+ readonly like: "Left";
101
+ readonly num: 14;
102
+ readonly name: "D-Pad Left";
103
+ }, {
104
+ readonly sdl: "dpright";
105
+ readonly like: "Right";
106
+ readonly num: 15;
107
+ readonly name: "D-Pad right";
108
+ }];
109
+ export type LikeButton = (typeof buttonMap)[number]["like"] | `Button${number}` | `Axis${number}+` | `Axis${number}-`;
110
+ export type GamepadMapping = {
111
+ buttons: ButtonMapping;
112
+ sticks: StickMapping[];
113
+ };
114
+ export type ButtonMapping = Record<number, LikeButton>;
115
+ export type StickMapping = [StickAxisMapping, StickAxisMapping];
116
+ export type StickAxisMapping = {
117
+ index: number;
118
+ invert: boolean;
119
+ };
120
+ export declare const defaultMapping: (stickCount: number) => GamepadMapping;
121
+ export declare const standardButtonMapping: () => ButtonMapping;
122
+ export declare const allButtons: Set<string>;
123
+ export declare const fullButtonName: Map<"BBottom" | "BRight" | "BLeft" | "BTop" | "L1" | "R1" | "L2" | "R2" | "MenuLeft" | "MenuRight" | "LeftStick" | "RightStick" | "Up" | "Down" | "Left" | "Right", "Bottom Face Button" | "Right Face Button" | "Left Face Button" | "Top Face Button" | "Left shoulder (front)" | "Right shoulder (front)" | "Left shoulder (rear)" | "Right shoulder (rear)" | "Left Menu Button" | "Right Menu Button" | "Left Stick Button" | "Right Stick Button" | "D-Pad Up" | "D-Pad Down" | "D-Pad Left" | "D-Pad right">;
124
+ export declare const mapStick: (gp: Gamepad, mapping: StickMapping) => Vector2;
125
+ type SdlMapping = {
126
+ vendor: number;
127
+ product: number;
128
+ name: string;
129
+ sdlName: string;
130
+ mapping: Record<number, LikeButton>;
131
+ };
132
+ export declare function getSdlMapping(gamepad: Gamepad): SdlMapping | undefined;
133
+ export {};
134
+ //# sourceMappingURL=gamepad-mapping.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gamepad-mapping.d.ts","sourceRoot":"","sources":["../../src/input/gamepad-mapping.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAEvC;;;;;;GAMG;AACH,QAAA,MAAM,SAAS;;;kBAC0B,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAyBrC,CAAC;AAEX,MAAM,MAAM,UAAU,GAClB,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,GAClC,SAAS,MAAM,EAAE,GACjB,OAAO,MAAM,GAAG,GAChB,OAAO,MAAM,GAAG,CAAC;AAIrB,MAAM,MAAM,cAAc,GAAG;IAC3B,OAAO,EAAE,aAAa,CAAC;IACvB,MAAM,EAAE,YAAY,EAAE,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AACvD,MAAM,MAAM,YAAY,GAAG,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,CAAC;AAChE,MAAM,MAAM,gBAAgB,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAAC;AAElE,eAAO,MAAM,cAAc,GAAI,YAAY,MAAM,KAAG,cAQlD,CAAC;AAEH,eAAO,MAAM,qBAAqB,QAAO,aAC0B,CAAC;AACpE,eAAO,MAAM,UAAU,aAAqD,CAAC;AAC7E,eAAO,MAAM,cAAc,qfAE1B,CAAC;AAEF,eAAO,MAAM,QAAQ,GAAI,IAAI,OAAO,EAAE,SAAS,YAAY,KAAG,OAK7D,CAAC;AAsEF,KAAK,UAAU,GAAG;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CACrC,CAAC;AAEF,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,CA0BtE"}
@@ -0,0 +1,146 @@
1
+ /**
2
+ * @module GamepadMapping
3
+ *
4
+ * A database, generated on module load,
5
+ * which uses SDL's database to coerce
6
+ * browser APIs into physical gamepad button
7
+ * mappings.
8
+ *
9
+ * Browser API shortcomings:
10
+ * - No standard way of exposing vendor and product
11
+ * - Almost nothing is standard
12
+ * - Vendor and product alone doesn't suffice for GUID -- Different controllers have the same.
13
+ * - D-pads either get mapped to an axis (last pair of axes in Chromium) or buttons (Firefox).
14
+ * - Analog axes get mapped differently in Firefox and Chromium.
15
+ *
16
+ * How we overcome them:
17
+ * - We parse out vendor and product based on currently known formats.
18
+ * - We go with best-match and always fall back on manual mapping.
19
+ */
20
+ /**
21
+ * ref: https://www.w3.org/TR/gamepad/#dfn-standard-gamepad
22
+ * note: `num` is only the corresponding number on standard mapping above.
23
+ *
24
+ * The point of the mapping system is to apply that _or_ non-standard mappings,
25
+ * Which are exceedingly common.
26
+ */
27
+ const buttonMap = [
28
+ { sdl: "a", like: "BBottom", num: 0, name: "Bottom Face Button" },
29
+ { sdl: "b", like: "BRight", num: 1, name: "Right Face Button" },
30
+ { sdl: "x", like: "BLeft", num: 2, name: "Left Face Button" },
31
+ { sdl: "y", like: "BTop", num: 3, name: "Top Face Button" },
32
+ { sdl: "leftshoulder", like: "L1", num: 4, name: "Left shoulder (front)" },
33
+ { sdl: "rightshoulder", like: "R1", num: 5, name: "Right shoulder (front)" },
34
+ { sdl: "lefttrigger", like: "L2", num: 6, name: "Left shoulder (rear)" },
35
+ { sdl: "righttrigger", like: "R2", num: 7, name: "Right shoulder (rear)" },
36
+ { sdl: "back", like: "MenuLeft", num: 8, name: "Left Menu Button" },
37
+ { sdl: "start", like: "MenuRight", num: 9, name: "Right Menu Button" },
38
+ { sdl: "leftstick", like: "LeftStick", num: 10, name: "Left Stick Button" },
39
+ {
40
+ sdl: "rightstick",
41
+ like: "RightStick",
42
+ num: 11,
43
+ name: "Right Stick Button",
44
+ },
45
+ { sdl: "dpup", like: "Up", num: 12, name: "D-Pad Up" },
46
+ { sdl: "dpdown", like: "Down", num: 13, name: "D-Pad Down" },
47
+ { sdl: "dpleft", like: "Left", num: 14, name: "D-Pad Left" },
48
+ { sdl: "dpright", like: "Right", num: 15, name: "D-Pad right" },
49
+ ];
50
+ export const defaultMapping = (stickCount) => ({
51
+ buttons: {},
52
+ sticks: Array(stickCount / 2)
53
+ .fill(0)
54
+ .map((_, i) => [
55
+ { index: i * 2, invert: false },
56
+ { index: i * 2 + 1, invert: false },
57
+ ]),
58
+ });
59
+ export const standardButtonMapping = () => Object.fromEntries(buttonMap.map(({ like, num }) => [num, like]));
60
+ export const allButtons = new Set(buttonMap.map(({ like }) => like));
61
+ export const fullButtonName = new Map(buttonMap.map(({ like, name }) => [like, name]));
62
+ export const mapStick = (gp, mapping) => {
63
+ return mapping.map((axis) => (axis.invert ? -1 : 1) * (gp.axes[axis.index] ?? 0));
64
+ };
65
+ //// ************* SDL Gamepad auto-binding system ******************* ////
66
+ // @ts-ignore
67
+ import gamecontrollerdb from "../gamecontrollerdb.txt?raw";
68
+ const sdlButtonSet = new Set(buttonMap.map(({ sdl }) => sdl));
69
+ const sdlToLikeMap = new Map(buttonMap.map(({ sdl, like }) => [sdl, like]));
70
+ const mappingDb = generateMappingDb(gamecontrollerdb);
71
+ function generateMappingDb(db) {
72
+ const dbEntries = db
73
+ .split("\n")
74
+ .map((l) => l.trim())
75
+ .filter((line) => line !== "" && !line.startsWith("#"))
76
+ .map(parseDbLine);
77
+ console.log(`[Gamepad] Parsed ${dbEntries.length} entries from DB`);
78
+ const mappingDb = Object.fromEntries(dbEntries);
79
+ console.log(`[Gamepad] Final entry count: ${Object.keys(mappingDb).length}`);
80
+ return mappingDb;
81
+ }
82
+ function parseDbLine(line) {
83
+ const [guid, name, ...mappings] = line.split(",");
84
+ mappings.pop();
85
+ const vpNum = 0x1 * parseInt(guid.substring(16, 18), 16) +
86
+ 0x100 * parseInt(guid.substring(18, 20), 16) +
87
+ 0x10000 * parseInt(guid.substring(8, 10), 16) +
88
+ 0x1000000 * parseInt(guid.substring(10, 12), 16);
89
+ const mapping = Object.fromEntries(mappings
90
+ .map((s) => {
91
+ const [sdl, bname] = s.split(":");
92
+ const browserIndex = sdlRawButtonToBrowser(bname);
93
+ return (browserIndex !== undefined &&
94
+ sdlButtonSet.has(sdl) && [browserIndex, sdlToLikeMap.get(sdl)]);
95
+ })
96
+ .filter((v) => !!v));
97
+ return [vpNum, { name, mapping }];
98
+ }
99
+ function sdlRawButtonToBrowser(btn) {
100
+ return btn.startsWith("b")
101
+ ? Number(btn.substring(1))
102
+ : btn.startsWith("h")
103
+ ? { 1: 12, 2: 13, 4: 14, 8: 15 }[Number(btn.substring(3))]
104
+ : undefined;
105
+ }
106
+ export function getSdlMapping(gamepad) {
107
+ const parsed = tryParseId(gamepad.id);
108
+ if (parsed) {
109
+ const [vendorStr, productStr, nameStr] = parsed;
110
+ const vendor = parseInt(vendorStr, 16);
111
+ const product = parseInt(productStr, 16);
112
+ const name = nameStr.trim();
113
+ const mapping = mappingDb[vendor * 0x10000 + product];
114
+ if (mapping) {
115
+ console.log(`[Gamepad] Found SDL db mapping for '${name}'`);
116
+ return {
117
+ vendor,
118
+ product,
119
+ name,
120
+ sdlName: mapping.name,
121
+ mapping: mapping.mapping,
122
+ };
123
+ }
124
+ else {
125
+ console.log(`[Gamepad] No SDL db mapping found for '${gamepad.id}.`);
126
+ }
127
+ }
128
+ else {
129
+ console.log(`[Gamepad] Failed to parse id: ${gamepad.id}. Please report this bug with the name of your web browser.`);
130
+ }
131
+ }
132
+ function tryParseId(id) {
133
+ const infoC = id.match(/^([^(]+)\(Vendor: ([0-9a-f]+) Product: ([0-9a-f]+)/i);
134
+ if (infoC) {
135
+ // chrome pattern: Name(Vendor: VEND Product: PROD)
136
+ const [, name, vendor, product] = infoC;
137
+ return [vendor, product, name.trim()];
138
+ }
139
+ else {
140
+ // firefox pattern: VEND-PROD-Name
141
+ const infoF = id.split("-");
142
+ if (infoF.length == 3) {
143
+ return infoF;
144
+ }
145
+ }
146
+ }
@@ -0,0 +1,74 @@
1
+ import { GamepadMapping, LikeButton } from './gamepad-mapping';
2
+ import { type LikeGamepadEvent } from '../events';
3
+ import { Vector2 } from '../math/vector2';
4
+ import { EngineProps } from '../engine';
5
+ /** A selector for a gamepad. */
6
+ export type GamepadTarget = number | "any";
7
+ /** LIKE Gamepad Wrapper
8
+ *
9
+ * - Allows events/callbacks to be sent from joy buttons.
10
+ * - Can track if any gamepad has a button pressed / just pressed.
11
+ * - Remaps raw input numbers to readable strings -- by default using SDL database.
12
+ *
13
+ * If you're planning on supporting gamepads, please include a
14
+ * way to generate {@link GamepadMapping} and set it with {@link Gamepad.setMapping}.
15
+ *
16
+ * If you don't want to make your own, take a look at `prefab-scenes/mapGamepad`.
17
+ */
18
+ export declare class Gamepad {
19
+ private dispatch;
20
+ private gamepads;
21
+ private autoLoadMapping;
22
+ constructor(props: EngineProps<LikeGamepadEvent>);
23
+ private onGamepadConnected;
24
+ /**
25
+ * @private
26
+ * Called by the engine every frame.
27
+ */
28
+ update(): void;
29
+ /**
30
+ *
31
+ * @param target Which controller?
32
+ * @returns all of the sticks. Convention is 0 = left, 1 = right.
33
+ */
34
+ getSticks(target: number): Vector2[];
35
+ fullButtonName(name: LikeButton): string;
36
+ private checkButton;
37
+ /** Check if a gamepad button is down. */
38
+ isDown(target: GamepadTarget, button: LikeButton | number): boolean | undefined;
39
+ /**
40
+ * Returns true for only one frame/update if a button is pressed.
41
+ * Considered an alternative to `like.gamepadpressed`.
42
+ */
43
+ justPressed(target: GamepadTarget, button: LikeButton | number): boolean | undefined;
44
+ /**
45
+ * Get a controller mapping.
46
+ * Note that modifying this mapping in place will modify the target controller.
47
+ * However, use `setMapping` to finalize the mapping.
48
+ */
49
+ getMapping(index: number): GamepadMapping | undefined;
50
+ /**
51
+ * Set the mapping for a particular controller.
52
+ *
53
+ * Set `save = false` if you don't want this written into localstorage.
54
+ */
55
+ setMapping(index: number, mapping: GamepadMapping, save?: boolean): void;
56
+ /**
57
+ * Get saved mapping from db, if it exists.
58
+ */
59
+ loadMapping(index: number): GamepadMapping | undefined;
60
+ /**
61
+ * Save a mapping to persistant storage
62
+ */
63
+ saveMapping(index: number, mapping: GamepadMapping): void;
64
+ /**
65
+ * Enable automatically loading mappings.
66
+ *
67
+ * When a gamepad with a known (to this system) ID is plugged in,
68
+ * this will load the previously saved mapping.
69
+ *
70
+ * @param enable
71
+ */
72
+ enableAutoLoadMapping(enable: boolean): void;
73
+ }
74
+ //# sourceMappingURL=gamepad.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gamepad.d.ts","sourceRoot":"","sources":["../../src/input/gamepad.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkC,cAAc,EAAiB,UAAU,EAAY,MAAM,mBAAmB,CAAC;AACxH,OAAO,EAAmB,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC,gCAAgC;AAChC,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,KAAK,CAAC;AAE3C;;;;;;;;;;GAUG;AACH,qBAAa,OAAO;IAClB,OAAO,CAAC,QAAQ,CAA+B;IAC/C,OAAO,CAAC,QAAQ,CAAoC;IACpD,OAAO,CAAC,eAAe,CAAQ;gBAEnB,KAAK,EAAE,WAAW,CAAC,gBAAgB,CAAC;IAgChD,OAAO,CAAC,kBAAkB;IA+B1B;;;OAGG;IACH,MAAM,IAAI,IAAI;IAId;;;;OAIG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,EAAE;IAQpC,cAAc,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAIxC,OAAO,CAAC,WAAW;IAYnB,yCAAyC;IACzC,MAAM,CACJ,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,UAAU,GAAG,MAAM,GAC1B,OAAO,GAAG,SAAS;IAItB;;;OAGG;IACH,WAAW,CACT,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,UAAU,GAAG,MAAM,GAC1B,OAAO,GAAG,SAAS;IAItB;;;;OAIG;IACH,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAIrD;;;;OAIG;IACH,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,IAAI,UAAO;IAU9D;;OAEG;IACH,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAYtD;;OAEG;IACH,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc;IASlD;;;;;;;OAOG;IACH,qBAAqB,CAAC,MAAM,EAAE,OAAO;CAGtC"}