1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 13:47:24 +08:00
osu-lazer/osu.Game/Screens/Select/Details/UserRatings.cs

142 lines
5.1 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using System.Linq;
using osu.Game.Beatmaps;
namespace osu.Game.Screens.Select.Details
{
public class UserRatings : Container
{
private readonly FillFlowContainer header;
private readonly Bar ratingsBar;
private readonly OsuSpriteText negativeRatings, positiveRatings;
private readonly Container graphContainer;
private readonly BarGraph graph;
2019-06-13 15:52:49 +08:00
private BeatmapSetMetrics metrics;
2019-06-13 15:52:49 +08:00
public BeatmapSetMetrics Metrics
2018-04-13 17:19:50 +08:00
{
get => metrics;
2018-04-13 17:19:50 +08:00
set
{
if (value == metrics) return;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
metrics = value;
const int rating_range = 10;
if (metrics == null)
{
negativeRatings.Text = "0";
positiveRatings.Text = "0";
ratingsBar.Length = 0;
graph.Values = new float[rating_range];
}
else
{
var ratings = Metrics.Ratings.Skip(1).Take(rating_range); // adjust for API returning weird empty data at 0.
var negativeCount = ratings.Take(rating_range / 2).Sum();
var totalCount = ratings.Sum();
negativeRatings.Text = negativeCount.ToString();
positiveRatings.Text = (totalCount - negativeCount).ToString();
ratingsBar.Length = totalCount == 0 ? 0 : (float)negativeCount / totalCount;
graph.Values = ratings.Take(rating_range).Select(r => (float)r);
}
2018-04-13 17:19:50 +08:00
}
}
public UserRatings()
{
Children = new Drawable[]
{
header = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = "User Rating",
Font = OsuFont.GetFont(size: 12),
Margin = new MarginPadding { Bottom = 5 },
2018-04-13 17:19:50 +08:00
},
ratingsBar = new Bar
{
RelativeSizeAxes = Axes.X,
Height = 5,
},
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Margin = new MarginPadding { Bottom = 10 },
2018-04-13 17:19:50 +08:00
Children = new[]
{
negativeRatings = new OsuSpriteText
{
Text = "0",
Font = OsuFont.GetFont(size: 12)
2018-04-13 17:19:50 +08:00
},
positiveRatings = new OsuSpriteText
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Text = @"0",
Font = OsuFont.GetFont(size: 12)
2018-04-13 17:19:50 +08:00
},
},
},
new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = "Rating Spread",
Font = OsuFont.GetFont(size: 12),
Margin = new MarginPadding { Bottom = 5 },
2018-04-13 17:19:50 +08:00
},
},
},
graphContainer = new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.Both,
Child = graph = new BarGraph
{
RelativeSizeAxes = Axes.Both,
},
},
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
ratingsBar.BackgroundColour = colours.Green;
ratingsBar.AccentColour = colours.Yellow;
graph.Colour = colours.BlueDark;
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
graphContainer.Padding = new MarginPadding { Top = header.DrawHeight };
}
}
}