2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-02-20 01:31:13 +00:00
|
|
|
* Create a new `Text` object. This uses a local hidden Canvas object and renders the type into it. It then makes a texture from this for renderning to the view.
|
|
|
|
* Because of this you can only display fonts that are currently loaded and available to the browser. It won't load the fonts for you.
|
|
|
|
* Here is a compatibility table showing the available default fonts across different mobile browsers: http://www.jordanm.co.uk/tinytype
|
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.Text
|
2014-02-21 13:25:08 +00:00
|
|
|
* @extends PIXI.Text
|
2013-10-01 12:54:29 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - Current game instance.
|
2013-10-25 14:22:45 +00:00
|
|
|
* @param {number} x - X position of the new text object.
|
|
|
|
* @param {number} y - Y position of the new text object.
|
|
|
|
* @param {string} text - The actual text that will be written.
|
|
|
|
* @param {object} style - The style object containing style attributes like font, font size ,
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-03 05:02:47 +00:00
|
|
|
Phaser.Text = function (game, x, y, text, style) {
|
|
|
|
|
|
|
|
x = x || 0;
|
|
|
|
y = y || 0;
|
2014-03-13 09:43:00 +00:00
|
|
|
text = text || ' ';
|
|
|
|
style = style || {};
|
|
|
|
|
2014-03-13 16:49:52 +00:00
|
|
|
if (text.length === 0)
|
2014-03-13 09:43:00 +00:00
|
|
|
{
|
|
|
|
text = ' ';
|
|
|
|
}
|
2014-03-19 00:54:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
text = text.toString();
|
|
|
|
}
|
2013-09-03 05:02:47 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - A reference to the currently running Game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {boolean} exists - If exists = false then the Text isn't updated by the core game loop.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-17 15:28:59 +00:00
|
|
|
this.exists = true;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {string} name - The user defined name given to this object.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-17 15:28:59 +00:00
|
|
|
this.name = '';
|
2013-09-03 05:02:47 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {number} type - The const type of this object.
|
|
|
|
* @default
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 03:13:04 +00:00
|
|
|
this.type = Phaser.TEXT;
|
2013-09-03 05:02:47 +00:00
|
|
|
|
2014-03-10 23:16:49 +00:00
|
|
|
/**
|
|
|
|
* @property {number} z - The z-depth value of this object within its Group (remember the World is a Group as well). No two objects in a Group can have the same z value.
|
|
|
|
*/
|
|
|
|
this.z = 0;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* @property {Phaser.Point} world - The world coordinates of this Sprite. This differs from the x/y coordinates which are relative to the Sprites container.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-02-09 13:36:02 +00:00
|
|
|
this.world = new Phaser.Point(x, y);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* @property {string} _text - Internal cache var.
|
|
|
|
* @private
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-02-09 13:36:02 +00:00
|
|
|
this._text = text;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* @property {string} _font - Internal cache var.
|
|
|
|
* @private
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-02-09 13:36:02 +00:00
|
|
|
this._font = '';
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* @property {number} _fontSize - Internal cache var.
|
|
|
|
* @private
|
2013-12-16 15:16:44 +00:00
|
|
|
*/
|
2014-02-09 13:36:02 +00:00
|
|
|
this._fontSize = 32;
|
2013-12-16 15:16:44 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* @property {string} _fontWeight - Internal cache var.
|
|
|
|
* @private
|
2013-12-16 15:16:44 +00:00
|
|
|
*/
|
2014-02-09 13:36:02 +00:00
|
|
|
this._fontWeight = 'normal';
|
2013-12-16 15:16:44 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* @property {number} lineSpacing - Additional spacing (in pixels) between each line of text if multi-line.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-02-09 13:36:02 +00:00
|
|
|
this._lineSpacing = 0;
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2014-02-09 22:48:35 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Events} events - The Events you can subscribe to that are dispatched when certain things happen on this Sprite or its components.
|
|
|
|
*/
|
|
|
|
this.events = new Phaser.Events(this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.InputHandler|null} input - The Input Handler for this object. Needs to be enabled with image.inputEnabled = true before you can use it.
|
|
|
|
*/
|
|
|
|
this.input = null;
|
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Point} cameraOffset - If this object is fixedToCamera then this stores the x/y offset that its drawn at, from the top-left of the camera view.
|
|
|
|
*/
|
|
|
|
this.cameraOffset = new Phaser.Point();
|
|
|
|
|
2014-03-13 09:43:00 +00:00
|
|
|
this.setStyle(style);
|
|
|
|
|
|
|
|
PIXI.Text.call(this, text, this.style);
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
this.position.set(x, y);
|
2013-09-03 05:02:47 +00:00
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
/**
|
|
|
|
* A small internal cache:
|
|
|
|
* 0 = previous position.x
|
|
|
|
* 1 = previous position.y
|
|
|
|
* 2 = previous rotation
|
|
|
|
* 3 = renderID
|
|
|
|
* 4 = fresh? (0 = no, 1 = yes)
|
|
|
|
* 5 = outOfBoundsFired (0 = no, 1 = yes)
|
|
|
|
* 6 = exists (0 = no, 1 = yes)
|
|
|
|
* 7 = fixed to camera (0 = no, 1 = yes)
|
2014-03-19 12:05:19 +00:00
|
|
|
* @property {Array} _cache
|
2014-02-15 01:27:42 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-03-19 12:05:19 +00:00
|
|
|
this._cache = [ 0, 0, 0, 0, 1, 0, 1, 0 ];
|
2014-02-15 01:27:42 +00:00
|
|
|
|
2013-09-03 05:02:47 +00:00
|
|
|
};
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
Phaser.Text.prototype = Object.create(PIXI.Text.prototype);
|
2013-09-03 05:02:47 +00:00
|
|
|
Phaser.Text.prototype.constructor = Phaser.Text;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* Automatically called by World.preUpdate.
|
|
|
|
* @method Phaser.Text.prototype.preUpdate
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-02-12 19:45:09 +00:00
|
|
|
Phaser.Text.prototype.preUpdate = function () {
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
this._cache[0] = this.world.x;
|
|
|
|
this._cache[1] = this.world.y;
|
|
|
|
this._cache[2] = this.rotation;
|
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
if (!this.exists || !this.parent.exists)
|
2013-09-17 15:28:59 +00:00
|
|
|
{
|
2014-02-15 01:27:42 +00:00
|
|
|
this.renderOrderID = -1;
|
2014-02-09 13:36:02 +00:00
|
|
|
return false;
|
2013-09-17 15:28:59 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
if (this.autoCull)
|
|
|
|
{
|
|
|
|
// Won't get rendered but will still get its transform updated
|
|
|
|
this.renderable = this.game.world.camera.screenView.intersects(this.getBounds());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
|
|
|
|
|
|
|
|
if (this.visible)
|
|
|
|
{
|
2014-02-28 19:45:15 +00:00
|
|
|
this._cache[3] = this.game.stage.currentRenderOrderID++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update any Children
|
|
|
|
for (var i = 0, len = this.children.length; i < len; i++)
|
|
|
|
{
|
|
|
|
this.children[i].preUpdate();
|
2014-02-15 01:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-12-16 15:16:44 +00:00
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2014-02-14 01:09:52 +00:00
|
|
|
/**
|
|
|
|
* Override and use this function in your own custom objects to handle any update requirements you may have.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text#update
|
|
|
|
* @memberof Phaser.Text
|
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.update = function() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
/**
|
|
|
|
* Automatically called by World.postUpdate.
|
|
|
|
* @method Phaser.Text.prototype.postUpdate
|
|
|
|
*/
|
2014-02-12 19:45:09 +00:00
|
|
|
Phaser.Text.prototype.postUpdate = function () {
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
if (this._cache[7] === 1)
|
2013-09-17 15:28:59 +00:00
|
|
|
{
|
2014-03-06 17:12:00 +00:00
|
|
|
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
|
|
|
|
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
|
2013-09-17 15:28:59 +00:00
|
|
|
}
|
2014-02-15 01:27:42 +00:00
|
|
|
|
2014-02-28 19:45:15 +00:00
|
|
|
// Update any Children
|
|
|
|
for (var i = 0, len = this.children.length; i < len; i++)
|
|
|
|
{
|
|
|
|
this.children[i].postUpdate();
|
|
|
|
}
|
|
|
|
|
2013-09-17 15:28:59 +00:00
|
|
|
}
|
|
|
|
|
2013-10-17 14:40:44 +00:00
|
|
|
/**
|
|
|
|
* @method Phaser.Text.prototype.destroy
|
2014-02-27 20:05:16 +00:00
|
|
|
* @param {boolean} [destroyChildren=true] - Should every child of this object have its destroy method called?
|
2013-10-17 14:40:44 +00:00
|
|
|
*/
|
2014-02-27 20:05:16 +00:00
|
|
|
Phaser.Text.prototype.destroy = function (destroyChildren) {
|
|
|
|
|
|
|
|
if (this.game === null) { return; }
|
|
|
|
|
|
|
|
if (typeof destroyChildren === 'undefined') { destroyChildren = true; }
|
2013-10-17 14:40:44 +00:00
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
if (this.parent)
|
2013-10-17 14:40:44 +00:00
|
|
|
{
|
2014-02-28 09:30:53 +00:00
|
|
|
if (this.parent instanceof Phaser.Group)
|
2014-02-28 06:17:18 +00:00
|
|
|
{
|
|
|
|
this.parent.remove(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.parent.removeChild(this);
|
|
|
|
}
|
2013-10-17 14:40:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
this.texture.destroy();
|
|
|
|
|
2013-10-17 14:40:44 +00:00
|
|
|
if (this.canvas.parentNode)
|
|
|
|
{
|
|
|
|
this.canvas.parentNode.removeChild(this.canvas);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.canvas = null;
|
|
|
|
this.context = null;
|
|
|
|
}
|
|
|
|
|
2014-02-21 13:25:08 +00:00
|
|
|
var i = this.children.length;
|
|
|
|
|
2014-02-27 20:05:16 +00:00
|
|
|
if (destroyChildren)
|
|
|
|
{
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
this.children[i].destroy(destroyChildren);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-02-21 13:25:08 +00:00
|
|
|
{
|
2014-02-27 20:05:16 +00:00
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
this.removeChild(this.children[i]);
|
|
|
|
}
|
2014-02-21 13:25:08 +00:00
|
|
|
}
|
|
|
|
|
2013-10-17 14:40:44 +00:00
|
|
|
this.exists = false;
|
2014-02-09 13:36:02 +00:00
|
|
|
this.visible = false;
|
|
|
|
|
2014-02-21 13:25:08 +00:00
|
|
|
this.filters = null;
|
|
|
|
this.mask = null;
|
2014-02-09 13:36:02 +00:00
|
|
|
this.game = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method Phaser.Text.prototype.setShadow
|
|
|
|
* @param {number} [x=0] - The shadowOffsetX value in pixels. This is how far offset horizontally the shadow effect will be.
|
|
|
|
* @param {number} [y=0] - The shadowOffsetY value in pixels. This is how far offset vertically the shadow effect will be.
|
|
|
|
* @param {string} [color='rgba(0,0,0,0)'] - The color of the shadow, as given in CSS rgba format. Set the alpha component to 0 to disable the shadow.
|
|
|
|
* @param {number} [blur=0] - The shadowBlur value. Make the shadow softer by applying a Gaussian blur to it. A number from 0 (no blur) up to approx. 10 (depending on scene).
|
|
|
|
*/
|
2014-02-12 19:45:09 +00:00
|
|
|
Phaser.Text.prototype.setShadow = function (x, y, color, blur) {
|
2013-10-17 14:40:44 +00:00
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
this.style.shadowOffsetX = x || 0;
|
|
|
|
this.style.shadowOffsetY = y || 0;
|
|
|
|
this.style.shadowColor = color || 'rgba(0,0,0,0)';
|
|
|
|
this.style.shadowBlur = blur || 0;
|
|
|
|
this.dirty = true;
|
2013-10-17 14:40:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
/**
|
|
|
|
* Set the style of the text by passing a single style object to it.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text.prototype.setStyle
|
|
|
|
* @param [style] {Object} The style parameters
|
|
|
|
* @param [style.font='bold 20pt Arial'] {String} The style and size of the font
|
|
|
|
* @param [style.fill='black'] {Object} A canvas fillstyle that will be used on the text eg 'red', '#00FF00'
|
|
|
|
* @param [style.align='left'] {String} Alignment for multiline text ('left', 'center' or 'right'), does not affect single line text
|
|
|
|
* @param [style.stroke='black'] {String} A canvas fillstyle that will be used on the text stroke eg 'blue', '#FCFF00'
|
|
|
|
* @param [style.strokeThickness=0] {Number} A number that represents the thickness of the stroke. Default is 0 (no stroke)
|
|
|
|
* @param [style.wordWrap=false] {Boolean} Indicates if word wrap should be used
|
|
|
|
* @param [style.wordWrapWidth=100] {Number} The width at which text will wrap
|
|
|
|
*/
|
2014-02-12 19:45:09 +00:00
|
|
|
Phaser.Text.prototype.setStyle = function (style) {
|
2014-02-09 13:36:02 +00:00
|
|
|
|
|
|
|
style = style || {};
|
|
|
|
style.font = style.font || 'bold 20pt Arial';
|
|
|
|
style.fill = style.fill || 'black';
|
|
|
|
style.align = style.align || 'left';
|
|
|
|
style.stroke = style.stroke || 'black'; //provide a default, see: https://github.com/GoodBoyDigital/pixi.js/issues/136
|
|
|
|
style.strokeThickness = style.strokeThickness || 0;
|
|
|
|
style.wordWrap = style.wordWrap || false;
|
|
|
|
style.wordWrapWidth = style.wordWrapWidth || 100;
|
|
|
|
style.shadowOffsetX = style.shadowOffsetX || 0;
|
|
|
|
style.shadowOffsetY = style.shadowOffsetY || 0;
|
|
|
|
style.shadowColor = style.shadowColor || 'rgba(0,0,0,0)';
|
|
|
|
style.shadowBlur = style.shadowBlur || 0;
|
|
|
|
|
|
|
|
this.style = style;
|
|
|
|
this.dirty = true;
|
|
|
|
|
2014-02-14 01:09:52 +00:00
|
|
|
}
|
2014-02-09 13:36:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders text. This replaces the Pixi.Text.updateText function as we need a few extra bits in here.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text.prototype.updateText
|
|
|
|
* @private
|
|
|
|
*/
|
2014-02-12 19:45:09 +00:00
|
|
|
Phaser.Text.prototype.updateText = function () {
|
2014-02-09 13:36:02 +00:00
|
|
|
|
|
|
|
this.context.font = this.style.font;
|
|
|
|
|
|
|
|
var outputText = this.text;
|
|
|
|
|
|
|
|
// word wrap
|
|
|
|
// preserve original text
|
2014-03-13 16:49:52 +00:00
|
|
|
if (this.style.wordWrap)
|
|
|
|
{
|
|
|
|
outputText = this.runWordWrap(this.text);
|
|
|
|
}
|
2014-02-09 13:36:02 +00:00
|
|
|
|
|
|
|
//split text into lines
|
|
|
|
var lines = outputText.split(/(?:\r\n|\r|\n)/);
|
|
|
|
|
|
|
|
//calculate text width
|
|
|
|
var lineWidths = [];
|
|
|
|
var maxLineWidth = 0;
|
2014-03-13 16:49:52 +00:00
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
for (var i = 0; i < lines.length; i++)
|
|
|
|
{
|
|
|
|
var lineWidth = this.context.measureText(lines[i]).width;
|
|
|
|
lineWidths[i] = lineWidth;
|
|
|
|
maxLineWidth = Math.max(maxLineWidth, lineWidth);
|
|
|
|
}
|
2014-03-13 16:49:52 +00:00
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
this.canvas.width = maxLineWidth + this.style.strokeThickness;
|
|
|
|
|
|
|
|
//calculate text height
|
|
|
|
var lineHeight = this.determineFontHeight('font: ' + this.style.font + ';') + this.style.strokeThickness + this._lineSpacing + this.style.shadowOffsetY;
|
2014-03-13 16:49:52 +00:00
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
this.canvas.height = lineHeight * lines.length;
|
|
|
|
|
2014-03-13 16:49:52 +00:00
|
|
|
if (navigator.isCocoonJS)
|
|
|
|
{
|
|
|
|
this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
|
|
|
|
}
|
2014-02-09 13:36:02 +00:00
|
|
|
|
|
|
|
//set canvas text styles
|
|
|
|
this.context.fillStyle = this.style.fill;
|
|
|
|
this.context.font = this.style.font;
|
|
|
|
|
|
|
|
this.context.strokeStyle = this.style.stroke;
|
|
|
|
this.context.lineWidth = this.style.strokeThickness;
|
|
|
|
|
|
|
|
this.context.shadowOffsetX = this.style.shadowOffsetX;
|
|
|
|
this.context.shadowOffsetY = this.style.shadowOffsetY;
|
|
|
|
this.context.shadowColor = this.style.shadowColor;
|
|
|
|
this.context.shadowBlur = this.style.shadowBlur;
|
|
|
|
|
|
|
|
this.context.textBaseline = 'top';
|
|
|
|
|
|
|
|
//draw lines line by line
|
|
|
|
for (i = 0; i < lines.length; i++)
|
|
|
|
{
|
|
|
|
var linePosition = new PIXI.Point(this.style.strokeThickness / 2, this.style.strokeThickness / 2 + i * lineHeight);
|
|
|
|
|
|
|
|
if(this.style.align === 'right')
|
|
|
|
{
|
|
|
|
linePosition.x += maxLineWidth - lineWidths[i];
|
|
|
|
}
|
|
|
|
else if(this.style.align === 'center')
|
|
|
|
{
|
|
|
|
linePosition.x += (maxLineWidth - lineWidths[i]) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
linePosition.y += this._lineSpacing;
|
|
|
|
|
|
|
|
if(this.style.stroke && this.style.strokeThickness)
|
|
|
|
{
|
|
|
|
this.context.strokeText(lines[i], linePosition.x, linePosition.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.style.fill)
|
|
|
|
{
|
|
|
|
this.context.fillText(lines[i], linePosition.x, linePosition.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateTexture();
|
2014-02-14 01:09:52 +00:00
|
|
|
}
|
2014-02-09 13:36:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Greedy wrapping algorithm that will wrap words as the line grows longer than its horizontal bounds.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text.prototype.runWordWrap
|
|
|
|
* @private
|
|
|
|
*/
|
2014-02-12 19:45:09 +00:00
|
|
|
Phaser.Text.prototype.runWordWrap = function (text) {
|
2014-02-09 13:36:02 +00:00
|
|
|
|
|
|
|
var result = '';
|
|
|
|
var lines = text.split('\n');
|
|
|
|
|
|
|
|
for (var i = 0; i < lines.length; i++)
|
|
|
|
{
|
|
|
|
var spaceLeft = this.style.wordWrapWidth;
|
|
|
|
var words = lines[i].split(' ');
|
|
|
|
|
|
|
|
for (var j = 0; j < words.length; j++)
|
|
|
|
{
|
|
|
|
var wordWidth = this.context.measureText(words[j]).width;
|
|
|
|
var wordWidthWithSpace = wordWidth + this.context.measureText(' ').width;
|
|
|
|
|
|
|
|
if (wordWidthWithSpace > spaceLeft)
|
|
|
|
{
|
|
|
|
// Skip printing the newline if it's the first word of the line that is greater than the word wrap width.
|
|
|
|
if (j > 0)
|
|
|
|
{
|
|
|
|
result += '\n';
|
|
|
|
}
|
|
|
|
result += words[j] + ' ';
|
|
|
|
spaceLeft = this.style.wordWrapWidth - wordWidth;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spaceLeft -= wordWidthWithSpace;
|
|
|
|
result += words[j] + ' ';
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:18:53 +00:00
|
|
|
|
|
|
|
if (i < lines.length-1)
|
|
|
|
{
|
|
|
|
result += '\n';
|
|
|
|
}
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
2014-02-14 01:09:52 +00:00
|
|
|
}
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* Indicates the rotation of the Text, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.
|
|
|
|
* Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement player.angle = 450 is the same as player.angle = 90.
|
|
|
|
* If you wish to work in radians instead of degrees use the property Sprite.rotation instead.
|
|
|
|
* @name Phaser.Text#angle
|
|
|
|
* @property {number} angle - Gets or sets the angle of rotation in degrees.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-17 15:28:59 +00:00
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'angle', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return Phaser.Math.radToDeg(this.rotation);
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
this.rotation = Phaser.Math.degToRad(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* The text string to be displayed by this Text object, taking into account the style settings.
|
|
|
|
* @name Phaser.Text#text
|
|
|
|
* @property {string} text - The text string to be displayed by this Text object, taking into account the style settings.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'text', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this._text;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this._text)
|
|
|
|
{
|
|
|
|
this._text = value.toString() || ' ';
|
|
|
|
this.dirty = true;
|
2014-03-11 20:21:07 +00:00
|
|
|
this.updateTransform();
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#font
|
|
|
|
* @property {string} font - The font the text will be rendered in, i.e. 'Arial'. Must be loaded in the browser before use.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-02-09 13:36:02 +00:00
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'font', {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
get: function() {
|
2014-02-09 13:36:02 +00:00
|
|
|
return this._font;
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
2014-02-09 13:36:02 +00:00
|
|
|
|
|
|
|
if (value !== this._font)
|
|
|
|
{
|
|
|
|
this._font = value.trim();
|
|
|
|
this.style.font = this._fontWeight + ' ' + this._fontSize + "px '" + this._font + "'";
|
|
|
|
this.dirty = true;
|
2014-03-11 20:21:07 +00:00
|
|
|
this.updateTransform();
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* @name Phaser.Text#fontSize
|
|
|
|
* @property {number} fontSize - The size of the font in pixels.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-02-09 13:36:02 +00:00
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'fontSize', {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
get: function() {
|
2014-02-09 13:36:02 +00:00
|
|
|
return this._fontSize;
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2014-03-13 16:49:52 +00:00
|
|
|
value = parseInt(value, 10);
|
2014-02-09 13:36:02 +00:00
|
|
|
|
|
|
|
if (value !== this._fontSize)
|
|
|
|
{
|
|
|
|
this._fontSize = value;
|
|
|
|
this.style.font = this._fontWeight + ' ' + this._fontSize + "px '" + this._font + "'";
|
|
|
|
this.dirty = true;
|
2014-03-11 20:21:07 +00:00
|
|
|
this.updateTransform();
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* @name Phaser.Text#fontWeight
|
|
|
|
* @property {number} fontWeight - The weight of the font: 'normal', 'bold', 'italic'. You can combine settings too, such as 'bold italic'.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-02-09 13:36:02 +00:00
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'fontWeight', {
|
2013-09-20 12:55:33 +00:00
|
|
|
|
|
|
|
get: function() {
|
2014-02-09 13:36:02 +00:00
|
|
|
return this._fontWeight;
|
2013-09-20 12:55:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
if (value !== this._fontWeight)
|
2013-09-20 12:55:33 +00:00
|
|
|
{
|
2014-02-09 13:36:02 +00:00
|
|
|
this._fontWeight = value;
|
|
|
|
this.style.font = this._fontWeight + ' ' + this._fontSize + "px '" + this._font + "'";
|
|
|
|
this.dirty = true;
|
2014-03-11 20:21:07 +00:00
|
|
|
this.updateTransform();
|
2013-09-20 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-02-09 13:36:02 +00:00
|
|
|
* @name Phaser.Text#fill
|
|
|
|
* @property {object} fill - A canvas fillstyle that will be used on the text eg 'red', '#00FF00'.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-02-09 13:36:02 +00:00
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'fill', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.fill;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.fill)
|
|
|
|
{
|
|
|
|
this.style.fill = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#align
|
|
|
|
* @property {string} align - Alignment for multiline text ('left', 'center' or 'right'), does not affect single line text.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'align', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.align;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.align)
|
|
|
|
{
|
|
|
|
this.style.align = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#stroke
|
|
|
|
* @property {string} stroke - A canvas fillstyle that will be used on the text stroke eg 'blue', '#FCFF00'.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'stroke', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.stroke;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.stroke)
|
|
|
|
{
|
|
|
|
this.style.stroke = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#strokeThickness
|
|
|
|
* @property {number} strokeThickness - A number that represents the thickness of the stroke. Default is 0 (no stroke)
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'strokeThickness', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.strokeThickness;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.strokeThickness)
|
|
|
|
{
|
|
|
|
this.style.strokeThickness = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#wordWrap
|
|
|
|
* @property {boolean} wordWrap - Indicates if word wrap should be used.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'wordWrap', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.wordWrap;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.wordWrap)
|
|
|
|
{
|
|
|
|
this.style.wordWrap = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#wordWrapWidth
|
|
|
|
* @property {number} wordWrapWidth - The width at which text will wrap.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'wordWrapWidth', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.wordWrapWidth;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.wordWrapWidth)
|
|
|
|
{
|
|
|
|
this.style.wordWrapWidth = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#lineSpacing
|
|
|
|
* @property {number} lineSpacing - Additional spacing (in pixels) between each line of text if multi-line.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'lineSpacing', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this._lineSpacing;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this._lineSpacing)
|
|
|
|
{
|
|
|
|
this._lineSpacing = parseFloat(value);
|
|
|
|
this.dirty = true;
|
2014-03-11 20:21:07 +00:00
|
|
|
this.updateTransform();
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#shadowOffsetX
|
|
|
|
* @property {number} shadowOffsetX - The shadowOffsetX value in pixels. This is how far offset horizontally the shadow effect will be.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'shadowOffsetX', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.shadowOffsetX;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.shadowOffsetX)
|
|
|
|
{
|
|
|
|
this.style.shadowOffsetX = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#shadowOffsetY
|
|
|
|
* @property {number} shadowOffsetY - The shadowOffsetY value in pixels. This is how far offset vertically the shadow effect will be.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'shadowOffsetY', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.shadowOffsetY;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.shadowOffsetY)
|
|
|
|
{
|
|
|
|
this.style.shadowOffsetY = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#shadowColor
|
|
|
|
* @property {string} shadowColor - The color of the shadow, as given in CSS rgba format. Set the alpha component to 0 to disable the shadow.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'shadowColor', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.shadowColor;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.shadowColor)
|
|
|
|
{
|
|
|
|
this.style.shadowColor = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#shadowBlur
|
|
|
|
* @property {number} shadowBlur - The shadowBlur value. Make the shadow softer by applying a Gaussian blur to it. A number from 0 (no blur) up to approx. 10 (depending on scene).
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'shadowBlur', {
|
2013-09-20 12:55:33 +00:00
|
|
|
|
|
|
|
get: function() {
|
2014-02-09 13:36:02 +00:00
|
|
|
return this.style.shadowBlur;
|
2013-09-20 12:55:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
if (value !== this.style.shadowBlur)
|
2013-09-20 12:55:33 +00:00
|
|
|
{
|
2014-02-09 13:36:02 +00:00
|
|
|
this.style.shadowBlur = value;
|
|
|
|
this.dirty = true;
|
2013-09-20 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2014-02-09 22:48:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* By default a Text object won't process any input events at all. By setting inputEnabled to true the Phaser.InputHandler is
|
|
|
|
* activated for this object and it will then start to process click/touch events and more.
|
|
|
|
*
|
|
|
|
* @name Phaser.Text#inputEnabled
|
|
|
|
* @property {boolean} inputEnabled - Set to true to allow this object to receive input events.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, "inputEnabled", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return (this.input && this.input.enabled);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
if (this.input === null)
|
|
|
|
{
|
|
|
|
this.input = new Phaser.InputHandler(this);
|
|
|
|
this.input.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.input && this.input.enabled)
|
|
|
|
{
|
|
|
|
this.input.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2014-02-15 01:27:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An Text that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in Text.cameraOffset.
|
|
|
|
* Note that the cameraOffset values are in addition to any parent in the display list.
|
|
|
|
* So if this Text was in a Group that has x: 200, then this will be added to the cameraOffset.x
|
|
|
|
*
|
|
|
|
* @name Phaser.Text#fixedToCamera
|
|
|
|
* @property {boolean} fixedToCamera - Set to true to fix this Text to the Camera at its current world coordinates.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, "fixedToCamera", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return !!this._cache[7];
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
this._cache[7] = 1;
|
|
|
|
this.cameraOffset.set(this.x, this.y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._cache[7] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|