2020-06-18 22:35:03 +08:00
|
|
|
// 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.
|
|
|
|
|
2020-06-18 22:52:35 +08:00
|
|
|
using System;
|
2020-06-18 22:54:20 +08:00
|
|
|
using System.Diagnostics;
|
2020-06-18 22:52:35 +08:00
|
|
|
using JetBrains.Annotations;
|
2020-06-18 22:35:03 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
{
|
2020-06-18 22:52:35 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Ensures screen is not suspended / dimmed while gameplay is active.
|
|
|
|
/// </summary>
|
|
|
|
public class ScreenSuspensionHandler : Component
|
2020-06-18 22:35:03 +08:00
|
|
|
{
|
|
|
|
private readonly GameplayClockContainer gameplayClockContainer;
|
|
|
|
private Bindable<bool> isPaused;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private GameHost host { get; set; }
|
|
|
|
|
2020-06-18 22:52:35 +08:00
|
|
|
public ScreenSuspensionHandler([NotNull] GameplayClockContainer gameplayClockContainer)
|
2020-06-18 22:35:03 +08:00
|
|
|
{
|
2020-06-18 22:52:35 +08:00
|
|
|
this.gameplayClockContainer = gameplayClockContainer ?? throw new ArgumentNullException(nameof(gameplayClockContainer));
|
2020-06-18 22:35:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2020-06-18 22:54:20 +08:00
|
|
|
// This is the only usage game-wide of suspension changes.
|
|
|
|
// Assert to ensure we don't accidentally forget this in the future.
|
|
|
|
Debug.Assert(host.AllowScreenSuspension.Value);
|
|
|
|
|
2020-06-18 22:35:03 +08:00
|
|
|
isPaused = gameplayClockContainer.IsPaused.GetBoundCopy();
|
|
|
|
isPaused.BindValueChanged(paused => host.AllowScreenSuspension.Value = paused.NewValue, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
isPaused?.UnbindAll();
|
|
|
|
|
|
|
|
if (host != null)
|
|
|
|
host.AllowScreenSuspension.Value = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|