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
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
2 changed files with 13 additions and 4 deletions

View File

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

View File

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