2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-05-17 20:57:01 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using osu.Framework.Timing;
|
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A container which handles pausing children, displaying a pause overlay with choices etc.
|
|
|
|
|
/// This alleviates a lot of the intricate pause logic from being in <see cref="Player"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class PauseContainer : Container
|
|
|
|
|
{
|
|
|
|
|
public bool IsPaused { get; private set; }
|
|
|
|
|
|
|
|
|
|
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;
|
2018-02-10 02:20:49 +08:00
|
|
|
|
public bool IsResuming { get; private set; }
|
2017-05-17 20:57:01 +08:00
|
|
|
|
|
|
|
|
|
public Action OnRetry;
|
|
|
|
|
public Action OnQuit;
|
|
|
|
|
|
|
|
|
|
public Action OnResume;
|
|
|
|
|
public Action OnPause;
|
|
|
|
|
|
2018-02-27 16:23:12 +08:00
|
|
|
|
private readonly IAdjustableClock adjustableClock;
|
|
|
|
|
private readonly FramedClock framedClock;
|
2017-05-17 20:57:01 +08:00
|
|
|
|
|
2018-02-27 15:10:12 +08:00
|
|
|
|
public PauseContainer(FramedClock framedClock, IAdjustableClock adjustableClock)
|
2017-05-17 20:57:01 +08:00
|
|
|
|
{
|
2018-02-27 16:23:12 +08:00
|
|
|
|
this.framedClock = framedClock;
|
|
|
|
|
this.adjustableClock = adjustableClock;
|
2018-02-27 15:02:57 +08:00
|
|
|
|
|
2017-05-17 20:57:01 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
|
2018-02-27 15:02:57 +08:00
|
|
|
|
AddInternal(content = new Container
|
|
|
|
|
{
|
2018-02-27 16:23:12 +08:00
|
|
|
|
Clock = this.framedClock,
|
2018-02-28 20:50:52 +08:00
|
|
|
|
ProcessCustomClock = false,
|
2018-02-27 15:02:57 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
|
});
|
2017-05-17 20:57:01 +08:00
|
|
|
|
|
|
|
|
|
AddInternal(pauseOverlay = new PauseOverlay
|
|
|
|
|
{
|
2018-02-10 02:20:49 +08:00
|
|
|
|
OnResume = () =>
|
|
|
|
|
{
|
|
|
|
|
IsResuming = true;
|
|
|
|
|
this.Delay(400).Schedule(Resume);
|
|
|
|
|
},
|
2017-05-17 20:57:01 +08:00
|
|
|
|
OnRetry = () => OnRetry(),
|
|
|
|
|
OnQuit = () => OnQuit(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 15:02:57 +08:00
|
|
|
|
public void Pause(bool force = false) => Schedule(() => // Scheduled to ensure a stable position in execution order, no matter how it was called.
|
2017-05-17 20:57:01 +08:00
|
|
|
|
{
|
|
|
|
|
if (!CanPause && !force) return;
|
|
|
|
|
|
|
|
|
|
if (IsPaused) return;
|
|
|
|
|
|
2018-02-27 15:02:57 +08:00
|
|
|
|
// stop the seekable clock (stops the audio eventually)
|
2018-02-27 16:23:12 +08:00
|
|
|
|
adjustableClock.Stop();
|
2017-05-17 20:57:01 +08:00
|
|
|
|
IsPaused = true;
|
|
|
|
|
|
2018-02-27 15:02:57 +08:00
|
|
|
|
OnPause?.Invoke();
|
|
|
|
|
pauseOverlay.Show();
|
2017-05-17 20:57:01 +08:00
|
|
|
|
|
2018-02-27 15:02:57 +08:00
|
|
|
|
lastPauseActionTime = Time.Current;
|
|
|
|
|
});
|
2017-05-17 20:57:01 +08:00
|
|
|
|
|
|
|
|
|
public void Resume()
|
|
|
|
|
{
|
|
|
|
|
if (!IsPaused) return;
|
|
|
|
|
|
|
|
|
|
IsPaused = false;
|
2018-02-27 15:02:57 +08:00
|
|
|
|
IsResuming = false;
|
2017-05-17 20:57:01 +08:00
|
|
|
|
lastPauseActionTime = Time.Current;
|
|
|
|
|
|
2018-02-27 15:02:57 +08:00
|
|
|
|
// seek back to the time of the framed clock.
|
|
|
|
|
// this accounts for the audio clock potentially taking time to enter a completely stopped state.
|
2018-02-27 16:23:12 +08:00
|
|
|
|
adjustableClock.Seek(framedClock.CurrentTime);
|
|
|
|
|
adjustableClock.Start();
|
2017-05-17 20:57:01 +08:00
|
|
|
|
|
2018-02-27 15:02:57 +08:00
|
|
|
|
OnResume?.Invoke();
|
2017-05-17 20:57:01 +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 && CanPause)
|
|
|
|
|
Pause();
|
|
|
|
|
|
2018-02-27 15:02:57 +08:00
|
|
|
|
if (!IsPaused)
|
2018-02-27 16:23:12 +08:00
|
|
|
|
framedClock.ProcessFrame();
|
2018-02-27 15:02:57 +08:00
|
|
|
|
|
2017-05-17 20:57:01 +08:00
|
|
|
|
base.Update();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-18 15:40:50 +08:00
|
|
|
|
public class PauseOverlay : GameplayMenuOverlay
|
2017-05-17 20:57:01 +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 bool OnKeyDown(InputState state, KeyDownEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (!args.Repeat && args.Key == Key.Escape)
|
|
|
|
|
{
|
2017-12-18 18:13:08 +08:00
|
|
|
|
InternalButtons.Children.First().TriggerOnClick();
|
2017-05-17 20:57:01 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.OnKeyDown(state, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
2017-12-18 18:13:25 +08:00
|
|
|
|
AddButton("Continue", colours.Green, () => OnResume?.Invoke());
|
|
|
|
|
AddButton("Retry", colours.YellowDark, () => OnRetry?.Invoke());
|
|
|
|
|
AddButton("Quit", new Color4(170, 27, 39, 255), () => OnQuit?.Invoke());
|
2017-05-17 20:57:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|