forked from ppy/osu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerformFromMenuRunner.cs
181 lines (147 loc) · 6.5 KB
/
PerformFromMenuRunner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
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;
using osu.Game.Screens;
using osu.Game.Screens.Menu;
namespace osu.Game
{
internal partial class PerformFromMenuRunner : Component
{
private readonly Action<IScreen> finalAction;
private readonly Type[] validScreens;
private readonly Func<IScreen> getCurrentScreen;
[Resolved]
private INotificationOverlay notifications { get; set; }
[Resolved]
private IDialogOverlay 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;
this.validScreens = validScreens.ToArray();
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();
if (current == null)
return;
// a dialog may be blocking the execution for now.
if (checkForDialog(current)) return;
game?.CloseAllOverlays(false);
findValidTarget(current);
}
private bool findValidTarget(IScreen current)
{
var type = current.GetType();
// check if we are already at a valid target screen.
if (validScreens.Any(t => t.IsAssignableFrom(type)))
{
if (!((Drawable)current).IsLoaded)
// wait until screen is loaded before invoking action.
return true;
finalAction(current);
Cancel();
return true;
}
while (current != null)
{
// if this has a sub stack, recursively check the screens within it.
if (current is IHasSubScreenStack currentSubScreen)
{
var nestedCurrent = currentSubScreen.SubScreenStack.CurrentScreen;
if (nestedCurrent != null)
{
// should be correct in theory, but currently untested/unused in existing implementations.
// note that calling findValidTarget actually performs the final operation.
if (findValidTarget(nestedCurrent))
return true;
}
}
if (validScreens.Any(t => t.IsAssignableFrom(type)))
{
current.MakeCurrent();
return true;
}
current = current.GetParentScreen();
type = current?.GetType();
}
return false;
}
/// <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)
{
// An exit process may traverse multiple levels.
// When checking for dismissing dialogs, let's also consider sub screens.
while (current is IHasSubScreenStack currentWithSubScreenStack)
{
var nestedCurrent = currentWithSubScreenStack.SubScreenStack.CurrentScreen;
if (nestedCurrent == null)
break;
current = nestedCurrent;
}
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;
}
}
}