1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 22:52:57 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/Volume/VolumeControl.cs

158 lines
5.2 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-12-06 17:56:20 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Threading;
using OpenTK;
2016-11-23 19:26:46 +08:00
using osu.Framework.Audio;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Game.Input.Bindings;
namespace osu.Game.Graphics.UserInterface.Volume
{
public class VolumeControl : OverlayContainer
{
private readonly VolumeMeter volumeMeterMaster;
2018-01-18 03:43:08 +08:00
private readonly IconButton muteIcon;
protected override bool BlockPassThroughMouse => false;
2016-10-13 03:17:53 +08:00
2016-10-22 16:50:42 +08:00
public VolumeControl()
{
AutoSizeAxes = Axes.Both;
Anchor = Anchor.BottomRight;
Origin = Anchor.BottomRight;
Children = new Drawable[]
{
2017-03-02 02:33:01 +08:00
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.BottomRight,
Origin = Anchor.BottomRight,
Margin = new MarginPadding { Left = 10, Right = 10, Top = 30, Bottom = 30 },
2017-03-02 02:33:01 +08:00
Spacing = new Vector2(15, 0),
Children = new Drawable[]
{
2018-01-31 16:36:53 +08:00
new Container
{
2018-01-31 16:36:53 +08:00
Size = new Vector2(IconButton.BUTTON_SIZE),
Child = muteIcon = new IconButton
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Icon = FontAwesome.fa_volume_up,
Action = () => Adjust(GlobalAction.ToggleMute),
}
},
volumeMeterMaster = new VolumeMeter("Master"),
volumeMeterEffect = new VolumeMeter("Effects"),
volumeMeterMusic = new VolumeMeter("Music")
}
}
};
2016-10-22 16:50:42 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2016-11-29 17:35:40 +08:00
volumeMeterMaster.Bindable.ValueChanged += volumeChanged;
volumeMeterEffect.Bindable.ValueChanged += volumeChanged;
volumeMeterMusic.Bindable.ValueChanged += volumeChanged;
}
2016-10-13 22:52:49 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2016-11-29 17:35:40 +08:00
volumeMeterMaster.Bindable.ValueChanged -= volumeChanged;
volumeMeterEffect.Bindable.ValueChanged -= volumeChanged;
volumeMeterMusic.Bindable.ValueChanged -= volumeChanged;
2016-10-13 22:52:49 +08:00
}
public bool Adjust(GlobalAction action)
{
switch (action)
{
case GlobalAction.DecreaseVolume:
if (State == Visibility.Hidden)
Show();
else
volumeMeterMaster.Decrease();
return true;
case GlobalAction.IncreaseVolume:
if (State == Visibility.Hidden)
Show();
else
volumeMeterMaster.Increase();
return true;
2018-01-17 00:46:54 +08:00
case GlobalAction.ToggleMute:
Show();
muted.Toggle();
2018-01-17 00:46:54 +08:00
return true;
}
return false;
}
private void volumeChanged(double newVolume)
{
Show();
schedulePopOut();
}
private readonly BindableDouble muteBindable = new BindableDouble();
private readonly BindableBool muted = new BindableBool();
2016-11-23 19:26:46 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
2017-02-27 11:24:50 +08:00
volumeMeterMaster.Bindable.BindTo(audio.Volume);
volumeMeterEffect.Bindable.BindTo(audio.VolumeSample);
volumeMeterMusic.Bindable.BindTo(audio.VolumeTrack);
muted.ValueChanged += mute =>
{
if (mute)
{
audio.AddAdjustment(AdjustableProperty.Volume, muteBindable);
muteIcon.Icon = FontAwesome.fa_volume_off;
}
else
{
audio.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
muteIcon.Icon = FontAwesome.fa_volume_up;
}
};
2016-11-23 19:26:46 +08:00
}
2017-03-07 09:59:19 +08:00
private ScheduledDelegate popOutDelegate;
private readonly VolumeMeter volumeMeterEffect;
private readonly VolumeMeter volumeMeterMusic;
protected override void PopIn()
{
2017-02-25 20:12:39 +08:00
ClearTransforms();
this.FadeIn(100);
schedulePopOut();
}
protected override void PopOut()
{
this.FadeOut(100);
}
private void schedulePopOut()
{
popOutDelegate?.Cancel();
this.Delay(1000).Schedule(Hide, out popOutDelegate);
}
}
}