using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using RecrownedAthenaeum.Render; using System; namespace RecrownedAthenaeum.SpecialTypes { /// /// An object that represents a ninepatch. /// public class NinePatch : ISpecialDrawable { /// /// Dimensions in ninepatch. May also represent position in texture atlas. /// public Rectangle textureRegion; readonly Texture2D texture; readonly int left, right, bottom, top; Rectangle[] sourcePatches; /// /// A nine patch object. /// /// 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. /// Left side. /// Right side. /// Bottom side. /// Top side. /// The dimensions and potentially the coordinates of the rectangle on an atlas. If left to default of null, will only be set to texture bounds. public NinePatch(Texture2D texture, int left, int right, int bottom, int top, Rectangle? textureBounds = null) { this.texture = texture; if (textureBounds.HasValue) textureRegion = textureBounds.Value; else textureRegion = texture.Bounds; if (left + right >= textureRegion.Width) throw new ArgumentOutOfRangeException("left and right values cannot be greater than or equal to the width of region. Left value is " + left + " and right value is " + right + ". Bounds of texture are: " + textureRegion); if (bottom + top >= textureRegion.Height) throw new ArgumentOutOfRangeException("Bottom and top values cannot be greater than or equal to the width of region. Bottom value is " + bottom + " and top value is " + top + "."); this.left = left; this.right = right; this.bottom = bottom; this.top = top; sourcePatches = GenerateSourcesPatches(); } private Rectangle[] GenerateSourcesPatches() { Rectangle[] patches = { new Rectangle(0, 0, left, bottom), new Rectangle(left, 0, textureRegion.Width - left - right, bottom), new Rectangle(textureRegion.Width - right, 0, right, bottom), new Rectangle(0, bottom, left, textureRegion.Height - top - bottom), new Rectangle(left, bottom, textureRegion.Width - left - right, textureRegion.Height - bottom - top), new Rectangle(textureRegion.Width - right, bottom, right, textureRegion.Height - top - bottom), new Rectangle(0, textureRegion.Height - top, left, top), new Rectangle(left, textureRegion.Height - top, textureRegion.Width - left - right, top), new Rectangle(textureRegion.Width - right, textureRegion.Height - top, right, top), }; for (int i = 0; i < patches.Length; i++) { patches[i].X += textureRegion.X + 1; patches[i].Y += textureRegion.Y + 1; patches[i].Width -= 2; patches[i].Height -= 2; } return patches; } private Rectangle[] GenenerateDestinationRectangles(int width, int height) { Rectangle[] patches = { new Rectangle(-left, -bottom, left, bottom), new Rectangle(0, -bottom, width, bottom), new Rectangle(width, -bottom, right, bottom), new Rectangle(-left, 0, left, height), new Rectangle(0, 0, width, height), new Rectangle(width, 0, right, height), new Rectangle(-left, height, left, top), new Rectangle(0, height, width, top), new Rectangle(width, height, right, top), }; return patches; } /// /// Draws the ninepatch. /// /// Batch to use. /// Where to the patch. /// The color of the patch. public void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color) { Rectangle[] destinations = GenenerateDestinationRectangles(destination.Width, destination.Height); for (int i = 0; i < destinations.Length; i++) { destinations[i].X += destination.X; destinations[i].Y += destination.Y; spriteBatch.Draw(texture, destinations[i], sourcePatches[i], color); } } /// /// Draw with more options. /// /// Spritebatch to use. /// The destination to draw the patch. /// The tint for each patch. /// Not considered for 9patches. /// Not considered for 9patches. public void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2)) { Draw(spriteBatch, destination, color); } } }