1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 22:22:55 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/BarGraph.cs

65 lines
2.7 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
2017-03-26 06:33:03 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using System.Collections.Generic;
using System.Linq;
2017-03-26 06:33:03 +08:00
namespace osu.Game.Graphics.UserInterface
2017-03-26 06:33:03 +08:00
{
public class BarGraph : FillFlowContainer<Bar>
{
2017-04-10 22:42:23 +08:00
/// <summary>
/// Manually sets the max value, if null <see cref="Enumerable.Max(IEnumerable{float})"/> is instead used
/// </summary>
public float? MaxValue { get; set; }
2017-04-04 23:17:22 +08:00
private BarDirection direction = BarDirection.BottomToTop;
public new BarDirection Direction
{
get
{
return direction;
}
set
{
direction = value;
base.Direction = (direction & BarDirection.Horizontal) > 0 ? FillDirection.Vertical : FillDirection.Horizontal;
2017-04-04 23:17:22 +08:00
foreach (var bar in Children)
{
2017-06-25 13:46:59 +08:00
bar.Size = (direction & BarDirection.Horizontal) > 0 ? new Vector2(1, 1.0f / Children.Count) : new Vector2(1.0f / Children.Count, 1);
2017-04-04 23:17:22 +08:00
bar.Direction = direction;
}
2017-04-04 23:17:22 +08:00
}
}
2017-04-10 22:42:23 +08:00
/// <summary>
/// A list of floats that defines the length of each <see cref="Bar"/>
/// </summary>
public IEnumerable<float> Values
{
set
{
2017-04-12 00:43:48 +08:00
List<Bar> bars = Children.ToList();
foreach (var bar in value.Select((length, index) => new { Value = length, Bar = bars.Count > index ? bars[index] : null }))
if (bar.Bar != null)
{
2017-04-12 00:43:48 +08:00
bar.Bar.Length = bar.Value / (MaxValue ?? value.Max());
bar.Bar.Size = (direction & BarDirection.Horizontal) > 0 ? new Vector2(1, 1.0f / value.Count()) : new Vector2(1.0f / value.Count(), 1);
}
else
Add(new Bar
{
RelativeSizeAxes = Axes.Both,
2017-04-12 00:43:48 +08:00
Size = (direction & BarDirection.Horizontal) > 0 ? new Vector2(1, 1.0f / value.Count()) : new Vector2(1.0f / value.Count(), 1),
Length = bar.Value / (MaxValue ?? value.Max()),
2017-04-04 23:17:22 +08:00
Direction = Direction,
});
//I'm using ToList() here because Where() returns an Enumerable which can change it's elements afterwards
2017-07-11 21:58:06 +08:00
RemoveRange(Children.Where((bar, index) => index >= value.Count()).ToList());
}
}
}
2017-04-04 23:27:08 +08:00
}