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-05-22 00:44:06 +08:00
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2018-05-22 00:44:06 +08:00
|
|
|
|
using System;
|
2019-09-19 13:04:51 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-05-22 00:44:06 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-09-19 13:04:51 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2018-05-22 00:44:06 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
|
{
|
2022-05-06 13:43:24 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A container which adds a common "hold-to-perform" pattern to a container.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This container does not handle triggering the hold/abort operations.
|
|
|
|
|
/// To use this class, please call <see cref="BeginConfirm"/> and <see cref="AbortConfirm"/> when necessary.
|
|
|
|
|
///
|
|
|
|
|
/// The <see cref="Progress"/> is exposed as a transforming bindable which smoothly tracks the progress of a hold operation.
|
|
|
|
|
/// It can be used for animating and displaying progress directly.
|
|
|
|
|
/// </remarks>
|
2018-05-22 15:04:36 +08:00
|
|
|
|
public abstract class HoldToConfirmContainer : Container
|
2018-05-22 00:44:06 +08:00
|
|
|
|
{
|
2022-05-06 13:34:31 +08:00
|
|
|
|
public const double DANGEROUS_HOLD_ACTIVATION_DELAY = 500;
|
|
|
|
|
|
2022-05-06 13:37:50 +08:00
|
|
|
|
private const int fadeout_delay = 200;
|
|
|
|
|
|
2022-05-06 13:34:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the associated action is considered dangerous, warranting a longer hold.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsDangerousAction { get; }
|
|
|
|
|
|
2022-05-06 13:37:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The action to perform when a hold successfully completes.
|
|
|
|
|
/// </summary>
|
2018-05-22 00:44:06 +08:00
|
|
|
|
public Action Action;
|
|
|
|
|
|
2019-09-19 19:17:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether currently in a fired state (and the confirm <see cref="Action"/> has been sent).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool Fired { get; private set; }
|
|
|
|
|
|
2018-05-22 00:44:06 +08:00
|
|
|
|
private bool confirming;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-05-06 13:37:50 +08:00
|
|
|
|
/// The current activation delay for this control.
|
2018-05-22 00:44:06 +08:00
|
|
|
|
/// </summary>
|
2022-05-06 13:37:50 +08:00
|
|
|
|
public IBindable<double> HoldActivationDelay => holdActivationDelay;
|
2018-05-22 00:44:06 +08:00
|
|
|
|
|
2022-03-21 15:07:26 +08:00
|
|
|
|
/// <summary>
|
2022-05-06 13:37:50 +08:00
|
|
|
|
/// The progress of any ongoing hold operation. 0 means no hold has started; 1 means a hold has been completed.
|
2022-03-21 15:07:26 +08:00
|
|
|
|
/// </summary>
|
2022-05-06 13:37:50 +08:00
|
|
|
|
public IBindable<double> Progress => progress;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the overlay should be allowed to return from a fired state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool AllowMultipleFires => false;
|
2022-03-21 15:07:26 +08:00
|
|
|
|
|
2022-05-06 13:37:50 +08:00
|
|
|
|
private readonly Bindable<double> progress = new BindableDouble();
|
2018-05-22 00:44:06 +08:00
|
|
|
|
|
2022-05-06 13:37:50 +08:00
|
|
|
|
private readonly Bindable<double> holdActivationDelay = new Bindable<double>();
|
2019-09-19 13:04:51 +08:00
|
|
|
|
|
2022-05-03 15:06:04 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private OsuConfigManager config { get; set; }
|
|
|
|
|
|
2022-05-06 13:34:31 +08:00
|
|
|
|
protected HoldToConfirmContainer(bool isDangerousAction = false)
|
|
|
|
|
{
|
|
|
|
|
IsDangerousAction = isDangerousAction;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 15:06:04 +08:00
|
|
|
|
protected override void LoadComplete()
|
2019-08-16 12:21:41 +08:00
|
|
|
|
{
|
2022-05-03 15:06:04 +08:00
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
2022-05-06 13:34:31 +08:00
|
|
|
|
if (IsDangerousAction)
|
|
|
|
|
holdActivationDelay.Value = DANGEROUS_HOLD_ACTIVATION_DELAY;
|
|
|
|
|
else
|
2022-05-06 13:37:50 +08:00
|
|
|
|
config.BindWith(OsuSetting.UIHoldActivationDelay, holdActivationDelay);
|
2019-08-16 12:21:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 13:43:24 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Begin a new confirmation. Should be called when the container is interacted with (ie. the user presses a key).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Calling this method when already in the process of confirming has no effect.
|
|
|
|
|
/// </remarks>
|
2018-05-22 00:44:06 +08:00
|
|
|
|
protected void BeginConfirm()
|
|
|
|
|
{
|
2019-09-19 19:17:58 +08:00
|
|
|
|
if (confirming || (!AllowMultipleFires && Fired)) return;
|
2018-05-22 00:44:06 +08:00
|
|
|
|
|
|
|
|
|
confirming = true;
|
|
|
|
|
|
2022-05-06 13:37:50 +08:00
|
|
|
|
this.TransformBindableTo(progress, 1, holdActivationDelay.Value * (1 - progress.Value), Easing.Out).OnComplete(_ => Confirm());
|
2018-05-22 00:44:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 13:43:24 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Abort any ongoing confirmation. Should be called when the container's interaction is no longer valid (ie. the user releases a key).
|
|
|
|
|
/// </summary>
|
2018-05-22 00:44:06 +08:00
|
|
|
|
protected void AbortConfirm()
|
|
|
|
|
{
|
2019-09-19 19:17:58 +08:00
|
|
|
|
if (!AllowMultipleFires && Fired) return;
|
2018-05-22 00:44:06 +08:00
|
|
|
|
|
|
|
|
|
confirming = false;
|
2019-09-19 19:17:58 +08:00
|
|
|
|
Fired = false;
|
2018-05-22 00:44:06 +08:00
|
|
|
|
|
2022-04-14 11:33:59 +08:00
|
|
|
|
this
|
2022-05-06 13:37:50 +08:00
|
|
|
|
.TransformBindableTo(progress, progress.Value)
|
2022-04-14 11:33:59 +08:00
|
|
|
|
.Delay(200)
|
2022-05-06 13:37:50 +08:00
|
|
|
|
.TransformBindableTo(progress, 0, fadeout_delay, Easing.InSine);
|
2018-05-22 00:44:06 +08:00
|
|
|
|
}
|
2022-05-06 13:43:24 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A method which is invoked when the confirmation sequence completes successfully.
|
|
|
|
|
/// By default, will fire the associated <see cref="Action"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void Confirm()
|
|
|
|
|
{
|
|
|
|
|
Action?.Invoke();
|
|
|
|
|
Fired = true;
|
|
|
|
|
}
|
2018-05-22 00:44:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|