Sprite Rework (#897)

* Add file header

* Refator to file-scoped namespace

* Least complex to most complext method ordering

* Refactor: Group properties at top before constructor

* Use the new UV properties from Texture2DRegion.
This removes the dependency on the texture size in the calculations if the TextureRegion property is changed.

* No longer need to cache normalized origin to preserve it

* Use ArgumentNullException instead

* Use ArgumentNullException.ThrowIfNull

* Refactor:  Use brackets for readability

* Add xml documentation

* Add check for disposed texture

* Moved `Sprite` to `MonoGame.Extended.Graphics`

* Moved extension methods for rendering sprite into `SpriteBatch.Extensions`

* `ISpriteBatchDrawable` is not used by anything
It was originally part of the ScreneGraph implementation in MonoGame.Extended which was replaced by the ECS system.  It appears that this interface was forgotten to be removed and is not used for anything anymore.

* Add `CreateSprite` methods

* Add file header

* Moved `SpriteSheetAnimationFrame` to `MonoGame.Extended.Graphics`

* Move properties before constructor.

* Changed `Duration` to `TimeSpan` type

* Renamed `Index` to `FrameIndex`

* Add `TextureRegion` property

* Make property get only

* Cleanup whitespace

* Remove `SpriteSheetAnimationFrameJsonConverter`

* Cleanup unused namespaces

* Make file-scoped namespace

* Added xml documentation

* Added file header

* marked constructor as internal.
This will be created from a spritesheet itself

* Move `SpriteSheetAnimationCycle` to `MonoGame.Extended.Graphcis`

* File scoped namespace

* Add file header

* Reorganize: Move properties above constructor

* Make `Frames` property a read-only span

* Remove duration, this is being moved to the frame itself

* Add `Name` property

* Set looping, reversed, and ping pong properties in ctor

* Make ctor internal
This will be an object created by a spritesheet instane itself.

* Cleanup: Whitespace

* Add `FrameCount` property

* Add `GetFrame` method and accompanying `this[int]` method.

* Documentation: Added missing documentation.

* WIP: Temporary commit for workstation change

* WIP: Temporary commit for workstation change

* WIP: Temporary commit for workstation change

* WIP: Temporary commit for workstation change

* Refactor: Animation Refactor Completed

* Refactor: Sprite stuff finished

* Remove tests that can't be run on GitHub CI
Need to find alternative

* Make optional name param the last param in Texture2DRegion ctors

* Resolve merge conflicts
This commit is contained in:
Christopher Whitley
2024-06-23 23:10:16 -04:00
committed by GitHub
parent 4ecb74f6b6
commit a501f76162
48 changed files with 1579 additions and 1907 deletions
@@ -0,0 +1,220 @@
// Copyright (c) Craftwork Games. All rights reserved.
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
using System;
using Microsoft.Xna.Framework;
using Xunit;
using MonoGame.Extended.Animations;
namespace MonoGame.Extended.Tests.Animations;
public class AnimationTests
{
private class TestAnimationFrame : IAnimationFrame
{
public int FrameIndex { get; set; }
public TimeSpan Duration { get; set; }
}
private class TestAnimationDefinition : IAnimationDefinition
{
private readonly IAnimationFrame[] _frames;
public string Name { get; set; }
public ReadOnlySpan<IAnimationFrame> Frames => _frames;
public int FrameCount { get; set; }
public bool IsLooping { get; set; }
public bool IsReversed { get; set; }
public bool IsPingPong { get; set; }
public TestAnimationDefinition(IAnimationFrame[] frames) => _frames = frames;
}
private readonly IAnimationDefinition _definition;
private readonly Animation _animation;
public AnimationTests()
{
var frames = new IAnimationFrame[]
{
new TestAnimationFrame { FrameIndex = 0, Duration = TimeSpan.FromSeconds(1) },
new TestAnimationFrame { FrameIndex = 1, Duration = TimeSpan.FromSeconds(1) }
};
_definition = new TestAnimationDefinition(frames)
{
FrameCount = frames.Length,
IsLooping = false,
IsReversed = false,
IsPingPong = false
};
_animation = new Animation(_definition);
}
[Fact]
public void Play_ShouldStartAnimation()
{
var result = _animation.Play();
Assert.True(result);
Assert.True(_animation.IsAnimating);
Assert.Equal(0, _animation.CurrentFrame);
}
[Fact]
public void Play_ShouldThrowException_ForInvalidFrame()
{
Assert.Throws<ArgumentOutOfRangeException>(() => _animation.Play(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => _animation.Play(10));
}
[Fact]
public void Pause_ShouldPauseAnimation()
{
_animation.Play();
var result = _animation.Pause();
Assert.True(result);
Assert.True(_animation.IsPaused);
}
[Fact]
public void Pause_ShouldResetFrameDuration_WhenSpecified()
{
_animation.Play();
_animation.Update(new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(0.5)));
var result = _animation.Pause(true);
Assert.True(result);
Assert.Equal(_definition.Frames[0].Duration, _animation.CurrentFrameTimeRemaining);
}
[Fact]
public void UnPause_ShouldResumeAnimation()
{
_animation.Play();
_animation.Pause();
var result = _animation.Unpause();
Assert.True(result);
Assert.False(_animation.IsPaused);
}
[Fact]
public void UnPause_ShouldAdvanceFrame_WhenSpecified()
{
_animation.Play();
_animation.Pause();
var result = _animation.Unpause(true);
Assert.True(result);
Assert.Equal(1, _animation.CurrentFrame);
}
[Fact]
public void Stop_ShouldStopAnimation()
{
_animation.Play();
var result = _animation.Stop();
Assert.True(result);
Assert.False(_animation.IsAnimating);
}
[Fact]
public void Update_ShouldAdvanceFrame()
{
_animation.Play();
_animation.Update(new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1.1)));
Assert.Equal(1, _animation.CurrentFrame);
}
[Fact]
public void Update_ShouldTriggerAnimationEvents()
{
bool frameBeginTriggered = false;
bool frameEndTriggered = false;
bool animationLoopTriggered = false;
bool animationCompletedTriggered = false;
_animation.OnAnimationEvent += (anim, trigger) =>
{
switch (trigger)
{
case AnimationEventTrigger.FrameBegin:
frameBeginTriggered = true;
break;
case AnimationEventTrigger.FrameEnd:
frameEndTriggered = true;
break;
case AnimationEventTrigger.AnimationLoop:
animationLoopTriggered = true;
break;
case AnimationEventTrigger.AnimationCompleted:
animationCompletedTriggered = true;
break;
}
};
_animation.Play();
_animation.Update(new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1.1)));
Assert.True(frameBeginTriggered);
Assert.True(frameEndTriggered);
_animation.IsLooping = true;
_animation.Update(new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1.1)));
Assert.True(animationLoopTriggered);
_animation.IsLooping = false;
_animation.Update(new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(2)));
Assert.True(animationCompletedTriggered);
}
[Fact]
public void Update_ShouldLoopAnimation()
{
_animation.IsLooping = true;
_animation.Play();
_animation.Update(new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(2.1)));
Assert.Equal(0, _animation.CurrentFrame);
}
[Fact]
public void Reset_ShouldResetAnimation()
{
_animation.Play();
_animation.Reset();
Assert.False(_animation.IsAnimating);
Assert.True(_animation.IsPaused);
Assert.Equal(0, _animation.CurrentFrame);
}
[Fact]
public void SetFrame_ShouldChangeCurrentFrame()
{
_animation.SetFrame(1);
Assert.Equal(1, _animation.CurrentFrame);
Assert.Equal(_definition.Frames[1].Duration, _animation.CurrentFrameTimeRemaining);
}
[Fact]
public void SetFrame_ShouldThrowException_ForInvalidFrame()
{
Assert.Throws<ArgumentOutOfRangeException>(() => _animation.SetFrame(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => _animation.SetFrame(10));
}
[Fact]
public void Dispose_ShouldDisposeAnimation()
{
_animation.Dispose();
Assert.True(_animation.IsDisposed);
}
}
@@ -1,5 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<Compile Remove="Sprites\**" />
<EmbeddedResource Remove="Sprites\**" />
<None Remove="Sprites\**" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\source\MonoGame.Extended\MonoGame.Extended.csproj" />
<ProjectReference Include="..\..\source\MonoGame.Extended.Particles\MonoGame.Extended.Particles.csproj" />
@@ -1,911 +0,0 @@
using System;
using Microsoft.Xna.Framework;
using MonoGame.Extended.Graphics;
using MonoGame.Extended.Sprites;
using MonoGame.Extended.TextureAtlases;
using Xunit;
namespace MonoGame.Extended.Tests.Sprites
{
public class SpriteSheetAnimationTests
{
[Theory]
[InlineData(0, 0)]
[InlineData(0, 0.9f)]
[InlineData(1, 1f)]
[InlineData(1, 1.9f)]
[InlineData(0, 2f)]
[InlineData(0, 2.9f)]
[InlineData(1, 3f)]
[InlineData(0, 4f)]
[InlineData(1, 5f)]
public void Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Fact]
public void Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete_Over_Multiple_Updates()
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(0));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Theory]
[InlineData(0, 0.9f)]
[InlineData(1, 1f)]
[InlineData(1, 1.1f)]
[InlineData(1, 1.9f)]
public void Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
false
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Theory]
[InlineData(1, 2f)]
[InlineData(1, 3f)]
[InlineData(1, 4f)]
public void Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Complete_When_AnimationDuration_Is_Reached(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
false
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.True(isCompleteFired);
}
[Fact]
public void Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Complete_When_AnimationDuration_Is_Reached_Over_Multiple_Updates()
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
false
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(0));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.True(isCompleteFired);
isCompleteFired = false; // Reset isCompleteFired for next execution
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired); // Event is not fired again as animation was already completed
}
[Theory]
[InlineData(1, 0)]
[InlineData(1, 0.9f)]
[InlineData(0, 1f)]
[InlineData(0, 1.9f)]
[InlineData(1, 2f)]
[InlineData(1, 2.9f)]
[InlineData(0, 3f)]
[InlineData(1, 4f)]
[InlineData(0, 5f)]
public void Reversed_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
true,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Fact]
public void Reversed_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete_Over_Multiple_Updates()
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
true,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(0));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Theory]
[InlineData(1, 0.9f)]
[InlineData(0, 1f)]
[InlineData(0, 1.1f)]
[InlineData(0, 1.9f)]
public void Reversed_Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
false,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Theory]
[InlineData(0, 2f)]
[InlineData(0, 3f)]
[InlineData(0, 4f)]
public void Reversed_Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Complete_When_AnimationDuration_Is_Reached(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
false,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.True(isCompleteFired);
}
[Fact]
public void Reversed_Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Complete_When_AnimationDuration_Is_Reached_Over_Multiple_Updates()
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
false,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(0));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.True(isCompleteFired);
isCompleteFired = false; // Reset isCompleteFired for next execution
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired); // Event is not fired again as animation was already completed;
}
[Theory]
[InlineData(0, 0)]
[InlineData(0, 0.9f)]
[InlineData(1, 1f)]
[InlineData(1, 1.9f)]
[InlineData(0, 2f)]
[InlineData(0, 2.9f)]
[InlineData(1, 3f)]
[InlineData(0, 4f)]
[InlineData(1, 5f)]
[InlineData(0, 6f)]
[InlineData(1, 7f)]
[InlineData(0, 8f)]
public void PingPong_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
true,
false,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Fact]
public void PingPong_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete_Over_Multiple_Updates()
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
true,
false,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(0));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Theory]
[InlineData(0, 0.9f)]
[InlineData(1, 1f)]
[InlineData(1, 1.9f)]
[InlineData(0, 2f)]
[InlineData(0, 2.9f)]
public void PingPong_Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
false,
false,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Theory]
[InlineData(0, 3f)]
[InlineData(0, 4f)]
[InlineData(0, 5f)]
public void PingPong_Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Complete_When_AnimationDuration_Is_Reached(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
false,
false,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.True(isCompleteFired);
}
[Fact]
public void PingPong_Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Complete_When_AnimationDuration_Is_Reached_Over_Multiple_Updates()
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegion2D3 = new Texture2DRegion("Region 3", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2, textureRegion2D3 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1, 2 },
1,
false,
false,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(0));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[2], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.True(isCompleteFired);
isCompleteFired = false; // Reset isCompleteFired for next execution
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired); // Event is not fired again as animation was already completed
}
[Theory]
[InlineData(1, 0)]
[InlineData(1, 0.9f)]
[InlineData(0, 1f)]
[InlineData(0, 1.9f)]
[InlineData(1, 2f)]
[InlineData(1, 2.9f)]
[InlineData(0, 3f)]
[InlineData(1, 4f)]
[InlineData(0, 5f)]
[InlineData(1, 6f)]
[InlineData(0, 7f)]
[InlineData(1, 8f)]
public void Reversed_PingPong_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
true,
true,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Fact]
public void Reversed_PingPong_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete_Over_Multiple_Updates()
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
true,
true,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(0));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Theory]
[InlineData(1, 0.9f)]
[InlineData(0, 1f)]
[InlineData(0, 1.9f)]
[InlineData(1, 2f)]
[InlineData(1, 2.9f)]
public void Reversed_PingPong_Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Not_Complete(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
false,
true,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
}
[Theory]
[InlineData(1, 3f)]
[InlineData(1, 4f)]
[InlineData(1, 5f)]
public void Reversed_PingPong_Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Complete_When_AnimationDuration_Is_Reached(int expectedTextureRegionIndex, float time)
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1 },
1,
false,
true,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(time));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[expectedTextureRegionIndex], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.True(isCompleteFired);
}
[Fact]
public void Reversed_PingPong_Non_Looping_SpriteSheetAnimation_Should_Return_Correct_Frame_And_Complete_When_AnimationDuration_Is_Reached_Over_Multiple_Updates()
{
var textureRegion2D1 = new Texture2DRegion("Region 1", new Rectangle());
var textureRegion2D2 = new Texture2DRegion("Region 2", new Rectangle());
var textureRegion2D3 = new Texture2DRegion("Region 3", new Rectangle());
var textureRegions = new[] { textureRegion2D1, textureRegion2D2, textureRegion2D3 };
var spriteSheetAnimationData = new SpriteSheetAnimationData(
new[] { 0, 1, 2 },
1,
false,
true,
true
);
var spriteSheetAnimation = new SpriteSheetAnimation("Test", textureRegions, spriteSheetAnimationData);
var isCompleteFired = false;
spriteSheetAnimation.OnCompleted += () => isCompleteFired = true;
spriteSheetAnimation.Play();
var gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(0));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[2], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[0], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[1], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[2], spriteSheetAnimation.CurrentFrame);
Assert.False(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired);
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[2], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.True(isCompleteFired);
isCompleteFired = false; // Reset isCompleteFired for next execution
gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromSeconds(1));
spriteSheetAnimation.Update(gameTime);
Assert.Equal(textureRegions[2], spriteSheetAnimation.CurrentFrame);
Assert.True(spriteSheetAnimation.IsComplete);
Assert.False(isCompleteFired); // Event is not fired again as animation was already completed
}
}
}
@@ -1,91 +0,0 @@
//using Microsoft.Xna.Framework;
//using Microsoft.Xna.Framework.Graphics;
//using MonoGame.Extended.Sprites;
//using MonoGame.Extended.TextureAtlases;
//using NSubstitute;
//using Xunit;
//namespace MonoGame.Extended.Tests.Sprites
//{
//
// public class SpriteTests
// {
// [Fact]
// public void Sprite_BoundingRectangleAfterPosition_Test()
// {
// var graphicsDevice = TestHelper.CreateGraphicsDevice();
// var texture = Substitute.For<Texture2D>(graphicsDevice, 50, 200);
// var sprite = new Sprite(texture);
// Assert.Equal(new RectangleF(375, 140, 50, 200), sprite.GetBoundingRectangle(new Vector2(400, 240), 0, Vector2.One));
// }
// [Fact]
// 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.Equal(new RectangleF(-50, -200, 50, 200), sprite.GetBoundingRectangle(Vector2.Zero, 0, Vector2.One));
// }
// [Fact]
// public void Sprite_BoundingRectangleAfterScale_Test()
// {
// var graphicsDevice = TestHelper.CreateGraphicsDevice();
// var texture = Substitute.For<Texture2D>(graphicsDevice, 50, 200);
// var sprite = new Sprite(texture);
// Assert.Equal(new RectangleF(-50, -200, 100, 400), sprite.GetBoundingRectangle(Vector2.Zero, 0, Vector2.One * 2.0f));
// }
// [Fact]
// public void Sprite_BoundingRectangleAfterRotation_Test()
// {
// var graphicsDevice = TestHelper.CreateGraphicsDevice();
// var texture = Substitute.For<Texture2D>(graphicsDevice, 50, 200);
// var sprite = new Sprite(texture);
// AssertExtensions.AreApproximatelyEqual(new RectangleF(-100, -25, 200, 50), sprite.GetBoundingRectangle(Vector2.Zero, MathHelper.ToRadians(90), Vector2.One * 2.0f));
// }
// [Fact]
// public void Sprite_TextureRegionIsFullTextureWhenTextureConstructorIsUsed_Test()
// {
// var graphicsDevice = TestHelper.CreateGraphicsDevice();
// var texture = Substitute.For<Texture2D>(graphicsDevice, 100, 200);
// var sprite = new Sprite(texture);
// Assert.Equal(new Rectangle(0, 0, 100, 200), sprite.TextureRegion.Bounds);
// }
// [Fact]
// public void Sprite_DefaultOriginIsCentre_Test()
// {
// var graphicsDevice = TestHelper.CreateGraphicsDevice();
// var texture = Substitute.For<Texture2D>(graphicsDevice, 100, 200);
// var sprite = new Sprite(texture);
// Assert.Equal(new Vector2(0.5f, 0.5f), sprite.OriginNormalized);
// Assert.Equal(new Vector2(50, 100), sprite.Origin);
// }
// [Fact]
// 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.Equal(new Vector2(0.5f, 0.5f), sprite.OriginNormalized);
// Assert.Equal(new Vector2(15, 20), sprite.Origin);
// sprite.TextureRegion = new TextureRegion2D(texture, 30, 40, 50, 60);
// Assert.Equal(new Vector2(0.5f, 0.5f), sprite.OriginNormalized);
// Assert.Equal(new Vector2(25, 30), sprite.Origin);
// }
// }
//}
@@ -1,12 +0,0 @@
using Microsoft.Xna.Framework.Graphics;
namespace MonoGame.Extended.Tests
{
public class TestGraphicsDevice : GraphicsDevice
{
public TestGraphicsDevice()
: base(GraphicsAdapter.DefaultAdapter, GraphicsProfile.Reach, new PresentationParameters())
{
}
}
}
@@ -1,27 +0,0 @@
//using Microsoft.Xna.Framework;
//using Microsoft.Xna.Framework.Graphics;
//using Xunit;
//namespace MonoGame.Extended.Tests
//{
// public static class TestHelper
// {
// public static void AreEqual(Vector3 a, Vector3 b, double delta)
// {
// Assert.Equal(a.X, b.X, delta);
// Assert.Equal(a.Y, b.Y, delta);
// Assert.Equal(a.Z, b.Z, delta);
// }
// public static GraphicsDevice CreateGraphicsDevice()
// {
// return new GraphicsDevice(
// GraphicsAdapter.DefaultAdapter,
// GraphicsProfile.HiDef,
// new PresentationParameters())
// {
// Viewport = new Viewport(0, 0, 800, 480)
// };
// }
// }
//}
@@ -1,236 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.TextureAtlases;
using Xunit;
namespace MonoGame.Extended.Tests.TextureAtlases
{
//public class TextureAtlasTests : IDisposable
//{
// private readonly TestGame _game;
// public TextureAtlasTests()
// {
// _game = new TestGame();
// }
// public void Dispose()
// {
// _game.Dispose();
// }
// [Fact]
// public void TextureAtlas_CreateRegion_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 100, 200))
// {
// var atlas = new TextureAtlas(null, texture);
// var region = atlas.CreateRegion("region0", 10, 20, 30, 40);
// Assert.Same(texture, region.Texture);
// Assert.Equal(10, region.X);
// Assert.Equal(20, region.Y);
// Assert.Equal(30, region.Width);
// Assert.Equal(40, region.Height);
// }
// }
// [Fact]
// public void TextureAtlas_GetRegionsByIndex_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 100, 200))
// {
// var atlas = new TextureAtlas(null, texture);
// var region0 = atlas.CreateRegion("region0", 10, 20, 30, 40);
// var region1 = atlas.CreateRegion("region1", 50, 60, 35, 45);
// Assert.Same(region0, atlas[0]);
// Assert.Same(region1, atlas[1]);
// Assert.Same(region0, atlas.GetRegion(0));
// Assert.Same(region1, atlas.GetRegion(1));
// }
// }
// [Fact]
// public void TextureAtlas_GetRegionsByName_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 100, 200))
// {
// var atlas = new TextureAtlas(null, texture);
// var region0 = atlas.CreateRegion("region0", 10, 20, 30, 40);
// var region1 = atlas.CreateRegion("region1", 50, 60, 35, 45);
// Assert.Same(region0, atlas["region0"]);
// Assert.Same(region1, atlas["region1"]);
// Assert.Same(region0, atlas.GetRegion("region0"));
// Assert.Same(region1, atlas.GetRegion("region1"));
// }
// }
// [Fact]
// public void TextureAtlas_RemoveRegions_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 100, 200))
// {
// var atlas = new TextureAtlas(null, texture);
// var region0 = atlas.CreateRegion("region0", 10, 20, 30, 40);
// var region1 = atlas.CreateRegion("region1", 50, 60, 35, 45);
// var region2 = atlas.CreateRegion("region2", 32, 33, 34, 35);
// Assert.Same(texture, atlas.Texture);
// Assert.Equal(3, atlas.RegionCount);
// Assert.Equal(atlas.RegionCount, atlas.Regions.Count());
// Assert.Same(region1, atlas[1]);
// atlas.RemoveRegion(1);
// Assert.Equal(2, atlas.Regions.Count());
// Assert.Same(region0, atlas[0]);
// Assert.Same(region2, atlas[1]);
// atlas.RemoveRegion("region0");
// Assert.Single(atlas.Regions);
// Assert.Same(region2, atlas[0]);
// }
// }
// [Fact]
// public void TextureAtlas_CreateRegionThatAlreadyExistsThrowsException_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 100, 200))
// {
// var atlas = new TextureAtlas(null, texture);
// atlas.CreateRegion("region0", 10, 20, 30, 40);
// Assert.Throws<InvalidOperationException>(() => atlas.CreateRegion("region0", 50, 60, 35, 45));
// }
// }
// [Fact]
// public void TextureAtlas_GetRegion_InvalidIndexThrowsException_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 100, 200))
// {
// var atlas = new TextureAtlas(null, texture);
// atlas.CreateRegion("region0", 10, 20, 30, 40);
// Assert.Throws<IndexOutOfRangeException>(() => atlas.GetRegion(-1));
// }
// }
// [Fact]
// public void TextureAtlas_GetRegion_InvalidNameThrowsException_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 100, 200))
// {
// var atlas = new TextureAtlas(null, texture);
// atlas.CreateRegion("region0", 10, 20, 30, 40);
// Assert.Throws<KeyNotFoundException>(() => atlas.GetRegion("region1"));
// }
// }
// [Fact]
// public void TextureAtlas_EnumerateRegions_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 100, 200))
// {
// var atlas = new TextureAtlas(null, texture);
// var regions = new TextureRegion2D[3];
// regions[0] = atlas.CreateRegion("region0", 10, 20, 30, 40);
// regions[1] = atlas.CreateRegion("region1", 50, 60, 35, 45);
// regions[2] = atlas.CreateRegion("region2", 32, 33, 34, 35);
// var index = 0;
// foreach (var region in atlas)
// {
// Assert.Same(region, regions[index]);
// index++;
// }
// }
// }
// [Fact]
// public void TextureAtlas_Create_WithDefaultParameters_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 50, 100) {Name = "testTexture"})
// {
// var atlas = TextureAtlas.Create(null, texture, 25, 50);
// Assert.Equal(4, atlas.RegionCount);
// Assert.True(atlas.Regions.All(i => i.Width == 25));
// Assert.True(atlas.Regions.All(i => i.Height == 50));
// Assert.True(atlas.Regions.All(i => ReferenceEquals(i.Texture, texture)));
// Assert.True(atlas.Regions.All(i => i.Name.StartsWith(texture.Name)));
// }
// }
// [Fact]
// public void TextureAtlas_Create_WithMaxRegionCount_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 64, 64))
// {
// var atlas = TextureAtlas.Create(null, texture, 32, 32, maxRegionCount: 3);
// Assert.Equal(3, atlas.RegionCount);
// }
// }
// [Fact]
// public void TextureAtlas_Create_WithMargin_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 24, 24))
// {
// var atlas = TextureAtlas.Create(null, texture, 10, 10, margin: 2);
// Assert.Equal(4, atlas.RegionCount);
// Assert.True(atlas.Regions.All(i => i.Width == 10 && i.Height == 10));
// Assert.Equal(2, atlas[0].X);
// Assert.Equal(2, atlas[0].Y);
// Assert.Equal(12, atlas[3].X);
// Assert.Equal(12, atlas[3].Y);
// }
// }
// [Fact]
// public void TextureAtlas_Create_WithSpacing_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 24, 24))
// {
// var atlas = TextureAtlas.Create(null, texture, 10, 10, spacing: 2);
// Assert.Equal(4, atlas.RegionCount);
// Assert.True(atlas.Regions.All(i => i.Width == 10 && i.Height == 10));
// Assert.Equal(0, atlas[0].X);
// Assert.Equal(0, atlas[0].Y);
// Assert.Equal(12, atlas[3].X);
// Assert.Equal(12, atlas[3].Y);
// }
// }
// [Fact]
// public void TextureAtlas_Create_WithMarginAndSpacing_Test()
// {
// using (var texture = new Texture2D(_game.GraphicsDevice, 28, 28))
// {
// var atlas = TextureAtlas.Create(null, texture, 10, 10, margin: 3, spacing: 2);
// Assert.Equal(4, atlas.RegionCount);
// Assert.True(atlas.Regions.All(i => i.Width == 10 && i.Height == 10));
// Assert.Equal(3, atlas[0].X);
// Assert.Equal(3, atlas[0].Y);
// Assert.Equal(15, atlas[3].X);
// Assert.Equal(15, atlas[3].Y);
// }
// }
//}
}
@@ -1,40 +0,0 @@
//using Microsoft.Xna.Framework.Graphics;
//using MonoGame.Extended.TextureAtlases;
//using Xunit;
//namespace MonoGame.Extended.Tests.TextureAtlases
//{
//
// public class TextureRegion2DTests
// {
// [Fact]
// public void TextureRegion2D_FromTexture_Test()
// {
// var graphicsDevice = TestHelper.CreateGraphicsDevice();
// var texture = new Texture2D(graphicsDevice, 100, 200);
// var textureRegion = new TextureRegion2D(texture);
// Assert.AreSame(texture, textureRegion.Texture);
// Assert.Equal(0, textureRegion.X);
// Assert.Equal(0, textureRegion.Y);
// Assert.Equal(100, textureRegion.Width);
// Assert.Equal(200, textureRegion.Height);
// Assert.IsNull(textureRegion.Tag);
// }
// [Fact]
// public void TextureRegion2D_Specified_Test()
// {
// var graphicsDevice = TestHelper.CreateGraphicsDevice();
// var texture = new Texture2D(graphicsDevice, 100, 200);
// var textureRegion = new TextureRegion2D(texture, 10, 20, 30, 40);
// Assert.AreSame(texture, textureRegion.Texture);
// Assert.Equal(10, textureRegion.X);
// Assert.Equal(20, textureRegion.Y);
// Assert.Equal(30, textureRegion.Width);
// Assert.Equal(40, textureRegion.Height);
// Assert.IsNull(textureRegion.Tag);
// }
// }
//}