mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 21:43:22 +08:00
Merge pull request #4487 from peppy/pause-logic-simplification
Simplify and centralise pause logic Co-authored-by: Dan Balasescu <1329837+smoogipoo@users.noreply.github.com>
This commit is contained in:
commit
123dbb2c5c
@ -184,10 +184,10 @@ namespace osu.Game.Tests.Visual
|
||||
public void PauseTest()
|
||||
{
|
||||
performFullSetup(true);
|
||||
AddStep("Pause", () => player.CurrentPausableGameplayContainer.Pause());
|
||||
AddStep("Pause", () => player.Pause());
|
||||
waitForDim();
|
||||
AddAssert("Screen is dimmed and blur applied", () => songSelect.IsBackgroundDimmed() && songSelect.IsUserBlurApplied());
|
||||
AddStep("Unpause", () => player.CurrentPausableGameplayContainer.Resume());
|
||||
AddStep("Unpause", () => player.Resume());
|
||||
waitForDim();
|
||||
AddAssert("Screen is dimmed and blur applied", () => songSelect.IsBackgroundDimmed() && songSelect.IsUserBlurApplied());
|
||||
}
|
||||
@ -349,8 +349,6 @@ namespace osu.Game.Tests.Visual
|
||||
};
|
||||
}
|
||||
|
||||
public PausableGameplayContainer CurrentPausableGameplayContainer => PausableGameplayContainer;
|
||||
|
||||
public UserDimContainer CurrentStoryboardContainer => StoryboardContainer;
|
||||
|
||||
// Whether or not the player should be allowed to load.
|
||||
|
@ -17,15 +17,15 @@ namespace osu.Game.Tests.Visual
|
||||
[Description("player pause/fail screens")]
|
||||
public class TestCaseGameplayMenuOverlay : ManualInputManagerTestCase
|
||||
{
|
||||
public override IReadOnlyList<Type> RequiredTypes => new[] { typeof(FailOverlay), typeof(PausableGameplayContainer) };
|
||||
public override IReadOnlyList<Type> RequiredTypes => new[] { typeof(FailOverlay), typeof(PauseOverlay) };
|
||||
|
||||
private FailOverlay failOverlay;
|
||||
private PausableGameplayContainer.PauseOverlay pauseOverlay;
|
||||
private PauseOverlay pauseOverlay;
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load()
|
||||
{
|
||||
Add(pauseOverlay = new PausableGameplayContainer.PauseOverlay
|
||||
Add(pauseOverlay = new PauseOverlay
|
||||
{
|
||||
OnResume = () => Logger.Log(@"Resume"),
|
||||
OnRetry = () => Logger.Log(@"Retry"),
|
||||
|
152
osu.Game.Tests/Visual/TestCasePause.cs
Normal file
152
osu.Game.Tests/Visual/TestCasePause.cs
Normal file
@ -0,0 +1,152 @@
|
||||
// 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 NUnit.Framework;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Screens;
|
||||
using osu.Game.Rulesets;
|
||||
using osu.Game.Rulesets.Osu;
|
||||
using osu.Game.Rulesets.Scoring;
|
||||
using osu.Game.Screens.Play;
|
||||
|
||||
namespace osu.Game.Tests.Visual
|
||||
{
|
||||
public class TestCasePause : PlayerTestCase
|
||||
{
|
||||
protected new PausePlayer Player => (PausePlayer)base.Player;
|
||||
|
||||
public TestCasePause()
|
||||
: base(new OsuRuleset())
|
||||
{
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPauseResume()
|
||||
{
|
||||
pauseAndConfirm();
|
||||
resumeAndConfirm();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPauseTooSoon()
|
||||
{
|
||||
pauseAndConfirm();
|
||||
resumeAndConfirm();
|
||||
|
||||
pause();
|
||||
|
||||
confirmClockRunning(true);
|
||||
confirmPauseOverlayShown(false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestExitTooSoon()
|
||||
{
|
||||
pauseAndConfirm();
|
||||
|
||||
resume();
|
||||
|
||||
AddStep("exit too soon", () => Player.Exit());
|
||||
|
||||
confirmClockRunning(true);
|
||||
confirmPauseOverlayShown(false);
|
||||
|
||||
AddAssert("not exited", () => Player.IsCurrentScreen());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPauseAfterFail()
|
||||
{
|
||||
AddUntilStep("wait for fail", () => Player.HasFailed);
|
||||
AddAssert("fail overlay shown", () => Player.FailOverlayVisible);
|
||||
|
||||
confirmClockRunning(false);
|
||||
|
||||
pause();
|
||||
|
||||
confirmClockRunning(false);
|
||||
confirmPauseOverlayShown(false);
|
||||
|
||||
AddAssert("fail overlay still shown", () => Player.FailOverlayVisible);
|
||||
|
||||
exitAndConfirm();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestExitFromGameplay()
|
||||
{
|
||||
AddStep("exit", () => Player.Exit());
|
||||
|
||||
confirmPaused();
|
||||
|
||||
exitAndConfirm();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestExitFromPause()
|
||||
{
|
||||
pauseAndConfirm();
|
||||
exitAndConfirm();
|
||||
}
|
||||
|
||||
private void pauseAndConfirm()
|
||||
{
|
||||
pause();
|
||||
confirmPaused();
|
||||
}
|
||||
|
||||
private void resumeAndConfirm()
|
||||
{
|
||||
resume();
|
||||
confirmResumed();
|
||||
}
|
||||
|
||||
private void exitAndConfirm()
|
||||
{
|
||||
AddUntilStep("player not exited", () => Player.IsCurrentScreen());
|
||||
AddStep("exit", () => Player.Exit());
|
||||
confirmExited();
|
||||
}
|
||||
|
||||
private void confirmPaused()
|
||||
{
|
||||
confirmClockRunning(false);
|
||||
AddAssert("pause overlay shown", () => Player.PauseOverlayVisible);
|
||||
}
|
||||
|
||||
private void confirmResumed()
|
||||
{
|
||||
confirmClockRunning(true);
|
||||
confirmPauseOverlayShown(false);
|
||||
}
|
||||
|
||||
private void confirmExited()
|
||||
{
|
||||
AddUntilStep("player exited", () => !Player.IsCurrentScreen());
|
||||
}
|
||||
|
||||
private void pause() => AddStep("pause", () => Player.Pause());
|
||||
private void resume() => AddStep("resume", () => Player.Resume());
|
||||
|
||||
private void confirmPauseOverlayShown(bool isShown) =>
|
||||
AddAssert("pause overlay " + (isShown ? "shown" : "hidden"), () => Player.PauseOverlayVisible == isShown);
|
||||
|
||||
private void confirmClockRunning(bool isRunning) =>
|
||||
AddAssert("clock " + (isRunning ? "running" : "stopped"), () => Player.GameplayClockContainer.GameplayClock.IsRunning == isRunning);
|
||||
|
||||
protected override bool AllowFail => true;
|
||||
|
||||
protected override Player CreatePlayer(Ruleset ruleset) => new PausePlayer();
|
||||
|
||||
protected class PausePlayer : Player
|
||||
{
|
||||
public new GameplayClockContainer GameplayClockContainer => base.GameplayClockContainer;
|
||||
|
||||
public new ScoreProcessor ScoreProcessor => base.ScoreProcessor;
|
||||
|
||||
public bool FailOverlayVisible => FailOverlay.State == Visibility.Visible;
|
||||
|
||||
public bool PauseOverlayVisible => PauseOverlay.State == Visibility.Visible;
|
||||
}
|
||||
}
|
||||
}
|
@ -169,6 +169,8 @@ namespace osu.Game.Rulesets.UI
|
||||
mod.ApplyToDrawableHitObjects(Playfield.HitObjectContainer.Objects);
|
||||
}
|
||||
|
||||
public override void RequestResume(Action continueResume) => continueResume();
|
||||
|
||||
/// <summary>
|
||||
/// Creates and adds the visual representation of a <see cref="TObject"/> to this <see cref="DrawableRuleset{TObject}"/>.
|
||||
/// </summary>
|
||||
@ -339,6 +341,13 @@ namespace osu.Game.Rulesets.UI
|
||||
/// <param name="replayScore">The replay, null for local input.</param>
|
||||
public abstract void SetReplayScore(Score replayScore);
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when the interactive user requests resuming from a paused state.
|
||||
/// Allows potentially delaying the resume process until an interaction is performed.
|
||||
/// </summary>
|
||||
/// <param name="continueResume">The action to run when resuming is to be completed.</param>
|
||||
public abstract void RequestResume(Action continueResume);
|
||||
|
||||
/// <summary>
|
||||
/// Create a <see cref="ScoreProcessor"/> for the associated ruleset and link with this
|
||||
/// <see cref="DrawableRuleset"/>.
|
||||
|
@ -18,7 +18,7 @@ using osu.Game.Rulesets.Mods;
|
||||
namespace osu.Game.Screens.Play
|
||||
{
|
||||
/// <summary>
|
||||
/// Encapsulates gameplay timing logic and provides a <see cref="GameplayClock"/> for children.
|
||||
/// Encapsulates gameplay timing logic and provides a <see cref="Play.GameplayClock"/> for children.
|
||||
/// </summary>
|
||||
public class GameplayClockContainer : Container
|
||||
{
|
||||
@ -48,7 +48,7 @@ namespace osu.Game.Screens.Play
|
||||
/// The final clock which is exposed to underlying components.
|
||||
/// </summary>
|
||||
[Cached]
|
||||
private readonly GameplayClock gameplayClock;
|
||||
public readonly GameplayClock GameplayClock;
|
||||
|
||||
private Bindable<double> userAudioOffset;
|
||||
|
||||
@ -78,7 +78,7 @@ namespace osu.Game.Screens.Play
|
||||
offsetClock = new FramedOffsetClock(platformOffsetClock);
|
||||
|
||||
// the clock to be exposed via DI to children.
|
||||
gameplayClock = new GameplayClock(offsetClock);
|
||||
GameplayClock = new GameplayClock(offsetClock);
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
@ -118,11 +118,16 @@ namespace osu.Game.Screens.Play
|
||||
// This accounts for the audio clock source potentially taking time to enter a completely stopped state
|
||||
adjustableClock.Seek(adjustableClock.CurrentTime);
|
||||
adjustableClock.Start();
|
||||
IsPaused.Value = false;
|
||||
}
|
||||
|
||||
public void Seek(double time) => adjustableClock.Seek(time);
|
||||
|
||||
public void Stop() => adjustableClock.Stop();
|
||||
public void Stop()
|
||||
{
|
||||
adjustableClock.Stop();
|
||||
IsPaused.Value = true;
|
||||
}
|
||||
|
||||
public void ResetLocalAdjustments()
|
||||
{
|
||||
|
@ -1,137 +0,0 @@
|
||||
// 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.Linq;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Graphics;
|
||||
using osuTK.Graphics;
|
||||
|
||||
namespace osu.Game.Screens.Play
|
||||
{
|
||||
/// <summary>
|
||||
/// A container which handles pausing children, displaying an overlay blocking its children during paused state.
|
||||
/// </summary>
|
||||
public class PausableGameplayContainer : Container
|
||||
{
|
||||
public readonly BindableBool IsPaused = new BindableBool();
|
||||
|
||||
public Func<bool> CheckCanPause;
|
||||
|
||||
private const double pause_cooldown = 1000;
|
||||
private double lastPauseActionTime;
|
||||
|
||||
private readonly PauseOverlay pauseOverlay;
|
||||
|
||||
private readonly Container content;
|
||||
|
||||
protected override Container<Drawable> Content => content;
|
||||
|
||||
public int Retries
|
||||
{
|
||||
set => pauseOverlay.Retries = value;
|
||||
}
|
||||
|
||||
public bool CanPause => (CheckCanPause?.Invoke() ?? true) && Time.Current >= lastPauseActionTime + pause_cooldown;
|
||||
public bool IsResuming { get; private set; }
|
||||
|
||||
public Action OnRetry;
|
||||
public Action OnQuit;
|
||||
|
||||
public Action Stop;
|
||||
public Action Start;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="PausableGameplayContainer"/>.
|
||||
/// </summary>
|
||||
public PausableGameplayContainer()
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both;
|
||||
|
||||
InternalChildren = new[]
|
||||
{
|
||||
content = new Container
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both
|
||||
},
|
||||
pauseOverlay = new PauseOverlay
|
||||
{
|
||||
OnResume = () =>
|
||||
{
|
||||
IsResuming = true;
|
||||
this.Delay(400).Schedule(Resume);
|
||||
},
|
||||
OnRetry = () => OnRetry(),
|
||||
OnQuit = () => OnQuit(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void Pause(bool force = false) => Schedule(() => // Scheduled to ensure a stable position in execution order, no matter how it was called.
|
||||
{
|
||||
if (!CanPause && !force) return;
|
||||
|
||||
if (IsPaused.Value) return;
|
||||
|
||||
// stop the seekable clock (stops the audio eventually)
|
||||
Stop?.Invoke();
|
||||
IsPaused.Value = true;
|
||||
|
||||
pauseOverlay.Show();
|
||||
|
||||
lastPauseActionTime = Time.Current;
|
||||
});
|
||||
|
||||
public void Resume()
|
||||
{
|
||||
if (!IsPaused.Value) return;
|
||||
|
||||
IsResuming = false;
|
||||
lastPauseActionTime = Time.Current;
|
||||
|
||||
IsPaused.Value = false;
|
||||
|
||||
Start?.Invoke();
|
||||
|
||||
pauseOverlay.Hide();
|
||||
}
|
||||
|
||||
private OsuGameBase game;
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OsuGameBase game)
|
||||
{
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
// eagerly pause when we lose window focus (if we are locally playing).
|
||||
if (!game.IsActive.Value && CanPause)
|
||||
Pause();
|
||||
|
||||
base.Update();
|
||||
}
|
||||
|
||||
public class PauseOverlay : GameplayMenuOverlay
|
||||
{
|
||||
public Action OnResume;
|
||||
|
||||
public override string Header => "paused";
|
||||
public override string Description => "you're not going to do what i think you're going to do, are ya?";
|
||||
|
||||
protected override Action BackAction => () => InternalButtons.Children.First().Click();
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OsuColour colours)
|
||||
{
|
||||
AddButton("Continue", colours.Green, () => OnResume?.Invoke());
|
||||
AddButton("Retry", colours.YellowDark, () => OnRetry?.Invoke());
|
||||
AddButton("Quit", new Color4(170, 27, 39, 255), () => OnQuit?.Invoke());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
29
osu.Game/Screens/Play/PauseOverlay.cs
Normal file
29
osu.Game/Screens/Play/PauseOverlay.cs
Normal file
@ -0,0 +1,29 @@
|
||||
// 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.Linq;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Game.Graphics;
|
||||
using osuTK.Graphics;
|
||||
|
||||
namespace osu.Game.Screens.Play
|
||||
{
|
||||
public class PauseOverlay : GameplayMenuOverlay
|
||||
{
|
||||
public Action OnResume;
|
||||
|
||||
public override string Header => "paused";
|
||||
public override string Description => "you're not going to do what i think you're going to do, are ya?";
|
||||
|
||||
protected override Action BackAction => () => InternalButtons.Children.First().Click();
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OsuColour colours)
|
||||
{
|
||||
AddButton("Continue", colours.Green, () => OnResume?.Invoke());
|
||||
AddButton("Retry", colours.YellowDark, () => OnRetry?.Invoke());
|
||||
AddButton("Quit", new Color4(170, 27, 39, 255), () => OnQuit?.Invoke());
|
||||
}
|
||||
}
|
||||
}
|
@ -56,8 +56,6 @@ namespace osu.Game.Screens.Play
|
||||
[Resolved]
|
||||
private ScoreManager scoreManager { get; set; }
|
||||
|
||||
protected PausableGameplayContainer PausableGameplayContainer { get; private set; }
|
||||
|
||||
private RulesetInfo ruleset;
|
||||
|
||||
private IAPIProvider api;
|
||||
@ -68,23 +66,10 @@ namespace osu.Game.Screens.Play
|
||||
protected DrawableRuleset DrawableRuleset { get; private set; }
|
||||
|
||||
protected HUDOverlay HUDOverlay { get; private set; }
|
||||
private FailOverlay failOverlay;
|
||||
|
||||
private DrawableStoryboard storyboard;
|
||||
protected UserDimContainer StoryboardContainer { get; private set; }
|
||||
|
||||
private Bindable<bool> showStoryboard;
|
||||
|
||||
protected virtual UserDimContainer CreateStoryboardContainer() => new UserDimContainer(true)
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Alpha = 1,
|
||||
EnableUserDim = { Value = true }
|
||||
};
|
||||
|
||||
public bool LoadedBeatmapSuccessfully => DrawableRuleset?.Objects.Any() == true;
|
||||
|
||||
private GameplayClockContainer gameplayClockContainer;
|
||||
protected GameplayClockContainer GameplayClockContainer { get; private set; }
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(AudioManager audio, IAPIProvider api, OsuConfigManager config)
|
||||
@ -105,55 +90,49 @@ namespace osu.Game.Screens.Play
|
||||
if (!ScoreProcessor.Mode.Disabled)
|
||||
config.BindWith(OsuSetting.ScoreDisplayMode, ScoreProcessor.Mode);
|
||||
|
||||
InternalChild = gameplayClockContainer = new GameplayClockContainer(working, AllowLeadIn, DrawableRuleset.GameplayStartTime);
|
||||
InternalChild = GameplayClockContainer = new GameplayClockContainer(working, AllowLeadIn, DrawableRuleset.GameplayStartTime);
|
||||
|
||||
gameplayClockContainer.Children = new Drawable[]
|
||||
GameplayClockContainer.Children = new[]
|
||||
{
|
||||
PausableGameplayContainer = new PausableGameplayContainer
|
||||
StoryboardContainer = CreateStoryboardContainer(),
|
||||
new ScalingContainer(ScalingMode.Gameplay)
|
||||
{
|
||||
Retries = RestartCount,
|
||||
OnRetry = Restart,
|
||||
OnQuit = performUserRequestedExit,
|
||||
Start = gameplayClockContainer.Start,
|
||||
Stop = gameplayClockContainer.Stop,
|
||||
IsPaused = { BindTarget = gameplayClockContainer.IsPaused },
|
||||
CheckCanPause = () => AllowPause && ValidForResume && !HasFailed && !DrawableRuleset.HasReplayLoaded.Value,
|
||||
Children = new[]
|
||||
Child = new LocalSkinOverrideContainer(working.Skin)
|
||||
{
|
||||
StoryboardContainer = CreateStoryboardContainer(),
|
||||
new ScalingContainer(ScalingMode.Gameplay)
|
||||
{
|
||||
Child = new LocalSkinOverrideContainer(working.Skin)
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Child = DrawableRuleset
|
||||
}
|
||||
},
|
||||
new BreakOverlay(working.Beatmap.BeatmapInfo.LetterboxInBreaks, ScoreProcessor)
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
Breaks = working.Beatmap.Breaks
|
||||
},
|
||||
// display the cursor above some HUD elements.
|
||||
DrawableRuleset.Cursor?.CreateProxy() ?? new Container(),
|
||||
HUDOverlay = new HUDOverlay(ScoreProcessor, DrawableRuleset, working)
|
||||
{
|
||||
HoldToQuit = { Action = performUserRequestedExit },
|
||||
PlayerSettingsOverlay = { PlaybackSettings = { UserPlaybackRate = { BindTarget = gameplayClockContainer.UserPlaybackRate } } },
|
||||
KeyCounter = { Visible = { BindTarget = DrawableRuleset.HasReplayLoaded } },
|
||||
RequestSeek = gameplayClockContainer.Seek,
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre
|
||||
},
|
||||
new SkipOverlay(DrawableRuleset.GameplayStartTime)
|
||||
{
|
||||
RequestSeek = gameplayClockContainer.Seek
|
||||
},
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Child = DrawableRuleset
|
||||
}
|
||||
},
|
||||
failOverlay = new FailOverlay
|
||||
new BreakOverlay(working.Beatmap.BeatmapInfo.LetterboxInBreaks, ScoreProcessor)
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
Breaks = working.Beatmap.Breaks
|
||||
},
|
||||
// display the cursor above some HUD elements.
|
||||
DrawableRuleset.Cursor?.CreateProxy() ?? new Container(),
|
||||
HUDOverlay = new HUDOverlay(ScoreProcessor, DrawableRuleset, working)
|
||||
{
|
||||
HoldToQuit = { Action = performUserRequestedExit },
|
||||
PlayerSettingsOverlay = { PlaybackSettings = { UserPlaybackRate = { BindTarget = GameplayClockContainer.UserPlaybackRate } } },
|
||||
KeyCounter = { Visible = { BindTarget = DrawableRuleset.HasReplayLoaded } },
|
||||
RequestSeek = GameplayClockContainer.Seek,
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre
|
||||
},
|
||||
new SkipOverlay(DrawableRuleset.GameplayStartTime)
|
||||
{
|
||||
RequestSeek = GameplayClockContainer.Seek
|
||||
},
|
||||
FailOverlay = new FailOverlay
|
||||
{
|
||||
OnRetry = Restart,
|
||||
OnQuit = performUserRequestedExit,
|
||||
},
|
||||
PauseOverlay = new PauseOverlay
|
||||
{
|
||||
OnResume = Resume,
|
||||
Retries = RestartCount,
|
||||
OnRetry = Restart,
|
||||
OnQuit = performUserRequestedExit,
|
||||
},
|
||||
@ -170,10 +149,10 @@ namespace osu.Game.Screens.Play
|
||||
};
|
||||
|
||||
// bind clock into components that require it
|
||||
DrawableRuleset.IsPaused.BindTo(gameplayClockContainer.IsPaused);
|
||||
DrawableRuleset.IsPaused.BindTo(GameplayClockContainer.IsPaused);
|
||||
|
||||
if (showStoryboard.Value)
|
||||
initializeStoryboard(false);
|
||||
// load storyboard as part of player's load if we can
|
||||
initializeStoryboard(false);
|
||||
|
||||
// Bind ScoreProcessor to ourselves
|
||||
ScoreProcessor.AllJudged += onCompletion;
|
||||
@ -289,19 +268,148 @@ namespace osu.Game.Screens.Play
|
||||
return score;
|
||||
}
|
||||
|
||||
protected override bool OnScroll(ScrollEvent e) => mouseWheelDisabled.Value && !GameplayClockContainer.IsPaused.Value;
|
||||
|
||||
protected virtual Results CreateResults(ScoreInfo score) => new SoloResults(score);
|
||||
|
||||
#region Storyboard
|
||||
|
||||
private DrawableStoryboard storyboard;
|
||||
protected UserDimContainer StoryboardContainer { get; private set; }
|
||||
|
||||
protected virtual UserDimContainer CreateStoryboardContainer() => new UserDimContainer(true)
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Alpha = 1,
|
||||
EnableUserDim = { Value = true }
|
||||
};
|
||||
|
||||
private Bindable<bool> showStoryboard;
|
||||
|
||||
private void initializeStoryboard(bool asyncLoad)
|
||||
{
|
||||
if (StoryboardContainer == null || storyboard != null)
|
||||
return;
|
||||
|
||||
if (!showStoryboard.Value)
|
||||
return;
|
||||
|
||||
var beatmap = Beatmap.Value;
|
||||
|
||||
storyboard = beatmap.Storyboard.CreateDrawable();
|
||||
storyboard.Masking = true;
|
||||
|
||||
if (asyncLoad)
|
||||
LoadComponentAsync(storyboard, StoryboardContainer.Add);
|
||||
else
|
||||
StoryboardContainer.Add(storyboard);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fail Logic
|
||||
|
||||
protected FailOverlay FailOverlay { get; private set; }
|
||||
|
||||
private bool onFail()
|
||||
{
|
||||
if (Beatmap.Value.Mods.Value.OfType<IApplicableFailOverride>().Any(m => !m.AllowFail))
|
||||
return false;
|
||||
|
||||
gameplayClockContainer.Stop();
|
||||
GameplayClockContainer.Stop();
|
||||
|
||||
HasFailed = true;
|
||||
failOverlay.Retries = RestartCount;
|
||||
failOverlay.Show();
|
||||
|
||||
// There is a chance that we could be in a paused state as the ruleset's internal clock (see FrameStabilityContainer)
|
||||
// could process an extra frame after the GameplayClock is stopped.
|
||||
// In such cases we want the fail state to precede a user triggered pause.
|
||||
if (PauseOverlay.State == Visibility.Visible)
|
||||
PauseOverlay.Hide();
|
||||
|
||||
FailOverlay.Retries = RestartCount;
|
||||
FailOverlay.Show();
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Pause Logic
|
||||
|
||||
public bool IsResuming { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The amount of gameplay time after which a second pause is allowed.
|
||||
/// </summary>
|
||||
private const double pause_cooldown = 1000;
|
||||
|
||||
protected PauseOverlay PauseOverlay { get; private set; }
|
||||
|
||||
private double? lastPauseActionTime;
|
||||
|
||||
private bool canPause =>
|
||||
// must pass basic screen conditions (beatmap loaded, instance allows pause)
|
||||
LoadedBeatmapSuccessfully && AllowPause && ValidForResume
|
||||
// replays cannot be paused and exit immediately
|
||||
&& !DrawableRuleset.HasReplayLoaded.Value
|
||||
// cannot pause if we are already in a fail state
|
||||
&& !HasFailed
|
||||
// cannot pause if already paused (or in a cooldown state) unless we are in a resuming state.
|
||||
&& (IsResuming || (GameplayClockContainer.IsPaused.Value == false && !pauseCooldownActive));
|
||||
|
||||
private bool pauseCooldownActive =>
|
||||
lastPauseActionTime.HasValue && GameplayClockContainer.GameplayClock.CurrentTime < lastPauseActionTime + pause_cooldown;
|
||||
|
||||
private bool canResume =>
|
||||
// cannot resume from a non-paused state
|
||||
GameplayClockContainer.IsPaused.Value
|
||||
// cannot resume if we are already in a fail state
|
||||
&& !HasFailed
|
||||
// already resuming
|
||||
&& !IsResuming;
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
|
||||
// eagerly pause when we lose window focus (if we are locally playing).
|
||||
if (!Game.IsActive.Value)
|
||||
Pause();
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
if (!canPause) return;
|
||||
|
||||
IsResuming = false;
|
||||
GameplayClockContainer.Stop();
|
||||
PauseOverlay.Show();
|
||||
lastPauseActionTime = GameplayClockContainer.GameplayClock.CurrentTime;
|
||||
}
|
||||
|
||||
public void Resume()
|
||||
{
|
||||
if (!canResume) return;
|
||||
|
||||
IsResuming = true;
|
||||
PauseOverlay.Hide();
|
||||
|
||||
// time-based conditions may allow instant resume.
|
||||
if (GameplayClockContainer.GameplayClock.CurrentTime < Beatmap.Value.Beatmap.HitObjects.First().StartTime)
|
||||
completeResume();
|
||||
else
|
||||
DrawableRuleset.RequestResume(completeResume);
|
||||
|
||||
void completeResume()
|
||||
{
|
||||
GameplayClockContainer.Start();
|
||||
IsResuming = false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Screen Logic
|
||||
|
||||
public override void OnEntering(IScreen last)
|
||||
{
|
||||
base.OnEntering(last);
|
||||
@ -316,10 +424,7 @@ namespace osu.Game.Screens.Play
|
||||
.Delay(250)
|
||||
.FadeIn(250);
|
||||
|
||||
showStoryboard.ValueChanged += enabled =>
|
||||
{
|
||||
if (enabled.NewValue) initializeStoryboard(true);
|
||||
};
|
||||
showStoryboard.ValueChanged += _ => initializeStoryboard(true);
|
||||
|
||||
Background.EnableUserDim.Value = true;
|
||||
Background.BlurAmount.Value = 0;
|
||||
@ -329,10 +434,8 @@ namespace osu.Game.Screens.Play
|
||||
|
||||
storyboardReplacesBackground.Value = Beatmap.Value.Storyboard.ReplacesBackground && Beatmap.Value.Storyboard.HasDrawable;
|
||||
|
||||
gameplayClockContainer.Restart();
|
||||
|
||||
PausableGameplayContainer.Alpha = 0;
|
||||
PausableGameplayContainer.FadeIn(750, Easing.OutQuint);
|
||||
GameplayClockContainer.Restart();
|
||||
GameplayClockContainer.FadeInFromZero(750, Easing.OutQuint);
|
||||
}
|
||||
|
||||
public override void OnSuspending(IScreen next)
|
||||
@ -350,18 +453,20 @@ namespace osu.Game.Screens.Play
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((!AllowPause || HasFailed || !ValidForResume || PausableGameplayContainer?.IsPaused.Value != false || DrawableRuleset?.HasReplayLoaded.Value != false) && (!PausableGameplayContainer?.IsResuming ?? true))
|
||||
if (canPause)
|
||||
{
|
||||
gameplayClockContainer.ResetLocalAdjustments();
|
||||
|
||||
fadeOut();
|
||||
return base.OnExiting(next);
|
||||
Pause();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (LoadedBeatmapSuccessfully)
|
||||
PausableGameplayContainer?.Pause();
|
||||
if (pauseCooldownActive && !GameplayClockContainer.IsPaused.Value)
|
||||
// still want to block if we are within the cooldown period and not already paused.
|
||||
return true;
|
||||
|
||||
return true;
|
||||
GameplayClockContainer.ResetLocalAdjustments();
|
||||
|
||||
fadeOut();
|
||||
return base.OnExiting(next);
|
||||
}
|
||||
|
||||
private void fadeOut(bool instant = false)
|
||||
@ -373,24 +478,6 @@ namespace osu.Game.Screens.Play
|
||||
storyboardReplacesBackground.Value = false;
|
||||
}
|
||||
|
||||
protected override bool OnScroll(ScrollEvent e) => mouseWheelDisabled.Value && !PausableGameplayContainer.IsPaused.Value;
|
||||
|
||||
private void initializeStoryboard(bool asyncLoad)
|
||||
{
|
||||
if (StoryboardContainer == null || storyboard != null)
|
||||
return;
|
||||
|
||||
var beatmap = Beatmap.Value;
|
||||
|
||||
storyboard = beatmap.Storyboard.CreateDrawable();
|
||||
storyboard.Masking = true;
|
||||
|
||||
if (asyncLoad)
|
||||
LoadComponentAsync(storyboard, StoryboardContainer.Add);
|
||||
else
|
||||
StoryboardContainer.Add(storyboard);
|
||||
}
|
||||
|
||||
protected virtual Results CreateResults(ScoreInfo score) => new SoloResults(score);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -1,131 +0,0 @@
|
||||
// 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.Linq;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics.Shapes;
|
||||
using osu.Framework.Lists;
|
||||
using osu.Framework.Screens;
|
||||
using osu.Game.Beatmaps;
|
||||
using osu.Game.Rulesets;
|
||||
using osu.Game.Rulesets.Mods;
|
||||
using osu.Game.Screens.Play;
|
||||
using osu.Game.Tests.Beatmaps;
|
||||
using osuTK.Graphics;
|
||||
|
||||
namespace osu.Game.Tests.Visual
|
||||
{
|
||||
public abstract class TestCasePlayer : RateAdjustedBeatmapTestCase
|
||||
{
|
||||
private readonly Ruleset ruleset;
|
||||
|
||||
protected Player Player;
|
||||
|
||||
protected TestCasePlayer(Ruleset ruleset)
|
||||
{
|
||||
this.ruleset = ruleset;
|
||||
}
|
||||
|
||||
protected TestCasePlayer()
|
||||
{
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(RulesetStore rulesets)
|
||||
{
|
||||
Add(new Box
|
||||
{
|
||||
RelativeSizeAxes = Framework.Graphics.Axes.Both,
|
||||
Colour = Color4.Black,
|
||||
Depth = int.MaxValue
|
||||
});
|
||||
|
||||
if (ruleset != null)
|
||||
{
|
||||
Player p = null;
|
||||
AddStep(ruleset.RulesetInfo.Name, () => p = loadPlayerFor(ruleset));
|
||||
AddCheckSteps(() => p);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var r in rulesets.AvailableRulesets)
|
||||
{
|
||||
Player p = null;
|
||||
AddStep(r.Name, () => p = loadPlayerFor(r));
|
||||
AddCheckSteps(() => p);
|
||||
|
||||
AddUntilStep("no leaked beatmaps", () =>
|
||||
{
|
||||
p = null;
|
||||
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
int count = 0;
|
||||
|
||||
workingWeakReferences.ForEachAlive(_ => count++);
|
||||
return count == 1;
|
||||
});
|
||||
|
||||
AddUntilStep("no leaked players", () =>
|
||||
{
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
int count = 0;
|
||||
|
||||
playerWeakReferences.ForEachAlive(_ => count++);
|
||||
return count == 1;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void AddCheckSteps(Func<Player> player)
|
||||
{
|
||||
AddUntilStep("player loaded", () => player().IsLoaded);
|
||||
}
|
||||
|
||||
protected virtual IBeatmap CreateBeatmap(Ruleset ruleset) => new TestBeatmap(ruleset.RulesetInfo);
|
||||
|
||||
private readonly WeakList<WorkingBeatmap> workingWeakReferences = new WeakList<WorkingBeatmap>();
|
||||
private readonly WeakList<Player> playerWeakReferences = new WeakList<Player>();
|
||||
|
||||
private Player loadPlayerFor(RulesetInfo ri)
|
||||
{
|
||||
Ruleset.Value = ri;
|
||||
return loadPlayerFor(ri.CreateInstance());
|
||||
}
|
||||
|
||||
private Player loadPlayerFor(Ruleset r)
|
||||
{
|
||||
var beatmap = CreateBeatmap(r);
|
||||
var working = new TestWorkingBeatmap(beatmap, Clock);
|
||||
|
||||
workingWeakReferences.Add(working);
|
||||
|
||||
Beatmap.Value = working;
|
||||
Beatmap.Value.Mods.Value = new[] { r.GetAllMods().First(m => m is ModNoFail) };
|
||||
|
||||
Player?.Exit();
|
||||
|
||||
var player = CreatePlayer(r);
|
||||
|
||||
playerWeakReferences.Add(player);
|
||||
|
||||
LoadComponentAsync(player, p =>
|
||||
{
|
||||
Player = p;
|
||||
LoadScreen(p);
|
||||
});
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
protected virtual Player CreatePlayer(Ruleset ruleset) => new Player
|
||||
{
|
||||
AllowPause = false,
|
||||
AllowLeadIn = false,
|
||||
AllowResults = false,
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user