using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; namespace RecrownedAthenaeum.DataTypes { public class TextureAtlas { private Texture2D texture; private Dictionary dictionaryOfRegions = new Dictionary(); public TextureAtlasRegion this[string name] { get { if (name != null && dictionaryOfRegions.ContainsKey(name)) return dictionaryOfRegions[name]; else return null; } } public TextureAtlas(Texture2D texture, TextureAtlasRegion[] regions) { this.texture = texture; foreach (TextureAtlasRegion region in regions) { dictionaryOfRegions.Add(region.name, region); } } public TextureAtlas(Texture2D texture, Dictionary dictionaryOfRegions) { this.texture = texture; this.dictionaryOfRegions = dictionaryOfRegions; } /// /// Draw the region given by a string in the atlas onto a destination rectangle. /// /// Name of region to draw. /// SpriteBatch to be used. /// The location to draw this region. /// Color to use. /// Rotation of texture drawn. /// Origin used by rotation. public void Draw(string name, SpriteBatch batch, Rectangle destination, Color color = default(Color), float rotation = 0, Vector2 origin = new Vector2()) { dictionaryOfRegions[name].Draw(batch, destination, color, rotation, origin); } /// /// Creates or obtains a previously created texture of a region. /// /// Name of region. /// graphics device to be used. /// The texture from the region. public Texture2D ObtainRegionAsTexture(string name, GraphicsDevice graphicsDevice) { return dictionaryOfRegions[name].AsTexture2D(graphicsDevice); } public class TextureAtlasRegion : IComparable, ISpecialDrawable, IDisposable { public readonly string name; public readonly Rectangle sourceRectangle; readonly NinePatch ninepatch; Texture2D atlasTexture; Texture2D regionTexture; /// /// A specified region in a texture atlas. /// /// Name of region. /// The location of the region on the atlas. /// A definition for the region. /// The texture that holds the image data for the atlas. public TextureAtlasRegion(string name, Rectangle sourceRegion, NinePatch ninePatch, Texture2D atlasTexture) { this.atlasTexture = atlasTexture ?? throw new ArgumentNullException("Name parameters can be null."); this.name = name ?? throw new ArgumentNullException("Name parameters can be null."); this.sourceRectangle = sourceRegion; this.ninepatch = ninePatch; } public void Draw(SpriteBatch batch, Rectangle destination, Color color, float rotation, Vector2 origin) { if (ninepatch != null) { ninepatch.Draw(batch, destination); } else { batch.Draw(atlasTexture, destination, sourceRectangle, color, rotation, origin, SpriteEffects.None, 0f); } } /// /// Create or obtains a previously created texture of this region. /// /// The graphics device to use to create the texture. /// The texture of the region. public Texture2D AsTexture2D(GraphicsDevice graphicsDevice) { if (regionTexture == null) { Color[] data = new Color[sourceRectangle.Width * sourceRectangle.Height]; regionTexture = new Texture2D(graphicsDevice, sourceRectangle.Width, sourceRectangle.Height); atlasTexture.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(); } } } }