1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 07:53:06 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/Bar.cs

122 lines
3.1 KiB
C#
Raw Normal View History

// 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
using System;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
namespace osu.Game.Graphics.UserInterface
{
public class Bar : Container, IHasAccentColour
{
private readonly Box background;
private readonly Box bar;
private const int resize_duration = 250;
private const Easing easing = Easing.InOutCubic;
private float length;
/// <summary>
/// Length of the bar, ranges from 0 to 1
/// </summary>
public float Length
{
get => length;
2018-04-13 17:19:50 +08:00
set
{
length = Math.Clamp(value, 0, 1);
2018-04-13 17:19:50 +08:00
updateBarLength();
}
}
public Color4 BackgroundColour
{
2019-02-28 12:31:40 +08:00
get => background.Colour;
set => background.Colour = value;
2018-04-13 17:19:50 +08:00
}
public Color4 AccentColour
{
2019-02-28 12:31:40 +08:00
get => bar.Colour;
set => bar.Colour = value;
2018-04-13 17:19:50 +08:00
}
private BarDirection direction = BarDirection.LeftToRight;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public BarDirection Direction
{
get => direction;
2018-04-13 17:19:50 +08:00
set
{
direction = value;
updateBarLength();
}
}
public Bar()
{
Children = new[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = new Color4(0, 0, 0, 0)
},
bar = new Box
{
RelativeSizeAxes = Axes.Both,
Width = 0,
},
};
}
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
{
2019-08-09 11:29:33 +08:00
LeftToRight = 1,
2018-04-13 17:19:50 +08:00
RightToLeft = 1 << 1,
TopToBottom = 1 << 2,
BottomToTop = 1 << 3,
Vertical = TopToBottom | BottomToTop,
Horizontal = LeftToRight | RightToLeft,
}
}