Phaser.Text = function (game, x, y, text, style){ x = x || 0; y = y || 0; text = text || ' '; style = style || { } ; if (_AN_Read_length('length', text) === 0) { text = ' '; } else { text = text.toString(); } this.game = game; this.exists = true ; this.name = ''; this.type = Phaser.TEXT; this.z = 0; this.world = new Phaser.Point(x, y); this._text = text; this._font = ''; this._fontSize = 32; this._fontWeight = 'normal'; this._lineSpacing = 0; this._charCount = 0; this.events = new Phaser.Events(this); this.input = null ; this.cameraOffset = new Phaser.Point(); this.colors = [] ; this.setStyle(style); PIXI.Text.call(this, text, this.style); this.position.set(x, y); this._cache = [0, 0, 0, 0, 1, 0, 1, 0, 0] ; if (text !== ' ') { this.updateText(); } } ; Phaser.Text.prototype = Object.create(PIXI.Text.prototype); Phaser.Text.prototype.constructor = Phaser.Text; Phaser.Text.prototype.preUpdate = function (){ this._cache[0] = this.world.x; this._cache[1] = this.world.y; this._cache[2] = this.rotation; if (!this.exists || !this.parent.exists) { this.renderOrderID = -1; return false ; } if (this.autoCull) { this.renderable = this.game.world.camera.screenView.intersects(this.getBounds()); } this.world.setTo(this.game.camera.x + this.worldTransform.tx, this.game.camera.y + this.worldTransform.ty); if (this.visible) { this._cache[3] = this.game.stage.currentRenderOrderID++ ; } for (var i = 0, len = _AN_Read_length('length', this.children); i < len; i++ ){ this.children[i].preUpdate(); } return true ; } ; Phaser.Text.prototype.update = function (){ } ; Phaser.Text.prototype.postUpdate = function (){ if (this._cache[7] === 1) { this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x; this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y; } for (var i = 0, len = _AN_Read_length('length', this.children); i < len; i++ ){ this.children[i].postUpdate(); } } ; Phaser.Text.prototype.destroy = function (destroyChildren){ if (this.game === null || this.destroyPhase) { return ; } if (typeof destroyChildren === 'undefined') { destroyChildren = true ; } this._cache[8] = 1; if (this.events) { this.events.onDestroy$dispatch(this); } if (this.parent) { if (this.parent instanceof Phaser.Group) { this.parent.remove(this); } else { this.parent.removeChild(this); } } this.texture.destroy(true ); if (this.canvas.parentNode) { this.canvas.parentNode.removeChild(this.canvas); } else { this.canvas = null ; this.context = null ; } var i = _AN_Read_length('length', this.children); if (destroyChildren) { while (i-- ){ this.children[i].destroy(destroyChildren); } } else { while (i-- ){ this.removeChild(this.children[i]); } } this.exists = false ; this.visible = false ; this.filters = null ; this.mask = null ; this.game = null ; this._cache[8] = 0; } ; Phaser.Text.prototype.setShadow = function (x, y, color, blur){ 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; } this.style.shadowOffsetX = x; this.style.shadowOffsetY = y; this.style.shadowColor = color; this.style.shadowBlur = blur; this.dirty = true ; } ; Phaser.Text.prototype.setStyle = function (style){ style = style || { } ; style.font = style.font || 'bold 20pt Arial'; style.fill = style.fill || 'black'; style.align = style.align || 'left'; style.stroke = style.stroke || 'black'; 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; this.style = style; this.dirty = true ; } ; Phaser.Text.prototype.updateText = function (){ this.texture.baseTexture.resolution = this.resolution; this.context.font = this.style.font; var outputText = this.text; if (this.style.wordWrap) { outputText = this.runWordWrap(this.text); } var lines = outputText.split(/(?:\r\n|\r|\n)/); var lineWidths = [] ; var maxLineWidth = 0; var fontProperties = this.determineFontProperties(this.style.font); for (var i = 0; i < _AN_Read_length('length', lines); i++ ){ var lineWidth = this.context.measureText(lines[i]).width; lineWidths[i] = lineWidth; maxLineWidth = Math.max(maxLineWidth, lineWidth); } var width = maxLineWidth + this.style.strokeThickness; this.canvas.width = width * this.resolution; var lineHeight = fontProperties.fontSize + this.style.strokeThickness + this._lineSpacing; var height = (lineHeight + this._lineSpacing) * _AN_Read_length('length', lines); this.canvas.height = height * this.resolution; this.context.scale(this.resolution, this.resolution); if (navigator.isCocoonJS) { this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); } this.context.fillStyle = this.style.fill; this.context.font = this.style.font; this.context.strokeStyle = this.style.stroke; this.context.textBaseline = 'alphabetic'; this.context.shadowOffsetX = this.style.shadowOffsetX; this.context.shadowOffsetY = this.style.shadowOffsetY; this.context.shadowColor = this.style.shadowColor; this.context.shadowBlur = this.style.shadowBlur; this.context.lineWidth = this.style.strokeThickness; this.context.lineCap = 'round'; this.context.lineJoin = 'round'; var linePositionX; var linePositionY; this._charCount = 0; for (i = 0; i < _AN_Read_length('length', lines); i++ ){ linePositionX = this.style.strokeThickness / 2; linePositionY = (this.style.strokeThickness / 2 + i * lineHeight) + fontProperties.ascent; if (this.style.align === 'right') { linePositionX += maxLineWidth - lineWidths[i]; } else if (this.style.align === 'center') { linePositionX += (maxLineWidth - lineWidths[i]) / 2; } if (_AN_Read_length('length', this.colors) > 0) { this.updateLine(lines[i], linePositionX, linePositionY); } else { if (this.style.stroke && this.style.strokeThickness) { this.context.strokeText(lines[i], linePositionX, linePositionY); } if (this.style.fill) { this.context.fillText(lines[i], linePositionX, linePositionY); } } } this.updateTexture(); } ; Phaser.Text.prototype.updateLine = function (line, x, y){ for (var i = 0; i < _AN_Read_length('length', line); i++ ){ var letter = line[i]; if (this.colors[this._charCount]) { this.context.fillStyle = this.colors[this._charCount]; this.context.strokeStyle = this.colors[this._charCount]; } if (this.style.stroke && this.style.strokeThickness) { this.context.strokeText(letter, x, y); } if (this.style.fill) { this.context.fillText(letter, x, y); } x += this.context.measureText(letter).width; this._charCount++ ; } } ; Phaser.Text.prototype.clearColors = function (){ this.colors = [] ; this.dirty = true ; } ; Phaser.Text.prototype.addColor = function (color, position){ this.colors[position] = color; this.dirty = true ; } ; Phaser.Text.prototype.runWordWrap = function (text){ var result = ''; var lines = text.split('\n'); for (var i = 0; i < _AN_Read_length('length', lines); i++ ){ var spaceLeft = this.style.wordWrapWidth; var words = lines[i].split(' '); for (var j = 0; j < _AN_Read_length('length', words); j++ ){ var wordWidth = this.context.measureText(words[j]).width; var wordWidthWithSpace = wordWidth + this.context.measureText(' ').width; if (wordWidthWithSpace > spaceLeft) { if (j > 0) { result += '\n'; } result += words[j] + ' '; spaceLeft = this.style.wordWrapWidth - wordWidth; } else { spaceLeft -= wordWidthWithSpace; result += words[j] + ' '; } } if (i < _AN_Read_length('length', lines) - 1) { result += '\n'; } } return result; } ; Object.defineProperty(Phaser.Text.prototype, 'angle', { get: function (){ return Phaser.Math.radToDeg(this.rotation); } , set: function (value){ this.rotation = Phaser.Math.degToRad(value); } } ); Object.defineProperty(Phaser.Text.prototype, 'text', { get: function (){ return this._text; } , set: function (value){ if (value !== this._text) { this._text = value.toString() || ' '; this.dirty = true ; if (this.parent) { this.updateTransform(); } } } } ); Object.defineProperty(Phaser.Text.prototype, 'font', { get: function (){ return this._font; } , set: function (value){ if (value !== this._font) { this._font = value.trim(); this.style.font = this._fontWeight + ' ' + this._fontSize + "px '" + this._font + "'"; this.dirty = true ; if (this.parent) { this.updateTransform(); } } } } ); Object.defineProperty(Phaser.Text.prototype, 'fontSize', { get: function (){ return this._fontSize; } , set: function (value){ value = parseInt(value, 10); if (value !== this._fontSize) { this._fontSize = value; this.style.font = this._fontWeight + ' ' + this._fontSize + "px '" + this._font + "'"; this.dirty = true ; if (this.parent) { this.updateTransform(); } } } } ); Object.defineProperty(Phaser.Text.prototype, 'fontWeight', { get: function (){ return this._fontWeight; } , set: function (value){ if (value !== this._fontWeight) { this._fontWeight = value; this.style.font = this._fontWeight + ' ' + this._fontSize + "px '" + this._font + "'"; this.dirty = true ; if (this.parent) { this.updateTransform(); } } } } ); 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 ; } } } ); 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 ; } } } ); 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 ; } } } ); 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 ; } } } ); 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 ; } } } ); 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 ; } } } ); 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 ; if (this.parent) { this.updateTransform(); } } } } ); 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 ; } } } ); 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 ; } } } ); 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 ; } } } ); Object.defineProperty(Phaser.Text.prototype, 'shadowBlur', { get: function (){ return this.style.shadowBlur; } , set: function (value){ if (value !== this.style.shadowBlur) { this.style.shadowBlur = value; this.dirty = true ; } } } ); Object.defineProperty(Phaser.Text.prototype, "inputEnabled", { get: function (){ return (this.input && this.input.enabled); } , set: function (value){ if (value) { if (this.input === null ) { this.input = new Phaser.InputHandler(this); this.input.start(); } else if (this.input && !this.input.enabled) { this.input.start(); } } else { if (this.input && this.input.enabled) { this.input.stop(); } } } } ); Object.defineProperty(Phaser.Text.prototype, "fixedToCamera", { get: function (){ return !!this._cache[7]; } , set: function (value){ if (value) { this._cache[7] = 1; this.cameraOffset.set(this.x, this.y); } else { this._cache[7] = 0; } } } ); Object.defineProperty(Phaser.Text.prototype, "destroyPhase", { get: function (){ return !!this._cache[8]; } } );