recrownedgtk/RecrownedAthenaeum/SpecialTypes/NinePatch.cs

208 lines
8.6 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
2019-01-15 23:33:55 +00:00
namespace RecrownedAthenaeum.SpecialTypes
{
2019-01-14 07:26:46 +00:00
/// <summary>
/// An object that represents a ninepatch.
/// </summary>
public class NinePatch : ISpecialDrawable
{
2019-01-14 07:26:46 +00:00
/// <summary>
/// color of 9patch.
/// </summary>
public Color color;
2019-01-14 07:26:46 +00:00
/// <summary>
/// Dimensions in ninepatch. May also represent position in texture atlas.
/// </summary>
public Rectangle textureRegion;
readonly Texture2D texture;
readonly int left, right, bottom, top;
/// <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="left">Left side.</param>
/// <param name="right">Right side.</param>
/// <param name="bottom">Bottom side.</param>
/// <param name="top">Top side.</param>
/// <param name="textureBounds">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.</param>
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;
2019-01-22 05:38:50 +00:00
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;
color = Color.White;
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Draws the ninepatch.
/// </summary>
/// <param name="spriteBatch">Batch to use.</param>
/// <param name="destination">Where to the patch.</param>
public void Draw(SpriteBatch spriteBatch, Rectangle destination)
{
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);
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Draw with more options.
/// </summary>
/// <param name="spriteBatch">Spritebatch to use.</param>
/// <param name="destination">The destination to draw the patch.</param>
/// <param name="color">The tint for each patch.</param>
/// <param name="rotation">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))
{
this.color = color;
Draw(spriteBatch, destination);
}
}
}