osu/osu.Game/Modes/UI/ComboCounter.cs

270 lines
7.9 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-13 22:13:20 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Transforms;
using osu.Framework.MathUtils;
using osu.Game.Graphics.Sprites;
2016-10-13 22:13:20 +00:00
2016-11-14 09:54:24 +00:00
namespace osu.Game.Modes.UI
2016-10-13 22:13:20 +00:00
{
public abstract class ComboCounter : Container
2016-10-13 22:13:20 +00:00
{
public bool IsRolling
{
get; protected set;
}
2016-10-14 23:23:27 +00:00
protected SpriteText PopOutCount;
2016-10-29 23:26:12 +00:00
protected virtual double PopOutDuration => 150;
protected virtual float PopOutScale => 2.0f;
protected virtual EasingTypes PopOutEasing => EasingTypes.None;
protected virtual float PopOutInitialAlpha => 0.75f;
2016-10-13 22:13:20 +00:00
protected virtual double FadeOutDuration => 100;
/// <summary>
/// Duration in milliseconds for the counter roll-up animation for each element.
/// </summary>
protected virtual double RollingDuration => 20;
/// <summary>
/// Easing for the counter rollover animation.
/// </summary>
protected EasingTypes RollingEasing => EasingTypes.None;
2016-10-13 22:13:20 +00:00
private ulong displayedCount;
/// <summary>
/// Value shown at the current moment.
/// </summary>
public virtual ulong DisplayedCount
{
get
{
return displayedCount;
}
protected set
{
if (displayedCount.Equals(value))
return;
updateDisplayedCount(displayedCount, value, IsRolling);
}
2016-10-13 22:13:20 +00:00
}
private ulong count;
/// <summary>
/// Actual value of counter.
/// </summary>
public virtual ulong Count
{
get
{
return count;
}
set
{
updateCount(value);
}
}
public void Increment(ulong amount = 1)
2016-10-13 22:13:20 +00:00
{
Count = Count + amount;
}
2016-10-13 22:13:20 +00:00
protected SpriteText DisplayedCountSpriteText;
2016-10-22 08:50:42 +00:00
private float textSize;
public float TextSize
{
get { return textSize; }
set
{
textSize = value;
DisplayedCountSpriteText.TextSize = TextSize;
PopOutCount.TextSize = TextSize;
}
}
/// <summary>
/// Base of all combo counters.
/// </summary>
protected ComboCounter()
{
AutoSizeAxes = Axes.Both;
Children = new Drawable[]
{
DisplayedCountSpriteText = new OsuSpriteText
{
Alpha = 0,
},
PopOutCount = new OsuSpriteText
{
Alpha = 0,
Margin = new MarginPadding(0.05f),
}
};
TextSize = 80;
2016-10-13 22:13:20 +00:00
}
protected override void LoadComplete()
2016-10-13 22:13:20 +00:00
{
base.LoadComplete();
DisplayedCountSpriteText.Text = FormatCount(Count);
DisplayedCountSpriteText.Anchor = Anchor;
DisplayedCountSpriteText.Origin = Origin;
StopRolling();
2016-10-13 22:13:20 +00:00
}
/// <summary>
/// Stops rollover animation, forcing the displayed count to be the actual count.
/// </summary>
public void StopRolling()
2016-10-13 22:13:20 +00:00
{
updateCount(Count);
2016-10-13 22:13:20 +00:00
}
/// <summary>
/// Animates roll-up/roll-back to an specific value.
/// </summary>
/// <param name="newValue">Target value.</param>
public virtual void Roll(ulong newValue = 0)
2016-10-13 22:13:20 +00:00
{
updateCount(newValue, true);
}
2016-10-13 22:13:20 +00:00
/// <summary>
/// Resets count to default value.
/// </summary>
public virtual void ResetCount()
{
updateCount(0);
2016-10-13 22:13:20 +00:00
}
protected virtual string FormatCount(ulong count)
{
return count.ToString();
}
protected abstract void OnDisplayedCountRolling(ulong currentValue, ulong newValue);
protected abstract void OnDisplayedCountIncrement(ulong newValue);
protected abstract void OnDisplayedCountChange(ulong newValue);
protected virtual void OnCountRolling(ulong currentValue, ulong newValue)
{
transformRoll(new TransformComboRoll(), currentValue, newValue);
2016-10-16 00:07:07 +00:00
}
protected virtual void OnCountIncrement(ulong currentValue, ulong newValue)
2016-10-16 00:07:07 +00:00
{
DisplayedCount = newValue;
}
2016-10-13 22:13:20 +00:00
protected virtual void OnCountChange(ulong currentValue, ulong newValue)
{
DisplayedCount = newValue;
}
private double getProportionalDuration(ulong currentValue, ulong newValue)
{
double difference = currentValue > newValue ? currentValue - newValue : newValue - currentValue;
return difference * RollingDuration;
}
private void updateDisplayedCount(ulong currentValue, ulong newValue, bool rolling)
{
displayedCount = newValue;
if (rolling)
OnDisplayedCountRolling(currentValue, newValue);
else if (currentValue + 1 == newValue)
OnDisplayedCountIncrement(newValue);
else
OnDisplayedCountChange(newValue);
2016-10-13 22:13:20 +00:00
}
private void updateCount(ulong value, bool rolling = false)
2016-10-13 22:13:20 +00:00
{
ulong prevCount = count;
count = value;
2016-10-13 22:13:20 +00:00
if (!IsLoaded)
return;
2016-10-13 22:13:20 +00:00
if (!rolling)
{
Flush(false, typeof(TransformComboRoll));
IsRolling = false;
DisplayedCount = prevCount;
if (prevCount + 1 == count)
OnCountIncrement(prevCount, count);
else
OnCountChange(prevCount, count);
}
else
{
OnCountRolling(displayedCount, count);
IsRolling = true;
}
2016-10-13 22:13:20 +00:00
}
private void transformRoll(TransformComboRoll transform, ulong currentValue, ulong newValue)
2016-10-13 22:13:20 +00:00
{
Flush(false, typeof(TransformComboRoll));
if (RollingDuration < 1)
{
DisplayedCount = Count;
return;
}
transform.StartTime = Time.Current;
transform.EndTime = Time.Current + getProportionalDuration(currentValue, newValue);
transform.StartValue = currentValue;
transform.EndValue = newValue;
transform.Easing = RollingEasing;
Transforms.Add(transform);
}
2016-10-13 22:13:20 +00:00
protected class TransformComboRoll : Transform<ulong>
{
protected override ulong CurrentValue
{
get
{
double time = Time?.Current ?? 0;
if (time < StartTime) return StartValue;
if (time >= EndTime) return EndValue;
return (ulong)Interpolation.ValueAt(time, StartValue, EndValue, StartTime, EndTime, Easing);
}
}
2016-10-13 22:13:20 +00:00
public override void Apply(Drawable d)
{
base.Apply(d);
((ComboCounter)d).DisplayedCount = CurrentValue;
}
2016-10-13 22:13:20 +00:00
}
public void Set(ulong value)
{
if (value == 0)
Roll();
else
Count = value;
}
2016-10-13 22:13:20 +00:00
}
}