Use bindable flow for checking idle time

This commit is contained in:
Dean Herbert
2018-11-26 16:32:59 +09:00
parent fe5b043a59
commit 8d65d49126
3 changed files with 42 additions and 8 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
@ -8,10 +9,30 @@ using osu.Framework.Input.Events;
namespace osu.Game.Input
{
/// <summary>
/// Track whether the end-user is in an idle state, based on their last interaction with the game.
/// </summary>
public class IdleTracker : Component, IKeyBindingHandler<PlatformAction>, IHandleGlobalInput
{
private readonly double timeToIdle;
private double lastInteractionTime;
public double IdleTime => Clock.CurrentTime - lastInteractionTime;
protected double TimeSpentIdle => Clock.CurrentTime - lastInteractionTime;
/// <summary>
/// Whether the user is currently in an idle state.
/// </summary>
public BindableBool IsIdle = new BindableBool();
/// <summary>
/// Intstantiate a new <see cref="IdleTracker"/>.
/// </summary>
/// <param name="timeToIdle">The length in milliseconds until an idle state should be assumed.</param>
public IdleTracker(double timeToIdle)
{
this.timeToIdle = timeToIdle;
}
private bool updateLastInteractionTime()
{
@ -19,6 +40,12 @@ namespace osu.Game.Input
return false;
}
protected override void Update()
{
base.Update();
IsIdle.Value = TimeSpentIdle > timeToIdle;
}
public bool OnPressed(PlatformAction action) => updateLastInteractionTime();
public bool OnReleased(PlatformAction action) => updateLastInteractionTime();