1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 15:03:13 +08:00

Add open/close sounds to context menus

This commit is contained in:
Jamie Taylor 2021-08-06 20:59:26 +09:00
parent 4620ba8787
commit 52400961f6
No known key found for this signature in database
GPG Key ID: 2ACFA8B6370B8C8C
3 changed files with 100 additions and 16 deletions

View File

@ -1,6 +1,8 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -68,13 +70,40 @@ namespace osu.Game.Tests.Visual.UserInterface
); );
} }
private class MyContextMenuContainer : Container, IHasContextMenu private static MenuItem[] makeMenu()
{ {
public MenuItem[] ContextMenuItems => new MenuItem[] return new MenuItem[]
{ {
new OsuMenuItem(@"Some option"), new OsuMenuItem(@"Some option"),
new OsuMenuItem(@"Highlighted option", MenuItemType.Highlighted), new OsuMenuItem(@"Highlighted option", MenuItemType.Highlighted),
new OsuMenuItem(@"Another option"), new OsuMenuItem(@"Another option"),
new OsuMenuItem(@"Nested option >")
{
Items = new MenuItem[]
{
new OsuMenuItem(@"Sub-One"),
new OsuMenuItem(@"Sub-Two"),
new OsuMenuItem(@"Sub-Three"),
new OsuMenuItem(@"Sub-Nested option >")
{
Items = new MenuItem[]
{
new OsuMenuItem(@"Double Sub-One"),
new OsuMenuItem(@"Double Sub-Two"),
new OsuMenuItem(@"Double Sub-Three"),
new OsuMenuItem(@"Sub-Sub-Nested option >")
{
Items = new MenuItem[]
{
new OsuMenuItem(@"Too Deep One"),
new OsuMenuItem(@"Too Deep Two"),
new OsuMenuItem(@"Too Deep Three"),
}
}
}
}
}
},
new OsuMenuItem(@"Choose me please"), new OsuMenuItem(@"Choose me please"),
new OsuMenuItem(@"And me too"), new OsuMenuItem(@"And me too"),
new OsuMenuItem(@"Trying to fill"), new OsuMenuItem(@"Trying to fill"),
@ -82,17 +111,29 @@ namespace osu.Game.Tests.Visual.UserInterface
}; };
} }
private class MyContextMenuContainer : Container, IHasContextMenu
{
public MenuItem[] ContextMenuItems => makeMenu();
}
private class AnotherContextMenuContainer : Container, IHasContextMenu private class AnotherContextMenuContainer : Container, IHasContextMenu
{ {
public MenuItem[] ContextMenuItems => new MenuItem[] public MenuItem[] ContextMenuItems
{ {
new OsuMenuItem(@"Simple option"), get
new OsuMenuItem(@"Simple very very long option"), {
new OsuMenuItem(@"Change width", MenuItemType.Highlighted, () => this.ResizeWidthTo(Width * 2, 100, Easing.OutQuint)), List<MenuItem> items = makeMenu().ToList();
new OsuMenuItem(@"Change height", MenuItemType.Highlighted, () => this.ResizeHeightTo(Height * 2, 100, Easing.OutQuint)), items.AddRange(new MenuItem[]
new OsuMenuItem(@"Change width back", MenuItemType.Destructive, () => this.ResizeWidthTo(Width / 2, 100, Easing.OutQuint)), {
new OsuMenuItem(@"Change height back", MenuItemType.Destructive, () => this.ResizeHeightTo(Height / 2, 100, Easing.OutQuint)), new OsuMenuItem(@"Change width", MenuItemType.Highlighted, () => this.ResizeWidthTo(Width * 2, 100, Easing.OutQuint)),
}; new OsuMenuItem(@"Change height", MenuItemType.Highlighted, () => this.ResizeHeightTo(Height * 2, 100, Easing.OutQuint)),
new OsuMenuItem(@"Change width back", MenuItemType.Destructive, () => this.ResizeWidthTo(Width / 2, 100, Easing.OutQuint)),
new OsuMenuItem(@"Change height back", MenuItemType.Destructive, () => this.ResizeHeightTo(Height / 2, 100, Easing.OutQuint)),
});
return items.ToArray();
}
}
} }
} }
} }

View File

@ -9,6 +9,6 @@ namespace osu.Game.Graphics.Cursor
{ {
public class OsuContextMenuContainer : ContextMenuContainer public class OsuContextMenuContainer : ContextMenuContainer
{ {
protected override Menu CreateMenu() => new OsuContextMenu(); protected override Menu CreateMenu() => new OsuContextMenu(true);
} }
} }

View File

@ -3,6 +3,9 @@
using osuTK.Graphics; using osuTK.Graphics;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Extensions;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Effects; using osu.Framework.Graphics.Effects;
@ -13,8 +16,16 @@ namespace osu.Game.Graphics.UserInterface
public class OsuContextMenu : OsuMenu public class OsuContextMenu : OsuMenu
{ {
private const int fade_duration = 250; private const int fade_duration = 250;
private Sample sampleOpen;
private Sample sampleClose;
private Sample sampleClick;
public OsuContextMenu() // todo: this shouldn't be required after https://github.com/ppy/osu-framework/issues/4519 is fixed.
private bool wasOpened;
private readonly bool playClickSample;
private readonly Menu parentMenu;
public OsuContextMenu(bool playClickSample = false, Menu parentMenu = null)
: base(Direction.Vertical) : base(Direction.Vertical)
{ {
MaskingContainer.CornerRadius = 5; MaskingContainer.CornerRadius = 5;
@ -28,17 +39,49 @@ namespace osu.Game.Graphics.UserInterface
ItemsContainer.Padding = new MarginPadding { Vertical = DrawableOsuMenuItem.MARGIN_VERTICAL }; ItemsContainer.Padding = new MarginPadding { Vertical = DrawableOsuMenuItem.MARGIN_VERTICAL };
MaxHeight = 250; MaxHeight = 250;
this.playClickSample = playClickSample;
this.parentMenu = parentMenu;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colours) private void load(OsuColour colours, AudioManager audio)
{ {
BackgroundColour = colours.ContextMenuGray; BackgroundColour = colours.ContextMenuGray;
sampleClick = audio.Samples.Get($"UI/{HoverSampleSet.Default.GetDescription()}-select");
sampleOpen = audio.Samples.Get(@"UI/dropdown-open");
sampleClose = audio.Samples.Get(@"UI/dropdown-close");
} }
protected override void AnimateOpen() => this.FadeIn(fade_duration, Easing.OutQuint); protected override void AnimateOpen()
protected override void AnimateClose() => this.FadeOut(fade_duration, Easing.OutQuint); {
this.FadeIn(fade_duration, Easing.OutQuint);
protected override Menu CreateSubMenu() => new OsuContextMenu(); if (playClickSample)
sampleClick?.Play();
if (!wasOpened)
sampleOpen?.Play();
wasOpened = true;
}
protected override void AnimateClose()
{
this.FadeOut(fade_duration, Easing.OutQuint);
if (parentMenu?.State == MenuState.Closed)
{
wasOpened = false;
return;
}
if (wasOpened)
sampleClose?.Play();
wasOpened = false;
}
protected override Menu CreateSubMenu() => new OsuContextMenu(false, this);
} }
} }