mirror of
https://github.com/osukey/osukey.git
synced 2025-06-21 19:27:58 +09:00
Merge pull request #18693 from frenzibyte/toolbar-volume-control
Add basic volume control support in toolbar music button
This commit is contained in:
commit
584345a97e
@ -2,6 +2,7 @@
|
|||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
@ -13,6 +14,7 @@ using osu.Framework.Graphics.Shapes;
|
|||||||
using osu.Framework.Testing;
|
using osu.Framework.Testing;
|
||||||
using osu.Game.Graphics.Containers;
|
using osu.Game.Graphics.Containers;
|
||||||
using osu.Game.Overlays;
|
using osu.Game.Overlays;
|
||||||
|
using osu.Game.Overlays.Notifications;
|
||||||
using osu.Game.Overlays.Toolbar;
|
using osu.Game.Overlays.Toolbar;
|
||||||
using osu.Game.Rulesets;
|
using osu.Game.Rulesets;
|
||||||
using osuTK.Graphics;
|
using osuTK.Graphics;
|
||||||
@ -28,21 +30,44 @@ namespace osu.Game.Tests.Visual.Menus
|
|||||||
[Resolved]
|
[Resolved]
|
||||||
private IRulesetStore rulesets { get; set; }
|
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();
|
private readonly BindableInt unreadNotificationCount = new BindableInt();
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
[BackgroundDependencyLoader]
|
||||||
private void load()
|
private void load()
|
||||||
{
|
{
|
||||||
Dependencies.CacheAs(notifications.Object);
|
Dependencies.CacheAs<INotificationOverlay>(notifications.Object);
|
||||||
notifications.SetupGet(n => n.UnreadCount).Returns(unreadNotificationCount);
|
notifications.SetupGet(n => n.UnreadCount).Returns(unreadNotificationCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void SetUp() => Schedule(() =>
|
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]
|
[Test]
|
||||||
@ -122,9 +147,51 @@ namespace osu.Game.Tests.Visual.Menus
|
|||||||
AddAssert("not scrolled", () => scroll.Current == 0);
|
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 class TestToolbar : Toolbar
|
||||||
{
|
{
|
||||||
public new Bindable<OverlayActivation> OverlayActivationMode => base.OverlayActivationMode as Bindable<OverlayActivation>;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,24 +2,131 @@
|
|||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
using osu.Framework.Allocation;
|
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;
|
||||||
|
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 osu.Game.Input.Bindings;
|
||||||
|
using osuTK.Graphics;
|
||||||
|
using osuTK.Input;
|
||||||
|
|
||||||
namespace osu.Game.Overlays.Toolbar
|
namespace osu.Game.Overlays.Toolbar
|
||||||
{
|
{
|
||||||
public class ToolbarMusicButton : ToolbarOverlayToggleButton
|
public class ToolbarMusicButton : ToolbarOverlayToggleButton
|
||||||
{
|
{
|
||||||
|
private Circle volumeBar;
|
||||||
|
|
||||||
protected override Anchor TooltipAnchor => Anchor.TopRight;
|
protected override Anchor TooltipAnchor => Anchor.TopRight;
|
||||||
|
|
||||||
public ToolbarMusicButton()
|
public ToolbarMusicButton()
|
||||||
{
|
{
|
||||||
Hotkey = GlobalAction.ToggleNowPlaying;
|
Hotkey = GlobalAction.ToggleNowPlaying;
|
||||||
|
AutoSizeAxes = Axes.X;
|
||||||
}
|
}
|
||||||
|
|
||||||
[BackgroundDependencyLoader(true)]
|
[BackgroundDependencyLoader(true)]
|
||||||
private void load(NowPlayingOverlay music)
|
private void load(NowPlayingOverlay music)
|
||||||
{
|
{
|
||||||
StateContainer = 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,11 +140,16 @@ namespace osu.Game.Overlays
|
|||||||
|
|
||||||
private ScheduledDelegate popOutDelegate;
|
private ScheduledDelegate popOutDelegate;
|
||||||
|
|
||||||
|
public void FocusMasterVolume()
|
||||||
|
{
|
||||||
|
volumeMeters.Select(volumeMeterMaster);
|
||||||
|
}
|
||||||
|
|
||||||
public override void Show()
|
public override void Show()
|
||||||
{
|
{
|
||||||
// Focus on the master meter as a default if previously hidden
|
// Focus on the master meter as a default if previously hidden
|
||||||
if (State.Value == Visibility.Hidden)
|
if (State.Value == Visibility.Hidden)
|
||||||
volumeMeters.Select(volumeMeterMaster);
|
FocusMasterVolume();
|
||||||
|
|
||||||
if (State.Value == Visibility.Visible)
|
if (State.Value == Visibility.Visible)
|
||||||
schedulePopOut();
|
schedulePopOut();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user