Skip to content

Commit 55048a4

Browse files
committed
BitmapText objects now have an anchor property. This works in a similar way to Sprite.anchor except that it offsets the position of each letter of the BitmapText by the given amount, based on the overall BitmapText width - whereas Sprite.anchor offsets the position the texture is drawn at.
1 parent 506ff88 commit 55048a4

2 files changed

Lines changed: 101 additions & 36 deletions

File tree

src/gameobjects/BitmapText.js

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

77
/**
8-
* BitmapText objects work by taking a texture file and an XML file that describes the font layout.
8+
* BitmapText objects work by taking a texture file and an XML file that describes the font structure.
9+
* It then generates a new Sprite object for each letter of the text, proportionally spaced out and aligned to
10+
* match the font structure.
11+
*
12+
* BitmapText objects are less flexible than Text objects, in that they have less features such as shadows, fills and the ability
13+
* to use Web Fonts, however you trade this flexibility for rendering speed. You can also create visually compelling BitmapTexts by
14+
* processing the font texture in an image editor, applying fills and any other effects required.
915
*
10-
* On Windows you can use the free app BMFont: http://www.angelcode.com/products/bmfont/
11-
* On OS X we recommend Glyph Designer: http://www.71squared.com/en/glyphdesigner
12-
* For Web there is the great Littera: http://kvazars.com/littera/
16+
* To create a BitmapText you can use:
17+
*
18+
* BMFont (Windows, free): http://www.angelcode.com/products/bmfont/
19+
* Glyph Designer (OS X, commercial): http://www.71squared.com/en/glyphdesigner
20+
* Littera (Web-based, free): http://kvazars.com/littera/
1321
*
1422
* @class Phaser.BitmapText
1523
* @constructor
@@ -46,6 +54,12 @@ Phaser.BitmapText = function (game, x, y, font, text, size) {
4654
*/
4755
this.type = Phaser.BITMAPTEXT;
4856

57+
/**
58+
* @property {number} physicsType - The const physics body type of this object.
59+
* @readonly
60+
*/
61+
this.physicsType = Phaser.SPRITE;
62+
4963
/**
5064
* @property {string} _text - Internal cache var.
5165
* @private
@@ -76,6 +90,18 @@ Phaser.BitmapText = function (game, x, y, font, text, size) {
7690
*/
7791
this._tint = 0xFFFFFF;
7892

93+
/**
94+
* @property {number} _tw - Internal cache var. Holds the previous textWidth.
95+
* @private
96+
*/
97+
this._tw = 0;
98+
99+
/**
100+
* @property {number} _th - Internal cache var. Holds the previous textHeight.
101+
* @private
102+
*/
103+
this._th = 0;
104+
79105
PIXI.BitmapText.call(this, text);
80106

81107
Phaser.Component.Core.init.call(this, game, x, y, '', null);
@@ -85,7 +111,7 @@ Phaser.BitmapText = function (game, x, y, font, text, size) {
85111
Phaser.BitmapText.prototype = Object.create(PIXI.BitmapText.prototype);
86112
Phaser.BitmapText.prototype.constructor = Phaser.BitmapText;
87113

88-
var components = [
114+
Phaser.Component.Core.install.call(Phaser.BitmapText.prototype, [
89115
'Angle',
90116
'AutoCull',
91117
'Bounds',
@@ -96,33 +122,28 @@ var components = [
96122
'LifeSpan',
97123
'PhysicsBody',
98124
'Reset'
99-
];
125+
]);
100126

101-
Phaser.Component.Core.install.call(Phaser.BitmapText.prototype, components);
102-
103-
/**
104-
* @method Phaser.BitmapText.prototype.setStyle
105-
* @private
106-
*/
107-
Phaser.BitmapText.prototype.setStyle = function() {
108-
109-
this.style = { align: this._align };
110-
this.fontName = this._font;
111-
this.fontSize = this._fontSize;
112-
this.dirty = true;
113-
114-
};
127+
Phaser.BitmapText.prototype.preUpdatePhysics = Phaser.Component.PhysicsBody.preUpdate;
128+
Phaser.BitmapText.prototype.preUpdateLifeSpan = Phaser.Component.LifeSpan.preUpdate;
129+
Phaser.BitmapText.prototype.preUpdateInWorld = Phaser.Component.InWorld.preUpdate;
130+
Phaser.BitmapText.prototype.preUpdateCore = Phaser.Component.Core.preUpdate;
115131

116132
/**
117133
* Automatically called by World.preUpdate.
118-
* @method Phaser.BitmapText.prototype.preUpdate
134+
*
135+
* @method
136+
* @memberof Phaser.BitmapText
137+
* @return {boolean} True if the BitmapText was rendered, otherwise false.
119138
*/
120139
Phaser.BitmapText.prototype.preUpdate = function () {
121140

122-
Phaser.Component.InWorld.preUpdate.call(this);
123-
Phaser.Component.Core.preUpdate.call(this);
141+
if (!this.preUpdatePhysics() || !this.preUpdateLifeSpan() || !this.preUpdateInWorld())
142+
{
143+
return false;
144+
}
124145

125-
return true;
146+
return this.preUpdateCore();
126147

127148
};
128149

@@ -135,6 +156,26 @@ Phaser.BitmapText.prototype.postUpdate = function () {
135156
Phaser.Component.PhysicsBody.postUpdate.call(this);
136157
Phaser.Component.FixedToCamera.postUpdate.call(this);
137158

159+
if (this.body && ((this.textWidth !== this._tw) || (this.textHeight !== this._th)))
160+
{
161+
this.body.setSize(this.textWidth, this.textHeight);
162+
this._tw = this.textWidth;
163+
this._th = this.textHeight;
164+
}
165+
166+
};
167+
168+
/**
169+
* @method Phaser.BitmapText.prototype.setStyle
170+
* @private
171+
*/
172+
Phaser.BitmapText.prototype.setStyle = function() {
173+
174+
this.style = { align: this._align };
175+
this.fontName = this._font;
176+
this.fontSize = this._fontSize;
177+
this.dirty = true;
178+
138179
};
139180

140181
/**

src/pixi/text/BitmapText.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ PIXI.BitmapText = function(text, style)
4949
*/
5050
this.maxWidth = 0;
5151

52+
/**
53+
* @property anchor
54+
* @type Point
55+
*/
56+
this.anchor = new Phaser.Point(0, 0);
57+
58+
/**
59+
* @property _prevAnchor
60+
* @type Point
61+
*/
62+
this._prevAnchor = new Phaser.Point(0, 0);
63+
5264
/**
5365
* @property _pool
5466
* @type Array
@@ -177,49 +189,60 @@ PIXI.BitmapText.prototype.updateText = function()
177189

178190
var lineAlignOffsets = [];
179191

180-
for(i = 0; i <= line; i++)
192+
for (i = 0; i <= line; i++)
181193
{
182194
var alignOffset = 0;
183-
if(this.style.align === 'right')
195+
196+
if (this.style.align === 'right')
184197
{
185198
alignOffset = maxLineWidth - lineWidths[i];
186199
}
187-
else if(this.style.align === 'center')
200+
else if (this.style.align === 'center')
188201
{
189202
alignOffset = (maxLineWidth - lineWidths[i]) / 2;
190203
}
204+
191205
lineAlignOffsets.push(alignOffset);
192206
}
193207

194208
var lenChildren = this.children.length;
195209
var lenChars = chars.length;
196210
var tint = this.tint || 0xFFFFFF;
197211

198-
for(i = 0; i < lenChars; i++)
212+
this.textWidth = maxLineWidth * scale;
213+
this.textHeight = (pos.y + data.lineHeight) * scale;
214+
215+
var ax = this.textWidth * this.anchor.x;
216+
var ay = this.textHeight * this.anchor.y;
217+
218+
for (i = 0; i < lenChars; i++)
199219
{
200220
var c = i < lenChildren ? this.children[i] : this._pool.pop(); // get old child if have. if not - take from pool.
201221

202222
if (c) c.setTexture(chars[i].texture); // check if got one before.
203223
else c = new PIXI.Sprite(chars[i].texture); // if no create new one.
204224

205-
c.position.x = (chars[i].position.x + lineAlignOffsets[chars[i].line]) * scale;
206-
c.position.y = chars[i].position.y * scale;
225+
// c.position.x = (chars[i].position.x + lineAlignOffsets[chars[i].line]) * scale;
226+
// c.position.y = chars[i].position.y * scale;
227+
// c.position.x -= this.textWidth * this.anchor.x;
228+
// c.position.y -= this.textHeight * this.anchor.y;
229+
230+
c.position.x = ((chars[i].position.x + lineAlignOffsets[chars[i].line]) * scale) - ax;
231+
c.position.y = (chars[i].position.y * scale) - ay;
232+
207233
c.scale.x = c.scale.y = scale;
208234
c.tint = tint;
209235
if (!c.parent) this.addChild(c);
210236
}
211237

212-
// remove unnecessary children.
213-
// and put their into the pool.
214-
while(this.children.length > lenChars)
238+
// Remove unnecessary children and put them into the pool
239+
while (this.children.length > lenChars)
215240
{
216241
var child = this.getChildAt(this.children.length - 1);
217242
this._pool.push(child);
218243
this.removeChild(child);
219244
}
220245

221-
this.textWidth = maxLineWidth * scale;
222-
this.textHeight = (pos.y + data.lineHeight) * scale;
223246
};
224247

225248
/**
@@ -230,10 +253,11 @@ PIXI.BitmapText.prototype.updateText = function()
230253
*/
231254
PIXI.BitmapText.prototype.updateTransform = function()
232255
{
233-
if(this.dirty)
256+
if (this.dirty || !this.anchor.equals(this._prevAnchor))
234257
{
235258
this.updateText();
236259
this.dirty = false;
260+
this._prevAnchor.copyFrom(this.anchor);
237261
}
238262

239263
PIXI.DisplayObjectContainer.prototype.updateTransform.call(this);

0 commit comments

Comments
 (0)