Skip to content

Commit 286be7d

Browse files
committed
The GetBitmapTextSize and BitmapText.getTextBounds functions have a new boolean parameter includeChars. When set to true it will include a characters array in the returned bounds object that contains the scaled position coordinates of each character in the BitmapText, which you could use for tasks such as determining which character of the object was clicked.
1 parent 57fc54a commit 286be7d

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

src/gameobjects/bitmaptext/GetBitmapTextSize.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66

77
/**
8-
* Calculate the position, width and height of a BitmapText Game Object.
8+
* Calculate the full bounds, in local and world space, of a BitmapText Game Object.
99
*
1010
* Returns a BitmapTextSize object that contains global and local variants of the Game Objects x and y coordinates and
11-
* its width and height.
11+
* its width and height. Also includes an array of the line lengths and all word positions.
1212
*
1313
* The global position and size take into account the Game Object's position and scale.
1414
*
@@ -18,13 +18,14 @@
1818
* @since 3.0.0
1919
* @private
2020
*
21-
* @param {(Phaser.GameObjects.DynamicBitmapText|Phaser.GameObjects.BitmapText)} src - The BitmapText to calculate the position, width and height of.
21+
* @param {(Phaser.GameObjects.DynamicBitmapText|Phaser.GameObjects.BitmapText)} src - The BitmapText to calculate the position, width, height and bounds values for.
2222
* @param {boolean} [round] - Whether to round the results to the nearest integer.
23+
* @param {boolean} [includeChars] - Should the results include the locations of each character in the `characters` array?
2324
* @param {object} [out] - Optional object to store the results in, to save constant object creation.
2425
*
25-
* @return {Phaser.Types.GameObjects.BitmapText.BitmapTextSize} The calculated position, width and height of the BitmapText.
26+
* @return {Phaser.Types.GameObjects.BitmapText.BitmapTextSize} The calculated bounds values of BitmapText.
2627
*/
27-
var GetBitmapTextSize = function (src, round, out)
28+
var GetBitmapTextSize = function (src, round, includeChars, out)
2829
{
2930
if (out === undefined)
3031
{
@@ -49,6 +50,7 @@ var GetBitmapTextSize = function (src, round, out)
4950
},
5051
wrappedText: '',
5152
words: [],
53+
characters: [],
5254
scaleX: 0,
5355
scaleY: 0
5456
};
@@ -94,6 +96,7 @@ var GetBitmapTextSize = function (src, round, out)
9496

9597
var i;
9698
var words = [];
99+
var characters = [];
97100
var current = null;
98101

99102
// Scan for breach of maxWidth and insert carriage-returns
@@ -343,6 +346,8 @@ var GetBitmapTextSize = function (src, round, out)
343346
bh = gh;
344347
}
345348

349+
var charWidth = glyph.xOffset + glyph.xAdvance + ((kerningOffset !== undefined) ? kerningOffset : 0);
350+
346351
if (charCode === wordWrapCharCode)
347352
{
348353
if (current !== null)
@@ -368,7 +373,19 @@ var GetBitmapTextSize = function (src, round, out)
368373
}
369374

370375
current.word = current.word.concat(text[i]);
371-
current.w += glyph.xOffset + glyph.xAdvance + ((kerningOffset !== undefined) ? kerningOffset : 0);
376+
current.w += charWidth;
377+
}
378+
379+
if (includeChars)
380+
{
381+
characters.push({
382+
char: text[i],
383+
code: charCode,
384+
x: xAdvance * sx,
385+
y: yAdvance * sy,
386+
w: charWidth * sx,
387+
h: lineHeight * sy
388+
});
372389
}
373390

374391
xAdvance += glyph.xAdvance + letterSpacing;
@@ -439,6 +456,7 @@ var GetBitmapTextSize = function (src, round, out)
439456
// console.log(round, local);
440457

441458
out.words = words;
459+
out.characters = characters;
442460
out.lines.height = lineHeight;
443461
out.scaleX = src.scaleX;
444462
out.scaleY = src.scaleY;

src/gameobjects/bitmaptext/static/BitmapText.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,22 +359,23 @@ var BitmapText = new Class({
359359
* @since 3.0.0
360360
*
361361
* @param {boolean} [round=false] - Whether to round the results up to the nearest integer.
362+
* @param {boolean} [includeChars=false] - Should the results include the locations of each character in the `characters` array?
362363
*
363364
* @return {Phaser.Types.GameObjects.BitmapText.BitmapTextSize} An object that describes the size of this Bitmap Text.
364365
*/
365-
getTextBounds: function (round)
366+
getTextBounds: function (round, includeChars)
366367
{
367368
// local = The BitmapText based on fontSize and 0x0 coords
368369
// global = The BitmapText, taking into account scale and world position
369370
// lines = The BitmapText line data
370371

371372
var bounds = this._bounds;
372373

373-
if (this._dirty || this.scaleX !== bounds.scaleX || this.scaleY !== bounds.scaleY)
374+
if (this._dirty || round || this.scaleX !== bounds.scaleX || this.scaleY !== bounds.scaleY)
374375
{
375376
this._dirty = false;
376377

377-
GetBitmapTextSize(this, round, bounds);
378+
GetBitmapTextSize(this, round, includeChars, bounds);
378379

379380
this.updateDisplayOrigin();
380381
}

0 commit comments

Comments
 (0)