1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 17:02:55 +08:00
osu-lazer/osu.Game/Overlays/Music/MusicKeyBindingHandler.cs

93 lines
3.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.
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Input.Bindings;
2021-09-16 17:26:12 +08:00
using osu.Framework.Input.Events;
using osu.Game.Beatmaps;
2020-11-24 12:43:46 +08:00
using osu.Game.Configuration;
using osu.Game.Input.Bindings;
using osu.Game.Overlays.OSD;
namespace osu.Game.Overlays.Music
{
/// <summary>
2020-09-08 17:21:26 +08:00
/// Handles <see cref="GlobalAction"/>s related to music playback, and displays <see cref="Toast"/>s via the global <see cref="OnScreenDisplay"/> accordingly.
/// </summary>
2020-09-08 17:10:14 +08:00
public class MusicKeyBindingHandler : Component, IKeyBindingHandler<GlobalAction>
{
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; }
[Resolved]
private MusicController musicController { get; set; }
[Resolved(canBeNull: true)]
private OnScreenDisplay onScreenDisplay { get; set; }
2021-09-16 17:26:12 +08:00
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (beatmap.Disabled)
return false;
2021-09-16 17:26:12 +08:00
switch (e.Action)
{
case GlobalAction.MusicPlay:
// use previous state as TogglePause may not update the track's state immediately (state update is run on the audio thread see https://github.com/ppy/osu/issues/9880#issuecomment-674668842)
bool wasPlaying = musicController.IsPlaying;
if (musicController.TogglePause())
2021-09-16 17:26:12 +08:00
onScreenDisplay?.Display(new MusicActionToast(wasPlaying ? "Pause track" : "Play track", e.Action));
return true;
case GlobalAction.MusicNext:
2021-09-16 17:26:12 +08:00
musicController.NextTrack(() => onScreenDisplay?.Display(new MusicActionToast("Next track", e.Action)));
return true;
case GlobalAction.MusicPrev:
musicController.PreviousTrack(res =>
{
switch (res)
{
case PreviousTrackResult.Restart:
2021-09-16 17:26:12 +08:00
onScreenDisplay?.Display(new MusicActionToast("Restart track", e.Action));
break;
case PreviousTrackResult.Previous:
2021-09-16 17:26:12 +08:00
onScreenDisplay?.Display(new MusicActionToast("Previous track", e.Action));
break;
}
2020-09-08 17:26:13 +08:00
});
return true;
}
return false;
}
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
private class MusicActionToast : Toast
{
2020-11-24 12:43:46 +08:00
private readonly GlobalAction action;
public MusicActionToast(string value, GlobalAction action)
2020-11-24 12:43:46 +08:00
: base("Music Playback", value, string.Empty)
{
this.action = action;
}
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
2020-11-24 12:43:46 +08:00
ShortcutText.Text = config.LookupKeyBindings(action).ToUpperInvariant();
}
}
}
}