1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 16:32:55 +08:00
osu-lazer/osu.Game/Screens/Play/ScreenSuspensionHandler.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

56 lines
1.8 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.
2022-06-17 15:37:17 +08:00
#nullable disable
2020-06-18 22:52:35 +08:00
using System;
using JetBrains.Annotations;
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 partial class ScreenSuspensionHandler : Component
{
private readonly GameplayClockContainer gameplayClockContainer;
private IBindable<bool> isPaused;
private readonly Bindable<bool> disableSuspensionBindable = new Bindable<bool>();
[Resolved]
private GameHost host { get; set; }
2020-06-18 22:52:35 +08:00
public ScreenSuspensionHandler([NotNull] GameplayClockContainer gameplayClockContainer)
{
2020-06-18 22:52:35 +08:00
this.gameplayClockContainer = gameplayClockContainer ?? throw new ArgumentNullException(nameof(gameplayClockContainer));
}
protected override void LoadComplete()
{
base.LoadComplete();
isPaused = gameplayClockContainer.IsPaused.GetBoundCopy();
isPaused.BindValueChanged(paused =>
{
if (paused.NewValue)
host.AllowScreenSuspension.RemoveSource(disableSuspensionBindable);
else
host.AllowScreenSuspension.AddSource(disableSuspensionBindable);
}, true);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
isPaused?.UnbindAll();
host?.AllowScreenSuspension.RemoveSource(disableSuspensionBindable);
}
}
}