recrownedathenaeum/RecrownedAthenaeum/DataTypes/NinePatch.cs

190 lines
7.0 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace RecrownedAthenaeum.DataTypes
{
public class NinePatch : ISpecialDrawable
{
public Color color;
public Rectangle textureRegion;
readonly Texture2D texture;
readonly int a, b, c, d;
/// <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="a">Left side.</param>
/// <param name="b">Right side.</param>
/// <param name="c">Bottom side.</param>
/// <param name="d">Top side.</param>
public NinePatch(Texture2D texture, int a, int b, int c, int d)
{
this.texture = texture;
textureRegion = texture.Bounds;
if (a + b >= textureRegion.Width) throw new ArgumentOutOfRangeException("a and b cannot be greater than or equal to the width of region.");
if (c + d >= textureRegion.Height) throw new ArgumentOutOfRangeException("c and d cannot be greater or equal to the height of the texture.");
this.a = a;
this.b = b;
this.c = c;
this.d = d;
color = Color.White;
}
public void Draw(SpriteBatch spriteBatch, Rectangle destination)
{
Rectangle sourceRectangle;
Rectangle drawnRectangle;
//1x1
drawnRectangle.X = destination.X;
drawnRectangle.Y = destination.Y;
drawnRectangle.Width = a;
drawnRectangle.Height = c;
sourceRectangle.X = 0;
sourceRectangle.Y = 0;
sourceRectangle.Width = a;
sourceRectangle.Height = c;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//2x1
drawnRectangle.X = destination.X + a;
drawnRectangle.Y = destination.Y;
drawnRectangle.Width = destination.Width - a - b;
drawnRectangle.Height = c;
sourceRectangle.X = a;
sourceRectangle.Y = 0;
sourceRectangle.Width = textureRegion.Width - a - b;
sourceRectangle.Height = c;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//3x1
drawnRectangle.X = destination.X + destination.Width - b;
drawnRectangle.Y = destination.Y;
drawnRectangle.Width = b;
drawnRectangle.Height = c;
sourceRectangle.X = textureRegion.Width - b;
sourceRectangle.Y = 0;
sourceRectangle.Width = b;
sourceRectangle.Height = c;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//1x2
drawnRectangle.X = destination.X;
drawnRectangle.Y = destination.Y + c;
drawnRectangle.Width = a;
drawnRectangle.Height = destination.Height - d - c;
sourceRectangle.X = 0;
sourceRectangle.Y = c;
sourceRectangle.Width = a;
sourceRectangle.Height = textureRegion.Height - c - d;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//2x2
drawnRectangle.X = destination.X + a;
drawnRectangle.Y = destination.Y + c;
drawnRectangle.Width = destination.Width - a - b;
drawnRectangle.Height = destination.Height - c - d;
sourceRectangle.X = a;
sourceRectangle.Y = c;
sourceRectangle.Width = textureRegion.Width - a - b;
sourceRectangle.Height = textureRegion.Height - c - d;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//3x2
drawnRectangle.X = destination.X + destination.Width - b;
drawnRectangle.Y = destination.Y + c;
drawnRectangle.Width = b;
drawnRectangle.Height = destination.Height - c - d;
sourceRectangle.X = textureRegion.Width - b;
sourceRectangle.Y = c;
sourceRectangle.Width = b;
sourceRectangle.Height = textureRegion.Height - c - d;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//1x3
drawnRectangle.X = destination.X;
drawnRectangle.Y = destination.Height - d;
drawnRectangle.Width = a;
drawnRectangle.Height = d;
sourceRectangle.X = a;
sourceRectangle.Y = textureRegion.Height - d;
sourceRectangle.Width = a;
sourceRectangle.Height = d;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//2x3
drawnRectangle.X = destination.X + a;
drawnRectangle.Y = destination.Height - d;
drawnRectangle.Width = destination.Width - a - b;
drawnRectangle.Height = d;
sourceRectangle.X = a;
sourceRectangle.Y = textureRegion.Height - d;
sourceRectangle.Width = textureRegion.Width - a - b;
sourceRectangle.Height = d;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
//3x3
drawnRectangle.X = destination.X + destination.Width - b;
drawnRectangle.Y = destination.Height - d;
drawnRectangle.Width = b;
drawnRectangle.Height = d;
sourceRectangle.X = textureRegion.Width - b;
sourceRectangle.Y = textureRegion.Height - d;
sourceRectangle.Width = b;
sourceRectangle.Height = d;
sourceRectangle.X += textureRegion.X;
sourceRectangle.Y += textureRegion.Y;
spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color);
}
public void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2))
{
this.color = color;
Draw(spriteBatch, destination);
}
}
}