Completed StarCounter

More complex than the other counters, but hopefully functional.
I subestimated you and your silly bouncing animations.
>.>
This commit is contained in:
Adonais Romero González 2016-10-09 14:02:44 -05:00
parent 67d3d772f6
commit 707effb591
3 changed files with 97 additions and 31 deletions

View File

@ -11,6 +11,7 @@ using osu.Framework.Graphics.Transformations;
using OpenTK; using OpenTK;
using OpenTK.Graphics; using OpenTK.Graphics;
using osu.Framework.MathUtils; using osu.Framework.MathUtils;
using osu.Framework.Graphics.Sprites;
namespace osu.Desktop.Tests namespace osu.Desktop.Tests
{ {
@ -87,6 +88,15 @@ namespace osu.Desktop.Tests
}; };
Add(pc); 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 StarCounter tc = new StarCounter
{ {
Origin = Anchor.BottomLeft, Origin = Anchor.BottomLeft,
@ -103,6 +113,7 @@ namespace osu.Desktop.Tests
cc.Count = 0; cc.Count = 0;
pc.SetCount(0, 0); pc.SetCount(0, 0);
tc.Count = 0; tc.Count = 0;
text.Text = tc.Count.ToString("0.00");
}); });
AddButton(@"Hit! :D", delegate AddButton(@"Hit! :D", delegate
@ -131,6 +142,7 @@ namespace osu.Desktop.Tests
AddButton(@"Alter stars", delegate AddButton(@"Alter stars", delegate
{ {
tc.Count = RNG.NextSingle() * tc.MaxStars; tc.Count = RNG.NextSingle() * tc.MaxStars;
text.Text = tc.Count.ToString("0.00");
}); });
AddButton(@"Stop counters", delegate AddButton(@"Stop counters", delegate

View File

@ -52,6 +52,7 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public EasingTypes RollingEasing = EasingTypes.None; public EasingTypes RollingEasing = EasingTypes.None;
protected T prevVisibleCount;
protected T visibleCount; protected T visibleCount;
/// <summary> /// <summary>
@ -65,13 +66,15 @@ namespace osu.Game.Graphics.UserInterface
} }
protected set protected set
{ {
prevVisibleCount = visibleCount;
if (visibleCount.Equals(value)) if (visibleCount.Equals(value))
return; return;
transformVisibleCount(visibleCount, value);
visibleCount = value; visibleCount = value;
transformVisibleCount(prevVisibleCount, value);
} }
} }
protected T prevCount;
protected T count; protected T count;
/// <summary> /// <summary>
@ -85,6 +88,8 @@ namespace osu.Game.Graphics.UserInterface
} }
set set
{ {
prevCount = count;
count = value;
if (Clock != null) if (Clock != null)
{ {
RollingTotalDuration = RollingTotalDuration =
@ -93,13 +98,12 @@ namespace osu.Game.Graphics.UserInterface
: RollingDuration; : RollingDuration;
transformCount(IsRollingContinuous ? VisibleCount : count, value); transformCount(IsRollingContinuous ? VisibleCount : count, value);
} }
count = value;
} }
} }
protected RollingCounter() protected RollingCounter()
{ {
Debug.Assert(transformType.IsSubclassOf(typeof(Transform<T>)), @"transformType should be a subclass of Transform<T>."); Debug.Assert(transformType.IsSubclassOf(typeof(Transform<T>)) || transformType == typeof(Transform<T>), @"transformType should be a subclass of Transform<T>.");
} }
public override void Load() public override void Load()

View File

@ -9,6 +9,7 @@ using osu.Framework.MathUtils;
using osu.Framework.Timing; using osu.Framework.Timing;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -20,26 +21,23 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public class StarCounter : RollingCounter<float> public class StarCounter : RollingCounter<float>
{ {
protected override Type transformType => typeof(TransformStar); protected override Type transformType => typeof(TransformStarCounter);
protected float MinStarSize = 0.3f;
protected Container starContainer; protected Container starContainer;
protected List<TextAwesome> stars = new List<TextAwesome>(); protected List<TextAwesome> stars = new List<TextAwesome>();
public ulong StarAnimationDuration = 500;
public ulong FadeDuration = 100;
public float MinStarSize = 0.3f;
public float MinStarAlpha = 0.5f; public float MinStarAlpha = 0.5f;
public int MaxStars = 10; public int MaxStars = 10;
public int StarSize = 20; public int StarSize = 20;
public int StarSpacing = 4; public int StarSpacing = 4;
public StarCounter() : base() public StarCounter() : base()
{ {
IsRollingProportional = true; IsRollingProportional = true;
RollingDuration = 150; RollingDuration = 150;
RollingEasing = EasingTypes.Out;
} }
protected override ulong getProportionalDuration(float currentValue, float newValue) protected override ulong getProportionalDuration(float currentValue, float newValue)
@ -77,7 +75,7 @@ namespace osu.Game.Graphics.UserInterface
Origin = Anchor.Centre, Origin = Anchor.Centre,
TextSize = StarSize, TextSize = StarSize,
Scale = new Vector2(MinStarSize), Scale = new Vector2(MinStarSize),
Alpha = MinStarAlpha, Alpha = (i == 0) ? 1.0f : MinStarAlpha,
Position = new Vector2((StarSize + StarSpacing) * i + (StarSize + StarSpacing) / 2, 0), Position = new Vector2((StarSize + StarSpacing) * i + (StarSize + StarSpacing) / 2, 0),
}; };
stars.Add(star); stars.Add(star);
@ -87,27 +85,79 @@ namespace osu.Game.Graphics.UserInterface
ResetCount(); ResetCount();
} }
protected override void transformVisibleCount(float currentValue, float newValue) protected override void transformCount(float currentValue, float newValue)
{ {
for (int i = 0; i < MaxStars; i++) transformStar((int)Math.Floor(currentValue), currentValue < newValue);
{ transformCount(new TransformStarCounter(Clock), currentValue, newValue);
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));
}
}
} }
protected class TransformStar : Transform<float> 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<float>
{ {
public override float CurrentValue public override float CurrentValue
{ {
@ -127,8 +177,8 @@ namespace osu.Game.Graphics.UserInterface
(d as StarCounter).VisibleCount = CurrentValue; (d as StarCounter).VisibleCount = CurrentValue;
} }
public TransformStar(IClock clock) public TransformStarCounter(IClock clock)
: base(clock) : base(clock)
{ {
} }
} }