1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 08:42:55 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/StarCounter.cs

156 lines
4.3 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
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
2018-04-13 17:19:50 +08:00
using System;
using System.Linq;
using osu.Framework.Graphics.Sprites;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.UserInterface
{
public class StarCounter : Container
{
private readonly Container<Star> stars;
/// <summary>
/// Maximum amount of stars displayed.
/// </summary>
/// <remarks>
/// This does not limit the counter value, but the amount of stars displayed.
/// </remarks>
public int StarCount { get; }
private double animationDelay => 80;
private double scalingDuration => 1000;
private Easing scalingEasing => Easing.OutElasticHalf;
private float minStarScale => 0.4f;
private double fadingDuration => 100;
private float minStarAlpha => 0.5f;
private const float star_size = 20;
private const float star_spacing = 4;
private float countStars;
/// <summary>
/// Amount of stars represented.
/// </summary>
public float CountStars
{
get => countStars;
2018-04-13 17:19:50 +08:00
set
{
if (countStars == value) return;
if (IsLoaded)
transformCount(value);
countStars = value;
}
}
/// <summary>
/// Shows a float count as stars. Used as star difficulty display.
/// </summary>
/// <param name="starCount">Maximum amount of stars to display.</param>
public StarCounter(int starCount = 10)
{
StarCount = Math.Max(starCount, 0);
AutoSizeAxes = Axes.Both;
Children = new Drawable[]
{
stars = new FillFlowContainer<Star>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(star_spacing),
ChildrenEnumerable = Enumerable.Range(0, StarCount).Select(i => new Star { Alpha = minStarAlpha })
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
// Animate initial state from zero.
ReplayAnimation();
}
public void ResetCount()
{
countStars = 0;
StopAnimation();
}
public void ReplayAnimation()
{
var t = countStars;
ResetCount();
CountStars = t;
}
public void StopAnimation()
{
int i = 0;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
foreach (var star in stars.Children)
{
star.ClearTransforms(true);
star.FadeTo(i < countStars ? 1.0f : minStarAlpha);
star.Icon.ScaleTo(getStarScale(i, countStars));
i++;
}
}
private float getStarScale(int i, float value)
{
if (value <= i)
return minStarScale;
return i + 1 <= value ? 1.0f : Interpolation.ValueAt(value, minStarScale, 1.0f, i, i + 1);
}
private void transformCount(float newValue)
{
int i = 0;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
foreach (var star in stars.Children)
{
star.ClearTransforms(true);
var delay = (countStars <= newValue ? Math.Max(i - countStars, 0) : Math.Max(countStars - 1 - i, 0)) * animationDelay;
star.Delay(delay).FadeTo(i < newValue ? 1.0f : minStarAlpha, fadingDuration);
star.Icon.Delay(delay).ScaleTo(getStarScale(i, newValue), scalingDuration, scalingEasing);
i++;
}
}
private class Star : Container
{
public readonly SpriteIcon Icon;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public Star()
{
Size = new Vector2(star_size);
Child = Icon = new SpriteIcon
{
Size = new Vector2(star_size),
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.Star,
2018-04-13 17:19:50 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
};
}
}
}
}