osu/osu.Game/Screens/Play/SkipOverlay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

398 lines
13 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-13 09:19:50 +00:00
2022-06-17 07:37:17 +00:00
#nullable disable
2017-05-19 12:54:14 +00:00
using System;
using osu.Framework;
2017-01-27 12:57:22 +00:00
using osu.Framework.Allocation;
2018-07-22 20:57:55 +00:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2017-01-27 12:57:22 +00:00
using osu.Framework.Graphics;
2017-05-19 12:54:14 +00:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Bindings;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2020-01-09 04:43:44 +00:00
using osu.Framework.Utils;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Input.Bindings;
using osu.Game.Screens.Ranking;
using osuTK;
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
namespace osu.Game.Screens.Play
{
2020-05-18 10:37:49 +00:00
public class SkipOverlay : CompositeDrawable, IKeyBindingHandler<GlobalAction>
2017-01-27 12:57:22 +00:00
{
2017-05-19 09:18:21 +00:00
private readonly double startTime;
2018-04-13 09:19:50 +00:00
2019-11-21 09:57:19 +00:00
public Action RequestSkip;
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
private Button button;
private ButtonContainer buttonContainer;
2017-05-19 12:54:14 +00:00
private Box remainingTimeBox;
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
private FadeContainer fadeContainer;
private double displayTime;
2018-04-13 09:19:50 +00:00
private bool isClickable;
2022-08-16 05:27:02 +00:00
private bool skipQueued;
2022-08-05 21:00:37 +00:00
[Resolved]
private IGameplayClock gameplayClock { get; set; }
2022-08-16 05:17:28 +00:00
internal bool IsButtonVisible => fadeContainer.State == Visibility.Visible && buttonContainer.State.Value == Visibility.Visible;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
2018-04-13 09:19:50 +00:00
/// <summary>
/// Displays a skip overlay, giving the user the ability to skip forward.
/// </summary>
/// <param name="startTime">The time at which gameplay begins to appear.</param>
2018-03-05 15:10:53 +00:00
public SkipOverlay(double startTime)
2017-01-27 12:57:22 +00:00
{
2017-05-19 09:18:21 +00:00
this.startTime = startTime;
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
RelativePositionAxes = Axes.Both;
RelativeSizeAxes = Axes.X;
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
Position = new Vector2(0.5f, 0.7f);
Size = new Vector2(1, 100);
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
Origin = Anchor.Centre;
}
2018-04-13 09:19:50 +00:00
[BackgroundDependencyLoader(true)]
private void load(OsuColour colours)
2017-01-27 12:57:22 +00:00
{
2020-05-18 10:37:49 +00:00
InternalChild = buttonContainer = new ButtonContainer
2017-05-19 12:54:14 +00:00
{
RelativeSizeAxes = Axes.Both,
Child = fadeContainer = new FadeContainer
2017-05-19 12:54:14 +00:00
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
button = new Button
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
remainingTimeBox = new Box
{
Height = 5,
RelativeSizeAxes = Axes.X,
Colour = colours.Yellow,
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
}
}
}
};
}
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
private const double fade_time = 300;
2018-04-13 09:19:50 +00:00
2021-04-14 08:47:11 +00:00
private double fadeOutBeginTime => startTime - MasterGameplayClockContainer.MINIMUM_SKIP_TIME;
2018-04-13 09:19:50 +00:00
public override void Hide()
{
base.Hide();
fadeContainer.Hide();
}
public override void Show()
{
base.Show();
fadeContainer.TriggerShow();
}
2017-05-19 12:54:14 +00:00
protected override void LoadComplete()
{
base.LoadComplete();
2018-04-13 09:19:50 +00:00
displayTime = gameplayClock.CurrentTime;
// skip is not required if there is no extra "empty" time to skip.
// we may need to remove this if rewinding before the initial player load position becomes a thing.
if (fadeOutBeginTime <= displayTime)
2017-05-19 09:18:21 +00:00
{
Expire();
return;
}
2018-04-13 09:19:50 +00:00
2019-11-21 09:57:19 +00:00
button.Action = () => RequestSkip?.Invoke();
fadeContainer.TriggerShow();
2022-08-16 05:27:02 +00:00
if (skipQueued)
{
Scheduler.AddDelayed(() => button.TriggerClick(), 200);
skipQueued = false;
}
}
public void SkipWhenReady()
{
if (IsLoaded)
button.TriggerClick();
else
skipQueued = true;
2017-01-27 12:57:22 +00:00
}
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
protected override void Update()
{
base.Update();
2019-11-21 09:57:19 +00:00
// This case causes an immediate expire in `LoadComplete`, but `Update` may run once after that.
// Avoid div-by-zero below.
if (fadeOutBeginTime <= displayTime)
return;
double progress = Math.Max(0, 1 - (gameplayClock.CurrentTime - displayTime) / (fadeOutBeginTime - displayTime));
remainingTimeBox.Width = (float)Interpolation.Lerp(remainingTimeBox.Width, progress, Math.Clamp(Time.Elapsed / 40, 0, 1));
isClickable = progress > 0;
button.Enabled.Value = isClickable;
buttonContainer.State.Value = isClickable ? Visibility.Visible : Visibility.Hidden;
2017-05-19 12:54:14 +00:00
}
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
protected override bool OnMouseMove(MouseMoveEvent e)
{
if (isClickable && !e.HasAnyButtonPressed)
fadeContainer.TriggerShow();
2018-10-02 03:02:47 +00:00
return base.OnMouseMove(e);
}
2018-04-13 09:19:50 +00:00
2021-09-16 09:26:12 +00:00
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
2017-02-22 12:28:40 +00:00
{
if (e.Repeat)
return false;
2021-09-16 09:26:12 +00:00
switch (e.Action)
2017-02-22 12:28:40 +00:00
{
case GlobalAction.SkipCutscene:
if (!button.Enabled.Value)
return false;
2021-08-04 08:27:44 +00:00
button.TriggerClick();
2017-02-22 12:28:40 +00:00
return true;
}
2018-04-13 09:19:50 +00:00
return false;
2017-02-22 12:28:40 +00:00
}
2018-04-13 09:19:50 +00:00
2021-09-16 09:26:12 +00:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
2018-04-13 09:19:50 +00:00
public class FadeContainer : Container, IStateful<Visibility>
2017-05-19 12:54:14 +00:00
{
2017-09-04 00:10:04 +00:00
public event Action<Visibility> StateChanged;
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
private Visibility state;
private double? nextHideTime;
2018-04-13 09:19:50 +00:00
2019-07-04 07:53:08 +00:00
public override bool IsPresent => true;
public void TriggerShow()
{
Show();
if (!IsHovered && !IsDragged)
nextHideTime = Time.Current + 1000;
else
nextHideTime = null;
}
protected override void Update()
{
base.Update();
if (nextHideTime != null && nextHideTime <= Time.Current)
{
Hide();
nextHideTime = null;
}
}
2017-05-19 12:54:14 +00:00
public Visibility State
{
get => state;
2017-05-19 12:54:14 +00:00
set
{
if (value == state)
return;
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
state = value;
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
switch (state)
{
case Visibility.Visible:
this.FadeIn(500, Easing.OutExpo);
2017-05-19 12:54:14 +00:00
break;
2019-04-01 03:44:46 +00:00
2017-05-19 12:54:14 +00:00
case Visibility.Hidden:
2017-07-22 18:50:25 +00:00
this.FadeOut(1000, Easing.OutExpo);
2017-05-19 12:54:14 +00:00
break;
}
2018-04-13 09:19:50 +00:00
2017-09-04 00:10:04 +00:00
StateChanged?.Invoke(State);
2017-05-19 12:54:14 +00:00
}
}
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
protected override void LoadComplete()
{
base.LoadComplete();
Show();
2017-05-19 12:54:14 +00:00
}
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
protected override bool OnMouseDown(MouseDownEvent e)
{
Show();
nextHideTime = null;
return true;
}
2018-04-13 09:19:50 +00:00
protected override void OnMouseUp(MouseUpEvent e)
{
Show();
}
public override void Hide() => State = Visibility.Hidden;
public override void Show() => State = Visibility.Visible;
2017-05-19 12:54:14 +00:00
}
2018-04-13 09:19:50 +00:00
private class ButtonContainer : VisibilityContainer
{
protected override void PopIn() => this.FadeIn(fade_time);
protected override void PopOut() => this.FadeOut(fade_time);
}
private class Button : OsuClickableContainer
2017-05-19 12:54:14 +00:00
{
private Color4 colourNormal;
private Color4 colourHover;
private Box box;
private FillFlowContainer flow;
private Box background;
private AspectContainer aspect;
2018-04-13 09:19:50 +00:00
2021-01-19 08:11:40 +00:00
private Sample sampleConfirm;
2018-07-22 20:57:55 +00:00
2017-05-19 12:54:14 +00:00
public Button()
{
RelativeSizeAxes = Axes.Both;
}
2018-04-13 09:19:50 +00:00
2017-05-19 12:54:14 +00:00
[BackgroundDependencyLoader]
2018-08-30 22:04:40 +00:00
private void load(OsuColour colours, AudioManager audio)
2017-05-19 12:54:14 +00:00
{
colourNormal = colours.Yellow;
colourHover = colours.YellowDark;
2018-04-13 09:19:50 +00:00
sampleConfirm = audio.Samples.Get(@"UI/submit-select");
2018-07-22 20:57:55 +00:00
2017-05-19 12:54:14 +00:00
Children = new Drawable[]
{
background = new Box
{
Alpha = 0.2f,
Colour = Color4.Black,
RelativeSizeAxes = Axes.Both,
},
aspect = new AspectContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Y,
Height = 0.6f,
Masking = true,
CornerRadius = 15,
Children = new Drawable[]
{
box = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourNormal,
},
flow = new FillFlowContainer
{
Anchor = Anchor.TopCentre,
RelativePositionAxes = Axes.Y,
Y = 0.4f,
AutoSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Direction = FillDirection.Horizontal,
2017-06-08 07:27:35 +00:00
Children = new[]
2017-05-19 12:54:14 +00:00
{
2019-04-02 10:55:24 +00:00
new SpriteIcon { Size = new Vector2(15), Shadow = true, Icon = FontAwesome.Solid.ChevronRight },
new SpriteIcon { Size = new Vector2(15), Shadow = true, Icon = FontAwesome.Solid.ChevronRight },
new SpriteIcon { Size = new Vector2(15), Shadow = true, Icon = FontAwesome.Solid.ChevronRight },
2017-05-19 12:54:14 +00:00
}
},
new OsuSpriteText
{
Anchor = Anchor.TopCentre,
RelativePositionAxes = Axes.Y,
Y = 0.7f,
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 12),
2017-05-19 12:54:14 +00:00
Origin = Anchor.Centre,
Text = @"SKIP",
},
}
}
};
}
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
protected override bool OnHover(HoverEvent e)
2017-05-19 12:54:14 +00:00
{
2017-07-22 18:50:25 +00:00
flow.TransformSpacingTo(new Vector2(5), 500, Easing.OutQuint);
box.FadeColour(colourHover, 500, Easing.OutQuint);
background.FadeTo(0.4f, 500, Easing.OutQuint);
2018-03-05 15:27:55 +00:00
return true;
2017-05-19 12:54:14 +00:00
}
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
protected override void OnHoverLost(HoverLostEvent e)
2017-05-19 12:54:14 +00:00
{
2017-07-22 18:50:25 +00:00
flow.TransformSpacingTo(new Vector2(0), 500, Easing.OutQuint);
box.FadeColour(colourNormal, 500, Easing.OutQuint);
background.FadeTo(0.2f, 500, Easing.OutQuint);
2018-10-02 03:02:47 +00:00
base.OnHoverLost(e);
2017-05-19 12:54:14 +00:00
}
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
protected override bool OnMouseDown(MouseDownEvent e)
2017-05-19 12:54:14 +00:00
{
2017-07-22 18:50:25 +00:00
aspect.ScaleTo(0.75f, 2000, Easing.OutQuint);
2018-10-02 03:02:47 +00:00
return base.OnMouseDown(e);
2017-05-19 12:54:14 +00:00
}
2018-04-13 09:19:50 +00:00
protected override void OnMouseUp(MouseUpEvent e)
2017-05-19 12:54:14 +00:00
{
2017-07-22 18:50:25 +00:00
aspect.ScaleTo(1, 1000, Easing.OutElastic);
base.OnMouseUp(e);
2017-05-19 12:54:14 +00:00
}
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
protected override bool OnClick(ClickEvent e)
2017-05-19 12:54:14 +00:00
{
2019-02-21 09:56:34 +00:00
if (!Enabled.Value)
return false;
2018-04-13 09:19:50 +00:00
2018-07-22 20:57:55 +00:00
sampleConfirm.Play();
2017-07-22 18:50:25 +00:00
box.FlashColour(Color4.White, 500, Easing.OutQuint);
aspect.ScaleTo(1.2f, 2000, Easing.OutQuint);
2018-04-13 09:19:50 +00:00
2019-11-21 09:57:19 +00:00
return base.OnClick(e);
2017-05-19 12:54:14 +00:00
}
}
2017-01-27 12:57:22 +00:00
}
}