1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 09:27:29 +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,
Anchor = Anchor.TopRight,
TextSize = 40,
Margin = new MarginPadding(20),
};
Add(score);
@ -30,7 +29,6 @@ namespace osu.Game.Tests.Visual.Gameplay
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
Margin = new MarginPadding(10),
TextSize = 40,
};
Add(comboCounter);

View File

@ -8,6 +8,7 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Tournament.IPC;
using osu.Game.Tournament.Models;
@ -127,21 +128,29 @@ namespace osu.Game.Tournament.Screens.Gameplay.Components
private class MatchScoreCounter : ScoreCounter
{
private OsuSpriteText displayedSpriteText;
public MatchScoreCounter()
{
Margin = new MarginPadding { Top = bar_height, Horizontal = 10 };
Winning = false;
DisplayedCountSpriteText.Spacing = new Vector2(-6);
}
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.Regular, size: 40, fixedWidth: true);
}
}
}
}

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));
}
}

View File

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

View File

@ -3,6 +3,7 @@
using osu.Framework.Graphics;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Screens.Ranking.Expanded.Accuracy;
using osu.Game.Utils;
@ -43,16 +44,16 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
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();
public override void Increment(double 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.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Screens.Ranking.Expanded.Accuracy;
using osuTK;
@ -43,11 +44,11 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
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);
DisplayedCountSpriteText.Spacing = new Vector2(-2, 0);
}
s.Font = OsuFont.Torus.With(size: 20, fixedWidth: true);
s.Spacing = new Vector2(-2, 0);
});
public override void Increment(int amount)
=> Current.Value += amount;

View File

@ -3,6 +3,7 @@
using osu.Framework.Graphics;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Screens.Ranking.Expanded.Accuracy;
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
AutoSizeAxes = Axes.Y;
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 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)
=> Current.Value += amount;
}