Phaser.RetroFont = function (game, key, characterWidth, characterHeight, chars, charsPerRow, xSpacing, ySpacing, xOffset, yOffset){ if (!game.cache.checkImageKey(key)) { return false ; } if (typeof 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 = [] ; var currentX = this.offsetX; var currentY = this.offsetY; var r = 0; var data = new Phaser.FrameData(); for (var c = 0; c < _AN_Read_length('length', chars); c++ ){ var uuid = game.rnd.uuid(); var frame = data.addFrame(new Phaser.Frame(c, currentX, currentY, this.characterWidth, this.characterHeight, '', uuid)); this.grabData[chars.charCodeAt(c)] = frame.index; PIXI.TextureCache[uuid] = new PIXI.Texture(PIXI.BaseTextureCache[key], { x: currentX, y: currentY, width: this.characterWidth, height: this.characterHeight} ); 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, data); 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 (typeof 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++ ){ switch (this.align){ case Phaser.RetroFont.ALIGN_LEFT: cx = 0; break ; case Phaser.RetroFont.ALIGN_RIGHT: cx = this.width - (_AN_Read_length("length", lines[i]) * (this.characterWidth + this.customSpacingX)); break ; case Phaser.RetroFont.ALIGN_CENTER: cx = (this.width / 2) - ((_AN_Read_length("length", lines[i]) * (this.characterWidth + this.customSpacingX)) / 2); cx += this.customSpacingX / 2; break ; } 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 ); } switch (this.align){ case Phaser.RetroFont.ALIGN_LEFT: cx = 0; break ; case Phaser.RetroFont.ALIGN_RIGHT: cx = this.width - (_AN_Read_length("length", this._text) * (this.characterWidth + this.customSpacingX)); break ; case Phaser.RetroFont.ALIGN_CENTER: cx = (this.width / 2) - ((_AN_Read_length("length", this._text) * (this.characterWidth + this.customSpacingX)) / 2); cx += this.customSpacingX / 2; break ; } _AN_Call_clear("clear", this.textureBuffer); this.pasteLine(this._text, cx, 0, this.customSpacingX); } } ; Phaser.RetroFont.prototype.pasteLine = function (line, x, y, customSpacingX){ var p = new Phaser.Point(); 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)]; p.set(x, y); this.render(this.stamp, p, 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; PIXI.TextureCache[frames[i].uuid].frame.x = frames[i].x; PIXI.TextureCache[frames[i].uuid].frame.y = frames[i].y; } 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(); } } );