1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-25 01:32:56 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuMenu.cs

156 lines
4.9 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
2022-06-17 15:37:17 +08:00
#nullable disable
2021-08-06 19:58:53 +08:00
using osu.Framework.Allocation;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
2019-06-21 11:33:49 +08:00
using osu.Game.Graphics.Containers;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.UserInterface
{
2022-11-24 13:32:20 +08:00
public partial class OsuMenu : Menu
2018-04-13 17:19:50 +08:00
{
2025-02-04 22:21:41 +08:00
protected const double DELAY_BEFORE_FADE_OUT = 50;
protected const double FADE_DURATION = 280;
2021-08-06 19:58:53 +08:00
// todo: this shouldn't be required after https://github.com/ppy/osu-framework/issues/4519 is fixed.
protected bool WasOpened { get; private set; }
public bool PlaySamples { get; }
2021-08-06 19:58:53 +08:00
[Resolved]
private OsuMenuSamples menuSamples { get; set; } = null!;
2018-04-13 17:19:50 +08:00
public OsuMenu(Direction direction, bool topLevelMenu = false)
: this(direction, topLevelMenu, playSamples: !topLevelMenu)
{
}
protected OsuMenu(Direction direction, bool topLevelMenu, bool playSamples)
2018-04-13 17:19:50 +08:00
: base(direction, topLevelMenu)
{
PlaySamples = playSamples;
2018-04-13 17:19:50 +08:00
BackgroundColour = Color4.Black.Opacity(0.5f);
MaskingContainer.CornerRadius = 4;
ItemsContainer.Padding = new MarginPadding(5);
OnSubmenuOpen += _ => menuSamples?.PlaySubOpenSample();
2021-08-06 19:58:53 +08:00
}
protected override void Update()
{
base.Update();
2024-10-04 17:25:21 +08:00
bool showCheckboxes = false;
2024-10-04 17:25:21 +08:00
foreach (var drawableItem in ItemsContainer)
{
if (drawableItem.Item is StatefulMenuItem)
showCheckboxes = true;
}
foreach (var drawableItem in ItemsContainer)
{
if (drawableItem is DrawableOsuMenuItem osuItem)
osuItem.ShowCheckbox.Value = showCheckboxes;
}
}
2021-08-06 19:58:53 +08:00
protected override void AnimateOpen()
{
if (PlaySamples && !WasOpened)
menuSamples?.PlayOpenSample();
2021-08-06 19:58:53 +08:00
WasOpened = true;
this.FadeIn(FADE_DURATION, Easing.OutQuint);
2021-08-06 19:58:53 +08:00
}
protected override void AnimateClose()
{
if (PlaySamples && WasOpened)
menuSamples?.PlayCloseSample();
2021-08-06 19:58:53 +08:00
2025-02-04 22:21:41 +08:00
this.Delay(DELAY_BEFORE_FADE_OUT)
.FadeOut(FADE_DURATION, Easing.OutQuint);
WasOpened = false;
2021-08-06 19:58:53 +08:00
}
2018-04-13 17:19:50 +08:00
protected override void UpdateSize(Vector2 newSize)
{
if (Direction == Direction.Vertical)
{
Width = newSize.X;
if (newSize.Y > 0)
this.ResizeHeightTo(newSize.Y, 300, Easing.OutQuint);
else
// Delay until the fade out finishes from AnimateClose.
this.Delay(DELAY_BEFORE_FADE_OUT + FADE_DURATION).ResizeHeightTo(0);
2018-04-13 17:19:50 +08:00
}
else
{
Height = newSize.Y;
if (newSize.X > 0)
this.ResizeWidthTo(newSize.X, 300, Easing.OutQuint);
else
// Delay until the fade out finishes from AnimateClose.
this.Delay(DELAY_BEFORE_FADE_OUT + FADE_DURATION).ResizeWidthTo(0);
2018-04-13 17:19:50 +08:00
}
}
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item)
{
switch (item)
{
case StatefulMenuItem stateful:
return new DrawableStatefulMenuItem(stateful);
case OsuMenuItemSpacer spacer:
return new DrawableSpacer(spacer);
}
return new DrawableOsuMenuItem(item);
}
2018-04-13 17:19:50 +08:00
protected override ScrollContainer<Drawable> CreateScrollContainer(Direction direction) => new OsuScrollContainer(direction);
2019-06-21 11:33:49 +08:00
2018-04-13 17:19:50 +08:00
protected override Menu CreateSubMenu() => new OsuMenu(Direction.Vertical)
{
Anchor = Direction == Direction.Horizontal ? Anchor.BottomLeft : Anchor.TopRight
};
protected partial class DrawableSpacer : DrawableOsuMenuItem
{
public DrawableSpacer(MenuItem item)
: base(item)
{
Scale = new Vector2(1, 0.6f);
AddInternal(new Box
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Colour = BackgroundColourHover,
RelativeSizeAxes = Axes.X,
Height = 2f,
Width = 0.9f,
});
}
protected override bool OnHover(HoverEvent e) => true;
protected override bool OnClick(ClickEvent e) => true;
}
2018-04-13 17:19:50 +08:00
}
}