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

249 lines
7.8 KiB
C#
Raw Normal View History

2016-10-14 06:13:20 +08:00
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework;
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;
2016-10-07 15:05:02 +08:00
using osu.Framework.Graphics.Transformations;
using System;
using System.Collections.Generic;
using System.Diagnostics;
2016-10-07 15:05:02 +08:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace osu.Game.Graphics.UserInterface
{
2016-10-13 03:33:04 +08:00
public abstract class RollingCounter<T> : AutoSizeContainer
2016-10-07 15:05:02 +08:00
{
/// <summary>
/// Type of the Transform to use.
/// </summary>
/// <remarks>
/// Must be a subclass of Transform<T>
/// </remarks>
protected virtual Type transformType => typeof(Transform<T>);
2016-10-14 08:50:06 +08:00
protected double rollingTotalDuration = 0;
2016-10-07 15:05:02 +08:00
2016-10-14 06:13:20 +08:00
protected SpriteText countSpriteText;
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>
public bool IsRollingProportional = false;
/// <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-14 08:50:06 +08:00
public double RollingDuration = 0;
2016-10-07 15:05:02 +08:00
/// <summary>
/// Easing for the counter rollover animation.
/// </summary>
public EasingTypes RollingEasing = EasingTypes.None;
protected T prevVisibleCount;
2016-10-07 15:05:02 +08:00
protected T visibleCount;
/// <summary>
/// Value shown at the current moment.
/// </summary>
public virtual T VisibleCount
{
get
{
return visibleCount;
}
protected set
{
if (visibleCount.Equals(value))
return;
visibleCount = value;
2016-10-14 06:13:20 +08:00
countSpriteText.Text = formatCount(value);
2016-10-07 15:05:02 +08:00
}
}
protected T prevCount;
2016-10-07 15:05:02 +08:00
protected T count;
/// <summary>
/// Actual value of counter.
/// </summary>
public virtual T Count
{
get
{
return count;
}
set
{
prevCount = count;
count = value;
2016-10-13 03:33:04 +08:00
if (IsLoaded)
2016-10-07 15:05:02 +08:00
{
2016-10-14 06:13:20 +08:00
rollingTotalDuration =
2016-10-08 05:14:35 +08:00
IsRollingProportional
2016-10-14 06:13:20 +08:00
? getProportionalDuration(visibleCount, value)
2016-10-08 05:14:35 +08:00
: RollingDuration;
2016-10-14 06:13:20 +08:00
transformCount(visibleCount, count);
2016-10-07 15:05:02 +08:00
}
}
}
2016-10-14 06:13:20 +08:00
protected float textSize = 20.0f;
public float TextSize
{
get { return textSize; }
set
{
textSize = value;
countSpriteText.TextSize = value;
}
}
/// <summary>
/// Skeleton of a numeric counter which value rolls over time.
/// </summary>
2016-10-13 10:46:51 +08:00
protected RollingCounter()
{
2016-10-10 03:48:24 +08:00
Debug.Assert(
transformType.IsSubclassOf(typeof(Transform<T>)) || transformType == typeof(Transform<T>),
@"transformType should be a subclass of Transform<T>."
);
2016-10-14 06:13:20 +08:00
Children = new Drawable[]
{
countSpriteText = new SpriteText
{
Anchor = this.Anchor,
Origin = this.Origin,
},
};
}
public override void Load(BaseGame game)
2016-10-07 15:05:02 +08:00
{
base.Load(game);
2016-10-13 03:33:04 +08:00
removeTransforms(transformType);
2016-10-14 06:13:20 +08:00
2016-10-07 15:05:02 +08:00
VisibleCount = Count;
2016-10-14 06:13:20 +08:00
countSpriteText.Text = formatCount(count);
countSpriteText.Anchor = this.Anchor;
countSpriteText.Origin = this.Origin;
2016-10-07 15:05:02 +08:00
}
/// <summary>
/// Sets count value, bypassing rollover animation.
/// </summary>
/// <param name="count">New count value.</param>
public virtual void SetCountWithoutRolling(T count)
{
Count = count;
StopRolling();
}
/// <summary>
/// Stops rollover animation, forcing the visible count to be the actual count.
/// </summary>
public virtual void StopRolling()
{
removeTransforms(transformType);
VisibleCount = Count;
}
/// <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-14 08:50:06 +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>
protected virtual string formatCount(T count)
{
return count.ToString();
}
protected void updateTransforms(Type type)
{
foreach (ITransform t in Transforms.AliveItems)
if (t.GetType() == type)
2016-10-07 15:05:02 +08:00
t.Apply(this);
}
protected void removeTransforms(Type type)
{
Transforms.RemoveAll(t => t.GetType() == type);
2016-10-07 15:05:02 +08:00
}
/// <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>
/// <seealso cref="transformType"/>
protected virtual void transformCount(T currentValue, T newValue)
{
object[] parameters = { Clock };
transformCount((Transform<T>)Activator.CreateInstance(transformType, parameters), currentValue, newValue);
}
2016-10-07 15:05:02 +08:00
/// <summary>
/// Intended to be used by transformCount().
/// </summary>
/// <see cref="transformCount"/>
protected void transformCount(Transform<T> transform, T currentValue, T newValue)
{
Type type = transform.GetType();
updateTransforms(type);
removeTransforms(type);
if (Clock == null)
return;
if (RollingDuration == 0)
{
VisibleCount = Count;
return;
}
transform.StartTime = Time;
2016-10-14 06:13:20 +08:00
transform.EndTime = Time + rollingTotalDuration;
2016-10-07 15:05:02 +08:00
transform.StartValue = currentValue;
transform.EndValue = newValue;
transform.Easing = RollingEasing;
Transforms.Add(transform);
}
}
}