mirror of
https://github.com/osukey/osukey.git
synced 2025-08-03 14:46:38 +09:00
62
osu.Game/Graphics/UserInterface/PercentageCounter.cs
Normal file
62
osu.Game/Graphics/UserInterface/PercentageCounter.cs
Normal file
@ -0,0 +1,62 @@
|
||||
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
|
||||
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using osu.Framework;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Transformations;
|
||||
using osu.Framework.Timing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
/// <summary>
|
||||
/// Used as an accuracy counter. Represented visually as a percentage.
|
||||
/// </summary>
|
||||
public class PercentageCounter : RollingCounter<float>
|
||||
{
|
||||
protected override Type TransformType => typeof(TransformAccuracy);
|
||||
|
||||
protected override double RollingDuration => 20;
|
||||
protected override bool IsRollingProportional => true;
|
||||
|
||||
private float epsilon => 1e-10f;
|
||||
|
||||
public void SetFraction(float numerator, float denominator)
|
||||
{
|
||||
Count = Math.Abs(denominator) < epsilon ? 1.0f : numerator / denominator;
|
||||
}
|
||||
|
||||
public PercentageCounter()
|
||||
{
|
||||
Count = 1.0f;
|
||||
}
|
||||
|
||||
protected override string FormatCount(float count)
|
||||
{
|
||||
return $@"{count:P2}";
|
||||
}
|
||||
|
||||
protected override double GetProportionalDuration(float currentValue, float newValue)
|
||||
{
|
||||
return Math.Abs(currentValue - newValue) * RollingDuration * 100.0f;
|
||||
}
|
||||
|
||||
protected class TransformAccuracy : TransformFloat
|
||||
{
|
||||
public override void Apply(Drawable d)
|
||||
{
|
||||
base.Apply(d);
|
||||
(d as PercentageCounter).DisplayedCount = CurrentValue;
|
||||
}
|
||||
|
||||
public TransformAccuracy(IClock clock)
|
||||
: base(clock)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
229
osu.Game/Graphics/UserInterface/RollingCounter.cs
Normal file
229
osu.Game/Graphics/UserInterface/RollingCounter.cs
Normal file
@ -0,0 +1,229 @@
|
||||
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
|
||||
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using osu.Framework;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Framework.Graphics.Transformations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
public abstract class RollingCounter<T> : AutoSizeContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// Type of the Transform to use.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Must be a subclass of Transform<T>
|
||||
/// </remarks>
|
||||
protected virtual Type TransformType => typeof(Transform<T>);
|
||||
|
||||
protected SpriteText DisplayedCountSpriteText;
|
||||
|
||||
/// <summary>
|
||||
/// If true, the roll-up duration will be proportional to change in value.
|
||||
/// </summary>
|
||||
protected virtual bool IsRollingProportional => false;
|
||||
|
||||
/// <summary>
|
||||
/// If IsRollingProportional = false, duration in milliseconds for the counter roll-up animation for each
|
||||
/// element; else duration in milliseconds for the counter roll-up animation in total.
|
||||
/// </summary>
|
||||
protected virtual double RollingDuration => 0;
|
||||
|
||||
/// <summary>
|
||||
/// Easing for the counter rollover animation.
|
||||
/// </summary>
|
||||
protected virtual EasingTypes RollingEasing => EasingTypes.None;
|
||||
|
||||
private T displayedCount;
|
||||
|
||||
/// <summary>
|
||||
/// Value shown at the current moment.
|
||||
/// </summary>
|
||||
public virtual T DisplayedCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return displayedCount;
|
||||
}
|
||||
protected set
|
||||
{
|
||||
if (displayedCount.Equals(value))
|
||||
return;
|
||||
displayedCount = value;
|
||||
DisplayedCountSpriteText.Text = FormatCount(value);
|
||||
}
|
||||
}
|
||||
|
||||
protected T prevCount;
|
||||
protected T count;
|
||||
|
||||
/// <summary>
|
||||
/// Actual value of counter.
|
||||
/// </summary>
|
||||
public virtual T Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return count;
|
||||
}
|
||||
set
|
||||
{
|
||||
prevCount = count;
|
||||
count = value;
|
||||
if (IsLoaded)
|
||||
{
|
||||
TransformCount(displayedCount, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected float textSize = 20.0f;
|
||||
|
||||
public float TextSize
|
||||
{
|
||||
get { return textSize; }
|
||||
set
|
||||
{
|
||||
textSize = value;
|
||||
DisplayedCountSpriteText.TextSize = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skeleton of a numeric counter which value rolls over time.
|
||||
/// </summary>
|
||||
protected RollingCounter()
|
||||
{
|
||||
Children = new Drawable[]
|
||||
{
|
||||
DisplayedCountSpriteText = new SpriteText(),
|
||||
};
|
||||
}
|
||||
|
||||
public override void Load(BaseGame game)
|
||||
{
|
||||
base.Load(game);
|
||||
|
||||
Flush(false, TransformType);
|
||||
|
||||
DisplayedCount = Count;
|
||||
|
||||
DisplayedCountSpriteText.Text = FormatCount(count);
|
||||
DisplayedCountSpriteText.Anchor = this.Anchor;
|
||||
DisplayedCountSpriteText.Origin = this.Origin;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets count value, bypassing rollover animation.
|
||||
/// </summary>
|
||||
/// <param name="count">New count value.</param>
|
||||
public virtual void SetCountWithoutRolling(T count)
|
||||
{
|
||||
Count = count;
|
||||
StopRolling();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops rollover animation, forcing the displayed count to be the actual count.
|
||||
/// </summary>
|
||||
public virtual void StopRolling()
|
||||
{
|
||||
Flush(false, TransformType);
|
||||
DisplayedCount = Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets count to default value.
|
||||
/// </summary>
|
||||
public virtual void ResetCount()
|
||||
{
|
||||
SetCountWithoutRolling(default(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the duration of the roll-up animation by using the difference between the current visible value
|
||||
/// and the new final value.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// To be used in conjunction with IsRollingProportional = true.
|
||||
/// Unless a derived class needs to have a proportional rolling, it is not necessary to override this function.
|
||||
/// </remarks>
|
||||
/// <param name="currentValue">Current visible value.</param>
|
||||
/// <param name="newValue">New final value.</param>
|
||||
/// <returns>Calculated rollover duration in milliseconds.</returns>
|
||||
protected virtual double GetProportionalDuration(T currentValue, T newValue)
|
||||
{
|
||||
return RollingDuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to format counts.
|
||||
/// </summary>
|
||||
/// <param name="count">Count to format.</param>
|
||||
/// <returns>Count formatted as a string.</returns>
|
||||
protected virtual string FormatCount(T count)
|
||||
{
|
||||
return count.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the count is updated to add a transformer that changes the value of the visible count (i.e.
|
||||
/// implement the rollover animation).
|
||||
/// </summary>
|
||||
/// <param name="currentValue">Count value before modification.</param>
|
||||
/// <param name="newValue">Expected count value after modification-</param>
|
||||
/// <seealso cref="TransformType"/>
|
||||
protected virtual void TransformCount(T currentValue, T newValue)
|
||||
{
|
||||
object[] parameters = { Clock };
|
||||
|
||||
Debug.Assert(
|
||||
TransformType.IsSubclassOf(typeof(Transform<T>)) || TransformType == typeof(Transform<T>),
|
||||
@"transformType should be a subclass of Transform<T>."
|
||||
);
|
||||
|
||||
TransformCount((Transform<T>)Activator.CreateInstance(TransformType, parameters), currentValue, newValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Intended to be used by TransformCount(T currentValue, T newValue).
|
||||
/// </summary>
|
||||
protected void TransformCount(Transform<T> transform, T currentValue, T newValue)
|
||||
{
|
||||
Type type = transform.GetType();
|
||||
|
||||
Flush(false, type);
|
||||
|
||||
if (Clock == null)
|
||||
return;
|
||||
|
||||
if (RollingDuration < 1)
|
||||
{
|
||||
DisplayedCount = Count;
|
||||
return;
|
||||
}
|
||||
|
||||
double rollingTotalDuration =
|
||||
IsRollingProportional
|
||||
? GetProportionalDuration(currentValue, newValue)
|
||||
: RollingDuration;
|
||||
|
||||
transform.StartTime = Time;
|
||||
transform.EndTime = Time + rollingTotalDuration;
|
||||
transform.StartValue = currentValue;
|
||||
transform.EndValue = newValue;
|
||||
transform.Easing = RollingEasing;
|
||||
|
||||
Transforms.Add(transform);
|
||||
}
|
||||
}
|
||||
}
|
78
osu.Game/Graphics/UserInterface/ScoreCounter.cs
Normal file
78
osu.Game/Graphics/UserInterface/ScoreCounter.cs
Normal file
@ -0,0 +1,78 @@
|
||||
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
|
||||
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Transformations;
|
||||
using osu.Framework.MathUtils;
|
||||
using osu.Framework.Timing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
public class ScoreCounter : RollingCounter<ulong>
|
||||
{
|
||||
protected override Type TransformType => typeof(TransformScore);
|
||||
|
||||
protected override double RollingDuration => 1000;
|
||||
protected override EasingTypes RollingEasing => EasingTypes.Out;
|
||||
|
||||
/// <summary>
|
||||
/// How many leading zeroes the counter has.
|
||||
/// </summary>
|
||||
public uint LeadingZeroes
|
||||
{
|
||||
get;
|
||||
protected set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays score.
|
||||
/// </summary>
|
||||
/// <param name="leading">How many leading zeroes the counter will have.</param>
|
||||
public ScoreCounter(uint leading = 0)
|
||||
{
|
||||
DisplayedCountSpriteText.FixedWidth = true;
|
||||
LeadingZeroes = leading;
|
||||
}
|
||||
|
||||
protected override double GetProportionalDuration(ulong currentValue, ulong newValue)
|
||||
{
|
||||
return currentValue > newValue ? currentValue - newValue : newValue - currentValue;
|
||||
}
|
||||
|
||||
protected override string FormatCount(ulong count)
|
||||
{
|
||||
return count.ToString("D" + LeadingZeroes);
|
||||
}
|
||||
|
||||
protected class TransformScore : Transform<ulong>
|
||||
{
|
||||
public override ulong CurrentValue
|
||||
{
|
||||
get
|
||||
{
|
||||
double time = Time;
|
||||
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);
|
||||
(d as ScoreCounter).DisplayedCount = CurrentValue;
|
||||
}
|
||||
|
||||
public TransformScore(IClock clock)
|
||||
: base(clock)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
188
osu.Game/Graphics/UserInterface/StarCounter.cs
Normal file
188
osu.Game/Graphics/UserInterface/StarCounter.cs
Normal file
@ -0,0 +1,188 @@
|
||||
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
|
||||
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using OpenTK;
|
||||
using osu.Framework;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Transformations;
|
||||
using osu.Framework.MathUtils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
public class StarCounter : AutoSizeContainer
|
||||
{
|
||||
private readonly Container starContainer;
|
||||
private readonly List<TextAwesome> stars = new List<TextAwesome>();
|
||||
|
||||
private double transformStartTime = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum amount of stars displayed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This does not limit the counter value, but the amount of stars displayed.
|
||||
/// </remarks>
|
||||
public int MaxStars
|
||||
{
|
||||
get;
|
||||
protected set;
|
||||
}
|
||||
|
||||
private double animationDelay => 150;
|
||||
|
||||
private double scalingDuration => 500;
|
||||
private EasingTypes scalingEasing => EasingTypes.OutElasticHalf;
|
||||
private float minStarScale => 0.3f;
|
||||
|
||||
private double fadingDuration => 100;
|
||||
private float minStarAlpha => 0.5f;
|
||||
|
||||
public float StarSize = 20;
|
||||
public float StarSpacing = 4;
|
||||
|
||||
public float VisibleValue
|
||||
{
|
||||
get
|
||||
{
|
||||
double elapsedTime = Time - transformStartTime;
|
||||
double expectedElapsedTime = Math.Abs(prevCount - count) * animationDelay;
|
||||
if (elapsedTime >= expectedElapsedTime)
|
||||
return count;
|
||||
return Interpolation.ValueAt(elapsedTime, prevCount, count, 0, expectedElapsedTime);
|
||||
}
|
||||
}
|
||||
|
||||
private float prevCount;
|
||||
private float count;
|
||||
|
||||
/// <summary>
|
||||
/// Amount of stars represented.
|
||||
/// </summary>
|
||||
public float Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return count;
|
||||
}
|
||||
set
|
||||
{
|
||||
prevCount = VisibleValue;
|
||||
count = value;
|
||||
if (IsLoaded)
|
||||
{
|
||||
transformCount(prevCount, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows a float count as stars (up to 10). Used as star difficulty display.
|
||||
/// </summary>
|
||||
public StarCounter() : this(10) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows a float count as stars. Used as star difficulty display.
|
||||
/// </summary>
|
||||
/// <param name="maxstars">Maximum amount of stars to display.</param>
|
||||
public StarCounter(int maxstars)
|
||||
{
|
||||
MaxStars = Math.Max(maxstars, 0);
|
||||
|
||||
Children = new Drawable[]
|
||||
{
|
||||
starContainer = new Container
|
||||
{
|
||||
Anchor = Anchor.CentreLeft,
|
||||
Origin = Anchor.CentreLeft,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public override void Load(BaseGame game)
|
||||
{
|
||||
base.Load(game);
|
||||
|
||||
starContainer.Width = MaxStars * StarSize + Math.Max(MaxStars - 1, 0) * StarSpacing;
|
||||
starContainer.Height = StarSize;
|
||||
|
||||
for (int i = 0; i < MaxStars; i++)
|
||||
{
|
||||
TextAwesome star = new TextAwesome
|
||||
{
|
||||
Icon = FontAwesome.star,
|
||||
Anchor = Anchor.CentreLeft,
|
||||
Origin = Anchor.Centre,
|
||||
TextSize = StarSize,
|
||||
Scale = new Vector2(minStarScale),
|
||||
Alpha = minStarAlpha,
|
||||
Position = new Vector2((StarSize + StarSpacing) * i + (StarSize + StarSpacing) / 2, 0),
|
||||
};
|
||||
|
||||
//todo: user Container<T> once we have it.
|
||||
stars.Add(star);
|
||||
starContainer.Add(star);
|
||||
}
|
||||
|
||||
// Animate initial state from zero.
|
||||
transformCount(0, Count);
|
||||
}
|
||||
|
||||
public void ResetCount()
|
||||
{
|
||||
Count = 0;
|
||||
StopAnimation();
|
||||
}
|
||||
|
||||
public void StopAnimation()
|
||||
{
|
||||
prevCount = count;
|
||||
transformStartTime = Time;
|
||||
|
||||
for (int i = 0; i < MaxStars; i++)
|
||||
transformStarQuick(i, count);
|
||||
}
|
||||
|
||||
private float getStarScale(int i, float value)
|
||||
{
|
||||
if (value <= i)
|
||||
return minStarScale;
|
||||
if (i + 1 <= value)
|
||||
return 1.0f;
|
||||
return Interpolation.ValueAt(value, minStarScale, 1.0f, i, i + 1);
|
||||
}
|
||||
|
||||
private void transformStar(int i, float value)
|
||||
{
|
||||
stars[i].FadeTo(i < value ? 1.0f : minStarAlpha, fadingDuration);
|
||||
stars[i].ScaleTo(getStarScale(i, value), scalingDuration, scalingEasing);
|
||||
}
|
||||
|
||||
private void transformStarQuick(int i, float value)
|
||||
{
|
||||
stars[i].FadeTo(i < value ? 1.0f : minStarAlpha);
|
||||
stars[i].ScaleTo(getStarScale(i, value));
|
||||
}
|
||||
|
||||
private void transformCount(float currentValue, float newValue)
|
||||
{
|
||||
for (int i = 0; i < MaxStars; i++)
|
||||
{
|
||||
stars[i].ClearTransformations();
|
||||
if (currentValue <= newValue)
|
||||
stars[i].Delay(Math.Max(i - currentValue, 0) * animationDelay);
|
||||
else
|
||||
stars[i].Delay(Math.Max(currentValue - 1 - i, 0) * animationDelay);
|
||||
transformStar(i, newValue);
|
||||
stars[i].DelayReset();
|
||||
}
|
||||
transformStartTime = Time;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user