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.
- package/README.md +2 -0
- package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +10 -0
- package/dist/melonjs.mjs/_virtual/arraymultimap.js +10 -0
- package/dist/melonjs.mjs/_virtual/earcut.js +10 -0
- package/dist/melonjs.mjs/_virtual/howler.js +10 -0
- package/dist/melonjs.mjs/_virtual/index.js +10 -0
- package/dist/melonjs.mjs/_virtual/index2.js +10 -0
- package/dist/melonjs.mjs/_virtual/multimap.js +10 -0
- package/dist/melonjs.mjs/_virtual/setmultimap.js +10 -0
- package/dist/melonjs.mjs/application/application.js +240 -0
- package/dist/melonjs.mjs/audio/audio.js +536 -0
- package/dist/melonjs.mjs/camera/camera2d.js +732 -0
- package/dist/melonjs.mjs/entity/entity.js +248 -0
- package/dist/melonjs.mjs/game.js +29 -0
- package/dist/melonjs.mjs/geometries/ellipse.js +275 -0
- package/dist/melonjs.mjs/geometries/line.js +116 -0
- package/dist/melonjs.mjs/geometries/path2d.js +319 -0
- package/dist/melonjs.mjs/geometries/point.js +89 -0
- package/dist/melonjs.mjs/geometries/poly.js +500 -0
- package/dist/melonjs.mjs/geometries/rectangle.js +375 -0
- package/dist/melonjs.mjs/geometries/roundrect.js +168 -0
- package/dist/melonjs.mjs/index.js +248 -0
- package/dist/melonjs.mjs/input/gamepad.js +501 -0
- package/dist/melonjs.mjs/input/input.js +26 -0
- package/dist/melonjs.mjs/input/keyboard.js +470 -0
- package/dist/melonjs.mjs/input/pointer.js +393 -0
- package/dist/melonjs.mjs/input/pointerevent.js +818 -0
- package/dist/melonjs.mjs/lang/deprecated.js +157 -0
- package/dist/melonjs.mjs/level/level.js +297 -0
- package/dist/melonjs.mjs/level/tiled/TMXGroup.js +141 -0
- package/dist/melonjs.mjs/level/tiled/TMXLayer.js +448 -0
- package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
- package/dist/melonjs.mjs/level/tiled/TMXTile.js +194 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +639 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileset.js +311 -0
- package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +83 -0
- package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +506 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +220 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +157 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +125 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +109 -0
- package/dist/melonjs.mjs/loader/loader.js +801 -0
- package/dist/melonjs.mjs/loader/loadingscreen.js +120 -0
- package/dist/melonjs.mjs/loader/melonjs_logo.png.js +11 -0
- package/dist/melonjs.mjs/math/color.js +618 -0
- package/dist/melonjs.mjs/math/math.js +218 -0
- package/dist/melonjs.mjs/math/matrix2.js +503 -0
- package/dist/melonjs.mjs/math/matrix3.js +681 -0
- package/dist/melonjs.mjs/math/observable_vector2.js +471 -0
- package/dist/melonjs.mjs/math/observable_vector3.js +561 -0
- package/dist/melonjs.mjs/math/vector2.js +528 -0
- package/dist/melonjs.mjs/math/vector3.js +569 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +73 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +21 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +324 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +69 -0
- package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +691 -0
- package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +350 -0
- package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +3241 -0
- package/dist/melonjs.mjs/particles/emitter.js +267 -0
- package/dist/melonjs.mjs/particles/particle.js +188 -0
- package/dist/melonjs.mjs/particles/settings.js +319 -0
- package/dist/melonjs.mjs/physics/body.js +704 -0
- package/dist/melonjs.mjs/physics/bounds.js +460 -0
- package/dist/melonjs.mjs/physics/collision.js +132 -0
- package/dist/melonjs.mjs/physics/detector.js +194 -0
- package/dist/melonjs.mjs/physics/quadtree.js +391 -0
- package/dist/melonjs.mjs/physics/response.js +57 -0
- package/dist/melonjs.mjs/physics/sat.js +483 -0
- package/dist/melonjs.mjs/physics/world.js +221 -0
- package/dist/melonjs.mjs/plugin/plugin.js +141 -0
- package/dist/melonjs.mjs/renderable/collectable.js +62 -0
- package/dist/melonjs.mjs/renderable/colorlayer.js +80 -0
- package/dist/melonjs.mjs/renderable/container.js +1018 -0
- package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
- package/dist/melonjs.mjs/renderable/imagelayer.js +306 -0
- package/dist/melonjs.mjs/renderable/light2d.js +156 -0
- package/dist/melonjs.mjs/renderable/nineslicesprite.js +247 -0
- package/dist/melonjs.mjs/renderable/renderable.js +783 -0
- package/dist/melonjs.mjs/renderable/sprite.js +654 -0
- package/dist/melonjs.mjs/renderable/trigger.js +157 -0
- package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +213 -0
- package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +226 -0
- package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +128 -0
- package/dist/melonjs.mjs/state/stage.js +237 -0
- package/dist/melonjs.mjs/state/state.js +596 -0
- package/dist/melonjs.mjs/system/device.js +909 -0
- package/dist/melonjs.mjs/system/dom.js +78 -0
- package/dist/melonjs.mjs/system/event.js +537 -0
- package/dist/melonjs.mjs/system/platform.js +41 -0
- package/dist/melonjs.mjs/system/pooling.js +209 -0
- package/dist/melonjs.mjs/system/save.js +157 -0
- package/dist/melonjs.mjs/system/timer.js +286 -0
- package/dist/melonjs.mjs/text/bitmaptext.js +364 -0
- package/dist/melonjs.mjs/text/bitmaptextdata.js +199 -0
- package/dist/melonjs.mjs/text/glyph.js +66 -0
- package/dist/melonjs.mjs/text/text.js +453 -0
- package/dist/melonjs.mjs/text/textmetrics.js +176 -0
- package/dist/melonjs.mjs/text/textstyle.js +23 -0
- package/dist/melonjs.mjs/tweens/easing.js +336 -0
- package/dist/melonjs.mjs/tweens/interpolation.js +112 -0
- package/dist/melonjs.mjs/tweens/tween.js +480 -0
- package/dist/melonjs.mjs/utils/agent.js +76 -0
- package/dist/melonjs.mjs/utils/array.js +63 -0
- package/dist/melonjs.mjs/utils/file.js +42 -0
- package/dist/melonjs.mjs/utils/function.js +70 -0
- package/dist/melonjs.mjs/utils/string.js +82 -0
- package/dist/melonjs.mjs/utils/utils.js +173 -0
- package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +807 -0
- package/dist/melonjs.mjs/video/renderer.js +411 -0
- package/dist/melonjs.mjs/video/texture/atlas.js +519 -0
- package/dist/melonjs.mjs/video/texture/cache.js +143 -0
- package/dist/melonjs.mjs/video/texture/canvas_texture.js +144 -0
- package/dist/melonjs.mjs/video/video.js +462 -0
- package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +143 -0
- package/dist/melonjs.mjs/video/webgl/glshader.js +168 -0
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +10 -0
- package/dist/melonjs.mjs/video/webgl/utils/attributes.js +25 -0
- package/dist/melonjs.mjs/video/webgl/utils/precision.js +20 -0
- package/dist/melonjs.mjs/video/webgl/utils/program.js +67 -0
- package/dist/melonjs.mjs/video/webgl/utils/string.js +25 -0
- package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +92 -0
- package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +495 -0
- package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1036 -0
- package/dist/melonjs.module.d.ts +1163 -1163
- package/dist/melonjs.module.js +1903 -3274
- package/package.json +22 -17
- package/src/application/application.js +3 -3
- package/src/audio/audio.js +32 -32
- package/src/camera/camera2d.js +31 -31
- package/src/entity/entity.js +17 -17
- package/src/geometries/ellipse.js +16 -16
- package/src/geometries/line.js +5 -5
- package/src/geometries/path2d.js +32 -32
- package/src/geometries/poly.js +15 -15
- package/src/geometries/rectangle.js +18 -18
- package/src/geometries/roundrect.js +8 -8
- package/src/input/gamepad.js +15 -15
- package/src/input/keyboard.js +12 -12
- package/src/input/pointer.js +6 -6
- package/src/input/pointerevent.js +12 -12
- package/src/lang/deprecated.js +12 -12
- package/src/level/level.js +25 -25
- package/src/level/tiled/TMXLayer.js +22 -22
- package/src/level/tiled/TMXTile.js +5 -5
- package/src/level/tiled/TMXTileMap.js +6 -6
- package/src/level/tiled/TMXTileset.js +2 -2
- package/src/level/tiled/TMXUtils.js +5 -5
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +2 -2
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +2 -2
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXRenderer.js +19 -19
- package/src/loader/loader.js +20 -20
- package/src/math/color.js +20 -20
- package/src/math/math.js +16 -16
- package/src/math/matrix2.js +16 -16
- package/src/math/matrix3.js +25 -25
- package/src/math/observable_vector2.js +14 -14
- package/src/math/observable_vector3.js +16 -16
- package/src/math/vector2.js +9 -9
- package/src/math/vector3.js +10 -10
- package/src/particles/emitter.js +6 -6
- package/src/particles/particle.js +2 -2
- package/src/physics/body.js +28 -28
- package/src/physics/bounds.js +8 -8
- package/src/physics/collision.js +2 -2
- package/src/physics/detector.js +6 -6
- package/src/physics/quadtree.js +11 -11
- package/src/physics/sat.js +31 -31
- package/src/physics/world.js +5 -5
- package/src/plugin/plugin.js +5 -5
- package/src/renderable/collectable.js +3 -3
- package/src/renderable/colorlayer.js +5 -5
- package/src/renderable/container.js +21 -21
- package/src/renderable/dragndrop.js +14 -14
- package/src/renderable/imagelayer.js +13 -13
- package/src/renderable/light2d.js +3 -3
- package/src/renderable/nineslicesprite.js +16 -16
- package/src/renderable/renderable.js +23 -23
- package/src/renderable/sprite.js +28 -28
- package/src/renderable/trigger.js +15 -15
- package/src/renderable/ui/uibaseelement.js +7 -7
- package/src/renderable/ui/uispriteelement.js +6 -6
- package/src/renderable/ui/uitextbutton.js +13 -13
- package/src/state/stage.js +7 -7
- package/src/state/state.js +17 -17
- package/src/system/device.js +11 -11
- package/src/system/event.js +10 -10
- package/src/system/pooling.js +9 -9
- package/src/system/save.js +2 -2
- package/src/system/timer.js +10 -10
- package/src/text/bitmaptext.js +18 -18
- package/src/text/bitmaptextdata.js +2 -2
- package/src/text/text.js +23 -23
- package/src/text/textmetrics.js +8 -8
- package/src/tweens/tween.js +19 -19
- package/src/utils/agent.js +5 -5
- package/src/utils/array.js +4 -4
- package/src/utils/file.js +2 -2
- package/src/utils/function.js +6 -6
- package/src/utils/string.js +5 -5
- package/src/utils/utils.js +4 -4
- package/src/video/canvas/canvas_renderer.js +70 -70
- package/src/video/renderer.js +26 -26
- package/src/video/texture/atlas.js +22 -22
- package/src/video/texture/canvas_texture.js +9 -9
- package/src/video/video.js +17 -17
- package/src/video/webgl/glshader.js +10 -10
- package/src/video/webgl/webgl_compositor.js +41 -41
- package/src/video/webgl/webgl_renderer.js +75 -75
|
@@ -0,0 +1,453 @@
|
|
|
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 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
|
+
var Text$1 = Text;
|
|
452
|
+
|
|
453
|
+
export { Text$1 as default };
|
|
@@ -0,0 +1,176 @@
|
|
|
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 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
|
+
var TextMetrics$1 = TextMetrics;
|
|
175
|
+
|
|
176
|
+
export { TextMetrics$1 as default };
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
/**
|
|
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 };
|