1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 08:42:55 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/ToolbarMusicButton.cs

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
2018-04-13 17:19:50 +08:00
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 class ToolbarMusicButton : ToolbarOverlayToggleButton
{
2022-06-15 03:13:26 +08:00
private Circle volumeBar;
protected override Anchor TooltipAnchor => Anchor.TopRight;
2018-04-13 17:19:50 +08:00
public ToolbarMusicButton()
{
Hotkey = GlobalAction.ToggleNowPlaying;
2022-06-15 03:13:26 +08:00
AutoSizeAxes = Axes.X;
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader(true)]
private void load(NowPlayingOverlay music)
2018-04-13 17:19:50 +08:00
{
StateContainer = music;
2022-06-15 03:13:26 +08:00
Flow.Padding = new MarginPadding { Horizontal = Toolbar.HEIGHT / 4 };
Flow.Add(volumeDisplay = new Container
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,
Children = new[]
{
new Circle
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White.Opacity(0.25f),
},
volumeBar = new Circle
{
RelativeSizeAxes = Axes.Both,
Height = 0f,
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Colour = Color4.White,
}
}
});
2018-04-13 17:19:50 +08:00
}
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);
}
2018-04-13 17:19:50 +08:00
}
}