var Class = require('../../../utils/Class'); var Components = require('../../components'); var GameObject = require('../../GameObject'); var GetBitmapTextSize = require('../GetBitmapTextSize'); var ParseFromAtlas = require('../ParseFromAtlas'); var ParseXMLBitmapFont = require('../ParseXMLBitmapFont'); var Render = require('./BitmapTextRender'); var BitmapText = new Class({ Extends: GameObject, Mixins: [Components.Alpha, Components.BlendMode, Components.Depth, Components.Mask, Components.Origin, Components.Pipeline, Components.ScrollFactor, Components.Texture, Components.Tint, Components.Transform, Components.Visible, Render] , initialize: function BitmapText(scene, x, y, font, text, size, align){ if (text === undefined) { text = ''; } if (align === undefined) { align = 0; } GameObject.call(this, scene, 'BitmapText'); this.font = font; var entry = this.scene.sys.cache.bitmapFont.get(font); this.fontData = entry.data; this._text = ''; this._fontSize = size || this.fontData.size; this._letterSpacing = 0; this._align = align; this._bounds = GetBitmapTextSize(this, false , this._bounds); this._dirty = false ; this.setTexture(entry.texture, entry.frame); this.setPosition(x, y); this.setOrigin(0, 0); this.initPipeline(); this.setText(text); } , setLeftAlign: function (){ this._align = BitmapText.ALIGN_LEFT; this._dirty = true ; return this; } , setCenterAlign: function (){ this._align = BitmapText.ALIGN_CENTER; this._dirty = true ; return this; } , setRightAlign: function (){ this._align = BitmapText.ALIGN_RIGHT; this._dirty = true ; return this; } , setFontSize: function (size){ this._fontSize = size; this._dirty = true ; return this; } , setLetterSpacing: function (spacing){ if (spacing === undefined) { spacing = 0; } this._letterSpacing = spacing; this._dirty = true ; return this; } , setText: function (value){ if (!value && value !== 0) { value = ''; } if (Array.isArray(value)) { value = value.join('\n'); } if (value !== this.text) { this._text = value.toString(); this._dirty = true ; this.updateDisplayOrigin(); } return this; } , getTextBounds: function (round){ if (this._dirty) { GetBitmapTextSize(this, round, this._bounds); } return this._bounds; } , setFont: function (key, size, align){ if (size === undefined) { size = this._fontSize; } if (align === undefined) { align = this._align; } if (key !== this.font) { var entry = this.scene.sys.cache.bitmapFont.get(key); if (entry) { this.font = key; this.fontData = entry.data; this._fontSize = size; this._align = align; this.setTexture(entry.texture, entry.frame); GetBitmapTextSize(this, false , this._bounds); } } return this; } , align: { set: function (value){ this._align = value; this._dirty = true ; } , get: function (){ return this._align; } } , text: { set: function (value){ this.setText(value); } , get: function (){ return this._text; } } , fontSize: { set: function (value){ this._fontSize = value; this._dirty = true ; } , get: function (){ return this._fontSize; } } , letterSpacing: { set: function (value){ this._letterSpacing = value; this._dirty = true ; } , get: function (){ return this._letterSpacing; } } , width: { get: function (){ this.getTextBounds(false ); return this._bounds.global.width; } } , height: { get: function (){ this.getTextBounds(false ); return this._bounds.global.height; } } , toJSON: function (){ var out = Components.ToJSON(this); var data = { font: this.font, text: this.text, fontSize: this.fontSize, letterSpacing: this.letterSpacing, align: this.align} ; out.data = data; return out; } } ); BitmapText.ALIGN_LEFT = 0; BitmapText.ALIGN_CENTER = 1; BitmapText.ALIGN_RIGHT = 2; BitmapText.ParseFromAtlas = ParseFromAtlas; BitmapText.ParseXMLBitmapFont = ParseXMLBitmapFont; module.exports = BitmapText;