// Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using osu.Framework.Configuration; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Transforms; using osu.Game.Graphics.Sprites; using System; using System.Collections.Generic; using OpenTK.Graphics; using osu.Framework.MathUtils; namespace osu.Game.Graphics.UserInterface { public abstract class RollingCounter : Container, IHasAccentColour where T : struct, IEquatable { /// /// The current value. /// public Bindable Current = new Bindable(); 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.OutQuint; private T displayedCount; /// /// Value shown at the current moment. /// public virtual T DisplayedCount { get { return displayedCount; } set { if (EqualityComparer.Default.Equals(displayedCount, value)) return; displayedCount = value; DisplayedCountSpriteText.Text = FormatCount(value); } } public abstract void Increment(T amount); private float textSize; public float TextSize { get { return textSize; } set { textSize = value; DisplayedCountSpriteText.TextSize = value; } } public Color4 AccentColour { get { return DisplayedCountSpriteText.Colour; } set { DisplayedCountSpriteText.Colour = value; } } /// /// Skeleton of a numeric counter which value rolls over time. /// protected RollingCounter() { Children = new Drawable[] { DisplayedCountSpriteText = new OsuSpriteText { Font = @"Venera" }, }; TextSize = 40; AutoSizeAxes = Axes.Both; DisplayedCount = Current; Current.ValueChanged += newValue => { if (IsLoaded) TransformCount(displayedCount, newValue); }; } protected override void LoadComplete() { base.LoadComplete(); DisplayedCountSpriteText.Text = FormatCount(Current); } /// /// Sets count value, bypassing rollover animation. /// /// New count value. public virtual void SetCountWithoutRolling(T count) { Current.Value = count; StopRolling(); } /// /// Stops rollover animation, forcing the displayed count to be the actual count. /// public virtual void StopRolling() { Flush(false, typeof(TransformRollingCounter)); DisplayedCount = Current; } /// /// 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) { TransformCount(new TransformRollingCounter(this), currentValue, newValue); } /// /// Intended to be used by TransformCount(T currentValue, T newValue). /// protected void TransformCount(TransformRollingCounter transform, T currentValue, T newValue) { double rollingTotalDuration = IsRollingProportional ? GetProportionalDuration(currentValue, newValue) : RollingDuration; this.TransformTo(newValue, rollingTotalDuration, RollingEasing, transform); } protected class TransformRollingCounter : Transform> { public TransformRollingCounter(RollingCounter target) : base(target) { } public T CurrentValue { get { double time = Time?.Current ?? 0; if (time < StartTime) return StartValue; if (time >= EndTime) return EndValue; double result = Interpolation.ValueAt(time, Convert.ToDouble(StartValue), Convert.ToDouble(EndValue), StartTime, EndTime, Easing); return (T)Convert.ChangeType(result, typeof(T), null); } } public override void Apply(RollingCounter d) => d.DisplayedCount = CurrentValue; public override void ReadIntoStartValue(RollingCounter d) => StartValue = d.DisplayedCount; } } }