2018-11-30 02:41:06 +00:00
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Graphics ;
using System ;
2018-12-04 13:45:09 +00:00
namespace RecrownedAthenaeum.DataTypes
2018-11-30 02:41:06 +00:00
{
2018-12-14 06:42:51 +00:00
public class NinePatch : ISpecialDrawable
2018-11-30 02:41:06 +00:00
{
public Color color ;
2018-12-05 08:28:09 +00:00
public Rectangle textureRegion ;
2018-11-30 02:41:06 +00:00
readonly Texture2D texture ;
2018-12-29 06:29:31 +00:00
readonly int left , right , bottom , top ;
2018-11-30 02:41:06 +00:00
/// <summary>
/// A nine patch object.
/// </summary>
2018-12-05 08:28:09 +00:00
/// <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>
2018-12-29 06:29:31 +00:00
/// <param name="left">Left side.</param>
/// <param name="right">Right side.</param>
/// <param name="bottom">Bottom side.</param>
/// <param name="top">Top side.</param>
public NinePatch ( Texture2D texture , int left , int right , int bottom , int top )
2018-11-30 02:41:06 +00:00
{
this . texture = texture ;
2018-12-05 08:28:09 +00:00
textureRegion = texture . Bounds ;
2018-12-29 06:29:31 +00:00
if ( left + right > = textureRegion . Width ) throw new ArgumentOutOfRangeException ( "a and b cannot be greater than or equal to the width of region." ) ;
if ( bottom + top > = textureRegion . Height ) throw new ArgumentOutOfRangeException ( "c and d cannot be greater or equal to the height of the texture." ) ;
this . left = left ;
this . right = right ;
this . bottom = bottom ;
this . top = top ;
2018-11-30 02:41:06 +00:00
color = Color . White ;
}
2018-12-14 06:42:51 +00:00
public void Draw ( SpriteBatch spriteBatch , Rectangle destination )
2018-11-30 02:41:06 +00:00
{
2018-12-04 13:45:09 +00:00
Rectangle sourceRectangle ;
Rectangle drawnRectangle ;
2018-11-30 02:41:06 +00:00
//1x1
drawnRectangle . X = destination . X ;
drawnRectangle . Y = destination . Y ;
2018-12-29 06:29:31 +00:00
drawnRectangle . Width = left ;
drawnRectangle . Height = bottom ;
2018-11-30 02:41:06 +00:00
sourceRectangle . X = 0 ;
sourceRectangle . Y = 0 ;
2018-12-29 06:29:31 +00:00
sourceRectangle . Width = left ;
sourceRectangle . Height = bottom ;
2018-11-30 02:41:06 +00:00
2018-12-05 08:28:09 +00:00
sourceRectangle . X + = textureRegion . X ;
sourceRectangle . Y + = textureRegion . Y ;
2018-12-14 06:42:51 +00:00
spriteBatch . Draw ( texture , drawnRectangle , sourceRectangle , color ) ;
2018-11-30 02:41:06 +00:00
//2x1
2018-12-29 06:29:31 +00:00
drawnRectangle . X = destination . X + left ;
2018-11-30 02:41:06 +00:00
drawnRectangle . Y = destination . Y ;
2018-12-29 06:29:31 +00:00
drawnRectangle . Width = destination . Width - left - right ;
drawnRectangle . Height = bottom ;
2018-11-30 02:41:06 +00:00
2018-12-29 06:29:31 +00:00
sourceRectangle . X = left ;
2018-11-30 02:41:06 +00:00
sourceRectangle . Y = 0 ;
2018-12-29 06:29:31 +00:00
sourceRectangle . Width = textureRegion . Width - left - right ;
sourceRectangle . Height = bottom ;
2018-12-05 08:28:09 +00:00
sourceRectangle . X + = textureRegion . X ;
sourceRectangle . Y + = textureRegion . Y ;
2018-11-30 02:41:06 +00:00
2018-12-14 06:42:51 +00:00
spriteBatch . Draw ( texture , drawnRectangle , sourceRectangle , color ) ;
2018-11-30 02:41:06 +00:00
//3x1
2018-12-29 06:29:31 +00:00
drawnRectangle . X = destination . X + destination . Width - right ;
2018-11-30 02:41:06 +00:00
drawnRectangle . Y = destination . Y ;
2018-12-29 06:29:31 +00:00
drawnRectangle . Width = right ;
drawnRectangle . Height = bottom ;
2018-11-30 02:41:06 +00:00
2018-12-29 06:29:31 +00:00
sourceRectangle . X = textureRegion . Width - right ;
2018-11-30 02:41:06 +00:00
sourceRectangle . Y = 0 ;
2018-12-29 06:29:31 +00:00
sourceRectangle . Width = right ;
sourceRectangle . Height = bottom ;
2018-12-05 08:28:09 +00:00
sourceRectangle . X + = textureRegion . X ;
sourceRectangle . Y + = textureRegion . Y ;
2018-11-30 02:41:06 +00:00
2018-12-14 06:42:51 +00:00
spriteBatch . Draw ( texture , drawnRectangle , sourceRectangle , color ) ;
2018-11-30 02:41:06 +00:00
//1x2
drawnRectangle . X = destination . X ;
2018-12-29 06:29:31 +00:00
drawnRectangle . Y = destination . Y + bottom ;
drawnRectangle . Width = left ;
drawnRectangle . Height = destination . Height - top - bottom ;
2018-11-30 02:41:06 +00:00
sourceRectangle . X = 0 ;
2018-12-29 06:29:31 +00:00
sourceRectangle . Y = bottom ;
sourceRectangle . Width = left ;
sourceRectangle . Height = textureRegion . Height - bottom - top ;
2018-12-05 08:28:09 +00:00
sourceRectangle . X + = textureRegion . X ;
sourceRectangle . Y + = textureRegion . Y ;
2018-11-30 02:41:06 +00:00
2018-12-14 06:42:51 +00:00
spriteBatch . Draw ( texture , drawnRectangle , sourceRectangle , color ) ;
2018-11-30 02:41:06 +00:00
//2x2
2018-12-29 06:29:31 +00:00
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 ;
2018-12-05 08:28:09 +00:00
sourceRectangle . X + = textureRegion . X ;
sourceRectangle . Y + = textureRegion . Y ;
2018-11-30 02:41:06 +00:00
2018-12-14 06:42:51 +00:00
spriteBatch . Draw ( texture , drawnRectangle , sourceRectangle , color ) ;
2018-11-30 02:41:06 +00:00
//3x2
2018-12-29 06:29:31 +00:00
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 ;
2018-12-05 08:28:09 +00:00
sourceRectangle . X + = textureRegion . X ;
sourceRectangle . Y + = textureRegion . Y ;
2018-11-30 02:41:06 +00:00
2018-12-14 06:42:51 +00:00
spriteBatch . Draw ( texture , drawnRectangle , sourceRectangle , color ) ;
2018-11-30 02:41:06 +00:00
//1x3
drawnRectangle . X = destination . X ;
2018-12-29 06:29:31 +00:00
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 ;
2018-12-05 08:28:09 +00:00
sourceRectangle . X + = textureRegion . X ;
sourceRectangle . Y + = textureRegion . Y ;
2018-11-30 02:41:06 +00:00
2018-12-14 06:42:51 +00:00
spriteBatch . Draw ( texture , drawnRectangle , sourceRectangle , color ) ;
2018-11-30 02:41:06 +00:00
//2x3
2018-12-29 06:29:31 +00:00
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 ;
2018-12-05 08:28:09 +00:00
sourceRectangle . X + = textureRegion . X ;
sourceRectangle . Y + = textureRegion . Y ;
2018-11-30 02:41:06 +00:00
2018-12-14 06:42:51 +00:00
spriteBatch . Draw ( texture , drawnRectangle , sourceRectangle , color ) ;
2018-11-30 02:41:06 +00:00
//3x3
2018-12-29 06:29:31 +00:00
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 ;
2018-12-05 08:28:09 +00:00
sourceRectangle . X + = textureRegion . X ;
sourceRectangle . Y + = textureRegion . Y ;
2018-11-30 02:41:06 +00:00
2018-12-14 06:42:51 +00:00
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 ) ;
2018-11-30 02:41:06 +00:00
}
}
}