melonjs 14.0.2 → 14.1.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 (214) 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 +240 -0
  11. package/dist/melonjs.mjs/audio/audio.js +536 -0
  12. package/dist/melonjs.mjs/camera/camera2d.js +732 -0
  13. package/dist/melonjs.mjs/entity/entity.js +248 -0
  14. package/dist/melonjs.mjs/game.js +29 -0
  15. package/dist/melonjs.mjs/geometries/ellipse.js +275 -0
  16. package/dist/melonjs.mjs/geometries/line.js +116 -0
  17. package/dist/melonjs.mjs/geometries/path2d.js +319 -0
  18. package/dist/melonjs.mjs/geometries/point.js +89 -0
  19. package/dist/melonjs.mjs/geometries/poly.js +500 -0
  20. package/dist/melonjs.mjs/geometries/rectangle.js +375 -0
  21. package/dist/melonjs.mjs/geometries/roundrect.js +168 -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 +448 -0
  32. package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
  33. package/dist/melonjs.mjs/level/tiled/TMXTile.js +194 -0
  34. package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +639 -0
  35. package/dist/melonjs.mjs/level/tiled/TMXTileset.js +311 -0
  36. package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +83 -0
  37. package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
  38. package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +506 -0
  39. package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +220 -0
  40. package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +157 -0
  41. package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +125 -0
  42. package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +109 -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 +618 -0
  47. package/dist/melonjs.mjs/math/math.js +218 -0
  48. package/dist/melonjs.mjs/math/matrix2.js +503 -0
  49. package/dist/melonjs.mjs/math/matrix3.js +681 -0
  50. package/dist/melonjs.mjs/math/observable_vector2.js +471 -0
  51. package/dist/melonjs.mjs/math/observable_vector3.js +561 -0
  52. package/dist/melonjs.mjs/math/vector2.js +528 -0
  53. package/dist/melonjs.mjs/math/vector3.js +569 -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 +267 -0
  62. package/dist/melonjs.mjs/particles/particle.js +188 -0
  63. package/dist/melonjs.mjs/particles/settings.js +319 -0
  64. package/dist/melonjs.mjs/physics/body.js +704 -0
  65. package/dist/melonjs.mjs/physics/bounds.js +460 -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 +391 -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 +221 -0
  72. package/dist/melonjs.mjs/plugin/plugin.js +141 -0
  73. package/dist/melonjs.mjs/renderable/collectable.js +62 -0
  74. package/dist/melonjs.mjs/renderable/colorlayer.js +80 -0
  75. package/dist/melonjs.mjs/renderable/container.js +1018 -0
  76. package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
  77. package/dist/melonjs.mjs/renderable/imagelayer.js +306 -0
  78. package/dist/melonjs.mjs/renderable/light2d.js +156 -0
  79. package/dist/melonjs.mjs/renderable/nineslicesprite.js +247 -0
  80. package/dist/melonjs.mjs/renderable/renderable.js +783 -0
  81. package/dist/melonjs.mjs/renderable/sprite.js +654 -0
  82. package/dist/melonjs.mjs/renderable/trigger.js +157 -0
  83. package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +213 -0
  84. package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +226 -0
  85. package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +128 -0
  86. package/dist/melonjs.mjs/state/stage.js +237 -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 +364 -0
  96. package/dist/melonjs.mjs/text/bitmaptextdata.js +199 -0
  97. package/dist/melonjs.mjs/text/glyph.js +66 -0
  98. package/dist/melonjs.mjs/text/text.js +453 -0
  99. package/dist/melonjs.mjs/text/textmetrics.js +176 -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 +480 -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 +807 -0
  111. package/dist/melonjs.mjs/video/renderer.js +411 -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 +143 -0
  117. package/dist/melonjs.mjs/video/webgl/glshader.js +168 -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 +495 -0
  128. package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1036 -0
  129. package/dist/melonjs.module.d.ts +1163 -1163
  130. package/dist/melonjs.module.js +1903 -3274
  131. package/package.json +22 -17
  132. package/src/application/application.js +3 -3
  133. package/src/audio/audio.js +32 -32
  134. package/src/camera/camera2d.js +31 -31
  135. package/src/entity/entity.js +17 -17
  136. package/src/geometries/ellipse.js +16 -16
  137. package/src/geometries/line.js +5 -5
  138. package/src/geometries/path2d.js +32 -32
  139. package/src/geometries/poly.js +15 -15
  140. package/src/geometries/rectangle.js +18 -18
  141. package/src/geometries/roundrect.js +8 -8
  142. package/src/input/gamepad.js +15 -15
  143. package/src/input/keyboard.js +12 -12
  144. package/src/input/pointer.js +6 -6
  145. package/src/input/pointerevent.js +12 -12
  146. package/src/lang/deprecated.js +12 -12
  147. package/src/level/level.js +25 -25
  148. package/src/level/tiled/TMXLayer.js +22 -22
  149. package/src/level/tiled/TMXTile.js +5 -5
  150. package/src/level/tiled/TMXTileMap.js +6 -6
  151. package/src/level/tiled/TMXTileset.js +2 -2
  152. package/src/level/tiled/TMXUtils.js +5 -5
  153. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +2 -2
  154. package/src/level/tiled/renderer/TMXIsometricRenderer.js +2 -2
  155. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  156. package/src/level/tiled/renderer/TMXRenderer.js +19 -19
  157. package/src/loader/loader.js +20 -20
  158. package/src/math/color.js +20 -20
  159. package/src/math/math.js +16 -16
  160. package/src/math/matrix2.js +16 -16
  161. package/src/math/matrix3.js +25 -25
  162. package/src/math/observable_vector2.js +14 -14
  163. package/src/math/observable_vector3.js +16 -16
  164. package/src/math/vector2.js +9 -9
  165. package/src/math/vector3.js +10 -10
  166. package/src/particles/emitter.js +6 -6
  167. package/src/particles/particle.js +2 -2
  168. package/src/physics/body.js +28 -28
  169. package/src/physics/bounds.js +8 -8
  170. package/src/physics/collision.js +2 -2
  171. package/src/physics/detector.js +6 -6
  172. package/src/physics/quadtree.js +11 -11
  173. package/src/physics/sat.js +31 -31
  174. package/src/physics/world.js +5 -5
  175. package/src/plugin/plugin.js +5 -5
  176. package/src/renderable/collectable.js +3 -3
  177. package/src/renderable/colorlayer.js +5 -5
  178. package/src/renderable/container.js +21 -21
  179. package/src/renderable/dragndrop.js +14 -14
  180. package/src/renderable/imagelayer.js +13 -13
  181. package/src/renderable/light2d.js +3 -3
  182. package/src/renderable/nineslicesprite.js +16 -16
  183. package/src/renderable/renderable.js +23 -23
  184. package/src/renderable/sprite.js +28 -28
  185. package/src/renderable/trigger.js +15 -15
  186. package/src/renderable/ui/uibaseelement.js +7 -7
  187. package/src/renderable/ui/uispriteelement.js +6 -6
  188. package/src/renderable/ui/uitextbutton.js +13 -13
  189. package/src/state/stage.js +7 -7
  190. package/src/state/state.js +17 -17
  191. package/src/system/device.js +11 -11
  192. package/src/system/event.js +10 -10
  193. package/src/system/pooling.js +9 -9
  194. package/src/system/save.js +2 -2
  195. package/src/system/timer.js +10 -10
  196. package/src/text/bitmaptext.js +18 -18
  197. package/src/text/bitmaptextdata.js +2 -2
  198. package/src/text/text.js +23 -23
  199. package/src/text/textmetrics.js +8 -8
  200. package/src/tweens/tween.js +19 -19
  201. package/src/utils/agent.js +5 -5
  202. package/src/utils/array.js +4 -4
  203. package/src/utils/file.js +2 -2
  204. package/src/utils/function.js +6 -6
  205. package/src/utils/string.js +5 -5
  206. package/src/utils/utils.js +4 -4
  207. package/src/video/canvas/canvas_renderer.js +70 -70
  208. package/src/video/renderer.js +26 -26
  209. package/src/video/texture/atlas.js +22 -22
  210. package/src/video/texture/canvas_texture.js +9 -9
  211. package/src/video/video.js +17 -17
  212. package/src/video/webgl/glshader.js +10 -10
  213. package/src/video/webgl/webgl_compositor.js +41 -41
  214. package/src/video/webgl/webgl_renderer.js +75 -75
@@ -0,0 +1,364 @@
1
+ /*!
2
+ * melonJS Game Engine - v14.1.0
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 Color from '../math/color.js';
9
+ import pool from '../system/pooling.js';
10
+ import loader from '../loader/loader.js';
11
+ import Renderable from '../renderable/renderable.js';
12
+ import TextMetrics from './textmetrics.js';
13
+
14
+ /**
15
+ * @classdesc
16
+ * a bitmap font object
17
+ * @augments Renderable
18
+ */
19
+ class BitmapText extends Renderable {
20
+ /**
21
+ * @param {number} x - position of the text object
22
+ * @param {number} y - position of the text object
23
+ * @param {object} settings - the text configuration
24
+ * @param {string|Image} settings.font - a font name to identify the corresponing source image
25
+ * @param {string} [settings.fontData=settings.font] - the bitmap font data corresponding name, or the bitmap font data itself
26
+ * @param {number} [settings.size] - size a scaling ratio
27
+ * @param {Color|string} [settings.fillStyle] - a CSS color value used to tint the bitmapText (@see BitmapText.tint)
28
+ * @param {number} [settings.lineWidth=1] - line width, in pixels, when drawing stroke
29
+ * @param {string} [settings.textAlign="left"] - horizontal text alignment
30
+ * @param {string} [settings.textBaseline="top"] - the text baseline
31
+ * @param {number} [settings.lineHeight=1.0] - line spacing height
32
+ * @param {Vector2d} [settings.anchorPoint={x:0.0, y:0.0}] - anchor point to draw the text at
33
+ * @param {number} [settings.wordWrapWidth] - the maximum length in CSS pixel for a single segment of text
34
+ * @param {(string|string[])} [settings.text] - a string, or an array of strings
35
+ * @example
36
+ * // Use me.loader.preload or me.loader.load to load assets
37
+ * me.loader.preload([
38
+ * { name: "arial", type: "binary" src: "data/font/arial.fnt" },
39
+ * { name: "arial", type: "image" src: "data/font/arial.png" },
40
+ * ])
41
+ * // Then create an instance of your bitmap font:
42
+ * var myFont = new me.BitmapText(x, y, {font:"arial", text:"Hello"});
43
+ * // two possibilities for using "myFont"
44
+ * // either call the draw function from your Renderable draw function
45
+ * myFont.draw(renderer, "Hello!", 0, 0);
46
+ * // or just add it to the word container
47
+ * me.game.world.addChild(myFont);
48
+ */
49
+ constructor(x, y, settings) {
50
+ // call the parent constructor
51
+ super(x, y, settings.width || 0, settings.height || 0);
52
+
53
+ /**
54
+ * Set the default text alignment (or justification),<br>
55
+ * possible values are "left", "right", and "center".
56
+ * @public
57
+ * @type {string}
58
+ * @default "left"
59
+ */
60
+ this.textAlign = settings.textAlign || "left";
61
+
62
+ /**
63
+ * Set the text baseline (e.g. the Y-coordinate for the draw operation), <br>
64
+ * possible values are "top", "hanging, "middle, "alphabetic, "ideographic, "bottom"<br>
65
+ * @public
66
+ * @type {string}
67
+ * @default "top"
68
+ */
69
+ this.textBaseline = settings.textBaseline || "top";
70
+
71
+ /**
72
+ * Set the line spacing height (when displaying multi-line strings). <br>
73
+ * Current font height will be multiplied with this value to set the line height.
74
+ * @public
75
+ * @type {number}
76
+ * @default 1.0
77
+ */
78
+ this.lineHeight = settings.lineHeight || 1.0;
79
+
80
+ /**
81
+ * the maximum length in CSS pixel for a single segment of text.
82
+ * (use -1 to disable word wrapping)
83
+ * @public
84
+ * @type {number}
85
+ * @default -1
86
+ */
87
+ this.wordWrapWidth = settings.wordWrapWidth || -1;
88
+
89
+ /**
90
+ * the text to be displayed
91
+ * @private
92
+ */
93
+ this._text = [];
94
+
95
+ /**
96
+ * scaled font size
97
+ * @private
98
+ */
99
+ this.fontScale = pool.pull("Vector2d", 1.0, 1.0);
100
+
101
+ /**
102
+ * font image
103
+ * @private
104
+ */
105
+ this.fontImage = (typeof settings.font === "object") ? settings.font : loader.getImage(settings.font);
106
+
107
+ if (typeof settings.fontData !== "string") {
108
+ /**
109
+ * font data
110
+ * @private
111
+ */
112
+ // use settings.font to retreive the data from the loader
113
+ this.fontData = pool.pull("BitmapTextData", loader.getBinary(settings.font));
114
+ } else {
115
+ this.fontData = pool.pull("BitmapTextData",
116
+ // if starting/includes "info face" the whole data string was passed as parameter
117
+ (settings.fontData.includes("info face")) ? settings.fontData : loader.getBinary(settings.fontData)
118
+ );
119
+ }
120
+
121
+ // if floating was specified through settings
122
+ if (typeof settings.floating !== "undefined") {
123
+ this.floating = !!settings.floating;
124
+ }
125
+
126
+ // apply given fillstyle
127
+ if (typeof settings.fillStyle !== "undefined") {
128
+ this.fillStyle = settings.fillStyle;
129
+ }
130
+
131
+ // update anchorPoint if provided
132
+ if (typeof settings.anchorPoint !== "undefined") {
133
+ this.anchorPoint.set(settings.anchorPoint.x, settings.anchorPoint.y);
134
+ } else {
135
+ this.anchorPoint.set(0, 0);
136
+ }
137
+
138
+ // instance to text metrics functions
139
+ this.metrics = new TextMetrics(this);
140
+
141
+ // resize if necessary
142
+ if (typeof settings.size === "number" && settings.size !== 1.0) {
143
+ this.resize(settings.size);
144
+ }
145
+
146
+ // set the text
147
+ this.setText(settings.text);
148
+ }
149
+
150
+ /**
151
+ * change the font settings
152
+ * @param {string} textAlign - ("left", "center", "right")
153
+ * @param {number} [scale]
154
+ * @returns {BitmapText} this object for chaining
155
+ */
156
+ set(textAlign, scale) {
157
+ this.textAlign = textAlign;
158
+ // updated scaled Size
159
+ if (scale) {
160
+ this.resize(scale);
161
+ }
162
+ this.isDirty = true;
163
+
164
+ return this;
165
+ }
166
+
167
+ /**
168
+ * change the text to be displayed
169
+ * @param {number|string|string[]} value - a string, or an array of strings
170
+ * @returns {BitmapText} this object for chaining
171
+ */
172
+ setText(value = "") {
173
+ if (this._text.toString() !== value.toString()) {
174
+ if (!Array.isArray(value)) {
175
+ this._text = ("" + value).split("\n");
176
+ } else {
177
+ this._text = value;
178
+ }
179
+ this.isDirty = true;
180
+ }
181
+
182
+ if (this._text.length > 0 && this.wordWrapWidth > 0) {
183
+ this._text = this.metrics.wordWrap(this._text, this.wordWrapWidth);
184
+ }
185
+
186
+ this.getBounds().addBounds(this.metrics.measureText(this._text), true);
187
+
188
+ return this;
189
+ }
190
+
191
+ /**
192
+ * defines the color used to tint the bitmap text
193
+ * @public
194
+ * @type {Color}
195
+ * @see Renderable#tint
196
+ */
197
+ get fillStyle() {
198
+ return this.tint;
199
+ }
200
+ set fillStyle(value) {
201
+ if (value instanceof Color) {
202
+ this.tint.copy(value);
203
+ } else {
204
+ // string (#RGB, #ARGB, #RRGGBB, #AARRGGBB)
205
+ this.tint.parseCSS(value);
206
+ }
207
+ }
208
+
209
+ /**
210
+ * change the font display size
211
+ * @param {number} scale - ratio
212
+ * @returns {BitmapText} this object for chaining
213
+ */
214
+ resize(scale) {
215
+ this.fontScale.set(scale, scale);
216
+
217
+ this.getBounds().addBounds(this.metrics.measureText(this._text), true);
218
+
219
+ // clear the cache text to recalculate bounds
220
+ this.isDirty = true;
221
+
222
+ return this;
223
+ }
224
+
225
+ /**
226
+ * measure the given text size in pixels
227
+ * @param {string} [text]
228
+ * @returns {TextMetrics} a TextMetrics object with two properties: `width` and `height`, defining the output dimensions
229
+ */
230
+ measureText(text = this._text) {
231
+ return this.metrics.measureText(text);
232
+ }
233
+
234
+ /**
235
+ * draw the bitmap font
236
+ * @param {CanvasRenderer|WebGLRenderer} renderer - Reference to the destination renderer instance
237
+ * @param {string} [text]
238
+ * @param {number} [x]
239
+ * @param {number} [y]
240
+ */
241
+ draw(renderer, text, x, y) {
242
+ // save the previous global alpha value
243
+ var _alpha = renderer.globalAlpha();
244
+
245
+ // allows to provide backward compatibility when
246
+ // adding Bitmap Font to an object container
247
+ if (typeof this.ancestor === "undefined") {
248
+ // update cache
249
+ this.setText(text);
250
+ renderer.setGlobalAlpha(_alpha * this.getOpacity());
251
+ } else {
252
+ // added directly to an object container
253
+ x = this.pos.x;
254
+ y = this.pos.y;
255
+ }
256
+
257
+ var lX = x;
258
+ var stringHeight = this.metrics.lineHeight();
259
+ var maxWidth = 0;
260
+
261
+ for (var i = 0; i < this._text.length; i++) {
262
+ x = lX;
263
+ var string = this._text[i].trimEnd();
264
+ // adjust x pos based on alignment value
265
+ var stringWidth = this.metrics.lineWidth(string);
266
+ switch (this.textAlign) {
267
+ case "right":
268
+ x -= stringWidth;
269
+ break;
270
+
271
+ case "center":
272
+ x -= stringWidth * 0.5;
273
+ break;
274
+ }
275
+
276
+ // adjust y pos based on alignment value
277
+ switch (this.textBaseline) {
278
+ case "middle":
279
+ y -= stringHeight * 0.5;
280
+ break;
281
+
282
+ case "ideographic":
283
+ case "alphabetic":
284
+ case "bottom":
285
+ y -= stringHeight;
286
+ break;
287
+ }
288
+
289
+ // update initial position if required
290
+ if (this.isDirty === true && typeof this.ancestor === "undefined") {
291
+ if (i === 0) {
292
+ this.pos.y = y;
293
+ }
294
+ if (maxWidth < stringWidth) {
295
+ maxWidth = stringWidth;
296
+ this.pos.x = x;
297
+ }
298
+ }
299
+
300
+ // draw the string
301
+ var lastGlyph = null;
302
+ for (var c = 0, len = string.length; c < len; c++) {
303
+ // calculate the char index
304
+ var ch = string.charCodeAt(c);
305
+ var glyph = this.fontData.glyphs[ch];
306
+
307
+ if (typeof glyph !== "undefined") {
308
+ var glyphWidth = glyph.width;
309
+ var glyphHeight = glyph.height;
310
+ var kerning = (lastGlyph && lastGlyph.kerning) ? lastGlyph.getKerning(ch) : 0;
311
+ var scaleX = this.fontScale.x;
312
+ var scaleY = this.fontScale.y;
313
+
314
+ // draw it
315
+ if (glyphWidth !== 0 && glyphHeight !== 0) {
316
+ // some browser throw an exception when drawing a 0 width or height image
317
+ renderer.drawImage(this.fontImage,
318
+ glyph.x, glyph.y,
319
+ glyphWidth, glyphHeight,
320
+ x + glyph.xoffset * scaleX,
321
+ y + glyph.yoffset * scaleY,
322
+ glyphWidth * scaleX, glyphHeight * scaleY
323
+ );
324
+ }
325
+
326
+ // increment position
327
+ x += (glyph.xadvance + kerning) * scaleX;
328
+ lastGlyph = glyph;
329
+ } else {
330
+ console.warn("BitmapText: no defined Glyph in for " + String.fromCharCode(ch));
331
+ }
332
+ }
333
+ // increment line
334
+ y += stringHeight;
335
+ }
336
+
337
+ if (typeof this.ancestor === "undefined") {
338
+ // restore the previous global alpha value
339
+ renderer.setGlobalAlpha(_alpha);
340
+ }
341
+
342
+ // clear the dirty flag here for
343
+ // backward compatibility
344
+ this.isDirty = false;
345
+ }
346
+
347
+ /**
348
+ * Destroy function
349
+ * @ignore
350
+ */
351
+ destroy() {
352
+ pool.push(this.fontScale);
353
+ this.fontScale = undefined;
354
+ pool.push(this.fontData);
355
+ this.fontData = undefined;
356
+ this._text.length = 0;
357
+ this.metrics = undefined;
358
+ super.destroy();
359
+ }
360
+
361
+ }
362
+ var BitmapText$1 = BitmapText;
363
+
364
+ export { BitmapText$1 as default };
@@ -0,0 +1,199 @@
1
+ /*!
2
+ * melonJS Game Engine - v14.1.0
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 Glyph from './glyph.js';
9
+
10
+ // bitmap constants
11
+ const capChars = ["M", "N", "B", "D", "C", "E", "F", "K", "A", "G", "H", "I", "J", "L", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
12
+
13
+ /**
14
+ * Gets the value from a string of pairs.
15
+ * @ignore
16
+ */
17
+ function getValueFromPair(string, pattern) {
18
+ var value = string.match(pattern);
19
+ if (!value) {
20
+ throw new Error("Could not find pattern " + pattern + " in string: " + string);
21
+ }
22
+
23
+ return value[0].split("=")[1];
24
+ }
25
+
26
+ /**
27
+ * Gets the first glyph in the map that is not a space character
28
+ * @ignore
29
+ * @name _getFirstGlyph
30
+ * @memberof BitmapTextData
31
+ * @param {object} glyphs - the map of glyphs, each key is a char code
32
+ * @returns {Glyph}
33
+ */
34
+ function getFirstGlyph(glyphs) {
35
+ var keys = Object.keys(glyphs);
36
+ for (var i = 0; i < keys.length; i++) {
37
+ if (keys[i] > 32) {
38
+ return glyphs[keys[i]];
39
+ }
40
+ }
41
+ return null;
42
+ }
43
+
44
+ /**
45
+ * Creates a glyph to use for the space character
46
+ * @ignore
47
+ * @name createSpaceGlyph
48
+ * @memberof BitmapTextData
49
+ * @param {object} glyphs - the map of glyphs, each key is a char code
50
+ */
51
+ function createSpaceGlyph(glyphs) {
52
+ var spaceCharCode = " ".charCodeAt(0);
53
+ var glyph = glyphs[spaceCharCode];
54
+ if (!glyph) {
55
+ glyph = new Glyph();
56
+ glyph.id = spaceCharCode;
57
+ glyph.xadvance = getFirstGlyph(glyphs).xadvance;
58
+ glyphs[spaceCharCode] = glyph;
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Class for storing relevant data from the font file.
64
+ * @ignore
65
+ */
66
+ class BitmapTextData {
67
+
68
+ /**
69
+ * @param {string} data - The bitmap font data pulled from the resource loader using me.loader.getBinary()
70
+ */
71
+ constructor(data) {
72
+ this.onResetEvent(data);
73
+ }
74
+
75
+ /**
76
+ * @ignore
77
+ */
78
+ onResetEvent(data) {
79
+ this.padTop = 0;
80
+ this.padRight = 0;
81
+ this.padBottom = 0;
82
+ this.padLeft = 0;
83
+ this.lineHeight = 0;
84
+ // The distance from the top of most uppercase characters to the baseline. Since the drawing position is the cap height of
85
+ // the first line, the cap height can be used to get the location of the baseline.
86
+ this.capHeight = 1;
87
+ // The distance from the bottom of the glyph that extends the lowest to the baseline. This number is negative.
88
+ this.descent = 0;
89
+
90
+ /**
91
+ * The map of glyphs, each key is a char code.
92
+ * @name glyphs
93
+ * @type {object}
94
+ * @memberof BitmapTextData
95
+ */
96
+ this.glyphs = {};
97
+
98
+ // parse the data
99
+ this.parse(data);
100
+ }
101
+
102
+ /**
103
+ * This parses the font data text and builds a map of glyphs containing the data for each character
104
+ * @name parse
105
+ * @memberof BitmapTextData
106
+ * @param {string} fontData
107
+ */
108
+ parse(fontData) {
109
+ if (!fontData) {
110
+ throw new Error("File containing font data was empty, cannot load the bitmap font.");
111
+ }
112
+ var lines = fontData.split(/\r\n|\n/);
113
+ var padding = fontData.match(/padding\=\d+,\d+,\d+,\d+/g);
114
+ if (!padding) {
115
+ throw new Error("Padding not found in first line");
116
+ }
117
+ var paddingValues = padding[0].split("=")[1].split(",");
118
+ this.padTop = parseFloat(paddingValues[0]);
119
+ this.padLeft = parseFloat(paddingValues[1]);
120
+ this.padBottom = parseFloat(paddingValues[2]);
121
+ this.padRight = parseFloat(paddingValues[3]);
122
+
123
+ this.lineHeight = parseFloat(getValueFromPair(lines[1], /lineHeight\=\d+/g));
124
+
125
+ var baseLine = parseFloat(getValueFromPair(lines[1], /base\=\d+/g));
126
+
127
+ var padY = this.padTop + this.padBottom;
128
+
129
+ var glyph = null;
130
+
131
+ var i;
132
+
133
+ for (i = 4; i < lines.length; i++) {
134
+ var line = lines[i];
135
+ var characterValues = line.split(/=|\s+/);
136
+ if (!line || /^kernings/.test(line)) {
137
+ continue;
138
+ }
139
+ if (/^kerning\s/.test(line)) {
140
+ var first = parseFloat(characterValues[2]);
141
+ var second = parseFloat(characterValues[4]);
142
+ var amount = parseFloat(characterValues[6]);
143
+
144
+ glyph = this.glyphs[first];
145
+ if (glyph !== null && typeof glyph !== "undefined") {
146
+ glyph.setKerning(second, amount);
147
+ }
148
+ } else {
149
+ glyph = new Glyph();
150
+
151
+ var ch = parseFloat(characterValues[2]);
152
+ glyph.id = ch;
153
+ glyph.x = parseFloat(characterValues[4]);
154
+ glyph.y = parseFloat(characterValues[6]);
155
+ glyph.width = parseFloat(characterValues[8]);
156
+ glyph.height = parseFloat(characterValues[10]);
157
+ glyph.xoffset = parseFloat(characterValues[12]);
158
+ glyph.yoffset = parseFloat(characterValues[14]);
159
+ glyph.xadvance = parseFloat(characterValues[16]);
160
+
161
+ if (glyph.width > 0 && glyph.height > 0) {
162
+ this.descent = Math.min(baseLine + glyph.yoffset, this.descent);
163
+ }
164
+
165
+ this.glyphs[ch] = glyph;
166
+ }
167
+ }
168
+
169
+ this.descent += this.padBottom;
170
+
171
+ createSpaceGlyph(this.glyphs);
172
+
173
+ var capGlyph = null;
174
+ for (i = 0; i < capChars.length; i++) {
175
+ var capChar = capChars[i];
176
+ capGlyph = this.glyphs[capChar.charCodeAt(0)];
177
+ if (capGlyph) {
178
+ break;
179
+ }
180
+ }
181
+ if (!capGlyph) {
182
+ for (var charCode in this.glyphs) {
183
+ if (this.glyphs.hasOwnProperty(charCode)) {
184
+ glyph = this.glyphs[charCode];
185
+ if (glyph.height === 0 || glyph.width === 0) {
186
+ continue;
187
+ }
188
+ this.capHeight = Math.max(this.capHeight, glyph.height);
189
+ }
190
+ }
191
+ } else {
192
+ this.capHeight = capGlyph.height;
193
+ }
194
+ this.capHeight -= padY;
195
+ }
196
+ }
197
+ var BitmapTextData$1 = BitmapTextData;
198
+
199
+ export { BitmapTextData$1 as default };
@@ -0,0 +1,66 @@
1
+ /*!
2
+ * melonJS Game Engine - v14.1.0
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
+ // bitmap constants
9
+ const LOG2_PAGE_SIZE = 9;
10
+ const PAGE_SIZE = 1 << LOG2_PAGE_SIZE;
11
+
12
+ /**
13
+ * a glyph representing a single character in a font
14
+ * @ignore
15
+ */
16
+ class Glyph {
17
+ /**
18
+ * @ignore
19
+ */
20
+ constructor() {
21
+ this.id = 0;
22
+ this.x = 0;
23
+ this.y = 0;
24
+ this.width = 0;
25
+ this.height = 0;
26
+ this.u = 0;
27
+ this.v = 0;
28
+ this.u2 = 0;
29
+ this.v2 = 0;
30
+ this.xoffset = 0;
31
+ this.yoffset = 0;
32
+ this.xadvance = 0;
33
+ this.fixedWidth = false;
34
+ }
35
+
36
+ /**
37
+ * @ignore
38
+ */
39
+ getKerning(ch) {
40
+ if (this.kerning) {
41
+ var page = this.kerning[ch >>> LOG2_PAGE_SIZE];
42
+ if (page) {
43
+ return page[ch & PAGE_SIZE - 1] || 0;
44
+ }
45
+ }
46
+ return 0;
47
+ }
48
+
49
+ /**
50
+ * @ignore
51
+ */
52
+ setKerning(ch, value) {
53
+ if (!this.kerning) {
54
+ this.kerning = {};
55
+ }
56
+ var page = this.kerning[ch >>> LOG2_PAGE_SIZE];
57
+ if (typeof page === "undefined") {
58
+ this.kerning[ch >>> LOG2_PAGE_SIZE] = {};
59
+ page = this.kerning[ch >>> LOG2_PAGE_SIZE];
60
+ }
61
+ page[ch & PAGE_SIZE - 1] = value;
62
+ }
63
+ }
64
+ var Glyph$1 = Glyph;
65
+
66
+ export { Glyph$1 as default };