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