diff --git a/Source/MonoGame.Extended.Tests/Graphics/Camera2DTests.cs b/Source/MonoGame.Extended.Tests/Camera2DTests.cs similarity index 98% rename from Source/MonoGame.Extended.Tests/Graphics/Camera2DTests.cs rename to Source/MonoGame.Extended.Tests/Camera2DTests.cs index 15c482d0..de6b0234 100644 --- a/Source/MonoGame.Extended.Tests/Graphics/Camera2DTests.cs +++ b/Source/MonoGame.Extended.Tests/Camera2DTests.cs @@ -3,7 +3,7 @@ using MonoGame.Extended.ViewportAdapters; using NSubstitute; using NUnit.Framework; -namespace MonoGame.Extended.Tests.Graphics +namespace MonoGame.Extended.Tests { [TestFixture] public class Camera2DTests diff --git a/Source/MonoGame.Extended.Tests/MonoGame.Extended.Tests.csproj b/Source/MonoGame.Extended.Tests/MonoGame.Extended.Tests.csproj index 925b7214..0901dddb 100644 --- a/Source/MonoGame.Extended.Tests/MonoGame.Extended.Tests.csproj +++ b/Source/MonoGame.Extended.Tests/MonoGame.Extended.Tests.csproj @@ -48,10 +48,11 @@ - - - - + + + + + diff --git a/Source/MonoGame.Extended.Tests/Sprites/SpriteTests.cs b/Source/MonoGame.Extended.Tests/Sprites/SpriteTests.cs new file mode 100644 index 00000000..11e2b12e --- /dev/null +++ b/Source/MonoGame.Extended.Tests/Sprites/SpriteTests.cs @@ -0,0 +1,103 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.Sprites; +using MonoGame.Extended.TextureAtlases; +using NSubstitute; +using NUnit.Framework; + +namespace MonoGame.Extended.Tests.Sprites +{ + [TestFixture] + public class SpriteTests + { + [Test] + public void Sprite_BoundingRectangleAfterPosition_Test() + { + var graphicsDevice = TestHelper.CreateGraphicsDevice(); + var texture = Substitute.For(graphicsDevice, 50, 200); + var sprite = new Sprite(texture) + { + Position = new Vector2(400, 240) + }; + + Assert.AreEqual(new Rectangle(375, 140, 50, 200), sprite.GetBoundingRectangle()); + } + + [Test] + public void Sprite_BoundingRectangleAfterOrigin_Test() + { + var graphicsDevice = TestHelper.CreateGraphicsDevice(); + var texture = Substitute.For(graphicsDevice, 50, 200); + var sprite = new Sprite(texture) + { + OriginNormalized = new Vector2(1.0f, 1.0f) + }; + + Assert.AreEqual(new Rectangle(-50, -200, 50, 200), sprite.GetBoundingRectangle()); + } + + [Test] + public void Sprite_BoundingRectangleAfterScale_Test() + { + var graphicsDevice = TestHelper.CreateGraphicsDevice(); + var texture = Substitute.For(graphicsDevice, 50, 200); + var sprite = new Sprite(texture) + { + Scale = Vector2.One * 2.0f + }; + + Assert.AreEqual(new Rectangle(-50, -200, 100, 400), sprite.GetBoundingRectangle()); + } + + [Test] + public void Sprite_BoundingRectangleAfterRotation_Test() + { + var graphicsDevice = TestHelper.CreateGraphicsDevice(); + var texture = Substitute.For(graphicsDevice, 50, 200); + var sprite = new Sprite(texture) + { + Rotation = MathHelper.ToRadians(90) + }; + + Assert.AreEqual(new Rectangle(-100, -25, 200, 50), sprite.GetBoundingRectangle()); + } + + [Test] + public void Sprite_TextureRegionIsFullTextureWhenTextureConstructorIsUsed_Test() + { + var graphicsDevice = TestHelper.CreateGraphicsDevice(); + var texture = Substitute.For(graphicsDevice, 100, 200); + var sprite = new Sprite(texture); + + Assert.AreEqual(new Rectangle(0, 0, 100, 200), sprite.TextureRegion.Bounds); + } + + [Test] + public void Sprite_DefaultOriginIsCentre_Test() + { + var graphicsDevice = TestHelper.CreateGraphicsDevice(); + var texture = Substitute.For(graphicsDevice, 100, 200); + var sprite = new Sprite(texture); + + Assert.AreEqual(new Vector2(0.5f, 0.5f), sprite.OriginNormalized); + Assert.AreEqual(new Vector2(50, 100), sprite.Origin); + } + + [Test] + public void Sprite_PreserveNormalizedOriginWhenTextureRegionChanges_Test() + { + var graphicsDevice = TestHelper.CreateGraphicsDevice(); + var texture = Substitute.For(graphicsDevice, 100, 100); + var textureRegion = new TextureRegion2D(texture, 10, 20, 30, 40); + var sprite = new Sprite(textureRegion); + + Assert.AreEqual(new Vector2(0.5f, 0.5f), sprite.OriginNormalized); + Assert.AreEqual(new Vector2(15, 20), sprite.Origin); + + sprite.TextureRegion = new TextureRegion2D(texture, 30, 40, 50, 60); + + Assert.AreEqual(new Vector2(0.5f, 0.5f), sprite.OriginNormalized); + Assert.AreEqual(new Vector2(25, 30), sprite.Origin); + } + } +} diff --git a/Source/MonoGame.Extended.Tests/Graphics/Texture2DTests.cs b/Source/MonoGame.Extended.Tests/TextureAtlases/Texture2DTests.cs similarity index 96% rename from Source/MonoGame.Extended.Tests/Graphics/Texture2DTests.cs rename to Source/MonoGame.Extended.Tests/TextureAtlases/Texture2DTests.cs index 2db88060..b2463558 100644 --- a/Source/MonoGame.Extended.Tests/Graphics/Texture2DTests.cs +++ b/Source/MonoGame.Extended.Tests/TextureAtlases/Texture2DTests.cs @@ -2,7 +2,7 @@ using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended.TextureAtlases; using NUnit.Framework; -namespace MonoGame.Extended.Tests.Graphics +namespace MonoGame.Extended.Tests.TextureAtlases { [TestFixture] public class Texture2DTests diff --git a/Source/MonoGame.Extended.Tests/Graphics/BoxingViewportAdapterTests.cs b/Source/MonoGame.Extended.Tests/ViewportAdapters/BoxingViewportAdapterTests.cs similarity index 96% rename from Source/MonoGame.Extended.Tests/Graphics/BoxingViewportAdapterTests.cs rename to Source/MonoGame.Extended.Tests/ViewportAdapters/BoxingViewportAdapterTests.cs index 04d7006a..9542c0b2 100644 --- a/Source/MonoGame.Extended.Tests/Graphics/BoxingViewportAdapterTests.cs +++ b/Source/MonoGame.Extended.Tests/ViewportAdapters/BoxingViewportAdapterTests.cs @@ -2,7 +2,7 @@ using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended.ViewportAdapters; using NUnit.Framework; -namespace MonoGame.Extended.Tests.Graphics +namespace MonoGame.Extended.Tests.ViewportAdapters { [TestFixture] public class BoxingViewportAdapterTests diff --git a/Source/MonoGame.Extended.Tests/Graphics/DefaultViewportAdapterTests.cs b/Source/MonoGame.Extended.Tests/ViewportAdapters/DefaultViewportAdapterTests.cs similarity index 94% rename from Source/MonoGame.Extended.Tests/Graphics/DefaultViewportAdapterTests.cs rename to Source/MonoGame.Extended.Tests/ViewportAdapters/DefaultViewportAdapterTests.cs index 7a61e5a9..3c997ca2 100644 --- a/Source/MonoGame.Extended.Tests/Graphics/DefaultViewportAdapterTests.cs +++ b/Source/MonoGame.Extended.Tests/ViewportAdapters/DefaultViewportAdapterTests.cs @@ -3,7 +3,7 @@ using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended.ViewportAdapters; using NUnit.Framework; -namespace MonoGame.Extended.Tests.Graphics +namespace MonoGame.Extended.Tests.ViewportAdapters { [TestFixture] public class DefaultViewportAdapterTests diff --git a/Source/MonoGame.Extended/Sprites/Sprite.cs b/Source/MonoGame.Extended/Sprites/Sprite.cs index 2bdbcf5b..79489a08 100644 --- a/Source/MonoGame.Extended/Sprites/Sprite.cs +++ b/Source/MonoGame.Extended/Sprites/Sprite.cs @@ -12,7 +12,8 @@ namespace MonoGame.Extended.Sprites { if (textureRegion == null) throw new ArgumentNullException("textureRegion"); - TextureRegion = textureRegion; + _textureRegion = textureRegion; + Color = Color.White; IsVisible = true; Scale = Vector2.One; @@ -43,13 +44,16 @@ namespace MonoGame.Extended.Sprites 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 OriginNormalized { - get { return new Vector2(Origin.Y / TextureRegion.Width, Origin.Y / TextureRegion.Height); } + get { return new Vector2(Origin.X / TextureRegion.Width, Origin.Y / TextureRegion.Height); } set { Origin = new Vector2(value.X * TextureRegion.Width, value.Y * TextureRegion.Height); } }