Files
MonoGame.Extended/source/MonoGame.Extended/Sprites/Sprite.cs
T
Christopher Whitley 8850875fb4 Texture Atlas Refactor (#890)
* Moved `TextureRegion2D` and `NinePatchRegion2D` into Graphics namespace
These are used for more than just texture atlas related things and are primarly for working with `Texture2D` instances and creating sub textures from them.  Moving them to Graphics namespace where `Texture2D` lives in MonoGame

* Dropped the `2D` suffix from `TextureRegion2D` and `NinePatchRegion2D`

* Moved `RectanglExtensions` to project root
This moves the extensions for `Rectangle` to the same relative namespace location as `Rectangle` is in MonoGame.

* Renamed to `Rectangle.Extensions.cs`

* Separate `Rectangle` from `RectangleF` extensions

* Added `GetRelativeRectangle` method

* Updated documentation

* Added `GetRelativeRectangle` method

* Updated documentation

* Top-level namespace

* Remove whitespace

* Move properties up

* Top-level namespace

* Don't recreate structs for `Bounds` and Size`
By having the property return a new struct, this allocates on the stack during situations like a loop during draw.

* Size should be `Size` not `SizeF` value
Textures deal in pixels (ints not floats)

* Removed setter for `Texture` property
Texture should not be set after region is created

* Added UV properties

* Refactor constructors

* Added documentation

* Added license header

* Make it an actual extension method

* Added `GetSubRegion` extension methods for `TextureRegion`

* Update documentation about ObjectDisposedException

* Move const values to top

* Resolve errors from`TextureRegion` refactor

* Update unit tests for TextureRegion refactor

* Started NinePatch rework

* Refactored NinePatch

* Update for texture region constructor change

* Remove ninepatch from atlas

* Moved `TextureAtlasExtensions` into `SpriteBatch.Extensions`
These are extension methods for the sprite batch

* Updated to use new `DrawTextureRegion` method after rename

* Renamed to NinePatchJsonConverter

* Removed `GetRegion<T>`
TextureAtlas no longer contains nine patch regions, so this method isn't needed

* Update after `NinePatchRegion2DJsonConverter` name change

* Resolve errors from NinePatch refactor

* TextureAtlas rework

* Renamed `TextureRegion` to `Texture2DRegion`

* Moved ContentReaders to new Content\ContentReaders directory

* Moved TextureAtlasJsonConvert to Serialization directory

* Adjusted name from `Texture*` to `Texture2D*`

* Update runtimereader and runtimettype strings
2024-06-15 01:37:18 -04:00

110 lines
3.4 KiB
C#

using System;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Graphics;
namespace MonoGame.Extended.Sprites
{
public class Sprite : IColorable
{
private Texture2DRegion _textureRegion;
public Sprite(Texture2DRegion textureRegion)
{
if (textureRegion == null) throw new ArgumentNullException(nameof(textureRegion));
_textureRegion = textureRegion;
Alpha = 1.0f;
Color = Color.White;
IsVisible = true;
Effect = SpriteEffects.None;
OriginNormalized = new Vector2(0.5f, 0.5f);
Depth = 0.0f;
}
public Sprite(Texture2D texture)
: this(new Texture2DRegion(texture))
{
}
public float Alpha { get; set; }
public float Depth { get; set; }
public object Tag { get; set; }
public Vector2 OriginNormalized
{
get => new Vector2(Origin.X/TextureRegion.Width, Origin.Y/TextureRegion.Height);
set => Origin = new Vector2(value.X*TextureRegion.Width, value.Y*TextureRegion.Height);
}
public Color Color { get; set; }
public RectangleF GetBoundingRectangle(Transform2 transform)
{
return GetBoundingRectangle(transform.Position, transform.Rotation, transform.Scale);
}
public RectangleF GetBoundingRectangle(Vector2 position, float rotation, Vector2 scale)
{
var corners = GetCorners(position, rotation, scale);
var min = new Vector2(corners.Min(i => i.X), corners.Min(i => i.Y));
var max = new Vector2(corners.Max(i => i.X), corners.Max(i => i.Y));
return new RectangleF(min.X, min.Y, max.X - min.X, max.Y - min.Y);
}
public bool IsVisible { get; set; }
public Vector2 Origin { get; set; }
public SpriteEffects Effect { get; set; }
public Texture2DRegion TextureRegion
{
get => _textureRegion;
set
{
if (value == null)
throw new InvalidOperationException("TextureRegion cannot be null");
// preserve the origin if the texture size changes
var originNormalized = OriginNormalized;
_textureRegion = value;
OriginNormalized = originNormalized;
}
}
public Vector2[] GetCorners(Vector2 position, float rotation, Vector2 scale)
{
var min = -Origin;
var max = min + new Vector2(TextureRegion.Width, TextureRegion.Height);
var offset = position;
if (scale != Vector2.One)
{
min *= scale;
max = max * scale;
}
var corners = new Vector2[4];
corners[0] = min;
corners[1] = new Vector2(max.X, min.Y);
corners[2] = max;
corners[3] = new Vector2(min.X, max.Y);
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (rotation != 0)
{
var matrix = Matrix.CreateRotationZ(rotation);
for (var i = 0; i < 4; i++)
corners[i] = Vector2.Transform(corners[i], matrix);
}
for (var i = 0; i < 4; i++)
corners[i] += offset;
return corners;
}
}
}