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

Using BindableBool to mute the volume when it's value changes

This commit is contained in:
aQaTL 2018-01-18 17:23:02 +01:00
parent 9c09b33e4e
commit 4a85266fca
No known key found for this signature in database
GPG Key ID: 181719411A8555F0
2 changed files with 16 additions and 25 deletions

View File

@ -14,8 +14,6 @@ namespace osu.Game.Graphics.UserInterface.Volume
{ {
public class VolumeControl : OverlayContainer public class VolumeControl : OverlayContainer
{ {
private AudioManager audio;
private readonly VolumeMeter volumeMeterMaster; private readonly VolumeMeter volumeMeterMaster;
private readonly IconButton muteIcon; private readonly IconButton muteIcon;
@ -42,13 +40,7 @@ namespace osu.Game.Graphics.UserInterface.Volume
{ {
Icon = FontAwesome.fa_volume_up, Icon = FontAwesome.fa_volume_up,
Scale = new Vector2(2.0f), Scale = new Vector2(2.0f),
Action = () => Action = () => Adjust(GlobalAction.ToggleMute),
{
if (IsMuted)
Unmute();
else
Mute();
},
}, },
volumeMeterMaster = new VolumeMeter("Master"), volumeMeterMaster = new VolumeMeter("Master"),
volumeMeterEffect = new VolumeMeter("Effects"), volumeMeterEffect = new VolumeMeter("Effects"),
@ -113,35 +105,36 @@ namespace osu.Game.Graphics.UserInterface.Volume
private readonly BindableDouble muteBindable = new BindableDouble(); private readonly BindableDouble muteBindable = new BindableDouble();
public bool IsMuted { get; private set; } private readonly BindableBool muted = new BindableBool();
public bool IsMuted => muted.Value;
public void Mute() public void Mute()
{ {
if (IsMuted) muted.Value = true;
return;
audio.AddAdjustment(AdjustableProperty.Volume, muteBindable);
IsMuted = true;
muteIcon.Icon = FontAwesome.fa_volume_off;
} }
public void Unmute() public void Unmute()
{ {
if (!IsMuted) muted.Value = false;
return;
audio.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
IsMuted = false;
muteIcon.Icon = FontAwesome.fa_volume_up;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(AudioManager audio) private void load(AudioManager audio)
{ {
this.audio = audio;
volumeMeterMaster.Bindable.BindTo(audio.Volume); volumeMeterMaster.Bindable.BindTo(audio.Volume);
volumeMeterEffect.Bindable.BindTo(audio.VolumeSample); volumeMeterEffect.Bindable.BindTo(audio.VolumeSample);
volumeMeterMusic.Bindable.BindTo(audio.VolumeTrack); volumeMeterMusic.Bindable.BindTo(audio.VolumeTrack);
muted.ValueChanged += mute =>
{
if (mute)
audio.AddAdjustment(AdjustableProperty.Volume, muteBindable);
else
audio.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
muteIcon.Icon = mute ? FontAwesome.fa_volume_off : FontAwesome.fa_volume_up;
};
} }
private ScheduledDelegate popOutDelegate; private ScheduledDelegate popOutDelegate;

View File

@ -405,10 +405,8 @@ namespace osu.Game
{ {
base.OnActivated(); base.OnActivated();
if (IsLoaded && muteWhenInactive && !wasMuted) if (IsLoaded && muteWhenInactive && !wasMuted)
{
volume.Unmute(); volume.Unmute();
} }
}
public bool OnReleased(GlobalAction action) => false; public bool OnReleased(GlobalAction action) => false;