1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 21:27:24 +08:00
osu-lazer/osu.Game/Overlays/VolumeOverlay.cs

231 lines
7.2 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Framework.Threading;
using osu.Game.Graphics;
using osu.Game.Input.Bindings;
using osu.Game.Overlays.Volume;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays
{
[Cached]
public class VolumeOverlay : VisibilityContainer
2018-04-13 17:19:50 +08:00
{
private const float offset = 10;
private VolumeMeter volumeMeterMaster;
private VolumeMeter volumeMeterEffect;
private VolumeMeter volumeMeterMusic;
private MuteButton muteButton;
private readonly BindableDouble muteAdjustment = new BindableDouble();
2019-11-12 17:45:42 +08:00
public Bindable<bool> IsMuted { get; } = new Bindable<bool>();
2019-09-15 22:31:57 +08:00
private FillFlowContainer<VolumeMeter> volumeMeters;
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio, OsuColour colours)
{
AutoSizeAxes = Axes.X;
RelativeSizeAxes = Axes.Y;
AddRange(new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Y,
Width = 300,
Colour = ColourInfo.GradientHorizontal(Color4.Black.Opacity(0.75f), Color4.Black.Opacity(0))
},
muteButton = new MuteButton
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Margin = new MarginPadding(10),
Current = { BindTarget = IsMuted }
},
volumeMeters = new FillFlowContainer<VolumeMeter>
2018-04-13 17:19:50 +08:00
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Both,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Spacing = new Vector2(0, offset),
Margin = new MarginPadding { Left = offset },
Children = new VolumeMeter[]
2018-04-13 17:19:50 +08:00
{
volumeMeterEffect = new VolumeMeter("EFFECTS", 125, colours.BlueDarker),
2018-04-13 17:19:50 +08:00
volumeMeterMaster = new VolumeMeter("MASTER", 150, colours.PinkDarker),
volumeMeterMusic = new VolumeMeter("MUSIC", 125, colours.BlueDarker),
}
}
2018-04-13 17:19:50 +08:00
});
volumeMeterMaster.Bindable.BindTo(audio.Volume);
volumeMeterEffect.Bindable.BindTo(audio.VolumeSample);
volumeMeterMusic.Bindable.BindTo(audio.VolumeTrack);
2019-11-12 17:45:42 +08:00
IsMuted.BindValueChanged(muted =>
2018-04-13 17:19:50 +08:00
{
if (muted.NewValue)
2018-04-13 17:19:50 +08:00
audio.AddAdjustment(AdjustableProperty.Volume, muteAdjustment);
else
audio.RemoveAdjustment(AdjustableProperty.Volume, muteAdjustment);
2019-10-03 18:11:50 +08:00
});
focusedMeter.BindValueChanged(meter => meter.OldValue?.Unfocus());
2018-04-13 17:19:50 +08:00
}
[Cached]
private Bindable<VolumeMeter> focusedMeter = new Bindable<VolumeMeter>();
2018-04-13 17:19:50 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
volumeMeterMaster.Bindable.ValueChanged += _ => Show();
volumeMeterEffect.Bindable.ValueChanged += _ => Show();
volumeMeterMusic.Bindable.ValueChanged += _ => Show();
muteButton.Current.ValueChanged += _ => Show();
2018-04-13 17:19:50 +08:00
}
public bool HandleAction(GlobalAction action)
{
if (!IsLoaded) return false;
switch (action)
{
case GlobalAction.DecreaseVolume:
case GlobalAction.IncreaseVolume:
return Adjust(action);
case GlobalAction.NextVolumeMeter:
return true;
}
return true;
}
public bool Adjust(GlobalAction action, float amount = 1, bool isPrecise = false)
2018-04-13 17:19:50 +08:00
{
if (!IsLoaded) return false;
2018-04-13 17:19:50 +08:00
switch (action)
{
case GlobalAction.DecreaseVolume:
if (State.Value == Visibility.Hidden)
2018-04-13 17:19:50 +08:00
Show();
else
focusedMeter.Value.Decrease(amount, isPrecise);
2018-04-13 17:19:50 +08:00
return true;
2019-04-01 11:44:46 +08:00
2018-04-13 17:19:50 +08:00
case GlobalAction.IncreaseVolume:
if (State.Value == Visibility.Hidden)
2018-04-13 17:19:50 +08:00
Show();
else
focusedMeter.Value.Increase(amount, isPrecise);
2018-04-13 17:19:50 +08:00
return true;
2019-04-01 11:44:46 +08:00
case GlobalAction.NextVolumeMeter:
if (State.Value == Visibility.Hidden)
Show();
else
focusShift(1);
return true;
case GlobalAction.PreviousVolumeMeter:
if (State.Value == Visibility.Hidden)
Show();
else
focusShift(-1);
return true;
2018-04-13 17:19:50 +08:00
case GlobalAction.ToggleMute:
Show();
2019-02-21 17:56:34 +08:00
muteButton.Current.Value = !muteButton.Current.Value;
2018-04-13 17:19:50 +08:00
return true;
}
return false;
}
private void focusShift(int direction = 1)
{
Show();
var newIndex = volumeMeters.IndexOf(focusedMeter.Value) + direction;
if (newIndex < 0)
newIndex += volumeMeters.Count;
volumeMeters.Children[newIndex % volumeMeters.Count].Focus();
}
2018-04-13 17:19:50 +08:00
private ScheduledDelegate popOutDelegate;
public override void Show()
{
if (State.Value == Visibility.Hidden)
volumeMeterMaster.Focus();
if (State.Value == Visibility.Visible)
schedulePopOut();
base.Show();
}
2018-04-13 17:19:50 +08:00
protected override void PopIn()
{
ClearTransforms();
this.FadeIn(100);
schedulePopOut();
}
protected override void PopOut()
{
this.FadeOut(100);
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
{
// keep the scheduled event correctly timed as long as we have movement.
schedulePopOut();
2018-10-02 11:02:47 +08:00
return base.OnMouseMove(e);
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
{
2018-06-22 12:32:00 +08:00
schedulePopOut();
return true;
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
{
schedulePopOut();
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
}
2018-04-13 17:19:50 +08:00
private void schedulePopOut()
{
popOutDelegate?.Cancel();
this.Delay(1000).Schedule(() =>
{
if (!IsHovered)
Hide();
}, out popOutDelegate);
2018-04-13 17:19:50 +08:00
}
}
}