mirror of
https://github.com/ppy/osu.git
synced 2025-02-20 19:23:22 +08:00
Nicer generic rolling counters
This commit is contained in:
parent
b5fbb26f8f
commit
fd58c6e835
@ -13,8 +13,6 @@ namespace osu.Game.Graphics.UserInterface
|
||||
/// </summary>
|
||||
public class PercentageCounter : RollingCounter<double>
|
||||
{
|
||||
protected override Type TransformType => typeof(TransformAccuracy);
|
||||
|
||||
protected override double RollingDuration => 750;
|
||||
|
||||
private float epsilon => 1e-10f;
|
||||
@ -44,23 +42,5 @@ namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using OpenTK.Graphics;
|
||||
using osu.Framework.MathUtils;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
@ -22,14 +23,6 @@ namespace osu.Game.Graphics.UserInterface
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
@ -59,7 +52,8 @@ namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
return displayedCount;
|
||||
}
|
||||
protected set
|
||||
|
||||
set
|
||||
{
|
||||
if (EqualityComparer<T>.Default.Equals(displayedCount, value))
|
||||
return;
|
||||
@ -134,7 +128,7 @@ namespace osu.Game.Graphics.UserInterface
|
||||
/// </summary>
|
||||
public virtual void StopRolling()
|
||||
{
|
||||
Flush(false, TransformType);
|
||||
Flush(false, typeof(TransformRollingCounter));
|
||||
DisplayedCount = Current;
|
||||
}
|
||||
|
||||
@ -181,25 +175,43 @@ namespace osu.Game.Graphics.UserInterface
|
||||
/// <seealso cref="TransformType"/>
|
||||
protected virtual void TransformCount(T currentValue, T newValue)
|
||||
{
|
||||
Debug.Assert(
|
||||
typeof(Transform<T, Drawable>).IsAssignableFrom(TransformType),
|
||||
@"transformType should be a subclass of Transform<T>."
|
||||
);
|
||||
|
||||
TransformCount((Transform<T, Drawable>)Activator.CreateInstance(TransformType), currentValue, newValue);
|
||||
TransformCount(new TransformRollingCounter(this), currentValue, newValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Intended to be used by TransformCount(T currentValue, T newValue).
|
||||
/// </summary>
|
||||
protected void TransformCount(Transform<T, Drawable> transform, T currentValue, T newValue)
|
||||
protected void TransformCount(TransformRollingCounter transform, T currentValue, T newValue)
|
||||
{
|
||||
double rollingTotalDuration =
|
||||
IsRollingProportional
|
||||
? GetProportionalDuration(currentValue, newValue)
|
||||
: 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,6 @@ namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
public class ScoreCounter : RollingCounter<double>
|
||||
{
|
||||
protected override Type TransformType => typeof(TransformScore);
|
||||
|
||||
protected override double RollingDuration => 1000;
|
||||
protected override EasingTypes RollingEasing => EasingTypes.Out;
|
||||
|
||||
@ -55,23 +53,5 @@ namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,6 @@ namespace osu.Game.Graphics.UserInterface
|
||||
/// </summary>
|
||||
public class SimpleComboCounter : RollingCounter<int>
|
||||
{
|
||||
protected override Type TransformType => typeof(TransformCounterCount);
|
||||
|
||||
protected override double RollingDuration => 750;
|
||||
|
||||
public SimpleComboCounter()
|
||||
@ -36,23 +34,5 @@ namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -12,44 +12,24 @@ namespace osu.Game.Screens.Play.HUD
|
||||
/// <summary>
|
||||
/// Used to display combo with a roll-up animation in results screen.
|
||||
/// </summary>
|
||||
public class ComboResultCounter : RollingCounter<ulong>
|
||||
public class ComboResultCounter : RollingCounter<long>
|
||||
{
|
||||
protected override Type TransformType => typeof(TransformComboResult);
|
||||
|
||||
protected override double RollingDuration => 500;
|
||||
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;
|
||||
}
|
||||
|
||||
protected override string FormatCount(ulong count)
|
||||
protected override string FormatCount(long count)
|
||||
{
|
||||
return $@"{count}x";
|
||||
}
|
||||
|
||||
public override void Increment(ulong amount)
|
||||
public override void Increment(long 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user