forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmapText.js
More file actions
621 lines (537 loc) · 17.6 KB
/
Copy pathBitmapText.js
File metadata and controls
621 lines (537 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../../utils/Class');
var Components = require('../../components');
var GameObject = require('../../GameObject');
var GetBitmapTextSize = require('../GetBitmapTextSize');
var ParseFromAtlas = require('../ParseFromAtlas');
var Render = require('./BitmapTextRender');
/**
* @classdesc
* BitmapText objects work by taking a texture file and an XML or JSON file that describes the font structure.
*
* During rendering for each letter of the text is rendered to the display, proportionally spaced out and aligned to
* match the font structure.
*
* BitmapText objects are less flexible than Text objects, in that they have less features such as shadows, fills and the ability
* to use Web Fonts, however you trade this flexibility for rendering speed. You can also create visually compelling BitmapTexts by
* processing the font texture in an image editor, applying fills and any other effects required.
*
* To create multi-line text insert \r, \n or \r\n escape codes into the text string.
*
* To create a BitmapText data files you need a 3rd party app such as:
*
* BMFont (Windows, free): {@link http://www.angelcode.com/products/bmfont/|http://www.angelcode.com/products/bmfont/}
* Glyph Designer (OS X, commercial): {@link http://www.71squared.com/en/glyphdesigner|http://www.71squared.com/en/glyphdesigner}
* Littera (Web-based, free): {@link http://kvazars.com/littera/|http://kvazars.com/littera/}
*
* For most use cases it is recommended to use XML. If you wish to use JSON, the formatting should be equal to the result of
* converting a valid XML file through the popular X2JS library. An online tool for conversion can be found here: {@link http://codebeautify.org/xmltojson|http://codebeautify.org/xmltojson}
*
* @class BitmapText
* @extends Phaser.GameObjects.GameObject
* @memberof Phaser.GameObjects
* @constructor
* @since 3.0.0
*
* @extends Phaser.GameObjects.Components.Alpha
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.Mask
* @extends Phaser.GameObjects.Components.Origin
* @extends Phaser.GameObjects.Components.Pipeline
* @extends Phaser.GameObjects.Components.ScaleMode
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Texture
* @extends Phaser.GameObjects.Components.Tint
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.Visible
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. It can only belong to one Scene at any given time.
* @param {number} x - The x coordinate of this Game Object in world space.
* @param {number} y - The y coordinate of this Game Object in world space.
* @param {string} font - The key of the font to use from the Bitmap Font cache.
* @param {(string|string[])} [text] - The string, or array of strings, to be set as the content of this Bitmap Text.
* @param {number} [size] - The font size of this Bitmap Text.
* @param {integer} [align=0] - The alignment of the text in a multi-line BitmapText object.
*/
var BitmapText = new Class({
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.Depth,
Components.Mask,
Components.Origin,
Components.Pipeline,
Components.ScaleMode,
Components.ScrollFactor,
Components.Texture,
Components.Tint,
Components.Transform,
Components.Visible,
Render
],
initialize:
function BitmapText (scene, x, y, font, text, size, align)
{
if (text === undefined) { text = ''; }
if (align === undefined) { align = 0; }
GameObject.call(this, scene, 'BitmapText');
/**
* The key of the Bitmap Font used by this Bitmap Text.
* To change the font after creation please use `setFont`.
*
* @name Phaser.GameObjects.BitmapText#font
* @type {string}
* @readonly
* @since 3.0.0
*/
this.font = font;
var entry = this.scene.sys.cache.bitmapFont.get(font);
/**
* The data of the Bitmap Font used by this Bitmap Text.
*
* @name Phaser.GameObjects.BitmapText#fontData
* @type {Phaser.GameObjects.BitmapText.Types.BitmapFontData}
* @readonly
* @since 3.0.0
*/
this.fontData = entry.data;
/**
* The text that this Bitmap Text object displays.
*
* @name Phaser.GameObjects.BitmapText#_text
* @type {string}
* @private
* @since 3.0.0
*/
this._text = '';
/**
* The font size of this Bitmap Text.
*
* @name Phaser.GameObjects.BitmapText#_fontSize
* @type {number}
* @private
* @since 3.0.0
*/
this._fontSize = size || this.fontData.size;
/**
* Adds / Removes spacing between characters.
*
* Can be a negative or positive number.
*
* @name Phaser.GameObjects.BitmapText#_letterSpacing
* @type {number}
* @private
* @since 3.4.0
*/
this._letterSpacing = 0;
/**
* Controls the alignment of each line of text in this BitmapText object.
* Only has any effect when this BitmapText contains multiple lines of text, split with carriage-returns.
* Has no effect with single-lines of text.
*
* See the methods `setLeftAlign`, `setCenterAlign` and `setRightAlign`.
*
* 0 = Left aligned (default)
* 1 = Middle aligned
* 2 = Right aligned
*
* The alignment position is based on the longest line of text.
*
* @name Phaser.GameObjects.BitmapText#_align
* @type {integer}
* @private
* @since 3.11.0
*/
this._align = align;
/**
* An object that describes the size of this Bitmap Text.
*
* @name Phaser.GameObjects.BitmapText#_bounds
* @type {Phaser.GameObjects.BitmapText.Types.BitmapTextSize}
* @private
* @since 3.0.0
*/
this._bounds = GetBitmapTextSize(this, false, this._bounds);
/**
* An internal dirty flag for bounds calculation.
*
* @name Phaser.GameObjects.BitmapText#_dirty
* @type {boolean}
* @private
* @since 3.11.0
*/
this._dirty = false;
this.setTexture(entry.texture, entry.frame);
this.setPosition(x, y);
this.setOrigin(0, 0);
this.initPipeline();
this.setText(text);
},
/**
* Set the lines of text in this BitmapText to be left-aligned.
* This only has any effect if this BitmapText contains more than one line of text.
*
* @method Phaser.GameObjects.BitmapText#setLeftAlign
* @since 3.11.0
*
* @return {this} This BitmapText Object.
*/
setLeftAlign: function ()
{
this._align = BitmapText.ALIGN_LEFT;
this._dirty = true;
return this;
},
/**
* Set the lines of text in this BitmapText to be center-aligned.
* This only has any effect if this BitmapText contains more than one line of text.
*
* @method Phaser.GameObjects.BitmapText#setCenterAlign
* @since 3.11.0
*
* @return {this} This BitmapText Object.
*/
setCenterAlign: function ()
{
this._align = BitmapText.ALIGN_CENTER;
this._dirty = true;
return this;
},
/**
* Set the lines of text in this BitmapText to be right-aligned.
* This only has any effect if this BitmapText contains more than one line of text.
*
* @method Phaser.GameObjects.BitmapText#setRightAlign
* @since 3.11.0
*
* @return {this} This BitmapText Object.
*/
setRightAlign: function ()
{
this._align = BitmapText.ALIGN_RIGHT;
this._dirty = true;
return this;
},
/**
* Set the font size of this Bitmap Text.
*
* @method Phaser.GameObjects.BitmapText#setFontSize
* @since 3.0.0
*
* @param {number} size - The font size to set.
*
* @return {this} This BitmapText Object.
*/
setFontSize: function (size)
{
this._fontSize = size;
this._dirty = true;
return this;
},
/**
* Sets the letter spacing between each character of this Bitmap Text.
* Can be a positive value to increase the space, or negative to reduce it.
* Spacing is applied after the kerning values have been set.
*
* @method Phaser.GameObjects.BitmapText#setLetterSpacing
* @since 3.4.0
*
* @param {number} [spacing=0] - The amount of horizontal space to add between each character.
*
* @return {this} This BitmapText Object.
*/
setLetterSpacing: function (spacing)
{
if (spacing === undefined) { spacing = 0; }
this._letterSpacing = spacing;
this._dirty = true;
return this;
},
/**
* Set the textual content of this BitmapText.
*
* An array of strings will be converted into multi-line text. Use the align methods to change multi-line alignment.
*
* @method Phaser.GameObjects.BitmapText#setText
* @since 3.0.0
*
* @param {(string|string[])} value - The string, or array of strings, to be set as the content of this BitmapText.
*
* @return {this} This BitmapText Object.
*/
setText: function (value)
{
if (!value && value !== 0)
{
value = '';
}
if (Array.isArray(value))
{
value = value.join('\n');
}
if (value !== this.text)
{
this._text = value.toString();
this._dirty = true;
this.updateDisplayOrigin();
}
return this;
},
/**
* Calculate the bounds of this Bitmap Text.
*
* An object is returned that contains the position, width and height of the Bitmap Text in local and global
* contexts.
*
* Local size is based on just the font size and a [0, 0] position.
*
* Global size takes into account the Game Object's scale, world position and display origin.
*
* Also in the object is data regarding the length of each line, should this be a multi-line BitmapText.
*
* @method Phaser.GameObjects.BitmapText#getTextBounds
* @since 3.0.0
*
* @param {boolean} [round] - Whether to round the results to the nearest integer.
*
* @return {Phaser.GameObjects.BitmapText.Types.BitmapTextSize} An object that describes the size of this Bitmap Text.
*/
getTextBounds: function (round)
{
// local = The BitmapText based on fontSize and 0x0 coords
// global = The BitmapText, taking into account scale and world position
// lines = The BitmapText line data
if (this._dirty)
{
GetBitmapTextSize(this, round, this._bounds);
}
return this._bounds;
},
/**
* Changes the font this BitmapText is using to render.
*
* The new texture is loaded and applied to the BitmapText. The existing test, size and alignment are preserved,
* unless overridden via the arguments.
*
* @method Phaser.GameObjects.BitmapText#setFont
* @since 3.11.0
*
* @param {string} font - The key of the font to use from the Bitmap Font cache.
* @param {number} [size] - The font size of this Bitmap Text. If not specified the current size will be used.
* @param {integer} [align=0] - The alignment of the text in a multi-line BitmapText object. If not specified the current alignment will be used.
*
* @return {this} This BitmapText Object.
*/
setFont: function (key, size, align)
{
if (size === undefined) { size = this._fontSize; }
if (align === undefined) { align = this._align; }
if (key !== this.font)
{
var entry = this.scene.sys.cache.bitmapFont.get(key);
if (entry)
{
this.font = key;
this.fontData = entry.data;
this._fontSize = size;
this._align = align;
this.setTexture(entry.texture, entry.frame);
GetBitmapTextSize(this, false, this._bounds);
}
}
return this;
},
/**
* Controls the alignment of each line of text in this BitmapText object.
*
* Only has any effect when this BitmapText contains multiple lines of text, split with carriage-returns.
* Has no effect with single-lines of text.
*
* See the methods `setLeftAlign`, `setCenterAlign` and `setRightAlign`.
*
* 0 = Left aligned (default)
* 1 = Middle aligned
* 2 = Right aligned
*
* The alignment position is based on the longest line of text.
*
* @name Phaser.GameObjects.BitmapText#align
* @type {integer}
* @since 3.11.0
*/
align: {
set: function (value)
{
this._align = value;
this._dirty = true;
},
get: function ()
{
return this._align;
}
},
/**
* The text that this Bitmap Text object displays.
*
* You can also use the method `setText` if you want a chainable way to change the text content.
*
* @name Phaser.GameObjects.BitmapText#text
* @type {string}
* @since 3.0.0
*/
text: {
set: function (value)
{
this.setText(value);
},
get: function ()
{
return this._text;
}
},
/**
* The font size of this Bitmap Text.
*
* You can also use the method `setFontSize` if you want a chainable way to change the font size.
*
* @name Phaser.GameObjects.BitmapText#fontSize
* @type {number}
* @since 3.0.0
*/
fontSize: {
set: function (value)
{
this._fontSize = value;
this._dirty = true;
},
get: function ()
{
return this._fontSize;
}
},
/**
* Adds / Removes spacing between characters.
*
* Can be a negative or positive number.
*
* You can also use the method `setLetterSpacing` if you want a chainable way to change the letter spacing.
*
* @name Phaser.GameObjects.BitmapText#letterSpacing
* @type {number}
* @since 3.0.0
*/
letterSpacing: {
set: function (value)
{
this._letterSpacing = value;
this._dirty = true;
},
get: function ()
{
return this._letterSpacing;
}
},
/**
* The width of this Bitmap Text.
*
* @name Phaser.GameObjects.BitmapText#width
* @type {number}
* @readonly
* @since 3.0.0
*/
width: {
get: function ()
{
this.getTextBounds(false);
return this._bounds.global.width;
}
},
/**
* The height of this bitmap text.
*
* @name Phaser.GameObjects.BitmapText#height
* @type {number}
* @readonly
* @since 3.0.0
*/
height: {
get: function ()
{
this.getTextBounds(false);
return this._bounds.global.height;
}
},
/**
* Build a JSON representation of this Bitmap Text.
*
* @method Phaser.GameObjects.BitmapText#toJSON
* @since 3.0.0
*
* @return {Phaser.GameObjects.BitmapText.Types.JSONBitmapText} A JSON representation of this Bitmap Text.
*/
toJSON: function ()
{
var out = Components.ToJSON(this);
// Extra data is added here
var data = {
font: this.font,
text: this.text,
fontSize: this.fontSize,
letterSpacing: this.letterSpacing,
align: this.align
};
out.data = data;
return out;
}
});
/**
* Left align the text characters in a multi-line BitmapText object.
*
* @name Phaser.GameObjects.BitmapText.ALIGN_LEFT
* @type {integer}
* @since 3.11.0
*/
BitmapText.ALIGN_LEFT = 0;
/**
* Center align the text characters in a multi-line BitmapText object.
*
* @name Phaser.GameObjects.BitmapText.ALIGN_CENTER
* @type {integer}
* @since 3.11.0
*/
BitmapText.ALIGN_CENTER = 1;
/**
* Right align the text characters in a multi-line BitmapText object.
*
* @name Phaser.GameObjects.BitmapText.ALIGN_RIGHT
* @type {integer}
* @since 3.11.0
*/
BitmapText.ALIGN_RIGHT = 2;
/**
* Parse an XML Bitmap Font from an Atlas.
*
* Adds the parsed Bitmap Font data to the cache with the `fontName` key.
*
* @name Phaser.GameObjects.BitmapText.ParseFromAtlas
* @type {function}
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene to parse the Bitmap Font for.
* @param {string} fontName - The key of the font to add to the Bitmap Font cache.
* @param {string} textureKey - The key of the BitmapFont's texture.
* @param {string} frameKey - The key of the BitmapFont texture's frame.
* @param {string} xmlKey - The key of the XML data of the font to parse.
* @param {integer} [xSpacing] - The x-axis spacing to add between each letter.
* @param {integer} [ySpacing] - The y-axis spacing to add to the line height.
*
* @return {boolean} Whether the parsing was successful or not.
*/
BitmapText.ParseFromAtlas = ParseFromAtlas;
module.exports = BitmapText;