fixed up 9 patch stuff.
This commit is contained in:
parent
8bd6ba6dc8
commit
668090cd5d
@ -1,5 +1,6 @@
|
|||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using RecrownedAthenaeum.Render;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace RecrownedAthenaeum.SpecialTypes
|
namespace RecrownedAthenaeum.SpecialTypes
|
||||||
@ -9,10 +10,6 @@ namespace RecrownedAthenaeum.SpecialTypes
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class NinePatch : ISpecialDrawable
|
public class NinePatch : ISpecialDrawable
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// color of 9patch.
|
|
||||||
/// </summary>
|
|
||||||
public Color color;
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Dimensions in ninepatch. May also represent position in texture atlas.
|
/// Dimensions in ninepatch. May also represent position in texture atlas.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -20,6 +17,8 @@ namespace RecrownedAthenaeum.SpecialTypes
|
|||||||
readonly Texture2D texture;
|
readonly Texture2D texture;
|
||||||
readonly int left, right, bottom, top;
|
readonly int left, right, bottom, top;
|
||||||
|
|
||||||
|
Rectangle[] sourcePatches;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A nine patch object.
|
/// A nine patch object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -40,7 +39,49 @@ namespace RecrownedAthenaeum.SpecialTypes
|
|||||||
this.bottom = bottom;
|
this.bottom = bottom;
|
||||||
this.top = top;
|
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>
|
/// <summary>
|
||||||
@ -48,146 +89,16 @@ namespace RecrownedAthenaeum.SpecialTypes
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="spriteBatch">Batch to use.</param>
|
/// <param name="spriteBatch">Batch to use.</param>
|
||||||
/// <param name="destination">Where to the patch.</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[] destinations = GenenerateDestinationRectangles(destination.Width, destination.Height);
|
||||||
Rectangle drawnRectangle;
|
for (int i = 0; i < destinations.Length; i++)
|
||||||
|
{
|
||||||
//1x1
|
destinations[i].X += destination.X;
|
||||||
drawnRectangle.X = destination.X;
|
destinations[i].Y += destination.Y;
|
||||||
drawnRectangle.Y = destination.Y;
|
spriteBatch.Draw(texture, destinations[i], sourcePatches[i], color);
|
||||||
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>
|
/// <summary>
|
||||||
@ -200,8 +111,7 @@ namespace RecrownedAthenaeum.SpecialTypes
|
|||||||
/// <param name="origin">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))
|
public void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2))
|
||||||
{
|
{
|
||||||
this.color = color;
|
Draw(spriteBatch, destination, color);
|
||||||
Draw(spriteBatch, destination);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,7 +174,7 @@ namespace RecrownedAthenaeum.SpecialTypes
|
|||||||
|
|
||||||
if (ninepatch != null)
|
if (ninepatch != null)
|
||||||
{
|
{
|
||||||
ninepatch.Draw(batch, destination);
|
ninepatch.Draw(batch, destination, Color.White);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user