mirror of
https://github.com/osukey/osukey.git
synced 2025-08-04 23:24:04 +09:00
Add component for displaying simple statistics on result screen
This commit is contained in:
80
osu.Game/Screens/Ranking/Statistics/SimpleStatisticItem.cs
Normal file
80
osu.Game/Screens/Ranking/Statistics/SimpleStatisticItem.cs
Normal file
@ -0,0 +1,80 @@
|
||||
// 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.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
|
||||
namespace osu.Game.Screens.Ranking.Statistics
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a simple statistic item (one that only needs textual display).
|
||||
/// Richer visualisations should be done with <see cref="StatisticItem"/>s.
|
||||
/// </summary>
|
||||
public abstract class SimpleStatisticItem : Container
|
||||
{
|
||||
/// <summary>
|
||||
/// The text to display as the statistic's value.
|
||||
/// </summary>
|
||||
protected string Value
|
||||
{
|
||||
set => this.value.Text = value;
|
||||
}
|
||||
|
||||
private readonly OsuSpriteText value;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new simple statistic item.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the statistic.</param>
|
||||
protected SimpleStatisticItem(string name)
|
||||
{
|
||||
Name = name;
|
||||
|
||||
RelativeSizeAxes = Axes.X;
|
||||
AutoSizeAxes = Axes.Y;
|
||||
|
||||
AddRange(new[]
|
||||
{
|
||||
new OsuSpriteText
|
||||
{
|
||||
Text = Name,
|
||||
Anchor = Anchor.CentreLeft,
|
||||
Origin = Anchor.CentreLeft
|
||||
},
|
||||
value = new OsuSpriteText
|
||||
{
|
||||
Anchor = Anchor.CentreRight,
|
||||
Origin = Anchor.CentreRight,
|
||||
Font = OsuFont.Torus.With(weight: FontWeight.Bold)
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Strongly-typed generic specialisation for <see cref="SimpleStatisticItem"/>.
|
||||
/// </summary>
|
||||
public class SimpleStatisticItem<TValue> : SimpleStatisticItem
|
||||
{
|
||||
/// <summary>
|
||||
/// The statistic's value to be displayed.
|
||||
/// </summary>
|
||||
public new TValue Value
|
||||
{
|
||||
set => base.Value = DisplayValue(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to convert <see cref="Value"/> to a text representation.
|
||||
/// Defaults to using <see cref="object.ToString"/>.
|
||||
/// </summary>
|
||||
protected virtual string DisplayValue(TValue value) => value.ToString();
|
||||
|
||||
public SimpleStatisticItem(string name)
|
||||
: base(name)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
122
osu.Game/Screens/Ranking/Statistics/SimpleStatisticRow.cs
Normal file
122
osu.Game/Screens/Ranking/Statistics/SimpleStatisticRow.cs
Normal file
@ -0,0 +1,122 @@
|
||||
// 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;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Extensions.Color4Extensions;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Shapes;
|
||||
|
||||
namespace osu.Game.Screens.Ranking.Statistics
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a statistic row with simple statistics (ones that only need textual display).
|
||||
/// Richer visualisations should be done with <see cref="StatisticRow"/>s and <see cref="StatisticItem"/>s.
|
||||
/// </summary>
|
||||
public class SimpleStatisticRow : CompositeDrawable
|
||||
{
|
||||
private readonly SimpleStatisticItem[] items;
|
||||
private readonly int columnCount;
|
||||
|
||||
private FillFlowContainer[] columns;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a statistic row for the supplied <see cref="SimpleStatisticItem"/>s.
|
||||
/// </summary>
|
||||
/// <param name="columnCount">The number of columns to layout the <paramref name="items"/> into.</param>
|
||||
/// <param name="items">The <see cref="SimpleStatisticItem"/>s to display in this row.</param>
|
||||
public SimpleStatisticRow(int columnCount, IEnumerable<SimpleStatisticItem> items)
|
||||
{
|
||||
if (columnCount < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(columnCount));
|
||||
|
||||
this.columnCount = columnCount;
|
||||
this.items = items.ToArray();
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load()
|
||||
{
|
||||
columns = new FillFlowContainer[columnCount];
|
||||
|
||||
RelativeSizeAxes = Axes.X;
|
||||
AutoSizeAxes = Axes.Y;
|
||||
|
||||
InternalChild = new GridContainer
|
||||
{
|
||||
RelativeSizeAxes = Axes.X,
|
||||
AutoSizeAxes = Axes.Y,
|
||||
RowDimensions = new[]
|
||||
{
|
||||
new Dimension(GridSizeMode.AutoSize)
|
||||
},
|
||||
ColumnDimensions = createColumnDimensions().ToArray(),
|
||||
Content = new[] { createColumns().ToArray() }
|
||||
};
|
||||
|
||||
for (int i = 0; i < items.Length; ++i)
|
||||
columns[i % columnCount].Add(items[i]);
|
||||
}
|
||||
|
||||
private IEnumerable<Dimension> createColumnDimensions()
|
||||
{
|
||||
for (int column = 0; column < columnCount; ++column)
|
||||
{
|
||||
if (column > 0)
|
||||
yield return new Dimension(GridSizeMode.Absolute, 30);
|
||||
|
||||
yield return new Dimension();
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<Drawable> createColumns()
|
||||
{
|
||||
for (int column = 0; column < columnCount; ++column)
|
||||
{
|
||||
if (column > 0)
|
||||
{
|
||||
yield return new Spacer
|
||||
{
|
||||
Alpha = items.Length > column ? 1 : 0
|
||||
};
|
||||
}
|
||||
|
||||
yield return columns[column] = createColumn();
|
||||
}
|
||||
}
|
||||
|
||||
private FillFlowContainer createColumn() => new FillFlowContainer
|
||||
{
|
||||
RelativeSizeAxes = Axes.X,
|
||||
AutoSizeAxes = Axes.Y,
|
||||
Direction = FillDirection.Vertical
|
||||
};
|
||||
|
||||
private class Spacer : CompositeDrawable
|
||||
{
|
||||
public Spacer()
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both;
|
||||
Padding = new MarginPadding { Vertical = 4 };
|
||||
|
||||
InternalChild = new CircularContainer
|
||||
{
|
||||
RelativeSizeAxes = Axes.Y,
|
||||
Width = 3,
|
||||
Origin = Anchor.Centre,
|
||||
Anchor = Anchor.Centre,
|
||||
CornerRadius = 2,
|
||||
Masking = true,
|
||||
Child = new Box
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Colour = Color4Extensions.FromHex("#222")
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user