2019-01-24 08:43:03 +00: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.
|
2018-11-19 17:48:59 +00:00
|
|
|
|
|
2019-02-21 10:04:31 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-11-19 17:48:59 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
|
using osu.Framework.Input.Events;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Input
|
|
|
|
|
{
|
2018-11-26 07:32:59 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Track whether the end-user is in an idle state, based on their last interaction with the game.
|
|
|
|
|
/// </summary>
|
2018-11-19 17:50:46 +00:00
|
|
|
|
public class IdleTracker : Component, IKeyBindingHandler<PlatformAction>, IHandleGlobalInput
|
2018-11-19 17:48:59 +00:00
|
|
|
|
{
|
2018-11-26 07:32:59 +00:00
|
|
|
|
private readonly double timeToIdle;
|
|
|
|
|
|
2018-11-19 17:48:59 +00:00
|
|
|
|
private double lastInteractionTime;
|
2018-11-26 07:32:59 +00:00
|
|
|
|
|
|
|
|
|
protected double TimeSpentIdle => Clock.CurrentTime - lastInteractionTime;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the user is currently in an idle state.
|
|
|
|
|
/// </summary>
|
2018-11-26 08:07:30 +00:00
|
|
|
|
public IBindable<bool> IsIdle => isIdle;
|
|
|
|
|
|
|
|
|
|
private readonly BindableBool isIdle = new BindableBool();
|
2018-11-26 07:32:59 +00:00
|
|
|
|
|
2018-12-26 09:39:57 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the game can currently enter an idle state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool AllowIdle => true;
|
|
|
|
|
|
2018-11-26 07:32:59 +00:00
|
|
|
|
/// <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;
|
2018-11-26 08:07:20 +00:00
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2018-11-26 07:32:59 +00:00
|
|
|
|
}
|
2018-11-19 17:48:59 +00:00
|
|
|
|
|
2018-11-26 07:32:59 +00:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
2018-12-26 09:39:57 +00:00
|
|
|
|
isIdle.Value = TimeSpentIdle > timeToIdle && AllowIdle;
|
2018-11-26 07:32:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-19 17:48:59 +00:00
|
|
|
|
public bool OnPressed(PlatformAction action) => updateLastInteractionTime();
|
|
|
|
|
|
|
|
|
|
public bool OnReleased(PlatformAction action) => updateLastInteractionTime();
|
|
|
|
|
|
|
|
|
|
protected override bool Handle(UIEvent e)
|
|
|
|
|
{
|
|
|
|
|
switch (e)
|
|
|
|
|
{
|
|
|
|
|
case KeyDownEvent _:
|
|
|
|
|
case KeyUpEvent _:
|
|
|
|
|
case MouseDownEvent _:
|
|
|
|
|
case MouseUpEvent _:
|
|
|
|
|
case MouseMoveEvent _:
|
|
|
|
|
return updateLastInteractionTime();
|
2019-04-01 03:44:46 +00:00
|
|
|
|
|
2018-11-19 17:48:59 +00:00
|
|
|
|
default:
|
|
|
|
|
return base.Handle(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-26 07:33:58 +00:00
|
|
|
|
|
|
|
|
|
private bool updateLastInteractionTime()
|
|
|
|
|
{
|
|
|
|
|
lastInteractionTime = Clock.CurrentTime;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-11-19 17:48:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|