2019-01-24 08:43:03 +00: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.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2017-06-14 12:37:07 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK;
|
2017-06-14 12:37:07 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Lines;
|
2020-02-24 11:52:15 +00:00
|
|
|
|
using osu.Framework.Layout;
|
2018-12-22 20:50:25 +00:00
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-14 12:37:07 +00:00
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
|
|
|
|
public partial class LineGraph : Container
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manually set the max value, otherwise <see cref="Enumerable.Max(IEnumerable{float})"/> will be used.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float? MaxValue { get; set; }
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-14 12:37:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manually set the min value, otherwise <see cref="Enumerable.Min(IEnumerable{float})"/> will be used.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float? MinValue { get; set; }
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-16 06:47:14 +00:00
|
|
|
|
public float ActualMaxValue { get; private set; } = float.NaN;
|
|
|
|
|
public float ActualMinValue { get; private set; } = float.NaN;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-26 07:26:43 +00:00
|
|
|
|
private const double transform_duration = 1500;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-14 12:37:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Hold an empty area if values are less.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int DefaultValueCount;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-14 16:46:39 +00:00
|
|
|
|
private readonly Container<Path> maskingContainer;
|
|
|
|
|
private readonly Path path;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-14 14:00:22 +00:00
|
|
|
|
private float[] values;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-14 12:37:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A list of floats decides position of each line node.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<float> Values
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => values;
|
2017-06-14 12:37:07 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
2017-06-14 14:00:22 +00:00
|
|
|
|
values = value.ToArray();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-12-22 09:58:35 +00:00
|
|
|
|
float max = values.Max(), min = values.Min();
|
|
|
|
|
if (MaxValue > max) max = MaxValue.Value;
|
|
|
|
|
if (MinValue < min) min = MinValue.Value;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-12-22 09:58:35 +00:00
|
|
|
|
ActualMaxValue = max;
|
|
|
|
|
ActualMinValue = min;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-12-22 09:58:35 +00:00
|
|
|
|
pathCached.Invalidate();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-14 16:46:39 +00:00
|
|
|
|
maskingContainer.Width = 0;
|
2017-07-22 18:50:25 +00:00
|
|
|
|
maskingContainer.ResizeWidthTo(1, transform_duration, Easing.OutQuint);
|
2017-06-14 14:00:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-12-22 20:50:25 +00:00
|
|
|
|
public Color4 LineColour
|
|
|
|
|
{
|
|
|
|
|
get => maskingContainer.Colour;
|
|
|
|
|
set => maskingContainer.Colour = value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-14 16:46:39 +00:00
|
|
|
|
public LineGraph()
|
|
|
|
|
{
|
|
|
|
|
Add(maskingContainer = new Container<Path>
|
|
|
|
|
{
|
|
|
|
|
Masking = true,
|
2017-06-23 05:02:19 +00:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2019-07-26 08:48:19 +00:00
|
|
|
|
Child = path = new SmoothPath
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.None,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
PathRadius = 1
|
|
|
|
|
}
|
2017-06-14 16:46:39 +00:00
|
|
|
|
});
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-02-24 11:52:15 +00:00
|
|
|
|
AddLayout(pathCached);
|
2017-06-14 14:00:22 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-02-24 11:52:15 +00:00
|
|
|
|
private readonly LayoutValue pathCached = new LayoutValue(Invalidation.DrawSize);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-12-22 09:58:35 +00:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
2019-04-01 03:16:05 +00:00
|
|
|
|
|
2017-12-22 09:58:35 +00:00
|
|
|
|
if (!pathCached.IsValid)
|
|
|
|
|
{
|
|
|
|
|
applyPath();
|
|
|
|
|
pathCached.Validate();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-14 14:00:22 +00:00
|
|
|
|
private void applyPath()
|
|
|
|
|
{
|
2017-06-23 05:02:19 +00:00
|
|
|
|
path.ClearVertices();
|
2017-06-14 14:00:22 +00:00
|
|
|
|
if (values == null) return;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-14 14:00:22 +00:00
|
|
|
|
int count = Math.Max(values.Length, DefaultValueCount);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-14 14:00:22 +00:00
|
|
|
|
for (int i = 0; i < values.Length; i++)
|
|
|
|
|
{
|
2019-03-07 04:45:55 +00:00
|
|
|
|
// Make sure that we are accounting for path width when calculating vertex positions
|
2019-03-07 08:25:51 +00:00
|
|
|
|
// We need to apply 2x the path radius to account for it because the full diameter of the line accounts into height
|
2019-03-07 08:39:19 +00:00
|
|
|
|
float x = (i + count - values.Length) / (float)(count - 1) * (DrawWidth - 2 * path.PathRadius);
|
|
|
|
|
float y = GetYPosition(values[i]) * (DrawHeight - 2 * path.PathRadius);
|
2017-06-14 16:46:39 +00:00
|
|
|
|
path.AddVertex(new Vector2(x, y));
|
2017-06-14 12:37:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-16 06:47:14 +00:00
|
|
|
|
protected float GetYPosition(float value)
|
|
|
|
|
{
|
2020-11-23 21:12:32 +00:00
|
|
|
|
if (ActualMaxValue == ActualMinValue)
|
|
|
|
|
// show line at top if the only value on the graph is positive,
|
|
|
|
|
// and at bottom if the only value on the graph is zero or negative.
|
|
|
|
|
// just kind of makes most sense intuitively.
|
|
|
|
|
return value > 1 ? 0 : 1;
|
2019-02-28 04:31:40 +00:00
|
|
|
|
|
2017-06-16 06:47:14 +00:00
|
|
|
|
return (ActualMaxValue - value) / (ActualMaxValue - ActualMinValue);
|
|
|
|
|
}
|
2017-06-14 12:37:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|