1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 23:37:26 +08:00
osu-lazer/osu.Game/Screens/Ranking/Statistics/HitEventTimingDistributionGraph.cs

174 lines
6.2 KiB
C#
Raw Normal View History

2020-06-15 20:48:59 +08:00
// 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;
2020-06-19 19:31:52 +08:00
using System.Collections.Generic;
2020-06-15 20:48:59 +08:00
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;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2020-06-19 19:31:52 +08:00
using osu.Game.Rulesets.Scoring;
2020-06-15 20:48:59 +08:00
namespace osu.Game.Screens.Ranking.Statistics
2020-06-15 20:48:59 +08:00
{
2020-06-19 19:31:52 +08:00
/// <summary>
/// A graph which displays the distribution of hit timing in a series of <see cref="HitEvent"/>s.
/// </summary>
public class HitEventTimingDistributionGraph : CompositeDrawable
2020-06-15 20:48:59 +08:00
{
/// <summary>
/// The number of bins on each side of the timing distribution.
/// </summary>
private const int timing_distribution_bins = 50;
/// <summary>
/// The total number of bins in the timing distribution, including bins on both sides and the centre bin at 0.
/// </summary>
private const int total_timing_distribution_bins = timing_distribution_bins * 2 + 1;
/// <summary>
/// The centre bin, with a timing distribution very close to/at 0.
/// </summary>
private const int timing_distribution_centre_bin_index = timing_distribution_bins;
2020-06-15 20:48:59 +08:00
/// <summary>
2020-06-19 19:31:52 +08:00
/// The number of data points shown on each side of the axis below the graph.
2020-06-15 20:48:59 +08:00
/// </summary>
private const float axis_points = 5;
2020-06-19 19:31:52 +08:00
private readonly IReadOnlyList<HitEvent> hitEvents;
2020-06-15 20:48:59 +08:00
2020-06-19 19:31:52 +08:00
/// <summary>
/// Creates a new <see cref="HitEventTimingDistributionGraph"/>.
/// </summary>
/// <param name="hitEvents">The <see cref="HitEvent"/>s to display the timing distribution of.</param>
public HitEventTimingDistributionGraph(IReadOnlyList<HitEvent> hitEvents)
2020-06-15 20:48:59 +08:00
{
this.hitEvents = hitEvents.Where(e => !(e.HitObject.HitWindows is HitWindows.EmptyHitWindows) && e.Result.IsHit()).ToList();
2020-06-15 20:48:59 +08:00
}
[BackgroundDependencyLoader]
private void load()
{
2020-06-19 19:31:52 +08:00
if (hitEvents == null || hitEvents.Count == 0)
return;
int[] bins = new int[total_timing_distribution_bins];
2020-06-22 19:32:04 +08:00
2020-06-19 19:53:24 +08:00
double binSize = Math.Ceiling(hitEvents.Max(e => Math.Abs(e.TimeOffset)) / timing_distribution_bins);
2020-06-22 19:32:04 +08:00
// Prevent div-by-0 by enforcing a minimum bin size
binSize = Math.Max(1, binSize);
2020-06-19 19:31:52 +08:00
foreach (var e in hitEvents)
{
int binOffset = (int)Math.Round(e.TimeOffset / binSize, MidpointRounding.AwayFromZero);
bins[timing_distribution_centre_bin_index + binOffset]++;
}
2020-06-15 20:48:59 +08:00
int maxCount = bins.Max();
var bars = new Drawable[total_timing_distribution_bins];
2020-06-15 20:48:59 +08:00
for (int i = 0; i < bars.Length; i++)
bars[i] = new Bar { Height = Math.Max(0.05f, (float)bins[i] / maxCount) };
2020-06-15 20:48:59 +08:00
Container axisFlow;
InternalChild = new GridContainer
{
2020-06-19 19:31:52 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2020-06-15 20:48:59 +08:00
RelativeSizeAxes = Axes.Both,
Width = 0.8f,
2020-06-15 20:48:59 +08:00
Content = new[]
{
new Drawable[]
{
new GridContainer
{
RelativeSizeAxes = Axes.Both,
Content = new[] { bars }
}
},
new Drawable[]
{
axisFlow = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y
}
},
},
RowDimensions = new[]
{
new Dimension(),
new Dimension(GridSizeMode.AutoSize),
}
};
// Our axis will contain one centre element + 5 points on each side, each with a value depending on the number of bins * bin size.
double maxValue = timing_distribution_bins * binSize;
double axisValueStep = maxValue / axis_points;
2020-06-15 20:48:59 +08:00
axisFlow.Add(new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = "0",
Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold)
});
for (int i = 1; i <= axis_points; i++)
{
double axisValue = i * axisValueStep;
float position = (float)(axisValue / maxValue);
float alpha = 1f - position * 0.8f;
axisFlow.Add(new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.X,
X = -position / 2,
Alpha = alpha,
Text = axisValue.ToString("-0"),
Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold)
});
axisFlow.Add(new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.X,
X = position / 2,
Alpha = alpha,
Text = axisValue.ToString("+0"),
Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold)
});
}
}
private class Bar : CompositeDrawable
{
public Bar()
{
Anchor = Anchor.BottomCentre;
Origin = Anchor.BottomCentre;
RelativeSizeAxes = Axes.Both;
Padding = new MarginPadding { Horizontal = 1 };
InternalChild = new Circle
{
RelativeSizeAxes = Axes.Both,
Colour = Color4Extensions.FromHex("#66FFCC")
};
}
}
}
}