2017-04-03 17:28:05 +00:00
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
2017-03-25 22:33:03 +00:00
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
using OpenTK;
|
2017-04-03 17:28:05 +00:00
|
|
|
using OpenTK.Graphics;
|
2017-03-25 22:33:03 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Colour;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2017-04-04 16:23:29 +00:00
|
|
|
using System;
|
2017-04-03 17:28:05 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2017-03-25 22:33:03 +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-04 15:17:22 +00:00
|
|
|
private BarDirection direction = BarDirection.BottomToTop;
|
|
|
|
public new BarDirection Direction
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
direction = value;
|
2017-04-04 16:09:16 +00:00
|
|
|
base.Direction = (direction & BarDirection.Horizontal) > 0 ? FillDirection.Vertical : FillDirection.Horizontal;
|
2017-04-04 15:17:22 +00:00
|
|
|
foreach (var bar in Children)
|
2017-04-04 16:09:16 +00:00
|
|
|
{
|
|
|
|
bar.Size = (direction & BarDirection.Horizontal) > 0 ? 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 17:28:05 +00:00
|
|
|
public IEnumerable<float> Values
|
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
|
|
|
List<float> values = value.ToList();
|
|
|
|
List<Bar> graphBars = Children.ToList();
|
|
|
|
for (int i = 0; i < values.Count; i++)
|
|
|
|
if (graphBars.Count > i)
|
|
|
|
{
|
|
|
|
graphBars[i].Length = values[i] / values.Max();
|
2017-04-04 16:09:16 +00:00
|
|
|
graphBars[i].Size = (direction & BarDirection.Horizontal) > 0 ? new Vector2(1, 1.0f / values.Count) : new Vector2(1.0f / values.Count, 1);
|
2017-04-03 17:28:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Add(new Bar
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-04-04 16:09:16 +00:00
|
|
|
Size = (direction & BarDirection.Horizontal) > 0 ? new Vector2(1, 1.0f / values.Count) : new Vector2(1.0f / values.Count, 1),
|
2017-04-03 17:28:05 +00:00
|
|
|
Length = values[i] / values.Max(),
|
2017-04-04 15:17:22 +00:00
|
|
|
Direction = Direction,
|
2017-04-03 17:28:05 +00:00
|
|
|
BackgroundColour = new Color4(0, 0, 0, 0),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Bar : Container
|
2017-03-25 22:33:03 +00:00
|
|
|
{
|
2017-03-29 14:00:29 +00:00
|
|
|
private readonly Box background;
|
|
|
|
private readonly Box bar;
|
2017-03-25 22:33:03 +00:00
|
|
|
|
2017-03-28 18:18:56 +00:00
|
|
|
private const int resize_duration = 250;
|
2017-03-28 15:12:54 +00:00
|
|
|
|
2017-03-29 12:48:43 +00:00
|
|
|
private const EasingTypes easing = EasingTypes.InOutCubic;
|
|
|
|
|
2017-03-28 15:12:54 +00:00
|
|
|
private float length;
|
|
|
|
public float Length
|
2017-03-25 22:33:03 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2017-03-28 15:12:54 +00:00
|
|
|
return length;
|
2017-03-25 22:33:03 +00:00
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2017-04-03 17:28:05 +00:00
|
|
|
length = MathHelper.Clamp(value, 0, 1);
|
2017-03-28 15:12:54 +00:00
|
|
|
updateBarLength();
|
2017-03-25 22:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public SRGBColour BackgroundColour
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return background.Colour;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
background.Colour = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public SRGBColour BarColour
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return bar.Colour;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
bar.Colour = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 15:12:54 +00:00
|
|
|
private BarDirection direction = BarDirection.LeftToRight;
|
|
|
|
public BarDirection Direction
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
direction = value;
|
|
|
|
updateBarLength();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 17:28:05 +00:00
|
|
|
public Bar()
|
2017-03-25 22:33:03 +00:00
|
|
|
{
|
2017-04-03 17:28:05 +00:00
|
|
|
Children = new[]
|
2017-03-25 22:33:03 +00:00
|
|
|
{
|
|
|
|
background = new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
},
|
|
|
|
bar = new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-03-28 15:12:54 +00:00
|
|
|
|
|
|
|
private void updateBarLength()
|
|
|
|
{
|
|
|
|
switch (direction)
|
|
|
|
{
|
|
|
|
case BarDirection.LeftToRight:
|
|
|
|
case BarDirection.RightToLeft:
|
2017-03-29 12:48:43 +00:00
|
|
|
bar.ResizeTo(new Vector2(length, 1), resize_duration, easing);
|
2017-03-28 15:12:54 +00:00
|
|
|
break;
|
2017-04-04 15:27:08 +00:00
|
|
|
|
2017-03-28 15:12:54 +00:00
|
|
|
case BarDirection.TopToBottom:
|
|
|
|
case BarDirection.BottomToTop:
|
2017-03-29 12:48:43 +00:00
|
|
|
bar.ResizeTo(new Vector2(1, length), resize_duration, easing);
|
2017-03-28 15:12:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (direction)
|
|
|
|
{
|
|
|
|
case BarDirection.LeftToRight:
|
|
|
|
case BarDirection.TopToBottom:
|
|
|
|
bar.Anchor = Anchor.TopLeft;
|
|
|
|
bar.Origin = Anchor.TopLeft;
|
|
|
|
break;
|
2017-04-04 15:27:08 +00:00
|
|
|
|
2017-03-28 15:12:54 +00:00
|
|
|
case BarDirection.RightToLeft:
|
|
|
|
case BarDirection.BottomToTop:
|
|
|
|
bar.Anchor = Anchor.BottomRight;
|
|
|
|
bar.Origin = Anchor.BottomRight;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 16:23:29 +00:00
|
|
|
[Flags]
|
2017-03-28 15:12:54 +00:00
|
|
|
public enum BarDirection
|
|
|
|
{
|
2017-04-04 16:09:16 +00:00
|
|
|
LeftToRight = 1 << 0,
|
|
|
|
RightToLeft = 1 << 1,
|
|
|
|
TopToBottom = 1 << 2,
|
|
|
|
BottomToTop = 1 << 3,
|
|
|
|
|
|
|
|
Vertical = TopToBottom | BottomToTop,
|
|
|
|
Horizontal = LeftToRight | RightToLeft,
|
2017-03-25 22:33:03 +00:00
|
|
|
}
|
2017-04-04 15:27:08 +00:00
|
|
|
}
|