mirror of
https://github.com/ppy/osu.git
synced 2025-02-15 14:42:56 +08:00
Use bindable flow for checking idle time
This commit is contained in:
parent
fe5b043a59
commit
8d65d49126
@ -1,6 +1,7 @@
|
||||
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using osu.Framework.Configuration;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Input;
|
||||
using osu.Framework.Input.Bindings;
|
||||
@ -8,10 +9,30 @@ 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>, IHandleGlobalInput
|
||||
{
|
||||
private readonly double timeToIdle;
|
||||
|
||||
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()
|
||||
{
|
||||
@ -19,6 +40,12 @@ namespace osu.Game.Input
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
IsIdle.Value = TimeSpentIdle > timeToIdle;
|
||||
}
|
||||
|
||||
public bool OnPressed(PlatformAction action) => updateLastInteractionTime();
|
||||
|
||||
public bool OnReleased(PlatformAction action) => updateLastInteractionTime();
|
||||
|
@ -319,7 +319,7 @@ namespace osu.Game
|
||||
},
|
||||
mainContent = new Container { RelativeSizeAxes = Axes.Both },
|
||||
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 =>
|
||||
|
@ -8,6 +8,7 @@ using osu.Framework;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Audio;
|
||||
using osu.Framework.Audio.Sample;
|
||||
using osu.Framework.Configuration;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Input.Bindings;
|
||||
@ -27,6 +28,8 @@ namespace osu.Game.Screens.Menu
|
||||
{
|
||||
public event Action<ButtonSystemState> StateChanged;
|
||||
|
||||
private readonly BindableBool isIdle = new BindableBool();
|
||||
|
||||
public Action OnEdit;
|
||||
public Action OnExit;
|
||||
public Action OnDirect;
|
||||
@ -65,8 +68,6 @@ namespace osu.Game.Screens.Menu
|
||||
|
||||
private SampleChannel sampleBack;
|
||||
|
||||
private IdleTracker idleTracker;
|
||||
|
||||
public ButtonSystem()
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both;
|
||||
@ -108,10 +109,19 @@ namespace osu.Game.Screens.Menu
|
||||
private void load(AudioManager audio, OsuGame game, IdleTracker idleTracker)
|
||||
{
|
||||
this.game = game;
|
||||
this.idleTracker = idleTracker;
|
||||
|
||||
isIdle.ValueChanged += updateIdleState;
|
||||
isIdle.BindTo(idleTracker.IsIdle);
|
||||
|
||||
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)
|
||||
{
|
||||
switch (action)
|
||||
@ -270,9 +280,6 @@ namespace osu.Game.Screens.Menu
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
if (idleTracker?.IdleTime > 6000 && State != ButtonSystemState.Exit)
|
||||
State = ButtonSystemState.Initial;
|
||||
|
||||
base.Update();
|
||||
|
||||
if (logo != null)
|
||||
|
Loading…
Reference in New Issue
Block a user