Reflection to simplify transforms + some refactoring

This commit is contained in:
Adonais Romero González 2016-10-07 16:59:52 -05:00
parent 965e542eaf
commit 7277cf5af1
6 changed files with 72 additions and 59 deletions

View File

@ -18,6 +18,8 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public class AccuracyCounter : RollingCounter<float> public class AccuracyCounter : RollingCounter<float>
{ {
protected override Type transformType => typeof(TransformAccuracy);
private long numerator = 0; private long numerator = 0;
public long Numerator public long Numerator
{ {
@ -71,11 +73,6 @@ namespace osu.Game.Graphics.UserInterface
return count.ToString("0.00") + "%"; return count.ToString("0.00") + "%";
} }
protected override void transformCount(float currentValue, float newValue)
{
transformCount(new TransformAccuracy(Clock), currentValue, newValue);
}
protected class TransformAccuracy : Transform<float> protected class TransformAccuracy : Transform<float>
{ {
public override float CurrentValue public override float CurrentValue

View File

@ -24,7 +24,7 @@ namespace osu.Game.Graphics.UserInterface
public EasingTypes TintEasing = EasingTypes.None; public EasingTypes TintEasing = EasingTypes.None;
public bool CanAnimateWhenBackwards = false; public bool CanAnimateWhenBackwards = false;
public AlternativeComboCounter() public AlternativeComboCounter() : base()
{ {
IsRollingContinuous = false; IsRollingContinuous = false;
} }
@ -46,15 +46,15 @@ namespace osu.Game.Graphics.UserInterface
// Animate rollover only when going backwards // Animate rollover only when going backwards
if (newValue > currentValue) if (newValue > currentValue)
{ {
updateTransforms(typeof(TranformULongCounter)); updateTransforms(typeof(TransformULongCounter));
removeTransforms(typeof(TranformULongCounter)); removeTransforms(typeof(TransformULongCounter));
VisibleCount = newValue; VisibleCount = newValue;
} }
else 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; ulong difference = currentValue > newValue ? currentValue - newValue : currentValue - newValue;
return difference * RollingDuration; return difference * RollingDuration;

View File

@ -15,7 +15,7 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public class CatchComboCounter : StandardComboCounter public class CatchComboCounter : StandardComboCounter
{ {
public CatchComboCounter() public CatchComboCounter() : base()
{ {
CanPopOutWhenBackwards = true; CanPopOutWhenBackwards = true;
} }
@ -30,15 +30,15 @@ namespace osu.Game.Graphics.UserInterface
// Animate rollover only when going backwards // Animate rollover only when going backwards
if (newValue > currentValue) if (newValue > currentValue)
{ {
updateTransforms(typeof(TranformULongCounter)); updateTransforms(typeof(TransformULongCounter));
removeTransforms(typeof(TranformULongCounter)); removeTransforms(typeof(TransformULongCounter));
VisibleCount = newValue; VisibleCount = newValue;
} }
else else
{ {
// Backwards pop-up animation has no tint colour // Backwards pop-up animation has no tint colour
popOutSpriteText.Colour = countSpriteText.Colour; popOutSpriteText.Colour = countSpriteText.Colour;
transformCount(new TranformULongCounter(Clock), currentValue, newValue); transformCount(new TransformULongCounter(Clock), currentValue, newValue);
} }
} }

View File

@ -7,6 +7,7 @@ using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Transformations; using osu.Framework.Graphics.Transformations;
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;
@ -19,6 +20,14 @@ namespace osu.Game.Graphics.UserInterface
/// <typeparam name="T">Type of the actual counter.</typeparam> /// <typeparam name="T">Type of the actual counter.</typeparam>
public abstract class RollingCounter<T> : Container public abstract class RollingCounter<T> : Container
{ {
/// <summary>
/// Type of the Transform to use.
/// </summary>
/// <remarks>
/// Must be a subclass of Transform<T>
/// </remarks>
protected virtual Type transformType => typeof(Transform<T>);
protected SpriteText countSpriteText; protected SpriteText countSpriteText;
protected ulong RollingTotalDuration = 0; protected ulong RollingTotalDuration = 0;
@ -92,7 +101,7 @@ namespace osu.Game.Graphics.UserInterface
{ {
RollingTotalDuration = RollingTotalDuration =
IsRollingProportional IsRollingProportional
? GetProportionalDuration(VisibleCount, value) ? getProportionalDuration(VisibleCount, value)
: RollingDuration; : RollingDuration;
transformCount(IsRollingContinuous ? VisibleCount : count, value); transformCount(IsRollingContinuous ? VisibleCount : count, value);
} }
@ -100,10 +109,15 @@ namespace osu.Game.Graphics.UserInterface
} }
} }
protected RollingCounter()
{
Debug.Assert(transformType.IsSubclassOf(typeof(Transform<T>)), @"transformType should be a subclass of Transform<T>.");
}
public override void Load() public override void Load()
{ {
base.Load(); base.Load();
removeTransforms(typeof(Transform<T>)); removeTransforms(transformType);
if (Count == null) if (Count == null)
ResetCount(); ResetCount();
VisibleCount = Count; VisibleCount = Count;
@ -119,32 +133,6 @@ namespace osu.Game.Graphics.UserInterface
}; };
} }
/// <summary>
/// Calculates the duration of the roll-up animation by using the difference between the current visible value
/// and the new final value.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="currentValue">Current visible value.</param>
/// <param name="newValue">New final value.</param>
/// <returns>Calculated rollover duration in milliseconds.</returns>
protected virtual ulong GetProportionalDuration(T currentValue, T newValue)
{
return RollingDuration;
}
/// <summary>
/// Used to format counts.
/// </summary>
/// <param name="count">Count to format.</param>
/// <returns>Count formatted as a string.</returns>
protected virtual string formatCount(T count)
{
return count.ToString();
}
/// <summary> /// <summary>
/// Sets count value, bypassing rollover animation. /// Sets count value, bypassing rollover animation.
/// </summary> /// </summary>
@ -160,7 +148,7 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public virtual void StopRolling() public virtual void StopRolling()
{ {
removeTransforms(typeof(Transform<T>)); removeTransforms(transformType);
VisibleCount = Count; VisibleCount = Count;
} }
@ -169,16 +157,42 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public abstract void ResetCount(); public abstract void ResetCount();
/// <summary>
/// Calculates the duration of the roll-up animation by using the difference between the current visible value
/// and the new final value.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="currentValue">Current visible value.</param>
/// <param name="newValue">New final value.</param>
/// <returns>Calculated rollover duration in milliseconds.</returns>
protected virtual ulong getProportionalDuration(T currentValue, T newValue)
{
return RollingDuration;
}
/// <summary>
/// Used to format counts.
/// </summary>
/// <param name="count">Count to format.</param>
/// <returns>Count formatted as a string.</returns>
protected virtual string formatCount(T count)
{
return count.ToString();
}
protected void updateTransforms(Type type) protected void updateTransforms(Type type)
{ {
foreach (ITransform t in Transforms.AliveItems) foreach (ITransform t in Transforms.AliveItems)
if (t.GetType().IsAssignableFrom(type)) if (t.GetType() == type)
t.Apply(this); t.Apply(this);
} }
protected void removeTransforms(Type type) protected void removeTransforms(Type type)
{ {
Transforms.RemoveAll(t => t.GetType().IsSubclassOf(type)); Transforms.RemoveAll(t => t.GetType() == type);
} }
/// <summary> /// <summary>
@ -190,11 +204,16 @@ namespace osu.Game.Graphics.UserInterface
/// <remarks> /// <remarks>
/// Unless you need to set a custom animation according to the current or new value of the count, the /// 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 /// 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 /// By using this approach, there is no need to check if the Clock is not null; this validation is done before
/// adding the transformer. /// adding the transformer.
/// </remarks> /// </remarks>
protected abstract void transformCount(T currentValue, T newValue); /// <seealso cref="transformType"/>
protected virtual void transformCount(T currentValue, T newValue)
{
object[] parameters = { Clock };
transformCount((Transform<T>)Activator.CreateInstance(transformType, parameters), currentValue, newValue);
}
/// <summary> /// <summary>
/// Intended to be used by transformCount(). /// 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) if (countSpriteText != null)
countSpriteText.TextSize = TextSize; countSpriteText.TextSize = TextSize;

View File

@ -25,7 +25,7 @@ namespace osu.Game.Graphics.UserInterface
public bool CanPopOutWhenBackwards = false; public bool CanPopOutWhenBackwards = false;
public float PopOutInitialAlpha = 1.0f; public float PopOutInitialAlpha = 1.0f;
public StandardComboCounter() public StandardComboCounter() : base()
{ {
IsRollingContinuous = false; IsRollingContinuous = false;
} }
@ -57,15 +57,15 @@ namespace osu.Game.Graphics.UserInterface
// Animate rollover only when going backwards // Animate rollover only when going backwards
if (newValue > currentValue) if (newValue > currentValue)
{ {
updateTransforms(typeof(TranformULongCounter)); updateTransforms(typeof(TransformULongCounter));
removeTransforms(typeof(TranformULongCounter)); removeTransforms(typeof(TransformULongCounter));
VisibleCount = newValue; VisibleCount = newValue;
} }
else 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; ulong difference = currentValue > newValue ? currentValue - newValue : currentValue - newValue;
return difference * RollingDuration; return difference * RollingDuration;

View File

@ -18,10 +18,7 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public class ULongCounter : RollingCounter<ulong> public class ULongCounter : RollingCounter<ulong>
{ {
protected override void transformCount(ulong currentValue, ulong newValue) protected override Type transformType => typeof(TransformULongCounter);
{
transformCount(new TranformULongCounter(Clock), currentValue, newValue);
}
public override void ResetCount() public override void ResetCount()
{ {
@ -33,7 +30,7 @@ namespace osu.Game.Graphics.UserInterface
return count.ToString("#,0"); return count.ToString("#,0");
} }
protected class TranformULongCounter : Transform<ulong> protected class TransformULongCounter : Transform<ulong>
{ {
public override ulong CurrentValue public override ulong CurrentValue
{ {
@ -53,7 +50,7 @@ namespace osu.Game.Graphics.UserInterface
(d as ULongCounter).VisibleCount = CurrentValue; (d as ULongCounter).VisibleCount = CurrentValue;
} }
public TranformULongCounter(IClock clock) public TransformULongCounter(IClock clock)
: base(clock) : base(clock)
{ {
} }