1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 19:02:56 +08:00
osu-lazer/osu.Game/Overlays/VolumeOverlay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

195 lines
6.3 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 System.Linq;
using osu.Framework.Allocation;
2018-03-04 02:08:35 +08:00
using osu.Framework.Audio;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-03-04 02:08:35 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
2018-03-04 02:08:35 +08:00
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
2018-03-04 02:08:35 +08:00
using osu.Framework.Graphics.Shapes;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Threading;
2018-03-04 02:08:35 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Input.Bindings;
2018-03-04 02:08:35 +08:00
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
2018-03-04 02:08:35 +08:00
namespace osu.Game.Overlays
{
[Cached]
public partial class VolumeOverlay : VisibilityContainer
{
public Bindable<bool> IsMuted { get; } = new Bindable<bool>();
2018-03-04 02:08:35 +08:00
private const float offset = 10;
2018-04-13 17:19:50 +08:00
2024-07-11 19:29:56 +08:00
private VolumeMeter volumeMeterMaster = null!;
private VolumeMeter volumeMeterEffect = null!;
private VolumeMeter volumeMeterMusic = null!;
private SelectionCycleFillFlowContainer<VolumeMeter> volumeMeters = null!;
2018-04-13 17:19:50 +08:00
2018-03-04 02:08:35 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio, OsuColour colours)
2016-10-22 16:50:42 +08:00
{
2018-03-04 02:25:34 +08:00
AutoSizeAxes = Axes.X;
RelativeSizeAxes = Axes.Y;
2018-04-13 17:19:50 +08:00
2018-03-04 02:08:35 +08:00
AddRange(new Drawable[]
{
2018-03-04 02:08:35 +08:00
new Box
{
RelativeSizeAxes = Axes.Y,
Width = 300,
2018-03-07 13:07:04 +08:00
Colour = ColourInfo.GradientHorizontal(Color4.Black.Opacity(0.75f), Color4.Black.Opacity(0))
2018-03-04 02:08:35 +08:00
},
new FillFlowContainer
{
2018-03-04 02:08:35 +08:00
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Both,
2018-03-04 02:08:35 +08:00
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Spacing = new Vector2(0, offset),
Margin = new MarginPadding { Left = offset },
Children = new Drawable[]
{
volumeMeters = new SelectionCycleFillFlowContainer<VolumeMeter>
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Both,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Spacing = new Vector2(0, offset),
Children = new[]
{
volumeMeterEffect = new VolumeMeter("EFFECTS", 125, colours.BlueDarker),
volumeMeterMaster = new MasterVolumeMeter("MASTER", 150, colours.PinkDarker) { IsMuted = { BindTarget = IsMuted }, },
volumeMeterMusic = new VolumeMeter("MUSIC", 125, colours.BlueDarker),
}
},
},
},
2018-03-04 02:08:35 +08:00
});
2018-04-13 17:19:50 +08:00
2018-03-04 02:08:35 +08:00
volumeMeterMaster.Bindable.BindTo(audio.Volume);
volumeMeterEffect.Bindable.BindTo(audio.VolumeSample);
volumeMeterMusic.Bindable.BindTo(audio.VolumeTrack);
2016-10-22 16:50:42 +08:00
}
2018-04-13 17:19:50 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
2018-04-13 17:19:50 +08:00
foreach (var volumeMeter in volumeMeters)
volumeMeter.Bindable.ValueChanged += _ => Show();
2016-10-13 22:52:49 +08:00
}
2018-04-13 17:19:50 +08:00
public bool Adjust(GlobalAction action, float amount = 1, bool isPrecise = false)
{
if (!IsLoaded) return false;
switch (action)
{
case GlobalAction.DecreaseVolume:
if (State.Value == Visibility.Hidden)
Show();
else
volumeMeters.Selected?.Decrease(amount, isPrecise);
return true;
2019-04-01 11:44:46 +08:00
case GlobalAction.IncreaseVolume:
if (State.Value == Visibility.Hidden)
Show();
else
volumeMeters.Selected?.Increase(amount, isPrecise);
return true;
2019-04-01 11:44:46 +08:00
case GlobalAction.NextVolumeMeter:
if (State.Value == Visibility.Visible)
volumeMeters.SelectNext();
Show();
return true;
case GlobalAction.PreviousVolumeMeter:
if (State.Value == Visibility.Visible)
volumeMeters.SelectPrevious();
Show();
return true;
2018-01-17 00:46:54 +08:00
case GlobalAction.ToggleMute:
Show();
volumeMeters.OfType<MasterVolumeMeter>().First().ToggleMute();
2018-01-17 00:46:54 +08:00
return true;
}
2018-04-13 17:19:50 +08:00
return false;
}
2018-04-13 17:19:50 +08:00
public void FocusMasterVolume()
{
volumeMeters.Select(volumeMeterMaster);
}
public override void Show()
{
// Focus on the master meter as a default if previously hidden
if (State.Value == Visibility.Hidden)
FocusMasterVolume();
if (State.Value == Visibility.Visible)
schedulePopOut();
base.Show();
}
protected override void PopIn()
{
2017-02-25 20:12:39 +08:00
ClearTransforms();
this.FadeIn(100);
2018-04-13 17:19:50 +08:00
schedulePopOut();
}
2018-04-13 17:19:50 +08:00
protected override void PopOut()
{
this.FadeOut(100);
}
2018-04-13 17:19:50 +08:00
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);
}
2024-07-11 19:29:56 +08:00
private ScheduledDelegate? popOutDelegate;
private void schedulePopOut()
{
popOutDelegate?.Cancel();
this.Delay(1000).Schedule(() =>
{
if (!IsHovered)
Hide();
}, out popOutDelegate);
}
}
}