2018-12-05 08:28:09 +00:00
|
|
|
|
using Microsoft.Xna.Framework.Content;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using Newtonsoft.Json;
|
2019-01-20 07:07:52 +00:00
|
|
|
|
using RecrownedAthenaeum.Data;
|
2019-01-16 01:33:22 +00:00
|
|
|
|
using RecrownedAthenaeum.SpecialTypes;
|
2019-01-21 05:06:56 +00:00
|
|
|
|
using System.IO;
|
2019-01-22 01:56:51 +00:00
|
|
|
|
using System.Text;
|
2018-12-05 08:28:09 +00:00
|
|
|
|
|
2019-01-21 03:45:15 +00:00
|
|
|
|
namespace RecrownedAthenaeum.ContentReaders
|
2018-12-05 08:28:09 +00:00
|
|
|
|
{
|
2019-01-16 01:33:22 +00:00
|
|
|
|
class TextureAtlasDataReader : ContentTypeReader<TextureAtlas>
|
2018-12-05 08:28:09 +00:00
|
|
|
|
{
|
2019-01-16 01:33:22 +00:00
|
|
|
|
protected override TextureAtlas Read(ContentReader input, TextureAtlas existingInstance)
|
2018-12-05 08:28:09 +00:00
|
|
|
|
{
|
2019-01-22 01:56:51 +00:00
|
|
|
|
TextureAtlasData atlasData;
|
|
|
|
|
Texture2D atlasTexture;
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(input.ReadBytes(input.ReadInt32())))
|
|
|
|
|
{
|
2019-01-22 04:18:22 +00:00
|
|
|
|
atlasTexture = Texture2D.FromStream(Configuration.graphicsDeviceManager.GraphicsDevice, stream);
|
2019-01-22 01:56:51 +00:00
|
|
|
|
}
|
|
|
|
|
string serialized = Encoding.ASCII.GetString(input.ReadBytes(input.ReadInt32()));
|
|
|
|
|
atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(serialized);
|
2019-01-16 01:33:22 +00:00
|
|
|
|
TextureAtlas atlas = new TextureAtlas(atlasTexture, GenerateAtlasRegionsFromData(atlasData, atlasTexture));
|
2018-12-05 08:28:09 +00:00
|
|
|
|
|
2019-01-16 01:33:22 +00:00
|
|
|
|
return atlas;
|
|
|
|
|
}
|
2018-12-05 08:28:09 +00:00
|
|
|
|
|
2019-01-21 05:06:56 +00:00
|
|
|
|
/// <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>
|
2019-01-16 01:33:22 +00:00
|
|
|
|
public TextureAtlas.Region[] GenerateAtlasRegionsFromData(TextureAtlasData textureAtlasData, Texture2D atlasTexture)
|
|
|
|
|
{
|
|
|
|
|
TextureAtlas.Region[] regions = new TextureAtlas.Region[textureAtlasData.regions.Length];
|
2018-12-05 08:28:09 +00:00
|
|
|
|
for (int regionID = 0; regionID < regions.Length; regionID++)
|
|
|
|
|
{
|
2019-01-16 01:33:22 +00:00
|
|
|
|
TextureAtlasData.AtlasRegionData regionData = textureAtlasData.regions[regionID];
|
|
|
|
|
NinePatch nPatch = null;
|
2018-12-05 08:28:09 +00:00
|
|
|
|
if (regionData.ninePatchData != null)
|
|
|
|
|
{
|
|
|
|
|
NinePatchData nPatchData = regionData.ninePatchData;
|
2019-01-22 01:56:51 +00:00
|
|
|
|
nPatch = new NinePatch(atlasTexture, nPatchData.left, nPatchData.right, nPatchData.bottom, nPatchData.bottom, regionData.bounds);
|
2018-12-05 08:28:09 +00:00
|
|
|
|
}
|
2019-01-22 01:56:51 +00:00
|
|
|
|
regions[regionID] = new TextureAtlas.Region(regionData.name, regionData.bounds, nPatch, atlasTexture);
|
2018-12-05 08:28:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 01:33:22 +00:00
|
|
|
|
return regions;
|
2018-12-05 08:28:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|