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-04-21 23:24:31 +08:00
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2018-04-21 23:24:31 +08:00
|
|
|
|
using System;
|
2018-09-19 19:52:57 +08:00
|
|
|
|
using System.Linq;
|
2018-04-21 23:24:31 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-05-04 15:50:36 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-21 23:24:31 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2019-03-27 18:29:27 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2018-04-21 23:24:31 +08:00
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2018-11-07 00:47:02 +08:00
|
|
|
|
using osu.Framework.Input.Bindings;
|
2018-10-02 11:02:47 +08:00
|
|
|
|
using osu.Framework.Input.Events;
|
2022-05-06 14:01:24 +08:00
|
|
|
|
using osu.Framework.Threading;
|
2020-01-09 12:43:44 +08:00
|
|
|
|
using osu.Framework.Utils;
|
2024-01-15 16:48:37 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2018-04-21 23:24:31 +08:00
|
|
|
|
using osu.Game.Graphics;
|
2018-05-22 00:44:06 +08:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2018-04-21 23:24:31 +08:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2018-11-07 00:47:02 +08:00
|
|
|
|
using osu.Game.Input.Bindings;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK;
|
2022-05-06 14:01:24 +08:00
|
|
|
|
using osuTK.Graphics;
|
2018-04-21 23:24:31 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play.HUD
|
|
|
|
|
{
|
2018-11-07 00:47:02 +08:00
|
|
|
|
public partial class HoldForMenuButton : FillFlowContainer
|
2018-04-21 23:24:31 +08:00
|
|
|
|
{
|
2018-09-26 13:01:15 +08:00
|
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
|
2018-05-21 23:02:03 +08:00
|
|
|
|
|
2024-08-19 19:28:02 +08:00
|
|
|
|
public override bool PropagatePositionalInputSubTree => alwaysShow.Value || touchActive.Value;
|
|
|
|
|
|
2019-05-10 14:51:12 +08:00
|
|
|
|
public readonly Bindable<bool> IsPaused = new Bindable<bool>();
|
|
|
|
|
|
2023-01-02 10:00:39 +08:00
|
|
|
|
public readonly Bindable<bool> ReplayLoaded = new Bindable<bool>();
|
|
|
|
|
|
2022-05-06 13:44:10 +08:00
|
|
|
|
private HoldButton button;
|
2018-05-11 02:08:02 +08:00
|
|
|
|
|
2022-05-06 13:34:31 +08:00
|
|
|
|
public Action Action { get; set; }
|
2022-05-03 15:06:04 +08:00
|
|
|
|
|
2022-05-06 13:34:31 +08:00
|
|
|
|
private OsuSpriteText text;
|
2022-05-03 15:06:04 +08:00
|
|
|
|
|
2024-08-19 19:23:25 +08:00
|
|
|
|
private Bindable<bool> alwaysShow;
|
|
|
|
|
|
2018-11-07 00:47:02 +08:00
|
|
|
|
public HoldForMenuButton()
|
2018-04-21 23:24:31 +08:00
|
|
|
|
{
|
2018-04-22 01:21:09 +08:00
|
|
|
|
Direction = FillDirection.Horizontal;
|
|
|
|
|
Spacing = new Vector2(20, 0);
|
2018-05-21 23:02:03 +08:00
|
|
|
|
Margin = new MarginPadding(10);
|
2024-01-15 16:48:37 +08:00
|
|
|
|
|
|
|
|
|
AlwaysPresent = true;
|
2022-05-06 13:34:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader(true)]
|
2024-08-19 19:23:25 +08:00
|
|
|
|
private void load(Player player, OsuConfigManager config)
|
2022-05-06 13:34:31 +08:00
|
|
|
|
{
|
2018-04-21 23:24:31 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
text = new OsuSpriteText
|
|
|
|
|
{
|
2019-02-12 12:04:46 +08:00
|
|
|
|
Font = OsuFont.GetFont(weight: FontWeight.Bold),
|
2018-04-21 23:24:31 +08:00
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
|
Origin = Anchor.CentreLeft
|
|
|
|
|
},
|
2022-05-06 13:44:10 +08:00
|
|
|
|
button = new HoldButton(player?.Configuration.AllowRestart == false)
|
2018-05-22 15:26:11 +08:00
|
|
|
|
{
|
|
|
|
|
HoverGained = () => text.FadeIn(500, Easing.OutQuint),
|
2019-05-10 14:51:12 +08:00
|
|
|
|
HoverLost = () => text.FadeOut(500, Easing.OutQuint),
|
2022-05-06 13:34:31 +08:00
|
|
|
|
IsPaused = { BindTarget = IsPaused },
|
2023-01-02 10:00:39 +08:00
|
|
|
|
ReplayLoaded = { BindTarget = ReplayLoaded },
|
2022-05-06 13:34:31 +08:00
|
|
|
|
Action = () => Action(),
|
2018-05-22 15:26:11 +08:00
|
|
|
|
}
|
2018-04-21 23:24:31 +08:00
|
|
|
|
};
|
2024-01-15 16:48:37 +08:00
|
|
|
|
|
2018-04-21 23:24:31 +08:00
|
|
|
|
AutoSizeAxes = Axes.Both;
|
2024-08-19 19:23:25 +08:00
|
|
|
|
|
|
|
|
|
alwaysShow = config.GetBindable<bool>(OsuSetting.AlwaysShowHoldForMenuButton);
|
2018-04-21 23:24:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 16:48:37 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private SessionStatics sessionStatics { get; set; }
|
|
|
|
|
|
|
|
|
|
private Bindable<bool> touchActive;
|
|
|
|
|
|
2019-09-27 13:15:24 +08:00
|
|
|
|
protected override void LoadComplete()
|
2019-09-26 06:42:56 +08:00
|
|
|
|
{
|
2022-05-06 16:04:36 +08:00
|
|
|
|
button.HoldActivationDelay.BindValueChanged(v =>
|
2019-09-27 07:59:42 +08:00
|
|
|
|
{
|
|
|
|
|
text.Text = v.NewValue > 0
|
|
|
|
|
? "hold for menu"
|
|
|
|
|
: "press for menu";
|
|
|
|
|
}, true);
|
2019-09-26 06:42:56 +08:00
|
|
|
|
|
2024-01-15 16:48:37 +08:00
|
|
|
|
touchActive = sessionStatics.GetBindable<bool>(Static.TouchInputActive);
|
|
|
|
|
|
|
|
|
|
if (touchActive.Value)
|
|
|
|
|
{
|
|
|
|
|
Alpha = 1f;
|
|
|
|
|
text.FadeInFromZero(500, Easing.OutQuint)
|
|
|
|
|
.Delay(1500)
|
|
|
|
|
.FadeOut(500, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Alpha = 0;
|
|
|
|
|
text.Alpha = 0f;
|
|
|
|
|
}
|
2019-09-27 13:15:24 +08:00
|
|
|
|
|
2018-05-21 23:02:03 +08:00
|
|
|
|
base.LoadComplete();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 13:48:34 +08:00
|
|
|
|
private float positionalAdjust = 1; // Start at 1 to handle the case where a user never send positional input.
|
2018-05-21 23:02:03 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnMouseMove(MouseMoveEvent e)
|
2018-05-21 23:02:03 +08:00
|
|
|
|
{
|
2024-02-02 18:48:13 +08:00
|
|
|
|
positionalAdjust = Vector2.Distance(e.MousePosition, button.ToSpaceOfOtherDrawable(button.DrawRectangle.Centre, Parent!)) / 100;
|
2018-10-02 11:02:47 +08:00
|
|
|
|
return base.OnMouseMove(e);
|
2018-05-21 23:02:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-21 23:49:33 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2024-08-19 19:28:02 +08:00
|
|
|
|
// While the button is hovered or still animating, keep fully visible.
|
2018-05-22 15:26:11 +08:00
|
|
|
|
if (text.Alpha > 0 || button.Progress.Value > 0 || button.IsHovered)
|
|
|
|
|
Alpha = 1;
|
2024-08-19 19:28:02 +08:00
|
|
|
|
// When touch input is detected, keep visible at a constant opacity.
|
2024-08-19 19:23:25 +08:00
|
|
|
|
else if (touchActive.Value)
|
2024-08-19 19:28:02 +08:00
|
|
|
|
Alpha = 0.5f;
|
|
|
|
|
// Otherwise, if the user chooses, show it when the mouse is nearby.
|
2024-08-19 19:23:25 +08:00
|
|
|
|
else if (alwaysShow.Value)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2024-01-15 16:48:37 +08:00
|
|
|
|
float minAlpha = touchActive.Value ? .08f : 0;
|
|
|
|
|
|
2018-05-22 15:26:11 +08:00
|
|
|
|
Alpha = Interpolation.ValueAt(
|
2019-11-20 20:19:49 +08:00
|
|
|
|
Math.Clamp(Clock.ElapsedFrameTime, 0, 200),
|
2024-01-15 16:48:37 +08:00
|
|
|
|
Alpha, Math.Clamp(1 - positionalAdjust, minAlpha, 1), 0, 200, Easing.OutQuint);
|
2019-11-11 19:53:22 +08:00
|
|
|
|
}
|
2024-08-19 19:23:25 +08:00
|
|
|
|
else
|
|
|
|
|
Alpha = 0;
|
2018-05-21 23:49:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 13:44:10 +08:00
|
|
|
|
private partial class HoldButton : HoldToConfirmContainer, IKeyBindingHandler<GlobalAction>
|
2018-04-21 23:24:31 +08:00
|
|
|
|
{
|
|
|
|
|
private SpriteIcon icon;
|
2018-05-22 01:08:44 +08:00
|
|
|
|
private CircularProgress circularProgress;
|
2018-05-22 14:59:53 +08:00
|
|
|
|
private Circle overlayCircle;
|
2018-05-21 23:49:33 +08:00
|
|
|
|
|
2019-05-10 14:51:12 +08:00
|
|
|
|
public readonly Bindable<bool> IsPaused = new Bindable<bool>();
|
|
|
|
|
|
2023-01-02 10:00:39 +08:00
|
|
|
|
public readonly Bindable<bool> ReplayLoaded = new Bindable<bool>();
|
|
|
|
|
|
2018-05-22 01:08:44 +08:00
|
|
|
|
protected override bool AllowMultipleFires => true;
|
|
|
|
|
|
2018-05-22 15:26:11 +08:00
|
|
|
|
public Action HoverGained;
|
|
|
|
|
public Action HoverLost;
|
|
|
|
|
|
2022-05-06 14:01:24 +08:00
|
|
|
|
private const double shake_duration = 20;
|
|
|
|
|
|
|
|
|
|
private bool pendingAnimation;
|
|
|
|
|
private ScheduledDelegate shakeOperation;
|
|
|
|
|
|
2022-05-06 13:44:10 +08:00
|
|
|
|
public HoldButton(bool isDangerousAction)
|
2022-05-06 13:34:31 +08:00
|
|
|
|
: base(isDangerousAction)
|
|
|
|
|
{
|
|
|
|
|
}
|
2022-05-03 15:06:04 +08:00
|
|
|
|
|
2018-04-21 23:24:31 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2022-01-15 08:06:39 +08:00
|
|
|
|
private void load(OsuColour colours)
|
2018-04-21 23:24:31 +08:00
|
|
|
|
{
|
|
|
|
|
Size = new Vector2(60);
|
2018-05-22 00:44:06 +08:00
|
|
|
|
|
|
|
|
|
Child = new CircularContainer
|
2018-04-21 23:24:31 +08:00
|
|
|
|
{
|
2018-05-22 00:44:06 +08:00
|
|
|
|
Masking = true,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Children = new Drawable[]
|
2018-04-21 23:24:31 +08:00
|
|
|
|
{
|
2018-05-22 00:44:06 +08:00
|
|
|
|
new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = colours.Gray1,
|
|
|
|
|
Alpha = 0.5f,
|
|
|
|
|
},
|
2018-05-22 01:08:44 +08:00
|
|
|
|
circularProgress = new CircularProgress
|
2018-05-22 00:44:06 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
InnerRadius = 1
|
|
|
|
|
},
|
2018-05-22 14:59:53 +08:00
|
|
|
|
overlayCircle = new Circle
|
2018-05-22 00:44:06 +08:00
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = colours.Gray1,
|
|
|
|
|
Size = new Vector2(0.9f),
|
|
|
|
|
},
|
|
|
|
|
icon = new SpriteIcon
|
|
|
|
|
{
|
|
|
|
|
Shadow = false,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Size = new Vector2(15),
|
2019-04-22 17:47:28 +08:00
|
|
|
|
Icon = FontAwesome.Solid.Times
|
2018-05-22 00:44:06 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-05-22 01:08:44 +08:00
|
|
|
|
bind();
|
2019-05-07 12:34:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-06 10:19:40 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
circularProgress.Progress = Progress.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 01:08:44 +08:00
|
|
|
|
private void bind()
|
|
|
|
|
{
|
2022-05-06 14:01:24 +08:00
|
|
|
|
Progress.ValueChanged += progress =>
|
|
|
|
|
{
|
|
|
|
|
icon.Scale = new Vector2(1 + (float)progress.NewValue * 0.2f);
|
|
|
|
|
|
|
|
|
|
if (IsDangerousAction)
|
|
|
|
|
{
|
|
|
|
|
Colour = Interpolation.ValueAt(progress.NewValue, Color4.White, Color4.Red, 0, 1, Easing.OutQuint);
|
|
|
|
|
|
|
|
|
|
if (progress.NewValue > 0 && progress.NewValue < 1)
|
|
|
|
|
{
|
|
|
|
|
shakeOperation ??= Scheduler.AddDelayed(shake, shake_duration, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Child.MoveTo(Vector2.Zero, shake_duration * 2, Easing.OutQuint);
|
|
|
|
|
shakeOperation?.Cancel();
|
|
|
|
|
shakeOperation = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-04-21 23:24:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 14:01:24 +08:00
|
|
|
|
private void shake()
|
|
|
|
|
{
|
|
|
|
|
const float shake_magnitude = 8;
|
|
|
|
|
|
|
|
|
|
Child.MoveTo(new Vector2(
|
|
|
|
|
RNG.NextSingle(-1, 1) * (float)Progress.Value * shake_magnitude,
|
|
|
|
|
RNG.NextSingle(-1, 1) * (float)Progress.Value * shake_magnitude
|
|
|
|
|
), shake_duration);
|
|
|
|
|
}
|
2018-05-22 01:08:44 +08:00
|
|
|
|
|
2018-05-22 00:44:06 +08:00
|
|
|
|
protected override void Confirm()
|
2018-04-21 23:24:31 +08:00
|
|
|
|
{
|
2018-05-22 00:44:06 +08:00
|
|
|
|
base.Confirm();
|
2018-05-22 01:08:44 +08:00
|
|
|
|
|
2018-05-22 15:04:07 +08:00
|
|
|
|
// temporarily unbind as to not look weird if releasing during confirm animation (can see the unwind of progress).
|
2018-05-22 01:08:44 +08:00
|
|
|
|
Progress.UnbindAll();
|
2018-05-22 14:58:00 +08:00
|
|
|
|
|
|
|
|
|
// avoid starting a new confirm call until we finish animating.
|
2018-05-22 01:08:44 +08:00
|
|
|
|
pendingAnimation = true;
|
|
|
|
|
|
2019-03-12 01:21:34 +08:00
|
|
|
|
AbortConfirm();
|
2018-05-22 17:06:40 +08:00
|
|
|
|
|
2018-05-22 14:59:53 +08:00
|
|
|
|
overlayCircle.ScaleTo(0, 100)
|
2018-05-22 15:23:05 +08:00
|
|
|
|
.Then().FadeOut().ScaleTo(1).FadeIn(500)
|
2022-06-24 20:25:23 +08:00
|
|
|
|
.OnComplete(_ =>
|
2018-05-22 15:23:05 +08:00
|
|
|
|
{
|
2018-05-22 15:44:37 +08:00
|
|
|
|
icon.ScaleTo(1, 100);
|
|
|
|
|
circularProgress.FadeOut(100).OnComplete(_ =>
|
|
|
|
|
{
|
|
|
|
|
bind();
|
2018-05-22 15:23:05 +08:00
|
|
|
|
|
2018-05-22 15:44:37 +08:00
|
|
|
|
circularProgress.FadeIn();
|
|
|
|
|
pendingAnimation = false;
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-05-22 00:44:06 +08:00
|
|
|
|
}
|
2018-04-22 00:25:21 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
2018-05-22 15:26:11 +08:00
|
|
|
|
{
|
|
|
|
|
HoverGained?.Invoke();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
2018-05-22 15:26:11 +08:00
|
|
|
|
{
|
|
|
|
|
HoverLost?.Invoke();
|
2018-10-02 11:02:47 +08:00
|
|
|
|
base.OnHoverLost(e);
|
2018-05-22 15:26:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 17:26:12 +08:00
|
|
|
|
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
|
2019-03-12 01:21:34 +08:00
|
|
|
|
{
|
2021-11-18 11:35:47 +08:00
|
|
|
|
if (e.Repeat)
|
|
|
|
|
return false;
|
|
|
|
|
|
2021-09-16 17:26:12 +08:00
|
|
|
|
switch (e.Action)
|
2019-03-12 01:21:34 +08:00
|
|
|
|
{
|
|
|
|
|
case GlobalAction.Back:
|
2023-01-02 10:00:39 +08:00
|
|
|
|
if (!pendingAnimation)
|
|
|
|
|
BeginConfirm();
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case GlobalAction.PauseGameplay:
|
|
|
|
|
// handled by replay player
|
|
|
|
|
if (ReplayLoaded.Value) return false;
|
|
|
|
|
|
2019-03-12 01:21:34 +08:00
|
|
|
|
if (!pendingAnimation)
|
|
|
|
|
BeginConfirm();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 17:26:12 +08:00
|
|
|
|
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
|
2019-03-12 01:21:34 +08:00
|
|
|
|
{
|
2021-09-16 17:26:12 +08:00
|
|
|
|
switch (e.Action)
|
2019-03-12 01:21:34 +08:00
|
|
|
|
{
|
|
|
|
|
case GlobalAction.Back:
|
2023-01-02 10:00:39 +08:00
|
|
|
|
AbortConfirm();
|
|
|
|
|
break;
|
|
|
|
|
|
2020-07-13 08:00:10 +08:00
|
|
|
|
case GlobalAction.PauseGameplay:
|
2023-01-02 10:00:39 +08:00
|
|
|
|
if (ReplayLoaded.Value) return;
|
|
|
|
|
|
2019-03-12 01:21:34 +08:00
|
|
|
|
AbortConfirm();
|
2020-01-22 12:22:34 +08:00
|
|
|
|
break;
|
2019-03-12 01:21:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
2018-05-22 00:44:06 +08:00
|
|
|
|
{
|
2018-09-19 19:52:57 +08:00
|
|
|
|
if (!pendingAnimation && e.CurrentState.Mouse.Buttons.Count() == 1)
|
2018-05-22 00:44:06 +08:00
|
|
|
|
BeginConfirm();
|
|
|
|
|
return true;
|
2018-04-21 23:24:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 17:17:21 +08:00
|
|
|
|
protected override void OnMouseUp(MouseUpEvent e)
|
2018-04-21 23:24:31 +08:00
|
|
|
|
{
|
2018-10-02 11:44:14 +08:00
|
|
|
|
if (!e.HasAnyButtonPressed)
|
2018-05-22 00:44:06 +08:00
|
|
|
|
AbortConfirm();
|
2018-04-21 23:24:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|