mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 12:06:37 +00:00
preserved origins on texture region changes, added some unit tests around sprites and fixed a bug in the origin property
This commit is contained in:
+1
-1
@@ -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
|
||||
@@ -48,10 +48,11 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Graphics\BoxingViewportAdapterTests.cs" />
|
||||
<Compile Include="Graphics\Camera2DTests.cs" />
|
||||
<Compile Include="Graphics\DefaultViewportAdapterTests.cs" />
|
||||
<Compile Include="Graphics\Texture2DTests.cs" />
|
||||
<Compile Include="Sprites\SpriteTests.cs" />
|
||||
<Compile Include="ViewportAdapters\BoxingViewportAdapterTests.cs" />
|
||||
<Compile Include="Camera2DTests.cs" />
|
||||
<Compile Include="ViewportAdapters\DefaultViewportAdapterTests.cs" />
|
||||
<Compile Include="TextureAtlases\Texture2DTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TestHelper.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -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<Texture2D>(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<Texture2D>(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<Texture2D>(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<Texture2D>(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<Texture2D>(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<Texture2D>(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<Texture2D>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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
|
||||
+1
-1
@@ -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
|
||||
+1
-1
@@ -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
|
||||
@@ -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); }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user