1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 00:27:25 +08:00
osu-lazer/osu.Game/Modes/UI/ComboCounter.cs

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