recrownedathenaeum/RecrownedAthenaeum/Pipeline/TextureAtlasDataReader.cs

40 lines
1.7 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json;
using RecrownedAthenaeum.Pipeline.NinePatch;
using RecrownedAthenaeum.Pipeline.TextureAtlas;
using System.Text;
namespace RecrownedAthenaeum.Pipeline
{
class TextureAtlasDataReader : ContentTypeReader<DataTypes.TextureAtlas>
{
protected override DataTypes.TextureAtlas Read(ContentReader input, DataTypes.TextureAtlas existingInstance)
{
string serialized = input.ReadString();
TextureAtlasData atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(serialized);
DataTypes.TextureAtlas atlas;
Texture2D atlasTexture = input.ContentManager.Load<Texture2D>(atlasData.textureName);
DataTypes.TextureAtlas.TextureAtlasRegion[] regions = new DataTypes.TextureAtlas.TextureAtlasRegion[atlasData.regions.Length];
for (int regionID = 0; regionID < regions.Length; regionID++)
{
TextureAtlasData.TextureAtlasRegion regionData = atlasData.regions[regionID];
DataTypes.NinePatch nPatch = null;
if (regionData.ninePatchData != null)
{
NinePatchData nPatchData = regionData.ninePatchData;
nPatch = new DataTypes.NinePatch(atlasTexture, nPatchData.left, nPatchData.right, nPatchData.down, nPatchData.down);
}
regions[regionID] = new DataTypes.TextureAtlas.TextureAtlasRegion(regionData.name, regionData.location, nPatch, atlasTexture);
}
atlas = new DataTypes.TextureAtlas(atlasTexture, regions);
return atlas;
}
}
}