fixed up 9 patch stuff.

This commit is contained in:
Harrison Deng 2019-03-04 18:18:43 -06:00
parent 8bd6ba6dc8
commit 668090cd5d
2 changed files with 57 additions and 147 deletions

View File

@ -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
/// </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>
@ -20,6 +17,8 @@ namespace RecrownedAthenaeum.SpecialTypes
readonly Texture2D texture;
readonly int left, right, bottom, top;
Rectangle[] sourcePatches;
/// <summary>
/// A nine patch object.
/// </summary>
@ -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;
}
/// <summary>
@ -48,146 +89,16 @@ namespace RecrownedAthenaeum.SpecialTypes
/// </summary>
/// <param name="spriteBatch">Batch to use.</param>
/// <param name="destination">Where to the patch.</param>
public void Draw(SpriteBatch spriteBatch, Rectangle destination)
/// <param name="color">The color of the patch.</param>
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);
}
}
/// <summary>
@ -200,8 +111,7 @@ namespace RecrownedAthenaeum.SpecialTypes
/// <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);
Draw(spriteBatch, destination, color);
}
}
}

View File

@ -174,7 +174,7 @@ namespace RecrownedAthenaeum.SpecialTypes
if (ninepatch != null)
{
ninepatch.Draw(batch, destination);
ninepatch.Draw(batch, destination, Color.White);
}
else
{