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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2020-06-18 21:11:03 +08:00
|
|
|
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.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;
|
2022-03-04 13:34:33 +08:00
|
|
|
using osuTK.Graphics;
|
2020-06-15 20:48:59 +08:00
|
|
|
|
2020-06-19 19:03:18 +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>
|
2020-06-19 19:03:18 +08:00
|
|
|
public class HitEventTimingDistributionGraph : CompositeDrawable
|
2020-06-15 20:48:59 +08:00
|
|
|
{
|
2020-06-18 21:11:03 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The number of bins on each side of the timing distribution.
|
|
|
|
/// </summary>
|
2020-06-19 18:41:36 +08:00
|
|
|
private const int timing_distribution_bins = 50;
|
2020-06-18 21:11:03 +08:00
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
2022-03-04 12:09:19 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The currently displayed hit events.
|
|
|
|
/// </summary>
|
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
|
|
|
{
|
2020-10-03 04:57:49 +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
|
|
|
}
|
|
|
|
|
2022-09-05 01:02:38 +08:00
|
|
|
private IDictionary<HitResult, int>[] bins;
|
2022-03-04 12:09:19 +08:00
|
|
|
private double binSize;
|
|
|
|
private double hitOffset;
|
|
|
|
|
2022-03-04 13:34:33 +08:00
|
|
|
private Bar[] barDrawables;
|
|
|
|
|
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)
|
2020-06-16 16:20:38 +08:00
|
|
|
return;
|
|
|
|
|
2022-09-05 01:02:38 +08:00
|
|
|
bins = Enumerable.Range(0, total_timing_distribution_bins).Select(_ => new Dictionary<HitResult, int>()).ToArray<IDictionary<HitResult, int>>();
|
2020-06-22 19:32:04 +08:00
|
|
|
|
2022-03-04 12:09:19 +08:00
|
|
|
binSize = Math.Ceiling(hitEvents.Max(e => Math.Abs(e.TimeOffset)) / timing_distribution_bins);
|
2020-06-18 21:11:03 +08:00
|
|
|
|
2020-06-22 19:32:04 +08:00
|
|
|
// Prevent div-by-0 by enforcing a minimum bin size
|
|
|
|
binSize = Math.Max(1, binSize);
|
|
|
|
|
2022-03-04 12:09:19 +08:00
|
|
|
Scheduler.AddOnce(updateDisplay);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateOffset(double hitOffset)
|
|
|
|
{
|
|
|
|
this.hitOffset = hitOffset;
|
|
|
|
Scheduler.AddOnce(updateDisplay);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateDisplay()
|
|
|
|
{
|
2022-03-03 14:43:01 +08:00
|
|
|
bool roundUp = true;
|
|
|
|
|
2022-09-05 01:02:38 +08:00
|
|
|
foreach (var bin in bins)
|
|
|
|
bin.Clear();
|
2022-03-04 12:09:19 +08:00
|
|
|
|
2020-06-19 19:31:52 +08:00
|
|
|
foreach (var e in hitEvents)
|
2020-06-18 21:11:03 +08:00
|
|
|
{
|
2022-03-04 12:09:19 +08:00
|
|
|
double time = e.TimeOffset + hitOffset;
|
|
|
|
|
|
|
|
double binOffset = time / binSize;
|
2022-03-03 14:43:01 +08:00
|
|
|
|
|
|
|
// .NET's round midpoint handling doesn't provide a behaviour that works amazingly for display
|
|
|
|
// purposes here. We want midpoint rounding to roughly distribute evenly to each adjacent bucket
|
|
|
|
// so the easiest way is to cycle between downwards and upwards rounding as we process events.
|
|
|
|
if (Math.Abs(binOffset - (int)binOffset) == 0.5)
|
|
|
|
{
|
2022-03-04 11:42:07 +08:00
|
|
|
binOffset = (int)binOffset + Math.Sign(binOffset) * (roundUp ? 1 : 0);
|
2022-03-03 14:43:01 +08:00
|
|
|
roundUp = !roundUp;
|
|
|
|
}
|
|
|
|
|
2022-03-04 12:09:19 +08:00
|
|
|
int index = timing_distribution_centre_bin_index + (int)Math.Round(binOffset, MidpointRounding.AwayFromZero);
|
|
|
|
|
|
|
|
// may be out of range when applying an offset. for such cases we can just drop the results.
|
|
|
|
if (index >= 0 && index < bins.Length)
|
2022-09-05 01:02:38 +08:00
|
|
|
{
|
|
|
|
bins[index].TryGetValue(e.Result, out int value);
|
|
|
|
bins[index][e.Result] = ++value;
|
|
|
|
}
|
2020-06-18 21:11:03 +08:00
|
|
|
}
|
2020-06-15 20:48:59 +08:00
|
|
|
|
2022-03-04 13:34:33 +08:00
|
|
|
if (barDrawables != null)
|
2022-03-04 12:09:19 +08:00
|
|
|
{
|
2022-03-04 13:34:33 +08:00
|
|
|
for (int i = 0; i < barDrawables.Length; i++)
|
2022-03-04 12:09:19 +08:00
|
|
|
{
|
2022-09-05 01:02:38 +08:00
|
|
|
barDrawables[i].UpdateOffset(bins[i].Sum(b => b.Value));
|
2022-03-04 13:34:33 +08:00
|
|
|
}
|
2022-03-04 12:09:19 +08:00
|
|
|
}
|
2022-03-04 13:34:33 +08:00
|
|
|
else
|
|
|
|
{
|
2022-09-05 01:02:38 +08:00
|
|
|
int maxCount = bins.Max(b => b.Values.Sum());
|
2022-09-07 22:29:10 +08:00
|
|
|
barDrawables = bins.Select((bin, i) => new Bar(bins[i], maxCount, i == timing_distribution_centre_bin_index)).ToArray();
|
2020-06-15 20:48:59 +08:00
|
|
|
|
2022-03-04 13:34:33 +08:00
|
|
|
Container axisFlow;
|
2022-03-04 12:48:42 +08:00
|
|
|
|
2022-03-04 13:34:33 +08:00
|
|
|
const float axis_font_size = 12;
|
|
|
|
|
|
|
|
InternalChild = new GridContainer
|
2020-06-15 20:48:59 +08:00
|
|
|
{
|
2022-03-04 13:34:33 +08:00
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Width = 0.8f,
|
|
|
|
Content = new[]
|
2020-06-15 20:48:59 +08:00
|
|
|
{
|
2022-03-04 13:34:33 +08:00
|
|
|
new Drawable[]
|
2020-06-15 20:48:59 +08:00
|
|
|
{
|
2022-03-04 13:34:33 +08:00
|
|
|
new GridContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Content = new[] { barDrawables }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
new Drawable[]
|
2020-06-15 20:48:59 +08:00
|
|
|
{
|
2022-03-04 13:34:33 +08:00
|
|
|
axisFlow = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Height = axis_font_size,
|
|
|
|
}
|
|
|
|
},
|
2020-06-15 20:48:59 +08:00
|
|
|
},
|
2022-03-04 13:34:33 +08:00
|
|
|
RowDimensions = new[]
|
|
|
|
{
|
|
|
|
new Dimension(),
|
|
|
|
new Dimension(GridSizeMode.AutoSize),
|
|
|
|
}
|
|
|
|
};
|
2020-06-15 20:48:59 +08:00
|
|
|
|
2022-03-04 13:34:33 +08:00
|
|
|
// 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,
|
2022-03-04 13:34:33 +08:00
|
|
|
Text = "0",
|
2022-03-04 12:48:42 +08:00
|
|
|
Font = OsuFont.GetFont(size: axis_font_size, weight: FontWeight.SemiBold)
|
2020-06-15 20:48:59 +08:00
|
|
|
});
|
|
|
|
|
2022-03-04 13:34:33 +08:00
|
|
|
for (int i = 1; i <= axis_points; i++)
|
2020-06-15 20:48:59 +08:00
|
|
|
{
|
2022-03-04 13:34:33 +08:00
|
|
|
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: axis_font_size, 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: axis_font_size, weight: FontWeight.SemiBold)
|
|
|
|
});
|
|
|
|
}
|
2020-06-15 20:48:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class Bar : CompositeDrawable
|
|
|
|
{
|
2022-09-05 01:02:38 +08:00
|
|
|
private float totalValue => values.Sum(v => v.Value);
|
|
|
|
private float basalHeight => BoundingBox.Width / BoundingBox.Height;
|
|
|
|
private float availableHeight => 1 - basalHeight;
|
|
|
|
|
2022-09-07 22:29:10 +08:00
|
|
|
private readonly IReadOnlyList<KeyValuePair<HitResult, int>> values;
|
2022-03-04 13:34:33 +08:00
|
|
|
private readonly float maxValue;
|
2022-09-07 22:29:10 +08:00
|
|
|
private readonly bool isCentre;
|
2020-06-15 20:48:59 +08:00
|
|
|
|
2022-09-07 22:29:10 +08:00
|
|
|
private Circle[] boxOriginals;
|
2022-03-04 14:01:54 +08:00
|
|
|
private Circle boxAdjustment;
|
2020-06-15 20:48:59 +08:00
|
|
|
|
2022-09-07 22:29:10 +08:00
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
|
|
|
public Bar(IDictionary<HitResult, int> values, float maxValue, bool isCentre)
|
2022-03-04 13:34:33 +08:00
|
|
|
{
|
2022-09-08 16:50:27 +08:00
|
|
|
this.values = values.OrderBy(v => v.Key.GetIndexForOrderedDisplay()).ToList();
|
2022-03-04 13:34:33 +08:00
|
|
|
this.maxValue = maxValue;
|
2022-09-07 22:29:10 +08:00
|
|
|
this.isCentre = isCentre;
|
2022-03-04 13:09:27 +08:00
|
|
|
|
2022-03-04 13:34:33 +08:00
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
Masking = true;
|
2022-09-07 22:29:10 +08:00
|
|
|
}
|
2022-03-04 13:09:27 +08:00
|
|
|
|
2022-09-07 22:29:10 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2022-09-05 01:02:38 +08:00
|
|
|
if (values.Any())
|
2020-06-15 20:48:59 +08:00
|
|
|
{
|
2022-09-07 22:29:10 +08:00
|
|
|
boxOriginals = values.Select((v, i) => new Circle
|
2022-03-04 13:34:33 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
2022-09-07 22:34:46 +08:00
|
|
|
Colour = isCentre && i == 0 ? Color4.White : colours.ForHitResult(v.Key),
|
2022-09-05 01:02:38 +08:00
|
|
|
Height = 0,
|
|
|
|
}).ToArray();
|
2022-09-07 22:29:10 +08:00
|
|
|
// The bars of the stacked bar graph will be processed (stacked) from the bottom, which is the base position,
|
|
|
|
// to the top, and the bottom bar should be drawn more toward the front by design,
|
|
|
|
// while the drawing order is from the back to the front, so the order passed to `InternalChildren` is the opposite.
|
|
|
|
InternalChildren = boxOriginals.Reverse().ToArray();
|
2022-09-05 01:02:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-07 22:29:10 +08:00
|
|
|
// A bin with no value draws a grey dot instead.
|
2022-09-05 01:02:38 +08:00
|
|
|
InternalChildren = boxOriginals = new[]
|
|
|
|
{
|
|
|
|
new Circle
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Colour = isCentre ? Color4.White : Color4.Gray,
|
|
|
|
Height = 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-06-15 20:48:59 +08:00
|
|
|
}
|
2022-03-04 13:34:33 +08:00
|
|
|
|
2022-09-05 23:04:10 +08:00
|
|
|
private const double duration = 300;
|
2022-03-04 13:34:33 +08:00
|
|
|
|
2022-09-05 01:02:38 +08:00
|
|
|
private float offsetForValue(float value)
|
|
|
|
{
|
|
|
|
return availableHeight * value / maxValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private float heightForValue(float value)
|
|
|
|
{
|
|
|
|
return basalHeight + offsetForValue(value);
|
|
|
|
}
|
|
|
|
|
2022-03-04 13:34:33 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2022-03-04 13:59:53 +08:00
|
|
|
|
2022-09-05 23:04:10 +08:00
|
|
|
foreach (var boxOriginal in boxOriginals)
|
|
|
|
boxOriginal.Height = basalHeight;
|
|
|
|
|
2022-09-05 01:02:38 +08:00
|
|
|
float offsetValue = 0;
|
2022-03-04 13:59:53 +08:00
|
|
|
|
2022-09-07 01:29:15 +08:00
|
|
|
for (int i = 0; i < values.Count; i++)
|
2022-09-05 01:02:38 +08:00
|
|
|
{
|
2022-09-07 01:29:15 +08:00
|
|
|
boxOriginals[i].MoveToY(offsetForValue(offsetValue) * BoundingBox.Height, duration, Easing.OutQuint);
|
|
|
|
boxOriginals[i].ResizeHeightTo(heightForValue(values[i].Value), duration, Easing.OutQuint);
|
|
|
|
offsetValue -= values[i].Value;
|
2022-09-05 01:02:38 +08:00
|
|
|
}
|
2022-03-04 13:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateOffset(float adjustment)
|
|
|
|
{
|
2022-09-05 01:02:38 +08:00
|
|
|
bool hasAdjustment = adjustment != totalValue;
|
2022-03-04 14:19:55 +08:00
|
|
|
|
2022-03-04 14:01:54 +08:00
|
|
|
if (boxAdjustment == null)
|
|
|
|
{
|
2022-03-04 14:19:55 +08:00
|
|
|
if (!hasAdjustment)
|
|
|
|
return;
|
|
|
|
|
2022-03-04 14:01:54 +08:00
|
|
|
AddInternal(boxAdjustment = new Circle
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Anchor = Anchor.BottomCentre,
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
Colour = Color4.Yellow,
|
|
|
|
Blending = BlendingParameters.Additive,
|
|
|
|
Alpha = 0.6f,
|
|
|
|
Height = 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-05 01:02:38 +08:00
|
|
|
boxAdjustment.ResizeHeightTo(heightForValue(adjustment), duration, Easing.OutQuint);
|
2022-03-04 14:19:55 +08:00
|
|
|
boxAdjustment.FadeTo(!hasAdjustment ? 0 : 1, duration, Easing.OutQuint);
|
2022-03-04 13:34:33 +08:00
|
|
|
}
|
2020-06-15 20:48:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|