recrownedathenaeum/RecrownedAthenaeum/Pipeline/TextureAtlasDataReader.cs

38 lines
1.6 KiB
C#

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