/*
* 2016 Maciej Wiecierzewski
*/
/**
* @constructor
* @classdesc Multiline text
* @param {string} font
* @param {number} lineHeight
* @param {string} color
* @returns {Text}
*/
function Text(font, lineHeight, color)
{
this.font = font;
this.color = color;
this.lineHeight = lineHeight;
this.textStr = "";
this.width = 0;
this.fields = [];
this.container = new createjs.Container();
}
Text.prototype.appendTo = function(parentContainer)
{
parentContainer.addChild(this.container);
}
/**
* Resizes the text field
* @param {number} x
* @param {number} y
* @param {number} w
* @param {number} h
*/
Text.prototype.resize = function(x, y, w, h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.container.x = x;
this.container.y = y;
var width = w;
var wordX = 0;
var wordY = 0;
for(var i = 0; i < this.fields.length; i++)
{
var measuredWidth = this.fields[i].getMeasuredWidth();
if(wordX+this.space+measuredWidth > width)
{
wordX = 0;
wordY += this.lineHeight;
}
this.fields[i].x = wordX;
this.fields[i].y = wordY;
wordX += this.space+measuredWidth;
}
}
/** Sets new text */
Text.prototype.setText = function(textStr)
{
for(var i = 0; i < this.fields.length; i++)
{
this.container.removeChild(this.fields[i]);
delete this.fields[i];
}
this.fields = [];
var words = textStr.split(" ");
var width = this.width;
for(var i = 0; i < words.length; i++)
{
var textField = new createjs.Text(words[i], this.font, this.color);
this.container.addChild(textField);
this.fields.push(textField);
}
var textField = new createjs.Text(" ", this.font, this.color);
this.space = textField.getMeasuredWidth();
this.textStr = textStr;
this.resize(this.x, this.y, this.w, this.h);
}