1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 03:12:54 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/ComboCounter.cs

201 lines
5.8 KiB
C#
Raw Normal View History

// 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 17:19:50 +08:00
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Screens.Play.HUD
{
public abstract class ComboCounter : Container
{
public BindableInt Current = new BindableInt
{
MinValue = 0,
};
public bool IsRolling { get; protected set; }
protected SpriteText PopOutCount;
protected virtual double PopOutDuration => 150;
protected virtual float PopOutScale => 2.0f;
protected virtual Easing PopOutEasing => Easing.None;
protected virtual float PopOutInitialAlpha => 0.75f;
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 Easing RollingEasing => Easing.None;
protected SpriteText DisplayedCountSpriteText;
private int previousValue;
/// <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;
Current.ValueChanged += combo => updateCount(combo.NewValue == 0);
2018-04-13 17:19:50 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2019-02-21 17:56:34 +08:00
DisplayedCountSpriteText.Text = FormatCount(Current.Value);
2018-04-13 17:19:50 +08:00
DisplayedCountSpriteText.Anchor = Anchor;
DisplayedCountSpriteText.Origin = Origin;
StopRolling();
}
private int displayedCount;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
/// <summary>
/// Value shown at the current moment.
/// </summary>
public virtual int DisplayedCount
{
get => displayedCount;
2018-04-13 17:19:50 +08:00
protected set
{
if (displayedCount.Equals(value))
return;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
updateDisplayedCount(displayedCount, value, IsRolling);
}
}
private float textSize;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public float TextSize
{
get => textSize;
2018-04-13 17:19:50 +08:00
set
{
textSize = value;
DisplayedCountSpriteText.Font = DisplayedCountSpriteText.Font.With(size: TextSize);
PopOutCount.Font = PopOutCount.Font.With(size: TextSize);
2018-04-13 17:19:50 +08:00
}
}
/// <summary>
/// Increments the combo by an amount.
/// </summary>
/// <param name="amount"></param>
public void Increment(int amount = 1)
{
2019-02-21 17:56:34 +08:00
Current.Value = Current.Value + amount;
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// Stops rollover animation, forcing the displayed count to be the actual count.
/// </summary>
public void StopRolling()
{
updateCount(false);
}
protected virtual string FormatCount(int count)
{
return count.ToString();
}
protected virtual void OnCountRolling(int currentValue, int newValue)
{
transformRoll(currentValue, newValue);
}
protected virtual void OnCountIncrement(int currentValue, int newValue)
{
DisplayedCount = newValue;
}
protected virtual void OnCountChange(int currentValue, int newValue)
{
DisplayedCount = newValue;
}
private double getProportionalDuration(int currentValue, int newValue)
{
double difference = currentValue > newValue ? currentValue - newValue : newValue - currentValue;
return difference * RollingDuration;
}
private void updateDisplayedCount(int currentValue, int newValue, bool rolling)
{
displayedCount = newValue;
if (rolling)
OnDisplayedCountRolling(currentValue, newValue);
else if (currentValue + 1 == newValue)
OnDisplayedCountIncrement(newValue);
else
OnDisplayedCountChange(newValue);
}
private void updateCount(bool rolling)
{
int prev = previousValue;
2019-02-21 17:56:34 +08:00
previousValue = Current.Value;
2018-04-13 17:19:50 +08:00
if (!IsLoaded)
return;
if (!rolling)
{
FinishTransforms(false, nameof(DisplayedCount));
IsRolling = false;
DisplayedCount = prev;
2019-02-21 17:56:34 +08:00
if (prev + 1 == Current.Value)
OnCountIncrement(prev, Current.Value);
2018-04-13 17:19:50 +08:00
else
2019-02-21 17:56:34 +08:00
OnCountChange(prev, Current.Value);
2018-04-13 17:19:50 +08:00
}
else
{
2019-02-21 17:56:34 +08:00
OnCountRolling(displayedCount, Current.Value);
2018-04-13 17:19:50 +08:00
IsRolling = true;
}
}
private void transformRoll(int currentValue, int newValue)
{
this.TransformTo(nameof(DisplayedCount), newValue, getProportionalDuration(currentValue, newValue), RollingEasing);
}
protected abstract void OnDisplayedCountRolling(int currentValue, int newValue);
protected abstract void OnDisplayedCountIncrement(int newValue);
protected abstract void OnDisplayedCountChange(int newValue);
}
}