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:
parent
099ce39531
commit
2356d3e2d0
@ -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.
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user