2018-12-05 01:19:31 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2018-12-05 08:28:09 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2018-12-05 01:19:31 +00:00
|
|
|
|
using RecrownedAthenaeum.DataTypes;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace RecrownedAthenaeum.DataTypes
|
|
|
|
|
{
|
|
|
|
|
public class TextureAtlas
|
|
|
|
|
{
|
2018-12-05 08:28:09 +00:00
|
|
|
|
[JsonIgnore]
|
2018-12-05 01:19:31 +00:00
|
|
|
|
private Texture2D texture;
|
2018-12-05 08:28:09 +00:00
|
|
|
|
|
2018-12-05 01:19:31 +00:00
|
|
|
|
private Dictionary<string, TextureAtlasRegion> dictionaryOfRegions = new Dictionary<string, TextureAtlasRegion>();
|
|
|
|
|
|
|
|
|
|
public TextureAtlas(Texture2D texture, TextureAtlasRegion[] regions)
|
|
|
|
|
{
|
|
|
|
|
this.texture = texture;
|
|
|
|
|
foreach (TextureAtlasRegion region in regions)
|
|
|
|
|
{
|
|
|
|
|
dictionaryOfRegions.Add(region.name, region);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 08:28:09 +00:00
|
|
|
|
public TextureAtlas(Texture2D texture, Dictionary<string, TextureAtlasRegion> dictionaryOfRegions)
|
|
|
|
|
{
|
|
|
|
|
this.texture = texture;
|
|
|
|
|
this.dictionaryOfRegions = dictionaryOfRegions;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:19:31 +00:00
|
|
|
|
public void Draw(string name, SpriteBatch batch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = new Vector2())
|
|
|
|
|
{
|
2018-12-05 08:28:09 +00:00
|
|
|
|
dictionaryOfRegions[name].Draw(batch, destination, color, rotation, origin);
|
2018-12-05 01:19:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Texture2D TextureOfRegion(string name, GraphicsDevice graphicsDevice)
|
|
|
|
|
{
|
|
|
|
|
return dictionaryOfRegions[name].AsTexture2D(graphicsDevice, texture);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 06:03:00 +00:00
|
|
|
|
public class TextureAtlasRegion : IComparable<TextureAtlasRegion>, IDisposable
|
2018-12-05 01:19:31 +00:00
|
|
|
|
{
|
2018-12-05 06:03:00 +00:00
|
|
|
|
public readonly string name;
|
2018-12-05 08:28:09 +00:00
|
|
|
|
public readonly Rectangle sourceRectangle;
|
2018-12-05 06:03:00 +00:00
|
|
|
|
readonly NinePatch ninepatch;
|
2018-12-05 08:28:09 +00:00
|
|
|
|
Texture2D atlasTexture;
|
2018-12-05 01:19:31 +00:00
|
|
|
|
Texture2D regionTexture;
|
|
|
|
|
|
2018-12-05 08:28:09 +00:00
|
|
|
|
public TextureAtlasRegion(string name, Rectangle source, NinePatch ninePatch, Texture2D atlasTexture)
|
2018-12-05 06:03:00 +00:00
|
|
|
|
{
|
2018-12-05 08:28:09 +00:00
|
|
|
|
this.atlasTexture = atlasTexture;
|
2018-12-05 06:03:00 +00:00
|
|
|
|
this.name = name;
|
|
|
|
|
this.sourceRectangle = source;
|
|
|
|
|
this.ninepatch = ninePatch;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 08:28:09 +00:00
|
|
|
|
public void Draw(SpriteBatch batch, Rectangle destination, Color color, float rotation, Vector2 origin)
|
2018-12-05 01:19:31 +00:00
|
|
|
|
{
|
2018-12-05 08:28:09 +00:00
|
|
|
|
batch.Draw(atlasTexture, destination, sourceRectangle, color, rotation, origin, SpriteEffects.None, 0f);
|
2018-12-05 01:19:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Texture2D AsTexture2D(GraphicsDevice graphicsDevice, Texture2D textureAtlas)
|
|
|
|
|
{
|
|
|
|
|
if (regionTexture == null)
|
|
|
|
|
{
|
|
|
|
|
Color[] data = new Color[sourceRectangle.Width * sourceRectangle.Height];
|
|
|
|
|
regionTexture = new Texture2D(graphicsDevice, sourceRectangle.Width, sourceRectangle.Height);
|
|
|
|
|
textureAtlas.GetData(0, sourceRectangle, data, 0, sourceRectangle.Width * sourceRectangle.Height);
|
|
|
|
|
regionTexture.SetData(data);
|
|
|
|
|
}
|
|
|
|
|
return regionTexture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int CompareTo(TextureAtlasRegion other)
|
|
|
|
|
{
|
|
|
|
|
return name.CompareTo(other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
regionTexture?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|