//Copyright (c) 2007-2016 ppy Pty Ltd . //Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using osu.Framework; 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.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace osu.Game.Graphics.UserInterface { 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 DisplayedCountSpriteText; /// /// If true, the roll-up duration will be proportional to change in value. /// protected virtual 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. /// protected virtual double RollingDuration => 0; /// /// Easing for the counter rollover animation. /// protected virtual EasingTypes RollingEasing => EasingTypes.None; private T displayedCount; /// /// Value shown at the current moment. /// public virtual T DisplayedCount { get { return displayedCount; } protected set { if (displayedCount.Equals(value)) return; displayedCount = value; DisplayedCountSpriteText.Text = FormatCount(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) { TransformCount(displayedCount, count); } } } public void Set(T value) { Count = value; } public abstract void Increment(T amount); protected float textSize; public float TextSize { get { return textSize; } set { textSize = value; DisplayedCountSpriteText.TextSize = value; } } /// /// Skeleton of a numeric counter which value rolls over time. /// protected RollingCounter() { Children = new Drawable[] { DisplayedCountSpriteText = new SpriteText(), }; TextSize = 40; AutoSizeAxes = Axes.Both; } protected override void Load(BaseGame game) { base.Load(game); DisplayedCount = Count; DisplayedCountSpriteText.Text = FormatCount(count); DisplayedCountSpriteText.Anchor = this.Anchor; DisplayedCountSpriteText.Origin = this.Origin; } protected override void LoadComplete() { base.LoadComplete(); Flush(false, TransformType); } /// /// Sets count value, bypassing rollover animation. /// /// New count value. public virtual void SetCountWithoutRolling(T count) { Count = count; StopRolling(); } /// /// Stops rollover animation, forcing the displayed count to be the actual count. /// public virtual void StopRolling() { Flush(false, TransformType); DisplayedCount = Count; } /// /// Resets count to default value. /// public virtual void ResetCount() { SetCountWithoutRolling(default(T)); } /// /// 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 double 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(); } /// /// 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- /// protected virtual void TransformCount(T currentValue, T newValue) { Debug.Assert( TransformType.IsSubclassOf(typeof(Transform)) || TransformType == typeof(Transform), @"transformType should be a subclass of Transform." ); TransformCount((Transform)Activator.CreateInstance(TransformType), currentValue, newValue); } /// /// Intended to be used by TransformCount(T currentValue, T newValue). /// protected void TransformCount(Transform transform, T currentValue, T newValue) { Type type = transform.GetType(); Flush(false, type); if (RollingDuration < 1) { DisplayedCount = Count; return; } double rollingTotalDuration = IsRollingProportional ? GetProportionalDuration(currentValue, newValue) : RollingDuration; transform.StartTime = Time.Current; transform.EndTime = Time.Current + rollingTotalDuration; transform.StartValue = currentValue; transform.EndValue = newValue; transform.Easing = RollingEasing; Transforms.Add(transform); } } }