Skip to content

Commit 9419606

Browse files
committed
BitmapText.setCharacterTint is a new method that allows you to set a tint color (either additive, or fill) on a specific range of characters within a static Bitmap Text. You can specify the start and length offsets and a per-corner tint color.
1 parent fc6e7ef commit 9419606

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

src/gameobjects/bitmaptext/static/BitmapText.js

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

77
var Class = require('../../../utils/Class');
8+
var Clamp = require('../../../math/Clamp');
89
var Components = require('../../components');
910
var GameObject = require('../../GameObject');
1011
var GetBitmapTextSize = require('../GetBitmapTextSize');
@@ -13,6 +14,16 @@ var ParseXMLBitmapFont = require('../ParseXMLBitmapFont');
1314
var Rectangle = require('../../../geom/rectangle/Rectangle');
1415
var Render = require('./BitmapTextRender');
1516

17+
/**
18+
* @function GetColor
19+
* @since 3.0.0
20+
* @private
21+
*/
22+
var GetColor = function (value)
23+
{
24+
return (value >> 16) + (value & 0xff00) + ((value & 0xff) << 16);
25+
};
26+
1627
/**
1728
* @classdesc
1829
* BitmapText objects work by taking a texture file and an XML or JSON file that describes the font structure.
@@ -204,6 +215,8 @@ var BitmapText = new Class({
204215
*/
205216
this.wordWrapCharCode = 32;
206217

218+
this.charColors = [];
219+
207220
this.setTexture(entry.texture, entry.frame);
208221
this.setPosition(x, y);
209222
this.setOrigin(0, 0);
@@ -344,6 +357,117 @@ var BitmapText = new Class({
344357
return this;
345358
},
346359

360+
/**
361+
* Sets a tint on a range of characters in this Bitmap Text, starting from the `start` parameter index
362+
* and running for `length` quantity of characters.
363+
*
364+
* The `start` parameter can be negative. In this case, it starts at the end of the text and counts
365+
* backwards `start` places.
366+
*
367+
* You can also pass in -1 as the `length` and it will tint all characters from `start`
368+
* up until the end of the string.
369+
370+
* Remember that spaces and punctuation count as characters.
371+
*
372+
* This is a WebGL only feature.
373+
*
374+
* The tint works by taking the pixel color values from the Bitmap Text texture, and then
375+
* multiplying it by the color value of the tint. You can provide either one color value,
376+
* in which case the whole character will be tinted in that color. Or you can provide a color
377+
* per corner. The colors are blended together across the extent of the character range.
378+
*
379+
* To swap this from being an additive tint to a fill based tint, set the `tintFill` parameter to `true`.
380+
*
381+
* To modify the tint color once set, call this method again with new color values.
382+
*
383+
* To remove a tint call this method with just the `start`, and optionally, the `length` parameters defined.
384+
*
385+
* @method Phaser.GameObjects.BitmapText#setCharacterTint
386+
* @webglOnly
387+
* @since 3.50.0
388+
*
389+
* @param {number} [start=0] - The starting character to begin the tint at. If negative, it counts back from the end of the text.
390+
* @param {number} [length=1] - The number of characters to tint. Remember that spaces count as a character too. Pass -1 to tint all characters from `start` onwards.
391+
* @param {boolean} [tintFill=false] - Use a fill-based tint (true), or an additive tint (false)
392+
* @param {integer} [topLeft=0xffffff] - The tint being applied to the top-left of the character. If not other values are given this value is applied evenly, tinting the whole character.
393+
* @param {integer} [topRight] - The tint being applied to the top-right of the character.
394+
* @param {integer} [bottomLeft] - The tint being applied to the bottom-left of the character.
395+
* @param {integer} [bottomRight] - The tint being applied to the bottom-right of the character.
396+
*
397+
* @return {this} This BitmapText Object.
398+
*/
399+
setCharacterTint: function (start, length, tintFill, topLeft, topRight, bottomLeft, bottomRight)
400+
{
401+
if (start === undefined) { start = 0; }
402+
if (length === undefined) { length = 1; }
403+
if (tintFill === undefined) { tintFill = false; }
404+
if (topLeft === undefined) { topLeft = -1; }
405+
406+
if (topRight === undefined)
407+
{
408+
topRight = topLeft;
409+
bottomLeft = topLeft;
410+
bottomRight = topLeft;
411+
}
412+
413+
var len = this.text.length;
414+
415+
if (length === -1)
416+
{
417+
length = len;
418+
}
419+
420+
if (start < 0)
421+
{
422+
start = len + start;
423+
}
424+
425+
start = Clamp(start, 0, len - 1);
426+
427+
var end = Clamp(start + length, start, len);
428+
429+
var charColors = this.charColors;
430+
431+
for (var i = start; i < end; i++)
432+
{
433+
var color = charColors[i];
434+
435+
if (topLeft === -1)
436+
{
437+
charColors[i] = null;
438+
}
439+
else
440+
{
441+
var tintEffect = (tintFill) ? 1 : 0;
442+
var tintTL = GetColor(topLeft);
443+
var tintTR = GetColor(topRight);
444+
var tintBL = GetColor(bottomLeft);
445+
var tintBR = GetColor(bottomRight);
446+
447+
if (color)
448+
{
449+
color.tintEffect = tintEffect;
450+
color.tintTL = tintTL;
451+
color.tintTR = tintTR;
452+
color.tintBL = tintBL;
453+
color.tintBR = tintBR;
454+
}
455+
else
456+
{
457+
charColors[i] = {
458+
tintEffect: tintEffect,
459+
tintTL: tintTL,
460+
tintTR: tintTR,
461+
tintBL: tintBL,
462+
tintBR: tintBR
463+
};
464+
}
465+
}
466+
}
467+
468+
return this;
469+
},
470+
347471
/**
348472
* Calculate the bounds of this Bitmap Text.
349473
*

0 commit comments

Comments
 (0)