1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 15:12:57 +08:00
osu-lazer/osu.Game/Screens/Play/PausableGameplayContainer.cs

138 lines
4.0 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
using System.Linq;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Play
{
/// <summary>
/// A container which handles pausing children, displaying an overlay blocking its children during paused state.
2018-04-13 17:19:50 +08:00
/// </summary>
2019-03-05 18:30:55 +08:00
public class PausableGameplayContainer : Container
2018-04-13 17:19:50 +08:00
{
2018-07-11 16:01:27 +08:00
public readonly BindableBool IsPaused = new BindableBool();
2018-04-13 17:19:50 +08:00
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;
2019-02-28 12:31:40 +08:00
public int Retries
{
set => pauseOverlay.Retries = value;
2019-02-28 12:31:40 +08:00
}
2018-04-13 17:19:50 +08:00
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>
2019-03-05 18:30:55 +08:00
/// Creates a new <see cref="PausableGameplayContainer"/>.
/// </summary>
public PausableGameplayContainer()
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both;
InternalChildren = new[]
2018-04-13 17:19:50 +08:00
{
content = new Container
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both
2018-04-13 17:19:50 +08:00
},
pauseOverlay = new PauseOverlay
{
OnResume = () =>
{
IsResuming = true;
this.Delay(400).Schedule(Resume);
},
OnRetry = () => OnRetry(),
OnQuit = () => OnQuit(),
}
};
2018-04-13 17:19:50 +08:00
}
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;
2019-02-21 17:56:34 +08:00
if (IsPaused.Value) return;
2018-04-13 17:19:50 +08:00
// stop the seekable clock (stops the audio eventually)
Stop?.Invoke();
2018-07-11 16:01:27 +08:00
IsPaused.Value = true;
2018-04-13 17:19:50 +08:00
pauseOverlay.Show();
lastPauseActionTime = Time.Current;
});
public void Resume()
{
2019-02-21 17:56:34 +08:00
if (!IsPaused.Value) return;
2018-04-13 17:19:50 +08:00
IsResuming = false;
lastPauseActionTime = Time.Current;
IsPaused.Value = false;
Start?.Invoke();
2018-04-13 17:19:50 +08:00
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)
2018-04-13 17:19:50 +08:00
Pause();
base.Update();
}
public class PauseOverlay : GameplayMenuOverlay
2018-04-13 17:19:50 +08:00
{
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();
2018-06-09 15:14:52 +08:00
[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());
}
2018-04-13 17:19:50 +08:00
}
}
}