From 58cf1c11e4721c85b554ccd08bf3b64ca62b2395 Mon Sep 17 00:00:00 2001 From: Jamie Taylor Date: Thu, 28 Nov 2024 06:41:43 +0900 Subject: [PATCH 1/4] Improve menu/context-menu sample playback --- .../UserInterface/TestSceneContextMenu.cs | 11 ++- .../Cursor/OsuContextMenuContainer.cs | 8 --- .../Graphics/UserInterface/HoverSampleSet.cs | 5 +- .../Graphics/UserInterface/OsuContextMenu.cs | 15 ++-- .../UserInterface/OsuContextMenuSamples.cs | 37 ---------- osu.Game/Graphics/UserInterface/OsuMenu.cs | 19 ++--- .../Graphics/UserInterface/OsuMenuSamples.cs | 70 +++++++++++++++++++ osu.Game/OsuGameBase.cs | 5 ++ .../Edit/Components/Menus/EditorMenuBar.cs | 2 +- 9 files changed, 103 insertions(+), 69 deletions(-) delete mode 100644 osu.Game/Graphics/UserInterface/OsuContextMenuSamples.cs create mode 100644 osu.Game/Graphics/UserInterface/OsuMenuSamples.cs diff --git a/osu.Game.Tests/Visual/UserInterface/TestSceneContextMenu.cs b/osu.Game.Tests/Visual/UserInterface/TestSceneContextMenu.cs index 2a2f267fc8af..118e37dab4d5 100644 --- a/osu.Game.Tests/Visual/UserInterface/TestSceneContextMenu.cs +++ b/osu.Game.Tests/Visual/UserInterface/TestSceneContextMenu.cs @@ -101,7 +101,16 @@ private static MenuItem[] makeMenu() } } } - } + }, + } + }, + new OsuMenuItem(@"Another nested option") + { + Items = new MenuItem[] + { + new OsuMenuItem(@"Sub-One"), + new OsuMenuItem(@"Sub-Two"), + new OsuMenuItem(@"Sub-Three"), } }, new OsuMenuItem(@"Choose me please"), diff --git a/osu.Game/Graphics/Cursor/OsuContextMenuContainer.cs b/osu.Game/Graphics/Cursor/OsuContextMenuContainer.cs index 7b21a413f7a2..3180661b0caa 100644 --- a/osu.Game/Graphics/Cursor/OsuContextMenuContainer.cs +++ b/osu.Game/Graphics/Cursor/OsuContextMenuContainer.cs @@ -11,16 +11,8 @@ namespace osu.Game.Graphics.Cursor [Cached(typeof(OsuContextMenuContainer))] public partial class OsuContextMenuContainer : ContextMenuContainer { - [Cached] - private OsuContextMenuSamples samples = new OsuContextMenuSamples(); - private OsuContextMenu menu = null!; - public OsuContextMenuContainer() - { - AddInternal(samples); - } - protected override Menu CreateMenu() => menu = new OsuContextMenu(true); public void CloseMenu() diff --git a/osu.Game/Graphics/UserInterface/HoverSampleSet.cs b/osu.Game/Graphics/UserInterface/HoverSampleSet.cs index 5b0fbc693eee..62eb765cc80c 100644 --- a/osu.Game/Graphics/UserInterface/HoverSampleSet.cs +++ b/osu.Game/Graphics/UserInterface/HoverSampleSet.cs @@ -23,6 +23,9 @@ public enum HoverSampleSet DialogCancel, [Description("dialog-ok")] - DialogOk + DialogOk, + + [Description("menu-open")] + MenuOpen, } } diff --git a/osu.Game/Graphics/UserInterface/OsuContextMenu.cs b/osu.Game/Graphics/UserInterface/OsuContextMenu.cs index 96797e5d0153..7a5d2c369b55 100644 --- a/osu.Game/Graphics/UserInterface/OsuContextMenu.cs +++ b/osu.Game/Graphics/UserInterface/OsuContextMenu.cs @@ -15,7 +15,7 @@ public partial class OsuContextMenu : OsuMenu private const int fade_duration = 250; [Resolved] - private OsuContextMenuSamples samples { get; set; } = null!; + 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; @@ -47,15 +47,14 @@ private void load(OsuColour colours) protected override void AnimateOpen() { + wasOpened = true; this.FadeIn(fade_duration, Easing.OutQuint); - if (playClickSample) - samples.PlayClickSample(); - - if (!wasOpened) - samples.PlayOpenSample(); + if (!playClickSample) + return; - wasOpened = true; + menuSamples?.PlayClickSample(); + menuSamples?.PlayOpenSample(); } protected override void AnimateClose() @@ -63,7 +62,7 @@ protected override void AnimateClose() this.FadeOut(fade_duration, Easing.OutQuint); if (wasOpened) - samples.PlayCloseSample(); + menuSamples?.PlayCloseSample(); wasOpened = false; } diff --git a/osu.Game/Graphics/UserInterface/OsuContextMenuSamples.cs b/osu.Game/Graphics/UserInterface/OsuContextMenuSamples.cs deleted file mode 100644 index 6d7543c4727b..000000000000 --- a/osu.Game/Graphics/UserInterface/OsuContextMenuSamples.cs +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. -// See the LICENCE file in the repository root for full licence text. - -#nullable disable - -using osu.Framework.Allocation; -using osu.Framework.Audio; -using osu.Framework.Audio.Sample; -using osu.Framework.Extensions; -using osu.Framework.Graphics; - -namespace osu.Game.Graphics.UserInterface -{ - public partial class OsuContextMenuSamples : Component - { - private Sample sampleClick; - private Sample sampleOpen; - private Sample sampleClose; - - [BackgroundDependencyLoader] - private void load(AudioManager audio) - { - sampleClick = audio.Samples.Get($@"UI/{HoverSampleSet.Default.GetDescription()}-select"); - sampleOpen = audio.Samples.Get(@"UI/dropdown-open"); - sampleClose = audio.Samples.Get(@"UI/dropdown-close"); - } - - public void PlayClickSample() => Scheduler.AddOnce(playClickSample); - private void playClickSample() => sampleClick.Play(); - - public void PlayOpenSample() => Scheduler.AddOnce(playOpenSample); - private void playOpenSample() => sampleOpen.Play(); - - public void PlayCloseSample() => Scheduler.AddOnce(playCloseSample); - private void playCloseSample() => sampleClose.Play(); - } -} diff --git a/osu.Game/Graphics/UserInterface/OsuMenu.cs b/osu.Game/Graphics/UserInterface/OsuMenu.cs index 6e7dad2b5f7a..7cc1bab25f6d 100644 --- a/osu.Game/Graphics/UserInterface/OsuMenu.cs +++ b/osu.Game/Graphics/UserInterface/OsuMenu.cs @@ -4,8 +4,6 @@ #nullable disable using osu.Framework.Allocation; -using osu.Framework.Audio; -using osu.Framework.Audio.Sample; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; @@ -20,12 +18,12 @@ namespace osu.Game.Graphics.UserInterface { public partial class OsuMenu : Menu { - private Sample sampleOpen; - private Sample sampleClose; - // todo: this shouldn't be required after https://github.com/ppy/osu-framework/issues/4519 is fixed. private bool wasOpened; + [Resolved] + private OsuMenuSamples menuSamples { get; set; } = null!; + public OsuMenu(Direction direction, bool topLevelMenu = false) : base(direction, topLevelMenu) { @@ -33,13 +31,8 @@ public OsuMenu(Direction direction, bool topLevelMenu = false) MaskingContainer.CornerRadius = 4; ItemsContainer.Padding = new MarginPadding(5); - } - [BackgroundDependencyLoader] - private void load(AudioManager audio) - { - sampleOpen = audio.Samples.Get(@"UI/dropdown-open"); - sampleClose = audio.Samples.Get(@"UI/dropdown-close"); + OnSubmenuOpen += _ => { menuSamples?.PlaySubOpenSample(); }; } protected override void Update() @@ -64,7 +57,7 @@ protected override void Update() protected override void AnimateOpen() { if (!TopLevelMenu && !wasOpened) - sampleOpen?.Play(); + menuSamples?.PlayOpenSample(); this.FadeIn(300, Easing.OutQuint); wasOpened = true; @@ -73,7 +66,7 @@ protected override void AnimateOpen() protected override void AnimateClose() { if (!TopLevelMenu && wasOpened) - sampleClose?.Play(); + menuSamples?.PlayCloseSample(); this.FadeOut(300, Easing.OutQuint); wasOpened = false; diff --git a/osu.Game/Graphics/UserInterface/OsuMenuSamples.cs b/osu.Game/Graphics/UserInterface/OsuMenuSamples.cs new file mode 100644 index 000000000000..779671b6ad04 --- /dev/null +++ b/osu.Game/Graphics/UserInterface/OsuMenuSamples.cs @@ -0,0 +1,70 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +#nullable disable + +using osu.Framework.Allocation; +using osu.Framework.Audio; +using osu.Framework.Audio.Sample; +using osu.Framework.Graphics; + +namespace osu.Game.Graphics.UserInterface +{ + public partial class OsuMenuSamples : Component + { + private Sample sampleClick; + private Sample sampleOpen; + private Sample sampleSubOpen; + private Sample sampleClose; + + private bool triggerOpen; + private bool triggerSubOpen; + private bool triggerClose; + + [BackgroundDependencyLoader] + private void load(AudioManager audio) + { + sampleClick = audio.Samples.Get(@"UI/menu-open-select"); + sampleOpen = audio.Samples.Get(@"UI/menu-open"); + sampleSubOpen = audio.Samples.Get(@"UI/menu-sub-open"); + sampleClose = audio.Samples.Get(@"UI/menu-close"); + } + + public void PlayClickSample() + { + Scheduler.AddOnce(playClickSample); + } + + public void PlayOpenSample() + { + triggerOpen = true; + Scheduler.AddOnce(resolvePlayback); + } + + public void PlaySubOpenSample() + { + triggerSubOpen = true; + Scheduler.AddOnce(resolvePlayback); + } + + public void PlayCloseSample() + { + triggerClose = true; + Scheduler.AddOnce(resolvePlayback); + } + + private void playClickSample() => sampleClick.Play(); + + private void resolvePlayback() + { + if (triggerSubOpen) + sampleSubOpen?.Play(); + else if (triggerOpen) + sampleOpen?.Play(); + else if (triggerClose) + sampleClose?.Play(); + + triggerOpen = triggerSubOpen = triggerClose = false; + } + } +} diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index dc13924b4fea..0f9848caccbe 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -41,6 +41,7 @@ using osu.Game.Extensions; using osu.Game.Graphics; using osu.Game.Graphics.Cursor; +using osu.Game.Graphics.UserInterface; using osu.Game.Input; using osu.Game.Input.Bindings; using osu.Game.IO; @@ -385,6 +386,10 @@ private void load(ReadableKeyCombinationProvider keyCombinationProvider, Framewo GlobalActionContainer globalBindings; + OsuMenuSamples menuSamples; + dependencies.Cache(menuSamples = new OsuMenuSamples()); + base.Content.Add(menuSamples); + base.Content.Add(SafeAreaContainer = new SafeAreaContainer { SafeAreaOverrideEdges = SafeAreaOverrideEdges, diff --git a/osu.Game/Screens/Edit/Components/Menus/EditorMenuBar.cs b/osu.Game/Screens/Edit/Components/Menus/EditorMenuBar.cs index 47a13dcfbacd..76b8811b899e 100644 --- a/osu.Game/Screens/Edit/Components/Menus/EditorMenuBar.cs +++ b/osu.Game/Screens/Edit/Components/Menus/EditorMenuBar.cs @@ -99,7 +99,7 @@ private void load(OverlayColourProvider colourProvider) ForegroundColourHover = colourProvider.Content1; BackgroundColourHover = colourProvider.Background1; - AddInternal(hoverClickSounds = new HoverClickSounds()); + AddInternal(hoverClickSounds = new HoverClickSounds(HoverSampleSet.MenuOpen)); } protected override void LoadComplete() From 84a85000afab3f37985ef59e3efe03b0bdd69d36 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 5 Dec 2024 21:07:08 +0900 Subject: [PATCH 2/4] Update framework --- osu.Android.props | 2 +- osu.Game/osu.Game.csproj | 2 +- osu.iOS.props | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/osu.Android.props b/osu.Android.props index 02898623a91d..ebfb13615068 100644 --- a/osu.Android.props +++ b/osu.Android.props @@ -10,7 +10,7 @@ true - + diff --git a/osu.iOS.props b/osu.iOS.props index 80e695e5d161..252c7a14c49c 100644 --- a/osu.iOS.props +++ b/osu.iOS.props @@ -17,6 +17,6 @@ -all - + From 49f4b0e6eff6423758382a5c7d79aed21e9c7077 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 5 Dec 2024 21:07:13 +0900 Subject: [PATCH 3/4] Update resources --- osu.Game/osu.Game.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 43353370b72d..7b4453b01505 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -36,7 +36,7 @@ - + From 69014550b5499a1bc60bcd6144a43180b94ff5ae Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 6 Dec 2024 12:48:06 +0900 Subject: [PATCH 4/4] Remove unnecessary null checks --- osu.Game/Graphics/UserInterface/OsuContextMenu.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/osu.Game/Graphics/UserInterface/OsuContextMenu.cs b/osu.Game/Graphics/UserInterface/OsuContextMenu.cs index 7a5d2c369b55..433d37834f8c 100644 --- a/osu.Game/Graphics/UserInterface/OsuContextMenu.cs +++ b/osu.Game/Graphics/UserInterface/OsuContextMenu.cs @@ -53,8 +53,8 @@ protected override void AnimateOpen() if (!playClickSample) return; - menuSamples?.PlayClickSample(); - menuSamples?.PlayOpenSample(); + menuSamples.PlayClickSample(); + menuSamples.PlayOpenSample(); } protected override void AnimateClose() @@ -62,7 +62,7 @@ protected override void AnimateClose() this.FadeOut(fade_duration, Easing.OutQuint); if (wasOpened) - menuSamples?.PlayCloseSample(); + menuSamples.PlayCloseSample(); wasOpened = false; }