Skip to content

Commit 22e8d22

Browse files
committed
BitmapText.setWordTint is a new method that allows you to set a tint color (either additive, or fill) on all matching words within a static Bitmap Text. You can specify the word by string, or numeric offset, and the number of replacements to tint.
1 parent 21e1fe2 commit 22e8d22

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

src/gameobjects/bitmaptext/static/BitmapText.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ var BitmapText = new Class({
475475
*
476476
* To modify the tint color once set, call this method again with new color values.
477477
*
478+
* Using `setWordTint` can override tints set by this function, and vice versa.
479+
*
478480
* To remove a tint call this method with just the `start`, and optionally, the `length` parameters defined.
479481
*
480482
* @method Phaser.GameObjects.BitmapText#setCharacterTint
@@ -563,6 +565,79 @@ var BitmapText = new Class({
563565
return this;
564566
},
565567

568+
/**
569+
* Sets a tint on a matching word within this Bitmap Text.
570+
*
571+
* The `word` parameter can be either a string or a number.
572+
*
573+
* If a string, it will run a string comparison against the text contents, and if matching,
574+
* it will tint the whole word.
575+
*
576+
* If a number, if till that word, based on its offset within the text contents.
577+
*
578+
* The `count` parameter controls how many words are replaced. Pass in -1 to replace them all.
579+
*
580+
* This parameter is ignored if you pass a number as the `word` to be searched for.
581+
*
582+
* This is a WebGL only feature and only works with Static Bitmap Text, not Dynamic.
583+
*
584+
* The tint works by taking the pixel color values from the Bitmap Text texture, and then
585+
* multiplying it by the color value of the tint. You can provide either one color value,
586+
* in which case the whole character will be tinted in that color. Or you can provide a color
587+
* per corner. The colors are blended together across the extent of the character range.
588+
*
589+
* To swap this from being an additive tint to a fill based tint, set the `tintFill` parameter to `true`.
590+
*
591+
* To modify the tint color once set, call this method again with new color values.
592+
*
593+
* Using `setCharacterTint` can override tints set by this function, and vice versa.
594+
*
595+
* @method Phaser.GameObjects.BitmapText#setWordTint
596+
* @webglOnly
597+
* @since 3.50.0
598+
*
599+
* @param {(string|number)} word - The word to search for. Either a string, or an index of the word in the words array.
600+
* @param {number} [count=1] - The number of matching words to tint. Pass -1 to tint all matching words.
601+
* @param {boolean} [tintFill=false] - Use a fill-based tint (true), or an additive tint (false)
602+
* @param {integer} [topLeft=0xffffff] - The tint being applied to the top-left of the word. If not other values are given this value is applied evenly, tinting the whole word.
603+
* @param {integer} [topRight] - The tint being applied to the top-right of the word.
604+
* @param {integer} [bottomLeft] - The tint being applied to the bottom-left of the word.
605+
* @param {integer} [bottomRight] - The tint being applied to the bottom-right of the word.
606+
*
607+
* @return {this} This BitmapText Object.
608+
*/
609+
setWordTint: function (word, count, tintFill, topLeft, topRight, bottomLeft, bottomRight)
610+
{
611+
if (count === undefined) { count = 1; }
612+
613+
var bounds = this.getTextBounds();
614+
615+
var words = bounds.words;
616+
617+
var wordIsNumber = (typeof(word) === 'number');
618+
619+
var total = 0;
620+
621+
for (var i = 0; i < words.length; i++)
622+
{
623+
var lineword = words[i];
624+
625+
if ((wordIsNumber && i === word) || (!wordIsNumber && lineword.word === word))
626+
{
627+
this.setCharacterTint(lineword.i, lineword.word.length, tintFill, topLeft, topRight, bottomLeft, bottomRight);
628+
629+
total++;
630+
631+
if (total === count)
632+
{
633+
return this;
634+
}
635+
}
636+
}
637+
638+
return this;
639+
},
640+
566641
/**
567642
* Calculate the bounds of this Bitmap Text.
568643
*

0 commit comments

Comments
 (0)