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,452 @@
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 Color from '../math/color.js';
9
+ import WebGLRenderer from '../video/webgl/webgl_renderer.js';
10
+ import { renderer } from '../video/video.js';
11
+ import pool from '../system/pooling.js';
12
+ import Renderable from '../renderable/renderable.js';
13
+ import { nextPowerOfTwo } from '../math/math.js';
14
+ import setContextStyle from './textstyle.js';
15
+ import TextMetrics from './textmetrics.js';
16
+
17
+ /*
18
+ * ASCII Table
19
+ * http://www.asciitable.com/
20
+ * [ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz]
21
+ *
22
+ * -> first char " " 32d (0x20);
23
+ */
24
+
25
+
26
+ const runits = ["ex", "em", "pt", "px"];
27
+ const toPX = [12, 24, 0.75, 1];
28
+
29
+ // return a valid 2d context for Text rendering/styling
30
+ var getContext2d = function (renderer$1, text) {
31
+ if (text.offScreenCanvas === true) {
32
+ return text.canvasTexture.context;
33
+ } else {
34
+ if (typeof renderer$1 === "undefined") {
35
+ renderer$1 = renderer;
36
+ }
37
+ return renderer$1.getFontContext();
38
+ }
39
+ };
40
+
41
+ /**
42
+ * @classdesc
43
+ * a generic system font object.
44
+ * @augments Renderable
45
+ */
46
+ class Text extends Renderable {
47
+ /**
48
+ * @param {number} x - position of the text object
49
+ * @param {number} y - position of the text object
50
+ * @param {object} settings - the text configuration
51
+ * @param {string} settings.font - a CSS family font name
52
+ * @param {number|string} settings.size - size, or size + suffix (px, em, pt)
53
+ * @param {Color|string} [settings.fillStyle="#000000"] - a CSS color value
54
+ * @param {Color|string} [settings.strokeStyle="#000000"] - a CSS color value
55
+ * @param {number} [settings.lineWidth=1] - line width, in pixels, when drawing stroke
56
+ * @param {string} [settings.textAlign="left"] - horizontal text alignment
57
+ * @param {string} [settings.textBaseline="top"] - the text baseline
58
+ * @param {number} [settings.lineHeight=1.0] - line spacing height
59
+ * @param {Vector2d} [settings.anchorPoint={x:0.0, y:0.0}] - anchor point to draw the text at
60
+ * @param {boolean} [settings.offScreenCanvas=false] - whether to draw the font to an individual "cache" texture first
61
+ * @param {number} [settings.wordWrapWidth] - the maximum length in CSS pixel for a single segment of text
62
+ * @param {(string|string[])} [settings.text=""] - a string, or an array of strings
63
+ * @example
64
+ * var font = new me.Text(0, 0, {font: "Arial", size: 8, fillStyle: this.color});
65
+ */
66
+ constructor(x, y, settings) {
67
+ // call the parent constructor
68
+ super(x, y, settings.width || 0, settings.height || 0);
69
+ this.onResetEvent(x, y, settings);
70
+ }
71
+
72
+ /** @ignore */
73
+ onResetEvent(x, y, settings) {
74
+
75
+ /**
76
+ * defines the color used to draw the font.<br>
77
+ * @public
78
+ * @member {Color}
79
+ * @name Text#fillStyle
80
+ * @default black
81
+ */
82
+ if (typeof settings.fillStyle !== "undefined") {
83
+ if (settings.fillStyle instanceof Color) {
84
+ this.fillStyle = settings.fillStyle;
85
+ } else {
86
+ // string (#RGB, #ARGB, #RRGGBB, #AARRGGBB)
87
+ this.fillStyle = pool.pull("Color").parseCSS(settings.fillStyle);
88
+ }
89
+ } else {
90
+ this.fillStyle = pool.pull("Color", 0, 0, 0);
91
+ }
92
+
93
+ /**
94
+ * defines the color used to draw the font stroke.<br>
95
+ * @public
96
+ * @member {Color}
97
+ * @name strokeStyle
98
+ * @default black
99
+ */
100
+ if (typeof settings.strokeStyle !== "undefined") {
101
+ if (settings.strokeStyle instanceof Color) {
102
+ this.strokeStyle = settings.strokeStyle;
103
+ } else {
104
+ // string (#RGB, #ARGB, #RRGGBB, #AARRGGBB)
105
+ this.strokeStyle = pool.pull("Color").parseCSS(settings.strokeStyle);
106
+ }
107
+ } else {
108
+ this.strokeStyle = pool.pull("Color", 0, 0, 0);
109
+ }
110
+
111
+ /**
112
+ * sets the current line width, in pixels, when drawing stroke
113
+ * @public
114
+ * @type {number}
115
+ * @default 1
116
+ */
117
+ this.lineWidth = settings.lineWidth || 1;
118
+
119
+ /**
120
+ * Set the default text alignment (or justification),<br>
121
+ * possible values are "left", "right", and "center".<br>
122
+ * @public
123
+ * @type {string}
124
+ * @default "left"
125
+ */
126
+ this.textAlign = settings.textAlign || "left";
127
+
128
+ /**
129
+ * Set the text baseline (e.g. the Y-coordinate for the draw operation), <br>
130
+ * possible values are "top", "hanging, "middle, "alphabetic, "ideographic, "bottom"<br>
131
+ * @public
132
+ * @type {string}
133
+ * @default "top"
134
+ */
135
+ this.textBaseline = settings.textBaseline || "top";
136
+
137
+ /**
138
+ * Set the line spacing height (when displaying multi-line strings). <br>
139
+ * Current font height will be multiplied with this value to set the line height.
140
+ * @public
141
+ * @type {number}
142
+ * @default 1.0
143
+ */
144
+ this.lineHeight = settings.lineHeight || 1.0;
145
+
146
+ /**
147
+ * whether to draw the font to a indidividual offscreen canvas texture first <br>
148
+ * Note: this will improve performances when using WebGL, but will impact
149
+ * memory consumption as every text element will have its own canvas texture
150
+ * @public
151
+ * @type {boolean}
152
+ * @default false
153
+ */
154
+ this.offScreenCanvas = false;
155
+
156
+ /**
157
+ * the maximum length in CSS pixel for a single segment of text.
158
+ * (use -1 to disable word wrapping)
159
+ * @public
160
+ * @type {number}
161
+ * @default -1
162
+ */
163
+ this.wordWrapWidth = settings.wordWrapWidth || -1;
164
+
165
+ /**
166
+ * the text to be displayed
167
+ * @private
168
+ */
169
+ this._text = [];
170
+
171
+ /**
172
+ * the font size (in px)
173
+ * @public
174
+ * @type {number}
175
+ * @default 10
176
+ */
177
+ this.fontSize = 10;
178
+
179
+ // anchor point
180
+ if (typeof settings.anchorPoint !== "undefined") {
181
+ this.anchorPoint.setV(settings.anchorPoint);
182
+ } else {
183
+ this.anchorPoint.set(0, 0);
184
+ }
185
+
186
+ // if floating was specified through settings
187
+ if (typeof settings.floating !== "undefined") {
188
+ this.floating = !!settings.floating;
189
+ }
190
+
191
+ // font name and type
192
+ this.setFont(settings.font, settings.size);
193
+
194
+ // aditional
195
+ if (settings.bold === true) {
196
+ this.bold();
197
+ }
198
+ if (settings.italic === true) {
199
+ this.italic();
200
+ }
201
+
202
+ if (settings.offScreenCanvas === true) {
203
+ this.offScreenCanvas = true;
204
+ this.canvasTexture = pool.pull("CanvasTexture", 2, 2, { offscreenCanvas: true });
205
+ }
206
+
207
+ // instance to text metrics functions
208
+ this.metrics = new TextMetrics(this);
209
+
210
+ // set the text
211
+ this.setText(settings.text);
212
+ }
213
+
214
+ /**
215
+ * make the font bold
216
+ * @returns {Text} this object for chaining
217
+ */
218
+ bold() {
219
+ this.font = "bold " + this.font;
220
+ this.isDirty = true;
221
+ return this;
222
+ }
223
+
224
+ /**
225
+ * make the font italic
226
+ * @returns {Text} this object for chaining
227
+ */
228
+ italic() {
229
+ this.font = "italic " + this.font;
230
+ this.isDirty = true;
231
+ return this;
232
+ }
233
+
234
+ /**
235
+ * set the font family and size
236
+ * @param {string} font - a CSS font name
237
+ * @param {number|string} [size=10] - size in px, or size + suffix (px, em, pt)
238
+ * @returns {Text} this object for chaining
239
+ * @example
240
+ * font.setFont("Arial", 20);
241
+ * font.setFont("Arial", "1.5em");
242
+ */
243
+ setFont(font, size = 10) {
244
+ // font name and type
245
+ var font_names = font.split(",").map((value) => {
246
+ value = value.trim();
247
+ return (
248
+ !/(^".*"$)|(^'.*'$)/.test(value)
249
+ ) ? "\"" + value + "\"" : value;
250
+ });
251
+
252
+ // font size
253
+ if (typeof size === "number") {
254
+ this.fontSize = size;
255
+ size += "px";
256
+ } else /* string */ {
257
+ // extract the units and convert if necessary
258
+ var CSSval = size.match(/([-+]?[\d.]*)(.*)/);
259
+ this.fontSize = parseFloat(CSSval[1]);
260
+ if (CSSval[2]) {
261
+ this.fontSize *= toPX[runits.indexOf(CSSval[2])];
262
+ } else {
263
+ // no unit define, assume px
264
+ size += "px";
265
+ }
266
+ }
267
+ this.height = this.fontSize;
268
+ this.font = size + " " + font_names.join(",");
269
+
270
+ this.isDirty = true;
271
+
272
+ return this;
273
+ }
274
+
275
+ /**
276
+ * change the text to be displayed
277
+ * @param {number|string|string[]} value - a string, or an array of strings
278
+ * @returns {Text} this object for chaining
279
+ */
280
+ setText(value = "") {
281
+ var bounds = this.getBounds();
282
+
283
+ // set the next text
284
+ if (this._text.toString() !== value.toString()) {
285
+ if (!Array.isArray(value)) {
286
+ this._text = ("" + value).split("\n");
287
+ } else {
288
+ this._text = value;
289
+ }
290
+ }
291
+
292
+ // word wrap if necessary
293
+ if (this._text.length > 0 && this.wordWrapWidth > 0) {
294
+ this._text = this.metrics.wordWrap(this._text, this.wordWrapWidth, getContext2d(renderer, this));
295
+ }
296
+
297
+ // calculcate the text size and update the bounds accordingly
298
+ bounds.addBounds(this.metrics.measureText(this._text, getContext2d(renderer, this)), true);
299
+
300
+ // update the offScreenCanvas texture if required
301
+ if (this.offScreenCanvas === true) {
302
+ var width = Math.ceil(this.metrics.width),
303
+ height = Math.ceil(this.metrics.height);
304
+
305
+ if (renderer instanceof WebGLRenderer) {
306
+ // invalidate the previous corresponding texture so that it can reuploaded once changed
307
+ this.glTextureUnit = renderer.cache.getUnit(renderer.cache.get(this.canvasTexture.canvas));
308
+ renderer.currentCompositor.unbindTexture2D(null, this.glTextureUnit);
309
+
310
+ if (renderer.WebGLVersion === 1) {
311
+ // round size to next Pow2
312
+ width = nextPowerOfTwo(this.metrics.width);
313
+ height = nextPowerOfTwo(this.metrics.height);
314
+ }
315
+ }
316
+
317
+ // resize the cache canvas if necessary
318
+ if (this.canvasTexture.width < width || this.canvasTexture.height < height) {
319
+ this.canvasTexture.resize(width, height);
320
+ }
321
+
322
+ this.canvasTexture.clear();
323
+ this._drawFont(this.canvasTexture.context, this._text, this.pos.x - this.metrics.x, this.pos.y - this.metrics.y, false);
324
+ }
325
+
326
+ this.isDirty = true;
327
+
328
+ return this;
329
+ }
330
+
331
+ /**
332
+ * measure the given text size in pixels
333
+ * @param {CanvasRenderer|WebGLRenderer} renderer - reference to the active renderer
334
+ * @param {string} [text] - the text to be measured
335
+ * @returns {TextMetrics} a TextMetrics object defining the dimensions of the given piece of text
336
+ */
337
+ measureText(renderer, text = this._text) {
338
+ return this.metrics.measureText(text, getContext2d(renderer, this));
339
+ }
340
+
341
+
342
+ /**
343
+ * draw a text at the specified coord
344
+ * @param {CanvasRenderer|WebGLRenderer} renderer - Reference to the destination renderer instance
345
+ * @param {string} [text]
346
+ * @param {number} [x]
347
+ * @param {number} [y]
348
+ * @param {boolean} [stroke=false] - draw stroke the the text if true
349
+ */
350
+ draw(renderer, text, x = this.pos.x, y = this.pos.y, stroke = false) {
351
+ // "hacky patch" for backward compatibilty
352
+ if (typeof this.ancestor === "undefined") {
353
+
354
+ // update position if changed
355
+ if (this.pos.x !== x || this.pos.y !== y) {
356
+ this.pos.x = x;
357
+ this.pos.y = y;
358
+ this.isDirty = true;
359
+ }
360
+
361
+ // update text cache
362
+ this.setText(text);
363
+
364
+ x = this.metrics.x;
365
+ y = this.metrics.y;
366
+
367
+ // save the previous context
368
+ renderer.save();
369
+
370
+ // apply the defined alpha value
371
+ renderer.setGlobalAlpha(renderer.globalAlpha() * this.getOpacity());
372
+
373
+ } else {
374
+ // added directly to an object container
375
+ x = this.pos.x;
376
+ y = this.pos.y;
377
+ }
378
+
379
+ // clamp to pixel grid if required
380
+ if (renderer.settings.subPixel === false) {
381
+ x = ~~x;
382
+ y = ~~y;
383
+ }
384
+
385
+ // draw the text
386
+ if (this.offScreenCanvas === true) {
387
+ renderer.drawImage(this.canvasTexture.canvas, x, y);
388
+ } else {
389
+ renderer.drawFont(this._drawFont(renderer.getFontContext(), this._text, x, y, stroke));
390
+ }
391
+
392
+
393
+ // for backward compatibilty
394
+ if (typeof this.ancestor === "undefined") {
395
+ // restore previous context
396
+ renderer.restore();
397
+ }
398
+ }
399
+
400
+ /**
401
+ * draw a stroke text at the specified coord, as defined <br>
402
+ * by the `lineWidth` and `fillStroke` properties. <br>
403
+ * Note : using drawStroke is not recommended for performance reasons
404
+ * @param {CanvasRenderer|WebGLRenderer} renderer - Reference to the destination renderer instance
405
+ * @param {string} text
406
+ * @param {number} x
407
+ * @param {number} y
408
+ */
409
+ drawStroke(renderer, text, x, y) {
410
+ this.draw(renderer, text, x, y, true);
411
+ }
412
+
413
+ /**
414
+ * @ignore
415
+ */
416
+ _drawFont(context, text, x, y, stroke = false) {
417
+ setContextStyle(context, this, stroke);
418
+
419
+ for (var i = 0; i < text.length; i++) {
420
+ var string = text[i].trimEnd();
421
+ // draw the string
422
+ context[stroke ? "strokeText" : "fillText"](string, x, y);
423
+ // add leading space
424
+ y += this.metrics.lineHeight();
425
+ }
426
+ return this.metrics;
427
+ }
428
+
429
+ /**
430
+ * Destroy function
431
+ * @ignore
432
+ */
433
+ destroy() {
434
+ if (this.offScreenCanvas === true) {
435
+ if (renderer instanceof WebGLRenderer) {
436
+ renderer.currentCompositor.deleteTexture2D(renderer.currentCompositor.getTexture2D(this.glTextureUnit));
437
+ this.glTextureUnit = undefined;
438
+ }
439
+ renderer.cache.delete(this.canvasTexture.canvas);
440
+ pool.push(this.canvasTexture);
441
+ this.canvasTexture = undefined;
442
+ }
443
+ pool.push(this.fillStyle);
444
+ pool.push(this.strokeStyle);
445
+ this.fillStyle = this.strokeStyle = undefined;
446
+ this.metrics = undefined;
447
+ this._text.length = 0;
448
+ super.destroy();
449
+ }
450
+ }
451
+
452
+ export { Text as default };
@@ -0,0 +1,175 @@
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 Bounds from '../physics/bounds.js';
9
+ import Text from './text.js';
10
+ import setContextStyle from './textstyle.js';
11
+
12
+ /**
13
+ * @classdesc
14
+ * a Text Metrics object that contains helper for text manipulation
15
+ * @augments Bounds
16
+ */
17
+ class TextMetrics extends Bounds {
18
+
19
+ /**
20
+ * @param {Text|BitmapText} ancestor - the parent object that contains this TextMetrics object
21
+ */
22
+ constructor(ancestor) {
23
+
24
+ // parent constructor
25
+ super();
26
+
27
+ /**
28
+ * a reference to the parent object that contains this TextMetrics object
29
+ * @public
30
+ * @type {Renderable}
31
+ * @default undefined
32
+ */
33
+ this.ancestor = ancestor;
34
+
35
+ this.setMinMax(0, 0, 0, 0);
36
+ }
37
+
38
+ /**
39
+ * Returns the height of a segment of inline text in CSS pixels.
40
+ * @returns {number} the height of a segment of inline text in CSS pixels.
41
+ */
42
+ lineHeight() {
43
+ if (this.ancestor instanceof Text) {
44
+ return this.ancestor.fontSize * this.ancestor.lineHeight;
45
+ } else { // it's a BitmapText
46
+ return this.ancestor.fontData.capHeight * this.ancestor.lineHeight * this.ancestor.fontScale.y;
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Returns the width of the given segment of inline text in CSS pixels.
52
+ * @param {string} text - the text to be measured
53
+ * @param {CanvasRenderingContext2D} [context] - reference to an active 2d context for canvas rendering
54
+ * @returns {number} the width of the given segment of inline text in CSS pixels.
55
+ */
56
+ lineWidth(text, context) {
57
+ if (this.ancestor instanceof Text) {
58
+ return context.measureText(text).width;
59
+ } else { // it's a BitmapText
60
+ var characters = text.split("");
61
+ var width = 0;
62
+ var lastGlyph = null;
63
+ for (var i = 0; i < characters.length; i++) {
64
+ var ch = characters[i].charCodeAt(0);
65
+ var glyph = this.ancestor.fontData.glyphs[ch];
66
+ if (typeof glyph !== "undefined") {
67
+ var kerning = (lastGlyph && lastGlyph.kerning) ? lastGlyph.getKerning(ch) : 0;
68
+ width += (glyph.xadvance + kerning) * this.ancestor.fontScale.x;
69
+ lastGlyph = glyph;
70
+ }
71
+ }
72
+ return width;
73
+ }
74
+ }
75
+
76
+ /**
77
+ * measure the given text size in CSS pixels
78
+ * @param {string} text - the text to be measured
79
+ * @param {CanvasRenderingContext2D} [context] - reference to an active 2d context for canvas rendering
80
+ * @returns {TextMetrics} this
81
+ */
82
+ measureText(text, context) {
83
+ var strings;
84
+
85
+ if (!Array.isArray(text)) {
86
+ strings = ("" + text).split("\n");
87
+ } else {
88
+ strings = text;
89
+ }
90
+
91
+ if (typeof context !== "undefined") {
92
+ // save the previous context
93
+ context.save();
94
+
95
+ // apply the style font
96
+ setContextStyle(context, this.ancestor);
97
+ }
98
+
99
+ // compute the bounding box size
100
+ this.width = this.height = 0;
101
+
102
+ for (var i = 0; i < strings.length; i++) {
103
+ this.width = Math.max(this.lineWidth(strings[i].trimEnd(), context), this.width);
104
+ this.height += this.lineHeight();
105
+ }
106
+ this.width = Math.ceil(this.width);
107
+ this.height = Math.ceil(this.height);
108
+
109
+ // compute the bounding box position
110
+ this.x = Math.floor((this.ancestor.textAlign === "right" ? this.ancestor.pos.x - this.width : (
111
+ this.ancestor.textAlign === "center" ? this.ancestor.pos.x - (this.width / 2) : this.ancestor.pos.x
112
+ )));
113
+ this.y = Math.floor((this.ancestor.textBaseline.search(/^(top|hanging)$/) === 0) ? this.ancestor.pos.y : (
114
+ this.ancestor.textBaseline === "middle" ? this.ancestor.pos.y - (this.lineHeight() / 2) : this.ancestor.pos.y - this.lineHeight()
115
+ ));
116
+
117
+ if (typeof context !== "undefined") {
118
+ // restore the context
119
+ context.restore();
120
+ }
121
+
122
+ return this;
123
+ }
124
+
125
+ /**
126
+ * wrap the given text based on the given width
127
+ * @param {string|string[]} text - the text to be wrapped
128
+ * @param {number} width - maximum width of one segment of text in css pixel
129
+ * @param {CanvasRenderingContext2D} [context] - reference to an active 2d context for canvas rendering
130
+ * @returns {string[]} an array of string representing wrapped text
131
+ */
132
+ wordWrap(text, width, context) {
133
+ var words;
134
+ var currentLine = "";
135
+ var output = [];
136
+
137
+ if (Array.isArray(text)) {
138
+ // join into a single string
139
+ text = text.join(" ");
140
+ }
141
+ // word splitting to be improved as it replaces \n by space if present
142
+ words = text.replace(/[\r\n]+/g, " ").split(" ");
143
+
144
+ if (typeof context !== "undefined") {
145
+ // save the previous context
146
+ context.save();
147
+
148
+ // apply the style font
149
+ setContextStyle(context, this.ancestor);
150
+ }
151
+
152
+ for (let i = 0; i < words.length; i++) {
153
+ var word = words[i];
154
+ var lineWidth = this.lineWidth(currentLine + word + " ", context);
155
+ if (lineWidth < width) {
156
+ // add the word to the current line
157
+ currentLine += word + " ";
158
+ } else {
159
+ output.push(currentLine + "\n");
160
+ currentLine = word + " ";
161
+ }
162
+ }
163
+ // last line
164
+ output.push(currentLine);
165
+
166
+ if (typeof context !== "undefined") {
167
+ // restore the context
168
+ context.restore();
169
+ }
170
+
171
+ return output;
172
+ }
173
+ }
174
+
175
+ export { TextMetrics as default };
@@ -0,0 +1,23 @@
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
+ /**
9
+ * apply the current text style to the given context
10
+ * @ignore
11
+ */
12
+ function setContextStyle(context, style, stroke = false) {
13
+ context.font = style.font;
14
+ context.fillStyle = style.fillStyle.toRGBA();
15
+ if (stroke === true) {
16
+ context.strokeStyle = style.strokeStyle.toRGBA();
17
+ context.lineWidth = style.lineWidth;
18
+ }
19
+ context.textAlign = style.textAlign;
20
+ context.textBaseline = style.textBaseline;
21
+ }
22
+
23
+ export { setContextStyle as default };