2017-02-07 04:59:30 +00:00
|
|
|
|
// 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-09 00:11:01 +00:00
|
|
|
|
|
2016-10-09 02:45:01 +00:00
|
|
|
|
using OpenTK;
|
2016-10-09 00:11:01 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-02-25 12:12:39 +00:00
|
|
|
|
using osu.Framework.Graphics.Transforms;
|
2016-10-09 00:11:01 +00:00
|
|
|
|
using osu.Framework.MathUtils;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
2016-10-22 08:50:42 +00:00
|
|
|
|
public class StarCounter : Container
|
2016-10-09 00:11:01 +00:00
|
|
|
|
{
|
2017-02-24 10:30:04 +00:00
|
|
|
|
private readonly Container<Star> stars;
|
2016-10-09 00:11:01 +00:00
|
|
|
|
|
2016-10-13 02:36:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Maximum amount of stars displayed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This does not limit the counter value, but the amount of stars displayed.
|
|
|
|
|
/// </remarks>
|
2017-02-24 10:30:04 +00:00
|
|
|
|
public int StarCount { get; }
|
2016-10-13 01:51:50 +00:00
|
|
|
|
|
2016-12-15 13:51:35 +00:00
|
|
|
|
private double animationDelay => 80;
|
2016-10-13 22:13:20 +00:00
|
|
|
|
|
2017-02-24 10:30:04 +00:00
|
|
|
|
private double scalingDuration => 1000;
|
2016-10-14 23:23:27 +00:00
|
|
|
|
private EasingTypes scalingEasing => EasingTypes.OutElasticHalf;
|
2017-02-24 10:30:04 +00:00
|
|
|
|
private float minStarScale => 0.4f;
|
2016-10-13 22:13:20 +00:00
|
|
|
|
|
2016-10-14 23:23:27 +00:00
|
|
|
|
private double fadingDuration => 100;
|
|
|
|
|
private float minStarAlpha => 0.5f;
|
2016-10-13 22:13:20 +00:00
|
|
|
|
|
2017-02-24 10:30:04 +00:00
|
|
|
|
private const float star_size = 20;
|
2017-03-06 09:49:23 +00:00
|
|
|
|
private const float star_spacing = 4;
|
2016-10-13 22:13:20 +00:00
|
|
|
|
|
|
|
|
|
private float count;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Amount of stars represented.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float Count
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2016-11-06 10:13:52 +00:00
|
|
|
|
|
2016-10-13 22:13:20 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
2017-02-24 10:30:04 +00:00
|
|
|
|
if (count == value) return;
|
2016-11-06 10:13:52 +00:00
|
|
|
|
|
2017-02-24 10:30:04 +00:00
|
|
|
|
if (IsLoaded)
|
|
|
|
|
transformCount(value);
|
2016-11-06 10:13:52 +00:00
|
|
|
|
count = value;
|
2016-10-13 22:13:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-09 02:45:01 +00:00
|
|
|
|
|
2016-10-13 02:36:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Shows a float count as stars. Used as star difficulty display.
|
|
|
|
|
/// </summary>
|
2017-02-24 10:30:04 +00:00
|
|
|
|
/// <param name="starCount">Maximum amount of stars to display.</param>
|
|
|
|
|
public StarCounter(int starCount = 10)
|
2016-10-09 00:11:01 +00:00
|
|
|
|
{
|
2017-02-24 10:30:04 +00:00
|
|
|
|
StarCount = Math.Max(starCount, 0);
|
|
|
|
|
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
2016-10-13 01:51:50 +00:00
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-03-01 18:33:01 +00:00
|
|
|
|
stars = new FillFlowContainer<Star>
|
2016-10-13 01:51:50 +00:00
|
|
|
|
{
|
2017-02-24 10:30:04 +00:00
|
|
|
|
AutoSizeAxes = Axes.Both,
|
2017-03-04 10:00:17 +00:00
|
|
|
|
Direction = FillDirection.Horizontal,
|
2017-03-01 18:33:01 +00:00
|
|
|
|
Spacing = new Vector2(star_spacing),
|
2016-10-13 01:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2016-10-09 02:45:01 +00:00
|
|
|
|
|
2017-02-24 10:30:04 +00:00
|
|
|
|
for (int i = 0; i < StarCount; i++)
|
2016-10-09 00:11:01 +00:00
|
|
|
|
{
|
2017-02-24 10:30:04 +00:00
|
|
|
|
stars.Add(new Star
|
2016-10-09 00:11:01 +00:00
|
|
|
|
{
|
2016-10-16 23:30:25 +00:00
|
|
|
|
Alpha = minStarAlpha,
|
2017-02-24 10:30:04 +00:00
|
|
|
|
});
|
2016-10-09 00:11:01 +00:00
|
|
|
|
}
|
2016-11-01 14:24:14 +00:00
|
|
|
|
}
|
2016-10-09 00:11:01 +00:00
|
|
|
|
|
2016-11-01 14:24:14 +00:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2017-02-24 10:30:04 +00:00
|
|
|
|
|
2016-10-16 23:30:25 +00:00
|
|
|
|
// Animate initial state from zero.
|
2017-02-24 10:30:04 +00:00
|
|
|
|
ReplayAnimation();
|
2016-10-09 00:11:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 22:13:20 +00:00
|
|
|
|
public void ResetCount()
|
2016-10-09 19:02:44 +00:00
|
|
|
|
{
|
2017-02-24 10:30:04 +00:00
|
|
|
|
count = 0;
|
2016-10-13 22:13:20 +00:00
|
|
|
|
StopAnimation();
|
2016-10-09 19:02:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-24 10:30:04 +00:00
|
|
|
|
public void ReplayAnimation()
|
2016-10-09 19:02:44 +00:00
|
|
|
|
{
|
2017-02-24 10:30:04 +00:00
|
|
|
|
var t = count;
|
|
|
|
|
ResetCount();
|
|
|
|
|
Count = t;
|
|
|
|
|
}
|
2016-10-09 19:02:44 +00:00
|
|
|
|
|
2017-02-24 10:30:04 +00:00
|
|
|
|
public void StopAnimation()
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
foreach (var star in stars.Children)
|
|
|
|
|
{
|
2017-02-25 12:12:39 +00:00
|
|
|
|
star.ClearTransforms(true);
|
2017-02-24 10:30:04 +00:00
|
|
|
|
star.FadeTo(i < count ? 1.0f : minStarAlpha);
|
|
|
|
|
star.Icon.ScaleTo(getStarScale(i, count));
|
|
|
|
|
i++;
|
|
|
|
|
}
|
2016-10-09 19:02:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 22:13:20 +00:00
|
|
|
|
private float getStarScale(int i, float value)
|
2016-10-09 19:02:44 +00:00
|
|
|
|
{
|
2016-10-13 22:13:20 +00:00
|
|
|
|
if (value <= i)
|
2016-10-14 23:23:27 +00:00
|
|
|
|
return minStarScale;
|
2016-10-09 19:02:44 +00:00
|
|
|
|
|
2017-03-10 05:09:07 +00:00
|
|
|
|
return i + 1 <= value ? 1.0f : (float)Interpolation.ValueAt(value, minStarScale, 1.0f, i, i + 1);
|
2016-10-09 19:02:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-24 10:30:04 +00:00
|
|
|
|
private void transformCount(float newValue)
|
2016-10-09 00:11:01 +00:00
|
|
|
|
{
|
2017-02-24 10:30:04 +00:00
|
|
|
|
int i = 0;
|
|
|
|
|
foreach (var star in stars.Children)
|
|
|
|
|
{
|
2017-02-25 12:12:39 +00:00
|
|
|
|
star.ClearTransforms(true);
|
2017-02-24 10:30:04 +00:00
|
|
|
|
if (count <= newValue)
|
|
|
|
|
star.Delay(Math.Max(i - count, 0) * animationDelay, true);
|
|
|
|
|
else
|
|
|
|
|
star.Delay(Math.Max(count - 1 - i, 0) * animationDelay, true);
|
|
|
|
|
|
|
|
|
|
star.FadeTo(i < newValue ? 1.0f : minStarAlpha, fadingDuration);
|
|
|
|
|
star.Icon.ScaleTo(getStarScale(i, newValue), scalingDuration, scalingEasing);
|
|
|
|
|
star.DelayReset();
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
}
|
2016-10-09 00:11:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 01:59:19 +00:00
|
|
|
|
private class Star : Container
|
2016-10-09 00:11:01 +00:00
|
|
|
|
{
|
2017-02-24 10:30:04 +00:00
|
|
|
|
public TextAwesome Icon;
|
|
|
|
|
public Star()
|
2016-10-09 00:11:01 +00:00
|
|
|
|
{
|
2017-02-24 10:30:04 +00:00
|
|
|
|
Size = new Vector2(star_size);
|
|
|
|
|
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
|
|
|
|
Icon = new TextAwesome
|
|
|
|
|
{
|
|
|
|
|
TextSize = star_size,
|
|
|
|
|
Icon = FontAwesome.fa_star,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-10-09 00:11:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|