// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; using osu.Game.Graphics.Sprites; using System; using System.Collections.Generic; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics.UserInterface; using osu.Framework.Localisation; namespace osu.Game.Graphics.UserInterface { public abstract class RollingCounter : Container, IHasCurrentValue where T : struct, IEquatable { private readonly BindableWithCurrent current = new BindableWithCurrent(); public Bindable Current { get => current.Current; set => current.Current = value; } private IHasText displayedCountText; public Drawable DrawableCount { get; private set; } /// /// 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 Easing RollingEasing => Easing.OutQuint; private T displayedCount; /// /// Value shown at the current moment. /// public virtual T DisplayedCount { get => displayedCount; set { if (EqualityComparer.Default.Equals(displayedCount, value)) return; displayedCount = value; UpdateDisplay(); } } /// /// Skeleton of a numeric counter which value rolls over time. /// protected RollingCounter() { AutoSizeAxes = Axes.Both; } [BackgroundDependencyLoader] private void load() { displayedCountText = CreateText(); UpdateDisplay(); Child = DrawableCount = (Drawable)displayedCountText; } protected void UpdateDisplay() { if (displayedCountText != null) displayedCountText.Text = FormatCount(DisplayedCount); } protected override void LoadComplete() { base.LoadComplete(); Current.BindValueChanged(val => TransformCount(DisplayedCount, val.NewValue), true); } /// /// 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() { FinishTransforms(false, nameof(DisplayedCount)); DisplayedCount = Current.Value; } /// /// Resets count to default value. /// public virtual void ResetCount() { SetCountWithoutRolling(default); } /// /// 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 localisable string. protected virtual LocalisableString 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) { double rollingTotalDuration = IsRollingProportional ? GetProportionalDuration(currentValue, newValue) : RollingDuration; this.TransformTo(nameof(DisplayedCount), newValue, rollingTotalDuration, RollingEasing); } /// /// Creates the text. Delegates to by default. /// protected virtual IHasText CreateText() => CreateSpriteText(); /// /// Creates an which may be used to display this counter's text. /// May not be called if is overridden. /// protected virtual OsuSpriteText CreateSpriteText() => new OsuSpriteText { Font = OsuFont.Numeric.With(size: 40f), }; } }