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}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-11-28 08:12:10 +00:00
|
|
|
* Create a new game object for displaying Text.
|
|
|
|
*
|
2015-03-19 01:54:14 +00:00
|
|
|
* This uses a local hidden Canvas object and renders the type into it. It then makes a texture from this for rendering to the view.
|
2014-11-28 08:12:10 +00:00
|
|
|
* Because of this you can only display fonts that are currently loaded and available to the browser: fonts must be pre-loaded.
|
|
|
|
*
|
|
|
|
* See {@link http://www.jordanm.co.uk/tinytype this compatibility table} for the available default fonts across mobile browsers.
|
2014-02-20 01:31:13 +00:00
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.Text
|
2015-06-17 12:21:28 +00:00
|
|
|
* @extends Phaser.Sprite
|
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.
|
2015-06-17 16:18:04 +00:00
|
|
|
* @param {object} [style] - The style properties to be set on the Text.
|
|
|
|
* @param {string} [style.font='bold 20pt Arial'] - The style and size of the font.
|
|
|
|
* @param {string} [style.fontStyle=(from font)] - The style of the font (eg. 'italic'): overrides the value in `style.font`.
|
|
|
|
* @param {string} [style.fontVariant=(from font)] - The variant of the font (eg. 'small-caps'): overrides the value in `style.font`.
|
|
|
|
* @param {string} [style.fontWeight=(from font)] - The weight of the font (eg. 'bold'): overrides the value in `style.font`.
|
|
|
|
* @param {string|number} [style.fontSize=(from font)] - The size of the font (eg. 32 or '32px'): overrides the value in `style.font`.
|
|
|
|
* @param {string} [style.backgroundColor=null] - A canvas fillstyle that will be used as the background for the whole Text object. Set to `null` to disable.
|
|
|
|
* @param {string} [style.fill='black'] - A canvas fillstyle that will be used on the text eg 'red', '#00FF00'.
|
|
|
|
* @param {string} [style.align='left'] - Horizontal alignment of each line in multiline text. Can be: 'left', 'center' or 'right'. Does not affect single lines of text (see `textBounds` and `boundsAlignH` for that).
|
|
|
|
* @param {string} [style.boundsAlignH='left'] - Horizontal alignment of the text within the `textBounds`. Can be: 'left', 'center' or 'right'.
|
|
|
|
* @param {string} [style.boundsAlignV='top'] - Vertical alignment of the text within the `textBounds`. Can be: 'top', 'middle' or 'bottom'.
|
|
|
|
* @param {string} [style.stroke='black'] - A canvas stroke style that will be used on the text stroke eg 'blue', '#FCFF00'.
|
|
|
|
* @param {number} [style.strokeThickness=0] - A number that represents the thickness of the stroke. Default is 0 (no stroke).
|
|
|
|
* @param {boolean} [style.wordWrap=false] - Indicates if word wrap should be used.
|
|
|
|
* @param {number} [style.wordWrapWidth=100] - The width in pixels at which text will wrap.
|
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;
|
2015-07-01 22:37:39 +00:00
|
|
|
text = text.toString() || '';
|
2014-03-13 09:43:00 +00:00
|
|
|
style = style || {};
|
|
|
|
|
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
|
|
|
|
2015-03-23 15:04:27 +00:00
|
|
|
/**
|
|
|
|
* @property {number} physicsType - The const physics body type of this object.
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.physicsType = Phaser.SPRITE;
|
|
|
|
|
2015-03-19 01:54:14 +00:00
|
|
|
/**
|
|
|
|
* Specify a padding value which is added to the line width and height when calculating the Text size.
|
|
|
|
* ALlows you to add extra spacing if Phaser is unable to accurately determine the true font dimensions.
|
|
|
|
* @property {Phaser.Point} padding
|
|
|
|
*/
|
|
|
|
this.padding = new Phaser.Point();
|
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
/**
|
|
|
|
* The textBounds property allows you to specify a rectangular region upon which text alignment is based.
|
|
|
|
* See `Text.setTextBounds` for more details.
|
|
|
|
* @property {Phaser.Rectangle} textBounds
|
|
|
|
* @readOnly
|
|
|
|
*/
|
|
|
|
this.textBounds = null;
|
|
|
|
|
2015-06-17 12:21:28 +00:00
|
|
|
/**
|
|
|
|
* @property {HTMLCanvasElement} canvas - The canvas element that the text is rendered.
|
|
|
|
*/
|
|
|
|
this.canvas = document.createElement('canvas');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {HTMLCanvasElement} context - The context of the canvas element that the text is rendered to.
|
|
|
|
*/
|
|
|
|
this.context = this.canvas.getContext('2d');
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @property {number} resolution - The resolution of the canvas.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.resolution = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array} colors - An array of the color values as specified by {@link Phaser.Text#addColor addColor}.
|
|
|
|
*/
|
|
|
|
this.colors = [];
|
|
|
|
|
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-11-28 09:39:07 +00:00
|
|
|
* @property {object} _fontComponents - The font, broken down into components, set in `setStyle`.
|
2014-02-09 13:36:02 +00:00
|
|
|
* @private
|
2013-12-16 15:16:44 +00:00
|
|
|
*/
|
2014-11-28 09:39:07 +00:00
|
|
|
this._fontComponents = null;
|
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-09-24 16:10:02 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _charCount - Internal character counter used by the text coloring.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._charCount = 0;
|
|
|
|
|
|
|
|
/**
|
2015-06-17 12:21:28 +00:00
|
|
|
* @property {number} _width - Internal width var.
|
|
|
|
* @private
|
2014-09-24 16:10:02 +00:00
|
|
|
*/
|
2015-06-17 12:21:28 +00:00
|
|
|
this._width = 0;
|
2014-09-24 16:10:02 +00:00
|
|
|
|
2015-06-17 12:21:28 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _height - Internal height var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._height = 0;
|
2014-03-13 09:43:00 +00:00
|
|
|
|
2015-06-17 12:21:28 +00:00
|
|
|
Phaser.Sprite.call(this, game, x, y, PIXI.Texture.fromCanvas(this.canvas));
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
this.texture.trim = new Phaser.Rectangle();
|
|
|
|
|
2015-06-17 12:21:28 +00:00
|
|
|
this.setStyle(style);
|
2014-02-15 01:27:42 +00:00
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
if (text !== '')
|
2014-09-04 01:18:12 +00:00
|
|
|
{
|
|
|
|
this.updateText();
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:02:47 +00:00
|
|
|
};
|
|
|
|
|
2015-06-17 12:21:28 +00:00
|
|
|
Phaser.Text.prototype = Object.create(Phaser.Sprite.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.
|
2014-11-25 13:00:15 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Text#preUpdate
|
2014-11-28 08:12:10 +00:00
|
|
|
* @protected
|
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
|
|
|
|
2015-02-25 02:49:50 +00:00
|
|
|
if (!this.preUpdatePhysics() || !this.preUpdateLifeSpan() || !this.preUpdateInWorld())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-15 01:27:42 +00:00
|
|
|
|
2015-02-25 02:49:50 +00:00
|
|
|
return this.preUpdateCore();
|
2013-12-16 15:16:44 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-09-17 15:28:59 +00:00
|
|
|
|
2014-02-14 01:09:52 +00:00
|
|
|
/**
|
2014-11-28 08:12:10 +00:00
|
|
|
* Override this function to handle any special update requirements.
|
2014-02-14 01:09:52 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Text#update
|
2014-11-28 08:12:10 +00:00
|
|
|
* @protected
|
2014-02-14 01:09:52 +00:00
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.update = function() {
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2014-02-14 01:09:52 +00:00
|
|
|
|
2013-10-17 14:40:44 +00:00
|
|
|
/**
|
2014-11-28 09:39:07 +00:00
|
|
|
* Destroy this Text object, removing it from the group it belongs to.
|
|
|
|
*
|
2014-11-25 13:00:15 +00:00
|
|
|
* @method Phaser.Text#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) {
|
|
|
|
|
2014-09-03 21:34:10 +00:00
|
|
|
this.texture.destroy(true);
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2015-02-18 14:54:11 +00:00
|
|
|
if (this.canvas && this.canvas.parentNode)
|
2013-10-17 14:40:44 +00:00
|
|
|
{
|
|
|
|
this.canvas.parentNode.removeChild(this.canvas);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.canvas = null;
|
|
|
|
this.context = null;
|
|
|
|
}
|
|
|
|
|
2015-02-17 06:00:41 +00:00
|
|
|
Phaser.Component.Destroy.prototype.destroy.call(this, destroyChildren);
|
2014-04-22 00:43:22 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2014-02-09 13:36:02 +00:00
|
|
|
|
|
|
|
/**
|
2014-12-01 21:06:00 +00:00
|
|
|
* Sets a drop shadow effect on the Text. You can specify the horizontal and vertical distance of the drop shadow with the `x` and `y` parameters.
|
|
|
|
* The color controls the shade of the shadow (default is black) and can be either an `rgba` or `hex` value.
|
|
|
|
* The blur is the strength of the shadow. A value of zero means a hard shadow, a value of 10 means a very soft shadow.
|
|
|
|
* To remove a shadow already in place you can call this method with no parameters set.
|
2014-11-25 13:00:15 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Text#setShadow
|
2014-02-09 13:36:02 +00:00
|
|
|
* @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.
|
2014-12-01 21:06:00 +00:00
|
|
|
* @param {string} [color='rgba(0,0,0,1)'] - The color of the shadow, as given in CSS rgba or hex format. Set the alpha component to 0 to disable the shadow.
|
2014-02-09 13:36:02 +00:00
|
|
|
* @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).
|
2015-04-27 15:22:21 +00:00
|
|
|
* @param {boolean} [shadowStroke=true] - Apply the drop shadow to the Text stroke (if set).
|
|
|
|
* @param {boolean} [shadowFill=true] - Apply the drop shadow to the Text fill (if set).
|
2015-06-17 16:18:04 +00:00
|
|
|
* @return {Phaser.Text} This Text instance.
|
2014-02-09 13:36:02 +00:00
|
|
|
*/
|
2015-04-27 15:22:21 +00:00
|
|
|
Phaser.Text.prototype.setShadow = function (x, y, color, blur, shadowStroke, shadowFill) {
|
2013-10-17 14:40:44 +00:00
|
|
|
|
2014-12-01 21:06:00 +00:00
|
|
|
if (typeof x === 'undefined') { x = 0; }
|
|
|
|
if (typeof y === 'undefined') { y = 0; }
|
|
|
|
if (typeof color === 'undefined') { color = 'rgba(0, 0, 0, 1)'; }
|
|
|
|
if (typeof blur === 'undefined') { blur = 0; }
|
2015-04-27 15:22:21 +00:00
|
|
|
if (typeof shadowStroke === 'undefined') { shadowStroke = true; }
|
|
|
|
if (typeof shadowFill === 'undefined') { shadowFill = true; }
|
2014-12-01 21:06:00 +00:00
|
|
|
|
|
|
|
this.style.shadowOffsetX = x;
|
|
|
|
this.style.shadowOffsetY = y;
|
|
|
|
this.style.shadowColor = color;
|
|
|
|
this.style.shadowBlur = blur;
|
2015-04-27 15:22:21 +00:00
|
|
|
this.style.shadowStroke = shadowStroke;
|
|
|
|
this.style.shadowFill = shadowFill;
|
2014-02-09 13:36:02 +00:00
|
|
|
this.dirty = true;
|
2013-10-17 14:40:44 +00:00
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
return this;
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
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.
|
|
|
|
*
|
2014-11-25 13:00:15 +00:00
|
|
|
* @method Phaser.Text#setStyle
|
2014-11-30 11:10:52 +00:00
|
|
|
* @param {object} [style] - The style properties to be set on the Text.
|
2014-09-18 15:58:25 +00:00
|
|
|
* @param {string} [style.font='bold 20pt Arial'] - The style and size of the font.
|
2014-11-28 09:39:07 +00:00
|
|
|
* @param {string} [style.fontStyle=(from font)] - The style of the font (eg. 'italic'): overrides the value in `style.font`.
|
|
|
|
* @param {string} [style.fontVariant=(from font)] - The variant of the font (eg. 'small-caps'): overrides the value in `style.font`.
|
|
|
|
* @param {string} [style.fontWeight=(from font)] - The weight of the font (eg. 'bold'): overrides the value in `style.font`.
|
|
|
|
* @param {string|number} [style.fontSize=(from font)] - The size of the font (eg. 32 or '32px'): overrides the value in `style.font`.
|
2015-03-23 16:12:23 +00:00
|
|
|
* @param {string} [style.backgroundColor=null] - A canvas fillstyle that will be used as the background for the whole Text object. Set to `null` to disable.
|
2014-09-18 15:58:25 +00:00
|
|
|
* @param {string} [style.fill='black'] - A canvas fillstyle that will be used on the text eg 'red', '#00FF00'.
|
2015-06-17 16:18:04 +00:00
|
|
|
* @param {string} [style.align='left'] - Horizontal alignment of each line in multiline text. Can be: 'left', 'center' or 'right'. Does not affect single lines of text (see `textBounds` and `boundsAlignH` for that).
|
|
|
|
* @param {string} [style.boundsAlignH='left'] - Horizontal alignment of the text within the `textBounds`. Can be: 'left', 'center' or 'right'.
|
|
|
|
* @param {string} [style.boundsAlignV='top'] - Vertical alignment of the text within the `textBounds`. Can be: 'top', 'middle' or 'bottom'.
|
2014-09-18 15:58:25 +00:00
|
|
|
* @param {string} [style.stroke='black'] - A canvas stroke style that will be used on the text stroke eg 'blue', '#FCFF00'.
|
|
|
|
* @param {number} [style.strokeThickness=0] - A number that represents the thickness of the stroke. Default is 0 (no stroke).
|
|
|
|
* @param {boolean} [style.wordWrap=false] - Indicates if word wrap should be used.
|
|
|
|
* @param {number} [style.wordWrapWidth=100] - The width in pixels at which text will wrap.
|
2015-06-17 16:18:04 +00:00
|
|
|
* @return {Phaser.Text} This Text instance.
|
2014-02-09 13:36:02 +00:00
|
|
|
*/
|
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';
|
2015-03-23 16:12:23 +00:00
|
|
|
style.backgroundColor = style.backgroundColor || null;
|
2014-02-09 13:36:02 +00:00
|
|
|
style.fill = style.fill || 'black';
|
|
|
|
style.align = style.align || 'left';
|
2015-06-17 16:18:04 +00:00
|
|
|
style.boundsAlignH = style.boundsAlignH || 'left';
|
|
|
|
style.boundsAlignV = style.boundsAlignV || 'top';
|
2014-02-09 13:36:02 +00:00
|
|
|
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;
|
|
|
|
|
2014-11-28 09:39:07 +00:00
|
|
|
var components = this.fontToComponents(style.font);
|
2015-02-17 06:00:41 +00:00
|
|
|
|
2014-11-28 09:39:07 +00:00
|
|
|
if (style.fontStyle)
|
|
|
|
{
|
|
|
|
components.fontStyle = style.fontStyle;
|
|
|
|
}
|
2015-02-17 06:00:41 +00:00
|
|
|
|
2014-11-28 09:39:07 +00:00
|
|
|
if (style.fontVariant)
|
|
|
|
{
|
|
|
|
components.fontVariant = style.fontVariant;
|
|
|
|
}
|
2015-02-17 06:00:41 +00:00
|
|
|
|
2014-11-28 09:39:07 +00:00
|
|
|
if (style.fontWeight)
|
|
|
|
{
|
|
|
|
components.fontWeight = style.fontWeight;
|
|
|
|
}
|
2015-02-17 06:00:41 +00:00
|
|
|
|
2014-11-28 09:39:07 +00:00
|
|
|
if (style.fontSize)
|
|
|
|
{
|
|
|
|
if (typeof style.fontSize === 'number')
|
|
|
|
{
|
|
|
|
style.fontSize = style.fontSize + 'px';
|
|
|
|
}
|
2015-02-17 06:00:41 +00:00
|
|
|
|
2014-11-28 09:39:07 +00:00
|
|
|
components.fontSize = style.fontSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._fontComponents = components;
|
|
|
|
|
|
|
|
style.font = this.componentsToFont(this._fontComponents);
|
2014-02-09 13:36:02 +00:00
|
|
|
this.style = style;
|
|
|
|
this.dirty = true;
|
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
return this;
|
|
|
|
|
2014-03-23 08:40:24 +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.
|
|
|
|
*
|
2014-11-25 13:00:15 +00:00
|
|
|
* @method Phaser.Text#updateText
|
2014-02-09 13:36:02 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-02-12 19:45:09 +00:00
|
|
|
Phaser.Text.prototype.updateText = function () {
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2014-10-22 21:35:21 +00:00
|
|
|
this.texture.baseTexture.resolution = this.resolution;
|
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
this.context.font = this.style.font;
|
|
|
|
|
|
|
|
var outputText = this.text;
|
|
|
|
|
2014-10-22 21:49:20 +00:00
|
|
|
if (this.style.wordWrap)
|
|
|
|
{
|
2014-10-23 15:03:29 +00:00
|
|
|
outputText = this.runWordWrap(this.text);
|
2014-10-22 21:49:20 +00:00
|
|
|
}
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2015-04-27 15:22:21 +00:00
|
|
|
// Split text into lines
|
2014-02-09 13:36:02 +00:00
|
|
|
var lines = outputText.split(/(?:\r\n|\r|\n)/);
|
|
|
|
|
2015-04-27 15:22:21 +00:00
|
|
|
// Calculate text width
|
2014-02-09 13:36:02 +00:00
|
|
|
var lineWidths = [];
|
|
|
|
var maxLineWidth = 0;
|
2014-10-22 21:35:21 +00:00
|
|
|
var fontProperties = this.determineFontProperties(this.style.font);
|
2014-10-22 21:49:20 +00:00
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
for (var i = 0; i < lines.length; i++)
|
|
|
|
{
|
2015-03-19 01:54:14 +00:00
|
|
|
var lineWidth = this.context.measureText(lines[i]).width + this.padding.x;
|
2014-02-09 13:36:02 +00:00
|
|
|
lineWidths[i] = lineWidth;
|
|
|
|
maxLineWidth = Math.max(maxLineWidth, lineWidth);
|
|
|
|
}
|
2014-03-13 16:49:52 +00:00
|
|
|
|
2014-10-22 21:35:21 +00:00
|
|
|
var width = maxLineWidth + this.style.strokeThickness;
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2014-10-28 11:48:57 +00:00
|
|
|
this.canvas.width = width * this.resolution;
|
2014-10-22 21:35:21 +00:00
|
|
|
|
2015-04-27 15:22:21 +00:00
|
|
|
// Calculate text height
|
2015-03-23 16:12:23 +00:00
|
|
|
var lineHeight = fontProperties.fontSize + this.style.strokeThickness + this.padding.y;
|
|
|
|
var height = lineHeight * lines.length;
|
|
|
|
var lineSpacing = this._lineSpacing;
|
2014-11-25 17:06:17 +00:00
|
|
|
|
2015-03-23 16:12:23 +00:00
|
|
|
if (lineSpacing < 0 && Math.abs(lineSpacing) > lineHeight)
|
|
|
|
{
|
|
|
|
lineSpacing = -lineHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust for line spacing
|
|
|
|
if (lineSpacing !== 0)
|
|
|
|
{
|
|
|
|
var diff = lineSpacing * (lines.length - 1);
|
|
|
|
height += diff;
|
|
|
|
}
|
2014-03-13 16:49:52 +00:00
|
|
|
|
2014-10-22 21:35:21 +00:00
|
|
|
this.canvas.height = height * this.resolution;
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2014-10-22 21:49:20 +00:00
|
|
|
this.context.scale(this.resolution, this.resolution);
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-10-22 21:49:20 +00:00
|
|
|
if (navigator.isCocoonJS)
|
|
|
|
{
|
|
|
|
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
|
}
|
2015-03-23 16:12:23 +00:00
|
|
|
|
|
|
|
if (this.style.backgroundColor)
|
|
|
|
{
|
|
|
|
this.context.fillStyle = this.style.backgroundColor;
|
|
|
|
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
|
}
|
2014-10-22 21:35:21 +00:00
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
this.context.fillStyle = this.style.fill;
|
|
|
|
this.context.font = this.style.font;
|
|
|
|
this.context.strokeStyle = this.style.stroke;
|
2014-10-22 21:35:21 +00:00
|
|
|
this.context.textBaseline = 'alphabetic';
|
2015-04-27 15:22:21 +00:00
|
|
|
|
2014-10-22 21:35:21 +00:00
|
|
|
this.context.lineWidth = this.style.strokeThickness;
|
2014-04-28 14:17:47 +00:00
|
|
|
this.context.lineCap = 'round';
|
2014-03-31 20:00:35 +00:00
|
|
|
this.context.lineJoin = 'round';
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2014-10-22 21:35:21 +00:00
|
|
|
var linePositionX;
|
|
|
|
var linePositionY;
|
|
|
|
|
2014-09-24 16:10:02 +00:00
|
|
|
this._charCount = 0;
|
|
|
|
|
2015-03-23 16:12:23 +00:00
|
|
|
// Draw text line by line
|
2014-02-09 13:36:02 +00:00
|
|
|
for (i = 0; i < lines.length; i++)
|
|
|
|
{
|
2014-10-22 21:35:21 +00:00
|
|
|
linePositionX = this.style.strokeThickness / 2;
|
|
|
|
linePositionY = (this.style.strokeThickness / 2 + i * lineHeight) + fontProperties.ascent;
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2015-03-23 16:12:23 +00:00
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
linePositionY += (lineSpacing * i);
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
if (this.style.align === 'right')
|
2014-02-09 13:36:02 +00:00
|
|
|
{
|
2014-10-22 21:35:21 +00:00
|
|
|
linePositionX += maxLineWidth - lineWidths[i];
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
2014-03-23 08:40:24 +00:00
|
|
|
else if (this.style.align === 'center')
|
2014-02-09 13:36:02 +00:00
|
|
|
{
|
2014-10-22 21:35:21 +00:00
|
|
|
linePositionX += (maxLineWidth - lineWidths[i]) / 2;
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 16:10:02 +00:00
|
|
|
if (this.colors.length > 0)
|
|
|
|
{
|
2014-10-22 21:35:21 +00:00
|
|
|
this.updateLine(lines[i], linePositionX, linePositionY);
|
2014-09-24 16:10:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.style.stroke && this.style.strokeThickness)
|
|
|
|
{
|
2015-04-27 15:22:21 +00:00
|
|
|
this.updateShadow(this.style.shadowStroke);
|
2014-10-22 21:35:21 +00:00
|
|
|
this.context.strokeText(lines[i], linePositionX, linePositionY);
|
2014-09-24 16:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.style.fill)
|
|
|
|
{
|
2015-04-27 15:22:21 +00:00
|
|
|
this.updateShadow(this.style.shadowFill);
|
2014-10-22 21:35:21 +00:00
|
|
|
this.context.fillText(lines[i], linePositionX, linePositionY);
|
2014-09-24 16:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateTexture();
|
2014-10-22 21:35:21 +00:00
|
|
|
|
2014-09-24 16:10:02 +00:00
|
|
|
};
|
|
|
|
|
2015-04-27 15:22:21 +00:00
|
|
|
/**
|
|
|
|
* Sets the Shadow on the Text.context based on the Style settings, or disables it if not enabled.
|
|
|
|
* This is called automatically by Text.updateText.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text#updateShadow
|
|
|
|
* @param {boolean} state - If true the shadow will be set to the Style values, otherwise it will be set to zero.
|
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.updateShadow = function (state) {
|
|
|
|
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
this.context.shadowOffsetX = this.style.shadowOffsetX;
|
|
|
|
this.context.shadowOffsetY = this.style.shadowOffsetY;
|
|
|
|
this.context.shadowColor = this.style.shadowColor;
|
|
|
|
this.context.shadowBlur = this.style.shadowBlur;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.context.shadowOffsetX = 0;
|
|
|
|
this.context.shadowOffsetY = 0;
|
|
|
|
this.context.shadowColor = 0;
|
|
|
|
this.context.shadowBlur = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-11-25 13:00:15 +00:00
|
|
|
/**
|
|
|
|
* Updates a line of text.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text#updateLine
|
|
|
|
* @private
|
|
|
|
*/
|
2014-09-24 16:10:02 +00:00
|
|
|
Phaser.Text.prototype.updateLine = function (line, x, y) {
|
|
|
|
|
|
|
|
for (var i = 0; i < line.length; i++)
|
|
|
|
{
|
|
|
|
var letter = line[i];
|
|
|
|
|
|
|
|
if (this.colors[this._charCount])
|
|
|
|
{
|
|
|
|
this.context.fillStyle = this.colors[this._charCount];
|
|
|
|
this.context.strokeStyle = this.colors[this._charCount];
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
if (this.style.stroke && this.style.strokeThickness)
|
2014-02-09 13:36:02 +00:00
|
|
|
{
|
2015-04-27 15:22:21 +00:00
|
|
|
this.updateShadow(this.style.shadowStroke);
|
2014-09-24 16:10:02 +00:00
|
|
|
this.context.strokeText(letter, x, y);
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
if (this.style.fill)
|
2014-02-09 13:36:02 +00:00
|
|
|
{
|
2015-04-27 15:22:21 +00:00
|
|
|
this.updateShadow(this.style.shadowFill);
|
2014-09-24 16:10:02 +00:00
|
|
|
this.context.fillText(letter, x, y);
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
2014-09-24 16:10:02 +00:00
|
|
|
|
|
|
|
x += this.context.measureText(letter).width;
|
|
|
|
|
|
|
|
this._charCount++;
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 16:10:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears any previously set color stops.
|
|
|
|
*
|
2014-11-25 13:00:15 +00:00
|
|
|
* @method Phaser.Text#clearColors
|
2015-06-17 16:18:04 +00:00
|
|
|
* @return {Phaser.Text} This Text instance.
|
2014-09-24 16:10:02 +00:00
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.clearColors = function () {
|
|
|
|
|
|
|
|
this.colors = [];
|
|
|
|
this.dirty = true;
|
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
return this;
|
|
|
|
|
2014-09-24 16:10:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-11-28 08:12:10 +00:00
|
|
|
* Set specific colors for certain characters within the Text.
|
|
|
|
*
|
2014-09-24 16:10:02 +00:00
|
|
|
* It works by taking a color value, which is a typical HTML string such as `#ff0000` or `rgb(255,0,0)` and a position.
|
|
|
|
* The position value is the index of the character in the Text string to start applying this color to.
|
|
|
|
* Once set the color remains in use until either another color or the end of the string is encountered.
|
|
|
|
* For example if the Text was `Photon Storm` and you did `Text.addColor('#ffff00', 6)` it would color in the word `Storm` in yellow.
|
|
|
|
*
|
2014-11-25 13:00:15 +00:00
|
|
|
* @method Phaser.Text#addColor
|
2014-09-24 16:10:02 +00:00
|
|
|
* @param {string} color - A canvas fillstyle that will be used on the text eg `red`, `#00FF00`, `rgba()`.
|
|
|
|
* @param {number} position - The index of the character in the string to start applying this color value from.
|
2015-06-17 16:18:04 +00:00
|
|
|
* @return {Phaser.Text} This Text instance.
|
2014-09-24 16:10:02 +00:00
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.addColor = function (color, position) {
|
|
|
|
|
|
|
|
this.colors[position] = color;
|
|
|
|
this.dirty = true;
|
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
return this;
|
|
|
|
|
2014-03-23 08:40:24 +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.
|
|
|
|
*
|
2014-11-25 13:00:15 +00:00
|
|
|
* @method Phaser.Text#runWordWrap
|
2014-09-18 15:58:25 +00:00
|
|
|
* @param {string} text - The text to perform word wrap detection against.
|
2014-02-09 13:36:02 +00:00
|
|
|
* @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-03-23 08:40:24 +00:00
|
|
|
};
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-11-28 10:56:44 +00:00
|
|
|
* Updates the internal `style.font` if it now differs according to generation from components.
|
2014-11-28 11:48:49 +00:00
|
|
|
*
|
2014-11-28 10:56:44 +00:00
|
|
|
* @method Phaser.Text#updateFont
|
|
|
|
* @private
|
2014-11-28 11:48:49 +00:00
|
|
|
* @param {object} components - Font components.
|
2014-11-28 10:56:44 +00:00
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.updateFont = function (components) {
|
|
|
|
|
|
|
|
var font = this.componentsToFont(components);
|
|
|
|
|
|
|
|
if (this.style.font !== font)
|
|
|
|
{
|
|
|
|
this.style.font = font;
|
|
|
|
this.dirty = true;
|
|
|
|
|
|
|
|
if (this.parent)
|
|
|
|
{
|
|
|
|
this.updateTransform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-11-28 09:39:07 +00:00
|
|
|
/**
|
|
|
|
* Converting a short CSS-font string into the relevant components.
|
2014-11-28 11:48:49 +00:00
|
|
|
*
|
2014-11-28 09:39:07 +00:00
|
|
|
* @method Phaser.Text#fontToComponents
|
|
|
|
* @private
|
2014-11-28 11:48:49 +00:00
|
|
|
* @param {string} font - a CSS font string
|
2014-11-28 09:39:07 +00:00
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.fontToComponents = function (font) {
|
|
|
|
|
|
|
|
// The format is specified in http://www.w3.org/TR/CSS2/fonts.html#font-shorthand:
|
|
|
|
// style - normal | italic | oblique | inherit
|
|
|
|
// variant - normal | small-caps | inherit
|
|
|
|
// weight - normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | inherit
|
|
|
|
// size - xx-small | x-small | small | medium | large | x-large | xx-large,
|
|
|
|
// larger | smaller
|
|
|
|
// {number} (em | ex | ch | rem | vh | vw | vmin | vmax | px | mm | cm | in | pt | pc | %)
|
|
|
|
// font-family - rest (but identifiers or quoted with comma separation)
|
|
|
|
var m = font.match(/^\s*(?:\b(normal|italic|oblique|inherit)?\b)\s*(?:\b(normal|small-caps|inherit)?\b)\s*(?:\b(normal|bold|bolder|lighter|100|200|300|400|500|600|700|800|900|inherit)?\b)\s*(?:\b(xx-small|x-small|small|medium|large|x-large|xx-large|larger|smaller|0|\d*(?:[.]\d*)?(?:%|[a-z]{2,5}))?\b)\s*(.*)\s*$/);
|
2015-02-16 17:22:51 +00:00
|
|
|
|
2014-11-28 09:39:07 +00:00
|
|
|
if (m)
|
|
|
|
{
|
|
|
|
return {
|
2014-11-28 11:48:49 +00:00
|
|
|
font: font,
|
2014-11-28 09:39:07 +00:00
|
|
|
fontStyle: m[1] || 'normal',
|
|
|
|
fontVariant: m[2] || 'normal',
|
|
|
|
fontWeight: m[3] || 'normal',
|
|
|
|
fontSize: m[4] || 'medium',
|
2014-11-28 11:48:49 +00:00
|
|
|
fontFamily: m[5]
|
2014-11-28 09:39:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-28 11:48:49 +00:00
|
|
|
console.warn("Phaser.Text - unparsable CSS font: " + font);
|
2014-11-28 09:39:07 +00:00
|
|
|
return {
|
|
|
|
font: font
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-11-28 11:48:49 +00:00
|
|
|
* Converts individual font components (see `fontToComponents`) to a short CSS font string.
|
|
|
|
*
|
2014-11-28 09:39:07 +00:00
|
|
|
* @method Phaser.Text#componentsToFont
|
|
|
|
* @private
|
2014-11-28 11:48:49 +00:00
|
|
|
* @param {object} components - Font components.
|
2014-11-28 09:39:07 +00:00
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.componentsToFont = function (components) {
|
|
|
|
|
|
|
|
var parts = [];
|
|
|
|
var v;
|
|
|
|
|
|
|
|
v = components.fontStyle;
|
|
|
|
if (v && v !== 'normal') { parts.push(v); }
|
|
|
|
|
|
|
|
v = components.fontVariant;
|
|
|
|
if (v && v !== 'normal') { parts.push(v); }
|
|
|
|
|
|
|
|
v = components.fontWeight;
|
|
|
|
if (v && v !== 'normal') { parts.push(v); }
|
|
|
|
|
|
|
|
v = components.fontSize;
|
|
|
|
if (v && v !== 'medium') { parts.push(v); }
|
|
|
|
|
2014-11-28 11:48:49 +00:00
|
|
|
v = components.fontFamily;
|
|
|
|
if (v) { parts.push(v); }
|
|
|
|
|
|
|
|
if (!parts.length)
|
|
|
|
{
|
|
|
|
// Fallback to whatever value the 'font' was
|
|
|
|
parts.push(components.font);
|
|
|
|
}
|
2014-11-28 09:39:07 +00:00
|
|
|
|
|
|
|
return parts.join(" ");
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2015-06-17 12:21:28 +00:00
|
|
|
* The text to be displayed by this Text object.
|
|
|
|
* Use a \n to insert a carriage return and split the text.
|
|
|
|
* The text will be rendered with any style currently set.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text#setText
|
|
|
|
* @param {string} [text] - The text to be displayed. Set to an empty string to clear text that is already present.
|
2015-06-17 16:18:04 +00:00
|
|
|
* @return {Phaser.Text} This Text instance.
|
2015-06-17 12:21:28 +00:00
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.setText = function (text) {
|
|
|
|
|
|
|
|
this.text = text.toString() || '';
|
|
|
|
this.dirty = true;
|
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
return this;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-06-24 18:57:40 +00:00
|
|
|
* The Text Bounds is a rectangular region that you control the dimensions of into which the Text object itself is positioned,
|
|
|
|
* regardless of the number of lines in the text, the font size or any other attribute.
|
2015-06-17 16:18:04 +00:00
|
|
|
*
|
2015-06-24 18:57:40 +00:00
|
|
|
* Alignment is controlled via the properties `boundsAlignH` and `boundsAlignV` within the Text.style object, or can be directly
|
|
|
|
* set through the setters `Text.boundsAlignH` and `Text.boundsAlignV`. Bounds alignment is independent of text alignment.
|
|
|
|
*
|
|
|
|
* For example: If your game is 800x600 in size and you set the text bounds to be 0,0,800,600 then by setting boundsAlignH to
|
|
|
|
* 'center' and boundsAlignV to 'bottom' the text will render in the center and at the bottom of your game window, regardless of
|
|
|
|
* how many lines of text there may be. Even if you adjust the text content or change the style it will remain at the bottom center
|
|
|
|
* of the text bounds.
|
|
|
|
*
|
|
|
|
* This is especially powerful when you need to align text against specific coordinates in your game, but the actual text dimensions
|
|
|
|
* may vary based on font (say for multi-lingual games).
|
|
|
|
*
|
2015-06-17 16:18:04 +00:00
|
|
|
* It works by calculating the final position based on the Text.canvas size, which is modified as the text is updated. Some fonts
|
|
|
|
* have additional padding around them which you can mitigate by tweaking the Text.padding property.
|
|
|
|
*
|
2015-06-24 18:57:40 +00:00
|
|
|
* If `Text.wordWrapWidth` is greater than the width of the text bounds it is clamped to match the bounds width.
|
2015-06-17 16:18:04 +00:00
|
|
|
*
|
2015-06-24 18:57:40 +00:00
|
|
|
* Call this method with no arguments given to reset an existing textBounds.
|
2015-06-17 16:18:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Text#setTextBounds
|
|
|
|
* @param {number} [x] - The x coordinate of the Text Bounds region.
|
|
|
|
* @param {number} [y] - The y coordinate of the Text Bounds region.
|
|
|
|
* @param {number} [width] - The width of the Text Bounds region.
|
|
|
|
* @param {number} [height] - The height of the Text Bounds region.
|
|
|
|
* @return {Phaser.Text} This Text instance.
|
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.setTextBounds = function (x, y, width, height) {
|
|
|
|
|
|
|
|
if (typeof x === 'undefined')
|
|
|
|
{
|
|
|
|
this.textBounds = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!this.textBounds)
|
|
|
|
{
|
|
|
|
this.textBounds = new Phaser.Rectangle(x, y, width, height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.textBounds.setTo(x, y, width, height);
|
|
|
|
}
|
2015-06-24 18:57:40 +00:00
|
|
|
|
|
|
|
if (this.style.wordWrapWidth > width)
|
|
|
|
{
|
|
|
|
this.style.wordWrapWidth = width;
|
|
|
|
}
|
2015-06-17 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.updateTexture();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
2015-06-17 12:21:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the texture based on the canvas dimensions.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text#updateTexture
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.updateTexture = function () {
|
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
var base = this.texture.baseTexture;
|
|
|
|
var crop = this.texture.crop;
|
|
|
|
var trim = this.texture.trim;
|
|
|
|
var frame = this.texture.frame;
|
|
|
|
|
|
|
|
var w = this.canvas.width;
|
|
|
|
var h = this.canvas.height;
|
2015-06-17 12:21:28 +00:00
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
base.width = w;
|
|
|
|
base.height = h;
|
|
|
|
|
|
|
|
crop.width = w;
|
|
|
|
crop.height = h;
|
|
|
|
|
|
|
|
trim.width = w;
|
|
|
|
trim.height = h;
|
|
|
|
|
|
|
|
frame.width = w;
|
|
|
|
frame.height = h;
|
|
|
|
|
|
|
|
this.texture.width = w;
|
|
|
|
this.texture.height = h;
|
|
|
|
|
|
|
|
this._width = w;
|
|
|
|
this._height = h;
|
|
|
|
|
|
|
|
if (this.textBounds)
|
|
|
|
{
|
|
|
|
var x = this.textBounds.x;
|
|
|
|
var y = this.textBounds.y;
|
|
|
|
|
|
|
|
// Align the canvas based on the bounds
|
|
|
|
if (this.style.boundsAlignH === 'right')
|
|
|
|
{
|
|
|
|
x = this.textBounds.width - this.canvas.width;
|
|
|
|
}
|
|
|
|
else if (this.style.boundsAlignH === 'center')
|
|
|
|
{
|
|
|
|
x = this.textBounds.halfWidth - (this.canvas.width / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.style.boundsAlignV === 'bottom')
|
|
|
|
{
|
|
|
|
y = this.textBounds.height - this.canvas.height;
|
|
|
|
}
|
|
|
|
else if (this.style.boundsAlignV === 'middle')
|
|
|
|
{
|
|
|
|
y = this.textBounds.halfHeight - (this.canvas.height / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
trim.x = x;
|
|
|
|
trim.y = y;
|
|
|
|
}
|
2015-06-17 12:21:28 +00:00
|
|
|
|
|
|
|
this.texture.baseTexture.dirty();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the object using the WebGL renderer
|
|
|
|
*
|
|
|
|
* @method Phaser.Text#_renderWebGL
|
|
|
|
* @private
|
|
|
|
* @param {RenderSession} renderSession - The Render Session to render the Text on.
|
|
|
|
*/
|
|
|
|
Phaser.Text.prototype._renderWebGL = function (renderSession) {
|
|
|
|
|
|
|
|
if (this.dirty)
|
|
|
|
{
|
|
|
|
this.resolution = renderSession.resolution;
|
|
|
|
|
|
|
|
this.updateText();
|
|
|
|
this.dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIXI.Sprite.prototype._renderWebGL.call(this, renderSession);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the object using the Canvas renderer.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text#_renderCanvas
|
|
|
|
* @private
|
|
|
|
* @param {RenderSession} renderSession - The Render Session to render the Text on.
|
|
|
|
*/
|
|
|
|
Phaser.Text.prototype._renderCanvas = function (renderSession) {
|
|
|
|
|
|
|
|
if (this.dirty)
|
|
|
|
{
|
|
|
|
this.resolution = renderSession.resolution;
|
|
|
|
|
|
|
|
this.updateText();
|
|
|
|
this.dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIXI.Sprite.prototype._renderCanvas.call(this, renderSession);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the ascent, descent and fontSize of a given font style.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text#determineFontProperties
|
|
|
|
* @private
|
|
|
|
* @param {object} fontStyle
|
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.determineFontProperties = function (fontStyle) {
|
|
|
|
|
|
|
|
var properties = Phaser.Text.fontPropertiesCache[fontStyle];
|
|
|
|
|
|
|
|
if (!properties)
|
|
|
|
{
|
|
|
|
properties = {};
|
|
|
|
|
|
|
|
var canvas = Phaser.Text.fontPropertiesCanvas;
|
|
|
|
var context = Phaser.Text.fontPropertiesContext;
|
|
|
|
|
|
|
|
context.font = fontStyle;
|
|
|
|
|
|
|
|
var width = Math.ceil(context.measureText('|MÉq').width);
|
|
|
|
var baseline = Math.ceil(context.measureText('|MÉq').width);
|
|
|
|
var height = 2 * baseline;
|
|
|
|
|
|
|
|
baseline = baseline * 1.4 | 0;
|
|
|
|
|
|
|
|
canvas.width = width;
|
|
|
|
canvas.height = height;
|
|
|
|
|
|
|
|
context.fillStyle = '#f00';
|
|
|
|
context.fillRect(0, 0, width, height);
|
|
|
|
|
|
|
|
context.font = fontStyle;
|
|
|
|
|
|
|
|
context.textBaseline = 'alphabetic';
|
|
|
|
context.fillStyle = '#000';
|
|
|
|
context.fillText('|MÉq', 0, baseline);
|
|
|
|
|
|
|
|
if (!context.getImageData(0, 0, width, height))
|
|
|
|
{
|
|
|
|
properties.ascent = baseline;
|
|
|
|
properties.descent = baseline + 6;
|
|
|
|
properties.fontSize = properties.ascent + properties.descent;
|
|
|
|
|
|
|
|
Phaser.Text.fontPropertiesCache[fontStyle] = properties;
|
|
|
|
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
var imagedata = context.getImageData(0, 0, width, height).data;
|
|
|
|
var pixels = imagedata.length;
|
|
|
|
var line = width * 4;
|
|
|
|
|
|
|
|
var i, j;
|
|
|
|
|
|
|
|
var idx = 0;
|
|
|
|
var stop = false;
|
|
|
|
|
|
|
|
// ascent. scan from top to bottom until we find a non red pixel
|
|
|
|
for (i = 0; i < baseline; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < line; j += 4)
|
|
|
|
{
|
|
|
|
if (imagedata[idx + j] !== 255)
|
|
|
|
{
|
|
|
|
stop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stop)
|
|
|
|
{
|
|
|
|
idx += line;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
properties.ascent = baseline - i;
|
|
|
|
|
|
|
|
idx = pixels - line;
|
|
|
|
stop = false;
|
|
|
|
|
|
|
|
// descent. scan from bottom to top until we find a non red pixel
|
|
|
|
for (i = height; i > baseline; i--)
|
|
|
|
{
|
|
|
|
for (j = 0; j < line; j += 4)
|
|
|
|
{
|
|
|
|
if (imagedata[idx + j] !== 255)
|
|
|
|
{
|
|
|
|
stop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stop)
|
|
|
|
{
|
|
|
|
idx -= line;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
properties.descent = i - baseline;
|
|
|
|
//TODO might need a tweak. kind of a temp fix!
|
|
|
|
properties.descent += 6;
|
|
|
|
properties.fontSize = properties.ascent + properties.descent;
|
|
|
|
|
|
|
|
Phaser.Text.fontPropertiesCache[fontStyle] = properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
return properties;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the bounds of the Text as a rectangle.
|
|
|
|
* The bounds calculation takes the worldTransform into account.
|
|
|
|
*
|
|
|
|
* @method Phaser.Text#getBounds
|
|
|
|
* @param {Phaser.Matrix} matrix - The transformation matrix of the Text.
|
|
|
|
* @return {Phaser.Rectangle} The framing rectangle
|
|
|
|
*/
|
|
|
|
Phaser.Text.prototype.getBounds = function (matrix) {
|
|
|
|
|
|
|
|
if (this.dirty)
|
|
|
|
{
|
|
|
|
this.updateText();
|
|
|
|
this.dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PIXI.Sprite.prototype.getBounds.call(this, matrix);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The text to be displayed by this Text object.
|
|
|
|
* Use a \n to insert a carriage return and split the text.
|
|
|
|
* The text will be rendered with any style currently set.
|
2014-11-28 11:48:49 +00:00
|
|
|
*
|
2014-02-09 13:36:02 +00:00
|
|
|
* @name Phaser.Text#text
|
2014-11-28 08:12:10 +00:00
|
|
|
* @property {string} text
|
2014-02-09 13:36:02 +00:00
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'text', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this._text;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this._text)
|
|
|
|
{
|
2015-07-01 22:37:39 +00:00
|
|
|
this._text = value.toString() || '';
|
2014-02-09 13:36:02 +00:00
|
|
|
this.dirty = true;
|
2014-09-24 00:10:36 +00:00
|
|
|
|
|
|
|
if (this.parent)
|
|
|
|
{
|
|
|
|
this.updateTransform();
|
|
|
|
}
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-11-28 11:48:49 +00:00
|
|
|
* Change the font used.
|
2014-11-28 10:56:44 +00:00
|
|
|
*
|
|
|
|
* This is equivalent of the `font` property specified to {@link Phaser.Text#setStyle setStyle}, except
|
|
|
|
* that unlike using `setStyle` this will not change any current font fill/color settings.
|
|
|
|
*
|
2015-02-16 17:22:51 +00:00
|
|
|
* The CSS font string can also be individually altered with the `font`, `fontSize`, `fontWeight`, `fontStyle`, and `fontVariant` properties.
|
2014-11-28 10:56:44 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Text#cssFont
|
|
|
|
* @property {string} cssFont
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'cssFont', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.componentsToFont(this._fontComponents);
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
value = value || 'bold 20pt Arial';
|
|
|
|
this._fontComponents = this.fontToComponents(value);
|
|
|
|
this.updateFont(this._fontComponents);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
/**
|
2014-11-28 11:48:49 +00:00
|
|
|
* Change the font family that the text will be rendered in, such as 'Arial'.
|
|
|
|
*
|
2015-02-16 17:22:51 +00:00
|
|
|
* Multiple CSS font families and generic fallbacks can be specified as long as
|
2014-11-28 11:48:49 +00:00
|
|
|
* {@link http://www.w3.org/TR/CSS2/fonts.html#propdef-font-family CSS font-family rules} are followed.
|
2014-11-28 09:39:07 +00:00
|
|
|
*
|
2014-11-28 11:48:49 +00:00
|
|
|
* To change the entire font string use {@link Phaser.Text#cssFont cssFont} instead: eg. `text.cssFont = 'bold 20pt Arial'`.
|
2014-11-28 09:39:07 +00:00
|
|
|
*
|
2014-02-09 13:36:02 +00:00
|
|
|
* @name Phaser.Text#font
|
2014-11-28 09:39:07 +00:00
|
|
|
* @property {string} font
|
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-11-28 11:48:49 +00:00
|
|
|
return this._fontComponents.fontFamily;
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2014-11-28 10:56:44 +00:00
|
|
|
value = value || 'Arial';
|
|
|
|
value = value.trim();
|
2014-09-24 00:10:36 +00:00
|
|
|
|
2014-11-28 11:48:49 +00:00
|
|
|
// If it looks like the value should be quoted, but isn't, then quote it.
|
|
|
|
if (!/^(?:inherit|serif|sans-serif|cursive|fantasy|monospace)$/.exec(value) && !/['",]/.exec(value))
|
2014-02-09 13:36:02 +00:00
|
|
|
{
|
2014-11-28 10:56:44 +00:00
|
|
|
value = "'" + value + "'";
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 11:48:49 +00:00
|
|
|
this._fontComponents.fontFamily = value;
|
2014-11-28 10:56:44 +00:00
|
|
|
this.updateFont(this._fontComponents);
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-11-28 09:39:07 +00:00
|
|
|
* The size of the font.
|
|
|
|
*
|
2014-11-28 11:48:49 +00:00
|
|
|
* If the font size is specified in pixels (eg. `32` or `'32px`') then a number (ie. `32`) representing
|
|
|
|
* the font size in pixels is returned; otherwise the value with CSS unit is returned as a string (eg. `'12pt'`).
|
2014-11-28 09:39:07 +00:00
|
|
|
*
|
2014-02-09 13:36:02 +00:00
|
|
|
* @name Phaser.Text#fontSize
|
2014-11-28 10:56:44 +00:00
|
|
|
* @property {number|string} fontSize
|
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-11-28 09:39:07 +00:00
|
|
|
|
|
|
|
var size = this._fontComponents.fontSize;
|
2015-02-16 17:22:51 +00:00
|
|
|
|
2014-11-28 10:56:44 +00:00
|
|
|
if (size && /(?:^0$|px$)/.exec(size))
|
2014-11-28 09:39:07 +00:00
|
|
|
{
|
|
|
|
return parseInt(size, 10);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-28 10:56:44 +00:00
|
|
|
return size;
|
2014-11-28 09:39:07 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2014-11-28 11:48:49 +00:00
|
|
|
value = value || '0';
|
2015-02-16 17:22:51 +00:00
|
|
|
|
2014-11-28 09:39:07 +00:00
|
|
|
if (typeof value === 'number')
|
2014-02-09 13:36:02 +00:00
|
|
|
{
|
2014-11-28 09:39:07 +00:00
|
|
|
value = value + 'px';
|
2014-02-09 13:36:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 10:56:44 +00:00
|
|
|
this._fontComponents.fontSize = value;
|
|
|
|
this.updateFont(this._fontComponents);
|
2014-02-09 13:36:02 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-11-28 10:56:44 +00:00
|
|
|
* The weight of the font: 'normal', 'bold', or {@link http://www.w3.org/TR/CSS2/fonts.html#propdef-font-weight a valid CSS font weight}.
|
2014-02-09 13:36:02 +00:00
|
|
|
* @name Phaser.Text#fontWeight
|
2014-11-28 10:56:44 +00:00
|
|
|
* @property {string} fontWeight
|
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-11-28 11:48:49 +00:00
|
|
|
return this._fontComponents.fontWeight || 'normal';
|
2013-09-20 12:55:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
2014-11-28 10:56:44 +00:00
|
|
|
value = value || 'normal';
|
|
|
|
this._fontComponents.fontWeight = value;
|
|
|
|
this.updateFont(this._fontComponents);
|
2014-09-24 00:10:36 +00:00
|
|
|
|
2014-11-28 10:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The style of the font: 'normal', 'italic', 'oblique'
|
|
|
|
* @name Phaser.Text#fontStyle
|
|
|
|
* @property {string} fontStyle
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'fontStyle', {
|
|
|
|
|
|
|
|
get: function() {
|
2014-11-28 11:48:49 +00:00
|
|
|
return this._fontComponents.fontStyle || 'normal';
|
2014-11-28 10:56:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
value = value || 'normal';
|
|
|
|
this._fontComponents.fontStyle = value;
|
|
|
|
this.updateFont(this._fontComponents);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The variant the font: 'normal', 'small-caps'
|
|
|
|
* @name Phaser.Text#fontVariant
|
|
|
|
* @property {string} fontVariant
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'fontVariant', {
|
|
|
|
|
|
|
|
get: function() {
|
2014-11-28 11:48:49 +00:00
|
|
|
return this._fontComponents.fontVariant || 'normal';
|
2014-11-28 10:56:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
value = value || 'normal';
|
|
|
|
this._fontComponents.fontVariant = value;
|
|
|
|
this.updateFont(this._fontComponents);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2015-06-17 16:18:04 +00:00
|
|
|
* Controls the horizontal alignment for multiline text.
|
|
|
|
* Can be: 'left', 'center' or 'right'.
|
|
|
|
* Does not affect single lines of text. For that please see `setTextBounds`.
|
2014-02-09 13:36:02 +00:00
|
|
|
* @name Phaser.Text#align
|
2015-06-17 16:18:04 +00:00
|
|
|
* @property {string} align
|
2014-02-09 13:36:02 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-06-17 16:18:04 +00:00
|
|
|
/**
|
|
|
|
* Horizontal alignment of the text within the `textBounds`. Can be: 'left', 'center' or 'right'.
|
|
|
|
* @name Phaser.Text#boundsAlignH
|
|
|
|
* @property {string} boundsAlignH
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'boundsAlignH', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.boundsAlignH;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.boundsAlignH)
|
|
|
|
{
|
|
|
|
this.style.boundsAlignH = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Vertical alignment of the text within the `textBounds`. Can be: 'top', 'middle' or 'bottom'.
|
|
|
|
* @name Phaser.Text#boundsAlignV
|
|
|
|
* @property {string} boundsAlignV
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'boundsAlignV', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.boundsAlignV;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.boundsAlignV)
|
|
|
|
{
|
|
|
|
this.style.boundsAlignV = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-02-09 13:36:02 +00:00
|
|
|
/**
|
|
|
|
* @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-09-24 00:10:36 +00:00
|
|
|
|
|
|
|
if (this.parent)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2015-04-27 15:22:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#shadowStroke
|
|
|
|
* @property {boolean} shadowStroke - Sets if the drop shadow is applied to the Text stroke.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'shadowStroke', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.shadowStroke;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.shadowStroke)
|
|
|
|
{
|
|
|
|
this.style.shadowStroke = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#shadowFill
|
|
|
|
* @property {boolean} shadowFill - Sets if the drop shadow is applied to the Text fill.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'shadowFill', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return this.style.shadowFill;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
if (value !== this.style.shadowFill)
|
|
|
|
{
|
|
|
|
this.style.shadowFill = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2015-06-17 12:21:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#width
|
|
|
|
* @property {number} width - The width of the Text. Setting this will modify the scale to achieve the value requested.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'width', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
|
|
|
|
if (this.dirty)
|
|
|
|
{
|
|
|
|
this.updateText();
|
|
|
|
this.dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.scale.x * this.texture.frame.width;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
this.scale.x = value / this.texture.frame.width;
|
|
|
|
this._width = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Text#height
|
|
|
|
* @property {number} height - The height of the Text. Setting this will modify the scale to achieve the value requested.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Text.prototype, 'height', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
|
|
|
|
if (this.dirty)
|
|
|
|
{
|
|
|
|
this.updateText();
|
|
|
|
this.dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.scale.y * this.texture.frame.height;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
this.scale.y = value / this.texture.frame.height;
|
|
|
|
this._height = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Phaser.Text.fontPropertiesCache = {};
|
|
|
|
|
|
|
|
Phaser.Text.fontPropertiesCanvas = document.createElement('canvas');
|
|
|
|
Phaser.Text.fontPropertiesContext = Phaser.Text.fontPropertiesCanvas.getContext('2d');
|