using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Extensions; using MLEM.Graphics; using MLEM.Misc; namespace MLEM.Textures { /// /// This class represents a part of a texture. /// public class TextureRegion : GenericDataHolder { /// /// The texture that this region is a part of /// public readonly Texture2D Texture; /// /// The area that is covered by this texture region /// public readonly Rectangle Area; /// /// The top left corner of this texture region /// public Point Position => this.Area.Location; /// /// The x coordinate of the top left corner of this texture region /// public int U => this.Area.X; /// /// The y coordinate of the top left corner of this texture region /// public int V => this.Area.Y; /// /// The size of this texture region /// public Point Size => this.Area.Size; /// /// The width of this texture region /// public int Width => this.Area.Width; /// /// The height of this texture region /// public int Height => this.Area.Height; /// /// 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. /// public Vector2 Pivot = Vector2.Zero; /// /// The of this texture region, but in absolute pixels rather than percentage. /// public Vector2 PivotPixels { get => this.Pivot * this.Size.ToVector2(); set => this.Pivot = value / this.Size.ToVector2(); } /// /// The name of this texture region. By default, this name is . /// public string Name = string.Empty; /// /// Creates a new texture region from a texture and a rectangle which defines the region's area /// /// The texture to use /// The area that this texture region should cover public TextureRegion(Texture2D texture, Rectangle area) { this.Texture = texture; this.Area = area; } /// /// Creates a new texture region that spans the entire texture /// /// The texture to use public TextureRegion(Texture2D texture) : this(texture, new Rectangle(0, 0, texture.Width, texture.Height)) {} /// /// Creates a new texture region based on a texture and area coordinates /// /// The texture to use /// The x coordinate of the top left corner of this area /// The y coordinate of the top left corner of this area /// The width of this area /// The height of this area public TextureRegion(Texture2D texture, int u, int v, int width, int height) : this(texture, new Rectangle(u, v, width, height)) {} /// /// Creates a new texture region based on a texture, a position and a size /// /// The texture to use /// The top left corner of this area /// The size of this area public TextureRegion(Texture2D texture, Point uv, Point size) : this(texture, new Rectangle(uv, size)) {} /// /// Creates a new texture region which is a sub-region of the given texture region /// /// The texture region to create a sub-region of /// The new texture region area public TextureRegion(TextureRegion region, Rectangle area) : this(region, area.Location, area.Size) {} /// /// Creates a new texture region which is a sub-region of the given texture region /// /// The texture region to create a sub-region of /// The x coordinate of the top left corner of this area /// The y coordinate of the top left corner of this area /// The width of this area /// The height of this area public TextureRegion(TextureRegion region, int u, int v, int width, int height) : this(region, new Point(u, v), new Point(width, height)) {} /// /// Creates a new texture region which is a sub-region of the given texture region /// /// The texture region to create a sub-region of /// The top left corner of this area /// The size of this area public TextureRegion(TextureRegion region, Point uv, Point size) : this(region.Texture, region.Position + uv, size) {} } /// /// This class provides a set of extension methods for dealing with /// public static class TextureRegionExtensions { /// public static void Draw(this SpriteBatch batch, TextureRegion texture, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { batch.Draw(texture.Texture, position, texture.Area, color, rotation, origin + texture.PivotPixels, scale, effects, layerDepth); } /// public static void Draw(this SpriteBatch batch, TextureRegion texture, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) { batch.Draw(texture, position, color, rotation, origin, new Vector2(scale), effects, layerDepth); } /// public static void Draw(this SpriteBatch batch, TextureRegion texture, Rectangle destinationRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) { batch.Draw(texture.Texture, destinationRectangle, texture.Area, color, rotation, origin + texture.PivotPixels, effects, layerDepth); } /// public static void Draw(this SpriteBatch batch, TextureRegion texture, RectangleF destinationRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) { batch.Draw(texture.Texture, destinationRectangle, texture.Area, color, rotation, origin + texture.PivotPixels, effects, layerDepth); } /// 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); } /// public static void Draw(this SpriteBatch batch, TextureRegion texture, Rectangle destinationRectangle, Color color) { batch.Draw(texture, destinationRectangle, color, 0, Vector2.Zero, SpriteEffects.None, 0); } /// public static void Draw(this SpriteBatch batch, TextureRegion texture, RectangleF destinationRectangle, Color color) { batch.Draw(texture, destinationRectangle, color, 0, Vector2.Zero, SpriteEffects.None, 0); } /// 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); } /// 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); } /// 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); } /// 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); } /// 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); } /// 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); } /// 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); } } }