2023-06-23 00:37:25 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2020-09-04 15:22:37 +08:00
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation ;
using osu.Framework.Bindables ;
2021-10-11 16:02:26 +08:00
using osu.Framework.Extensions.LocalisationExtensions ;
2020-09-04 15:22:37 +08:00
using osu.Framework.Graphics ;
using osu.Framework.Input.Bindings ;
2021-09-16 17:26:12 +08:00
using osu.Framework.Input.Events ;
2021-10-11 10:36:50 +08:00
using osu.Framework.Localisation ;
2020-09-04 15:22:37 +08:00
using osu.Game.Beatmaps ;
2020-11-24 12:43:46 +08:00
using osu.Game.Configuration ;
2020-09-04 15:22:37 +08:00
using osu.Game.Input.Bindings ;
2021-10-11 10:36:50 +08:00
using osu.Game.Localisation ;
2020-09-04 15:22:37 +08:00
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.
2020-09-04 15:22:37 +08:00
/// </summary>
2020-09-08 17:10:14 +08:00
public partial class MusicKeyBindingHandler : Component , IKeyBindingHandler < GlobalAction >
2020-09-04 15:22:37 +08:00
{
[Resolved]
2023-06-23 00:37:25 +08:00
private IBindable < WorkingBeatmap > beatmap { get ; set ; } = null ! ;
2020-09-04 15:22:37 +08:00
[Resolved]
2023-06-23 00:37:25 +08:00
private MusicController musicController { get ; set ; } = null ! ;
2020-09-04 15:22:37 +08:00
2023-06-23 00:37:25 +08:00
[Resolved]
private OnScreenDisplay ? onScreenDisplay { get ; set ; }
2020-09-04 15:22:37 +08:00
2022-02-24 14:54:45 +08:00
[Resolved]
2023-06-23 00:37:25 +08:00
private OsuGame game { get ; set ; } = null ! ;
2022-02-24 14:54:45 +08:00
2021-09-16 17:26:12 +08:00
public bool OnPressed ( KeyBindingPressEvent < GlobalAction > e )
2020-09-04 15:22:37 +08:00
{
2021-11-18 11:35:47 +08:00
if ( e . Repeat )
return false ;
2021-09-16 17:26:12 +08:00
switch ( e . Action )
2020-09-04 15:22:37 +08:00
{
case GlobalAction . MusicPlay :
2022-02-24 14:54:45 +08:00
if ( game . LocalUserPlaying . Value )
return false ;
2020-09-08 17:12:03 +08: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 15:22:37 +08:00
2020-09-08 17:12:03 +08:00
if ( musicController . TogglePause ( ) )
2021-10-11 16:02:26 +08:00
onScreenDisplay ? . Display ( new MusicActionToast ( wasPlaying ? ToastStrings . PauseTrack : ToastStrings . PlayTrack , e . Action ) ) ;
2020-09-04 15:22:37 +08:00
return true ;
case GlobalAction . MusicNext :
2022-02-24 14:54:45 +08:00
if ( beatmap . Disabled )
return false ;
2021-10-11 10:36:50 +08:00
musicController . NextTrack ( ( ) = > onScreenDisplay ? . Display ( new MusicActionToast ( GlobalActionKeyBindingStrings . MusicNext , e . Action ) ) ) ;
2020-09-04 15:22:37 +08:00
return true ;
case GlobalAction . MusicPrev :
2022-02-24 14:54:45 +08:00
if ( beatmap . Disabled )
return false ;
2020-09-04 15:22:37 +08:00
musicController . PreviousTrack ( res = >
{
switch ( res )
{
case PreviousTrackResult . Restart :
2021-10-11 16:02:26 +08:00
onScreenDisplay ? . Display ( new MusicActionToast ( ToastStrings . RestartTrack , e . Action ) ) ;
2020-09-04 15:22:37 +08:00
break ;
case PreviousTrackResult . Previous :
2021-10-11 10:36:50 +08:00
onScreenDisplay ? . Display ( new MusicActionToast ( GlobalActionKeyBindingStrings . MusicPrev , e . Action ) ) ;
2020-09-04 15:22:37 +08:00
break ;
}
2020-09-08 17:26:13 +08:00
} ) ;
2020-09-04 15:22:37 +08:00
return true ;
}
return false ;
}
2021-09-16 17:26:12 +08:00
public void OnReleased ( KeyBindingReleaseEvent < GlobalAction > e )
2020-09-04 15:22:37 +08:00
{
}
private partial class MusicActionToast : Toast
{
2020-11-24 12:43:46 +08:00
private readonly GlobalAction action ;
2021-10-11 10:36:50 +08:00
public MusicActionToast ( LocalisableString value , GlobalAction action )
2021-10-11 16:02:26 +08:00
: base ( ToastStrings . MusicPlayback , value , string . Empty )
2020-11-24 12:43:46 +08:00
{
this . action = action ;
}
[BackgroundDependencyLoader]
private void load ( OsuConfigManager config )
2020-09-04 15:22:37 +08:00
{
2021-10-11 16:02:26 +08:00
ShortcutText . Text = config . LookupKeyBindings ( action ) . ToUpper ( ) ;
2020-09-04 15:22:37 +08:00
}
}
}
}