Use bindable flow for checking idle time

This commit is contained in:
Dean Herbert 2018-11-26 16:32:59 +09:00
parent fe5b043a59
commit 8d65d49126
3 changed files with 42 additions and 8 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>. // Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Configuration;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Input; using osu.Framework.Input;
using osu.Framework.Input.Bindings; using osu.Framework.Input.Bindings;
@ -8,10 +9,30 @@
namespace osu.Game.Input 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 public class IdleTracker : Component, IKeyBindingHandler<PlatformAction>, IHandleGlobalInput
{ {
private readonly double timeToIdle;
private double lastInteractionTime; 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() private bool updateLastInteractionTime()
{ {
@ -19,6 +40,12 @@ private bool updateLastInteractionTime()
return false; return false;
} }
protected override void Update()
{
base.Update();
IsIdle.Value = TimeSpentIdle > timeToIdle;
}
public bool OnPressed(PlatformAction action) => updateLastInteractionTime(); public bool OnPressed(PlatformAction action) => updateLastInteractionTime();
public bool OnReleased(PlatformAction action) => updateLastInteractionTime(); public bool OnReleased(PlatformAction action) => updateLastInteractionTime();

View File

@ -319,7 +319,7 @@ protected override void LoadComplete()
}, },
mainContent = new Container { RelativeSizeAxes = Axes.Both }, mainContent = new Container { RelativeSizeAxes = Axes.Both },
overlayContent = new Container { RelativeSizeAxes = Axes.Both, Depth = float.MinValue }, overlayContent = new Container { RelativeSizeAxes = Axes.Both, Depth = float.MinValue },
idleTracker = new IdleTracker { RelativeSizeAxes = Axes.Both } idleTracker = new IdleTracker(6000) { RelativeSizeAxes = Axes.Both }
}); });
loadComponentSingleFile(screenStack = new Loader(), d => loadComponentSingleFile(screenStack = new Loader(), d =>

View File

@ -8,6 +8,7 @@
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio; using osu.Framework.Audio;
using osu.Framework.Audio.Sample; using osu.Framework.Audio.Sample;
using osu.Framework.Configuration;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Bindings; using osu.Framework.Input.Bindings;
@ -27,6 +28,8 @@ public class ButtonSystem : Container, IStateful<ButtonSystemState>, IKeyBinding
{ {
public event Action<ButtonSystemState> StateChanged; public event Action<ButtonSystemState> StateChanged;
private readonly BindableBool isIdle = new BindableBool();
public Action OnEdit; public Action OnEdit;
public Action OnExit; public Action OnExit;
public Action OnDirect; public Action OnDirect;
@ -65,8 +68,6 @@ public void SetOsuLogo(OsuLogo logo)
private SampleChannel sampleBack; private SampleChannel sampleBack;
private IdleTracker idleTracker;
public ButtonSystem() public ButtonSystem()
{ {
RelativeSizeAxes = Axes.Both; RelativeSizeAxes = Axes.Both;
@ -108,10 +109,19 @@ public ButtonSystem()
private void load(AudioManager audio, OsuGame game, IdleTracker idleTracker) private void load(AudioManager audio, OsuGame game, IdleTracker idleTracker)
{ {
this.game = game; this.game = game;
this.idleTracker = idleTracker;
isIdle.ValueChanged += updateIdleState;
isIdle.BindTo(idleTracker.IsIdle);
sampleBack = audio.Sample.Get(@"Menu/button-back-select"); sampleBack = audio.Sample.Get(@"Menu/button-back-select");
} }
private void updateIdleState(bool isIdle)
{
if (isIdle && State != ButtonSystemState.Exit)
State = ButtonSystemState.Initial;
}
public bool OnPressed(GlobalAction action) public bool OnPressed(GlobalAction action)
{ {
switch (action) switch (action)
@ -270,9 +280,6 @@ private void updateLogoState(ButtonSystemState lastState = ButtonSystemState.Ini
protected override void Update() protected override void Update()
{ {
if (idleTracker?.IdleTime > 6000 && State != ButtonSystemState.Exit)
State = ButtonSystemState.Initial;
base.Update(); base.Update();
if (logo != null) if (logo != null)