Phaser.BitmapText = function (game, x, y, font, text, size, align){ x = x || 0; y = y || 0; font = font || ''; text = text || ''; size = size || 32; align = align || 'left'; PIXI.DisplayObjectContainer.call(this); this.type = Phaser.BITMAPTEXT; this.physicsType = Phaser.SPRITE; this.textWidth = 0; this.textHeight = 0; this.anchor = new Phaser.Point(); this._prevAnchor = new Phaser.Point(); this._glyphs = [] ; this._maxWidth = 0; this._text = text.toString() || ''; this._data = game.cache.getBitmapFont(font); this._font = font; this._fontSize = size; this._align = align; this._tint = 16777215; this.updateText(); this.dirty = false ; Phaser.Component.Core.init.call(this, game, x, y, '', null ); } ; Phaser.BitmapText.prototype = Object.create(PIXI.DisplayObjectContainer.prototype); Phaser.BitmapText.prototype.constructor = Phaser.BitmapText; Phaser.Component.Core.install.call(Phaser.BitmapText.prototype, ['Angle', 'AutoCull', 'Bounds', 'Destroy', 'FixedToCamera', 'InputEnabled', 'InWorld', 'LifeSpan', 'PhysicsBody', 'Reset'] ); Phaser.BitmapText.prototype.preUpdatePhysics = Phaser.Component.PhysicsBody.preUpdate; Phaser.BitmapText.prototype.preUpdateLifeSpan = Phaser.Component.LifeSpan.preUpdate; Phaser.BitmapText.prototype.preUpdateInWorld = Phaser.Component.InWorld.preUpdate; Phaser.BitmapText.prototype.preUpdateCore = Phaser.Component.Core.preUpdate; Phaser.BitmapText.prototype.preUpdate = function (){ if (!this.preUpdatePhysics() || !this.preUpdateLifeSpan() || !this.preUpdateInWorld()) { return false ; } return this.preUpdateCore(); } ; Phaser.BitmapText.prototype.postUpdate = function (){ Phaser.Component.PhysicsBody.postUpdate.call(this); Phaser.Component.FixedToCamera.postUpdate.call(this); if (this.body && this.body.type === Phaser.Physics.ARCADE) { if ((this.textWidth !== this.body.sourceWidth) || (this.textHeight !== this.body.sourceHeight)) { this.body.setSize(this.textWidth, this.textHeight); } } } ; Phaser.BitmapText.prototype.setText = function (text){ _AN_Write_text('text', this, false , text); } ; Phaser.BitmapText.prototype.scanLine = function (data, scale, text){ var x = 0; var w = 0; var lastSpace = -1; var prevCharCode = null ; var maxWidth = (this._maxWidth > 0)? this._maxWidth: null ; var chars = [] ; for (var i = 0; i < _AN_Read_length('length', text); i++ ){ var end = (i === _AN_Read_length('length', text) - 1)? true : false ; if (/(?:\r\n|\r|\n)/.test(text.charAt(i))) { return { width: w, text: text.substr(0, i), end: end, chars: chars} ; } else { var charCode = text.charCodeAt(i); var charData = data.chars[charCode]; var c = 0; if (charData === undefined) { charCode = 32; charData = data.chars[charCode]; } var kerning = (prevCharCode && charData.kerning[prevCharCode])? charData.kerning[prevCharCode]: 0; lastSpace = /(\s)/.test(text.charAt(i))? i: lastSpace; c = (kerning + charData.texture.width + charData.xOffset) * scale; if (maxWidth && ((w + c) >= maxWidth) && lastSpace > -1) { return { width: w, text: text.substr(0, i - (i - lastSpace)), end: end, chars: chars} ; } else { w += (charData.xAdvance + kerning) * scale; chars.push(x + (charData.xOffset + kerning) * scale); x += (charData.xAdvance + kerning) * scale; prevCharCode = charCode; } } } return { width: w, text: text, end: end, chars: chars} ; } ; Phaser.BitmapText.prototype.cleanText = function (text, replace){ if (replace === undefined) { replace = ''; } var data = this._data.font; if (!data) { return ''; } var re = /\r\n|\n\r|\n|\r/g; var lines = _AN_Call_replace('replace', text, re, "\n").split("\n"); for (var i = 0; i < _AN_Read_length("length", lines); i++ ){ var output = ''; var line = lines[i]; for (var c = 0; c < _AN_Read_length('length', line); c++ ){ if (data.chars[line.charCodeAt(c)]) { output = output.concat(line[c]); } else { output = output.concat(replace); } } lines[i] = output; } return lines.join("\n"); } ; Phaser.BitmapText.prototype.updateText = function (){ var data = this._data.font; if (!data) { return ; } var text = this.text; var scale = this._fontSize / data.size; var lines = [] ; var y = 0; this.textWidth = 0; do { var line = this.scanLine(data, scale, text); line.y = y; lines.push(line); if (line.width > this.textWidth) { this.textWidth = line.width; } y += (data.lineHeight * scale); text = text.substr(_AN_Read_length("length", line.text) + 1); } while(line.end === false )this.textHeight = y; var t = 0; var align = 0; var ax = this.textWidth * this.anchor.x; var ay = this.textHeight * this.anchor.y; for (var i = 0; i < _AN_Read_length("length", lines); i++ ){ var line = lines[i]; if (this._align === 'right') { align = this.textWidth - line.width; } else if (this._align === 'center') { align = (this.textWidth - line.width) / 2; } for (var c = 0; c < _AN_Read_length('length', line.text); c++ ){ var charCode = line.text.charCodeAt(c); var charData = data.chars[charCode]; if (charData === undefined) { charCode = 32; charData = data.chars[charCode]; } var g = this._glyphs[t]; if (g) { g.texture = charData.texture; } else { g = new PIXI.Sprite(charData.texture); g.name = line.text[c]; this._glyphs.push(g); } g.position.x = (line.chars[c] + align) - ax; g.position.y = (line.y + (charData.yOffset * scale)) - ay; g.scale.set(scale); g.tint = this.tint; g.texture.requiresReTint = true ; if (!g.parent) { this.addChild(g); } t++ ; } } for (i = t; i < _AN_Read_length('length', this._glyphs); i++ ){ this.removeChild(this._glyphs[i]); } } ; Phaser.BitmapText.prototype.purgeGlyphs = function (){ var len = _AN_Read_length('length', this._glyphs); var kept = [] ; for (var i = 0; i < _AN_Read_length('length', this._glyphs); i++ ){ if (this._glyphs[i].parent !== this) { this._glyphs[i].destroy(); } else { kept.push(this._glyphs[i]); } } this._glyphs = [] ; this._glyphs = kept; this.updateText(); return len - _AN_Read_length('length', kept); } ; Phaser.BitmapText.prototype.updateTransform = function (){ if (this.dirty || !this.anchor.equals(this._prevAnchor)) { this.updateText(); this.dirty = false ; this._prevAnchor.copyFrom(this.anchor); } PIXI.DisplayObjectContainer.prototype.updateTransform.call(this); } ; Object.defineProperty(Phaser.BitmapText.prototype, 'align', { get: function (){ return this._align; } , set: function (value){ if (value !== this._align && (value === 'left' || value === 'center' || value === 'right')) { this._align = value; this.updateText(); } } } ); Object.defineProperty(Phaser.BitmapText.prototype, 'tint', { get: function (){ return this._tint; } , set: function (value){ if (value !== this._tint) { this._tint = value; this.updateText(); } } } ); Object.defineProperty(Phaser.BitmapText.prototype, 'font', { get: function (){ return this._font; } , set: function (value){ if (value !== this._font) { this._font = value.trim(); this._data = this.game.cache.getBitmapFont(this._font); this.updateText(); } } } ); Object.defineProperty(Phaser.BitmapText.prototype, 'fontSize', { get: function (){ return this._fontSize; } , set: function (value){ value = parseInt(value, 10); if (value !== this._fontSize && value > 0) { this._fontSize = value; this.updateText(); } } } ); Object.defineProperty(Phaser.BitmapText.prototype, 'text', { get: function (){ return this._text; } , set: function (value){ if (value !== this._text) { this._text = value.toString() || ''; this.updateText(); } } } ); Object.defineProperty(Phaser.BitmapText.prototype, 'maxWidth', { get: function (){ return this._maxWidth; } , set: function (value){ if (value !== this._maxWidth) { this._maxWidth = value; this.updateText(); } } } ); Object.defineProperty(Phaser.BitmapText.prototype, 'smoothed', { get: function (){ return !this._data.base.scaleMode; } , set: function (value){ if (value) { this._data.base.scaleMode = 0; } else { this._data.base.scaleMode = 1; } } } );