osu/osu.Game/Overlays/VolumeOverlay.cs

151 lines
5.1 KiB
C#
Raw Normal View History

2018-03-03 18:08:35 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Configuration;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
2018-03-03 18:08:35 +00:00
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
2018-03-03 18:08:35 +00:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Threading;
2018-03-03 18:08:35 +00:00
using osu.Game.Graphics;
using osu.Game.Input.Bindings;
2018-03-03 18:08:35 +00:00
using osu.Game.Overlays.Volume;
using OpenTK;
using OpenTK.Graphics;
2018-03-03 18:08:35 +00:00
namespace osu.Game.Overlays
{
2018-03-03 18:08:35 +00:00
public class VolumeOverlay : OverlayContainer
{
2018-03-03 18:08:35 +00:00
private const float offset = 10;
private VolumeMeter volumeMeterMaster;
private VolumeMeter volumeMeterEffect;
private VolumeMeter volumeMeterMusic;
private MuteButton muteButton;
protected override bool BlockPassThroughMouse => false;
2016-10-12 19:17:53 +00:00
2018-03-03 18:08:35 +00:00
private readonly BindableDouble muteAdjustment = new BindableDouble();
[BackgroundDependencyLoader]
private void load(AudioManager audio, OsuColour colours)
2016-10-22 08:50:42 +00:00
{
2018-03-03 18:08:35 +00:00
RelativeSizeAxes = Axes.Both;
2018-03-03 18:08:35 +00:00
AddRange(new Drawable[]
{
2018-03-03 18:08:35 +00:00
new Box
{
RelativeSizeAxes = Axes.Y,
Width = 300,
Colour = new ColourInfo
{
TopLeft = Color4.Black.Opacity(0.5f),
BottomLeft = Color4.Black.Opacity(0.5f),
TopRight = Color4.Black.Opacity(0),
BottomRight = Color4.Black.Opacity(0),
}
},
2017-03-01 18:33:01 +00:00
new FillFlowContainer
{
2018-03-03 18:08:35 +00:00
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Both,
2018-03-03 18:08:35 +00:00
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Spacing = new Vector2(0, offset),
Margin = new MarginPadding { Left = offset },
Children = new Drawable[]
{
2018-03-03 18:08:35 +00:00
volumeMeterEffect = new VolumeMeter("EFFECTS", 125, colours.Blue)
{
2018-03-03 18:08:35 +00:00
Margin = new MarginPadding { Top = 100 + MuteButton.HEIGHT } //to counter the mute button and re-center the volume meters
},
2018-03-03 18:08:35 +00:00
volumeMeterMaster = new VolumeMeter("MASTER", 150, colours.Pink),
volumeMeterMusic = new VolumeMeter("MUSIC", 125, colours.Blue),
muteButton = new MuteButton
{
Margin = new MarginPadding { Top = 100 }
}
}
2018-03-03 18:08:35 +00:00
},
});
volumeMeterMaster.Bindable.BindTo(audio.Volume);
volumeMeterEffect.Bindable.BindTo(audio.VolumeSample);
volumeMeterMusic.Bindable.BindTo(audio.VolumeTrack);
muteButton.Current.ValueChanged += mute =>
{
if (mute)
audio.AddAdjustment(AdjustableProperty.Volume, muteAdjustment);
else
audio.RemoveAdjustment(AdjustableProperty.Volume, muteAdjustment);
};
2016-10-22 08:50:42 +00:00
}
protected override void LoadComplete()
{
base.LoadComplete();
volumeMeterMaster.Bindable.ValueChanged += _ => settingChanged();
volumeMeterEffect.Bindable.ValueChanged += _ => settingChanged();
volumeMeterMusic.Bindable.ValueChanged += _ => settingChanged();
2018-03-03 18:08:35 +00:00
muteButton.Current.ValueChanged += _ => settingChanged();
}
private void settingChanged()
{
Show();
schedulePopOut();
2016-10-13 14:52:49 +00: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-16 16:46:54 +00:00
case GlobalAction.ToggleMute:
Show();
2018-03-03 18:08:35 +00:00
muteButton.Current.Value = !muteButton.Current;
2018-01-16 16:46:54 +00:00
return true;
}
return false;
}
2017-03-07 01:59:19 +00:00
private ScheduledDelegate popOutDelegate;
protected override void PopIn()
{
2017-02-25 12:12:39 +00: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);
}
}
}