ParseXMLBitmapFont has a new optional parameter texture. If defined, this Texture is populated with Frame data, one frame per glyph. This happens automatically when loading Bitmap Text data in Phaser.

This commit is contained in:
Richard Davey 2020-08-05 12:46:28 +01:00
parent d98d305e2f
commit 96a64eae4e
3 changed files with 26 additions and 8 deletions

View file

@ -27,12 +27,13 @@ var ParseXMLBitmapFont = require('./ParseXMLBitmapFont');
*/ */
var ParseFromAtlas = function (scene, fontName, textureKey, frameKey, xmlKey, xSpacing, ySpacing) var ParseFromAtlas = function (scene, fontName, textureKey, frameKey, xmlKey, xSpacing, ySpacing)
{ {
var frame = scene.sys.textures.getFrame(textureKey, frameKey); var texture = scene.sys.textures.get(textureKey);
var frame = texture.get(frameKey);
var xml = scene.sys.cache.xml.get(xmlKey); var xml = scene.sys.cache.xml.get(xmlKey);
if (frame && xml) if (frame && xml)
{ {
var data = ParseXMLBitmapFont(xml, frame, xSpacing, ySpacing); var data = ParseXMLBitmapFont(xml, frame, xSpacing, ySpacing, texture);
scene.sys.cache.bitmapFont.add(fontName, { data: data, texture: textureKey, frame: frameKey }); scene.sys.cache.bitmapFont.add(fontName, { data: data, texture: textureKey, frame: frameKey });

View file

@ -32,10 +32,11 @@ function getValue (node, attribute)
* @param {Phaser.Textures.Frame} frame - The texture frame to take into account when creating the uv data. * @param {Phaser.Textures.Frame} frame - The texture frame to take into account when creating the uv data.
* @param {integer} [xSpacing=0] - The x-axis spacing to add between each letter. * @param {integer} [xSpacing=0] - The x-axis spacing to add between each letter.
* @param {integer} [ySpacing=0] - The y-axis spacing to add to the line height. * @param {integer} [ySpacing=0] - The y-axis spacing to add to the line height.
* @param {Phaser.Textures.Texture} [texture] - If provided, each glyph in the Bitmap Font will be added to this texture as a frame.
* *
* @return {Phaser.Types.GameObjects.BitmapText.BitmapFontData} The parsed Bitmap Font data. * @return {Phaser.Types.GameObjects.BitmapText.BitmapFontData} The parsed Bitmap Font data.
*/ */
var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing) var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing, texture)
{ {
if (xSpacing === undefined) { xSpacing = 0; } if (xSpacing === undefined) { xSpacing = 0; }
if (ySpacing === undefined) { ySpacing = 0; } if (ySpacing === undefined) { ySpacing = 0; }
@ -44,6 +45,7 @@ var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing)
var textureY = frame.cutY; var textureY = frame.cutY;
var textureWidth = frame.source.width; var textureWidth = frame.source.width;
var textureHeight = frame.source.height; var textureHeight = frame.source.height;
var sourceIndex = frame.sourceIndex;
var data = {}; var data = {};
var info = xml.getElementsByTagName('info')[0]; var info = xml.getElementsByTagName('info')[0];
@ -69,6 +71,7 @@ var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing)
var node = letters[i]; var node = letters[i];
var charCode = getValue(node, 'id'); var charCode = getValue(node, 'id');
var letter = String.fromCharCode(charCode);
var gx = getValue(node, 'x'); var gx = getValue(node, 'x');
var gy = getValue(node, 'y'); var gy = getValue(node, 'y');
var gw = getValue(node, 'width'); var gw = getValue(node, 'width');
@ -98,6 +101,11 @@ var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing)
gy -= frame.y; gy -= frame.y;
} }
var u0 = (textureX + gx) / textureWidth;
var v0 = (textureY + gy) / textureHeight;
var u1 = (textureX + gx + gw) / textureWidth;
var v1 = (textureY + gy + gh) / textureHeight;
data.chars[charCode] = data.chars[charCode] =
{ {
x: gx, x: gx,
@ -111,11 +119,18 @@ var ParseXMLBitmapFont = function (xml, frame, xSpacing, ySpacing)
xAdvance: getValue(node, 'xadvance') + xSpacing, xAdvance: getValue(node, 'xadvance') + xSpacing,
data: {}, data: {},
kerning: {}, kerning: {},
u0: (textureX + gx) / textureWidth, u0: u0,
v0: (textureY + gy) / textureHeight, v0: v0,
u1: (textureX + gx + gw) / textureWidth, u1: u1,
v1: (textureY + gy + gh) / textureHeight v1: v1
}; };
if (texture)
{
var charFrame = texture.add(letter, sourceIndex, gx, gy, gw, gh);
charFrame.setUVs(gw, gh, u0, v0, u1, v1);
}
} }
var kernings = xml.getElementsByTagName('kerning'); var kernings = xml.getElementsByTagName('kerning');

View file

@ -99,7 +99,9 @@ var BitmapFontFile = new Class({
image.addToCache(); image.addToCache();
xml.addToCache(); xml.addToCache();
var data = ParseXMLBitmapFont(xml.data, image.cache.getFrame(image.key)); var texture = image.cache.get(image.key);
var data = ParseXMLBitmapFont(xml.data, image.cache.getFrame(image.key), 0, 0, texture);
this.loader.cacheManager.bitmapFont.add(image.key, { data: data, texture: image.key, frame: null }); this.loader.cacheManager.bitmapFont.add(image.key, { data: data, texture: image.key, frame: null });