2019-08-09 15:15:22 +02:00
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Graphics ;
2019-11-02 14:53:59 +01:00
using MLEM.Extensions ;
2021-11-29 21:24:08 +01:00
using MLEM.Graphics ;
2019-11-02 14:53:59 +01:00
using MLEM.Misc ;
2019-08-09 15:15:22 +02:00
namespace MLEM.Textures {
2020-05-21 17:21:34 +02:00
/// <summary>
/// This class represents a part of a texture.
/// </summary>
2020-03-21 00:49:43 +01:00
public class TextureRegion : GenericDataHolder {
2019-08-09 15:15:22 +02:00
2020-05-21 17:21:34 +02:00
/// <summary>
/// The texture that this region is a part of
/// </summary>
2019-08-09 15:15:22 +02:00
public readonly Texture2D Texture ;
2020-05-21 17:21:34 +02:00
/// <summary>
/// The area that is covered by this texture region
/// </summary>
2019-08-09 15:15:22 +02:00
public readonly Rectangle Area ;
2020-05-21 17:21:34 +02:00
/// <summary>
/// The top left corner of this texture region
/// </summary>
2019-08-09 15:15:22 +02:00
public Point Position = > this . Area . Location ;
2020-05-21 17:21:34 +02:00
/// <summary>
/// The x coordinate of the top left corner of this texture region
/// </summary>
2019-08-09 15:15:22 +02:00
public int U = > this . Area . X ;
2020-05-21 17:21:34 +02:00
/// <summary>
/// The y coordinate of the top left corner of this texture region
/// </summary>
2019-08-09 15:15:22 +02:00
public int V = > this . Area . Y ;
2020-05-21 17:21:34 +02:00
/// <summary>
/// The size of this texture region
/// </summary>
2022-06-24 14:01:26 +02:00
public Point Size = > new Point ( this . Area . Width , this . Area . Height ) ;
2020-05-21 17:21:34 +02:00
/// <summary>
/// The width of this texture region
/// </summary>
2019-08-09 15:15:22 +02:00
public int Width = > this . Area . Width ;
2020-05-21 17:21:34 +02:00
/// <summary>
/// The height of this texture region
/// </summary>
2019-08-09 15:15:22 +02:00
public int Height = > this . Area . Height ;
2020-07-23 23:30:44 +02:00
/// <summary>
/// The pivot point of this texture region, where 0, 0 is the top left and 1, 1 is the bottom right of the texture.
/// When drawing, this will be seen as the origin from where to start drawing.
/// </summary>
public Vector2 Pivot = Vector2 . Zero ;
/// <summary>
/// The <see cref="Pivot"/> of this texture region, but in absolute pixels rather than percentage.
/// </summary>
public Vector2 PivotPixels {
2022-06-24 14:01:26 +02:00
get = > this . Pivot * new Vector2 ( this . Size . X , this . Size . Y ) ;
set = > this . Pivot = value / new Vector2 ( this . Size . X , this . Size . Y ) ;
2020-07-23 23:30:44 +02:00
}
/// <summary>
/// The name of this texture region. By default, this name is <see cref="string.Empty"/>.
/// </summary>
public string Name = string . Empty ;
2019-08-09 15:15:22 +02:00
2020-05-21 17:21:34 +02:00
/// <summary>
/// Creates a new texture region from a texture and a rectangle which defines the region's area
/// </summary>
/// <param name="texture">The texture to use</param>
/// <param name="area">The area that this texture region should cover</param>
2019-08-09 15:15:22 +02:00
public TextureRegion ( Texture2D texture , Rectangle area ) {
this . Texture = texture ;
this . Area = area ;
}
2020-05-21 17:21:34 +02:00
/// <summary>
/// Creates a new texture region that spans the entire texture
/// </summary>
/// <param name="texture">The texture to use</param>
2021-12-28 14:56:11 +01:00
public TextureRegion ( Texture2D texture ) : this ( texture , new Rectangle ( 0 , 0 , texture . Width , texture . Height ) ) { }
2019-08-09 15:15:22 +02:00
2020-05-21 17:21:34 +02:00
/// <summary>
/// Creates a new texture region based on a texture and area coordinates
/// </summary>
/// <param name="texture">The texture to use</param>
/// <param name="u">The x coordinate of the top left corner of this area</param>
/// <param name="v">The y coordinate of the top left corner of this area</param>
/// <param name="width">The width of this area</param>
/// <param name="height">The height of this area</param>
2021-12-28 14:56:11 +01:00
public TextureRegion ( Texture2D texture , int u , int v , int width , int height ) : this ( texture , new Rectangle ( u , v , width , height ) ) { }
2019-08-09 15:15:22 +02:00
2020-05-21 17:21:34 +02:00
/// <summary>
/// Creates a new texture region based on a texture, a position and a size
/// </summary>
/// <param name="texture">The texture to use</param>
/// <param name="uv">The top left corner of this area</param>
/// <param name="size">The size of this area</param>
2022-06-24 14:01:26 +02:00
public TextureRegion ( Texture2D texture , Point uv , Point size ) : this ( texture , new Rectangle ( uv . X , uv . Y , size . X , size . Y ) ) { }
2020-12-19 14:34:30 +01:00
/// <summary>
/// Creates a new texture region which is a sub-region of the given texture region
/// </summary>
/// <param name="region">The texture region to create a sub-region of</param>
/// <param name="area">The new texture region area</param>
2022-06-24 14:01:26 +02:00
public TextureRegion ( TextureRegion region , Rectangle area ) : this ( region , area . Location , new Point ( area . Width , area . Height ) ) { }
2020-12-19 14:34:30 +01:00
/// <summary>
/// Creates a new texture region which is a sub-region of the given texture region
/// </summary>
/// <param name="region">The texture region to create a sub-region of</param>
/// <param name="u">The x coordinate of the top left corner of this area</param>
/// <param name="v">The y coordinate of the top left corner of this area</param>
/// <param name="width">The width of this area</param>
/// <param name="height">The height of this area</param>
2021-12-28 14:56:11 +01:00
public TextureRegion ( TextureRegion region , int u , int v , int width , int height ) : this ( region , new Point ( u , v ) , new Point ( width , height ) ) { }
2020-12-19 14:34:30 +01:00
/// <summary>
/// Creates a new texture region which is a sub-region of the given texture region
/// </summary>
/// <param name="region">The texture region to create a sub-region of</param>
/// <param name="uv">The top left corner of this area</param>
/// <param name="size">The size of this area</param>
2021-12-28 14:56:11 +01:00
public TextureRegion ( TextureRegion region , Point uv , Point size ) : this ( region . Texture , region . Position + uv , size ) { }
2019-08-09 15:15:22 +02:00
2022-01-22 16:51:42 +01:00
/// <summary>
/// Returns a new <see cref="TextureRegion"/> that has the same <see cref="Texture"/>, <see cref="Pivot"/> and <see cref="Size"/> as this texture, but the returned region's <see cref="Position"/> will be offset by <paramref name="offset"/>.
/// Note that the <see cref="Name"/> is not preserved in the copy.
/// </summary>
/// <param name="offset">The offset to apply to the <see cref="Position"/></param>
/// <returns>An offset copy of this texture region</returns>
public TextureRegion OffsetCopy ( Point offset ) {
return new TextureRegion ( this . Texture , this . Position + offset , this . Size ) { Pivot = this . Pivot } ;
}
2019-08-09 15:15:22 +02:00
}
2020-05-21 17:21:34 +02:00
/// <summary>
/// This class provides a set of extension methods for dealing with <see cref="TextureRegion"/>
/// </summary>
2019-08-09 15:15:22 +02:00
public static class TextureRegionExtensions {
2020-05-21 17:21:34 +02:00
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2019-08-09 15:15:22 +02:00
public static void Draw ( this SpriteBatch batch , TextureRegion texture , Vector2 position , Color color , float rotation , Vector2 origin , Vector2 scale , SpriteEffects effects , float layerDepth ) {
2020-07-23 23:30:44 +02:00
batch . Draw ( texture . Texture , position , texture . Area , color , rotation , origin + texture . PivotPixels , scale , effects , layerDepth ) ;
2019-08-09 15:15:22 +02:00
}
2020-05-21 17:21:34 +02:00
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2019-08-09 15:15:22 +02:00
public static void Draw ( this SpriteBatch batch , TextureRegion texture , Vector2 position , Color color , float rotation , Vector2 origin , float scale , SpriteEffects effects , float layerDepth ) {
2019-09-24 20:23:05 +02:00
batch . Draw ( texture , position , color , rotation , origin , new Vector2 ( scale ) , effects , layerDepth ) ;
2019-08-09 15:15:22 +02:00
}
2020-05-21 17:21:34 +02:00
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2019-08-09 15:15:22 +02:00
public static void Draw ( this SpriteBatch batch , TextureRegion texture , Rectangle destinationRectangle , Color color , float rotation , Vector2 origin , SpriteEffects effects , float layerDepth ) {
2020-07-23 23:30:44 +02:00
batch . Draw ( texture . Texture , destinationRectangle , texture . Area , color , rotation , origin + texture . PivotPixels , effects , layerDepth ) ;
2019-08-09 15:15:22 +02:00
}
2020-05-21 17:21:34 +02:00
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2019-11-02 14:53:59 +01:00
public static void Draw ( this SpriteBatch batch , TextureRegion texture , RectangleF destinationRectangle , Color color , float rotation , Vector2 origin , SpriteEffects effects , float layerDepth ) {
2020-07-23 23:30:44 +02:00
batch . Draw ( texture . Texture , destinationRectangle , texture . Area , color , rotation , origin + texture . PivotPixels , effects , layerDepth ) ;
2019-11-02 14:53:59 +01:00
}
2020-05-21 17:21:34 +02:00
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2019-08-09 15:15:22 +02:00
public static void Draw ( this SpriteBatch batch , TextureRegion texture , Vector2 position , Color color ) {
batch . Draw ( texture , position , color , 0 , Vector2 . Zero , Vector2 . One , SpriteEffects . None , 0 ) ;
}
2020-05-21 17:21:34 +02:00
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2019-08-09 15:15:22 +02:00
public static void Draw ( this SpriteBatch batch , TextureRegion texture , Rectangle destinationRectangle , Color color ) {
batch . Draw ( texture , destinationRectangle , color , 0 , Vector2 . Zero , SpriteEffects . None , 0 ) ;
}
2020-05-21 17:21:34 +02:00
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2019-11-02 14:53:59 +01:00
public static void Draw ( this SpriteBatch batch , TextureRegion texture , RectangleF destinationRectangle , Color color ) {
batch . Draw ( texture , destinationRectangle , color , 0 , Vector2 . Zero , SpriteEffects . None , 0 ) ;
}
2021-10-17 23:20:05 +02:00
/// <inheritdoc cref="StaticSpriteBatch.Add(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2021-12-03 19:57:26 +01:00
public static StaticSpriteBatch . Item Add ( this StaticSpriteBatch batch , TextureRegion texture , Vector2 position , Color color , float rotation , Vector2 origin , Vector2 scale , SpriteEffects effects , float layerDepth ) {
return batch . Add ( texture . Texture , position , texture . Area , color , rotation , origin + texture . PivotPixels , scale , effects , layerDepth ) ;
2021-10-17 23:20:05 +02:00
}
/// <inheritdoc cref="StaticSpriteBatch.Add(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2021-12-03 19:57:26 +01:00
public static StaticSpriteBatch . Item Add ( this StaticSpriteBatch batch , TextureRegion texture , Vector2 position , Color color , float rotation , Vector2 origin , float scale , SpriteEffects effects , float layerDepth ) {
return batch . Add ( texture , position , color , rotation , origin , new Vector2 ( scale ) , effects , layerDepth ) ;
2021-10-17 23:20:05 +02:00
}
/// <inheritdoc cref="StaticSpriteBatch.Add(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2021-12-03 19:57:26 +01:00
public static StaticSpriteBatch . Item Add ( this StaticSpriteBatch batch , TextureRegion texture , Rectangle destinationRectangle , Color color , float rotation , Vector2 origin , SpriteEffects effects , float layerDepth ) {
return batch . Add ( texture . Texture , destinationRectangle , texture . Area , color , rotation , origin + texture . PivotPixels , effects , layerDepth ) ;
2021-10-17 23:20:05 +02:00
}
/// <inheritdoc cref="StaticSpriteBatch.Add(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2021-12-03 19:57:26 +01:00
public static StaticSpriteBatch . Item Add ( this StaticSpriteBatch batch , TextureRegion texture , RectangleF destinationRectangle , Color color , float rotation , Vector2 origin , SpriteEffects effects , float layerDepth ) {
return batch . Add ( texture . Texture , destinationRectangle , texture . Area , color , rotation , origin + texture . PivotPixels , effects , layerDepth ) ;
2021-10-17 23:20:05 +02:00
}
/// <inheritdoc cref="StaticSpriteBatch.Add(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2021-12-03 19:57:26 +01:00
public static StaticSpriteBatch . Item Add ( this StaticSpriteBatch batch , TextureRegion texture , Vector2 position , Color color ) {
return batch . Add ( texture , position , color , 0 , Vector2 . Zero , Vector2 . One , SpriteEffects . None , 0 ) ;
2021-10-17 23:20:05 +02:00
}
/// <inheritdoc cref="StaticSpriteBatch.Add(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2021-12-03 19:57:26 +01:00
public static StaticSpriteBatch . Item Add ( this StaticSpriteBatch batch , TextureRegion texture , Rectangle destinationRectangle , Color color ) {
return batch . Add ( texture , destinationRectangle , color , 0 , Vector2 . Zero , SpriteEffects . None , 0 ) ;
2021-10-17 23:20:05 +02:00
}
/// <inheritdoc cref="StaticSpriteBatch.Add(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
2021-12-03 19:57:26 +01:00
public static StaticSpriteBatch . Item Add ( this StaticSpriteBatch batch , TextureRegion texture , RectangleF destinationRectangle , Color color ) {
return batch . Add ( texture , destinationRectangle , color , 0 , Vector2 . Zero , SpriteEffects . None , 0 ) ;
2021-10-17 23:20:05 +02:00
}
2019-08-09 15:15:22 +02:00
}
2022-06-17 18:23:47 +02:00
}