2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2015-02-25 03:36:23 +00:00
|
|
|
* @copyright 2015 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-03-23 15:03:53 +00:00
|
|
|
* BitmapText objects work by taking a texture file and an XML file that describes the font structure.
|
|
|
|
* It then generates a new Sprite object for each letter of the text, proportionally spaced out and aligned to
|
|
|
|
* match the font structure.
|
|
|
|
*
|
|
|
|
* BitmapText objects are less flexible than Text objects, in that they have less features such as shadows, fills and the ability
|
|
|
|
* to use Web Fonts, however you trade this flexibility for rendering speed. You can also create visually compelling BitmapTexts by
|
|
|
|
* processing the font texture in an image editor, applying fills and any other effects required.
|
2013-11-28 15:57:09 +00:00
|
|
|
*
|
2015-03-23 15:03:53 +00:00
|
|
|
* To create a BitmapText you can use:
|
|
|
|
*
|
|
|
|
* BMFont (Windows, free): http://www.angelcode.com/products/bmfont/
|
|
|
|
* Glyph Designer (OS X, commercial): http://www.71squared.com/en/glyphdesigner
|
|
|
|
* Littera (Web-based, free): http://kvazars.com/littera/
|
2013-11-01 17:28:09 +00:00
|
|
|
*
|
2014-09-16 16:35:08 +00:00
|
|
|
* @class Phaser.BitmapText
|
2013-10-01 12:54:29 +00:00
|
|
|
* @constructor
|
2014-09-16 16:35:08 +00:00
|
|
|
* @extends PIXI.BitmapText
|
2015-03-01 07:00:17 +00:00
|
|
|
* @extends Phaser.Component.Core
|
|
|
|
* @extends Phaser.Component.Angle
|
|
|
|
* @extends Phaser.Component.AutoCull
|
|
|
|
* @extends Phaser.Component.Bounds
|
|
|
|
* @extends Phaser.Component.Destroy
|
|
|
|
* @extends Phaser.Component.FixedToCamera
|
|
|
|
* @extends Phaser.Component.InputEnabled
|
|
|
|
* @extends Phaser.Component.InWorld
|
|
|
|
* @extends Phaser.Component.LifeSpan
|
|
|
|
* @extends Phaser.Component.PhysicsBody
|
|
|
|
* @extends Phaser.Component.Reset
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
2013-10-25 14:22:45 +00:00
|
|
|
* @param {number} x - X position of the new bitmapText object.
|
|
|
|
* @param {number} y - Y position of the new bitmapText object.
|
2014-02-14 03:34:35 +00:00
|
|
|
* @param {string} font - The key of the BitmapFont as stored in Game.Cache.
|
2014-02-14 04:34:57 +00:00
|
|
|
* @param {string} [text=''] - The actual text that will be rendered. Can be set later via BitmapText.text.
|
|
|
|
* @param {number} [size=32] - The size the font will be rendered in, in pixels.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-02-14 03:34:35 +00:00
|
|
|
Phaser.BitmapText = function (game, x, y, font, text, size) {
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
x = x || 0;
|
|
|
|
y = y || 0;
|
2014-02-14 03:34:35 +00:00
|
|
|
font = font || '';
|
2013-09-10 22:51:35 +00:00
|
|
|
text = text || '';
|
2014-02-14 03:34:35 +00:00
|
|
|
size = size || 32;
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {number} type - The const type of this object.
|
|
|
|
* @readonly
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.type = Phaser.BITMAPTEXT;
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2015-03-23 15:03:53 +00:00
|
|
|
/**
|
|
|
|
* @property {number} physicsType - The const physics body type of this object.
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.physicsType = Phaser.SPRITE;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-02-14 03:34:35 +00:00
|
|
|
* @property {string} _text - Internal cache var.
|
|
|
|
* @private
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-02-14 03:34:35 +00:00
|
|
|
this._text = text;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-02-14 03:34:35 +00:00
|
|
|
* @property {string} _font - Internal cache var.
|
|
|
|
* @private
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-02-14 03:34:35 +00:00
|
|
|
this._font = font;
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-02-14 03:34:35 +00:00
|
|
|
* @property {number} _fontSize - Internal cache var.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-02-14 03:34:35 +00:00
|
|
|
this._fontSize = size;
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2014-02-14 03:34:35 +00:00
|
|
|
/**
|
2014-02-14 04:34:57 +00:00
|
|
|
* @property {string} _align - Internal cache var.
|
2014-02-14 03:34:35 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-02-14 04:34:57 +00:00
|
|
|
this._align = 'left';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _tint - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tint = 0xFFFFFF;
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2015-03-23 15:03:53 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _tw - Internal cache var. Holds the previous textWidth.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tw = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _th - Internal cache var. Holds the previous textHeight.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._th = 0;
|
|
|
|
|
2014-02-14 03:34:35 +00:00
|
|
|
PIXI.BitmapText.call(this, text);
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2015-02-17 06:00:41 +00:00
|
|
|
Phaser.Component.Core.init.call(this, game, x, y, '', null);
|
2014-02-15 01:27:42 +00:00
|
|
|
|
2013-09-10 22:51:35 +00:00
|
|
|
};
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
Phaser.BitmapText.prototype = Object.create(PIXI.BitmapText.prototype);
|
2013-09-10 22:51:35 +00:00
|
|
|
Phaser.BitmapText.prototype.constructor = Phaser.BitmapText;
|
|
|
|
|
2015-03-23 15:03:53 +00:00
|
|
|
Phaser.Component.Core.install.call(Phaser.BitmapText.prototype, [
|
2015-02-23 04:44:11 +00:00
|
|
|
'Angle',
|
|
|
|
'AutoCull',
|
|
|
|
'Bounds',
|
|
|
|
'Destroy',
|
|
|
|
'FixedToCamera',
|
|
|
|
'InputEnabled',
|
|
|
|
'InWorld',
|
|
|
|
'LifeSpan',
|
|
|
|
'PhysicsBody',
|
|
|
|
'Reset'
|
2015-03-23 15:03:53 +00:00
|
|
|
]);
|
2015-02-23 04:44:11 +00:00
|
|
|
|
2015-03-23 15:03:53 +00:00
|
|
|
Phaser.BitmapText.prototype.preUpdatePhysics = Phaser.Component.PhysicsBody.preUpdate;
|
|
|
|
Phaser.BitmapText.prototype.preUpdateLifeSpan = Phaser.Component.LifeSpan.preUpdate;
|
|
|
|
Phaser.BitmapText.prototype.preUpdateInWorld = Phaser.Component.InWorld.preUpdate;
|
|
|
|
Phaser.BitmapText.prototype.preUpdateCore = Phaser.Component.Core.preUpdate;
|
2014-02-14 03:34:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatically called by World.preUpdate.
|
2015-03-23 15:03:53 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @memberof Phaser.BitmapText
|
|
|
|
* @return {boolean} True if the BitmapText was rendered, otherwise false.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-02-14 03:34:35 +00:00
|
|
|
Phaser.BitmapText.prototype.preUpdate = function () {
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2015-03-23 15:03:53 +00:00
|
|
|
if (!this.preUpdatePhysics() || !this.preUpdateLifeSpan() || !this.preUpdateInWorld())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-15 01:27:42 +00:00
|
|
|
|
2015-03-23 15:03:53 +00:00
|
|
|
return this.preUpdateCore();
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2014-02-14 03:34:35 +00:00
|
|
|
/**
|
2015-02-17 06:00:41 +00:00
|
|
|
* Automatically called by World.preUpdate.
|
2014-02-14 03:34:35 +00:00
|
|
|
* @method Phaser.BitmapText.prototype.postUpdate
|
|
|
|
*/
|
|
|
|
Phaser.BitmapText.prototype.postUpdate = function () {
|
2013-09-21 00:03:19 +00:00
|
|
|
|
2015-02-17 06:00:41 +00:00
|
|
|
Phaser.Component.PhysicsBody.postUpdate.call(this);
|
|
|
|
Phaser.Component.FixedToCamera.postUpdate.call(this);
|
2014-04-22 00:43:22 +00:00
|
|
|
|
2015-03-23 15:03:53 +00:00
|
|
|
if (this.body && ((this.textWidth !== this._tw) || (this.textHeight !== this._th)))
|
|
|
|
{
|
|
|
|
this.body.setSize(this.textWidth, this.textHeight);
|
|
|
|
this._tw = this.textWidth;
|
|
|
|
this._th = this.textHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method Phaser.BitmapText.prototype.setStyle
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Phaser.BitmapText.prototype.setStyle = function() {
|
|
|
|
|
|
|
|
this.style = { align: this._align };
|
|
|
|
this.fontName = this._font;
|
|
|
|
this.fontSize = this._fontSize;
|
|
|
|
this.dirty = true;
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-10-17 14:40:44 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-02-14 03:34:35 +00:00
|
|
|
* @name Phaser.BitmapText#align
|
|
|
|
* @property {string} align - Alignment for multiline text ('left', 'center' or 'right'), does not affect single line text.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.BitmapText.prototype, 'align', {
|
|
|
|
|
|
|
|
get: function() {
|
2014-02-14 04:34:57 +00:00
|
|
|
return this._align;
|
2014-02-14 03:34:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
2014-02-14 04:34:57 +00:00
|
|
|
if (value !== this._align)
|
2014-02-14 03:34:35 +00:00
|
|
|
{
|
2014-02-14 04:34:57 +00:00
|
|
|
this._align = value;
|
2014-02-14 06:04:29 +00:00
|
|
|
this.setStyle();
|
2014-02-14 03:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-02-14 04:34:57 +00:00
|
|
|
/**
|
|
|
|
* @name Phaser.BitmapText#tint
|
|
|
|
* @property {number} tint - The tint applied to the BitmapText. This is a hex value. Set to white to disable (0xFFFFFF)
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.BitmapText.prototype, 'tint', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this._tint;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this._tint)
|
|
|
|
{
|
|
|
|
this._tint = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2014-02-14 03:34:35 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-02-14 03:34:35 +00:00
|
|
|
* @name Phaser.BitmapText#font
|
|
|
|
* @property {string} font - The font the text will be rendered in, i.e. 'Arial'. Must be loaded in the browser before use.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.BitmapText.prototype, 'font', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this._font;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this._font)
|
|
|
|
{
|
|
|
|
this._font = value.trim();
|
2015-02-08 22:07:36 +00:00
|
|
|
this.fontName = this._font;
|
2014-02-14 03:34:35 +00:00
|
|
|
this.style.font = this._fontSize + "px '" + this._font + "'";
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.BitmapText#fontSize
|
|
|
|
* @property {number} fontSize - The size of the font in pixels.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-02-14 03:34:35 +00:00
|
|
|
Object.defineProperty(Phaser.BitmapText.prototype, 'fontSize', {
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
get: function() {
|
2014-02-14 03:34:35 +00:00
|
|
|
return this._fontSize;
|
2013-09-10 22:51:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
2014-02-14 03:34:35 +00:00
|
|
|
|
2014-03-13 16:49:52 +00:00
|
|
|
value = parseInt(value, 10);
|
2014-02-14 03:34:35 +00:00
|
|
|
|
|
|
|
if (value !== this._fontSize)
|
|
|
|
{
|
|
|
|
this._fontSize = value;
|
|
|
|
this.style.font = this._fontSize + "px '" + this._font + "'";
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
2013-09-10 22:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-02-14 03:34:35 +00:00
|
|
|
* The text string to be displayed by this Text object, taking into account the style settings.
|
|
|
|
* @name Phaser.BitmapText#text
|
|
|
|
* @property {string} text - The text string to be displayed by this Text object, taking into account the style settings.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-02-14 03:34:35 +00:00
|
|
|
Object.defineProperty(Phaser.BitmapText.prototype, 'text', {
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
get: function() {
|
2014-02-14 03:34:35 +00:00
|
|
|
return this._text;
|
2013-09-10 22:51:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
2014-02-14 03:34:35 +00:00
|
|
|
|
|
|
|
if (value !== this._text)
|
|
|
|
{
|
|
|
|
this._text = value.toString() || ' ';
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|