Skip to content

Commit

Permalink
Refactor to avoid dual-boolean mess
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Nov 14, 2024
1 parent afeb138 commit 3262b6d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private Storyboard createStoryboard(double duration)

protected partial class OutroPlayer : TestPlayer
{
public void ExitViaPause() => PerformExit(true);
public void ExitViaPause() => PerformExitWithConfirmation();

public new FailOverlay FailOverlay => base.FailOverlay;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void failAndBail(string message = null)
if (!string.IsNullOrEmpty(message))
Logger.Log(message, LoggingTarget.Runtime, LogLevel.Important);

Schedule(() => PerformExit(false));
Schedule(() => PerformExit());
}

private void onGameplayStarted() => Scheduler.Add(() =>
Expand Down
48 changes: 31 additions & 17 deletions osu.Game/Screens/Play/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public abstract partial class Player : ScreenWithBeatmapBackground, ISamplePlayb
public Action<bool> RestartRequested;

private bool isRestarting;
private bool noExitTransition;
private bool skipExitTransition;

private Bindable<bool> mouseWheelDisabled;

Expand Down Expand Up @@ -290,15 +290,15 @@ private void load(OsuConfigManager config, OsuGameBase game, CancellationToken c
{
SaveReplay = async () => await prepareAndImportScoreAsync(true).ConfigureAwait(false),
OnRetry = Configuration.AllowUserInteraction ? () => Restart() : null,
OnQuit = () => PerformExit(true),
OnQuit = () => PerformExitWithConfirmation(),
},
new HotkeyExitOverlay
{
Action = () =>
{
if (!this.IsCurrentScreen()) return;

PerformExit(false, true);
PerformExit(true);
},
},
});
Expand Down Expand Up @@ -443,7 +443,7 @@ private Drawable createOverlayComponents(IWorkingBeatmap working)
{
HoldToQuit =
{
Action = () => PerformExit(true),
Action = () => PerformExitWithConfirmation(),
IsPaused = { BindTarget = GameplayClockContainer.IsPaused },
ReplayLoaded = { BindTarget = DrawableRuleset.HasReplayLoaded },
},
Expand Down Expand Up @@ -480,7 +480,7 @@ private Drawable createOverlayComponents(IWorkingBeatmap working)
OnResume = Resume,
Retries = RestartCount,
OnRetry = () => Restart(),
OnQuit = () => PerformExit(true),
OnQuit = () => PerformExitWithConfirmation(),
},
},
};
Expand Down Expand Up @@ -583,26 +583,24 @@ private IBeatmap loadPlayableBeatmap(Mod[] gameplayMods, CancellationToken cance
}

/// <summary>
/// Attempts to complete a user request to exit gameplay.
/// Attempts to complete a user request to exit gameplay, with confirmation.
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item>This should only be called in response to a user interaction. Exiting is not guaranteed.</item>
/// <item>This will interrupt any pending progression to the results screen, even if the transition has begun.</item>
/// </list>
///
/// This method will show the pause or fail dialog before performing an exit.
/// If a dialog is not yet displayed, the exit will be blocked and the relevant dialog will display instead.
/// </remarks>
/// <param name="showDialogFirst">
/// Whether the pause or fail dialog should be shown before performing an exit.
/// If <see langword="true"/> and a dialog is not yet displayed, the exit will be blocked and the relevant dialog will display instead.
/// </param>
/// <param name="withoutTransition">Whether the exit should perform without a transition, because the screen had faded to black already.</param>
/// <returns>Whether this call resulted in a final exit.</returns>
protected bool PerformExit(bool showDialogFirst, bool withoutTransition = false)
/// <returns></returns>
protected bool PerformExitWithConfirmation()
{
bool pauseOrFailDialogVisible =
PauseOverlay.State.Value == Visibility.Visible || FailOverlay.State.Value == Visibility.Visible;

if (showDialogFirst && !pauseOrFailDialogVisible)
if (!pauseOrFailDialogVisible)
{
// if the fail animation is currently in progress, accelerate it (it will show the pause dialog on completion).
if (ValidForResume && GameplayState.HasFailed)
Expand All @@ -621,6 +619,22 @@ protected bool PerformExit(bool showDialogFirst, bool withoutTransition = false)
}
}

return PerformExit();
}

/// <summary>
/// Attempts to complete a user request to exit gameplay.
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item>This should only be called in response to a user interaction. Exiting is not guaranteed.</item>
/// <item>This will interrupt any pending progression to the results screen, even if the transition has begun.</item>
/// </list>
/// </remarks>
/// <param name="skipTransition">Whether the exit should perform without a transition, because the screen had faded to black already.</param>
/// <returns>Whether this call resulted in a final exit.</returns>
protected bool PerformExit(bool skipTransition = false)
{
// Matching osu!stable behaviour, if the results screen is pending and the user requests an exit,
// show the results instead.
if (GameplayState.HasPassed && !isRestarting)
Expand All @@ -635,7 +649,7 @@ protected bool PerformExit(bool showDialogFirst, bool withoutTransition = false)
// Screen may not be current if a restart has been performed.
if (this.IsCurrentScreen())
{
noExitTransition = withoutTransition;
skipExitTransition = skipTransition;

// The actual exit is performed if
// - the pause / fail dialog was not requested
Expand Down Expand Up @@ -707,7 +721,7 @@ public bool Restart(bool quickRestart = false)

RestartRequested?.Invoke(quickRestart);

return PerformExit(false, quickRestart);
return PerformExit(quickRestart);
}

/// <summary>
Expand Down Expand Up @@ -1254,7 +1268,7 @@ protected virtual Task ImportScore(Score score)

private void fadeOut()
{
if (!noExitTransition)
if (!skipExitTransition)
this.FadeOut(250);

if (this.IsCurrentScreen())
Expand Down

0 comments on commit 3262b6d

Please sign in to comment.