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