melonjs 14.0.2 → 14.1.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.
Files changed (219) hide show
  1. package/README.md +2 -0
  2. package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +10 -0
  3. package/dist/melonjs.mjs/_virtual/arraymultimap.js +10 -0
  4. package/dist/melonjs.mjs/_virtual/earcut.js +10 -0
  5. package/dist/melonjs.mjs/_virtual/howler.js +10 -0
  6. package/dist/melonjs.mjs/_virtual/index.js +10 -0
  7. package/dist/melonjs.mjs/_virtual/index2.js +10 -0
  8. package/dist/melonjs.mjs/_virtual/multimap.js +10 -0
  9. package/dist/melonjs.mjs/_virtual/setmultimap.js +10 -0
  10. package/dist/melonjs.mjs/application/application.js +238 -0
  11. package/dist/melonjs.mjs/audio/audio.js +536 -0
  12. package/dist/melonjs.mjs/camera/camera2d.js +731 -0
  13. package/dist/melonjs.mjs/entity/entity.js +247 -0
  14. package/dist/melonjs.mjs/game.js +29 -0
  15. package/dist/melonjs.mjs/geometries/ellipse.js +274 -0
  16. package/dist/melonjs.mjs/geometries/line.js +115 -0
  17. package/dist/melonjs.mjs/geometries/path2d.js +318 -0
  18. package/dist/melonjs.mjs/geometries/point.js +88 -0
  19. package/dist/melonjs.mjs/geometries/poly.js +498 -0
  20. package/dist/melonjs.mjs/geometries/rectangle.js +374 -0
  21. package/dist/melonjs.mjs/geometries/roundrect.js +167 -0
  22. package/dist/melonjs.mjs/index.js +248 -0
  23. package/dist/melonjs.mjs/input/gamepad.js +501 -0
  24. package/dist/melonjs.mjs/input/input.js +26 -0
  25. package/dist/melonjs.mjs/input/keyboard.js +470 -0
  26. package/dist/melonjs.mjs/input/pointer.js +393 -0
  27. package/dist/melonjs.mjs/input/pointerevent.js +818 -0
  28. package/dist/melonjs.mjs/lang/deprecated.js +157 -0
  29. package/dist/melonjs.mjs/level/level.js +297 -0
  30. package/dist/melonjs.mjs/level/tiled/TMXGroup.js +141 -0
  31. package/dist/melonjs.mjs/level/tiled/TMXLayer.js +446 -0
  32. package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
  33. package/dist/melonjs.mjs/level/tiled/TMXTile.js +193 -0
  34. package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +636 -0
  35. package/dist/melonjs.mjs/level/tiled/TMXTileset.js +309 -0
  36. package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +81 -0
  37. package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
  38. package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +504 -0
  39. package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +218 -0
  40. package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +155 -0
  41. package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +124 -0
  42. package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +107 -0
  43. package/dist/melonjs.mjs/loader/loader.js +801 -0
  44. package/dist/melonjs.mjs/loader/loadingscreen.js +120 -0
  45. package/dist/melonjs.mjs/loader/melonjs_logo.png.js +11 -0
  46. package/dist/melonjs.mjs/math/color.js +616 -0
  47. package/dist/melonjs.mjs/math/math.js +218 -0
  48. package/dist/melonjs.mjs/math/matrix2.js +501 -0
  49. package/dist/melonjs.mjs/math/matrix3.js +679 -0
  50. package/dist/melonjs.mjs/math/observable_vector2.js +469 -0
  51. package/dist/melonjs.mjs/math/observable_vector3.js +559 -0
  52. package/dist/melonjs.mjs/math/vector2.js +526 -0
  53. package/dist/melonjs.mjs/math/vector3.js +567 -0
  54. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +73 -0
  55. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +21 -0
  56. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +324 -0
  57. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +69 -0
  58. package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +691 -0
  59. package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +350 -0
  60. package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +3241 -0
  61. package/dist/melonjs.mjs/particles/emitter.js +265 -0
  62. package/dist/melonjs.mjs/particles/particle.js +186 -0
  63. package/dist/melonjs.mjs/particles/settings.js +319 -0
  64. package/dist/melonjs.mjs/physics/body.js +702 -0
  65. package/dist/melonjs.mjs/physics/bounds.js +459 -0
  66. package/dist/melonjs.mjs/physics/collision.js +132 -0
  67. package/dist/melonjs.mjs/physics/detector.js +194 -0
  68. package/dist/melonjs.mjs/physics/quadtree.js +394 -0
  69. package/dist/melonjs.mjs/physics/response.js +57 -0
  70. package/dist/melonjs.mjs/physics/sat.js +483 -0
  71. package/dist/melonjs.mjs/physics/world.js +219 -0
  72. package/dist/melonjs.mjs/plugin/plugin.js +141 -0
  73. package/dist/melonjs.mjs/renderable/collectable.js +60 -0
  74. package/dist/melonjs.mjs/renderable/colorlayer.js +78 -0
  75. package/dist/melonjs.mjs/renderable/container.js +1016 -0
  76. package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
  77. package/dist/melonjs.mjs/renderable/imagelayer.js +305 -0
  78. package/dist/melonjs.mjs/renderable/light2d.js +155 -0
  79. package/dist/melonjs.mjs/renderable/nineslicesprite.js +246 -0
  80. package/dist/melonjs.mjs/renderable/renderable.js +781 -0
  81. package/dist/melonjs.mjs/renderable/sprite.js +653 -0
  82. package/dist/melonjs.mjs/renderable/trigger.js +156 -0
  83. package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +212 -0
  84. package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +225 -0
  85. package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +127 -0
  86. package/dist/melonjs.mjs/state/stage.js +236 -0
  87. package/dist/melonjs.mjs/state/state.js +596 -0
  88. package/dist/melonjs.mjs/system/device.js +909 -0
  89. package/dist/melonjs.mjs/system/dom.js +78 -0
  90. package/dist/melonjs.mjs/system/event.js +537 -0
  91. package/dist/melonjs.mjs/system/platform.js +41 -0
  92. package/dist/melonjs.mjs/system/pooling.js +209 -0
  93. package/dist/melonjs.mjs/system/save.js +157 -0
  94. package/dist/melonjs.mjs/system/timer.js +286 -0
  95. package/dist/melonjs.mjs/text/bitmaptext.js +363 -0
  96. package/dist/melonjs.mjs/text/bitmaptextdata.js +198 -0
  97. package/dist/melonjs.mjs/text/glyph.js +65 -0
  98. package/dist/melonjs.mjs/text/text.js +452 -0
  99. package/dist/melonjs.mjs/text/textmetrics.js +175 -0
  100. package/dist/melonjs.mjs/text/textstyle.js +23 -0
  101. package/dist/melonjs.mjs/tweens/easing.js +336 -0
  102. package/dist/melonjs.mjs/tweens/interpolation.js +112 -0
  103. package/dist/melonjs.mjs/tweens/tween.js +479 -0
  104. package/dist/melonjs.mjs/utils/agent.js +76 -0
  105. package/dist/melonjs.mjs/utils/array.js +63 -0
  106. package/dist/melonjs.mjs/utils/file.js +42 -0
  107. package/dist/melonjs.mjs/utils/function.js +70 -0
  108. package/dist/melonjs.mjs/utils/string.js +82 -0
  109. package/dist/melonjs.mjs/utils/utils.js +173 -0
  110. package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +806 -0
  111. package/dist/melonjs.mjs/video/renderer.js +410 -0
  112. package/dist/melonjs.mjs/video/texture/atlas.js +519 -0
  113. package/dist/melonjs.mjs/video/texture/cache.js +143 -0
  114. package/dist/melonjs.mjs/video/texture/canvas_texture.js +144 -0
  115. package/dist/melonjs.mjs/video/video.js +462 -0
  116. package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +142 -0
  117. package/dist/melonjs.mjs/video/webgl/glshader.js +167 -0
  118. package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +10 -0
  119. package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +10 -0
  120. package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +10 -0
  121. package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +10 -0
  122. package/dist/melonjs.mjs/video/webgl/utils/attributes.js +25 -0
  123. package/dist/melonjs.mjs/video/webgl/utils/precision.js +20 -0
  124. package/dist/melonjs.mjs/video/webgl/utils/program.js +67 -0
  125. package/dist/melonjs.mjs/video/webgl/utils/string.js +25 -0
  126. package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +92 -0
  127. package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +494 -0
  128. package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1035 -0
  129. package/dist/melonjs.module.d.ts +1298 -1359
  130. package/dist/melonjs.module.js +2072 -3520
  131. package/package.json +21 -16
  132. package/src/application/application.js +4 -5
  133. package/src/audio/audio.js +32 -32
  134. package/src/camera/camera2d.js +32 -33
  135. package/src/entity/entity.js +18 -19
  136. package/src/geometries/ellipse.js +17 -18
  137. package/src/geometries/line.js +6 -7
  138. package/src/geometries/path2d.js +33 -34
  139. package/src/geometries/point.js +1 -2
  140. package/src/geometries/poly.js +16 -18
  141. package/src/geometries/rectangle.js +19 -20
  142. package/src/geometries/roundrect.js +9 -10
  143. package/src/input/gamepad.js +15 -15
  144. package/src/input/keyboard.js +12 -12
  145. package/src/input/pointer.js +6 -6
  146. package/src/input/pointerevent.js +12 -12
  147. package/src/lang/deprecated.js +12 -12
  148. package/src/level/level.js +25 -25
  149. package/src/level/tiled/TMXLayer.js +23 -24
  150. package/src/level/tiled/TMXTile.js +6 -7
  151. package/src/level/tiled/TMXTileMap.js +8 -10
  152. package/src/level/tiled/TMXTileset.js +3 -4
  153. package/src/level/tiled/TMXTilesetGroup.js +1 -2
  154. package/src/level/tiled/TMXUtils.js +5 -5
  155. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +3 -4
  156. package/src/level/tiled/renderer/TMXIsometricRenderer.js +3 -4
  157. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +2 -3
  158. package/src/level/tiled/renderer/TMXRenderer.js +20 -21
  159. package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -2
  160. package/src/loader/loader.js +20 -20
  161. package/src/math/color.js +21 -22
  162. package/src/math/math.js +16 -16
  163. package/src/math/matrix2.js +17 -18
  164. package/src/math/matrix3.js +26 -27
  165. package/src/math/observable_vector2.js +15 -16
  166. package/src/math/observable_vector3.js +17 -18
  167. package/src/math/vector2.js +10 -11
  168. package/src/math/vector3.js +11 -12
  169. package/src/particles/emitter.js +7 -8
  170. package/src/particles/particle.js +3 -4
  171. package/src/physics/body.js +29 -30
  172. package/src/physics/bounds.js +10 -10
  173. package/src/physics/collision.js +2 -2
  174. package/src/physics/detector.js +6 -6
  175. package/src/physics/quadtree.js +18 -23
  176. package/src/physics/sat.js +31 -31
  177. package/src/physics/world.js +6 -7
  178. package/src/plugin/plugin.js +5 -5
  179. package/src/renderable/collectable.js +4 -6
  180. package/src/renderable/colorlayer.js +6 -8
  181. package/src/renderable/container.js +25 -27
  182. package/src/renderable/dragndrop.js +14 -14
  183. package/src/renderable/imagelayer.js +14 -15
  184. package/src/renderable/light2d.js +4 -5
  185. package/src/renderable/nineslicesprite.js +17 -18
  186. package/src/renderable/renderable.js +26 -28
  187. package/src/renderable/sprite.js +29 -30
  188. package/src/renderable/trigger.js +16 -17
  189. package/src/renderable/ui/uibaseelement.js +8 -9
  190. package/src/renderable/ui/uispriteelement.js +8 -8
  191. package/src/renderable/ui/uitextbutton.js +15 -15
  192. package/src/state/stage.js +8 -9
  193. package/src/state/state.js +17 -17
  194. package/src/system/device.js +11 -11
  195. package/src/system/event.js +10 -10
  196. package/src/system/pooling.js +9 -9
  197. package/src/system/save.js +2 -2
  198. package/src/system/timer.js +10 -10
  199. package/src/text/bitmaptext.js +19 -20
  200. package/src/text/bitmaptextdata.js +3 -4
  201. package/src/text/glyph.js +1 -2
  202. package/src/text/text.js +24 -25
  203. package/src/text/textmetrics.js +9 -10
  204. package/src/tweens/tween.js +20 -21
  205. package/src/utils/agent.js +5 -5
  206. package/src/utils/array.js +4 -4
  207. package/src/utils/file.js +2 -2
  208. package/src/utils/function.js +6 -6
  209. package/src/utils/string.js +5 -5
  210. package/src/utils/utils.js +4 -4
  211. package/src/video/canvas/canvas_renderer.js +72 -73
  212. package/src/video/renderer.js +27 -28
  213. package/src/video/texture/atlas.js +22 -22
  214. package/src/video/texture/canvas_texture.js +9 -9
  215. package/src/video/video.js +17 -17
  216. package/src/video/webgl/buffer/vertex.js +1 -2
  217. package/src/video/webgl/glshader.js +12 -12
  218. package/src/video/webgl/webgl_compositor.js +42 -43
  219. package/src/video/webgl/webgl_renderer.js +76 -77
@@ -0,0 +1,501 @@
1
+ /*!
2
+ * melonJS Game Engine - v14.1.1
3
+ * http://www.melonjs.org
4
+ * melonjs is licensed under the MIT License.
5
+ * http://www.opensource.org/licenses/mit-license
6
+ * @copyright (C) 2011 - 2022 Olivier Biot (AltByte Pte Ltd)
7
+ */
8
+ import { getBindingKey, triggerKeyEvent } from './keyboard.js';
9
+ import { emit, GAMEPAD_CONNECTED, GAMEPAD_DISCONNECTED, on, GAME_BEFORE_UPDATE, GAMEPAD_UPDATE } from '../system/event.js';
10
+
11
+ // Analog deadzone
12
+ var deadzone = 0.1;
13
+
14
+ /**
15
+ * Normalize axis values for wired Xbox 360
16
+ * @ignore
17
+ */
18
+ function wiredXbox360NormalizeFn(value, axis, button) {
19
+ if (button === this.GAMEPAD.BUTTONS.L2 || button === this.GAMEPAD.BUTTONS.R2) {
20
+ return (value + 1) / 2;
21
+ }
22
+ return value;
23
+ }
24
+
25
+ /**
26
+ * Normalize axis values for OUYA
27
+ * @ignore
28
+ */
29
+ function ouyaNormalizeFn(value, axis, button) {
30
+ if (value > 0) {
31
+ if (button === this.GAMEPAD.BUTTONS.L2) {
32
+ // L2 is wonky; seems like the deadzone is around 20000
33
+ // (That's over 15% of the total range!)
34
+ value = Math.max(0, value - 20000) / 111070;
35
+ }
36
+ else {
37
+ // Normalize [1..65536] => [0.0..0.5]
38
+ value = (value - 1) / 131070;
39
+ }
40
+ }
41
+ else {
42
+ // Normalize [-65536..-1] => [0.5..1.0]
43
+ value = (65536 + value) / 131070 + 0.5;
44
+ }
45
+
46
+ return value;
47
+ }
48
+
49
+ // Match vendor and product codes for Firefox
50
+ var vendorProductRE = /^([0-9a-f]{1,4})-([0-9a-f]{1,4})-/i;
51
+
52
+ // Match leading zeros
53
+ var leadingZeroRE = /^0+/;
54
+
55
+ /**
56
+ * Firefox reports different ids for gamepads depending on the platform:
57
+ * - Windows: vendor and product codes contain leading zeroes
58
+ * - Mac: vendor and product codes are sparse (no leading zeroes)
59
+ *
60
+ * This function normalizes the id to support both formats
61
+ * @ignore
62
+ */
63
+ function addMapping(id, mapping) {
64
+ var expanded_id = id.replace(vendorProductRE, (_, a, b) => {
65
+ return (
66
+ "000".slice(a.length - 1) + a + "-" +
67
+ "000".slice(b.length - 1) + b + "-"
68
+ );
69
+ });
70
+ var sparse_id = id.replace(vendorProductRE, (_, a, b) => {
71
+ return (
72
+ a.replace(leadingZeroRE, "") + "-" +
73
+ b.replace(leadingZeroRE, "") + "-"
74
+ );
75
+ });
76
+
77
+ // Normalize optional parameters
78
+ mapping.analog = mapping.analog || mapping.buttons.map(() => {
79
+ return -1;
80
+ });
81
+ mapping.normalize_fn = mapping.normalize_fn || function (value) { return value; };
82
+
83
+ remap.set(expanded_id, mapping);
84
+ remap.set(sparse_id, mapping);
85
+ }
86
+
87
+ // binding list
88
+ var bindings = {};
89
+
90
+ // mapping list
91
+ var remap = new Map();
92
+
93
+ var updateEventHandler;
94
+
95
+ // Default gamepad mappings
96
+ [
97
+ // Firefox mappings
98
+ [
99
+ "45e-28e-Xbox 360 Wired Controller",
100
+ {
101
+ "axes" : [ 0, 1, 3, 4 ],
102
+ "buttons" : [ 11, 12, 13, 14, 8, 9, -1, -1, 5, 4, 6, 7, 0, 1, 2, 3, 10 ],
103
+ "analog" : [ -1, -1, -1, -1, -1, -1, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1 ],
104
+ "normalize_fn" : wiredXbox360NormalizeFn
105
+ }
106
+ ],
107
+ [
108
+ "54c-268-PLAYSTATION(R)3 Controller",
109
+ {
110
+ "axes" : [ 0, 1, 2, 3 ],
111
+ "buttons" : [ 14, 13, 15, 12, 10, 11, 8, 9, 0, 3, 1, 2, 4, 6, 7, 5, 16 ]
112
+ }
113
+ ],
114
+ [
115
+ "54c-5c4-Wireless Controller", // PS4 Controller
116
+ {
117
+ "axes" : [ 0, 1, 2, 3 ],
118
+ "buttons" : [ 1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 15, 16, 17, 12, 13 ]
119
+ }
120
+ ],
121
+ [
122
+ "2836-1-OUYA Game Controller",
123
+ {
124
+ "axes" : [ 0, 3, 7, 9 ],
125
+ "buttons" : [ 3, 6, 4, 5, 7, 8, 15, 16, -1, -1, 9, 10, 11, 12, 13, 14, -1 ],
126
+ "analog" : [ -1, -1, -1, -1, -1, -1, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1 ],
127
+ "normalize_fn" : ouyaNormalizeFn
128
+ }
129
+ ],
130
+
131
+ // Chrome mappings
132
+ [
133
+ "OUYA Game Controller (Vendor: 2836 Product: 0001)",
134
+ {
135
+ "axes" : [ 0, 1, 3, 4 ],
136
+ "buttons" : [ 0, 3, 1, 2, 4, 5, 12, 13, -1, -1, 6, 7, 8, 9, 10, 11, -1 ],
137
+ "analog" : [ -1, -1, -1, -1, -1, -1, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1 ],
138
+ "normalize_fn" : ouyaNormalizeFn
139
+ }
140
+ ]
141
+ ].forEach((value) => {
142
+ addMapping(value[0], value[1]);
143
+ });
144
+
145
+ /**
146
+ * Update gamepad status
147
+ * @ignore
148
+ */
149
+ var updateGamepads = function () {
150
+ var gamepads = navigator.getGamepads();
151
+
152
+ // Trigger button bindings
153
+ Object.keys(bindings).forEach((index) => {
154
+ var gamepad = gamepads[index];
155
+ if (!gamepad) {
156
+ return;
157
+ }
158
+
159
+ var mapping = null;
160
+ if (gamepad.mapping !== "standard") {
161
+ mapping = remap.get(gamepad.id);
162
+ }
163
+
164
+ var binding = bindings[index];
165
+
166
+ // Iterate all buttons that have active bindings
167
+ Object.keys(binding.buttons).forEach((button) => {
168
+ var last = binding.buttons[button];
169
+ var mapped_button = button;
170
+ var mapped_axis = -1;
171
+
172
+ // Remap buttons if necessary
173
+ if (mapping) {
174
+ mapped_button = mapping.buttons[button];
175
+ mapped_axis = mapping.analog[button];
176
+ if (mapped_button < 0 && mapped_axis < 0) {
177
+ // Button is not mapped
178
+ return;
179
+ }
180
+ }
181
+
182
+ // Get mapped button
183
+ var current = gamepad.buttons[mapped_button] || {};
184
+
185
+ // Remap an axis to an analog button
186
+ if (mapping) {
187
+ if (mapped_axis >= 0) {
188
+ var value = mapping.normalize_fn(gamepad.axes[mapped_axis], -1, +button);
189
+
190
+ // Create a new object, because GamepadButton is read-only
191
+ current = {
192
+ "value" : value,
193
+ "pressed" : current.pressed || (Math.abs(value) >= deadzone)
194
+ };
195
+ }
196
+ }
197
+
198
+ emit(GAMEPAD_UPDATE, index, "buttons", +button, current);
199
+
200
+ // Edge detection
201
+ if (!last.pressed && current.pressed) {
202
+ triggerKeyEvent(last.keyCode, true, mapped_button + 256);
203
+ }
204
+ else if (last.pressed && !current.pressed) {
205
+ triggerKeyEvent(last.keyCode, false, mapped_button + 256);
206
+ }
207
+
208
+ // Update last button state
209
+ last.value = current.value;
210
+ last.pressed = current.pressed;
211
+ });
212
+
213
+ // Iterate all axes that have active bindings
214
+ Object.keys(binding.axes).forEach((axis) => {
215
+ var last = binding.axes[axis];
216
+ var mapped_axis = axis;
217
+
218
+ // Remap buttons if necessary
219
+ if (mapping) {
220
+ mapped_axis = mapping.axes[axis];
221
+ if (mapped_axis < 0) {
222
+ // axe is not mapped
223
+ return;
224
+ }
225
+ }
226
+
227
+ // retrieve the current value and normalize if necessary
228
+ var value = gamepad.axes[mapped_axis];
229
+ if (typeof(value) === "undefined") {
230
+ return;
231
+ }
232
+ if (mapping) {
233
+ value = mapping.normalize_fn(value, +axis, -1);
234
+ }
235
+ // normalize value into a [-1, 1] range value (treat 0 as positive)
236
+ var range = Math.sign(value) || 1;
237
+ if (last[range].keyCode === 0) {
238
+ return;
239
+ }
240
+ var pressed = (Math.abs(value) >= (deadzone + Math.abs(last[range].threshold)));
241
+
242
+ emit(GAMEPAD_UPDATE, index, "axes", +axis, value);
243
+
244
+ // Edge detection
245
+ if (!last[range].pressed && pressed) {
246
+ // Release the opposite direction, if necessary
247
+ if (last[-range].pressed) {
248
+ triggerKeyEvent(last[-range].keyCode, false, mapped_axis + 256);
249
+ last[-range].value = 0;
250
+ last[-range].pressed = false;
251
+ }
252
+
253
+ triggerKeyEvent(last[range].keyCode, true, mapped_axis + 256);
254
+ }
255
+ else if ((last[range].pressed || last[-range].pressed) && !pressed) {
256
+ range = last[range].pressed ? range : -range;
257
+ triggerKeyEvent(last[range].keyCode, false, mapped_axis + 256);
258
+ }
259
+
260
+ // Update last axis state
261
+ last[range].value = value;
262
+ last[range].pressed = pressed;
263
+ });
264
+ });
265
+ };
266
+
267
+ // gamepad connected callback
268
+ if (globalThis.navigator && typeof globalThis.navigator.getGamepads === "function") {
269
+ globalThis.addEventListener("gamepadconnected", (e) => {
270
+ emit(GAMEPAD_CONNECTED, e.gamepad);
271
+ }, false);
272
+
273
+ /*
274
+ * gamepad disconnected callback
275
+ */
276
+ globalThis.addEventListener("gamepaddisconnected", (e) => {
277
+ emit(GAMEPAD_DISCONNECTED, e.gamepad);
278
+ }, false);
279
+ }
280
+
281
+ /*
282
+ * PUBLIC STUFF
283
+ */
284
+
285
+ /**
286
+ * Namespace for standard gamepad mapping constants
287
+ * @public
288
+ * @namespace GAMEPAD
289
+ * @memberof input
290
+ */
291
+ var GAMEPAD = {
292
+ /**
293
+ * Standard gamepad mapping information for axes<br>
294
+ * <ul>
295
+ * <li>Left control stick: <code>LX</code> (horizontal), <code>LY</code> (vertical)</li>
296
+ * <li>Right control stick: <code>RX</code> (horizontal), <code>RY</code> (vertical)</li>
297
+ * <li>Extras: <code>EXTRA_1</code>, <code>EXTRA_2</code>, <code>EXTRA_3</code>, <code>EXTRA_4</code></li>
298
+ * </ul>
299
+ * @public
300
+ * @name AXES
301
+ * @enum {number}
302
+ * @memberof input.GAMEPAD
303
+ * @see https://w3c.github.io/gamepad/#remapping
304
+ */
305
+ "AXES" : {
306
+ "LX" : 0,
307
+ "LY" : 1,
308
+ "RX" : 2,
309
+ "RY" : 3,
310
+ "EXTRA_1" : 4,
311
+ "EXTRA_2" : 5,
312
+ "EXTRA_3" : 6,
313
+ "EXTRA_4" : 7
314
+ },
315
+
316
+ /**
317
+ * Standard gamepad mapping information for buttons<br>
318
+ * <ul>
319
+ * <li>Face buttons: <code>FACE_1</code>, <code>FACE_2</code>, <code>FACE_3</code>, <code>FACE_4</code></li>
320
+ * <li>D-Pad: <code>UP</code>, <code>DOWN</code>, <code>LEFT</code>, <code>RIGHT</code></li>
321
+ * <li>Shoulder buttons: <code>L1</code>, <code>L2</code>, <code>R1</code>, <code>R2</code></li>
322
+ * <li>Analog stick (clicks): <code>L3</code>, <code>R3</code></li>
323
+ * <li>Navigation: <code>SELECT</code> (<code>BACK</code>), <code>START</code> (<code>FORWARD</code>), <code>HOME</code></li>
324
+ * <li>Extras: <code>EXTRA_1</code>, <code>EXTRA_2</code>, <code>EXTRA_3</code>, <code>EXTRA_4</code></li>
325
+ * </ul>
326
+ * @public
327
+ * @name BUTTONS
328
+ * @enum {number}
329
+ * @memberof input.GAMEPAD
330
+ * @see https://w3c.github.io/gamepad/#remapping
331
+ */
332
+ "BUTTONS" : {
333
+ "FACE_1" : 0,
334
+ "FACE_2" : 1,
335
+ "FACE_3" : 2,
336
+ "FACE_4" : 3,
337
+ "L1" : 4,
338
+ "R1" : 5,
339
+ "L2" : 6,
340
+ "R2" : 7,
341
+ "SELECT" : 8,
342
+ "BACK" : 8,
343
+ "START" : 9,
344
+ "FORWARD" : 9,
345
+ "L3" : 10,
346
+ "R3" : 11,
347
+ "UP" : 12,
348
+ "DOWN" : 13,
349
+ "LEFT" : 14,
350
+ "RIGHT" : 15,
351
+ "HOME" : 16,
352
+ "EXTRA_1" : 17,
353
+ "EXTRA_2" : 18,
354
+ "EXTRA_3" : 19,
355
+ "EXTRA_4" : 20
356
+ }
357
+ };
358
+
359
+ /**
360
+ * Associate a gamepad event to a keycode
361
+ * @name bindGamepad
362
+ * @memberof input
363
+ * @public
364
+ * @param {number} index - Gamepad index
365
+ * @param {object} button - Button/Axis definition
366
+ * @param {string} button.type - "buttons" or "axes"
367
+ * @param {number} button.code - button or axis code id (See {@link input.GAMEPAD.BUTTONS}, {@link input.GAMEPAD.AXES})
368
+ * @param {number} [button.threshold=1] - value indicating when the axis should trigger the keycode (e.g. -0.5 or 0.5)
369
+ * @param {number} keyCode - (See {@link input.KEY})
370
+ * @example
371
+ * // enable the keyboard
372
+ * me.input.bindKey(me.input.KEY.X, "shoot");
373
+ * ...
374
+ * // map the lower face button on the first gamepad to the X key
375
+ * me.input.bindGamepad(0, {type:"buttons", code: me.input.GAMEPAD.BUTTONS.FACE_1}, me.input.KEY.X);
376
+ * // map the left axis value on the first gamepad to the LEFT key
377
+ * me.input.bindGamepad(0, {type:"axes", code: me.input.GAMEPAD.AXES.LX, threshold: -0.5}, me.input.KEY.LEFT);
378
+ */
379
+ function bindGamepad(index, button, keyCode) {
380
+ // Throw an exception if no action is defined for the specified keycode
381
+ if (!getBindingKey(keyCode)) {
382
+ throw new Error("no action defined for keycode " + keyCode);
383
+ }
384
+
385
+ // register to the the update event if not yet done and supported by the browser
386
+ // if not supported, the function will fail silently (-> update loop won't be called)
387
+ if (typeof updateEventHandler === "undefined" && typeof navigator.getGamepads === "function") {
388
+ updateEventHandler = on(GAME_BEFORE_UPDATE, updateGamepads);
389
+ }
390
+
391
+ // Allocate bindings if not defined
392
+ if (!bindings[index]) {
393
+ bindings[index] = {
394
+ "axes" : {},
395
+ "buttons" : {}
396
+ };
397
+ }
398
+
399
+ var mapping = {
400
+ "keyCode" : keyCode,
401
+ "value" : 0,
402
+ "pressed" : false,
403
+ "threshold" : button.threshold // can be undefined
404
+ };
405
+ var binding = bindings[index][button.type];
406
+
407
+ // Map the gamepad button or axis to the keycode
408
+ if (button.type === "buttons") {
409
+ // buttons are defined by a `gamePadButton` object
410
+ binding[button.code] = mapping;
411
+ } else if (button.type === "axes") {
412
+ // normalize threshold into a value that can represent both side of the axis
413
+ var range = (Math.sign(button.threshold) || 1);
414
+ // axes are defined using two objects; one for negative and one for positive
415
+ if (!binding[button.code]) {
416
+ binding[button.code] = {};
417
+ }
418
+ var axes = binding[button.code];
419
+ axes[range] = mapping;
420
+
421
+ // Ensure the opposite axis exists
422
+ if (!axes[-range]) {
423
+ axes[-range] = {
424
+ "keyCode" : 0,
425
+ "value" : 0,
426
+ "pressed" : false,
427
+ "threshold" : -range
428
+ };
429
+ }
430
+ }
431
+ }
432
+
433
+ /**
434
+ * unbind the defined keycode
435
+ * @name unbindGamepad
436
+ * @memberof input
437
+ * @public
438
+ * @param {number} index - Gamepad index
439
+ * @param {number} button - (See {@link input.GAMEPAD.BUTTONS})
440
+ * @example
441
+ * me.input.unbindGamepad(0, me.input.GAMEPAD.BUTTONS.FACE_1);
442
+ */
443
+ function unbindGamepad(index, button) {
444
+ if (!bindings[index]) {
445
+ throw new Error("no bindings for gamepad " + index);
446
+ }
447
+ bindings[index].buttons[button] = {};
448
+ }
449
+
450
+ /**
451
+ * Set deadzone for analog gamepad inputs<br>
452
+ * The default deadzone is 0.1 (10%) Analog values less than this will be ignored
453
+ * @name setGamepadDeadzone
454
+ * @memberof input
455
+ * @public
456
+ * @param {number} value - Deadzone value
457
+ */
458
+ function setGamepadDeadzone(value) {
459
+ deadzone = value;
460
+ }
461
+
462
+ /**
463
+ * specify a custom mapping for a specific gamepad id<br>
464
+ * see below for the default mapping : <br>
465
+ * <center><img src="images/gamepad_diagram.png"/></center><br>
466
+ * @name setGamepadMapping
467
+ * @memberof input
468
+ * @public
469
+ * @param {string} id - Gamepad id string
470
+ * @param {object} mapping - A hash table
471
+ * @param {number[]} mapping.axes - Standard analog control stick axis locations
472
+ * @param {number[]} mapping.buttons - Standard digital button locations
473
+ * @param {number[]} [mapping.analog] - Analog axis locations for buttons
474
+ * @param {Function} [mapping.normalize_fn] - a function that returns a normalized value in range [-1.0..1.0] for the given value, axis and button
475
+ * @example
476
+ * // A weird controller that has its axis mappings reversed
477
+ * me.input.setGamepadMapping("Generic USB Controller", {
478
+ * "axes" : [ 3, 2, 1, 0 ],
479
+ * "buttons" : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]
480
+ * });
481
+ *
482
+ * // Mapping extra axes to analog buttons
483
+ * me.input.setGamepadMapping("Generic Analog Controller", {
484
+ * "axes" : [ 0, 1, 2, 3 ],
485
+ * "buttons" : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ],
486
+ *
487
+ * // Raw axis 4 is mapped to GAMEPAD.BUTTONS.FACE_1
488
+ * // Raw axis 5 is mapped to GAMEPAD.BUTTONS.FACE_2
489
+ * // etc...
490
+ * // Also maps left and right triggers
491
+ * "analog" : [ 4, 5, 6, 7, -1, -1, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1 ],
492
+ *
493
+ * // Normalize the value of button L2: [-1.0..1.0] => [0.0..1.0]
494
+ * "normalize_fn" : function (value, axis, button) {
495
+ * return ((button === me.input.GAMEPAD.BUTTONS.L2) ? ((value + 1) / 2) : value) || 0;
496
+ * }
497
+ * });
498
+ */
499
+ var setGamepadMapping = addMapping;
500
+
501
+ export { GAMEPAD, bindGamepad, setGamepadDeadzone, setGamepadMapping, unbindGamepad };
@@ -0,0 +1,26 @@
1
+ /*!
2
+ * melonJS Game Engine - v14.1.1
3
+ * http://www.melonjs.org
4
+ * melonjs is licensed under the MIT License.
5
+ * http://www.opensource.org/licenses/mit-license
6
+ * @copyright (C) 2011 - 2022 Olivier Biot (AltByte Pte Ltd)
7
+ */
8
+ export { bindPointer, exitPointerLock, globalToLocal, locked, pointer, pointerEventTarget, registerPointerEvent, releaseAllPointerEvents, releasePointerEvent, requestPointerLock, setTouchAction, throttlingInterval, unbindPointer } from './pointerevent.js';
9
+ export { KEY, bindKey, getBindingKey, initKeyboardEvent, isKeyPressed, keyBoardEventTarget, keyStatus, triggerKeyEvent, unbindKey, unlockKey } from './keyboard.js';
10
+ export { GAMEPAD, bindGamepad, setGamepadDeadzone, setGamepadMapping, unbindGamepad } from './gamepad.js';
11
+
12
+ /**
13
+ * @namespace input
14
+ */
15
+
16
+ /**
17
+ * specify if melonJS should prevent all default browser action on registered events.
18
+ * @public
19
+ * @type {boolean}
20
+ * @default true
21
+ * @name preventDefault
22
+ * @memberof input
23
+ */
24
+ var preventDefault = true;
25
+
26
+ export { preventDefault };