2020-09-04 07:22:37 +00:00
// 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 ;
using osu.Game.Beatmaps ;
using osu.Game.Input.Bindings ;
using osu.Game.Overlays.OSD ;
namespace osu.Game.Overlays.Music
{
/// <summary>
2020-09-08 09:21:26 +00:00
/// Handles <see cref="GlobalAction"/>s related to music playback, and displays <see cref="Toast"/>s via the global <see cref="OnScreenDisplay"/> accordingly.
2020-09-04 07:22:37 +00:00
/// </summary>
2020-09-08 09:10:14 +00:00
public class MusicKeyBindingHandler : Component , IKeyBindingHandler < GlobalAction >
2020-09-04 07:22:37 +00:00
{
[Resolved]
private IBindable < WorkingBeatmap > beatmap { get ; set ; }
[Resolved]
private MusicController musicController { get ; set ; }
2020-09-05 22:54:08 +00:00
[Resolved(canBeNull: true)]
2020-09-04 07:22:37 +00:00
private OnScreenDisplay onScreenDisplay { get ; set ; }
public bool OnPressed ( GlobalAction action )
{
if ( beatmap . Disabled )
return false ;
switch ( action )
{
case GlobalAction . MusicPlay :
2020-09-08 09:12:03 +00:00
// 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 ;
2020-09-04 07:22:37 +00:00
2020-09-08 09:12:03 +00:00
if ( musicController . TogglePause ( ) )
onScreenDisplay ? . Display ( new MusicActionToast ( wasPlaying ? "Pause track" : "Play track" ) ) ;
2020-09-04 07:22:37 +00:00
return true ;
case GlobalAction . MusicNext :
2020-09-08 09:26:13 +00:00
musicController . NextTrack ( ( ) = > onScreenDisplay ? . Display ( new MusicActionToast ( "Next track" ) ) ) ;
2020-09-04 07:22:37 +00:00
return true ;
case GlobalAction . MusicPrev :
musicController . PreviousTrack ( res = >
{
switch ( res )
{
case PreviousTrackResult . Restart :
2020-09-05 22:54:08 +00:00
onScreenDisplay ? . Display ( new MusicActionToast ( "Restart track" ) ) ;
2020-09-04 07:22:37 +00:00
break ;
case PreviousTrackResult . Previous :
2020-09-05 22:54:08 +00:00
onScreenDisplay ? . Display ( new MusicActionToast ( "Previous track" ) ) ;
2020-09-04 07:22:37 +00:00
break ;
}
2020-09-08 09:26:13 +00:00
} ) ;
2020-09-04 07:22:37 +00:00
return true ;
}
return false ;
}
public void OnReleased ( GlobalAction action )
{
}
private class MusicActionToast : Toast
{
public MusicActionToast ( string action )
: base ( "Music Playback" , action , string . Empty )
{
}
}
}
}