Merge pull request #9768 from frenzibyte/allow-counter-customize-sprite-text

Allow providing custom sprite text for RollingCounter<T>
This commit is contained in:
Dean Herbert
2020-08-20 00:14:48 +09:00
committed by GitHub
10 changed files with 71 additions and 61 deletions

View File

@ -3,6 +3,8 @@
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Utils;
namespace osu.Game.Graphics.UserInterface
@ -23,12 +25,11 @@ namespace osu.Game.Graphics.UserInterface
public PercentageCounter()
{
DisplayedCountSpriteText.Font = DisplayedCountSpriteText.Font.With(fixedWidth: true);
Current.Value = DisplayedCount = 1.0f;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours) => AccentColour = colours.BlueLighter;
private void load(OsuColour colours) => Colour = colours.BlueLighter;
protected override string FormatCount(double count) => count.FormatAccuracy();
@ -37,6 +38,9 @@ namespace osu.Game.Graphics.UserInterface
return Math.Abs(currentValue - newValue) * RollingDuration * 100.0f;
}
protected override OsuSpriteText CreateSpriteText()
=> base.CreateSpriteText().With(s => s.Font = s.Font.With(size: 20f, fixedWidth: true));
public override void Increment(double amount)
{
Current.Value += amount;

View File

@ -7,12 +7,12 @@ using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics.Sprites;
using System;
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osuTK.Graphics;
namespace osu.Game.Graphics.UserInterface
{
public abstract class RollingCounter<T> : Container, IHasAccentColour
public abstract class RollingCounter<T> : Container
where T : struct, IEquatable<T>
{
/// <summary>
@ -20,7 +20,7 @@ namespace osu.Game.Graphics.UserInterface
/// </summary>
public Bindable<T> Current = new Bindable<T>();
protected SpriteText DisplayedCountSpriteText;
private SpriteText displayedCountSpriteText;
/// <summary>
/// If true, the roll-up duration will be proportional to change in value.
@ -46,57 +46,39 @@ namespace osu.Game.Graphics.UserInterface
public virtual T DisplayedCount
{
get => displayedCount;
set
{
if (EqualityComparer<T>.Default.Equals(displayedCount, value))
return;
displayedCount = value;
DisplayedCountSpriteText.Text = FormatCount(value);
if (displayedCountSpriteText != null)
displayedCountSpriteText.Text = FormatCount(value);
}
}
public abstract void Increment(T amount);
public float TextSize
{
get => DisplayedCountSpriteText.Font.Size;
set => DisplayedCountSpriteText.Font = DisplayedCountSpriteText.Font.With(size: value);
}
public Color4 AccentColour
{
get => DisplayedCountSpriteText.Colour;
set => DisplayedCountSpriteText.Colour = value;
}
/// <summary>
/// Skeleton of a numeric counter which value rolls over time.
/// </summary>
protected RollingCounter()
{
Children = new Drawable[]
{
DisplayedCountSpriteText = new OsuSpriteText { Font = OsuFont.Numeric }
};
TextSize = 40;
AutoSizeAxes = Axes.Both;
DisplayedCount = Current.Value;
Current.ValueChanged += val =>
{
if (IsLoaded) TransformCount(displayedCount, val.NewValue);
if (IsLoaded)
TransformCount(DisplayedCount, val.NewValue);
};
}
protected override void LoadComplete()
[BackgroundDependencyLoader]
private void load()
{
base.LoadComplete();
DisplayedCountSpriteText.Text = FormatCount(Current.Value);
displayedCountSpriteText = CreateSpriteText();
displayedCountSpriteText.Text = FormatCount(DisplayedCount);
Child = displayedCountSpriteText;
}
/// <summary>
@ -167,5 +149,10 @@ namespace osu.Game.Graphics.UserInterface
this.TransformTo(nameof(DisplayedCount), newValue, rollingTotalDuration, RollingEasing);
}
protected virtual OsuSpriteText CreateSpriteText() => new OsuSpriteText
{
Font = OsuFont.Numeric.With(size: 40f),
};
}
}

View File

@ -3,6 +3,7 @@
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics.UserInterface
{
@ -24,12 +25,11 @@ namespace osu.Game.Graphics.UserInterface
/// <param name="leading">How many leading zeroes the counter will have.</param>
public ScoreCounter(uint leading = 0)
{
DisplayedCountSpriteText.Font = DisplayedCountSpriteText.Font.With(fixedWidth: true);
LeadingZeroes = leading;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours) => AccentColour = colours.BlueLighter;
private void load(OsuColour colours) => Colour = colours.BlueLighter;
protected override double GetProportionalDuration(double currentValue, double newValue)
{
@ -49,6 +49,9 @@ namespace osu.Game.Graphics.UserInterface
return ((long)count).ToString(format);
}
protected override OsuSpriteText CreateSpriteText()
=> base.CreateSpriteText().With(s => s.Font = s.Font.With(fixedWidth: true));
public override void Increment(double amount)
{
Current.Value += amount;

View File

@ -3,6 +3,8 @@
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics.UserInterface
{
@ -19,7 +21,7 @@ namespace osu.Game.Graphics.UserInterface
}
[BackgroundDependencyLoader]
private void load(OsuColour colours) => AccentColour = colours.BlueLighter;
private void load(OsuColour colours) => Colour = colours.BlueLighter;
protected override string FormatCount(int count)
{
@ -35,5 +37,8 @@ namespace osu.Game.Graphics.UserInterface
{
Current.Value += amount;
}
protected override OsuSpriteText CreateSpriteText()
=> base.CreateSpriteText().With(s => s.Font = s.Font.With(size: 20f));
}
}