1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 13:57:50 +08:00
osu-lazer/osu.Game/Overlays/ExpandingButtonContainer.cs

142 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.Graphics;
using osu.Framework.Graphics.Containers;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Testing;
2018-04-13 17:19:50 +08:00
using osu.Framework.Threading;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
2020-09-03 15:29:15 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays
2018-04-13 17:19:50 +08:00
{
public abstract class ExpandingButtonContainer : Container, IStateful<ExpandedState>
2018-04-13 17:19:50 +08:00
{
private readonly float contractedWidth;
private readonly float expandedWidth;
2018-04-13 17:19:50 +08:00
public event Action<ExpandedState> StateChanged;
protected override Container<Drawable> Content => FillFlow;
2018-04-13 17:19:50 +08:00
protected FillFlowContainer FillFlow { get; }
protected ExpandingButtonContainer(float contractedWidth, float expandedWidth)
2018-04-13 17:19:50 +08:00
{
this.contractedWidth = contractedWidth;
this.expandedWidth = expandedWidth;
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.Y;
Width = contractedWidth;
2018-04-13 17:19:50 +08:00
InternalChildren = new Drawable[]
{
new SidebarScrollContainer
{
Children = new[]
{
FillFlow = new FillFlowContainer
2018-04-13 17:19:50 +08:00
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
}
}
},
};
}
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();
hoveredButton = null;
2018-04-13 17:19:50 +08:00
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(contractedWidth, Height), 500, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
break;
2019-04-01 11:44:46 +08:00
2018-04-13 17:19:50 +08:00
case ExpandedState.Expanded:
this.ResizeTo(new Vector2(expandedWidth, Height), 500, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
break;
}
StateChanged?.Invoke(State);
}
}
private Drawable hoveredButton;
2018-04-13 17:19:50 +08:00
private void queueExpandIfHovering()
{
// if the same button is hovered, let the scheduled expand play out..
if (hoveredButton?.IsHovered == true)
return;
2018-04-13 17:19:50 +08:00
// ..otherwise check whether a new button is hovered, and if so, queue a new hover operation.
2018-04-13 17:19:50 +08:00
// usually we wouldn't use ChildrenOfType in implementations, but this is the simplest way
// to handle cases like the editor where the buttons may be nested within a child hierarchy.
hoveredButton = FillFlow.ChildrenOfType<OsuButton>().FirstOrDefault(c => c.IsHovered);
expandEvent?.Cancel();
if (hoveredButton?.IsHovered == true && State != ExpandedState.Expanded)
expandEvent = Scheduler.AddDelayed(() => State = ExpandedState.Expanded, 750);
2018-04-13 17:19:50 +08:00
}
}
2022-01-06 21:56:56 +08:00
public enum ExpandedState
{
Contracted,
Expanded,
}
2018-04-13 17:19:50 +08:00
}