phaser/src/textures/parsers/UnityYAML.js

165 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2020-01-15 12:07:09 +00:00
* @copyright 2020 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
var imageHeight = 0;
2018-03-16 17:29:39 +00:00
/**
* @function addFrame
* @private
* @since 3.0.0
*/
var addFrame = function (texture, sourceIndex, name, frame)
{
// The frame values are the exact coordinates to cut the frame out of the atlas from
var y = imageHeight - frame.y - frame.height;
2018-02-16 18:07:49 +00:00
texture.add(name, sourceIndex, frame.x, y, frame.width, frame.height);
// These are the original (non-trimmed) sprite values
/*
if (src.trimmed)
{
newFrame.setTrim(
src.sourceSize.w,
src.sourceSize.h,
src.spriteSourceSize.x,
src.spriteSourceSize.y,
src.spriteSourceSize.w,
src.spriteSourceSize.h
);
}
*/
};
2018-02-08 04:01:44 +00:00
/**
* Parses a Unity YAML File and creates Frames in the Texture.
* For more details about Sprite Meta Data see https://docs.unity3d.com/ScriptReference/SpriteMetaData.html
*
* @function Phaser.Textures.Parsers.UnityYAML
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Textures.Parsers
2018-04-16 15:37:07 +00:00
* @private
2018-02-08 04:01:44 +00:00
* @since 3.0.0
*
* @param {Phaser.Textures.Texture} texture - The Texture to add the Frames to.
2020-11-23 10:22:13 +00:00
* @param {number} sourceIndex - The index of the TextureSource.
2018-02-08 04:01:44 +00:00
* @param {object} yaml - The YAML data.
*
* @return {Phaser.Textures.Texture} The Texture modified by this parser.
*/
var UnityYAML = function (texture, sourceIndex, yaml)
{
// Add in a __BASE entry (for the entire atlas)
var source = texture.source[sourceIndex];
texture.add('__BASE', sourceIndex, 0, 0, source.width, source.height);
imageHeight = source.height;
var data = yaml.split('\n');
var lineRegExp = /^[ ]*(- )*(\w+)+[: ]+(.*)/;
var prevSprite = '';
var currentSprite = '';
var rect = { x: 0, y: 0, width: 0, height: 0 };
2018-02-16 18:17:51 +00:00
2018-02-16 18:07:49 +00:00
// var pivot = { x: 0, y: 0 };
// var border = { x: 0, y: 0, z: 0, w: 0 };
for (var i = 0; i < data.length; i++)
{
var results = data[i].match(lineRegExp);
if (!results)
{
continue;
}
var isList = (results[1] === '- ');
var key = results[2];
var value = results[3];
if (isList)
{
if (currentSprite !== prevSprite)
{
addFrame(texture, sourceIndex, currentSprite, rect);
prevSprite = currentSprite;
}
rect = { x: 0, y: 0, width: 0, height: 0 };
}
if (key === 'name')
{
// Start new list
currentSprite = value;
continue;
}
switch (key)
{
case 'x':
case 'y':
case 'width':
case 'height':
rect[key] = parseInt(value, 10);
break;
2018-02-16 18:07:49 +00:00
// case 'pivot':
// pivot = eval('var obj = ' + value);
// break;
2018-02-16 18:07:49 +00:00
// case 'border':
// border = eval('var obj = ' + value);
// break;
}
}
if (currentSprite !== prevSprite)
{
addFrame(texture, sourceIndex, currentSprite, rect);
}
return texture;
};
module.exports = UnityYAML;
2018-02-08 04:01:44 +00:00
/*
Example data:
TextureImporter:
spritePivot: {x: .5, y: .5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
spriteSheet:
sprites:
- name: asteroids_0
rect:
serializedVersion: 2
x: 5
y: 328
width: 65
height: 82
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
- name: asteroids_1
rect:
serializedVersion: 2
x: 80
y: 322
width: 53
height: 88
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
spritePackingTag: Asteroids
*/