1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 12:02:57 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/HoldForMenuButton.cs

249 lines
8.1 KiB
C#
Raw Normal View History

// 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
using System;
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;
using osu.Framework.Graphics.Sprites;
2018-04-21 23:24:31 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Bindings;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
using osu.Game.Configuration;
2018-04-21 23:24:31 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2018-04-21 23:24:31 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Input.Bindings;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-21 23:24:31 +08:00
namespace osu.Game.Screens.Play.HUD
{
public class HoldForMenuButton : FillFlowContainer
2018-04-21 23:24:31 +08:00
{
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
public readonly Bindable<bool> IsPaused = new Bindable<bool>();
2018-05-04 04:50:30 +08:00
private readonly Button button;
2018-05-11 02:08:02 +08:00
public Action Action
{
set => button.Action = value;
}
2018-04-21 23:24:31 +08:00
2018-05-21 23:49:33 +08:00
private readonly OsuSpriteText text;
public HoldForMenuButton()
2018-04-21 23:24:31 +08:00
{
Direction = FillDirection.Horizontal;
Spacing = new Vector2(20, 0);
Margin = new MarginPadding(10);
2018-04-21 23:24:31 +08:00
Children = new Drawable[]
{
text = new OsuSpriteText
{
Font = OsuFont.GetFont(weight: FontWeight.Bold),
2018-04-21 23:24:31 +08:00
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft
},
button = new Button
{
HoverGained = () => text.FadeIn(500, Easing.OutQuint),
HoverLost = () => text.FadeOut(500, Easing.OutQuint),
IsPaused = { BindTarget = IsPaused }
}
2018-04-21 23:24:31 +08:00
};
AutoSizeAxes = Axes.Both;
}
[Resolved]
private OsuConfigManager config { get; set; }
2019-10-02 12:26:46 +08:00
private Bindable<float> activationDelay;
protected override void LoadComplete()
{
2019-10-02 12:26:46 +08:00
activationDelay = config.GetBindable<float>(OsuSetting.UIHoldActivationDelay);
2019-09-27 07:59:42 +08:00
activationDelay.BindValueChanged(v =>
{
text.Text = v.NewValue > 0
? "hold for menu"
: "press for menu";
}, true);
text.FadeInFromZero(500, Easing.OutQuint).Delay(1500).FadeOut(500, Easing.OutQuint);
base.LoadComplete();
}
private float positionalAdjust;
2018-10-02 11:02:47 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
{
positionalAdjust = Vector2.Distance(e.ScreenSpaceMousePosition, button.ScreenSpaceDrawQuad.Centre) / 200;
2018-10-02 11:02:47 +08:00
return base.OnMouseMove(e);
}
2018-05-21 23:49:33 +08:00
protected override void Update()
{
base.Update();
if (text.Alpha > 0 || button.Progress.Value > 0 || button.IsHovered)
Alpha = 1;
else
2019-11-11 19:53:22 +08:00
{
Alpha = Interpolation.ValueAt(
Math.Clamp(Clock.ElapsedFrameTime, 0, 200),
Alpha, Math.Clamp(1 - positionalAdjust, 0.04f, 1), 0, 200, Easing.OutQuint);
2019-11-11 19:53:22 +08:00
}
2018-05-21 23:49:33 +08:00
}
private class Button : HoldToConfirmContainer, IKeyBindingHandler<GlobalAction>
2018-04-21 23:24:31 +08:00
{
private SpriteIcon icon;
private CircularProgress circularProgress;
2018-05-22 14:59:53 +08:00
private Circle overlayCircle;
2018-05-21 23:49:33 +08:00
public readonly Bindable<bool> IsPaused = new Bindable<bool>();
protected override bool AllowMultipleFires => true;
public Action HoverGained;
public Action HoverLost;
2018-04-21 23:24:31 +08:00
[BackgroundDependencyLoader]
2019-05-04 15:50:36 +08:00
private void load(OsuColour colours, Framework.Game game)
2018-04-21 23:24:31 +08:00
{
Size = new Vector2(60);
Child = new CircularContainer
2018-04-21 23:24:31 +08:00
{
Masking = true,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2018-04-21 23:24:31 +08:00
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.Gray1,
Alpha = 0.5f,
},
circularProgress = new CircularProgress
{
RelativeSizeAxes = Axes.Both,
InnerRadius = 1
},
2018-05-22 14:59:53 +08:00
overlayCircle = new Circle
{
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),
Icon = FontAwesome.Solid.Times
},
}
};
bind();
2019-05-07 12:34:06 +08:00
}
private void bind()
{
circularProgress.Current.BindTo(Progress);
Progress.ValueChanged += progress => icon.Scale = new Vector2(1 + (float)progress.NewValue * 0.2f);
2018-04-21 23:24:31 +08:00
}
private bool pendingAnimation;
protected override void Confirm()
2018-04-21 23:24:31 +08:00
{
base.Confirm();
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).
Progress.UnbindAll();
2018-05-22 14:58:00 +08:00
// avoid starting a new confirm call until we finish animating.
pendingAnimation = true;
AbortConfirm();
2018-05-22 14:59:53 +08:00
overlayCircle.ScaleTo(0, 100)
.Then().FadeOut().ScaleTo(1).FadeIn(500)
2018-05-22 15:44:37 +08:00
.OnComplete(a =>
{
2018-05-22 15:44:37 +08:00
icon.ScaleTo(1, 100);
circularProgress.FadeOut(100).OnComplete(_ =>
{
bind();
2018-05-22 15:44:37 +08:00
circularProgress.FadeIn();
pendingAnimation = false;
});
});
}
2018-04-22 00:25:21 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
{
HoverGained?.Invoke();
return true;
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
{
HoverLost?.Invoke();
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
}
public bool OnPressed(GlobalAction action)
{
switch (action)
{
case GlobalAction.Back:
2020-07-12 22:05:47 +08:00
case GlobalAction.PauseGameplay: // in the future this behaviour will differ for replays etc.
if (!pendingAnimation)
BeginConfirm();
return true;
}
return false;
}
public void OnReleased(GlobalAction action)
{
switch (action)
{
case GlobalAction.Back:
2020-07-13 08:00:10 +08:00
case GlobalAction.PauseGameplay:
AbortConfirm();
break;
}
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
{
if (!pendingAnimation && e.CurrentState.Mouse.Buttons.Count() == 1)
BeginConfirm();
return true;
2018-04-21 23:24:31 +08:00
}
protected override void OnMouseUp(MouseUpEvent e)
2018-04-21 23:24:31 +08:00
{
if (!e.HasAnyButtonPressed)
AbortConfirm();
2018-04-21 23:24:31 +08:00
}
}
}
}