Skip to content

Commit

Permalink
Merge pull request #19455 from peppy/confirm-playlist-discard
Browse files Browse the repository at this point in the history
Add confirmation dialog when about to discard a playlist
  • Loading branch information
peppy authored Jul 30, 2022
2 parents ecf7cc7 + 8ca8484 commit 43e612f
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using System.Linq;
using JetBrains.Annotations;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
Expand All @@ -17,13 +18,16 @@
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;
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;
Expand Down Expand Up @@ -65,7 +69,7 @@ public void SetupSteps()
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());
Expand Down Expand Up @@ -281,5 +285,29 @@ public void TestNextPlaylistItemSelectedAfterCompletion()
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);
}
}
}
}
52 changes: 52 additions & 0 deletions osu.Game.Tests/Visual/Navigation/TestSceneScreenNavigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -45,6 +46,57 @@ public class TestSceneScreenNavigation : OsuGameTestScene

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()
{
Expand Down
20 changes: 20 additions & 0 deletions osu.Game.Tests/Visual/Playlists/TestScenePlaylistsRoomCreation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,9 +17,12 @@
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;
Expand Down Expand Up @@ -221,10 +225,26 @@ private class TestPlaylistsRoomSubScreen : PlaylistsRoomSubScreen

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);
}
}
}
}
39 changes: 39 additions & 0 deletions osu.Game/Screens/Menu/ConfirmDiscardChangesDialog.cs
Original file line number Diff line number Diff line change
@@ -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
},
};
}
}
}
4 changes: 1 addition & 3 deletions osu.Game/Screens/Menu/ConfirmExitDialog.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +14,7 @@ public class ConfirmExitDialog : PopupDialog
/// </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";
Expand Down
35 changes: 34 additions & 1 deletion osu.Game/Screens/OnlinePlay/Match/RoomSubScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;

Expand Down Expand Up @@ -280,11 +281,16 @@ protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnl
};
}

[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();
}
Expand Down Expand Up @@ -330,8 +336,13 @@ public override void OnResuming(ScreenTransitionEvent e)
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>();

Expand All @@ -340,6 +351,28 @@ public override bool OnExiting(ScreenExitEvent e)
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.
Expand Down

0 comments on commit 43e612f

Please sign in to comment.