1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 14:57:25 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/ToolbarMusicButton.cs

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

138 lines
4.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
2022-06-17 15:37:17 +08:00
#nullable disable
using osu.Framework.Allocation;
2022-06-15 03:13:26 +08:00
using osu.Framework.Audio;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
2022-06-15 03:13:26 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Transforms;
using osu.Framework.Input.Events;
using osu.Framework.Threading;
using osu.Game.Input.Bindings;
2022-06-15 03:13:26 +08:00
using osuTK.Graphics;
2022-06-15 14:57:09 +08:00
using osuTK.Input;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Toolbar
{
public partial class ToolbarMusicButton : ToolbarOverlayToggleButton
{
2024-04-14 07:30:59 +08:00
private Box volumeBar;
2022-06-15 03:13:26 +08:00
protected override Anchor TooltipAnchor => Anchor.TopRight;
public ToolbarMusicButton()
{
Hotkey = GlobalAction.ToggleNowPlaying;
ButtonContent.AutoSizeAxes = Axes.X;
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader(true)]
private void load(NowPlayingOverlay music)
{
StateContainer = music;
2022-06-15 03:13:26 +08:00
Flow.Padding = new MarginPadding { Horizontal = Toolbar.HEIGHT / 4 };
2024-04-14 21:10:05 +08:00
Flow.Add(volumeDisplay = new CircularContainer
2022-06-15 03:13:26 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Width = 3f,
Height = IconContainer.Height,
Margin = new MarginPadding { Horizontal = 2.5f },
Masking = true,
2024-04-14 21:10:05 +08:00
Children = new[]
2022-06-15 03:13:26 +08:00
{
2024-04-14 21:10:05 +08:00
new Box
2022-06-15 03:13:26 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White.Opacity(0.25f),
},
2024-04-14 07:30:59 +08:00
volumeBar = new Box
2022-06-15 03:13:26 +08:00
{
RelativeSizeAxes = Axes.Both,
Height = 0f,
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Colour = Color4.White,
}
}
});
}
2022-06-15 03:13:26 +08:00
[Resolved]
private AudioManager audio { get; set; }
[Resolved(canBeNull: true)]
private VolumeOverlay volume { get; set; }
2022-06-15 03:13:26 +08:00
private IBindable<double> globalVolume;
private Container volumeDisplay;
2022-06-15 03:13:26 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
globalVolume = audio.Volume.GetBoundCopy();
globalVolume.BindValueChanged(v => volumeBar.ResizeHeightTo((float)v.NewValue, 200, Easing.OutQuint), true);
2022-06-15 03:13:26 +08:00
}
2022-06-15 14:57:09 +08:00
protected override bool OnKeyDown(KeyDownEvent e)
{
if (!IsHovered)
return false;
2022-06-15 14:57:09 +08:00
switch (e.Key)
{
case Key.Up:
focusForAdjustment();
volume?.Adjust(GlobalAction.IncreaseVolume);
return true;
case Key.Down:
focusForAdjustment();
volume?.Adjust(GlobalAction.DecreaseVolume);
2022-06-15 14:57:09 +08:00
return true;
}
return base.OnKeyDown(e);
}
protected override bool OnScroll(ScrollEvent e)
{
focusForAdjustment();
volume?.Adjust(GlobalAction.IncreaseVolume, e.ScrollDelta.Y, e.IsPrecise);
return true;
}
private void focusForAdjustment()
{
volume?.FocusMasterVolume();
expandVolumeBarTemporarily();
}
private TransformSequence<Container> expandTransform;
private ScheduledDelegate contractTransform;
private void expandVolumeBarTemporarily()
{
// avoid starting a new transform if one is already active.
if (expandTransform == null)
{
expandTransform = volumeDisplay.ResizeWidthTo(6, 500, Easing.OutQuint);
expandTransform.Finally(_ => expandTransform = null);
}
contractTransform?.Cancel();
contractTransform = Scheduler.AddDelayed(() =>
{
volumeDisplay.ResizeWidthTo(3f, 500, Easing.OutQuint);
}, 1000);
}
}
}