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.
- 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 +238 -0
- package/dist/melonjs.mjs/audio/audio.js +536 -0
- package/dist/melonjs.mjs/camera/camera2d.js +731 -0
- package/dist/melonjs.mjs/entity/entity.js +247 -0
- package/dist/melonjs.mjs/game.js +29 -0
- package/dist/melonjs.mjs/geometries/ellipse.js +274 -0
- package/dist/melonjs.mjs/geometries/line.js +115 -0
- package/dist/melonjs.mjs/geometries/path2d.js +318 -0
- package/dist/melonjs.mjs/geometries/point.js +88 -0
- package/dist/melonjs.mjs/geometries/poly.js +498 -0
- package/dist/melonjs.mjs/geometries/rectangle.js +374 -0
- package/dist/melonjs.mjs/geometries/roundrect.js +167 -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 +446 -0
- package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
- package/dist/melonjs.mjs/level/tiled/TMXTile.js +193 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +636 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileset.js +309 -0
- package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +81 -0
- package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +504 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +218 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +155 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +124 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +107 -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 +616 -0
- package/dist/melonjs.mjs/math/math.js +218 -0
- package/dist/melonjs.mjs/math/matrix2.js +501 -0
- package/dist/melonjs.mjs/math/matrix3.js +679 -0
- package/dist/melonjs.mjs/math/observable_vector2.js +469 -0
- package/dist/melonjs.mjs/math/observable_vector3.js +559 -0
- package/dist/melonjs.mjs/math/vector2.js +526 -0
- package/dist/melonjs.mjs/math/vector3.js +567 -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 +265 -0
- package/dist/melonjs.mjs/particles/particle.js +186 -0
- package/dist/melonjs.mjs/particles/settings.js +319 -0
- package/dist/melonjs.mjs/physics/body.js +702 -0
- package/dist/melonjs.mjs/physics/bounds.js +459 -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 +394 -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 +219 -0
- package/dist/melonjs.mjs/plugin/plugin.js +141 -0
- package/dist/melonjs.mjs/renderable/collectable.js +60 -0
- package/dist/melonjs.mjs/renderable/colorlayer.js +78 -0
- package/dist/melonjs.mjs/renderable/container.js +1016 -0
- package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
- package/dist/melonjs.mjs/renderable/imagelayer.js +305 -0
- package/dist/melonjs.mjs/renderable/light2d.js +155 -0
- package/dist/melonjs.mjs/renderable/nineslicesprite.js +246 -0
- package/dist/melonjs.mjs/renderable/renderable.js +781 -0
- package/dist/melonjs.mjs/renderable/sprite.js +653 -0
- package/dist/melonjs.mjs/renderable/trigger.js +156 -0
- package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +212 -0
- package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +225 -0
- package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +127 -0
- package/dist/melonjs.mjs/state/stage.js +236 -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 +363 -0
- package/dist/melonjs.mjs/text/bitmaptextdata.js +198 -0
- package/dist/melonjs.mjs/text/glyph.js +65 -0
- package/dist/melonjs.mjs/text/text.js +452 -0
- package/dist/melonjs.mjs/text/textmetrics.js +175 -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 +479 -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 +806 -0
- package/dist/melonjs.mjs/video/renderer.js +410 -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 +142 -0
- package/dist/melonjs.mjs/video/webgl/glshader.js +167 -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 +494 -0
- package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1035 -0
- package/dist/melonjs.module.d.ts +1298 -1359
- package/dist/melonjs.module.js +2072 -3520
- package/package.json +21 -16
- package/src/application/application.js +4 -5
- package/src/audio/audio.js +32 -32
- package/src/camera/camera2d.js +32 -33
- package/src/entity/entity.js +18 -19
- package/src/geometries/ellipse.js +17 -18
- package/src/geometries/line.js +6 -7
- package/src/geometries/path2d.js +33 -34
- package/src/geometries/point.js +1 -2
- package/src/geometries/poly.js +16 -18
- package/src/geometries/rectangle.js +19 -20
- package/src/geometries/roundrect.js +9 -10
- 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 +23 -24
- package/src/level/tiled/TMXTile.js +6 -7
- package/src/level/tiled/TMXTileMap.js +8 -10
- package/src/level/tiled/TMXTileset.js +3 -4
- package/src/level/tiled/TMXTilesetGroup.js +1 -2
- package/src/level/tiled/TMXUtils.js +5 -5
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +3 -4
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +3 -4
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +2 -3
- package/src/level/tiled/renderer/TMXRenderer.js +20 -21
- package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -2
- package/src/loader/loader.js +20 -20
- package/src/math/color.js +21 -22
- package/src/math/math.js +16 -16
- package/src/math/matrix2.js +17 -18
- package/src/math/matrix3.js +26 -27
- package/src/math/observable_vector2.js +15 -16
- package/src/math/observable_vector3.js +17 -18
- package/src/math/vector2.js +10 -11
- package/src/math/vector3.js +11 -12
- package/src/particles/emitter.js +7 -8
- package/src/particles/particle.js +3 -4
- package/src/physics/body.js +29 -30
- package/src/physics/bounds.js +10 -10
- package/src/physics/collision.js +2 -2
- package/src/physics/detector.js +6 -6
- package/src/physics/quadtree.js +18 -23
- package/src/physics/sat.js +31 -31
- package/src/physics/world.js +6 -7
- package/src/plugin/plugin.js +5 -5
- package/src/renderable/collectable.js +4 -6
- package/src/renderable/colorlayer.js +6 -8
- package/src/renderable/container.js +25 -27
- package/src/renderable/dragndrop.js +14 -14
- package/src/renderable/imagelayer.js +14 -15
- package/src/renderable/light2d.js +4 -5
- package/src/renderable/nineslicesprite.js +17 -18
- package/src/renderable/renderable.js +26 -28
- package/src/renderable/sprite.js +29 -30
- package/src/renderable/trigger.js +16 -17
- package/src/renderable/ui/uibaseelement.js +8 -9
- package/src/renderable/ui/uispriteelement.js +8 -8
- package/src/renderable/ui/uitextbutton.js +15 -15
- package/src/state/stage.js +8 -9
- 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 +19 -20
- package/src/text/bitmaptextdata.js +3 -4
- package/src/text/glyph.js +1 -2
- package/src/text/text.js +24 -25
- package/src/text/textmetrics.js +9 -10
- package/src/tweens/tween.js +20 -21
- 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 +72 -73
- package/src/video/renderer.js +27 -28
- 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/buffer/vertex.js +1 -2
- package/src/video/webgl/glshader.js +12 -12
- package/src/video/webgl/webgl_compositor.js +42 -43
- package/src/video/webgl/webgl_renderer.js +76 -77
|
@@ -0,0 +1,363 @@
|
|
|
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 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
|
+
|
|
363
|
+
export { BitmapText as default };
|
|
@@ -0,0 +1,198 @@
|
|
|
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 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
|
+
|
|
198
|
+
export { BitmapTextData as default };
|
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
// 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
|
+
|
|
65
|
+
export { Glyph as default };
|