1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 02:12:54 +08:00
osu-lazer/osu.Game/Overlays/Settings/Sidebar.cs

149 lines
4.2 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;
using System.Linq;
using osu.Framework;
using osu.Framework.Allocation;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Framework.Threading;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2020-09-03 15:29:15 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Settings
{
public class Sidebar : Container<SidebarIconButton>, IStateful<ExpandedState>
2018-04-13 17:19:50 +08:00
{
private readonly Box background;
private readonly FillFlowContainer<SidebarIconButton> content;
public const float DEFAULT_WIDTH = 70;
2018-04-13 17:19:50 +08:00
public const int EXPANDED_WIDTH = 200;
public event Action<ExpandedState> StateChanged;
protected override Container<SidebarIconButton> Content => content;
2018-04-13 17:19:50 +08:00
public Sidebar()
{
RelativeSizeAxes = Axes.Y;
InternalChildren = new Drawable[]
{
background = new Box
2018-04-13 17:19:50 +08:00
{
Colour = OsuColour.Gray(0.02f),
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.Both,
},
new SidebarScrollContainer
{
Children = new[]
{
content = new FillFlowContainer<SidebarIconButton>
2018-04-13 17:19:50 +08:00
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
}
}
},
};
}
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
background.Colour = colourProvider.Background5;
}
2018-04-13 17:19:50 +08:00
private ScheduledDelegate expandEvent;
private ExpandedState state;
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
queueExpandIfHovering();
return true;
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
expandEvent?.Cancel();
lastHoveredButton = null;
State = ExpandedState.Contracted;
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
2018-04-13 17:19:50 +08:00
{
queueExpandIfHovering();
2018-10-02 11:02:47 +08:00
return base.OnMouseMove(e);
2018-04-13 17:19:50 +08:00
}
private class SidebarScrollContainer : OsuScrollContainer
2018-04-13 17:19:50 +08:00
{
public SidebarScrollContainer()
{
RelativeSizeAxes = Axes.Both;
ScrollbarVisible = false;
2018-04-13 17:19:50 +08:00
}
}
public ExpandedState State
{
get => state;
2018-04-13 17:19:50 +08:00
set
{
expandEvent?.Cancel();
if (state == value) return;
state = value;
switch (state)
{
default:
this.ResizeTo(new Vector2(DEFAULT_WIDTH, Height), 500, Easing.OutQuint);
break;
2019-04-01 11:44:46 +08:00
2018-04-13 17:19:50 +08:00
case ExpandedState.Expanded:
this.ResizeTo(new Vector2(EXPANDED_WIDTH, Height), 500, Easing.OutQuint);
break;
}
StateChanged?.Invoke(State);
}
}
private Drawable lastHoveredButton;
private Drawable hoveredButton => content.Children.FirstOrDefault(c => c.IsHovered);
private void queueExpandIfHovering()
{
// only expand when we hover a different button.
if (lastHoveredButton == hoveredButton) return;
if (!IsHovered) return;
if (State != ExpandedState.Expanded)
{
expandEvent?.Cancel();
expandEvent = Scheduler.AddDelayed(() => State = ExpandedState.Expanded, 750);
}
lastHoveredButton = hoveredButton;
}
}
public enum ExpandedState
{
Contracted,
Expanded,
}
}