using osu.Framework; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Transformations; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace osu.Game.Graphics.UserInterface { /// /// Skeleton for a counter which value rolls-up in a lapse of time. /// /// /// This class only abstracts the basics to roll-up a value in a lapse of time by using Transforms. /// In order to show a value, you must implement a way to display it, i.e., as a numeric counter or a bar. /// /// Type of the actual counter. public abstract class RollingCounter : AutoSizeContainer { /// /// Type of the Transform to use. /// /// /// Must be a subclass of Transform /// protected virtual Type transformType => typeof(Transform); protected ulong RollingTotalDuration = 0; /// /// If true, each time the Count is updated, it will roll over from the current visible value. /// Else, it will roll up from the current count value. /// public bool IsRollingContinuous = true; /// /// If true, the roll-up duration will be proportional to the counter. /// public bool IsRollingProportional = false; /// /// If IsRollingProportional = false, duration in milliseconds for the counter roll-up animation for each /// element; else duration in milliseconds for the counter roll-up animation in total. /// public ulong RollingDuration = 0; /// /// Easing for the counter rollover animation. /// public EasingTypes RollingEasing = EasingTypes.None; protected T prevVisibleCount; protected T visibleCount; /// /// Value shown at the current moment. /// public virtual T VisibleCount { get { return visibleCount; } protected set { prevVisibleCount = visibleCount; if (visibleCount.Equals(value)) return; visibleCount = value; transformVisibleCount(prevVisibleCount, value); } } protected T prevCount; protected T count; /// /// Actual value of counter. /// public virtual T Count { get { return count; } set { prevCount = count; count = value; if (IsLoaded) { RollingTotalDuration = IsRollingProportional ? getProportionalDuration(VisibleCount, value) : RollingDuration; transformCount(IsRollingContinuous ? VisibleCount : prevCount, value); } } } protected RollingCounter() : base() { Debug.Assert( transformType.IsSubclassOf(typeof(Transform)) || transformType == typeof(Transform), @"transformType should be a subclass of Transform." ); } public override void Load(BaseGame game) { base.Load(game); removeTransforms(transformType); if (Count == null) ResetCount(); VisibleCount = Count; } /// /// Sets count value, bypassing rollover animation. /// /// New count value. public virtual void SetCountWithoutRolling(T count) { Count = count; StopRolling(); } /// /// Stops rollover animation, forcing the visible count to be the actual count. /// public virtual void StopRolling() { removeTransforms(transformType); VisibleCount = Count; } /// /// Resets count to default value. /// 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() == type) t.Apply(this); } protected void removeTransforms(Type type) { Transforms.RemoveAll(t => t.GetType() == type); } /// /// Called when the count is updated to add a transformer that changes the value of the visible count (i.e. /// implement the rollover animation). /// /// Count value before modification. /// Expected count value after modification- /// /// 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 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 virtual void transformCount(T currentValue, T newValue) { object[] parameters = { Clock }; transformCount((Transform)Activator.CreateInstance(transformType, parameters), currentValue, newValue); } /// /// Intended to be used by transformCount(). /// /// protected void transformCount(Transform transform, T currentValue, T newValue) { Type type = transform.GetType(); updateTransforms(type); removeTransforms(type); if (Clock == null) return; if (RollingDuration == 0) { VisibleCount = Count; return; } transform.StartTime = Time; transform.EndTime = Time + RollingTotalDuration; transform.StartValue = currentValue; transform.EndValue = newValue; transform.Easing = RollingEasing; Transforms.Add(transform); } /// /// This procedure is called each time the visible count value is updated. /// Override to create custom animations. /// /// Visible count value before modification. /// Expected visible count value after modification- protected abstract void transformVisibleCount(T currentValue, T newValue); } }