1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-25 11:22:55 +08:00
osu-lazer/osu.Game/Input/IdleTracker.cs

102 lines
3.3 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
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Platform;
using osu.Game.Input.Bindings;
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>, IKeyBindingHandler<GlobalAction>, 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>
2018-11-26 16:07:30 +08:00
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;
}
2021-09-16 17:26:12 +08:00
public bool OnPressed(KeyBindingPressEvent<PlatformAction> e) => updateLastInteractionTime();
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<PlatformAction> e) => updateLastInteractionTime();
2021-09-16 17:26:12 +08:00
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e) => updateLastInteractionTime();
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e) => updateLastInteractionTime();
[Resolved]
private GameHost host { get; set; }
protected override bool Handle(UIEvent e)
{
// Even when not active, `MouseMoveEvent`s will arrive.
// We don't want these to trigger a non-idle state as it's quite often the user interacting
// with other windows while osu! is in the background.
if (!host.IsActive.Value)
return base.Handle(e);
switch (e)
{
2022-06-24 20:25:23 +08:00
case KeyDownEvent:
case KeyUpEvent:
case MouseDownEvent:
case MouseUpEvent:
case MouseMoveEvent:
return updateLastInteractionTime();
2019-04-01 11:44:46 +08:00
default:
return base.Handle(e);
}
}
2018-11-26 15:33:58 +08:00
private bool updateLastInteractionTime()
{
lastInteractionTime = Clock.CurrentTime;
return false;
}
}
}