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
|
|
|
|
|
|
|
|
|
using System;
|
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;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
|
{
|
2018-05-22 15:04:36 +08:00
|
|
|
|
public abstract class HoldToConfirmContainer : Container
|
2018-05-22 00:44:06 +08:00
|
|
|
|
{
|
|
|
|
|
public Action Action;
|
|
|
|
|
|
2019-08-16 12:21:41 +08:00
|
|
|
|
private const int default_activation_delay = 200;
|
2018-05-22 00:44:06 +08:00
|
|
|
|
private const int fadeout_delay = 200;
|
|
|
|
|
|
2019-08-16 12:21:41 +08:00
|
|
|
|
private readonly double activationDelay;
|
|
|
|
|
|
2018-05-22 00:44:06 +08:00
|
|
|
|
private bool fired;
|
|
|
|
|
private bool confirming;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the overlay should be allowed to return from a fired state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool AllowMultipleFires => false;
|
|
|
|
|
|
|
|
|
|
public Bindable<double> Progress = new BindableDouble();
|
|
|
|
|
|
2019-08-16 12:21:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a new instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="activationDelay">The time requried before an action is confirmed.</param>
|
|
|
|
|
protected HoldToConfirmContainer(double activationDelay = default_activation_delay)
|
|
|
|
|
{
|
|
|
|
|
this.activationDelay = activationDelay;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 00:44:06 +08:00
|
|
|
|
protected void BeginConfirm()
|
|
|
|
|
{
|
2019-06-11 16:28:16 +08:00
|
|
|
|
if (confirming || (!AllowMultipleFires && fired)) return;
|
2018-05-22 00:44:06 +08:00
|
|
|
|
|
|
|
|
|
confirming = true;
|
|
|
|
|
|
2019-08-16 12:21:41 +08:00
|
|
|
|
this.TransformBindableTo(Progress, 1, activationDelay * (1 - Progress.Value), Easing.Out).OnComplete(_ => Confirm());
|
2018-05-22 00:44:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Confirm()
|
|
|
|
|
{
|
|
|
|
|
Action?.Invoke();
|
|
|
|
|
fired = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void AbortConfirm()
|
|
|
|
|
{
|
|
|
|
|
if (!AllowMultipleFires && fired) return;
|
|
|
|
|
|
|
|
|
|
confirming = false;
|
|
|
|
|
|
|
|
|
|
this.TransformBindableTo(Progress, 0, fadeout_delay, Easing.Out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|