57 lines
2.5 KiB
C#
57 lines
2.5 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Content;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Newtonsoft.Json;
|
|
using RecrownedAthenaeum.Data;
|
|
using RecrownedAthenaeum.SpecialTypes;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace RecrownedAthenaeum.ContentReaders
|
|
{
|
|
class TextureAtlasDataReader : ContentTypeReader<TextureAtlas>
|
|
{
|
|
public static GraphicsDevice graphicsDevice;
|
|
|
|
protected override TextureAtlas Read(ContentReader input, TextureAtlas existingInstance)
|
|
{
|
|
if (graphicsDevice == null) graphicsDevice = Configuration.GraphicsDeviceManager.GraphicsDevice;
|
|
TextureAtlasData atlasData;
|
|
Texture2D atlasTexture;
|
|
using (MemoryStream stream = new MemoryStream(input.ReadBytes(input.ReadInt32())))
|
|
{
|
|
atlasTexture = Texture2D.FromStream(graphicsDevice, stream);
|
|
}
|
|
string serialized = Encoding.ASCII.GetString(input.ReadBytes(input.ReadInt32()));
|
|
atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(serialized);
|
|
TextureAtlas atlas = new TextureAtlas(atlasTexture, GenerateAtlasRegionsFromData(atlasData, atlasTexture));
|
|
|
|
return atlas;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates region given <see cref="TextureAtlasData"/>.
|
|
/// </summary>
|
|
/// <param name="textureAtlasData">The data to generate the regions from.</param>
|
|
/// <param name="atlasTexture">The texture containing the atlas.</param>
|
|
/// <returns>An array of regions.</returns>
|
|
public TextureAtlas.Region[] GenerateAtlasRegionsFromData(TextureAtlasData textureAtlasData, Texture2D atlasTexture)
|
|
{
|
|
TextureAtlas.Region[] regions = new TextureAtlas.Region[textureAtlasData.regions.Length];
|
|
for (int regionID = 0; regionID < regions.Length; regionID++)
|
|
{
|
|
TextureAtlasData.AtlasRegionData regionData = textureAtlasData.regions[regionID];
|
|
NinePatch nPatch = null;
|
|
if (regionData.ninePatchData != null)
|
|
{
|
|
NinePatchData nPatchData = regionData.ninePatchData;
|
|
nPatch = new NinePatch(atlasTexture, nPatchData.left, nPatchData.right, nPatchData.bottom, nPatchData.bottom, regionData.bounds);
|
|
}
|
|
regions[regionID] = new TextureAtlas.Region(regionData.name, regionData.bounds, nPatch, atlasTexture);
|
|
}
|
|
|
|
return regions;
|
|
}
|
|
}
|
|
}
|