mirror of
https://github.com/ppy/osu
synced 2025-02-17 10:57:03 +00:00
Merge pull request #17239 from frenzibyte/storyboard-negative-scale
Fix drawable storyboard sprites not flipping origin on negative scale
This commit is contained in:
commit
8f9b72c2ee
@ -5,6 +5,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Testing;
|
using osu.Framework.Testing;
|
||||||
using osu.Framework.Utils;
|
using osu.Framework.Utils;
|
||||||
@ -52,6 +53,49 @@ namespace osu.Game.Tests.Visual.Gameplay
|
|||||||
AddAssert("skinnable sprite has correct size", () => sprites.Any(s => Precision.AlmostEquals(s.ChildrenOfType<SkinnableSprite>().Single().Size, new Vector2(128, 128))));
|
AddAssert("skinnable sprite has correct size", () => sprites.Any(s => Precision.AlmostEquals(s.ChildrenOfType<SkinnableSprite>().Single().Size, new Vector2(128, 128))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[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));
|
||||||
|
}
|
||||||
|
|
||||||
private DrawableStoryboardSprite createSprite(string lookupName, Anchor origin, Vector2 initialPosition)
|
private DrawableStoryboardSprite createSprite(string lookupName, Anchor origin, Vector2 initialPosition)
|
||||||
=> new DrawableStoryboardSprite(
|
=> new DrawableStoryboardSprite(
|
||||||
new StoryboardSprite(lookupName, origin, initialPosition)
|
new StoryboardSprite(lookupName, origin, initialPosition)
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Extensions.EnumExtensions;
|
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Animations;
|
using osu.Framework.Graphics.Animations;
|
||||||
using osu.Framework.Graphics.Textures;
|
using osu.Framework.Graphics.Textures;
|
||||||
@ -73,31 +72,7 @@ namespace osu.Game.Storyboards.Drawables
|
|||||||
protected override Vector2 DrawScale
|
protected override Vector2 DrawScale
|
||||||
=> new Vector2(FlipH ? -base.DrawScale.X : base.DrawScale.X, FlipV ? -base.DrawScale.Y : base.DrawScale.Y) * VectorScale;
|
=> new Vector2(FlipH ? -base.DrawScale.X : base.DrawScale.X, FlipV ? -base.DrawScale.Y : base.DrawScale.Y) * VectorScale;
|
||||||
|
|
||||||
public override Anchor Origin
|
public override Anchor Origin => StoryboardExtensions.AdjustOrigin(base.Origin, VectorScale, FlipH, FlipV);
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
var origin = base.Origin;
|
|
||||||
|
|
||||||
if (FlipH)
|
|
||||||
{
|
|
||||||
if (origin.HasFlagFast(Anchor.x0))
|
|
||||||
origin = Anchor.x2 | (origin & (Anchor.y0 | Anchor.y1 | Anchor.y2));
|
|
||||||
else if (origin.HasFlagFast(Anchor.x2))
|
|
||||||
origin = Anchor.x0 | (origin & (Anchor.y0 | Anchor.y1 | Anchor.y2));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FlipV)
|
|
||||||
{
|
|
||||||
if (origin.HasFlagFast(Anchor.y0))
|
|
||||||
origin = Anchor.y2 | (origin & (Anchor.x0 | Anchor.x1 | Anchor.x2));
|
|
||||||
else if (origin.HasFlagFast(Anchor.y2))
|
|
||||||
origin = Anchor.y0 | (origin & (Anchor.x0 | Anchor.x1 | Anchor.x2));
|
|
||||||
}
|
|
||||||
|
|
||||||
return origin;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool IsPresent
|
public override bool IsPresent
|
||||||
=> !float.IsNaN(DrawPosition.X) && !float.IsNaN(DrawPosition.Y) && base.IsPresent;
|
=> !float.IsNaN(DrawPosition.X) && !float.IsNaN(DrawPosition.Y) && base.IsPresent;
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Extensions.EnumExtensions;
|
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Framework.Graphics.Textures;
|
using osu.Framework.Graphics.Textures;
|
||||||
@ -73,31 +72,7 @@ namespace osu.Game.Storyboards.Drawables
|
|||||||
protected override Vector2 DrawScale
|
protected override Vector2 DrawScale
|
||||||
=> new Vector2(FlipH ? -base.DrawScale.X : base.DrawScale.X, FlipV ? -base.DrawScale.Y : base.DrawScale.Y) * VectorScale;
|
=> new Vector2(FlipH ? -base.DrawScale.X : base.DrawScale.X, FlipV ? -base.DrawScale.Y : base.DrawScale.Y) * VectorScale;
|
||||||
|
|
||||||
public override Anchor Origin
|
public override Anchor Origin => StoryboardExtensions.AdjustOrigin(base.Origin, VectorScale, FlipH, FlipV);
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
var origin = base.Origin;
|
|
||||||
|
|
||||||
if (FlipH)
|
|
||||||
{
|
|
||||||
if (origin.HasFlagFast(Anchor.x0))
|
|
||||||
origin = Anchor.x2 | (origin & (Anchor.y0 | Anchor.y1 | Anchor.y2));
|
|
||||||
else if (origin.HasFlagFast(Anchor.x2))
|
|
||||||
origin = Anchor.x0 | (origin & (Anchor.y0 | Anchor.y1 | Anchor.y2));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FlipV)
|
|
||||||
{
|
|
||||||
if (origin.HasFlagFast(Anchor.y0))
|
|
||||||
origin = Anchor.y2 | (origin & (Anchor.x0 | Anchor.x1 | Anchor.x2));
|
|
||||||
else if (origin.HasFlagFast(Anchor.y2))
|
|
||||||
origin = Anchor.y0 | (origin & (Anchor.x0 | Anchor.x1 | Anchor.x2));
|
|
||||||
}
|
|
||||||
|
|
||||||
return origin;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool IsPresent
|
public override bool IsPresent
|
||||||
=> !float.IsNaN(DrawPosition.X) && !float.IsNaN(DrawPosition.Y) && base.IsPresent;
|
=> !float.IsNaN(DrawPosition.X) && !float.IsNaN(DrawPosition.Y) && base.IsPresent;
|
||||||
|
43
osu.Game/Storyboards/StoryboardExtensions.cs
Normal file
43
osu.Game/Storyboards/StoryboardExtensions.cs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
using osu.Framework.Extensions.EnumExtensions;
|
||||||
|
using osu.Framework.Graphics;
|
||||||
|
using osuTK;
|
||||||
|
|
||||||
|
namespace osu.Game.Storyboards
|
||||||
|
{
|
||||||
|
public static class StoryboardExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Given an origin and a set of properties, adjust the origin to display the sprite/animation correctly.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="origin">The current origin.</param>
|
||||||
|
/// <param name="vectorScale">The vector scale.</param>
|
||||||
|
/// <param name="flipH">Whether the element is flipped horizontally.</param>
|
||||||
|
/// <param name="flipV">Whether the element is flipped vertically.</param>
|
||||||
|
/// <returns>The adjusted origin.</returns>
|
||||||
|
public static Anchor AdjustOrigin(Anchor origin, Vector2 vectorScale, bool flipH, bool flipV)
|
||||||
|
{
|
||||||
|
// Either flip horizontally or negative X scale, but not both.
|
||||||
|
if (flipH ^ (vectorScale.X < 0))
|
||||||
|
{
|
||||||
|
if (origin.HasFlagFast(Anchor.x0))
|
||||||
|
origin = Anchor.x2 | (origin & (Anchor.y0 | Anchor.y1 | Anchor.y2));
|
||||||
|
else if (origin.HasFlagFast(Anchor.x2))
|
||||||
|
origin = Anchor.x0 | (origin & (Anchor.y0 | Anchor.y1 | Anchor.y2));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Either flip vertically or negative Y scale, but not both.
|
||||||
|
if (flipV ^ (vectorScale.Y < 0))
|
||||||
|
{
|
||||||
|
if (origin.HasFlagFast(Anchor.y0))
|
||||||
|
origin = Anchor.y2 | (origin & (Anchor.x0 | Anchor.x1 | Anchor.x2));
|
||||||
|
else if (origin.HasFlagFast(Anchor.y2))
|
||||||
|
origin = Anchor.y0 | (origin & (Anchor.x0 | Anchor.x1 | Anchor.x2));
|
||||||
|
}
|
||||||
|
|
||||||
|
return origin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user