1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 02:02:53 +08:00

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
commit 5714c761eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 71 additions and 61 deletions

View File

@ -20,7 +20,6 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
Origin = Anchor.TopRight, Origin = Anchor.TopRight,
Anchor = Anchor.TopRight, Anchor = Anchor.TopRight,
TextSize = 40,
Margin = new MarginPadding(20), Margin = new MarginPadding(20),
}; };
Add(score); Add(score);
@ -30,7 +29,6 @@ namespace osu.Game.Tests.Visual.Gameplay
Origin = Anchor.BottomLeft, Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft, Anchor = Anchor.BottomLeft,
Margin = new MarginPadding(10), Margin = new MarginPadding(10),
TextSize = 40,
}; };
Add(comboCounter); Add(comboCounter);

View File

@ -8,6 +8,7 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Tournament.IPC; using osu.Game.Tournament.IPC;
using osu.Game.Tournament.Models; using osu.Game.Tournament.Models;
@ -127,21 +128,29 @@ namespace osu.Game.Tournament.Screens.Gameplay.Components
private class MatchScoreCounter : ScoreCounter private class MatchScoreCounter : ScoreCounter
{ {
private OsuSpriteText displayedSpriteText;
public MatchScoreCounter() public MatchScoreCounter()
{ {
Margin = new MarginPadding { Top = bar_height, Horizontal = 10 }; Margin = new MarginPadding { Top = bar_height, Horizontal = 10 };
Winning = false;
DisplayedCountSpriteText.Spacing = new Vector2(-6);
} }
public bool Winning public bool Winning
{ {
set => DisplayedCountSpriteText.Font = value set => updateFont(value);
}
protected override OsuSpriteText CreateSpriteText() => base.CreateSpriteText().With(s =>
{
displayedSpriteText = s;
displayedSpriteText.Spacing = new Vector2(-6);
updateFont(false);
});
private void updateFont(bool winning)
=> displayedSpriteText.Font = winning
? OsuFont.Torus.With(weight: FontWeight.Bold, size: 50, fixedWidth: true) ? OsuFont.Torus.With(weight: FontWeight.Bold, size: 50, fixedWidth: true)
: OsuFont.Torus.With(weight: FontWeight.Regular, size: 40, fixedWidth: true); : OsuFont.Torus.With(weight: FontWeight.Regular, size: 40, fixedWidth: true);
}
} }
} }
} }

View File

@ -3,6 +3,8 @@
using System; using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Utils; using osu.Game.Utils;
namespace osu.Game.Graphics.UserInterface namespace osu.Game.Graphics.UserInterface
@ -23,12 +25,11 @@ namespace osu.Game.Graphics.UserInterface
public PercentageCounter() public PercentageCounter()
{ {
DisplayedCountSpriteText.Font = DisplayedCountSpriteText.Font.With(fixedWidth: true);
Current.Value = DisplayedCount = 1.0f; Current.Value = DisplayedCount = 1.0f;
} }
[BackgroundDependencyLoader] [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(); 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; 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) public override void Increment(double amount)
{ {
Current.Value += amount; Current.Value += amount;

View File

@ -7,12 +7,12 @@ using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osuTK.Graphics;
namespace osu.Game.Graphics.UserInterface namespace osu.Game.Graphics.UserInterface
{ {
public abstract class RollingCounter<T> : Container, IHasAccentColour public abstract class RollingCounter<T> : Container
where T : struct, IEquatable<T> where T : struct, IEquatable<T>
{ {
/// <summary> /// <summary>
@ -20,7 +20,7 @@ namespace osu.Game.Graphics.UserInterface
/// </summary> /// </summary>
public Bindable<T> Current = new Bindable<T>(); public Bindable<T> Current = new Bindable<T>();
protected SpriteText DisplayedCountSpriteText; private SpriteText displayedCountSpriteText;
/// <summary> /// <summary>
/// If true, the roll-up duration will be proportional to change in value. /// 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 public virtual T DisplayedCount
{ {
get => displayedCount; get => displayedCount;
set set
{ {
if (EqualityComparer<T>.Default.Equals(displayedCount, value)) if (EqualityComparer<T>.Default.Equals(displayedCount, value))
return; return;
displayedCount = value; displayedCount = value;
DisplayedCountSpriteText.Text = FormatCount(value); if (displayedCountSpriteText != null)
displayedCountSpriteText.Text = FormatCount(value);
} }
} }
public abstract void Increment(T amount); 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> /// <summary>
/// Skeleton of a numeric counter which value rolls over time. /// Skeleton of a numeric counter which value rolls over time.
/// </summary> /// </summary>
protected RollingCounter() protected RollingCounter()
{ {
Children = new Drawable[]
{
DisplayedCountSpriteText = new OsuSpriteText { Font = OsuFont.Numeric }
};
TextSize = 40;
AutoSizeAxes = Axes.Both; AutoSizeAxes = Axes.Both;
DisplayedCount = Current.Value;
Current.ValueChanged += val => 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 = CreateSpriteText();
displayedCountSpriteText.Text = FormatCount(DisplayedCount);
DisplayedCountSpriteText.Text = FormatCount(Current.Value); Child = displayedCountSpriteText;
} }
/// <summary> /// <summary>
@ -167,5 +149,10 @@ namespace osu.Game.Graphics.UserInterface
this.TransformTo(nameof(DisplayedCount), newValue, rollingTotalDuration, RollingEasing); 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.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics.UserInterface 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> /// <param name="leading">How many leading zeroes the counter will have.</param>
public ScoreCounter(uint leading = 0) public ScoreCounter(uint leading = 0)
{ {
DisplayedCountSpriteText.Font = DisplayedCountSpriteText.Font.With(fixedWidth: true);
LeadingZeroes = leading; LeadingZeroes = leading;
} }
[BackgroundDependencyLoader] [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) protected override double GetProportionalDuration(double currentValue, double newValue)
{ {
@ -49,6 +49,9 @@ namespace osu.Game.Graphics.UserInterface
return ((long)count).ToString(format); 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) public override void Increment(double amount)
{ {
Current.Value += amount; Current.Value += amount;

View File

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

View File

@ -232,7 +232,6 @@ namespace osu.Game.Screens.Play
protected virtual RollingCounter<double> CreateAccuracyCounter() => new PercentageCounter protected virtual RollingCounter<double> CreateAccuracyCounter() => new PercentageCounter
{ {
TextSize = 20,
BypassAutoSizeAxes = Axes.X, BypassAutoSizeAxes = Axes.X,
Anchor = Anchor.TopLeft, Anchor = Anchor.TopLeft,
Origin = Anchor.TopRight, Origin = Anchor.TopRight,
@ -241,14 +240,12 @@ namespace osu.Game.Screens.Play
protected virtual ScoreCounter CreateScoreCounter() => new ScoreCounter(6) protected virtual ScoreCounter CreateScoreCounter() => new ScoreCounter(6)
{ {
TextSize = 40,
Anchor = Anchor.TopCentre, Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre, Origin = Anchor.TopCentre,
}; };
protected virtual RollingCounter<int> CreateComboCounter() => new SimpleComboCounter protected virtual RollingCounter<int> CreateComboCounter() => new SimpleComboCounter
{ {
TextSize = 20,
BypassAutoSizeAxes = Axes.X, BypassAutoSizeAxes = Axes.X,
Anchor = Anchor.TopRight, Anchor = Anchor.TopRight,
Origin = Anchor.TopLeft, Origin = Anchor.TopLeft,

View File

@ -3,6 +3,7 @@
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Screens.Ranking.Expanded.Accuracy; using osu.Game.Screens.Ranking.Expanded.Accuracy;
using osu.Game.Utils; using osu.Game.Utils;
@ -43,16 +44,16 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
protected override Easing RollingEasing => AccuracyCircle.ACCURACY_TRANSFORM_EASING; protected override Easing RollingEasing => AccuracyCircle.ACCURACY_TRANSFORM_EASING;
public Counter()
{
DisplayedCountSpriteText.Font = OsuFont.Torus.With(size: 20, fixedWidth: true);
DisplayedCountSpriteText.Spacing = new Vector2(-2, 0);
}
protected override string FormatCount(double count) => count.FormatAccuracy(); protected override string FormatCount(double count) => count.FormatAccuracy();
public override void Increment(double amount) public override void Increment(double amount)
=> Current.Value += amount; => Current.Value += amount;
protected override OsuSpriteText CreateSpriteText() => base.CreateSpriteText().With(s =>
{
s.Font = OsuFont.Torus.With(size: 20, fixedWidth: true);
s.Spacing = new Vector2(-2, 0);
});
} }
} }
} }

View File

@ -3,6 +3,7 @@
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Screens.Ranking.Expanded.Accuracy; using osu.Game.Screens.Ranking.Expanded.Accuracy;
using osuTK; using osuTK;
@ -43,11 +44,11 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
protected override Easing RollingEasing => AccuracyCircle.ACCURACY_TRANSFORM_EASING; protected override Easing RollingEasing => AccuracyCircle.ACCURACY_TRANSFORM_EASING;
public Counter() protected override OsuSpriteText CreateSpriteText() => base.CreateSpriteText().With(s =>
{ {
DisplayedCountSpriteText.Font = OsuFont.Torus.With(size: 20, fixedWidth: true); s.Font = OsuFont.Torus.With(size: 20, fixedWidth: true);
DisplayedCountSpriteText.Spacing = new Vector2(-2, 0); s.Spacing = new Vector2(-2, 0);
} });
public override void Increment(int amount) public override void Increment(int amount)
=> Current.Value += amount; => Current.Value += amount;

View File

@ -3,6 +3,7 @@
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Screens.Ranking.Expanded.Accuracy; using osu.Game.Screens.Ranking.Expanded.Accuracy;
using osuTK; using osuTK;
@ -23,15 +24,19 @@ namespace osu.Game.Screens.Ranking.Expanded
// Todo: AutoSize X removed here due to https://github.com/ppy/osu-framework/issues/3369 // Todo: AutoSize X removed here due to https://github.com/ppy/osu-framework/issues/3369
AutoSizeAxes = Axes.Y; AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X; RelativeSizeAxes = Axes.X;
DisplayedCountSpriteText.Anchor = Anchor.TopCentre;
DisplayedCountSpriteText.Origin = Anchor.TopCentre;
DisplayedCountSpriteText.Font = OsuFont.Torus.With(size: 60, weight: FontWeight.Light, fixedWidth: true);
DisplayedCountSpriteText.Spacing = new Vector2(-5, 0);
} }
protected override string FormatCount(long count) => count.ToString("N0"); protected override string FormatCount(long count) => count.ToString("N0");
protected override OsuSpriteText CreateSpriteText() => base.CreateSpriteText().With(s =>
{
s.Anchor = Anchor.TopCentre;
s.Origin = Anchor.TopCentre;
s.Font = OsuFont.Torus.With(size: 60, weight: FontWeight.Light, fixedWidth: true);
s.Spacing = new Vector2(-5, 0);
});
public override void Increment(long amount) public override void Increment(long amount)
=> Current.Value += amount; => Current.Value += amount;
} }