using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Transformations; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace osu.Game.Graphics.UserInterface { /// /// Skeleton for a counter with a simple rollover animation. /// /// Type of the actual counter. public abstract class RollingCounter : Container { protected SpriteText countSpriteText; protected ulong RollingTotalDuration = 0; protected float textSize = 20.0f; public float TextSize { get { return textSize; } set { textSize = value; updateTextSize(); } } /// /// If true, each time the Count is updated, it will roll over from the current visible value. /// Else, it will roll over from the current count value. /// public bool IsRollingContinuous = true; /// /// If true, the rollover duration will be proportional to the counter. /// public bool IsRollingProportional = false; /// /// If IsRollingProportional = false, duration in milliseconds for the counter rollover animation for each element. /// If IsRollingProportional = true, duration in milliseconds for the counter rollover animation in total. /// public ulong RollingDuration = 0; /// /// Easing for the counter rollover animation. /// public EasingTypes RollingEasing = EasingTypes.None; protected T visibleCount; /// /// Value shown at the current moment. /// public virtual T VisibleCount { get { return visibleCount; } protected set { if (visibleCount.Equals(value)) return; transformVisibleCount(visibleCount, value); visibleCount = value; } } protected T count; /// /// Actual value of counter. /// public virtual T Count { get { return count; } set { if (Clock != null) { RollingTotalDuration = IsRollingProportional ? GetProportionalDuration(VisibleCount, value) : RollingDuration; transformCount(IsRollingContinuous ? VisibleCount : count, value); } count = value; } } public override void Load() { base.Load(); removeTransforms(typeof(Transform)); if (Count == null) ResetCount(); VisibleCount = Count; Children = new Drawable[] { countSpriteText = new SpriteText { Text = formatCount(Count), TextSize = this.TextSize, Anchor = this.Anchor, Origin = this.Origin, }, }; } /// /// Calculates the duration of the rollover animation by using the difference between the current visible value and the new final value. /// /// /// Intended to be used in conjunction with IsRolloverProportional = true. /// If you're sure your superclass won't never need to be proportional, then 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. /// /// 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(typeof(Transform)); VisibleCount = Count; } /// /// Resets count to default value. /// public abstract void ResetCount(); protected void updateTransforms(Type type) { foreach (ITransform t in Transforms.AliveItems) if (t.GetType().IsAssignableFrom(type)) t.Apply(this); } protected void removeTransforms(Type type) { Transforms.RemoveAll(t => t.GetType().IsSubclassOf(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 a custom Transformer related to the /// type T of the RolloverCounter. /// 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); /// /// 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 virtual void transformVisibleCount(T currentValue, T newValue) { if (countSpriteText != null) { countSpriteText.Text = formatCount(newValue); } } protected virtual void updateTextSize() { if (countSpriteText != null) countSpriteText.TextSize = TextSize; } } }