osu/osu.Game/Graphics/UserInterface/LineGraph.cs

103 lines
3.4 KiB
C#
Raw Normal View History

2017-06-14 12:37:07 +00:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using OpenTK;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Lines;
namespace osu.Game.Graphics.UserInterface
{
public 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; }
/// <summary>
/// Manually set the min value, otherwise <see cref="Enumerable.Min(IEnumerable{float})"/> will be used.
/// </summary>
public float? MinValue { get; set; }
2017-06-16 06:47:14 +00:00
public float ActualMaxValue { get; private set; } = float.NaN;
public float ActualMinValue { get; private set; } = float.NaN;
2017-06-14 18:17:48 +00:00
2017-06-26 07:26:43 +00:00
private const double transform_duration = 1500;
2017-06-14 12:37:07 +00:00
/// <summary>
/// Hold an empty area if values are less.
/// </summary>
public int DefaultValueCount;
2017-06-14 16:46:39 +00:00
private readonly Container<Path> maskingContainer;
private readonly Path path;
2017-06-14 12:37:07 +00:00
private float[] values;
2017-06-14 12:37:07 +00:00
/// <summary>
/// A list of floats decides position of each line node.
/// </summary>
public IEnumerable<float> Values
{
2017-06-14 18:17:48 +00:00
get { return values; }
2017-06-14 12:37:07 +00:00
set
{
values = value.ToArray();
applyPath();
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 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,
2017-07-13 04:52:40 +00:00
Child = path = new Path { RelativeSizeAxes = Axes.Both, PathWidth = 1 }
2017-06-14 16:46:39 +00:00
});
}
public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true)
{
if ((invalidation & Invalidation.DrawSize) != 0)
applyPath();
return base.Invalidate(invalidation, source, shallPropagate);
}
private void applyPath()
{
2017-06-23 05:02:19 +00:00
path.ClearVertices();
if (values == null) return;
int count = Math.Max(values.Length, DefaultValueCount);
float max = values.Max(), min = values.Min();
if (MaxValue > max) max = MaxValue.Value;
if (MinValue < min) min = MinValue.Value;
2017-06-14 18:17:48 +00:00
ActualMaxValue = max;
ActualMinValue = min;
for (int i = 0; i < values.Length; i++)
{
float x = (i + count - values.Length) / (float)(count - 1) * DrawWidth - 1;
2017-06-16 06:47:14 +00:00
float y = GetYPosition(values[i]) * DrawHeight - 1;
// the -1 is for inner offset in path (actually -PathWidth)
2017-06-14 16:46:39 +00:00
path.AddVertex(new Vector2(x, y));
2017-06-14 12:37:07 +00:00
}
}
2017-06-16 06:47:14 +00:00
protected float GetYPosition(float value)
{
if (ActualMaxValue == ActualMinValue) return 0;
return (ActualMaxValue - value) / (ActualMaxValue - ActualMinValue);
}
2017-06-14 12:37:07 +00:00
}
}