1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 03:27:26 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/LineGraph.cs

106 lines
3.5 KiB
C#
Raw Normal View History

2017-06-14 20:37:07 +08: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 14:47:14 +08:00
public float ActualMaxValue { get; private set; } = float.NaN;
public float ActualMinValue { get; private set; } = float.NaN;
2017-06-15 02:17:48 +08:00
2017-06-15 00:46:39 +08:00
private const double transform_duration = 500;
2017-06-14 20:37:07 +08:00
/// <summary>
/// Hold an empty area if values are less.
/// </summary>
public int DefaultValueCount;
2017-06-15 00:46:39 +08:00
private readonly Container<Path> maskingContainer;
private readonly Path path;
2017-06-14 20:37:07 +08:00
private float[] values;
2017-06-14 20:37:07 +08:00
/// <summary>
/// A list of floats decides position of each line node.
/// </summary>
public IEnumerable<float> Values
{
2017-06-15 02:17:48 +08:00
get { return values; }
2017-06-14 20:37:07 +08:00
set
{
values = value.ToArray();
applyPath();
2017-06-15 00:46:39 +08:00
maskingContainer.Width = 0;
maskingContainer.ResizeWidthTo(1, transform_duration, EasingTypes.OutQuint);
}
}
2017-06-15 00:46:39 +08:00
public LineGraph()
{
Add(maskingContainer = new Container<Path>
{
Masking = true,
2017-06-23 13:02:19 +08:00
RelativeSizeAxes = Axes.Both,
Children = new[]
{
path = new Path { RelativeSizeAxes = Axes.Both, PathWidth = 1 }
}
2017-06-15 00:46:39 +08: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 13:02:19 +08: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-15 02:17:48 +08: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 14:47:14 +08:00
float y = GetYPosition(values[i]) * DrawHeight - 1;
// the -1 is for inner offset in path (actually -PathWidth)
2017-06-15 00:46:39 +08:00
path.AddVertex(new Vector2(x, y));
2017-06-14 20:37:07 +08:00
}
}
2017-06-16 14:47:14 +08:00
protected float GetYPosition(float value)
{
if (ActualMaxValue == ActualMinValue) return 0;
return (ActualMaxValue - value) / (ActualMaxValue - ActualMinValue);
}
2017-06-14 20:37:07 +08:00
}
}