2019-01-24 16:43:03 +08: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 17:19:50 +08:00
|
|
|
|
2018-11-20 15:51:59 +08:00
|
|
|
using osuTK;
|
2018-04-13 17:19:50 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
{
|
|
|
|
public class BarGraph : FillFlowContainer<Bar>
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Manually sets the max value, if null <see cref="Enumerable.Max(IEnumerable{float})"/> is instead used
|
|
|
|
/// </summary>
|
|
|
|
public float? MaxValue { get; set; }
|
|
|
|
|
|
|
|
private BarDirection direction = BarDirection.BottomToTop;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
public new BarDirection Direction
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
get => direction;
|
2018-04-13 17:19:50 +08:00
|
|
|
set
|
|
|
|
{
|
|
|
|
direction = value;
|
2018-07-16 15:17:22 +08:00
|
|
|
base.Direction = direction.HasFlag(BarDirection.Horizontal) ? FillDirection.Vertical : FillDirection.Horizontal;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
foreach (var bar in Children)
|
|
|
|
{
|
2018-07-16 15:17:22 +08:00
|
|
|
bar.Size = direction.HasFlag(BarDirection.Horizontal) ? new Vector2(1, 1.0f / Children.Count) : new Vector2(1.0f / Children.Count, 1);
|
2018-04-13 17:19:50 +08:00
|
|
|
bar.Direction = direction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A list of floats that defines the length of each <see cref="Bar"/>
|
|
|
|
/// </summary>
|
|
|
|
public IEnumerable<float> Values
|
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
|
|
|
List<Bar> bars = Children.ToList();
|
2019-04-01 11:16:05 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
foreach (var bar in value.Select((length, index) => new { Value = length, Bar = bars.Count > index ? bars[index] : null }))
|
|
|
|
{
|
|
|
|
float length = MaxValue ?? value.Max();
|
|
|
|
if (length != 0)
|
|
|
|
length = bar.Value / length;
|
|
|
|
|
|
|
|
float size = value.Count();
|
|
|
|
if (size != 0)
|
|
|
|
size = 1.0f / size;
|
|
|
|
|
|
|
|
if (bar.Bar != null)
|
|
|
|
{
|
|
|
|
bar.Bar.Length = length;
|
2018-07-16 15:17:22 +08:00
|
|
|
bar.Bar.Size = direction.HasFlag(BarDirection.Horizontal) ? new Vector2(1, size) : new Vector2(size, 1);
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Add(new Bar
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2018-07-16 15:17:22 +08:00
|
|
|
Size = direction.HasFlag(BarDirection.Horizontal) ? new Vector2(1, size) : new Vector2(size, 1),
|
2018-04-13 17:19:50 +08:00
|
|
|
Length = length,
|
|
|
|
Direction = Direction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
//I'm using ToList() here because Where() returns an Enumerable which can change it's elements afterwards
|
|
|
|
RemoveRange(Children.Where((bar, index) => index >= value.Count()).ToList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|