1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 01:47:24 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/LineGraph.cs

119 lines
3.8 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-06-14 20:37:07 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
2017-12-22 17:58:35 +08:00
using osu.Framework.Caching;
2017-06-14 20:37:07 +08:00
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-26 15:26:43 +08:00
private const double transform_duration = 1500;
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();
2017-12-22 17:58:35 +08:00
float max = values.Max(), min = values.Min();
if (MaxValue > max) max = MaxValue.Value;
if (MinValue < min) min = MinValue.Value;
ActualMaxValue = max;
ActualMinValue = min;
pathCached.Invalidate();
2017-06-15 00:46:39 +08:00
maskingContainer.Width = 0;
2017-07-23 02:50:25 +08:00
maskingContainer.ResizeWidthTo(1, transform_duration, Easing.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,
2017-07-13 12:52:40 +08:00
Child = 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)
{
2017-12-22 17:58:35 +08:00
if ((invalidation & Invalidation.DrawSize) > 0)
pathCached.Invalidate();
return base.Invalidate(invalidation, source, shallPropagate);
}
2017-12-22 17:58:35 +08:00
private Cached pathCached = new Cached();
protected override void Update()
{
base.Update();
if (!pathCached.IsValid)
{
applyPath();
pathCached.Validate();
}
}
private void applyPath()
{
2017-06-23 13:02:19 +08:00
path.ClearVertices();
if (values == null) return;
int count = Math.Max(values.Length, DefaultValueCount);
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
}
}