1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 12:17:26 +08:00

Merge pull request #18693 from frenzibyte/toolbar-volume-control

Add basic volume control support in toolbar music button
This commit is contained in:
Dean Herbert 2022-06-15 17:50:10 +09:00 committed by GitHub
commit 584345a97e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 183 additions and 4 deletions

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System.Linq;
using JetBrains.Annotations;
using Moq;
using NUnit.Framework;
using osu.Framework.Allocation;
@ -13,6 +14,7 @@ using osu.Framework.Graphics.Shapes;
using osu.Framework.Testing;
using osu.Game.Graphics.Containers;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osu.Game.Overlays.Toolbar;
using osu.Game.Rulesets;
using osuTK.Graphics;
@ -28,21 +30,44 @@ namespace osu.Game.Tests.Visual.Menus
[Resolved]
private IRulesetStore rulesets { get; set; }
private readonly Mock<INotificationOverlay> notifications = new Mock<INotificationOverlay>();
[Cached]
private readonly NowPlayingOverlay nowPlayingOverlay = new NowPlayingOverlay
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Y = Toolbar.HEIGHT,
};
[Cached]
private readonly VolumeOverlay volumeOverlay = new VolumeOverlay
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
};
private readonly Mock<TestNotificationOverlay> notifications = new Mock<TestNotificationOverlay>();
private readonly BindableInt unreadNotificationCount = new BindableInt();
[BackgroundDependencyLoader]
private void load()
{
Dependencies.CacheAs(notifications.Object);
Dependencies.CacheAs<INotificationOverlay>(notifications.Object);
notifications.SetupGet(n => n.UnreadCount).Returns(unreadNotificationCount);
}
[SetUp]
public void SetUp() => Schedule(() =>
{
Child = toolbar = new TestToolbar { State = { Value = Visibility.Visible } };
Remove(nowPlayingOverlay);
Remove(volumeOverlay);
Children = new Drawable[]
{
nowPlayingOverlay,
volumeOverlay,
toolbar = new TestToolbar { State = { Value = Visibility.Visible } },
};
});
[Test]
@ -122,9 +147,51 @@ namespace osu.Game.Tests.Visual.Menus
AddAssert("not scrolled", () => scroll.Current == 0);
}
[Test]
public void TestVolumeControlViaMusicButtonScroll()
{
AddStep("hover toolbar music button", () => InputManager.MoveMouseTo(this.ChildrenOfType<ToolbarMusicButton>().Single()));
AddStep("reset volume", () => Audio.Volume.Value = 1);
AddRepeatStep("scroll down", () => InputManager.ScrollVerticalBy(-10), 5);
AddAssert("volume lowered down", () => Audio.Volume.Value < 1);
AddRepeatStep("scroll up", () => InputManager.ScrollVerticalBy(10), 5);
AddAssert("volume raised up", () => Audio.Volume.Value == 1);
}
[Test]
public void TestVolumeControlViaMusicButtonArrowKeys()
{
AddStep("hover toolbar music button", () => InputManager.MoveMouseTo(this.ChildrenOfType<ToolbarMusicButton>().Single()));
AddStep("reset volume", () => Audio.Volume.Value = 1);
AddRepeatStep("arrow down", () => InputManager.Key(Key.Down), 5);
AddAssert("volume lowered down", () => Audio.Volume.Value < 1);
AddRepeatStep("arrow up", () => InputManager.Key(Key.Up), 5);
AddAssert("volume raised up", () => Audio.Volume.Value == 1);
}
public class TestToolbar : Toolbar
{
public new Bindable<OverlayActivation> OverlayActivationMode => base.OverlayActivationMode as Bindable<OverlayActivation>;
}
// interface mocks break hot reload, mocking this stub implementation instead works around it.
// see: https://github.com/moq/moq4/issues/1252
[UsedImplicitly]
public class TestNotificationOverlay : INotificationOverlay
{
public virtual void Post(Notification notification)
{
}
public virtual void Hide()
{
}
public virtual IBindable<int> UnreadCount => null;
}
}
}

View File

@ -2,24 +2,131 @@
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
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;
using osuTK.Graphics;
using osuTK.Input;
namespace osu.Game.Overlays.Toolbar
{
public class ToolbarMusicButton : ToolbarOverlayToggleButton
{
private Circle volumeBar;
protected override Anchor TooltipAnchor => Anchor.TopRight;
public ToolbarMusicButton()
{
Hotkey = GlobalAction.ToggleNowPlaying;
AutoSizeAxes = Axes.X;
}
[BackgroundDependencyLoader(true)]
private void load(NowPlayingOverlay music)
{
StateContainer = music;
Flow.Padding = new MarginPadding { Horizontal = Toolbar.HEIGHT / 4 };
Flow.Add(volumeDisplay = new Container
{
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,
}
}
});
}
[Resolved]
private AudioManager audio { get; set; }
[Resolved(canBeNull: true)]
private VolumeOverlay volume { get; set; }
private IBindable<double> globalVolume;
private Container volumeDisplay;
protected override void LoadComplete()
{
base.LoadComplete();
globalVolume = audio.Volume.GetBoundCopy();
globalVolume.BindValueChanged(v => volumeBar.ResizeHeightTo((float)v.NewValue, 200, Easing.OutQuint), true);
}
protected override bool OnKeyDown(KeyDownEvent e)
{
switch (e.Key)
{
case Key.Up:
focusForAdjustment();
volume?.Adjust(GlobalAction.IncreaseVolume);
return true;
case Key.Down:
focusForAdjustment();
volume?.Adjust(GlobalAction.DecreaseVolume);
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);
}
}
}

View File

@ -140,11 +140,16 @@ namespace osu.Game.Overlays
private ScheduledDelegate popOutDelegate;
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)
volumeMeters.Select(volumeMeterMaster);
FocusMasterVolume();
if (State.Value == Visibility.Visible)
schedulePopOut();