1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 12:57:25 +08:00
osu-lazer/osu.Game/Input/IdleTracker.cs
Bartłomiej Dach 250c7403e8 Fix idle tracker assuming time starts at 0
`IdleTracker` in its construction quietly assumed that the clock it
receives from its parent starts ticking from 0 at the point at which it
is passed down. This is not necessarily the case when headless
executions are involved, which means that the initial state of the
tracker could be computed as idle incorrectly.

Resolve by explicitly reading the clock time at the point of
`LoadComplete()`.
2021-04-17 13:50:00 +02:00

84 lines
2.5 KiB
C#

// 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.
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
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>, IHandleGlobalKeyboardInput
{
private readonly double timeToIdle;
private double lastInteractionTime;
protected double TimeSpentIdle => Clock.CurrentTime - lastInteractionTime;
/// <summary>
/// Whether the user is currently in an idle state.
/// </summary>
public IBindable<bool> IsIdle => isIdle;
private readonly BindableBool isIdle = new BindableBool();
/// <summary>
/// Whether the game can currently enter an idle state.
/// </summary>
protected virtual bool AllowIdle => true;
/// <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;
RelativeSizeAxes = Axes.Both;
}
protected override void LoadComplete()
{
base.LoadComplete();
updateLastInteractionTime();
}
protected override void Update()
{
base.Update();
isIdle.Value = TimeSpentIdle > timeToIdle && AllowIdle;
}
public bool OnPressed(PlatformAction action) => updateLastInteractionTime();
public void OnReleased(PlatformAction action) => updateLastInteractionTime();
protected override bool Handle(UIEvent e)
{
switch (e)
{
case KeyDownEvent _:
case KeyUpEvent _:
case MouseDownEvent _:
case MouseUpEvent _:
case MouseMoveEvent _:
return updateLastInteractionTime();
default:
return base.Handle(e);
}
}
private bool updateLastInteractionTime()
{
lastInteractionTime = Clock.CurrentTime;
return false;
}
}
}