-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathIScreen.cs
127 lines (106 loc) · 5.21 KB
/
IScreen.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
// 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 osu.Framework.Graphics;
namespace osu.Framework.Screens
{
public interface IScreen : IDrawable
{
/// <summary>
/// Whether this <see cref="IScreen"/> can be resumed.
/// </summary>
bool ValidForResume { get; set; }
/// <summary>
/// Whether this <see cref="IScreen"/> can be pushed.
/// </summary>
bool ValidForPush { get; set; }
/// <summary>
/// Invoked when this <see cref="IScreen"/> is entering from a parent <see cref="IScreen"/>.
/// </summary>
/// <param name="e">The <see cref="ScreenTransitionEvent"/> containing information about the screen event.</param>
void OnEntering(ScreenTransitionEvent e);
/// <summary>
/// Invoked when this <see cref="IScreen"/> is exiting to a parent <see cref="IScreen"/>.
/// </summary>
/// <param name="e">The <see cref="ScreenExitEvent"/> containing information about the screen event.</param>
/// <returns>True to cancel the exit process.</returns>
bool OnExiting(ScreenExitEvent e);
/// <summary>
/// Invoked when this <see cref="IScreen"/> is entered from a child <see cref="IScreen"/>.
/// </summary>
/// <param name="e">The <see cref="ScreenTransitionEvent"/> containing information about the screen event.</param>
void OnResuming(ScreenTransitionEvent e);
/// <summary>
/// Invoked when this <see cref="IScreen"/> is exited to a child <see cref="IScreen"/>.
/// </summary>
/// <param name="e">The <see cref="ScreenTransitionEvent"/> containing information about the screen event.</param>
void OnSuspending(ScreenTransitionEvent e);
}
public static class ScreenExtensions
{
/// <summary>
/// Pushes an <see cref="IScreen"/> to another.
/// </summary>
/// <param name="screen">The <see cref="IScreen"/> to push to.</param>
/// <param name="newScreen">The <see cref="IScreen"/> to push.</param>
public static void Push(this IScreen screen, IScreen newScreen)
{
var stack = getStack(screen);
if (stack == null)
throw new InvalidOperationException($"Cannot {nameof(Push)} to a non-loaded {nameof(IScreen)} directly. Consider using {nameof(ScreenStack)}.{nameof(ScreenStack.Push)} instead.");
stack.Push(screen, newScreen);
}
/// <summary>
/// Exits from an <see cref="IScreen"/>.
/// </summary>
/// <param name="screen">The <see cref="IScreen"/> to exit from.</param>
public static void Exit(this IScreen screen)
{
var stack = getStack(screen);
if (stack == null)
screen.ValidForPush = false;
else
stack.Exit(screen);
}
/// <summary>
/// Makes an <see cref="IScreen"/> the current screen, exiting all child <see cref="IScreen"/>s along the way.
/// </summary>
/// <param name="screen">The <see cref="IScreen"/> to make current.</param>
public static void MakeCurrent(this IScreen screen)
=> getStack(screen)?.MakeCurrent(screen);
/// <summary>
/// Retrieves whether an <see cref="IScreen"/> is the current screen.
/// This will return false on all <see cref="IScreen"/>s while a child <see cref="IScreen"/> is loading.
/// </summary>
/// <param name="screen">The <see cref="IScreen"/> to check.</param>
/// <returns>True if <paramref name="screen"/> is the current screen.</returns>
public static bool IsCurrentScreen(this IScreen screen)
=> getStack(screen)?.IsCurrentScreen(screen) ?? false;
/// <summary>
/// Retrieves the child <see cref="IScreen"/> of an <see cref="IScreen"/>.
/// </summary>
/// <param name="screen">The <see cref="IScreen"/> to retrieve the child of.</param>
/// <returns>The child <see cref="IScreen"/> of <paramref name="screen"/>, or null if <paramref name="screen"/> has no child.</returns>
public static IScreen GetChildScreen(this IScreen screen)
=> getStack(screen)?.GetChildScreen(screen);
/// <summary>
/// Retrieves the parent <see cref="IScreen"/> of an <see cref="IScreen"/>.
/// </summary>
/// <param name="screen">The <see cref="IScreen"/> to retrieve the parent of.</param>
/// <returns>The parent <see cref="IScreen"/> of <paramref name="screen"/>, or null if <paramref name="screen"/> has no parent.</returns>
public static IScreen GetParentScreen(this IScreen screen)
=> getStack(screen)?.GetParentScreen(screen);
internal static Drawable AsDrawable(this IScreen screen) => (Drawable)screen;
private static ScreenStack getStack(IDrawable current)
{
while (current != null)
{
if (current is ScreenStack stack)
return stack;
current = current.Parent;
}
return null;
}
}
}