2019-02-25 04:44:02 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Content;
|
2018-12-05 08:28:09 +00:00
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
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;
|
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-03-21 00:28:16 +00:00
|
|
|
|
TextureAtlasData atlasData = ReadTextureAtlasData(input);
|
|
|
|
|
Texture2D atlasTexture = input.ContentManager.Load<Texture2D>(atlasData.textureName);
|
|
|
|
|
|
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-03-21 00:28:16 +00:00
|
|
|
|
public TextureAtlasData ReadTextureAtlasData(BinaryReader reader)
|
|
|
|
|
{
|
|
|
|
|
string textureName = reader.ReadString();
|
|
|
|
|
TextureAtlasData.AtlasRegionData[] regions = new TextureAtlasData.AtlasRegionData[reader.ReadInt32()];
|
|
|
|
|
for (int i = 0; i < regions.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
regions[i] = new TextureAtlasData.AtlasRegionData();
|
|
|
|
|
regions[i].name = reader.ReadString();
|
|
|
|
|
regions[i].bounds = new Rectangle(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
|
|
|
|
|
if (reader.ReadBoolean())
|
|
|
|
|
{
|
|
|
|
|
regions[i].ninePatchData = new NinePatchData(null, reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextureAtlasData atlasData = new TextureAtlasData(textureName, regions);
|
|
|
|
|
|
|
|
|
|
return atlasData;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|