1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-14 05:47:20 +08:00

Refactor OsuContextMenu to avoid code duplication

This commit is contained in:
Dean Herbert 2025-02-05 16:24:13 +09:00
parent 099ce39531
commit 2356d3e2d0
No known key found for this signature in database
2 changed files with 23 additions and 36 deletions

View File

@ -15,12 +15,8 @@ namespace osu.Game.Graphics.UserInterface
[Resolved]
private OsuMenuSamples menuSamples { get; set; } = null!;
// todo: this shouldn't be required after https://github.com/ppy/osu-framework/issues/4519 is fixed.
private bool wasOpened;
private readonly bool playClickSample;
public OsuContextMenu(bool playClickSample = false)
: base(Direction.Vertical)
public OsuContextMenu(bool playSamples)
: base(Direction.Vertical, topLevelMenu: false, playSamples)
{
MaskingContainer.CornerRadius = 5;
MaskingContainer.EdgeEffect = new EdgeEffectParameters
@ -33,8 +29,6 @@ namespace osu.Game.Graphics.UserInterface
ItemsContainer.Padding = new MarginPadding { Vertical = DrawableOsuMenuItem.MARGIN_VERTICAL };
MaxHeight = 250;
this.playClickSample = playClickSample;
}
[BackgroundDependencyLoader]
@ -45,27 +39,12 @@ namespace osu.Game.Graphics.UserInterface
protected override void AnimateOpen()
{
wasOpened = true;
this.FadeIn(FADE_DURATION, Easing.OutQuint);
if (PlaySamples && !WasOpened)
menuSamples.PlayClickSample();
if (!playClickSample)
return;
menuSamples.PlayClickSample();
menuSamples.PlayOpenSample();
base.AnimateOpen();
}
protected override void AnimateClose()
{
this.Delay(DELAY_BEFORE_FADE_OUT)
.FadeOut(FADE_DURATION, Easing.OutQuint);
if (wasOpened)
menuSamples.PlayCloseSample();
wasOpened = false;
}
protected override Menu CreateSubMenu() => new OsuContextMenu();
protected override Menu CreateSubMenu() => new OsuContextMenu(false); // sub menu samples are handled by OsuMenu.OnSubmenuOpen.
}
}

View File

@ -22,20 +22,28 @@ namespace osu.Game.Graphics.UserInterface
protected const double FADE_DURATION = 280;
// todo: this shouldn't be required after https://github.com/ppy/osu-framework/issues/4519 is fixed.
private bool wasOpened;
protected bool WasOpened { get; private set; }
public bool PlaySamples { get; }
[Resolved]
private OsuMenuSamples menuSamples { get; set; } = null!;
public OsuMenu(Direction direction, bool topLevelMenu = false)
: this(direction, topLevelMenu, playSamples: !topLevelMenu)
{
}
protected OsuMenu(Direction direction, bool topLevelMenu, bool playSamples)
: base(direction, topLevelMenu)
{
PlaySamples = playSamples;
BackgroundColour = Color4.Black.Opacity(0.5f);
MaskingContainer.CornerRadius = 4;
ItemsContainer.Padding = new MarginPadding(5);
OnSubmenuOpen += _ => { menuSamples?.PlaySubOpenSample(); };
OnSubmenuOpen += _ => menuSamples?.PlaySubOpenSample();
}
protected override void Update()
@ -59,22 +67,22 @@ namespace osu.Game.Graphics.UserInterface
protected override void AnimateOpen()
{
if (!TopLevelMenu && !wasOpened)
if (PlaySamples && !WasOpened)
menuSamples?.PlayOpenSample();
this.FadeIn(300, Easing.OutQuint);
wasOpened = true;
WasOpened = true;
this.FadeIn(FADE_DURATION, Easing.OutQuint);
}
protected override void AnimateClose()
{
if (!TopLevelMenu && wasOpened)
if (PlaySamples && WasOpened)
menuSamples?.PlayCloseSample();
this.Delay(DELAY_BEFORE_FADE_OUT)
.FadeOut(FADE_DURATION, Easing.OutQuint);
wasOpened = false;
WasOpened = false;
}
protected override void UpdateSize(Vector2 newSize)
@ -87,7 +95,7 @@ namespace osu.Game.Graphics.UserInterface
this.ResizeHeightTo(newSize.Y, 300, Easing.OutQuint);
else
// Delay until the fade out finishes from AnimateClose.
this.Delay(350).ResizeHeightTo(0);
this.Delay(DELAY_BEFORE_FADE_OUT + FADE_DURATION).ResizeHeightTo(0);
}
else
{
@ -96,7 +104,7 @@ namespace osu.Game.Graphics.UserInterface
this.ResizeWidthTo(newSize.X, 300, Easing.OutQuint);
else
// Delay until the fade out finishes from AnimateClose.
this.Delay(350).ResizeWidthTo(0);
this.Delay(DELAY_BEFORE_FADE_OUT + FADE_DURATION).ResizeWidthTo(0);
}
}