diff --git a/osu.Game/Graphics/UserInterface/AccuracyCounter.cs b/osu.Game/Graphics/UserInterface/AccuracyCounter.cs index 2ca6d602c7..d00eda80b2 100644 --- a/osu.Game/Graphics/UserInterface/AccuracyCounter.cs +++ b/osu.Game/Graphics/UserInterface/AccuracyCounter.cs @@ -18,6 +18,8 @@ namespace osu.Game.Graphics.UserInterface /// public class AccuracyCounter : RollingCounter { + protected override Type transformType => typeof(TransformAccuracy); + private long numerator = 0; public long Numerator { @@ -71,11 +73,6 @@ namespace osu.Game.Graphics.UserInterface return count.ToString("0.00") + "%"; } - protected override void transformCount(float currentValue, float newValue) - { - transformCount(new TransformAccuracy(Clock), currentValue, newValue); - } - protected class TransformAccuracy : Transform { public override float CurrentValue diff --git a/osu.Game/Graphics/UserInterface/AlternativeComboCounter.cs b/osu.Game/Graphics/UserInterface/AlternativeComboCounter.cs index 5afe23fce7..c361f96644 100644 --- a/osu.Game/Graphics/UserInterface/AlternativeComboCounter.cs +++ b/osu.Game/Graphics/UserInterface/AlternativeComboCounter.cs @@ -24,7 +24,7 @@ namespace osu.Game.Graphics.UserInterface public EasingTypes TintEasing = EasingTypes.None; public bool CanAnimateWhenBackwards = false; - public AlternativeComboCounter() + public AlternativeComboCounter() : base() { IsRollingContinuous = false; } @@ -46,15 +46,15 @@ namespace osu.Game.Graphics.UserInterface // Animate rollover only when going backwards if (newValue > currentValue) { - updateTransforms(typeof(TranformULongCounter)); - removeTransforms(typeof(TranformULongCounter)); + updateTransforms(typeof(TransformULongCounter)); + removeTransforms(typeof(TransformULongCounter)); VisibleCount = newValue; } else - transformCount(new TranformULongCounter(Clock), currentValue, newValue); + transformCount(new TransformULongCounter(Clock), currentValue, newValue); } - protected override ulong GetProportionalDuration(ulong currentValue, ulong newValue) + protected override ulong getProportionalDuration(ulong currentValue, ulong newValue) { ulong difference = currentValue > newValue ? currentValue - newValue : currentValue - newValue; return difference * RollingDuration; diff --git a/osu.Game/Graphics/UserInterface/CatchComboCounter.cs b/osu.Game/Graphics/UserInterface/CatchComboCounter.cs index 6f6d586cb8..fae46de9bf 100644 --- a/osu.Game/Graphics/UserInterface/CatchComboCounter.cs +++ b/osu.Game/Graphics/UserInterface/CatchComboCounter.cs @@ -15,7 +15,7 @@ namespace osu.Game.Graphics.UserInterface /// public class CatchComboCounter : StandardComboCounter { - public CatchComboCounter() + public CatchComboCounter() : base() { CanPopOutWhenBackwards = true; } @@ -30,15 +30,15 @@ namespace osu.Game.Graphics.UserInterface // Animate rollover only when going backwards if (newValue > currentValue) { - updateTransforms(typeof(TranformULongCounter)); - removeTransforms(typeof(TranformULongCounter)); + updateTransforms(typeof(TransformULongCounter)); + removeTransforms(typeof(TransformULongCounter)); VisibleCount = newValue; } else { // Backwards pop-up animation has no tint colour popOutSpriteText.Colour = countSpriteText.Colour; - transformCount(new TranformULongCounter(Clock), currentValue, newValue); + transformCount(new TransformULongCounter(Clock), currentValue, newValue); } } diff --git a/osu.Game/Graphics/UserInterface/RollingCounter.cs b/osu.Game/Graphics/UserInterface/RollingCounter.cs index 780c747e96..fa8fb64efa 100644 --- a/osu.Game/Graphics/UserInterface/RollingCounter.cs +++ b/osu.Game/Graphics/UserInterface/RollingCounter.cs @@ -7,6 +7,7 @@ using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Transformations; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -19,6 +20,14 @@ namespace osu.Game.Graphics.UserInterface /// Type of the actual counter. public abstract class RollingCounter : Container { + /// + /// Type of the Transform to use. + /// + /// + /// Must be a subclass of Transform + /// + protected virtual Type transformType => typeof(Transform); + protected SpriteText countSpriteText; protected ulong RollingTotalDuration = 0; @@ -92,7 +101,7 @@ namespace osu.Game.Graphics.UserInterface { RollingTotalDuration = IsRollingProportional - ? GetProportionalDuration(VisibleCount, value) + ? getProportionalDuration(VisibleCount, value) : RollingDuration; transformCount(IsRollingContinuous ? VisibleCount : count, value); } @@ -100,10 +109,15 @@ namespace osu.Game.Graphics.UserInterface } } + protected RollingCounter() + { + Debug.Assert(transformType.IsSubclassOf(typeof(Transform)), @"transformType should be a subclass of Transform."); + } + public override void Load() { base.Load(); - removeTransforms(typeof(Transform)); + removeTransforms(transformType); if (Count == null) ResetCount(); VisibleCount = Count; @@ -119,32 +133,6 @@ namespace osu.Game.Graphics.UserInterface }; } - /// - /// Calculates the duration of the roll-up animation by using the difference between the current visible value - /// and the new final value. - /// - /// - /// Intended to be used in conjunction with IsRollingProportional = true. - /// Unless a derived class needs to have a proportional rolling, it is not necessary to override this function. - /// - /// Current visible value. - /// New final value. - /// Calculated rollover duration in milliseconds. - protected virtual ulong GetProportionalDuration(T currentValue, T newValue) - { - return RollingDuration; - } - - /// - /// Used to format counts. - /// - /// Count to format. - /// Count formatted as a string. - protected virtual string formatCount(T count) - { - return count.ToString(); - } - /// /// Sets count value, bypassing rollover animation. /// @@ -160,7 +148,7 @@ namespace osu.Game.Graphics.UserInterface /// public virtual void StopRolling() { - removeTransforms(typeof(Transform)); + removeTransforms(transformType); VisibleCount = Count; } @@ -169,16 +157,42 @@ namespace osu.Game.Graphics.UserInterface /// public abstract void ResetCount(); + /// + /// Calculates the duration of the roll-up animation by using the difference between the current visible value + /// and the new final value. + /// + /// + /// To be used in conjunction with IsRollingProportional = true. + /// Unless a derived class needs to have a proportional rolling, it is not necessary to override this function. + /// + /// Current visible value. + /// New final value. + /// Calculated rollover duration in milliseconds. + protected virtual ulong getProportionalDuration(T currentValue, T newValue) + { + return RollingDuration; + } + + /// + /// Used to format counts. + /// + /// Count to format. + /// Count formatted as a string. + protected virtual string formatCount(T count) + { + return count.ToString(); + } + protected void updateTransforms(Type type) { foreach (ITransform t in Transforms.AliveItems) - if (t.GetType().IsAssignableFrom(type)) + if (t.GetType() == type) t.Apply(this); } protected void removeTransforms(Type type) { - Transforms.RemoveAll(t => t.GetType().IsSubclassOf(type)); + Transforms.RemoveAll(t => t.GetType() == type); } /// @@ -190,11 +204,16 @@ namespace osu.Game.Graphics.UserInterface /// /// Unless you need to set a custom animation according to the current or new value of the count, the /// recommended approach is to call transformCount(CustomTransformer(Clock), currentValue, newValue), where - /// CustomTransformer is a custom Transformer related to the type T of the RolloverCounter. + /// CustomTransformer is of type transformerType. /// By using this approach, there is no need to check if the Clock is not null; this validation is done before /// adding the transformer. /// - protected abstract void transformCount(T currentValue, T newValue); + /// + protected virtual void transformCount(T currentValue, T newValue) + { + object[] parameters = { Clock }; + transformCount((Transform)Activator.CreateInstance(transformType, parameters), currentValue, newValue); + } /// /// Intended to be used by transformCount(). @@ -239,7 +258,7 @@ namespace osu.Game.Graphics.UserInterface } } - protected virtual void updateTextSize() + protected void updateTextSize() { if (countSpriteText != null) countSpriteText.TextSize = TextSize; diff --git a/osu.Game/Graphics/UserInterface/StandardComboCounter.cs b/osu.Game/Graphics/UserInterface/StandardComboCounter.cs index e0555c952e..7b8587ac94 100644 --- a/osu.Game/Graphics/UserInterface/StandardComboCounter.cs +++ b/osu.Game/Graphics/UserInterface/StandardComboCounter.cs @@ -25,7 +25,7 @@ namespace osu.Game.Graphics.UserInterface public bool CanPopOutWhenBackwards = false; public float PopOutInitialAlpha = 1.0f; - public StandardComboCounter() + public StandardComboCounter() : base() { IsRollingContinuous = false; } @@ -57,15 +57,15 @@ namespace osu.Game.Graphics.UserInterface // Animate rollover only when going backwards if (newValue > currentValue) { - updateTransforms(typeof(TranformULongCounter)); - removeTransforms(typeof(TranformULongCounter)); + updateTransforms(typeof(TransformULongCounter)); + removeTransforms(typeof(TransformULongCounter)); VisibleCount = newValue; } else - transformCount(new TranformULongCounter(Clock), currentValue, newValue); + transformCount(new TransformULongCounter(Clock), currentValue, newValue); } - protected override ulong GetProportionalDuration(ulong currentValue, ulong newValue) + protected override ulong getProportionalDuration(ulong currentValue, ulong newValue) { ulong difference = currentValue > newValue ? currentValue - newValue : currentValue - newValue; return difference * RollingDuration; diff --git a/osu.Game/Graphics/UserInterface/ULongCounter.cs b/osu.Game/Graphics/UserInterface/ULongCounter.cs index 434607be4c..ef61cf7879 100644 --- a/osu.Game/Graphics/UserInterface/ULongCounter.cs +++ b/osu.Game/Graphics/UserInterface/ULongCounter.cs @@ -18,10 +18,7 @@ namespace osu.Game.Graphics.UserInterface /// public class ULongCounter : RollingCounter { - protected override void transformCount(ulong currentValue, ulong newValue) - { - transformCount(new TranformULongCounter(Clock), currentValue, newValue); - } + protected override Type transformType => typeof(TransformULongCounter); public override void ResetCount() { @@ -33,7 +30,7 @@ namespace osu.Game.Graphics.UserInterface return count.ToString("#,0"); } - protected class TranformULongCounter : Transform + protected class TransformULongCounter : Transform { public override ulong CurrentValue { @@ -53,7 +50,7 @@ namespace osu.Game.Graphics.UserInterface (d as ULongCounter).VisibleCount = CurrentValue; } - public TranformULongCounter(IClock clock) + public TransformULongCounter(IClock clock) : base(clock) { }