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/Bar.cs

137 lines
3.5 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK;
using OpenTK.Graphics;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using System;
namespace osu.Game.Graphics.UserInterface
{
2017-04-08 19:53:11 +08:00
public class Bar : Container, IHasAccentColour
{
2017-04-10 22:49:48 +08:00
private readonly Box background;
private readonly Box bar;
private const int resize_duration = 250;
2017-07-23 02:50:25 +08:00
private const Easing easing = Easing.InOutCubic;
private float length;
2017-04-10 22:42:23 +08:00
/// <summary>
/// Length of the bar, ranges from 0 to 1
/// </summary>
public float Length
{
get
{
return length;
}
set
{
length = MathHelper.Clamp(value, 0, 1);
updateBarLength();
}
}
public Color4 BackgroundColour
{
get
{
2017-04-10 22:45:34 +08:00
return background.Colour;
}
set
{
background.Colour = value;
}
}
2017-04-08 19:53:11 +08:00
public Color4 AccentColour
{
get
{
return bar.Colour;
}
set
{
bar.Colour = value;
}
}
private BarDirection direction = BarDirection.LeftToRight;
public BarDirection Direction
{
get
{
return direction;
}
set
{
direction = value;
updateBarLength();
}
}
public Bar()
{
Children = new[]
{
2017-04-10 22:42:23 +08:00
background = new Box
{
RelativeSizeAxes = Axes.Both,
2017-06-08 14:35:10 +08:00
Colour = new Color4(0, 0, 0, 0)
2017-04-10 22:42:23 +08:00
},
bar = new Box
{
RelativeSizeAxes = Axes.Both,
2017-04-12 16:52:24 +08:00
Width = 0,
2017-04-10 22:42:23 +08:00
},
};
}
private void updateBarLength()
{
switch (direction)
{
case BarDirection.LeftToRight:
case BarDirection.RightToLeft:
bar.ResizeTo(new Vector2(length, 1), resize_duration, easing);
break;
case BarDirection.TopToBottom:
case BarDirection.BottomToTop:
bar.ResizeTo(new Vector2(1, length), resize_duration, easing);
break;
}
switch (direction)
{
case BarDirection.LeftToRight:
case BarDirection.TopToBottom:
bar.Anchor = Anchor.TopLeft;
bar.Origin = Anchor.TopLeft;
break;
case BarDirection.RightToLeft:
case BarDirection.BottomToTop:
bar.Anchor = Anchor.BottomRight;
bar.Origin = Anchor.BottomRight;
break;
}
}
}
[Flags]
public enum BarDirection
{
LeftToRight = 1 << 0,
RightToLeft = 1 << 1,
TopToBottom = 1 << 2,
BottomToTop = 1 << 3,
Vertical = TopToBottom | BottomToTop,
Horizontal = LeftToRight | RightToLeft,
}
}