1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 16:27:26 +08:00

Merge pull request #19455 from peppy/confirm-playlist-discard

Add confirmation dialog when about to discard a playlist
This commit is contained in:
Dean Herbert 2022-07-30 19:17:49 +09:00 committed by GitHub
commit 43e612f6d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 175 additions and 5 deletions

View File

@ -4,6 +4,7 @@
#nullable disable
using System.Linq;
using JetBrains.Annotations;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
@ -17,6 +18,8 @@ using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Rooms;
using osu.Game.Overlays;
using osu.Game.Overlays.Dialog;
using osu.Game.Overlays.Mods;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Osu;
@ -24,6 +27,7 @@ using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Rulesets.Taiko;
using osu.Game.Rulesets.Taiko.Mods;
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Menu;
using osu.Game.Screens.OnlinePlay;
using osu.Game.Screens.OnlinePlay.Match;
using osu.Game.Screens.OnlinePlay.Multiplayer;
@ -65,7 +69,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
AddStep("load match", () =>
{
SelectedRoom.Value = new Room { Name = { Value = "Test Room" } };
LoadScreen(screen = new MultiplayerMatchSubScreen(SelectedRoom.Value));
LoadScreen(screen = new TestMultiplayerMatchSubScreen(SelectedRoom.Value));
});
AddUntilStep("wait for load", () => screen.IsCurrentScreen());
@ -281,5 +285,29 @@ namespace osu.Game.Tests.Visual.Multiplayer
return lastItem.IsSelectedItem;
});
}
private class TestMultiplayerMatchSubScreen : MultiplayerMatchSubScreen
{
[Resolved(canBeNull: true)]
[CanBeNull]
private IDialogOverlay dialogOverlay { get; set; }
public TestMultiplayerMatchSubScreen(Room room)
: base(room)
{
}
public override bool OnExiting(ScreenExitEvent e)
{
// For testing purposes allow the screen to exit without confirming on second attempt.
if (!ExitConfirmed && dialogOverlay?.CurrentDialog is ConfirmDiscardChangesDialog confirmDialog)
{
confirmDialog.PerformAction<PopupDialogDangerousButton>();
return true;
}
return base.OnExiting(e);
}
}
}
}

View File

@ -26,6 +26,7 @@ using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Scoring;
using osu.Game.Screens.Menu;
using osu.Game.Screens.OnlinePlay.Lounge;
using osu.Game.Screens.OnlinePlay.Playlists;
using osu.Game.Screens.Play;
using osu.Game.Screens.Ranking;
using osu.Game.Screens.Select;
@ -45,6 +46,57 @@ namespace osu.Game.Tests.Visual.Navigation
private Vector2 optionsButtonPosition => Game.ToScreenSpace(new Vector2(click_padding, click_padding));
[TestCase(false)]
[TestCase(true)]
public void TestConfirmationRequiredToDiscardPlaylist(bool withPlaylistItemAdded)
{
Screens.OnlinePlay.Playlists.Playlists playlistScreen = null;
AddUntilStep("wait for dialog overlay", () => Game.ChildrenOfType<DialogOverlay>().SingleOrDefault() != null);
PushAndConfirm(() => playlistScreen = new Screens.OnlinePlay.Playlists.Playlists());
AddStep("import beatmap", () => BeatmapImportHelper.LoadQuickOszIntoOsu(Game).WaitSafely());
AddStep("open create screen", () =>
{
InputManager.MoveMouseTo(playlistScreen.ChildrenOfType<CreatePlaylistsRoomButton>().Single());
InputManager.Click(MouseButton.Left);
});
if (withPlaylistItemAdded)
{
AddUntilStep("wait for settings displayed",
() => (playlistScreen.CurrentSubScreen as PlaylistsRoomSubScreen)?.ChildrenOfType<PlaylistsRoomSettingsOverlay>().SingleOrDefault()?.State.Value == Visibility.Visible);
AddStep("edit playlist", () => InputManager.Key(Key.Enter));
AddUntilStep("wait for song select", () => (playlistScreen.CurrentSubScreen as PlaylistsSongSelect)?.BeatmapSetsLoaded == true);
AddUntilStep("wait for selection", () => !Game.Beatmap.IsDefault);
AddStep("add item", () => InputManager.Key(Key.Enter));
AddUntilStep("wait for return to playlist screen", () => playlistScreen.CurrentSubScreen is PlaylistsRoomSubScreen);
pushEscape();
AddAssert("confirmation dialog shown", () => Game.ChildrenOfType<DialogOverlay>().Single().CurrentDialog is not null);
AddStep("confirm exit", () => InputManager.Key(Key.Enter));
AddAssert("dialog dismissed", () => Game.ChildrenOfType<DialogOverlay>().Single().CurrentDialog == null);
exitViaEscapeAndConfirm();
}
else
{
pushEscape();
AddAssert("confirmation dialog not shown", () => Game.ChildrenOfType<DialogOverlay>().Single().CurrentDialog == null);
exitViaEscapeAndConfirm();
}
}
[Test]
public void TestExitSongSelectWithEscape()
{

View File

@ -6,6 +6,7 @@
using System;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
@ -16,9 +17,12 @@ using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Online.Rooms;
using osu.Game.Overlays;
using osu.Game.Overlays.Dialog;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Screens.Menu;
using osu.Game.Screens.OnlinePlay.Components;
using osu.Game.Screens.OnlinePlay.Match.Components;
using osu.Game.Screens.OnlinePlay.Playlists;
@ -221,10 +225,26 @@ namespace osu.Game.Tests.Visual.Playlists
public new Bindable<WorkingBeatmap> Beatmap => base.Beatmap;
[Resolved(canBeNull: true)]
[CanBeNull]
private IDialogOverlay dialogOverlay { get; set; }
public TestPlaylistsRoomSubScreen(Room room)
: base(room)
{
}
public override bool OnExiting(ScreenExitEvent e)
{
// For testing purposes allow the screen to exit without confirming on second attempt.
if (!ExitConfirmed && dialogOverlay?.CurrentDialog is ConfirmDiscardChangesDialog confirmDialog)
{
confirmDialog.PerformAction<PopupDialogDangerousButton>();
return true;
}
return base.OnExiting(e);
}
}
}
}

View File

@ -0,0 +1,39 @@
// 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 osu.Framework.Graphics.Sprites;
using osu.Game.Overlays.Dialog;
namespace osu.Game.Screens.Menu
{
public class ConfirmDiscardChangesDialog : PopupDialog
{
/// <summary>
/// Construct a new discard changes confirmation dialog.
/// </summary>
/// <param name="onConfirm">An action to perform on confirmation.</param>
/// <param name="onCancel">An optional action to perform on cancel.</param>
public ConfirmDiscardChangesDialog(Action onConfirm, Action? onCancel = null)
{
HeaderText = "Are you sure you want to go back?";
BodyText = "This will discard any unsaved changes";
Icon = FontAwesome.Solid.ExclamationTriangle;
Buttons = new PopupDialogButton[]
{
new PopupDialogDangerousButton
{
Text = @"Yes",
Action = onConfirm
},
new PopupDialogCancelButton
{
Text = @"No I didn't mean to",
Action = onCancel
},
};
}
}
}

View File

@ -1,8 +1,6 @@
// 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.
#nullable disable
using System;
using osu.Framework.Graphics.Sprites;
using osu.Game.Overlays.Dialog;
@ -16,7 +14,7 @@ namespace osu.Game.Screens.Menu
/// </summary>
/// <param name="onConfirm">An action to perform on confirmation.</param>
/// <param name="onCancel">An optional action to perform on cancel.</param>
public ConfirmExitDialog(Action onConfirm, Action onCancel = null)
public ConfirmExitDialog(Action onConfirm, Action? onCancel = null)
{
HeaderText = "Are you sure you want to exit osu!?";
BodyText = "Last chance to turn back";

View File

@ -28,6 +28,7 @@ using osu.Game.Overlays;
using osu.Game.Overlays.Mods;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Screens.Menu;
using osu.Game.Screens.OnlinePlay.Match.Components;
using osu.Game.Screens.OnlinePlay.Multiplayer;
@ -280,11 +281,16 @@ namespace osu.Game.Screens.OnlinePlay.Match
};
}
[Resolved(canBeNull: true)]
private IDialogOverlay dialogOverlay { get; set; }
public override bool OnBackButton()
{
if (Room.RoomID.Value == null)
{
// room has not been created yet; exit immediately.
if (!ensureExitConfirmed())
return true;
settingsOverlay.Hide();
return base.OnBackButton();
}
@ -330,8 +336,13 @@ namespace osu.Game.Screens.OnlinePlay.Match
Scheduler.AddOnce(updateRuleset);
}
protected bool ExitConfirmed { get; private set; }
public override bool OnExiting(ScreenExitEvent e)
{
if (!ensureExitConfirmed())
return true;
RoomManager?.PartRoom();
Mods.Value = Array.Empty<Mod>();
@ -340,6 +351,28 @@ namespace osu.Game.Screens.OnlinePlay.Match
return base.OnExiting(e);
}
private bool ensureExitConfirmed()
{
if (ExitConfirmed)
return true;
if (dialogOverlay == null || Room.RoomID.Value != null || Room.Playlist.Count == 0)
return true;
// if the dialog is already displayed, block exiting until the user explicitly makes a decision.
if (dialogOverlay.CurrentDialog is ConfirmDiscardChangesDialog)
return false;
dialogOverlay.Push(new ConfirmDiscardChangesDialog(() =>
{
ExitConfirmed = true;
settingsOverlay.Hide();
this.Exit();
}));
return false;
}
protected void StartPlay()
{
// User may be at song select or otherwise when the host starts gameplay.