Move dangerous hold specification to base class

This commit is contained in:
Dean Herbert
2022-05-06 14:34:31 +09:00
parent 0d8e42b941
commit be960eb092
3 changed files with 43 additions and 38 deletions

View File

@ -12,6 +12,13 @@ namespace osu.Game.Graphics.Containers
{ {
public abstract class HoldToConfirmContainer : Container public abstract class HoldToConfirmContainer : Container
{ {
public const double DANGEROUS_HOLD_ACTIVATION_DELAY = 500;
/// <summary>
/// Whether the associated action is considered dangerous, warranting a longer hold.
/// </summary>
public bool IsDangerousAction { get; }
public Action Action; public Action Action;
private const int fadeout_delay = 200; private const int fadeout_delay = 200;
@ -29,12 +36,9 @@ namespace osu.Game.Graphics.Containers
protected virtual bool AllowMultipleFires => false; protected virtual bool AllowMultipleFires => false;
/// <summary> /// <summary>
/// Specify a custom activation delay, overriding the game-wide user setting. /// The current activation delay for this control.
/// </summary> /// </summary>
/// <remarks> protected IBindable<double> HoldActivationDelay => holdActivationDelay;
/// This should be used in special cases where we want to be extra sure the user knows what they are doing. An example is when changes would be lost.
/// </remarks>
protected virtual double? HoldActivationDelay => null;
public Bindable<double> Progress = new BindableDouble(); public Bindable<double> Progress = new BindableDouble();
@ -43,14 +47,26 @@ namespace osu.Game.Graphics.Containers
[Resolved] [Resolved]
private OsuConfigManager config { get; set; } private OsuConfigManager config { get; set; }
protected HoldToConfirmContainer(bool isDangerousAction = false)
{
IsDangerousAction = isDangerousAction;
}
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
if (IsDangerousAction)
{
holdActivationDelay.Value = DANGEROUS_HOLD_ACTIVATION_DELAY;
}
else
{
holdActivationDelay = HoldActivationDelay != null holdActivationDelay = HoldActivationDelay != null
? new Bindable<double>(HoldActivationDelay.Value) ? new Bindable<double>(HoldActivationDelay.Value)
: config.GetBindable<double>(OsuSetting.UIHoldActivationDelay); : config.GetBindable<double>(OsuSetting.UIHoldActivationDelay);
} }
}
protected void BeginConfirm() protected void BeginConfirm()
{ {

View File

@ -12,8 +12,6 @@ namespace osu.Game.Overlays.Dialog
{ {
public class PopupDialogDangerousButton : PopupDialogButton public class PopupDialogDangerousButton : PopupDialogButton
{ {
public const double DANGEROUS_HOLD_ACTIVATION_DELAY = 500;
private Box progressBox; private Box progressBox;
private DangerousConfirmContainer confirmContainer; private DangerousConfirmContainer confirmContainer;
@ -44,7 +42,10 @@ namespace osu.Game.Overlays.Dialog
private class DangerousConfirmContainer : HoldToConfirmContainer private class DangerousConfirmContainer : HoldToConfirmContainer
{ {
protected override double? HoldActivationDelay => DANGEROUS_HOLD_ACTIVATION_DELAY; public DangerousConfirmContainer()
: base(isDangerousAction: true)
{
}
protected override bool OnMouseDown(MouseDownEvent e) protected override bool OnMouseDown(MouseDownEvent e)
{ {

View File

@ -13,12 +13,10 @@ using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Bindings; using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Configuration;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Input.Bindings; using osu.Game.Input.Bindings;
using osu.Game.Overlays.Dialog;
using osuTK; using osuTK;
namespace osu.Game.Screens.Play.HUD namespace osu.Game.Screens.Play.HUD
@ -29,20 +27,11 @@ namespace osu.Game.Screens.Play.HUD
public readonly Bindable<bool> IsPaused = new Bindable<bool>(); public readonly Bindable<bool> IsPaused = new Bindable<bool>();
private readonly Button button; private Button button;
public Action Action public Action Action { get; set; }
{
set => button.Action = value;
}
private readonly OsuSpriteText text; private OsuSpriteText text;
[Resolved]
private OsuConfigManager config { get; set; }
[Resolved(canBeNull: true)]
private Player player { get; set; }
private readonly Bindable<double> activationDelay = new Bindable<double>(); private readonly Bindable<double> activationDelay = new Bindable<double>();
@ -51,6 +40,11 @@ namespace osu.Game.Screens.Play.HUD
Direction = FillDirection.Horizontal; Direction = FillDirection.Horizontal;
Spacing = new Vector2(20, 0); Spacing = new Vector2(20, 0);
Margin = new MarginPadding(10); Margin = new MarginPadding(10);
}
[BackgroundDependencyLoader(true)]
private void load(Player player)
{
Children = new Drawable[] Children = new Drawable[]
{ {
text = new OsuSpriteText text = new OsuSpriteText
@ -59,11 +53,12 @@ namespace osu.Game.Screens.Play.HUD
Anchor = Anchor.CentreLeft, Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft Origin = Anchor.CentreLeft
}, },
button = new Button button = new Button(player?.Configuration.AllowRestart == false)
{ {
HoverGained = () => text.FadeIn(500, Easing.OutQuint), HoverGained = () => text.FadeIn(500, Easing.OutQuint),
HoverLost = () => text.FadeOut(500, Easing.OutQuint), HoverLost = () => text.FadeOut(500, Easing.OutQuint),
IsPaused = { BindTarget = IsPaused } IsPaused = { BindTarget = IsPaused },
Action = () => Action(),
} }
}; };
AutoSizeAxes = Axes.Both; AutoSizeAxes = Axes.Both;
@ -71,13 +66,6 @@ namespace osu.Game.Screens.Play.HUD
protected override void LoadComplete() protected override void LoadComplete()
{ {
if (player?.Configuration.AllowRestart == false)
{
activationDelay.Value = PopupDialogDangerousButton.DANGEROUS_HOLD_ACTIVATION_DELAY;
}
else
config.BindWith(OsuSetting.UIHoldActivationDelay, activationDelay);
activationDelay.BindValueChanged(v => activationDelay.BindValueChanged(v =>
{ {
text.Text = v.NewValue > 0 text.Text = v.NewValue > 0
@ -125,10 +113,10 @@ namespace osu.Game.Screens.Play.HUD
public Action HoverGained; public Action HoverGained;
public Action HoverLost; public Action HoverLost;
[Resolved(canBeNull: true)] public Button(bool isDangerousAction)
private Player player { get; set; } : base(isDangerousAction)
{
protected override double? HoldActivationDelay => player?.Configuration.AllowRestart == false ? PopupDialogDangerousButton.DANGEROUS_HOLD_ACTIVATION_DELAY : (double?)null; }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colours) private void load(OsuColour colours)