Skip to content

Commit 85ef743

Browse files
authored
Merge pull request #32450 from peppy/no-pause-loop-when-inactive-window
Fade out pause loop sound when the game window is inactive
2 parents 8b566d2 + c1604a7 commit 85ef743

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

osu.Game/Screens/Play/PauseOverlay.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
using System.Linq;
66
using osu.Framework.Allocation;
77
using osu.Framework.Audio;
8+
using osu.Framework.Bindables;
89
using osu.Framework.Graphics;
10+
using osu.Framework.Graphics.Containers;
911
using osu.Framework.Input.Events;
1012
using osu.Framework.Localisation;
13+
using osu.Framework.Platform;
1114
using osu.Game.Audio;
1215
using osu.Game.Input.Bindings;
1316
using osu.Game.Localisation;
@@ -31,14 +34,29 @@ public partial class PauseOverlay : GameplayMenuOverlay
3134
OnResume?.Invoke();
3235
};
3336

37+
private readonly IBindable<bool> windowActive = new Bindable<bool>(true);
38+
39+
private float targetVolume => windowActive.Value && State.Value == Visibility.Visible ? 1.0f : 0;
40+
3441
[BackgroundDependencyLoader]
35-
private void load()
42+
private void load(GameHost? host)
3643
{
3744
AddInternal(pauseLoop = new SkinnableSound(new SampleInfo("Gameplay/pause-loop"))
3845
{
3946
Looping = true,
4047
Volume = { Value = 0 }
4148
});
49+
50+
if (host != null)
51+
windowActive.BindTo(host.IsActive);
52+
}
53+
54+
protected override void LoadComplete()
55+
{
56+
base.LoadComplete();
57+
58+
// Schedule required because host.IsActive doesn't seem to always run on the update thread.
59+
windowActive.BindValueChanged(_ => Schedule(() => pauseLoop.VolumeTo(targetVolume, 1000, Easing.Out)));
4260
}
4361

4462
public void StopAllSamples()
@@ -53,15 +71,15 @@ protected override void PopIn()
5371
{
5472
base.PopIn();
5573

56-
pauseLoop.VolumeTo(1.0f, TRANSITION_DURATION, Easing.InQuint);
74+
pauseLoop.VolumeTo(targetVolume, TRANSITION_DURATION, Easing.InQuint);
5775
pauseLoop.Play();
5876
}
5977

6078
protected override void PopOut()
6179
{
6280
base.PopOut();
6381

64-
pauseLoop.VolumeTo(0, TRANSITION_DURATION, Easing.OutQuad).Finally(_ => pauseLoop.Stop());
82+
pauseLoop.VolumeTo(targetVolume, TRANSITION_DURATION, Easing.OutQuad).Finally(_ => pauseLoop.Stop());
6583
}
6684

6785
public override bool OnPressed(KeyBindingPressEvent<GlobalAction> e)

0 commit comments

Comments
 (0)