2018-12-05 01:19:31 +00:00
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Graphics ;
2019-03-24 00:04:43 +00:00
using RecrownedAthenaeum.Render ;
2018-12-05 01:19:31 +00:00
using System ;
using System.Collections.Generic ;
2019-01-15 03:06:00 +00:00
using System.Linq ;
2018-12-05 01:19:31 +00:00
2019-01-15 23:33:55 +00:00
namespace RecrownedAthenaeum.SpecialTypes
2018-12-05 01:19:31 +00:00
{
2019-01-15 02:00:11 +00:00
/// <summary>
/// Holds information about an image file that contains various textures in various regions in the file.
/// </summary>
2019-01-15 03:06:00 +00:00
public class TextureAtlas : IDisposable
2018-12-05 01:19:31 +00:00
{
private Texture2D texture ;
2019-01-15 03:06:00 +00:00
private bool disposed ;
2019-01-16 01:34:47 +00:00
private Dictionary < string , Region > dictionaryOfRegions = new Dictionary < string , Region > ( ) ;
2019-01-20 07:08:38 +00:00
2019-01-15 02:00:11 +00:00
/// <summary>
2019-01-16 01:34:47 +00:00
/// Given a name, can return a <see cref="Region"/>.
2019-01-15 02:00:11 +00:00
/// </summary>
2019-01-16 01:34:47 +00:00
/// <param name="name">Name of <see cref="Region"/> to obtain.</param>
/// <returns><see cref="Region"/> based off name.</returns>
2019-01-20 07:08:38 +00:00
public Region this [ string name ] { get { if ( name ! = null & & dictionaryOfRegions . ContainsKey ( name ) ) return dictionaryOfRegions [ name ] ; else throw new KeyNotFoundException ( "Given key \"" + name + "\" does not exist." ) ; } }
2019-01-15 02:00:11 +00:00
/// <summary>
2019-01-16 01:34:47 +00:00
/// Creates a texture atlas with given main texture as well as an array of <see cref="Region"/> to represent locations of which textures reside within the atlas. Region names will be used to refer to the regions within the dictionary.
2019-01-15 02:00:11 +00:00
/// </summary>
/// <param name="texture">The texture representing the overall atlas.</param>
/// <param name="regions">The sub regions that represent the individual textures.</param>
2019-01-16 01:34:47 +00:00
public TextureAtlas ( Texture2D texture , Region [ ] regions )
2018-12-05 01:19:31 +00:00
{
this . texture = texture ;
2019-01-16 01:34:47 +00:00
foreach ( Region region in regions )
2018-12-05 01:19:31 +00:00
{
dictionaryOfRegions . Add ( region . name , region ) ;
}
}
2019-01-15 02:00:11 +00:00
/// <summary>
/// Creates a texture region given a dictionary of regions keyed to strings that can be used to refer to them.
/// </summary>
/// <param name="texture">The texture representing the overall atlas.</param>
/// <param name="dictionaryOfRegions"></param>
2019-01-16 01:34:47 +00:00
public TextureAtlas ( Texture2D texture , Dictionary < string , Region > dictionaryOfRegions )
2018-12-05 08:28:09 +00:00
{
this . texture = texture ;
this . dictionaryOfRegions = dictionaryOfRegions ;
}
2018-12-12 02:03:35 +00:00
/// <summary>
/// Draw the region given by a string in the atlas onto a destination rectangle.
/// </summary>
/// <param name="name">Name of region to draw.</param>
/// <param name="batch">SpriteBatch to be used.</param>
/// <param name="destination">The location to draw this region.</param>
/// <param name="color">Color to use.</param>
/// <param name="rotation">Rotation of texture drawn.</param>
/// <param name="origin">Origin used by rotation.</param>
2019-03-24 00:04:43 +00:00
public void Draw ( string name , ConsistentSpriteBatch batch , Rectangle destination , Color color = default ( Color ) , float rotation = 0 , Vector2 origin = new Vector2 ( ) )
2018-12-05 01:19:31 +00:00
{
2018-12-05 08:28:09 +00:00
dictionaryOfRegions [ name ] . Draw ( batch , destination , color , rotation , origin ) ;
2018-12-05 01:19:31 +00:00
}
2018-12-12 02:03:35 +00:00
/// <summary>
/// Creates or obtains a previously created texture of a region.
/// </summary>
/// <param name="name">Name of region.</param>
2019-03-24 00:04:43 +00:00
/// <param name="graphicsDevice">graphics device to be used to generate the texture.</param>
2018-12-12 02:03:35 +00:00
/// <returns>The texture from the region.</returns>
2019-03-24 00:04:43 +00:00
public Texture2D ObtainRegionAsTexture ( string name , GraphicsDevice graphicsDevice )
2018-12-05 01:19:31 +00:00
{
2018-12-12 02:03:35 +00:00
return dictionaryOfRegions [ name ] . AsTexture2D ( graphicsDevice ) ;
2018-12-05 01:19:31 +00:00
}
2019-01-29 21:55:56 +00:00
/// <summary>
/// Whether or not this atlas contains the given region name.
/// </summary>
/// <param name="regionName">The name of the region to check for.</param>
/// <returns>True if this atlas does contain the region given by name.</returns>
public bool ContainsRegion ( string regionName )
{
return dictionaryOfRegions . ContainsKey ( regionName ) ;
}
2019-01-15 03:06:00 +00:00
/// <summary>
/// Disposes unmanaged resources for the texture atlas.
/// </summary>
public void Dispose ( )
{
Dispose ( true ) ;
}
/// <summary>
/// Overridable disposal method.
/// </summary>
/// <param name="disposing">Only true if user calls <see cref="Dispose()"/></param>
public virtual void Dispose ( bool disposing )
{
disposed = true ;
if ( ! disposed & & disposing )
{
texture . Dispose ( ) ;
for ( int i = 0 ; i < dictionaryOfRegions . Count ; i + + )
{
2019-01-20 07:08:38 +00:00
Region region = dictionaryOfRegions . ElementAt ( i ) . Value ;
if ( ! region . Disposed )
{
dictionaryOfRegions . ElementAt ( i ) . Value . Dispose ( ) ;
}
2019-01-15 03:06:00 +00:00
}
dictionaryOfRegions . Clear ( ) ;
}
}
/// <summary>
/// Destructor.
/// </summary>
~ TextureAtlas ( )
{
Dispose ( false ) ;
}
2019-01-15 02:00:11 +00:00
/// <summary>
/// A region of a <see cref="TextureAtlas"/>.
/// </summary>
2019-03-24 00:04:43 +00:00
public class Region : ISpecialDrawable , IDisposable
2018-12-05 01:19:31 +00:00
{
2019-01-15 02:00:11 +00:00
/// <summary>
/// The name of the region. Mostly used to be refered to within the context of a <see cref="TextureAtlas"/>.
/// </summary>
2018-12-05 06:03:00 +00:00
public readonly string name ;
2019-01-15 03:06:00 +00:00
2019-01-15 02:00:11 +00:00
/// <summary>
/// The location and dimensions of where the original texture resides on the texture representing the atlas.
/// </summary>
2018-12-05 08:28:09 +00:00
public readonly Rectangle sourceRectangle ;
2018-12-05 06:03:00 +00:00
readonly NinePatch ninepatch ;
2018-12-05 08:28:09 +00:00
Texture2D atlasTexture ;
2018-12-05 01:19:31 +00:00
Texture2D regionTexture ;
2019-01-20 07:08:38 +00:00
/// <summary>
/// If region has already been disposed.
/// </summary>
public bool Disposed { get ; private set ; }
2018-12-05 01:19:31 +00:00
2018-12-12 02:03:35 +00:00
/// <summary>
/// A specified region in a texture atlas.
/// </summary>
/// <param name="name">Name of region.</param>
/// <param name="sourceRegion">The location of the region on the atlas.</param>
/// <param name="ninePatch">A <see cref="NinePatch"/> definition for the region.</param>
/// <param name="atlasTexture">The texture that holds the image data for the atlas.</param>
2019-01-16 01:34:47 +00:00
public Region ( string name , Rectangle sourceRegion , NinePatch ninePatch , Texture2D atlasTexture )
2018-12-05 06:03:00 +00:00
{
2018-12-12 02:03:35 +00:00
this . atlasTexture = atlasTexture ? ? throw new ArgumentNullException ( "Name parameters can be null." ) ;
this . name = name ? ? throw new ArgumentNullException ( "Name parameters can be null." ) ;
2019-03-24 00:04:43 +00:00
sourceRectangle = sourceRegion ;
ninepatch = ninePatch ;
2018-12-05 06:03:00 +00:00
}
2019-01-15 02:00:11 +00:00
/// <summary>
/// Draws the region. If ninepatch, rotation and origin are ignored.
/// </summary>
/// <param name="batch">The batch to use. Should be began.</param>
/// <param name="destination">The destination rectangle to draw to.</param>
/// <param name="color">The color to use.</param>
/// <param name="rotation">Rotation of the final drawing. Ignored if is a 9patch.</param>
/// <param name="origin">The origin of the drawing. Ignored if is a 9patch.</param>
2019-03-24 00:04:43 +00:00
public void Draw ( ConsistentSpriteBatch batch , Rectangle destination , Color color , float rotation = 0 , Vector2 origin = default ( Vector2 ) )
2018-12-05 01:19:31 +00:00
{
2019-01-20 07:08:38 +00:00
if ( Disposed ) throw new ObjectDisposedException ( GetType ( ) . Name ) ;
2019-01-15 02:00:11 +00:00
2018-12-14 06:42:51 +00:00
if ( ninepatch ! = null )
{
2019-03-10 06:50:03 +00:00
ninepatch . Draw ( batch , Color . White , destination ) ;
2019-01-20 07:08:38 +00:00
}
else
2018-12-14 06:42:51 +00:00
{
batch . Draw ( atlasTexture , destination , sourceRectangle , color , rotation , origin , SpriteEffects . None , 0f ) ;
}
2018-12-05 01:19:31 +00:00
}
2018-12-12 02:03:35 +00:00
/// <summary>
/// Create or obtains a previously created texture of this region.
/// </summary>
2019-03-24 00:04:43 +00:00
/// <param name="graphicsDevice">The graphics device to use to create the texture.</param>
2018-12-12 02:03:35 +00:00
/// <returns>The texture of the region.</returns>
2019-03-24 00:04:43 +00:00
public Texture2D AsTexture2D ( GraphicsDevice graphicsDevice )
2018-12-05 01:19:31 +00:00
{
2019-01-20 07:08:38 +00:00
if ( Disposed ) throw new ObjectDisposedException ( GetType ( ) . Name ) ;
2019-01-15 02:00:11 +00:00
2018-12-05 01:19:31 +00:00
if ( regionTexture = = null )
{
Color [ ] data = new Color [ sourceRectangle . Width * sourceRectangle . Height ] ;
regionTexture = new Texture2D ( graphicsDevice , sourceRectangle . Width , sourceRectangle . Height ) ;
2018-12-12 02:03:35 +00:00
atlasTexture . GetData ( 0 , sourceRectangle , data , 0 , sourceRectangle . Width * sourceRectangle . Height ) ;
2018-12-05 01:19:31 +00:00
regionTexture . SetData ( data ) ;
}
return regionTexture ;
}
2019-01-15 02:00:11 +00:00
/// <summary>
/// Call this to dispose.
/// </summary>
2018-12-05 01:19:31 +00:00
public void Dispose ( )
{
2019-01-20 07:08:38 +00:00
if ( Disposed ) throw new ObjectDisposedException ( GetType ( ) . Name ) ;
2019-01-15 02:00:11 +00:00
Dispose ( true ) ;
GC . SuppressFinalize ( this ) ;
}
/// <summary>
/// Overridable dispose.
/// </summary>
/// <param name="disposing">Whether or not this was a user made call.</param>
public virtual void Dispose ( bool disposing )
{
2019-01-20 07:08:38 +00:00
if ( disposing & & ! Disposed )
2019-01-15 02:00:11 +00:00
{
regionTexture ? . Dispose ( ) ;
}
2019-01-20 07:08:38 +00:00
Disposed = true ;
2019-01-15 02:00:11 +00:00
}
2019-01-20 07:08:38 +00:00
2019-01-15 02:00:11 +00:00
/// <summary>
/// Destructor.
/// </summary>
2019-01-16 01:34:47 +00:00
~ Region ( )
2019-01-15 02:00:11 +00:00
{
Dispose ( false ) ;
2018-12-05 01:19:31 +00:00
}
}
}
}