2019-01-24 16:43:03 +08: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-20 01:48:59 +08:00
|
|
|
|
|
2021-12-26 15:26:33 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-11-20 01:48:59 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
|
using osu.Framework.Input.Events;
|
2021-12-26 15:26:33 +08:00
|
|
|
|
using osu.Framework.Platform;
|
2021-05-11 12:40:29 +08:00
|
|
|
|
using osu.Game.Input.Bindings;
|
2018-11-20 01:48:59 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Input
|
|
|
|
|
{
|
2018-11-26 15:32:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Track whether the end-user is in an idle state, based on their last interaction with the game.
|
|
|
|
|
/// </summary>
|
2022-11-24 13:32:20 +08:00
|
|
|
|
public partial class IdleTracker : Component, IKeyBindingHandler<PlatformAction>, IKeyBindingHandler<GlobalAction>, IHandleGlobalKeyboardInput
|
2018-11-20 01:48:59 +08:00
|
|
|
|
{
|
2018-11-26 15:32:59 +08:00
|
|
|
|
private readonly double timeToIdle;
|
|
|
|
|
|
2018-11-20 01:48:59 +08:00
|
|
|
|
private double lastInteractionTime;
|
2018-11-26 15:32:59 +08:00
|
|
|
|
|
|
|
|
|
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();
|
2018-11-26 15:32:59 +08:00
|
|
|
|
|
2018-12-26 17:39:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the game can currently enter an idle state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool AllowIdle => true;
|
|
|
|
|
|
2018-11-26 15:32:59 +08: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 16:07:20 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2018-11-26 15:32:59 +08:00
|
|
|
|
}
|
2018-11-20 01:48:59 +08:00
|
|
|
|
|
2021-04-17 19:50:00 +08:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
updateLastInteractionTime();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 15:32:59 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
2018-12-26 17:39:57 +08:00
|
|
|
|
isIdle.Value = TimeSpentIdle > timeToIdle && AllowIdle;
|
2018-11-26 15:32:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 17:26:12 +08:00
|
|
|
|
public bool OnPressed(KeyBindingPressEvent<PlatformAction> e) => updateLastInteractionTime();
|
2018-11-20 01:48:59 +08:00
|
|
|
|
|
2021-09-16 17:26:12 +08:00
|
|
|
|
public void OnReleased(KeyBindingReleaseEvent<PlatformAction> e) => updateLastInteractionTime();
|
2018-11-20 01:48:59 +08:00
|
|
|
|
|
2021-09-16 17:26:12 +08:00
|
|
|
|
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e) => updateLastInteractionTime();
|
2021-05-11 12:40:29 +08:00
|
|
|
|
|
2021-09-16 17:26:12 +08:00
|
|
|
|
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e) => updateLastInteractionTime();
|
2021-05-11 12:40:29 +08:00
|
|
|
|
|
2021-12-26 15:26:33 +08:00
|
|
|
|
[Resolved]
|
2023-06-23 00:37:25 +08:00
|
|
|
|
private GameHost host { get; set; } = null!;
|
2021-12-26 15:26:33 +08:00
|
|
|
|
|
2018-11-20 01:48:59 +08:00
|
|
|
|
protected override bool Handle(UIEvent e)
|
|
|
|
|
{
|
2021-12-27 12:31:07 +08:00
|
|
|
|
// 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.
|
2021-12-26 15:26:33 +08:00
|
|
|
|
if (!host.IsActive.Value)
|
|
|
|
|
return base.Handle(e);
|
|
|
|
|
|
2018-11-20 01:48:59 +08:00
|
|
|
|
switch (e)
|
|
|
|
|
{
|
2022-06-24 20:25:23 +08:00
|
|
|
|
case KeyDownEvent:
|
|
|
|
|
case KeyUpEvent:
|
|
|
|
|
case MouseDownEvent:
|
|
|
|
|
case MouseUpEvent:
|
|
|
|
|
case MouseMoveEvent:
|
2018-11-20 01:48:59 +08:00
|
|
|
|
return updateLastInteractionTime();
|
2019-04-01 11:44:46 +08:00
|
|
|
|
|
2018-11-20 01:48:59 +08:00
|
|
|
|
default:
|
|
|
|
|
return base.Handle(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-26 15:33:58 +08:00
|
|
|
|
|
|
|
|
|
private bool updateLastInteractionTime()
|
|
|
|
|
{
|
|
|
|
|
lastInteractionTime = Clock.CurrentTime;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-11-20 01:48:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|