2018-11-30 02:41:06 +00:00
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Graphics ;
2019-03-05 00:18:43 +00:00
using RecrownedAthenaeum.Render ;
2018-11-30 02:41:06 +00:00
using System ;
2019-01-15 23:33:55 +00:00
namespace RecrownedAthenaeum.SpecialTypes
2018-11-30 02:41:06 +00:00
{
2019-01-14 07:26:46 +00:00
/// <summary>
/// An object that represents a ninepatch.
/// </summary>
2018-12-14 06:42:51 +00:00
public class NinePatch : ISpecialDrawable
2018-11-30 02:41:06 +00:00
{
2019-01-14 07:26:46 +00:00
/// <summary>
/// Dimensions in ninepatch. May also represent position in texture atlas.
/// </summary>
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
2019-03-05 00:18:43 +00:00
Rectangle [ ] sourcePatches ;
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>
2019-01-22 01:56:51 +00:00
/// <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 )
2018-11-30 02:41:06 +00:00
{
this . texture = texture ;
2019-01-22 01:56:51 +00:00
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 + "." ) ;
2018-12-29 06:29:31 +00:00
this . left = left ;
this . right = right ;
this . bottom = bottom ;
this . top = top ;
2018-11-30 02:41:06 +00:00
2019-03-05 00:18:43 +00:00
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 + + )
{
2019-03-10 06:50:03 +00:00
patches [ i ] . X + = textureRegion . X ;
patches [ i ] . Y + = textureRegion . Y ;
2019-03-05 00:18:43 +00:00
}
return patches ;
}
private Rectangle [ ] GenenerateDestinationRectangles ( int width , int height )
{
Rectangle [ ] patches =
{
2019-03-10 06:50:03 +00:00
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 - top - bottom ) ,
new Rectangle ( width - right , bottom , right , height - top - bottom ) ,
new Rectangle ( 0 , height - top , left , top ) ,
new Rectangle ( left , height - top , width - left - right , top ) ,
new Rectangle ( width - right , height - top , right , top ) ,
2019-03-05 00:18:43 +00:00
} ;
return patches ;
2018-11-30 02:41:06 +00:00
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Draws the ninepatch.
/// </summary>
/// <param name="spriteBatch">Batch to use.</param>
2019-03-05 00:18:43 +00:00
/// <param name="color">The color of the patch.</param>
2019-03-24 00:04:43 +00:00
/// <param name="destination">Where to the patch.</param>
public void Draw ( ConsistentSpriteBatch spriteBatch , Color color , Rectangle destination )
2018-11-30 02:41:06 +00:00
{
2019-03-24 00:04:43 +00:00
try
{
spriteBatch . End ( ) ;
} catch ( InvalidOperationException )
{
throw new InvalidOperationException ( "Begin must be called to draw a nine patch!" ) ;
}
2019-03-10 06:50:03 +00:00
2019-03-27 07:35:20 +00:00
spriteBatch . BeginWithoutSaving ( samplerState : SamplerState . PointClamp ) ;
2019-03-10 06:50:03 +00:00
2019-03-05 00:18:43 +00:00
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 ) ;
}
2019-03-10 06:50:03 +00:00
spriteBatch . End ( ) ;
2019-03-27 07:35:20 +00:00
spriteBatch . Begin ( ) ;
2018-12-14 06:42:51 +00:00
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// </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>
2019-03-24 00:04:43 +00:00
public void Draw ( ConsistentSpriteBatch spriteBatch , Rectangle destination , Color color , float rotation = 0 , Vector2 origin = default ( Vector2 ) )
2018-12-14 06:42:51 +00:00
{
2019-03-09 06:54:41 +00:00
if ( rotation ! = 0 ) throw new NotImplementedException ( "Ninepatches can't be rotated." ) ;
if ( origin ! = default ( Vector2 ) ) throw new NotImplementedException ( "Ninepatches can't have origin changed (hint: use the destination rectangle to shift and position)." ) ;
2019-03-10 06:50:03 +00:00
Draw ( spriteBatch , color , destination ) ;
2018-11-30 02:41:06 +00:00
}
}
}