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.

This commit is contained in:
Richard Davey 2020-07-30 15:14:25 +01:00
parent 57fc54a42b
commit 286be7df49
2 changed files with 28 additions and 9 deletions

View file

@ -5,10 +5,10 @@
*/
/**
* Calculate the position, width and height of a BitmapText Game Object.
* Calculate the full bounds, in local and world space, of a BitmapText Game Object.
*
* Returns a BitmapTextSize object that contains global and local variants of the Game Objects x and y coordinates and
* its width and height.
* its width and height. Also includes an array of the line lengths and all word positions.
*
* The global position and size take into account the Game Object's position and scale.
*
@ -18,13 +18,14 @@
* @since 3.0.0
* @private
*
* @param {(Phaser.GameObjects.DynamicBitmapText|Phaser.GameObjects.BitmapText)} src - The BitmapText to calculate the position, width and height of.
* @param {(Phaser.GameObjects.DynamicBitmapText|Phaser.GameObjects.BitmapText)} src - The BitmapText to calculate the position, width, height and bounds values for.
* @param {boolean} [round] - Whether to round the results to the nearest integer.
* @param {boolean} [includeChars] - Should the results include the locations of each character in the `characters` array?
* @param {object} [out] - Optional object to store the results in, to save constant object creation.
*
* @return {Phaser.Types.GameObjects.BitmapText.BitmapTextSize} The calculated position, width and height of the BitmapText.
* @return {Phaser.Types.GameObjects.BitmapText.BitmapTextSize} The calculated bounds values of BitmapText.
*/
var GetBitmapTextSize = function (src, round, out)
var GetBitmapTextSize = function (src, round, includeChars, out)
{
if (out === undefined)
{
@ -49,6 +50,7 @@ var GetBitmapTextSize = function (src, round, out)
},
wrappedText: '',
words: [],
characters: [],
scaleX: 0,
scaleY: 0
};
@ -94,6 +96,7 @@ var GetBitmapTextSize = function (src, round, out)
var i;
var words = [];
var characters = [];
var current = null;
// Scan for breach of maxWidth and insert carriage-returns
@ -343,6 +346,8 @@ var GetBitmapTextSize = function (src, round, out)
bh = gh;
}
var charWidth = glyph.xOffset + glyph.xAdvance + ((kerningOffset !== undefined) ? kerningOffset : 0);
if (charCode === wordWrapCharCode)
{
if (current !== null)
@ -368,7 +373,19 @@ var GetBitmapTextSize = function (src, round, out)
}
current.word = current.word.concat(text[i]);
current.w += glyph.xOffset + glyph.xAdvance + ((kerningOffset !== undefined) ? kerningOffset : 0);
current.w += charWidth;
}
if (includeChars)
{
characters.push({
char: text[i],
code: charCode,
x: xAdvance * sx,
y: yAdvance * sy,
w: charWidth * sx,
h: lineHeight * sy
});
}
xAdvance += glyph.xAdvance + letterSpacing;
@ -439,6 +456,7 @@ var GetBitmapTextSize = function (src, round, out)
// console.log(round, local);
out.words = words;
out.characters = characters;
out.lines.height = lineHeight;
out.scaleX = src.scaleX;
out.scaleY = src.scaleY;

View file

@ -359,10 +359,11 @@ var BitmapText = new Class({
* @since 3.0.0
*
* @param {boolean} [round=false] - Whether to round the results up to the nearest integer.
* @param {boolean} [includeChars=false] - Should the results include the locations of each character in the `characters` array?
*
* @return {Phaser.Types.GameObjects.BitmapText.BitmapTextSize} An object that describes the size of this Bitmap Text.
*/
getTextBounds: function (round)
getTextBounds: function (round, includeChars)
{
// local = The BitmapText based on fontSize and 0x0 coords
// global = The BitmapText, taking into account scale and world position
@ -370,11 +371,11 @@ var BitmapText = new Class({
var bounds = this._bounds;
if (this._dirty || this.scaleX !== bounds.scaleX || this.scaleY !== bounds.scaleY)
if (this._dirty || round || this.scaleX !== bounds.scaleX || this.scaleY !== bounds.scaleY)
{
this._dirty = false;
GetBitmapTextSize(this, round, bounds);
GetBitmapTextSize(this, round, includeChars, bounds);
this.updateDisplayOrigin();
}