Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions osu.Game.Tests/Visual/Online/TestSceneLeaderboardScoreDisplay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Testing;
using osu.Game.Graphics;
using osu.Game.Online.Leaderboards;
using osu.Game.Overlays;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Rulesets.UI;
using osuTK;

namespace osu.Game.Tests.Visual.Online
{
public partial class TestSceneLeaderboardScoreDisplay : OsuTestScene
{
private Container content = null!;
protected override Container<Drawable> Content => content;

[Resolved]
private OsuColour colours { get; set; } = null!;

[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Plum);

[SetUpSteps]
public void SetUpSteps()
{
AddStep("create components", () => base.Content.Child = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Width = 200,
Height = 50,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.Gray5,
},
content = new Container
{
RelativeSizeAxes = Axes.Both,
}
}
});
}

[Test]
public void TestBasic()
{
AddStep("create content", () => Child = new LeaderboardScoreDisplay
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Current = { Value = "1,000,000" },
});
}

[Test]
public void TestCustomContent()
{
AddStep("create content", () => Child = new LeaderboardScoreDisplay
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Current = { Value = "1,000,000" },
Child = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(-10, 0),
Padding = new MarginPadding { Top = 4 },
ChildrenEnumerable = new Mod[] { new OsuModDoubleTime(), new OsuModHardRock() }.Select(mod => new ModIcon(mod)
{
Scale = new Vector2(0.3f),
Height = ModIcon.MOD_ICON_SIZE.Y * 3 / 4f,
}),
},
});
}

[Test]
public void TestCustomContent2()
{
AddStep("create content", () => Child = new LeaderboardScoreDisplay
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Current = { Value = "1,000,000" },
Child = new LeaderboardStatistic("Completed Beatmaps".ToUpperInvariant(), "2", false, 0)
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Margin = new MarginPadding { Top = 5, Right = 1.5f },
},
});
}

[Test]
public void TestUpdateScore()
{
LeaderboardScoreDisplay display = null!;
Bindable<string> score = null!;

AddStep("create content", () =>
{
score = new Bindable<string>("1,000,000");

Child = display = new LeaderboardScoreDisplay
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Current = { BindTarget = score },
};
});
AddUntilStep("score text is correct", () => display.ChildrenOfType<IHasText>().Single().Text.ToString(), () => Is.EqualTo("1,000,000"));
AddStep("update score", () => score.Value = "1,234,567");
AddUntilStep("score text is correct", () => display.ChildrenOfType<IHasText>().Single().Text.ToString(), () => Is.EqualTo("1,234,567"));
}
}
}
65 changes: 65 additions & 0 deletions osu.Game/Online/Leaderboards/LeaderboardScoreDisplay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osuTK;

namespace osu.Game.Online.Leaderboards
{
public partial class LeaderboardScoreDisplay : Container, IHasCurrentValue<string>
{
private readonly BindableWithCurrent<string> current = new BindableWithCurrent<string>();

public Bindable<string> Current
{
get => current.Current;
set => current.Current = value;
}

private readonly Container content;
protected override Container<Drawable> Content => content;

private readonly OsuSpriteText scoreText;

public LeaderboardScoreDisplay()
{
AutoSizeAxes = Axes.Both;

InternalChild = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0f, -2f),
Children = new Drawable[]
{
scoreText = new OsuSpriteText
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
UseFullGlyphHeight = false,
Spacing = new Vector2(-1.5f),
Font = OsuFont.Style.Subtitle.With(weight: FontWeight.Light, fixedWidth: true),
},
content = new Container
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
AutoSizeAxes = Axes.Both,
}
}
};
}

protected override void LoadComplete()
{
base.LoadComplete();

Current.BindValueChanged(score => scoreText.Text = score.NewValue, true);
}
}
}
106 changes: 106 additions & 0 deletions osu.Game/Online/Leaderboards/LeaderboardStatistic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Localisation;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Overlays;
using osuTK;
using osuTK.Graphics;

namespace osu.Game.Online.Leaderboards
{
public partial class LeaderboardStatistic : Container
{
public FillDirection Direction
{
get => fillFlowContainer.Direction;
set
{
fillFlowContainer.Direction = value;
updateDirection();
}
}

private readonly bool perfect;

private readonly Container content;
private readonly FillFlowContainer fillFlowContainer;

private readonly OsuSpriteText nameText;
private readonly OsuSpriteText valueText;

public override bool Contains(Vector2 screenSpacePos) => content.Contains(screenSpacePos);

public LeaderboardStatistic(LocalisableString name, LocalisableString value, bool perfect, float? minWidth = null)
{
this.perfect = perfect;

AutoSizeAxes = Axes.Both;
Child = content = new Container
{
AutoSizeAxes = Axes.Both,
Children = new[]
{
fillFlowContainer = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
{
nameText = new OsuSpriteText
{
Text = name,
Font = OsuFont.Style.Caption2.With(weight: FontWeight.SemiBold),
},
valueText = new OsuSpriteText
{
BypassAutoSizeAxes = Axes.X,
Text = value,
Font = OsuFont.Style.Body,
},
}
},
},
};

if (minWidth != null)
Add(Empty().With(d => d.Width = minWidth.Value));
}

[BackgroundDependencyLoader]
private void load(OsuColour colours, OverlayColourProvider colourProvider)
{
nameText.Colour = colourProvider.Content2;
valueText.Colour = perfect ? colours.Lime1 : Color4.White;
}

private void updateDirection()
{
if (Direction == FillDirection.Vertical)
{
// We don't want the value setting the horizontal size, since it leads to wonky accuracy container length,
// since the accuracy is sometimes longer than its name.
Comment on lines +85 to +86
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// We don't want the value setting the horizontal size, since it leads to wonky accuracy container length,
// since the accuracy is sometimes longer than its name.
// We don't want the value setting the horizontal size, since it leads to wonky overall length,
// since the value is sometimes longer than its label.

valueText.BypassAutoSizeAxes = Axes.X;

nameText.Anchor = nameText.Origin = Anchor.TopLeft;
valueText.Anchor = valueText.Origin = Anchor.TopLeft;

fillFlowContainer.Spacing = Vector2.Zero;
}
else
{
// When laid out horizontally, both name and value need to contribute to the horizontal size.
valueText.BypassAutoSizeAxes = Axes.None;

nameText.Anchor = nameText.Origin = Anchor.BottomLeft;
valueText.Anchor = valueText.Origin = Anchor.BottomLeft;

fillFlowContainer.Spacing = new Vector2(5);
}
}
}
}
Loading
Loading