Fix mod panels playing samples when hidden at a higher level

This commit is contained in:
Bartłomiej Dach
2022-05-07 16:35:34 +02:00
parent 81ca534f87
commit b92d95a17a
2 changed files with 13 additions and 4 deletions

View File

@ -10,6 +10,7 @@ namespace osu.Game.Audio
/// <summary> /// <summary>
/// Allows a component to disable sample playback dynamically as required. /// Allows a component to disable sample playback dynamically as required.
/// Automatically handled by <see cref="PausableSkinnableSound"/>. /// Automatically handled by <see cref="PausableSkinnableSound"/>.
/// May also be manually handled locally to particular components.
/// </summary> /// </summary>
[Cached] [Cached]
public interface ISamplePlaybackDisabler public interface ISamplePlaybackDisabler

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.
#nullable enable
using System; using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio; using osu.Framework.Audio;
@ -12,6 +14,7 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Audio;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
@ -21,8 +24,6 @@ using osu.Game.Rulesets.UI;
using osuTK; using osuTK;
using osuTK.Input; using osuTK.Input;
#nullable enable
namespace osu.Game.Overlays.Mods namespace osu.Game.Overlays.Mods
{ {
public class ModPanel : OsuClickableContainer public class ModPanel : OsuClickableContainer
@ -50,6 +51,7 @@ namespace osu.Game.Overlays.Mods
private Colour4 activeColour; private Colour4 activeColour;
private readonly Bindable<bool> samplePlaybackDisabled = new BindableBool();
private Sample? sampleOff; private Sample? sampleOff;
private Sample? sampleOn; private Sample? sampleOn;
@ -139,13 +141,16 @@ namespace osu.Game.Overlays.Mods
Action = Active.Toggle; Action = Active.Toggle;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader(true)]
private void load(AudioManager audio, OsuColour colours) private void load(AudioManager audio, OsuColour colours, ISamplePlaybackDisabler? samplePlaybackDisabler)
{ {
sampleOn = audio.Samples.Get(@"UI/check-on"); sampleOn = audio.Samples.Get(@"UI/check-on");
sampleOff = audio.Samples.Get(@"UI/check-off"); sampleOff = audio.Samples.Get(@"UI/check-off");
activeColour = colours.ForModType(Mod.Type); activeColour = colours.ForModType(Mod.Type);
if (samplePlaybackDisabler != null)
((IBindable<bool>)samplePlaybackDisabled).BindTo(samplePlaybackDisabler.SamplePlaybackDisabled);
} }
protected override HoverSounds CreateHoverSounds(HoverSampleSet sampleSet) => new HoverSounds(sampleSet); protected override HoverSounds CreateHoverSounds(HoverSampleSet sampleSet) => new HoverSounds(sampleSet);
@ -166,6 +171,9 @@ namespace osu.Game.Overlays.Mods
private void playStateChangeSamples() private void playStateChangeSamples()
{ {
if (samplePlaybackDisabled.Value)
return;
if (Active.Value) if (Active.Value)
sampleOn?.Play(); sampleOn?.Play();
else else