2017-03-13 23:38:48 +00:00
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
var Class = require('../../../utils/Class');
|
|
|
|
var GameObject = require('../../GameObject');
|
|
|
|
var Components = require('../../../components');
|
|
|
|
var CanvasPool = require('../../../dom/CanvasPool');
|
2017-03-13 23:38:48 +00:00
|
|
|
var TextRender = require('./TextRender');
|
|
|
|
var TextStyle = require('../TextStyle');
|
2017-03-15 01:07:58 +00:00
|
|
|
var MeasureText = require('../MeasureText');
|
|
|
|
var GetTextSize = require('../GetTextSize');
|
2017-03-13 23:38:48 +00:00
|
|
|
|
|
|
|
var Text = new Class({
|
|
|
|
|
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
|
|
|
Components.GetBounds,
|
|
|
|
Components.Origin,
|
|
|
|
Components.ScaleMode,
|
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
|
|
|
TextRender
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
function Text (state, x, y, text, style)
|
2017-03-13 23:38:48 +00:00
|
|
|
{
|
2017-03-15 01:07:58 +00:00
|
|
|
if (x === undefined) { x = 0; }
|
|
|
|
if (y === undefined) { y = 0; }
|
|
|
|
if (text === undefined) { text = ''; }
|
|
|
|
|
2017-03-13 23:38:48 +00:00
|
|
|
GameObject.call(this, state);
|
|
|
|
|
|
|
|
this.setPosition(x, y);
|
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
/**
|
|
|
|
* @property {HTMLCanvasElement} canvas - The canvas element that the text is rendered.
|
|
|
|
*/
|
|
|
|
this.canvas = CanvasPool.create(this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {HTMLCanvasElement} context - The context of the canvas element that the text is rendered to.
|
|
|
|
*/
|
|
|
|
this.context = this.canvas.getContext('2d');
|
|
|
|
|
|
|
|
this.style = new TextStyle(this, style);
|
|
|
|
|
2017-03-15 23:08:59 +00:00
|
|
|
this.autoRound = true;
|
2017-03-15 01:07:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Regular Expression that is used to split the text up into lines, in
|
|
|
|
* multi-line text. By default this is `/(?:\r\n|\r|\n)/`.
|
|
|
|
* You can change this RegExp to be anything else that you may need.
|
|
|
|
* @property {Object} splitRegExp
|
|
|
|
*/
|
|
|
|
this.splitRegExp = /(?:\r\n|\r|\n)/;
|
|
|
|
|
|
|
|
this.text = text;
|
|
|
|
|
|
|
|
this.resolution = 1;
|
|
|
|
|
|
|
|
this.padding = { x: 0, y: 0 };
|
|
|
|
|
2017-03-15 23:08:59 +00:00
|
|
|
this.width = 1;
|
|
|
|
this.height = 1;
|
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
if (text !== '')
|
|
|
|
{
|
|
|
|
this.updateText();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-03-15 23:44:39 +00:00
|
|
|
setText: function (value)
|
|
|
|
{
|
|
|
|
if (value !== this.text)
|
|
|
|
{
|
|
|
|
this.text = value;
|
|
|
|
this.updateText();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-16 17:15:58 +00:00
|
|
|
setStyle: function (style)
|
|
|
|
{
|
|
|
|
return this.style.setStyle(style);
|
|
|
|
},
|
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
updateText: function ()
|
|
|
|
{
|
|
|
|
var canvas = this.canvas;
|
|
|
|
var context = this.context;
|
|
|
|
var style = this.style;
|
|
|
|
|
|
|
|
var size = MeasureText(style);
|
|
|
|
|
|
|
|
var outputText = this.text;
|
|
|
|
|
|
|
|
// if (style.wordWrap)
|
|
|
|
// {
|
|
|
|
// outputText = this.runWordWrap(this.text);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Split text into lines
|
|
|
|
var lines = outputText.split(this.splitRegExp);
|
2017-03-13 23:38:48 +00:00
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
var textSize = GetTextSize(this, size, lines);
|
|
|
|
|
2017-03-15 23:08:59 +00:00
|
|
|
this.width = textSize.width;
|
|
|
|
this.height = textSize.height;
|
|
|
|
|
2017-03-15 23:44:39 +00:00
|
|
|
this.updateOrigin();
|
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
canvas.width = textSize.width * this.resolution;
|
|
|
|
canvas.height = textSize.height * this.resolution;
|
|
|
|
|
|
|
|
if (style.backgroundColor)
|
|
|
|
{
|
|
|
|
context.fillStyle = style.backgroundColor;
|
|
|
|
context.fillRect(0, 0, canvas.width, canvas.height);
|
|
|
|
}
|
|
|
|
|
2017-03-16 17:15:58 +00:00
|
|
|
style.syncToCanvas(canvas, context);
|
2017-03-15 01:07:58 +00:00
|
|
|
|
|
|
|
var linePositionX;
|
|
|
|
var linePositionY;
|
|
|
|
|
|
|
|
// Draw text line by line
|
|
|
|
for (var i = 0; i < textSize.lines; i++)
|
|
|
|
{
|
|
|
|
linePositionX = style.strokeThickness / 2;
|
|
|
|
linePositionY = (style.strokeThickness / 2 + i * textSize.lineHeight) + size.ascent;
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
linePositionY += (textSize.lineSpacing * i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (style.align === 'right')
|
|
|
|
{
|
|
|
|
linePositionX += textSize.width - textSize.lineWidths[i];
|
|
|
|
}
|
|
|
|
else if (style.align === 'center')
|
|
|
|
{
|
|
|
|
linePositionX += (textSize.width - textSize.lineWidths[i]) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.autoRound)
|
|
|
|
{
|
|
|
|
linePositionX = Math.round(linePositionX);
|
|
|
|
linePositionY = Math.round(linePositionY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (style.strokeThickness)
|
|
|
|
{
|
|
|
|
this.updateShadow(style.shadowStroke);
|
|
|
|
|
|
|
|
context.strokeText(lines[i], linePositionX, linePositionY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (style.fill)
|
|
|
|
{
|
|
|
|
this.updateShadow(style.shadowFill);
|
|
|
|
|
|
|
|
context.fillText(lines[i], linePositionX, linePositionY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-03-16 17:16:32 +00:00
|
|
|
// Add style callback so we can chain filter effects
|
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
updateShadow: function (visible)
|
|
|
|
{
|
|
|
|
var context = this.context;
|
|
|
|
var style = this.style;
|
2017-03-13 23:38:48 +00:00
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
if (visible)
|
|
|
|
{
|
|
|
|
context.shadowOffsetX = style.shadowOffsetX;
|
|
|
|
context.shadowOffsetY = style.shadowOffsetY;
|
|
|
|
context.shadowColor = style.shadowColor;
|
|
|
|
context.shadowBlur = style.shadowBlur;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context.shadowOffsetX = 0;
|
|
|
|
context.shadowOffsetY = 0;
|
|
|
|
context.shadowColor = 0;
|
|
|
|
context.shadowBlur = 0;
|
|
|
|
}
|
2017-03-13 23:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Text;
|