1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 11:57:30 +08:00
osu-lazer/osu.Game/Graphics/Containers/ExpandingContainer.cs

106 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.
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Framework.Threading;
2022-02-14 16:51:39 +08:00
namespace osu.Game.Graphics.Containers
{
/// <summary>
/// Represents a <see cref="Container"/> with the ability to expand/contract on hover.
/// </summary>
2022-11-24 13:32:20 +08:00
public partial class ExpandingContainer : Container, IExpandingContainer
{
private readonly float contractedWidth;
private readonly float expandedWidth;
public BindableBool Expanded { get; } = new BindableBool();
/// <summary>
/// Delay before the container switches to expanded state from hover.
/// </summary>
protected virtual double HoverExpansionDelay => 0;
protected virtual bool ExpandOnHover => true;
protected override Container<Drawable> Content => FillFlow;
protected FillFlowContainer FillFlow { get; }
protected ExpandingContainer(float contractedWidth, float expandedWidth)
{
this.contractedWidth = contractedWidth;
this.expandedWidth = expandedWidth;
RelativeSizeAxes = Axes.Y;
Width = contractedWidth;
InternalChild = new OsuScrollContainer
{
RelativeSizeAxes = Axes.Both,
ScrollbarVisible = false,
Child = FillFlow = new FillFlowContainer
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
},
};
}
private ScheduledDelegate? hoverExpandEvent;
protected override void LoadComplete()
{
base.LoadComplete();
Expanded.BindValueChanged(v =>
{
this.ResizeWidthTo(v.NewValue ? expandedWidth : contractedWidth, 500, Easing.OutQuint);
}, true);
}
protected override bool OnHover(HoverEvent e)
{
updateHoverExpansion();
return true;
}
protected override bool OnMouseMove(MouseMoveEvent e)
{
updateHoverExpansion();
return base.OnMouseMove(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
if (hoverExpandEvent != null)
{
hoverExpandEvent?.Cancel();
hoverExpandEvent = null;
Expanded.Value = false;
return;
}
base.OnHoverLost(e);
}
private void updateHoverExpansion()
{
if (!ExpandOnHover)
return;
hoverExpandEvent?.Cancel();
if (IsHovered && !Expanded.Value)
hoverExpandEvent = Scheduler.AddDelayed(() => Expanded.Value = true, HoverExpansionDelay);
}
}
}