Nicer generic rolling counters

This commit is contained in:
Thomas Müller
2017-07-14 19:14:07 +03:00
parent b5fbb26f8f
commit fd58c6e835
5 changed files with 34 additions and 102 deletions

View File

@ -13,8 +13,6 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public class PercentageCounter : RollingCounter<double> public class PercentageCounter : RollingCounter<double>
{ {
protected override Type TransformType => typeof(TransformAccuracy);
protected override double RollingDuration => 750; protected override double RollingDuration => 750;
private float epsilon => 1e-10f; private float epsilon => 1e-10f;
@ -44,23 +42,5 @@ namespace osu.Game.Graphics.UserInterface
{ {
Current.Value = Current + amount; Current.Value = Current + amount;
} }
protected class TransformAccuracy : Transform<double, Drawable>
{
public virtual double CurrentValue
{
get
{
double time = Time?.Current ?? 0;
if (time < StartTime) return StartValue;
if (time >= EndTime) return EndValue;
return Interpolation.ValueAt(time, (float)StartValue, (float)EndValue, StartTime, EndTime, Easing);
}
}
public override void Apply(Drawable d) => ((PercentageCounter)d).DisplayedCount = CurrentValue;
public override void ReadIntoStartValue(Drawable d) => StartValue = ((PercentageCounter)d).DisplayedCount;
}
} }
} }

View File

@ -11,6 +11,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using OpenTK.Graphics; using OpenTK.Graphics;
using osu.Framework.MathUtils;
namespace osu.Game.Graphics.UserInterface namespace osu.Game.Graphics.UserInterface
{ {
@ -22,14 +23,6 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public Bindable<T> Current = new Bindable<T>(); public Bindable<T> Current = new Bindable<T>();
/// <summary>
/// Type of the Transform to use.
/// </summary>
/// <remarks>
/// Must be a subclass of Transform(T)
/// </remarks>
protected virtual Type TransformType => typeof(Transform<T, Drawable>);
protected SpriteText DisplayedCountSpriteText; protected SpriteText DisplayedCountSpriteText;
/// <summary> /// <summary>
@ -59,7 +52,8 @@ namespace osu.Game.Graphics.UserInterface
{ {
return displayedCount; return displayedCount;
} }
protected set
set
{ {
if (EqualityComparer<T>.Default.Equals(displayedCount, value)) if (EqualityComparer<T>.Default.Equals(displayedCount, value))
return; return;
@ -134,7 +128,7 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public virtual void StopRolling() public virtual void StopRolling()
{ {
Flush(false, TransformType); Flush(false, typeof(TransformRollingCounter));
DisplayedCount = Current; DisplayedCount = Current;
} }
@ -181,25 +175,43 @@ namespace osu.Game.Graphics.UserInterface
/// <seealso cref="TransformType"/> /// <seealso cref="TransformType"/>
protected virtual void TransformCount(T currentValue, T newValue) protected virtual void TransformCount(T currentValue, T newValue)
{ {
Debug.Assert( TransformCount(new TransformRollingCounter(this), currentValue, newValue);
typeof(Transform<T, Drawable>).IsAssignableFrom(TransformType),
@"transformType should be a subclass of Transform<T>."
);
TransformCount((Transform<T, Drawable>)Activator.CreateInstance(TransformType), currentValue, newValue);
} }
/// <summary> /// <summary>
/// Intended to be used by TransformCount(T currentValue, T newValue). /// Intended to be used by TransformCount(T currentValue, T newValue).
/// </summary> /// </summary>
protected void TransformCount(Transform<T, Drawable> transform, T currentValue, T newValue) protected void TransformCount(TransformRollingCounter transform, T currentValue, T newValue)
{ {
double rollingTotalDuration = double rollingTotalDuration =
IsRollingProportional IsRollingProportional
? GetProportionalDuration(currentValue, newValue) ? GetProportionalDuration(currentValue, newValue)
: RollingDuration; : RollingDuration;
TransformTo(newValue, rollingTotalDuration, RollingEasing, transform); this.TransformTo(newValue, rollingTotalDuration, RollingEasing, transform);
}
protected class TransformRollingCounter : Transform<T, RollingCounter<T>>
{
public TransformRollingCounter(RollingCounter<T> target) : base(target)
{
}
public T CurrentValue
{
get
{
double time = Time?.Current ?? 0;
if (time < StartTime) return StartValue;
if (time >= EndTime) return EndValue;
double result = Interpolation.ValueAt(time, Convert.ToDouble(StartValue), Convert.ToDouble(EndValue), StartTime, EndTime, Easing);
return (T)Convert.ChangeType(result, typeof(T), null);
}
}
public override void Apply(RollingCounter<T> d) => d.DisplayedCount = CurrentValue;
public override void ReadIntoStartValue(RollingCounter<T> d) => StartValue = d.DisplayedCount;
} }
} }
} }

View File

@ -10,8 +10,6 @@ namespace osu.Game.Graphics.UserInterface
{ {
public class ScoreCounter : RollingCounter<double> public class ScoreCounter : RollingCounter<double>
{ {
protected override Type TransformType => typeof(TransformScore);
protected override double RollingDuration => 1000; protected override double RollingDuration => 1000;
protected override EasingTypes RollingEasing => EasingTypes.Out; protected override EasingTypes RollingEasing => EasingTypes.Out;
@ -55,23 +53,5 @@ namespace osu.Game.Graphics.UserInterface
{ {
Current.Value = Current + amount; Current.Value = Current + amount;
} }
protected class TransformScore : Transform<double, Drawable>
{
public virtual double CurrentValue
{
get
{
double time = Time?.Current ?? 0;
if (time < StartTime) return StartValue;
if (time >= EndTime) return EndValue;
return Interpolation.ValueAt(time, (float)StartValue, (float)EndValue, StartTime, EndTime, Easing);
}
}
public override void Apply(Drawable d) => ((ScoreCounter)d).DisplayedCount = CurrentValue;
public override void ReadIntoStartValue(Drawable d) => StartValue = ((ScoreCounter)d).DisplayedCount;
}
} }
} }

View File

@ -13,8 +13,6 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public class SimpleComboCounter : RollingCounter<int> public class SimpleComboCounter : RollingCounter<int>
{ {
protected override Type TransformType => typeof(TransformCounterCount);
protected override double RollingDuration => 750; protected override double RollingDuration => 750;
public SimpleComboCounter() public SimpleComboCounter()
@ -36,23 +34,5 @@ namespace osu.Game.Graphics.UserInterface
{ {
Current.Value = Current + amount; Current.Value = Current + amount;
} }
private class TransformCounterCount : Transform<int, Drawable>
{
public int CurrentValue
{
get
{
double time = Time?.Current ?? 0;
if (time < StartTime) return StartValue;
if (time >= EndTime) return EndValue;
return (int)Interpolation.ValueAt(time, StartValue, EndValue, StartTime, EndTime, Easing);
}
}
public override void Apply(Drawable d) => ((SimpleComboCounter)d).DisplayedCount = CurrentValue;
public override void ReadIntoStartValue(Drawable d) => StartValue = ((SimpleComboCounter)d).DisplayedCount;
}
} }
} }

View File

@ -12,44 +12,24 @@ namespace osu.Game.Screens.Play.HUD
/// <summary> /// <summary>
/// Used to display combo with a roll-up animation in results screen. /// Used to display combo with a roll-up animation in results screen.
/// </summary> /// </summary>
public class ComboResultCounter : RollingCounter<ulong> public class ComboResultCounter : RollingCounter<long>
{ {
protected override Type TransformType => typeof(TransformComboResult);
protected override double RollingDuration => 500; protected override double RollingDuration => 500;
protected override EasingTypes RollingEasing => EasingTypes.Out; protected override EasingTypes RollingEasing => EasingTypes.Out;
protected override double GetProportionalDuration(ulong currentValue, ulong newValue) protected override double GetProportionalDuration(long currentValue, long newValue)
{ {
return currentValue > newValue ? currentValue - newValue : newValue - currentValue; return currentValue > newValue ? currentValue - newValue : newValue - currentValue;
} }
protected override string FormatCount(ulong count) protected override string FormatCount(long count)
{ {
return $@"{count}x"; return $@"{count}x";
} }
public override void Increment(ulong amount) public override void Increment(long amount)
{ {
Current.Value = Current + amount; Current.Value = Current + amount;
} }
protected class TransformComboResult : Transform<ulong, Drawable>
{
public virtual ulong CurrentValue
{
get
{
double time = Time?.Current ?? 0;
if (time < StartTime) return StartValue;
if (time >= EndTime) return EndValue;
return (ulong)Interpolation.ValueAt(time, StartValue, EndValue, StartTime, EndTime, Easing);
}
}
public override void Apply(Drawable d) => ((ComboResultCounter)d).DisplayedCount = CurrentValue;
public override void ReadIntoStartValue(Drawable d) => StartValue = ((ComboResultCounter)d).DisplayedCount;
}
} }
} }