|
| 1 | +var GetValue = require('../../utils/object/GetValue'); |
| 2 | + |
| 3 | +// * @param {number} characterWidth - The width of each character in the font set. |
| 4 | +// * @param {number} characterHeight - The height of each character in the font set. |
| 5 | +// * @param {string} chars - The characters used in the font set, in display order. You can use the TEXT_SET consts for common font set arrangements. |
| 6 | +// * @param {number} [charsPerRow] - The number of characters per row in the font set. If not given charsPerRow will be the image width / characterWidth. |
| 7 | +// * @param {number} [xSpacing=0] - If the characters in the font set have horizontal spacing between them set the required amount here. |
| 8 | +// * @param {number} [ySpacing=0] - If the characters in the font set have vertical spacing between them set the required amount here. |
| 9 | +// * @param {number} [xOffset=0] - If the font set doesn't start at the top left of the given image, specify the X coordinate offset here. |
| 10 | +// * @param {number} [yOffset=0] - If the font set doesn't start at the top left of the given image, specify the Y coordinate offset here. |
| 11 | +// Phaser.GameObject.RetroFont = function (game, key, characterWidth, characterHeight, chars, charsPerRow, xSpacing, ySpacing, xOffset, yOffset) |
| 12 | + |
| 13 | +// { |
| 14 | +// image: key, |
| 15 | +// width: 32, |
| 16 | +// height: 32, |
| 17 | +// chars: 'string', |
| 18 | +// charsPerRow: null, |
| 19 | +// spacing: { x: 0, y: 0 }, |
| 20 | +// offset: { x: 0, y: 0 } |
| 21 | +// } |
| 22 | + |
| 23 | +var ParseRetroFont = function (state, config) |
| 24 | +{ |
| 25 | + var w = config.width; |
| 26 | + var h = config.height; |
| 27 | + var cx = Math.floor(w / 2); |
| 28 | + var cy = Math.floor(h / 2); |
| 29 | + var letters = config.chars; |
| 30 | + |
| 31 | + var key = GetValue(config, 'image', ''); |
| 32 | + var offsetX = GetValue(config, 'offset.x', 0); |
| 33 | + var offsetY = GetValue(config, 'offset.y', 0); |
| 34 | + var spacingX = GetValue(config, 'spacing.x', 0); |
| 35 | + var spacingY = GetValue(config, 'spacing.y', 0); |
| 36 | + |
| 37 | + var charsPerRow = GetValue(config, 'charsPerRow', null); |
| 38 | + |
| 39 | + if (charsPerRow === null) |
| 40 | + { |
| 41 | + charsPerRow = state.sys.textures.getFrame(key).width / w; |
| 42 | + } |
| 43 | + |
| 44 | + var x = offsetX; |
| 45 | + var y = offsetY; |
| 46 | + |
| 47 | + var data = { |
| 48 | + retroFont: true, |
| 49 | + font: key, |
| 50 | + size: w, |
| 51 | + lineHeight: h, |
| 52 | + chars: {} |
| 53 | + }; |
| 54 | + |
| 55 | + var r = 0; |
| 56 | + |
| 57 | + for (var i = 0; i < letters.length; i++) |
| 58 | + { |
| 59 | + var node = letters[i]; |
| 60 | + |
| 61 | + var charCode = letters.charCodeAt(i); |
| 62 | + |
| 63 | + data.chars[charCode] = |
| 64 | + { |
| 65 | + x: x, |
| 66 | + y: y, |
| 67 | + width: w, |
| 68 | + height: h, |
| 69 | + centerX: cx, |
| 70 | + centerY: cy, |
| 71 | + xOffset: 0, |
| 72 | + yOffset: 0, |
| 73 | + xAdvance: w, |
| 74 | + kerning: {} |
| 75 | + }; |
| 76 | + |
| 77 | + r++; |
| 78 | + |
| 79 | + if (r === charsPerRow) |
| 80 | + { |
| 81 | + r = 0; |
| 82 | + x = offsetX; |
| 83 | + y += h + spacingY; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + x += w + spacingX; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return data; |
| 92 | +}; |
| 93 | + |
| 94 | +ParseRetroFont.TEXT_SET1 = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; |
| 95 | + |
| 96 | +/** |
| 97 | +* Text Set 2 = !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ |
| 98 | +* @constant |
| 99 | +* @type {string} |
| 100 | +*/ |
| 101 | +ParseRetroFont.TEXT_SET2 = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 102 | + |
| 103 | +/** |
| 104 | +* Text Set 3 = ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 |
| 105 | +* @constant |
| 106 | +* @type {string} |
| 107 | +*/ |
| 108 | +ParseRetroFont.TEXT_SET3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "; |
| 109 | + |
| 110 | +/** |
| 111 | +* Text Set 4 = ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 |
| 112 | +* @constant |
| 113 | +* @type {string} |
| 114 | +*/ |
| 115 | +ParseRetroFont.TEXT_SET4 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789"; |
| 116 | + |
| 117 | +/** |
| 118 | +* Text Set 5 = ABCDEFGHIJKLMNOPQRSTUVWXYZ.,/() '!?-*:0123456789 |
| 119 | +* @constant |
| 120 | +* @type {string} |
| 121 | +*/ |
| 122 | +ParseRetroFont.TEXT_SET5 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,/() '!?-*:0123456789"; |
| 123 | + |
| 124 | +/** |
| 125 | +* Text Set 6 = ABCDEFGHIJKLMNOPQRSTUVWXYZ!?:;0123456789"(),-.' |
| 126 | +* @constant |
| 127 | +* @type {string} |
| 128 | +*/ |
| 129 | +ParseRetroFont.TEXT_SET6 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!?:;0123456789\"(),-.' "; |
| 130 | + |
| 131 | +/** |
| 132 | +* Text Set 7 = AGMSY+:4BHNTZ!;5CIOU.?06DJPV,(17EKQW")28FLRX-'39 |
| 133 | +* @constant |
| 134 | +* @type {string} |
| 135 | +*/ |
| 136 | +ParseRetroFont.TEXT_SET7 = "AGMSY+:4BHNTZ!;5CIOU.?06DJPV,(17EKQW\")28FLRX-'39"; |
| 137 | + |
| 138 | +/** |
| 139 | +* Text Set 8 = 0123456789 .ABCDEFGHIJKLMNOPQRSTUVWXYZ |
| 140 | +* @constant |
| 141 | +* @type {string} |
| 142 | +*/ |
| 143 | +ParseRetroFont.TEXT_SET8 = "0123456789 .ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 144 | + |
| 145 | +/** |
| 146 | +* Text Set 9 = ABCDEFGHIJKLMNOPQRSTUVWXYZ()-0123456789.:,'"?! |
| 147 | +* @constant |
| 148 | +* @type {string} |
| 149 | +*/ |
| 150 | +ParseRetroFont.TEXT_SET9 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ()-0123456789.:,'\"?!"; |
| 151 | + |
| 152 | +/** |
| 153 | +* Text Set 10 = ABCDEFGHIJKLMNOPQRSTUVWXYZ |
| 154 | +* @constant |
| 155 | +* @type {string} |
| 156 | +*/ |
| 157 | +ParseRetroFont.TEXT_SET10 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 158 | + |
| 159 | +/** |
| 160 | +* Text Set 11 = ABCDEFGHIJKLMNOPQRSTUVWXYZ.,"-+!?()':;0123456789 |
| 161 | +* @constant |
| 162 | +* @type {string} |
| 163 | +*/ |
| 164 | +ParseRetroFont.TEXT_SET11 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,\"-+!?()':;0123456789"; |
| 165 | + |
| 166 | +module.exports = ParseRetroFont; |
0 commit comments