mirror of
https://github.com/osukey/osukey.git
synced 2025-07-03 01:09:57 +09:00
Use bindable flow for checking idle time
This commit is contained in:
@ -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();
|
||||
|
Reference in New Issue
Block a user