2019-01-24 16:43:03 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
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;
|
2020-08-04 01:14:17 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2020-09-09 19:49:59 +08:00
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2021-07-24 04:37:08 +08:00
|
|
|
|
using osu.Framework.Localisation;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
|
public abstract partial class RollingCounter<T> : Container, IHasCurrentValue<T>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
where T : struct, IEquatable<T>
|
|
|
|
|
{
|
2020-09-09 19:49:59 +08:00
|
|
|
|
private readonly BindableWithCurrent<T> current = new BindableWithCurrent<T>();
|
|
|
|
|
|
|
|
|
|
public Bindable<T> Current
|
|
|
|
|
{
|
|
|
|
|
get => current.Current;
|
|
|
|
|
set => current.Current = value;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-05 14:39:29 +08:00
|
|
|
|
private IHasText displayedCountText;
|
|
|
|
|
|
|
|
|
|
public Drawable DrawableCount { get; private set; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If true, the roll-up duration will be proportional to change in value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool IsRollingProportional => false;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual double RollingDuration => 0;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Easing for the counter rollover animation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual Easing RollingEasing => Easing.OutQuint;
|
|
|
|
|
|
|
|
|
|
private T displayedCount;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Value shown at the current moment.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual T DisplayedCount
|
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
|
get => displayedCount;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (EqualityComparer<T>.Default.Equals(displayedCount, value))
|
|
|
|
|
return;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
displayedCount = value;
|
2020-10-19 13:05:28 +08:00
|
|
|
|
UpdateDisplay();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Skeleton of a numeric counter which value rolls over time.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected RollingCounter()
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 01:14:17 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2021-10-05 14:39:29 +08:00
|
|
|
|
displayedCountText = CreateText();
|
2020-10-19 13:05:28 +08:00
|
|
|
|
|
|
|
|
|
UpdateDisplay();
|
2021-10-05 14:39:29 +08:00
|
|
|
|
Child = DrawableCount = (Drawable)displayedCountText;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 13:05:28 +08:00
|
|
|
|
protected void UpdateDisplay()
|
|
|
|
|
{
|
2021-10-05 14:39:29 +08:00
|
|
|
|
if (displayedCountText != null)
|
|
|
|
|
displayedCountText.Text = FormatCount(DisplayedCount);
|
2020-10-19 13:05:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 17:13:51 +08:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
|
|
Current.BindValueChanged(val => TransformCount(DisplayedCount, val.NewValue), true);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets count value, bypassing rollover animation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="count">New count value.</param>
|
|
|
|
|
public virtual void SetCountWithoutRolling(T count)
|
|
|
|
|
{
|
|
|
|
|
Current.Value = count;
|
|
|
|
|
StopRolling();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops rollover animation, forcing the displayed count to be the actual count.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual void StopRolling()
|
|
|
|
|
{
|
|
|
|
|
FinishTransforms(false, nameof(DisplayedCount));
|
2019-02-21 17:56:34 +08:00
|
|
|
|
DisplayedCount = Current.Value;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resets count to default value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual void ResetCount()
|
|
|
|
|
{
|
2018-10-16 10:40:51 +08:00
|
|
|
|
SetCountWithoutRolling(default);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Calculates the duration of the roll-up animation by using the difference between the current visible value
|
|
|
|
|
/// and the new final value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// 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.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="currentValue">Current visible value.</param>
|
|
|
|
|
/// <param name="newValue">New final value.</param>
|
|
|
|
|
/// <returns>Calculated rollover duration in milliseconds.</returns>
|
|
|
|
|
protected virtual double GetProportionalDuration(T currentValue, T newValue)
|
|
|
|
|
{
|
|
|
|
|
return RollingDuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Used to format counts.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="count">Count to format.</param>
|
2021-07-24 04:37:08 +08:00
|
|
|
|
/// <returns>Count formatted as a localisable string.</returns>
|
|
|
|
|
protected virtual LocalisableString FormatCount(T count)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
return count.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when the count is updated to add a transformer that changes the value of the visible count (i.e.
|
|
|
|
|
/// implement the rollover animation).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="currentValue">Count value before modification.</param>
|
|
|
|
|
/// <param name="newValue">Expected count value after modification.</param>
|
|
|
|
|
protected virtual void TransformCount(T currentValue, T newValue)
|
|
|
|
|
{
|
|
|
|
|
double rollingTotalDuration =
|
|
|
|
|
IsRollingProportional
|
|
|
|
|
? GetProportionalDuration(currentValue, newValue)
|
|
|
|
|
: RollingDuration;
|
|
|
|
|
|
|
|
|
|
this.TransformTo(nameof(DisplayedCount), newValue, rollingTotalDuration, RollingEasing);
|
|
|
|
|
}
|
2020-08-04 01:14:17 +08:00
|
|
|
|
|
2021-10-01 19:09:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the text. Delegates to <see cref="CreateSpriteText"/> by default.
|
|
|
|
|
/// </summary>
|
2021-09-30 16:54:52 +08:00
|
|
|
|
protected virtual IHasText CreateText() => CreateSpriteText();
|
|
|
|
|
|
2021-10-01 19:09:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an <see cref="OsuSpriteText"/> which may be used to display this counter's text.
|
|
|
|
|
/// May not be called if <see cref="CreateText"/> is overridden.
|
|
|
|
|
/// </summary>
|
2020-08-04 01:14:17 +08:00
|
|
|
|
protected virtual OsuSpriteText CreateSpriteText() => new OsuSpriteText
|
|
|
|
|
{
|
2020-08-19 12:44:45 +08:00
|
|
|
|
Font = OsuFont.Numeric.With(size: 40f),
|
2020-08-04 01:14:17 +08:00
|
|
|
|
};
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|