Phaser.RetroFont = function (game, key, characterWidth, characterHeight, chars, charsPerRow, xSpacing, ySpacing, xOffset, yOffset){ if (!game.cache.checkImageKey(key)) { return false ; } if (charsPerRow === undefined || charsPerRow === null ) { charsPerRow = game.cache.getImage(key).width / characterWidth; } this.characterWidth = characterWidth; this.characterHeight = characterHeight; this.characterSpacingX = xSpacing || 0; this.characterSpacingY = ySpacing || 0; this.characterPerRow = charsPerRow; this.offsetX = xOffset || 0; this.offsetY = yOffset || 0; this.align = "left"; this.multiLine = false ; this.autoUpperCase = true ; this.customSpacingX = 0; this.customSpacingY = 0; this.fixedWidth = 0; this.fontSet = game.cache.getImage(key); this._text = ''; this.grabData = [] ; this.frameData = new Phaser.FrameData(); var currentX = this.offsetX; var currentY = this.offsetY; var r = 0; for (var c = 0; c < _AN_Read_length('length', chars); c++ ){ var frame = this.frameData.addFrame(new Phaser.Frame(c, currentX, currentY, this.characterWidth, this.characterHeight)); this.grabData[chars.charCodeAt(c)] = frame.index; r++ ; if (r === this.characterPerRow) { r = 0; currentX = this.offsetX; currentY += this.characterHeight + this.characterSpacingY; } else { currentX += this.characterWidth + this.characterSpacingX; } } game.cache.updateFrameData(key, this.frameData); this.stamp = new Phaser.Image(game, 0, 0, key, 0); Phaser.RenderTexture.call(this, game, 100, 100, '', Phaser.scaleModes.NEAREST); this.type = Phaser.RETROFONT; } ; Phaser.RetroFont.prototype = Object.create(Phaser.RenderTexture.prototype); Phaser.RetroFont.prototype.constructor = Phaser.RetroFont; Phaser.RetroFont.ALIGN_LEFT = "left"; Phaser.RetroFont.ALIGN_RIGHT = "right"; Phaser.RetroFont.ALIGN_CENTER = "center"; Phaser.RetroFont.TEXT_SET1 = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; Phaser.RetroFont.TEXT_SET2 = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Phaser.RetroFont.TEXT_SET3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "; Phaser.RetroFont.TEXT_SET4 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789"; Phaser.RetroFont.TEXT_SET5 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,/() '!?-*:0123456789"; Phaser.RetroFont.TEXT_SET6 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!?:;0123456789\"(),-.' "; Phaser.RetroFont.TEXT_SET7 = "AGMSY+:4BHNTZ!;5CIOU.?06DJPV,(17EKQW\")28FLRX-'39"; Phaser.RetroFont.TEXT_SET8 = "0123456789 .ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Phaser.RetroFont.TEXT_SET9 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ()-0123456789.:,'\"?!"; Phaser.RetroFont.TEXT_SET10 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Phaser.RetroFont.TEXT_SET11 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,\"-+!?()':;0123456789"; Phaser.RetroFont.prototype.setFixedWidth = function (width, lineAlignment){ if (lineAlignment === undefined) { lineAlignment = 'left'; } this.fixedWidth = width; this.align = lineAlignment; } ; Phaser.RetroFont.prototype.setText = function (content, multiLine, characterSpacing, lineSpacing, lineAlignment, allowLowerCase){ this.multiLine = multiLine || false ; this.customSpacingX = characterSpacing || 0; this.customSpacingY = lineSpacing || 0; this.align = lineAlignment || 'left'; if (allowLowerCase) { this.autoUpperCase = false ; } else { this.autoUpperCase = true ; } if (_AN_Read_length('length', content) > 0) { _AN_Write_text('text', this, false , content); } } ; Phaser.RetroFont.prototype.buildRetroFontText = function (){ var cx = 0; var cy = 0; _AN_Call_clear('clear', this); if (this.multiLine) { var lines = this._text.split("\n"); if (this.fixedWidth > 0) { this.resize(this.fixedWidth, (_AN_Read_length("length", lines) * (this.characterHeight + this.customSpacingY)) - this.customSpacingY, true ); } else { this.resize(this.getLongestLine() * (this.characterWidth + this.customSpacingX), (_AN_Read_length("length", lines) * (this.characterHeight + this.customSpacingY)) - this.customSpacingY, true ); } for (var i = 0; i < _AN_Read_length("length", lines); i++ ){ cx = 0; if (this.align === Phaser.RetroFont.ALIGN_RIGHT) { cx = this.width - (_AN_Read_length("length", lines[i]) * (this.characterWidth + this.customSpacingX)); } else if (this.align === Phaser.RetroFont.ALIGN_CENTER) { cx = (this.width / 2) - ((_AN_Read_length("length", lines[i]) * (this.characterWidth + this.customSpacingX)) / 2); cx += this.customSpacingX / 2; } if (cx < 0) { cx = 0; } this.pasteLine(lines[i], cx, cy, this.customSpacingX); cy += this.characterHeight + this.customSpacingY; } } else { if (this.fixedWidth > 0) { this.resize(this.fixedWidth, this.characterHeight, true ); } else { this.resize(_AN_Read_length("length", this._text) * (this.characterWidth + this.customSpacingX), this.characterHeight, true ); } cx = 0; if (this.align === Phaser.RetroFont.ALIGN_RIGHT) { cx = this.width - (_AN_Read_length("length", this._text) * (this.characterWidth + this.customSpacingX)); } else if (this.align === Phaser.RetroFont.ALIGN_CENTER) { cx = (this.width / 2) - ((_AN_Read_length("length", this._text) * (this.characterWidth + this.customSpacingX)) / 2); cx += this.customSpacingX / 2; } if (cx < 0) { cx = 0; } this.pasteLine(this._text, cx, 0, this.customSpacingX); } this.requiresReTint = true ; } ; Phaser.RetroFont.prototype.pasteLine = function (line, x, y, customSpacingX){ for (var c = 0; c < _AN_Read_length("length", line); c++ ){ if (line.charAt(c) === " ") { x += this.characterWidth + customSpacingX; } else { if (this.grabData[line.charCodeAt(c)] >= 0) { this.stamp.frame = this.grabData[line.charCodeAt(c)]; this.renderXY(this.stamp, x, y, false ); x += this.characterWidth + customSpacingX; if (x > this.width) { break ; } } } } } ; Phaser.RetroFont.prototype.getLongestLine = function (){ var longestLine = 0; if (_AN_Read_length("length", this._text) > 0) { var lines = this._text.split("\n"); for (var i = 0; i < _AN_Read_length("length", lines); i++ ){ if (_AN_Read_length("length", lines[i]) > longestLine) { longestLine = _AN_Read_length("length", lines[i]); } } } return longestLine; } ; Phaser.RetroFont.prototype.removeUnsupportedCharacters = function (stripCR){ var newString = ""; for (var c = 0; c < _AN_Read_length("length", this._text); c++ ){ var aChar = this._text[c]; var code = aChar.charCodeAt(0); if (this.grabData[code] >= 0 || (!stripCR && aChar === "\n")) { newString = newString.concat(aChar); } } return newString; } ; Phaser.RetroFont.prototype.updateOffset = function (x, y){ if (this.offsetX === x && this.offsetY === y) { return ; } var diffX = x - this.offsetX; var diffY = y - this.offsetY; var frames = this.game.cache.getFrameData(this.stamp.key).getFrames(); var i = _AN_Read_length("length", frames); while (i-- ){ frames[i].x += diffX; frames[i].y += diffY; } this.buildRetroFontText(); } ; Object.defineProperty(Phaser.RetroFont.prototype, "text", { get: function (){ return this._text; } , set: function (value){ var newText; if (this.autoUpperCase) { newText = value.toUpperCase(); } else { newText = value; } if (newText !== this._text) { this._text = newText; this.removeUnsupportedCharacters(this.multiLine); this.buildRetroFontText(); } } } ); Object.defineProperty(Phaser.RetroFont.prototype, "smoothed", { get: function (){ return this.stamp.smoothed; } , set: function (value){ this.stamp.smoothed = value; this.buildRetroFontText(); } } );