Refactoring

This commit is contained in:
2019-01-15 17:33:55 -06:00
parent a62ec1bd38
commit 441a4d1a36
16 changed files with 35 additions and 36 deletions

View File

@@ -0,0 +1,21 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace RecrownedAthenaeum.SpecialTypes
{
/// <summary>
/// A wrapper that makes sure anything implementing can be drawn with options.
/// </summary>
public interface ISpecialDrawable
{
/// <summary>
/// Should draw whatever implements this.
/// </summary>
/// <param name="spriteBatch">The batch to be used.</param>
/// <param name="destination">The location and dimensions to draw to.</param>
/// <param name="color">The color tint to draw with.</param>
/// <param name="rotation">The rotation to be used.</param>
/// <param name="origin">The origin for the rotation.</param>
void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0f, Vector2 origin = default(Vector2));
}
}

View File

@@ -0,0 +1,206 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace RecrownedAthenaeum.SpecialTypes
{
/// <summary>
/// An object that represents a ninepatch.
/// </summary>
public class NinePatch : ISpecialDrawable
{
/// <summary>
/// color of 9patch.
/// </summary>
public Color color;
/// <summary>
/// Dimensions in ninepatch. May also represent position in texture atlas.
/// </summary>
public Rectangle textureRegion;
readonly Texture2D texture;
readonly int left, right, bottom, top;
/// <summary>
/// A nine patch object.
/// </summary>
/// <param name="texture">Texture used for the nine patch. Dimensions must be greater than their sum border counter parts. If used as part of texture atlas, the texture should be the texture of the entire atlas.</param>
/// <param name="left">Left side.</param>
/// <param name="right">Right side.</param>
/// <param name="bottom">Bottom side.</param>
/// <param name="top">Top side.</param>
public NinePatch(Texture2D texture, int left, int right, int bottom, int top)
{
this.texture = texture;
textureRegion = texture.Bounds;
if (left + right >= textureRegion.Width) throw new ArgumentOutOfRangeException("a and b cannot be greater than or equal to the width of region.");
if (bottom + top >= textureRegion.Height) throw new ArgumentOutOfRangeException("c and d cannot be greater or equal to the height of the texture.");
this.left = left;
this.right = right;
this.bottom = bottom;
this.top = top;
color = Color.White;
}
/// <summary>
/// Draws the ninepatch.
/// </summary>
/// <param name="spriteBatch">Batch to use.</param>
/// <param name="destination">Where to the patch.</param>
public void Draw(SpriteBatch spriteBatch, Rectangle destination)
{
Rectangle sourceRectangle;
Rectangle drawnRectangle;
//1x1
drawnRectangle.X = destination.X;
drawnRectangle.Y = destination.Y;
drawnRectangle.Width = left;
drawnRectangle.Height = bottom;
sourceRectangle.X = 0;
sourceRectangle.Y = 0;
sourceRectangle.Width = left;
sourceRectangle.Height = bottom;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//2x1
drawnRectangle.X = destination.X + left;
drawnRectangle.Y = destination.Y;
drawnRectangle.Width = destination.Width - left - right;
drawnRectangle.Height = bottom;
sourceRectangle.X = left;
sourceRectangle.Y = 0;
sourceRectangle.Width = textureRegion.Width - left - right;
sourceRectangle.Height = bottom;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//3x1
drawnRectangle.X = destination.X + destination.Width - right;
drawnRectangle.Y = destination.Y;
drawnRectangle.Width = right;
drawnRectangle.Height = bottom;
sourceRectangle.X = textureRegion.Width - right;
sourceRectangle.Y = 0;
sourceRectangle.Width = right;
sourceRectangle.Height = bottom;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//1x2
drawnRectangle.X = destination.X;
drawnRectangle.Y = destination.Y + bottom;
drawnRectangle.Width = left;
drawnRectangle.Height = destination.Height - top - bottom;
sourceRectangle.X = 0;
sourceRectangle.Y = bottom;
sourceRectangle.Width = left;
sourceRectangle.Height = textureRegion.Height - bottom - top;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//2x2
drawnRectangle.X = destination.X + left;
drawnRectangle.Y = destination.Y + bottom;
drawnRectangle.Width = destination.Width - left - right;
drawnRectangle.Height = destination.Height - bottom - top;
sourceRectangle.X = left;
sourceRectangle.Y = bottom;
sourceRectangle.Width = textureRegion.Width - left - right;
sourceRectangle.Height = textureRegion.Height - bottom - top;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//3x2
drawnRectangle.X = destination.X + destination.Width - right;
drawnRectangle.Y = destination.Y + bottom;
drawnRectangle.Width = right;
drawnRectangle.Height = destination.Height - bottom - top;
sourceRectangle.X = textureRegion.Width - right;
sourceRectangle.Y = bottom;
sourceRectangle.Width = right;
sourceRectangle.Height = textureRegion.Height - bottom - top;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//1x3
drawnRectangle.X = destination.X;
drawnRectangle.Y = destination.Height - top;
drawnRectangle.Width = left;
drawnRectangle.Height = top;
sourceRectangle.X = left;
sourceRectangle.Y = textureRegion.Height - top;
sourceRectangle.Width = left;
sourceRectangle.Height = top;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//2x3
drawnRectangle.X = destination.X + left;
drawnRectangle.Y = destination.Height - top;
drawnRectangle.Width = destination.Width - left - right;
drawnRectangle.Height = top;
sourceRectangle.X = left;
sourceRectangle.Y = textureRegion.Height - top;
sourceRectangle.Width = textureRegion.Width - left - right;
sourceRectangle.Height = top;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//3x3
drawnRectangle.X = destination.X + destination.Width - right;
drawnRectangle.Y = destination.Height - top;
drawnRectangle.Width = right;
drawnRectangle.Height = top;
sourceRectangle.X = textureRegion.Width - right;
sourceRectangle.Y = textureRegion.Height - top;
sourceRectangle.Width = right;
sourceRectangle.Height = top;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
}
/// <summary>
/// Draw with more options.
/// </summary>
/// <param name="spriteBatch">Spritebatch to use.</param>
/// <param name="destination">The destination to draw the patch.</param>
/// <param name="color">The tint for each patch.</param>
/// <param name="rotation">Not considered for 9patches.</param>
/// <param name="origin">Not considered for 9patches.</param>
public void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2))
{
this.color = color;
Draw(spriteBatch, destination);
}
}
}

View File

@@ -0,0 +1,54 @@
using System;
namespace RecrownedAthenaeum.SpecialTypes
{
/// <summary>
/// Holds a width and height while allowing for easier comparison between other <see cref="Resolution"/>s.
/// </summary>
public struct Resolution : IComparable<Resolution>
{
/// <summary>
/// Dimensions of resolution.
/// </summary>
public int Width, Height;
/// <summary>
/// Constructs resolution given the dimensions.
/// </summary>
/// <param name="width">Width of resolution.</param>
/// <param name="height">Height of resolution.</param>
public Resolution(int width, int height)
{
Width = width;
Height = height;
}
/// <summary>
/// Compares area of this resolution to another.
/// </summary>
/// <param name="other">The other resolution to compare to.</param>
/// <returns>A value less than 0 for this being the smaller resolution, 0 for the two being the same in area, and larger for this being the larger in area.</returns>
public int CompareTo(Resolution other)
{
return Area() - other.Area();
}
/// <summary>
/// Gets a string representation of this resolution.
/// </summary>
/// <returns>"WidthxHeight"</returns>
public override string ToString()
{
return Width + "x" + Height;
}
/// <summary>
/// Calculates area of resolution.
/// </summary>
/// <returns>Area of resolution.</returns>
public int Area()
{
return Width * Height;
}
}
}

View File

@@ -0,0 +1,225 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
namespace RecrownedAthenaeum.SpecialTypes
{
/// <summary>
/// Holds information about an image file that contains various textures in various regions in the file.
/// </summary>
public class TextureAtlas : IDisposable
{
private Texture2D texture;
private bool disposed;
private Dictionary<string, TextureAtlasRegion> dictionaryOfRegions = new Dictionary<string, TextureAtlasRegion>();
/// <summary>
/// Given a name, can return a <see cref="TextureAtlasRegion"/>.
/// </summary>
/// <param name="name">Name of <see cref="TextureAtlasRegion"/> to obtain.</param>
/// <returns><see cref="TextureAtlasRegion"/> based off name.</returns>
public TextureAtlasRegion this[string name] { get { if (name != null && dictionaryOfRegions.ContainsKey(name)) return dictionaryOfRegions[name]; else return null; } }
/// <summary>
/// Creates a texture atlas with given main texture as well as an array of <see cref="TextureAtlasRegion"/> to represent locations of which textures reside within the atlas. Region names will be used to refer to the regions within the dictionary.
/// </summary>
/// <param name="texture">The texture representing the overall atlas.</param>
/// <param name="regions">The sub regions that represent the individual textures.</param>
public TextureAtlas(Texture2D texture, TextureAtlasRegion[] regions)
{
this.texture = texture;
foreach (TextureAtlasRegion region in regions)
{
dictionaryOfRegions.Add(region.name, region);
}
}
/// <summary>
/// Creates a texture region given a dictionary of regions keyed to strings that can be used to refer to them.
/// </summary>
/// <param name="texture">The texture representing the overall atlas.</param>
/// <param name="dictionaryOfRegions"></param>
public TextureAtlas(Texture2D texture, Dictionary<string, TextureAtlasRegion> dictionaryOfRegions)
{
this.texture = texture;
this.dictionaryOfRegions = dictionaryOfRegions;
}
/// <summary>
/// Draw the region given by a string in the atlas onto a destination rectangle.
/// </summary>
/// <param name="name">Name of region to draw.</param>
/// <param name="batch">SpriteBatch to be used.</param>
/// <param name="destination">The location to draw this region.</param>
/// <param name="color">Color to use.</param>
/// <param name="rotation">Rotation of texture drawn.</param>
/// <param name="origin">Origin used by rotation.</param>
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);
}
/// <summary>
/// Creates or obtains a previously created texture of a region.
/// </summary>
/// <param name="name">Name of region.</param>
/// <param name="graphicsDevice">graphics device to be used.</param>
/// <returns>The texture from the region.</returns>
public Texture2D ObtainRegionAsTexture(string name, GraphicsDevice graphicsDevice)
{
return dictionaryOfRegions[name].AsTexture2D(graphicsDevice);
}
/// <summary>
/// Disposes unmanaged resources for the texture atlas.
/// </summary>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Overridable disposal method.
/// </summary>
/// <param name="disposing">Only true if user calls <see cref="Dispose()"/></param>
public virtual void Dispose(bool disposing)
{
disposed = true;
if (!disposed && disposing)
{
texture.Dispose();
for (int i = 0; i < dictionaryOfRegions.Count; i++)
{
dictionaryOfRegions.ElementAt(i).Value.Dispose();
}
dictionaryOfRegions.Clear();
}
}
/// <summary>
/// Destructor.
/// </summary>
~TextureAtlas()
{
Dispose(false);
}
/// <summary>
/// A region of a <see cref="TextureAtlas"/>.
/// </summary>
public class TextureAtlasRegion : IComparable<TextureAtlasRegion>, ISpecialDrawable, IDisposable
{
/// <summary>
/// The name of the region. Mostly used to be refered to within the context of a <see cref="TextureAtlas"/>.
/// </summary>
public readonly string name;
/// <summary>
/// The location and dimensions of where the original texture resides on the texture representing the atlas.
/// </summary>
public readonly Rectangle sourceRectangle;
readonly NinePatch ninepatch;
Texture2D atlasTexture;
Texture2D regionTexture;
private bool disposed;
/// <summary>
/// A specified region in a texture atlas.
/// </summary>
/// <param name="name">Name of region.</param>
/// <param name="sourceRegion">The location of the region on the atlas.</param>
/// <param name="ninePatch">A <see cref="NinePatch"/> definition for the region.</param>
/// <param name="atlasTexture">The texture that holds the image data for the atlas.</param>
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;
}
/// <summary>
/// Draws the region. If ninepatch, rotation and origin are ignored.
/// </summary>
/// <param name="batch">The batch to use. Should be began.</param>
/// <param name="destination">The destination rectangle to draw to.</param>
/// <param name="color">The color to use.</param>
/// <param name="rotation">Rotation of the final drawing. Ignored if is a 9patch.</param>
/// <param name="origin">The origin of the drawing. Ignored if is a 9patch.</param>
public void Draw(SpriteBatch batch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2))
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
if (ninepatch != null)
{
ninepatch.Draw(batch, destination);
} else
{
batch.Draw(atlasTexture, destination, sourceRectangle, color, rotation, origin, SpriteEffects.None, 0f);
}
}
/// <summary>
/// Create or obtains a previously created texture of this region.
/// </summary>
/// <param name="graphicsDevice">The graphics device to use to create the texture.</param>
/// <returns>The texture of the region.</returns>
public Texture2D AsTexture2D(GraphicsDevice graphicsDevice)
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
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;
}
/// <summary>
/// Compares this region to another in terms of name.
/// </summary>
/// <param name="other">The other region to compare to in terms of name.</param>
/// <returns>Less than one if precedes, greater than one if after, 0 if same.</returns>
public int CompareTo(TextureAtlasRegion other)
{
return name.CompareTo(other);
}
/// <summary>
/// Call this to dispose.
/// </summary>
public void Dispose()
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Overridable dispose.
/// </summary>
/// <param name="disposing">Whether or not this was a user made call.</param>
public virtual void Dispose(bool disposing)
{
if (disposing && !disposed)
{
regionTexture?.Dispose();
}
disposed = true;
}
/// <summary>
/// Destructor.
/// </summary>
~TextureAtlasRegion()
{
Dispose(false);
}
}
}
}