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

90 lines
3.0 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-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
{
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,
RelativeSizeAxes = Axes.Both
});
maskingContainer.Add(path = new Path { RelativeSizeAxes = Axes.Both, PathWidth = 1 });
}
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()
{
if (values == null) return;
2017-06-15 00:46:39 +08:00
path.ClearVertices();
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;
for (int i = 0; i < values.Length; i++)
{
float x = (i + count - values.Length) / (float)(count - 1) * DrawWidth - 1;
float y = (max - values[i]) / (max - min) * 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
}
}
}
}