From 707effb591edb33cbef8d1e2c45a1c00c55b0722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adonais=20Romero=20Gonz=C3=A1lez?= Date: Sun, 9 Oct 2016 14:02:44 -0500 Subject: [PATCH] Completed StarCounter More complex than the other counters, but hopefully functional. I subestimated you and your silly bouncing animations. >.> --- .../Tests/TestCaseScoreCounter.cs | 12 ++ .../Graphics/UserInterface/RollingCounter.cs | 10 +- .../Graphics/UserInterface/StarCounter.cs | 106 +++++++++++++----- 3 files changed, 97 insertions(+), 31 deletions(-) diff --git a/osu.Desktop.VisualTests/Tests/TestCaseScoreCounter.cs b/osu.Desktop.VisualTests/Tests/TestCaseScoreCounter.cs index 36218fa813..2ad177a6be 100644 --- a/osu.Desktop.VisualTests/Tests/TestCaseScoreCounter.cs +++ b/osu.Desktop.VisualTests/Tests/TestCaseScoreCounter.cs @@ -11,6 +11,7 @@ using osu.Framework.Graphics.Transformations; using OpenTK; using OpenTK.Graphics; using osu.Framework.MathUtils; +using osu.Framework.Graphics.Sprites; namespace osu.Desktop.Tests { @@ -87,6 +88,15 @@ namespace osu.Desktop.Tests }; Add(pc); + SpriteText text = new SpriteText + { + Origin = Anchor.BottomLeft, + Anchor = Anchor.BottomLeft, + Position = new Vector2(20, 190), + Text = @"- unset -", + }; + Add(text); + StarCounter tc = new StarCounter { Origin = Anchor.BottomLeft, @@ -103,6 +113,7 @@ namespace osu.Desktop.Tests cc.Count = 0; pc.SetCount(0, 0); tc.Count = 0; + text.Text = tc.Count.ToString("0.00"); }); AddButton(@"Hit! :D", delegate @@ -131,6 +142,7 @@ namespace osu.Desktop.Tests AddButton(@"Alter stars", delegate { tc.Count = RNG.NextSingle() * tc.MaxStars; + text.Text = tc.Count.ToString("0.00"); }); AddButton(@"Stop counters", delegate diff --git a/osu.Game/Graphics/UserInterface/RollingCounter.cs b/osu.Game/Graphics/UserInterface/RollingCounter.cs index 54d269079d..1c7d024d85 100644 --- a/osu.Game/Graphics/UserInterface/RollingCounter.cs +++ b/osu.Game/Graphics/UserInterface/RollingCounter.cs @@ -52,6 +52,7 @@ namespace osu.Game.Graphics.UserInterface /// public EasingTypes RollingEasing = EasingTypes.None; + protected T prevVisibleCount; protected T visibleCount; /// @@ -65,13 +66,15 @@ namespace osu.Game.Graphics.UserInterface } protected set { + prevVisibleCount = visibleCount; if (visibleCount.Equals(value)) return; - transformVisibleCount(visibleCount, value); visibleCount = value; + transformVisibleCount(prevVisibleCount, value); } } + protected T prevCount; protected T count; /// @@ -85,6 +88,8 @@ namespace osu.Game.Graphics.UserInterface } set { + prevCount = count; + count = value; if (Clock != null) { RollingTotalDuration = @@ -93,13 +98,12 @@ namespace osu.Game.Graphics.UserInterface : RollingDuration; transformCount(IsRollingContinuous ? VisibleCount : count, value); } - count = value; } } protected RollingCounter() { - Debug.Assert(transformType.IsSubclassOf(typeof(Transform)), @"transformType should be a subclass of Transform."); + Debug.Assert(transformType.IsSubclassOf(typeof(Transform)) || transformType == typeof(Transform), @"transformType should be a subclass of Transform."); } public override void Load() diff --git a/osu.Game/Graphics/UserInterface/StarCounter.cs b/osu.Game/Graphics/UserInterface/StarCounter.cs index 5c355d06d6..d4eeb202fb 100644 --- a/osu.Game/Graphics/UserInterface/StarCounter.cs +++ b/osu.Game/Graphics/UserInterface/StarCounter.cs @@ -9,6 +9,7 @@ using osu.Framework.MathUtils; using osu.Framework.Timing; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -20,26 +21,23 @@ namespace osu.Game.Graphics.UserInterface /// public class StarCounter : RollingCounter { - protected override Type transformType => typeof(TransformStar); - - protected float MinStarSize = 0.3f; + protected override Type transformType => typeof(TransformStarCounter); protected Container starContainer; protected List stars = new List(); + public ulong StarAnimationDuration = 500; + public ulong FadeDuration = 100; + public float MinStarSize = 0.3f; public float MinStarAlpha = 0.5f; - public int MaxStars = 10; - public int StarSize = 20; - public int StarSpacing = 4; public StarCounter() : base() { IsRollingProportional = true; RollingDuration = 150; - RollingEasing = EasingTypes.Out; } protected override ulong getProportionalDuration(float currentValue, float newValue) @@ -77,7 +75,7 @@ namespace osu.Game.Graphics.UserInterface Origin = Anchor.Centre, TextSize = StarSize, Scale = new Vector2(MinStarSize), - Alpha = MinStarAlpha, + Alpha = (i == 0) ? 1.0f : MinStarAlpha, Position = new Vector2((StarSize + StarSpacing) * i + (StarSize + StarSpacing) / 2, 0), }; stars.Add(star); @@ -87,27 +85,79 @@ namespace osu.Game.Graphics.UserInterface ResetCount(); } - protected override void transformVisibleCount(float currentValue, float newValue) + protected override void transformCount(float currentValue, float newValue) { - for (int i = 0; i < MaxStars; i++) - { - if (newValue < i) - { - stars[i].Alpha = MinStarAlpha; - stars[i].ScaleTo(MinStarSize); - } - else - { - stars[i].Alpha = 1; - if (newValue > (i + 1)) - stars[i].ScaleTo(1f); - else - stars[i].ScaleTo(Interpolation.ValueAt(newValue, MinStarSize, 1f, i, i + 1, EasingTypes.None)); - } - } + transformStar((int)Math.Floor(currentValue), currentValue < newValue); + transformCount(new TransformStarCounter(Clock), currentValue, newValue); } - protected class TransformStar : Transform + protected void updateTransformStar(int i) + { + foreach (ITransform t in stars[i].Transforms.AliveItems) + if (t.GetType() == typeof(TransformAlpha) || t.GetType() == typeof(TransformScaleVector)) + t.Apply(stars[i]); + + stars[i].Transforms.RemoveAll(t => t.GetType() == typeof(TransformScaleVector) || t.GetType() == typeof(TransformAlpha)); + } + + protected void transformStarScale(int i, TransformScaleVector transform, bool isIncrement, double startTime) + { + transform.StartTime = startTime; + transform.EndTime = transform.StartTime + StarAnimationDuration; + transform.StartValue = stars[i].Scale; + transform.EndValue = new Vector2(Interpolation.ValueAt((isIncrement ? Math.Min(i + 1, Count) : Math.Max(i, Count)), MinStarSize, 1.0f, i, i + 1)); + transform.Easing = EasingTypes.OutElasticHalf; + + stars[i].Transforms.Add(transform); + } + + protected void transformStarAlpha(int i, TransformAlpha transform, bool isIncrement, double startTime) + { + transform.StartTime = startTime; + //if (!isIncrement) + //transform.StartTime += StarAnimationDuration - FadeDuration; + transform.EndTime = transform.StartTime + FadeDuration; + transform.StartValue = stars[i].Alpha; + transform.EndValue = i < Count ? 1.0f : MinStarAlpha; + + stars[i].Transforms.Add(transform); + } + + + protected void transformStar(int i, bool isIncrement) + { + if (Clock == null) + return; + + // Calculate time where animation should had started + double startTime = Time; + // If incrementing, animation should had started when VisibleCount crossed start of star (i) + if (isIncrement) + startTime -= i == (int)Math.Floor(prevCount) ? getProportionalDuration(prevCount, VisibleCount) : getProportionalDuration(i, VisibleCount); + // If decrementing, animation should had started when VisibleCount crossed end of star (i + 1) + else + startTime -= i == (int)Math.Floor(prevCount) ? getProportionalDuration(prevCount, VisibleCount) : getProportionalDuration(i + 1, VisibleCount); + + updateTransformStar(i); + + transformStarScale(i, new TransformScaleVector(Clock), isIncrement, startTime); + transformStarAlpha(i, new TransformAlpha(Clock), isIncrement, startTime); + } + + protected override void transformVisibleCount(float currentValue, float newValue) + { + // Detect increment that passes over an integer value + if (Math.Ceiling(currentValue) <= Math.Floor(newValue)) + for (int i = (int)Math.Ceiling(currentValue); i <= Math.Floor(newValue); i++) + transformStar(i, true); + + // Detect decrement that passes over an integer value + if (Math.Floor(currentValue) >= Math.Ceiling(newValue)) + for (int i = (int)Math.Floor(newValue); i < Math.Floor(currentValue); i++) + transformStar(i, false); + } + + protected class TransformStarCounter : Transform { public override float CurrentValue { @@ -127,8 +177,8 @@ namespace osu.Game.Graphics.UserInterface (d as StarCounter).VisibleCount = CurrentValue; } - public TransformStar(IClock clock) - : base(clock) + public TransformStarCounter(IClock clock) + : base(clock) { } }