2020-11-11 05:45:50 +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 System ;
using System.Collections.Generic ;
using System.Linq ;
using osu.Framework.Allocation ;
using osu.Framework.Graphics ;
using osu.Framework.Screens ;
using osu.Framework.Threading ;
using osu.Game.Overlays ;
using osu.Game.Overlays.Dialog ;
using osu.Game.Overlays.Notifications ;
2021-02-19 08:58:04 +00:00
using osu.Game.Screens ;
2020-11-11 05:45:50 +00:00
using osu.Game.Screens.Menu ;
namespace osu.Game
{
internal class PerformFromMenuRunner : Component
{
private readonly Action < IScreen > finalAction ;
2020-11-12 01:55:22 +00:00
private readonly Type [ ] validScreens ;
2020-11-11 05:45:50 +00:00
private readonly Func < IScreen > getCurrentScreen ;
[Resolved]
private NotificationOverlay notifications { get ; set ; }
[Resolved]
private DialogOverlay dialogOverlay { get ; set ; }
[Resolved(canBeNull: true)]
private OsuGame game { get ; set ; }
private readonly ScheduledDelegate task ;
private PopupDialog lastEncounteredDialog ;
private IScreen lastEncounteredDialogScreen ;
/// <summary>
/// Perform an action only after returning to a specific screen as indicated by <paramref name="validScreens"/>.
/// Eagerly tries to exit the current screen until it succeeds.
/// </summary>
/// <param name="finalAction">The action to perform once we are in the correct state.</param>
/// <param name="validScreens">An optional collection of valid screen types. If any of these screens are already current we can perform the action immediately, else the first valid parent will be made current before performing the action. <see cref="MainMenu"/> is used if not specified.</param>
/// <param name="getCurrentScreen">A function to retrieve the currently displayed game screen.</param>
public PerformFromMenuRunner ( Action < IScreen > finalAction , IEnumerable < Type > validScreens , Func < IScreen > getCurrentScreen )
{
validScreens ? ? = Enumerable . Empty < Type > ( ) ;
validScreens = validScreens . Append ( typeof ( MainMenu ) ) ;
this . finalAction = finalAction ;
2020-11-12 01:55:22 +00:00
this . validScreens = validScreens . ToArray ( ) ;
2020-11-11 05:45:50 +00:00
this . getCurrentScreen = getCurrentScreen ;
Scheduler . Add ( task = new ScheduledDelegate ( checkCanComplete , 0 , 200 ) ) ;
}
/// <summary>
/// Cancel this runner from running.
/// </summary>
public void Cancel ( )
{
task . Cancel ( ) ;
Expire ( ) ;
}
private void checkCanComplete ( )
{
// find closest valid target
IScreen current = getCurrentScreen ( ) ;
2020-12-31 07:36:20 +00:00
if ( current = = null )
return ;
2020-11-11 05:45:50 +00:00
// a dialog may be blocking the execution for now.
if ( checkForDialog ( current ) ) return ;
2020-11-14 13:48:48 +00:00
game ? . CloseAllOverlays ( false ) ;
2020-11-13 05:08:08 +00:00
2021-02-19 08:58:04 +00:00
findValidTarget ( current ) ;
}
private bool findValidTarget ( IScreen current )
{
2021-02-18 10:19:28 +00:00
var type = current . GetType ( ) ;
2021-02-19 08:58:04 +00:00
// check if we are already at a valid target screen.
2021-03-03 05:04:00 +00:00
if ( validScreens . Any ( t = > t . IsAssignableFrom ( type ) ) )
2020-11-11 05:45:50 +00:00
{
2020-12-31 09:27:42 +00:00
finalAction ( current ) ;
Cancel ( ) ;
2021-02-19 08:58:04 +00:00
return true ;
2020-11-11 05:45:50 +00:00
}
while ( current ! = null )
{
2021-02-19 08:58:04 +00:00
// if this has a sub stack, recursively check the screens within it.
if ( current is IHasSubScreenStack currentSubScreen )
{
if ( findValidTarget ( currentSubScreen . SubScreenStack . CurrentScreen ) )
2021-02-19 10:45:29 +00:00
{
// should be correct in theory, but currently untested/unused in existing implementations.
current . MakeCurrent ( ) ;
2021-02-19 08:58:04 +00:00
return true ;
2021-02-19 10:45:29 +00:00
}
2021-02-19 08:58:04 +00:00
}
2021-02-19 05:33:08 +00:00
if ( validScreens . Any ( t = > t . IsAssignableFrom ( type ) ) )
2020-11-11 05:45:50 +00:00
{
current . MakeCurrent ( ) ;
2021-02-19 08:58:04 +00:00
return true ;
2020-11-11 05:45:50 +00:00
}
current = current . GetParentScreen ( ) ;
2021-02-18 10:19:28 +00:00
type = current ? . GetType ( ) ;
2020-11-11 05:45:50 +00:00
}
2021-02-19 08:58:04 +00:00
return false ;
2020-11-11 05:45:50 +00:00
}
/// <summary>
/// Check whether there is currently a dialog requiring user interaction.
/// </summary>
/// <param name="current"></param>
/// <returns>Whether a dialog blocked interaction.</returns>
private bool checkForDialog ( IScreen current )
{
var currentDialog = dialogOverlay . CurrentDialog ;
if ( lastEncounteredDialog ! = null )
{
if ( lastEncounteredDialog = = currentDialog )
// still waiting on user interaction
return true ;
if ( lastEncounteredDialogScreen ! = current )
{
// a dialog was previously encountered but has since been dismissed.
// if the screen changed, the user likely confirmed an exit dialog and we should continue attempting the action.
lastEncounteredDialog = null ;
lastEncounteredDialogScreen = null ;
return false ;
}
// the last dialog encountered has been dismissed but the screen has not changed, abort.
Cancel ( ) ;
notifications . Post ( new SimpleNotification { Text = @"An action was interrupted due to a dialog being displayed." } ) ;
return true ;
}
if ( currentDialog = = null )
return false ;
// a new dialog was encountered.
lastEncounteredDialog = currentDialog ;
lastEncounteredDialogScreen = current ;
return true ;
}
}
}