diff --git a/RecrownedAthenaeum/SpecialTypes/NinePatch.cs b/RecrownedAthenaeum/SpecialTypes/NinePatch.cs index 23793de..62d17e6 100644 --- a/RecrownedAthenaeum/SpecialTypes/NinePatch.cs +++ b/RecrownedAthenaeum/SpecialTypes/NinePatch.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using RecrownedAthenaeum.Render; using System; namespace RecrownedAthenaeum.SpecialTypes @@ -9,10 +10,6 @@ namespace RecrownedAthenaeum.SpecialTypes /// public class NinePatch : ISpecialDrawable { - /// - /// color of 9patch. - /// - public Color color; /// /// Dimensions in ninepatch. May also represent position in texture atlas. /// @@ -20,6 +17,8 @@ namespace RecrownedAthenaeum.SpecialTypes readonly Texture2D texture; readonly int left, right, bottom, top; + Rectangle[] sourcePatches; + /// /// A nine patch object. /// @@ -40,7 +39,49 @@ namespace RecrownedAthenaeum.SpecialTypes this.bottom = bottom; this.top = top; - color = Color.White; + 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--; + patches[i].Height--; + } + return patches; + } + + private Rectangle[] GenenerateDestinationRectangles(int width, int height) + { + Rectangle[] patches = + { + new Rectangle(0, 0, left, bottom), + new Rectangle(left, 0, width - left - right, bottom), + new Rectangle(width - right, 0, right, bottom), + new Rectangle(0, bottom, left, height - bottom - top), + new Rectangle(left, bottom, width - left - right, height - bottom - top), + new Rectangle(width - right, bottom, right, height - bottom - top), + new Rectangle(0, height - top, left, top), + new Rectangle(left, height - top, width - left - right, top), + new Rectangle(width - right, height - top, right, top), + }; + return patches; } /// @@ -48,146 +89,16 @@ namespace RecrownedAthenaeum.SpecialTypes /// /// Batch to use. /// Where to the patch. - public void Draw(SpriteBatch spriteBatch, Rectangle destination) + /// The color of the patch. + public void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color) { - 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); + 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); + } } /// @@ -200,8 +111,7 @@ namespace RecrownedAthenaeum.SpecialTypes /// Not considered for 9patches. public void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2)) { - this.color = color; - Draw(spriteBatch, destination); + Draw(spriteBatch, destination, color); } } } diff --git a/RecrownedAthenaeum/SpecialTypes/TextureAtlas.cs b/RecrownedAthenaeum/SpecialTypes/TextureAtlas.cs index 274ac62..8475568 100644 --- a/RecrownedAthenaeum/SpecialTypes/TextureAtlas.cs +++ b/RecrownedAthenaeum/SpecialTypes/TextureAtlas.cs @@ -174,7 +174,7 @@ namespace RecrownedAthenaeum.SpecialTypes if (ninepatch != null) { - ninepatch.Draw(batch, destination); + ninepatch.Draw(batch, destination, Color.White); } else {