1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 11:23:00 +08:00

Split out IPerformFromScreenRunner to allow for easier testing

This commit is contained in:
Dean Herbert 2022-04-18 17:49:28 +09:00
parent 587f1e2c4f
commit 2202863e1a
7 changed files with 43 additions and 19 deletions

View File

@ -63,7 +63,7 @@ namespace osu.Game
/// The full osu! experience. Builds on top of <see cref="OsuGameBase"/> to add menus and binding logic
/// for initial components that are generally retrieved via DI.
/// </summary>
public class OsuGame : OsuGameBase, IKeyBindingHandler<GlobalAction>, ILocalUserPlayInfo
public class OsuGame : OsuGameBase, IKeyBindingHandler<GlobalAction>, ILocalUserPlayInfo, IPerformFromScreenRunner
{
/// <summary>
/// The amount of global offset to apply when a left/right anchored overlay is displayed (ie. settings or notifications).
@ -586,12 +586,6 @@ namespace osu.Game
private PerformFromMenuRunner performFromMainMenuTask;
/// <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="action">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>
public void PerformFromScreen(Action<IScreen> action, IEnumerable<Type> validScreens = null)
{
performFromMainMenuTask?.Cancel();

View File

@ -14,6 +14,7 @@ using osu.Game.Database;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Spectator;
using osu.Game.Screens;
using osu.Game.Screens.OnlinePlay.Match.Components;
using osu.Game.Screens.Play;
using osu.Game.Users;
@ -106,7 +107,7 @@ namespace osu.Game.Overlays.Dashboard
public readonly APIUser User;
[Resolved(canBeNull: true)]
private OsuGame game { get; set; }
private IPerformFromScreenRunner performer { get; set; }
public PlayingUserPanel(APIUser user)
{
@ -140,7 +141,7 @@ namespace osu.Game.Overlays.Dashboard
Text = "Watch",
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Action = () => game?.PerformFromScreen(s => s.Push(new SoloSpectator(User))),
Action = () => performer?.PerformFromScreen(s => s.Push(new SoloSpectator(User))),
Enabled = { Value = User.Id != api.LocalUser.Value.Id }
}
}

View File

@ -7,6 +7,7 @@ using osu.Framework.Graphics;
using osu.Framework.Localisation;
using osu.Framework.Screens;
using osu.Game.Localisation;
using osu.Game.Screens;
using osu.Game.Screens.Import;
namespace osu.Game.Overlays.Settings.Sections.DebugSettings
@ -16,7 +17,7 @@ namespace osu.Game.Overlays.Settings.Sections.DebugSettings
protected override LocalisableString Header => DebugSettingsStrings.GeneralHeader;
[BackgroundDependencyLoader(true)]
private void load(FrameworkDebugConfigManager config, FrameworkConfigManager frameworkConfig, OsuGame game)
private void load(FrameworkDebugConfigManager config, FrameworkConfigManager frameworkConfig, IPerformFromScreenRunner performer)
{
Children = new Drawable[]
{
@ -34,7 +35,7 @@ namespace osu.Game.Overlays.Settings.Sections.DebugSettings
Add(new SettingsButton
{
Text = DebugSettingsStrings.ImportFiles,
Action = () => game?.PerformFromScreen(menu => menu.Push(new FileImportScreen()))
Action = () => performer?.PerformFromScreen(menu => menu.Push(new FileImportScreen()))
});
}
}

View File

@ -6,13 +6,14 @@ using osu.Framework.Allocation;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Screens;
using osu.Game.Overlays.Dialog;
using osu.Game.Screens;
namespace osu.Game.Overlays.Settings.Sections.Maintenance
{
public class StableDirectoryLocationDialog : PopupDialog
{
[Resolved]
private OsuGame game { get; set; }
[Resolved(canBeNull: true)]
private IPerformFromScreenRunner performer { get; set; }
public StableDirectoryLocationDialog(TaskCompletionSource<string> taskCompletionSource)
{
@ -25,7 +26,7 @@ namespace osu.Game.Overlays.Settings.Sections.Maintenance
new PopupDialogOkButton
{
Text = "Sure! I know where it is located!",
Action = () => Schedule(() => game.PerformFromScreen(screen => screen.Push(new StableDirectorySelectScreen(taskCompletionSource))))
Action = () => Schedule(() => performer.PerformFromScreen(screen => screen.Push(new StableDirectorySelectScreen(taskCompletionSource))))
},
new PopupDialogCancelButton
{

View File

@ -0,0 +1,26 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Screens;
using osu.Game.Screens.Menu;
namespace osu.Game.Screens
{
/// <summary>
/// Manages a global screen stack to allow nested components a guarantee of where work is executed.
/// </summary>
[Cached]
public interface IPerformFromScreenRunner
{
/// <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="action">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>
void PerformFromScreen(Action<IScreen> action, IEnumerable<Type> validScreens = null);
}
}

View File

@ -148,14 +148,14 @@ namespace osu.Game.Screens.Menu
}
[Resolved(canBeNull: true)]
private OsuGame game { get; set; }
private IPerformFromScreenRunner performer { get; set; }
private void confirmAndExit()
{
if (exitConfirmed) return;
exitConfirmed = true;
game?.PerformFromScreen(menu => menu.Exit());
performer?.PerformFromScreen(menu => menu.Exit());
}
private void preloadSongSelect()

View File

@ -15,6 +15,7 @@ using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Screens;
using osu.Game.Screens.Play;
using osu.Game.Screens.Select;
using osuTK;
@ -28,7 +29,7 @@ namespace osu.Game.Skinning.Editor
private const float padding = 10;
[Resolved(canBeNull: true)]
private OsuGame game { get; set; }
private IPerformFromScreenRunner performer { get; set; }
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
@ -75,7 +76,7 @@ namespace osu.Game.Skinning.Editor
Text = "Song Select",
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Action = () => game?.PerformFromScreen(screen =>
Action = () => performer?.PerformFromScreen(screen =>
{
if (screen is SongSelect)
return;
@ -88,7 +89,7 @@ namespace osu.Game.Skinning.Editor
Text = "Gameplay",
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Action = () => game?.PerformFromScreen(screen =>
Action = () => performer?.PerformFromScreen(screen =>
{
if (screen is Player)
return;