1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 23:23:04 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/RollingCounter.cs

221 lines
7.3 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-10-14 06:13:20 +08:00
2017-03-10 12:08:59 +08:00
using osu.Framework.Configuration;
using osu.Framework.Graphics;
2016-10-07 15:05:02 +08:00
using osu.Framework.Graphics.Containers;
2016-10-14 06:13:20 +08:00
using osu.Framework.Graphics.Sprites;
2017-02-25 20:12:39 +08:00
using osu.Framework.Graphics.Transforms;
2017-03-10 12:08:59 +08:00
using osu.Game.Graphics.Sprites;
2016-10-07 15:05:02 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTK.Graphics;
2016-10-07 15:05:02 +08:00
namespace osu.Game.Graphics.UserInterface
{
public abstract class RollingCounter<T> : Container, IHasAccentColour
2016-10-07 15:05:02 +08:00
{
2017-03-10 12:08:59 +08:00
/// <summary>
/// The current value.
/// </summary>
public Bindable<T> Current = new Bindable<T>();
/// <summary>
/// Type of the Transform to use.
/// </summary>
/// <remarks>
2017-03-07 09:59:19 +08:00
/// Must be a subclass of Transform(T)
/// </remarks>
2017-06-21 10:18:52 +08:00
protected virtual Type TransformType => typeof(Transform<T, Drawable>);
2016-10-16 08:07:07 +08:00
protected SpriteText DisplayedCountSpriteText;
2016-10-07 15:05:02 +08:00
/// <summary>
2016-10-14 08:50:06 +08:00
/// If true, the roll-up duration will be proportional to change in value.
2016-10-07 15:05:02 +08:00
/// </summary>
2016-10-18 10:40:50 +08:00
protected virtual bool IsRollingProportional => false;
2016-10-07 15:05:02 +08:00
/// <summary>
2016-10-10 03:48:24 +08: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 15:05:02 +08:00
/// </summary>
2016-10-18 10:40:50 +08:00
protected virtual double RollingDuration => 0;
2016-10-07 15:05:02 +08:00
/// <summary>
/// Easing for the counter rollover animation.
/// </summary>
protected virtual EasingTypes RollingEasing => EasingTypes.OutQuint;
2016-10-07 15:05:02 +08:00
2016-10-16 08:07:07 +08:00
private T displayedCount;
2016-10-07 15:05:02 +08:00
/// <summary>
/// Value shown at the current moment.
/// </summary>
2016-10-16 08:07:07 +08:00
public virtual T DisplayedCount
2016-10-07 15:05:02 +08:00
{
get
{
2016-10-16 08:07:07 +08:00
return displayedCount;
2016-10-07 15:05:02 +08:00
}
protected set
{
if (EqualityComparer<T>.Default.Equals(displayedCount, value))
2016-10-07 15:05:02 +08:00
return;
2016-10-16 08:07:07 +08:00
displayedCount = value;
DisplayedCountSpriteText.Text = FormatCount(value);
2016-10-07 15:05:02 +08:00
}
}
public abstract void Increment(T amount);
2017-02-07 15:15:45 +08:00
private float textSize;
2016-10-14 06:13:20 +08:00
public float TextSize
{
get { return textSize; }
set
{
textSize = value;
2016-10-16 08:07:07 +08:00
DisplayedCountSpriteText.TextSize = value;
2016-10-14 06:13:20 +08:00
}
}
public Color4 AccentColour
{
get { return DisplayedCountSpriteText.Colour; }
set { DisplayedCountSpriteText.Colour = value; }
}
2016-10-14 06:13:20 +08:00
/// <summary>
/// Skeleton of a numeric counter which value rolls over time.
/// </summary>
2016-10-13 10:46:51 +08:00
protected RollingCounter()
{
2016-10-14 06:13:20 +08:00
Children = new Drawable[]
{
2017-03-07 09:59:19 +08:00
DisplayedCountSpriteText = new OsuSpriteText
{
Font = @"Venera"
},
2016-10-14 06:13:20 +08:00
};
TextSize = 40;
2016-10-22 16:50:42 +08:00
AutoSizeAxes = Axes.Both;
2017-03-10 12:08:59 +08:00
DisplayedCount = Current;
Current.ValueChanged += newValue =>
{
if (IsLoaded) TransformCount(displayedCount, newValue);
};
2016-10-07 15:05:02 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2017-03-10 12:08:59 +08:00
DisplayedCountSpriteText.Text = FormatCount(Current);
}
/// <summary>
/// Sets count value, bypassing rollover animation.
/// </summary>
/// <param name="count">New count value.</param>
public virtual void SetCountWithoutRolling(T count)
{
2017-03-10 12:08:59 +08:00
Current.Value = count;
StopRolling();
}
/// <summary>
2016-10-16 08:07:07 +08:00
/// Stops rollover animation, forcing the displayed count to be the actual count.
/// </summary>
public virtual void StopRolling()
{
2016-10-15 07:23:27 +08:00
Flush(false, TransformType);
2017-03-10 12:08:59 +08:00
DisplayedCount = Current;
}
/// <summary>
/// Resets count to default value.
/// </summary>
2016-10-14 06:13:20 +08:00
public virtual void ResetCount()
{
SetCountWithoutRolling(default(T));
}
2016-10-07 15:05:02 +08:00
/// <summary>
2016-10-08 05:14:35 +08: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 15:05:02 +08:00
/// </summary>
/// <remarks>
/// To be used in conjunction with IsRollingProportional = true.
2016-10-08 05:14:35 +08:00
/// Unless a derived class needs to have a proportional rolling, it is not necessary to override this function.
2016-10-07 15:05:02 +08: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-15 07:23:27 +08:00
protected virtual double GetProportionalDuration(T currentValue, T newValue)
2016-10-07 15:05:02 +08:00
{
return RollingDuration;
}
/// <summary>
/// Used to format counts.
/// </summary>
/// <param name="count">Count to format.</param>
/// <returns>Count formatted as a string.</returns>
2016-10-15 07:23:27 +08:00
protected virtual string FormatCount(T count)
2016-10-07 15:05:02 +08:00
{
return count.ToString();
}
/// <summary>
2016-10-08 05:14:35 +08: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 15:05:02 +08:00
/// </summary>
/// <param name="currentValue">Count value before modification.</param>
/// <param name="newValue">Expected count value after modification-</param>
2016-10-15 07:23:27 +08:00
/// <seealso cref="TransformType"/>
protected virtual void TransformCount(T currentValue, T newValue)
{
2016-10-17 07:30:25 +08:00
Debug.Assert(
2017-06-21 10:18:52 +08:00
typeof(Transform<T, Drawable>).IsAssignableFrom(TransformType),
2016-10-17 07:30:25 +08:00
@"transformType should be a subclass of Transform<T>."
);
2017-06-21 10:18:52 +08:00
TransformCount((Transform<T, Drawable>)Activator.CreateInstance(TransformType), currentValue, newValue);
}
2016-10-07 15:05:02 +08:00
/// <summary>
2016-10-15 07:23:27 +08:00
/// Intended to be used by TransformCount(T currentValue, T newValue).
2016-10-07 15:05:02 +08:00
/// </summary>
2017-06-21 10:18:52 +08:00
protected void TransformCount(Transform<T, Drawable> transform, T currentValue, T newValue)
2016-10-07 15:05:02 +08:00
{
Type type = transform.GetType();
2016-10-15 07:23:27 +08:00
Flush(false, type);
2016-10-07 15:05:02 +08:00
2016-10-17 07:30:25 +08:00
if (RollingDuration < 1)
2016-10-07 15:05:02 +08:00
{
2017-03-10 12:08:59 +08:00
DisplayedCount = Current;
2016-10-07 15:05:02 +08:00
return;
}
2016-10-14 11:54:02 +08:00
double rollingTotalDuration =
IsRollingProportional
2016-10-15 07:23:27 +08:00
? GetProportionalDuration(currentValue, newValue)
2016-10-14 11:54:02 +08:00
: RollingDuration;
2017-04-11 12:45:16 +08:00
transform.StartTime = TransformStartTime;
transform.EndTime = TransformStartTime + rollingTotalDuration;
2016-10-07 15:05:02 +08:00
transform.StartValue = currentValue;
transform.EndValue = newValue;
transform.Easing = RollingEasing;
Transforms.Add(transform);
}
}
}