2020-10-21 04:42:47 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2022-03-14 08:46:05 +08:00
using System.Collections.Generic ;
2020-10-21 04:42:47 +08:00
using System.Linq ;
using NUnit.Framework ;
using osu.Framework.Allocation ;
2022-03-14 08:35:23 +08:00
using osu.Framework.Extensions.IEnumerableExtensions ;
2020-10-21 04:42:47 +08:00
using osu.Framework.Graphics ;
using osu.Framework.Testing ;
2022-03-14 08:46:05 +08:00
using osu.Framework.Utils ;
2020-10-21 04:42:47 +08:00
using osu.Game.Rulesets ;
using osu.Game.Rulesets.Osu ;
using osu.Game.Skinning ;
using osu.Game.Storyboards ;
using osu.Game.Storyboards.Drawables ;
using osuTK ;
namespace osu.Game.Tests.Visual.Gameplay
{
public class TestSceneDrawableStoryboardSprite : SkinnableTestScene
{
protected override Ruleset CreateRulesetForSkinProvider ( ) = > new OsuRuleset ( ) ;
[Cached]
private Storyboard storyboard { get ; set ; } = new Storyboard ( ) ;
2022-03-14 08:46:05 +08:00
private IEnumerable < DrawableStoryboardSprite > sprites = > this . ChildrenOfType < DrawableStoryboardSprite > ( ) ;
2020-10-21 04:42:47 +08:00
[Test]
public void TestSkinSpriteDisallowedByDefault ( )
{
const string lookup_name = "hitcircleoverlay" ;
AddStep ( "allow skin lookup" , ( ) = > storyboard . UseSkinSprites = false ) ;
2021-06-02 15:04:53 +08:00
AddStep ( "create sprites" , ( ) = > SetContents ( _ = > createSprite ( lookup_name , Anchor . TopLeft , Vector2 . Zero ) ) ) ;
2020-10-21 04:42:47 +08:00
assertSpritesFromSkin ( false ) ;
}
[Test]
public void TestAllowLookupFromSkin ( )
{
const string lookup_name = "hitcircleoverlay" ;
AddStep ( "allow skin lookup" , ( ) = > storyboard . UseSkinSprites = true ) ;
2022-03-14 08:46:05 +08:00
AddStep ( "create sprites" , ( ) = > SetContents ( _ = > createSprite ( lookup_name , Anchor . TopLeft , Vector2 . Zero ) ) ) ;
2020-10-21 04:42:47 +08:00
assertSpritesFromSkin ( true ) ;
2022-03-14 08:46:05 +08:00
AddAssert ( "skinnable sprite has correct size" , ( ) = > sprites . Any ( s = > Precision . AlmostEquals ( s . ChildrenOfType < SkinnableSprite > ( ) . Single ( ) . Size , new Vector2 ( 128 , 128 ) ) ) ) ;
2020-10-21 04:42:47 +08:00
}
2022-03-14 08:35:23 +08:00
[Test]
public void TestFlippedSprite ( )
{
const string lookup_name = "hitcircleoverlay" ;
AddStep ( "allow skin lookup" , ( ) = > storyboard . UseSkinSprites = true ) ;
AddStep ( "create sprites" , ( ) = > SetContents ( _ = > createSprite ( lookup_name , Anchor . TopLeft , Vector2 . Zero ) ) ) ;
AddStep ( "flip sprites" , ( ) = > sprites . ForEach ( s = >
{
s . FlipH = true ;
s . FlipV = true ;
} ) ) ;
AddAssert ( "origin flipped" , ( ) = > sprites . All ( s = > s . Origin = = Anchor . BottomRight ) ) ;
}
[Test]
public void TestNegativeScale ( )
{
const string lookup_name = "hitcircleoverlay" ;
AddStep ( "allow skin lookup" , ( ) = > storyboard . UseSkinSprites = true ) ;
AddStep ( "create sprites" , ( ) = > SetContents ( _ = > createSprite ( lookup_name , Anchor . TopLeft , Vector2 . Zero ) ) ) ;
AddStep ( "scale sprite" , ( ) = > sprites . ForEach ( s = > s . VectorScale = new Vector2 ( - 1 ) ) ) ;
AddAssert ( "origin flipped" , ( ) = > sprites . All ( s = > s . Origin = = Anchor . BottomRight ) ) ;
}
[Test]
public void TestNegativeScaleWithFlippedSprite ( )
{
const string lookup_name = "hitcircleoverlay" ;
AddStep ( "allow skin lookup" , ( ) = > storyboard . UseSkinSprites = true ) ;
AddStep ( "create sprites" , ( ) = > SetContents ( _ = > createSprite ( lookup_name , Anchor . TopLeft , Vector2 . Zero ) ) ) ;
AddStep ( "scale sprite" , ( ) = > sprites . ForEach ( s = > s . VectorScale = new Vector2 ( - 1 ) ) ) ;
AddAssert ( "origin flipped" , ( ) = > sprites . All ( s = > s . Origin = = Anchor . BottomRight ) ) ;
AddStep ( "flip sprites" , ( ) = > sprites . ForEach ( s = >
{
s . FlipH = true ;
s . FlipV = true ;
} ) ) ;
AddAssert ( "origin back" , ( ) = > sprites . All ( s = > s . Origin = = Anchor . TopLeft ) ) ;
}
2020-10-21 04:42:47 +08:00
private DrawableStoryboardSprite createSprite ( string lookupName , Anchor origin , Vector2 initialPosition )
= > new DrawableStoryboardSprite (
new StoryboardSprite ( lookupName , origin , initialPosition )
) . With ( s = >
{
s . LifetimeStart = double . MinValue ;
s . LifetimeEnd = double . MaxValue ;
} ) ;
private void assertSpritesFromSkin ( bool fromSkin ) = >
AddAssert ( $"sprites are {(fromSkin ? " from skin " : " from storyboard ")}" ,
2022-03-14 08:46:05 +08:00
( ) = > sprites . All ( sprite = > sprite . ChildrenOfType < SkinnableSprite > ( ) . Any ( ) = = fromSkin ) ) ;
2020-10-21 04:42:47 +08:00
}
}