1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 22:07:28 +08:00
osu-lazer/osu.Game/Screens/Play/SkipOverlay.cs

329 lines
11 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 17:19:50 +08:00
using System;
using osu.Framework;
using osu.Framework.Allocation;
2018-07-23 04:57:55 +08:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Threading;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Screens.Ranking;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.Containers;
using osu.Framework.Input.Bindings;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Game.Input.Bindings;
namespace osu.Game.Screens.Play
{
public class SkipOverlay : OverlayContainer, IKeyBindingHandler<GlobalAction>
{
private readonly double startTime;
public Action<double> RequestSeek;
2018-04-13 17:19:50 +08:00
private Button button;
private Box remainingTimeBox;
private FadeContainer fadeContainer;
private double displayTime;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
protected override bool BlockPositionalInput => false;
2018-04-13 17:19:50 +08:00
public SkipOverlay(double startTime)
{
this.startTime = startTime;
State = Visibility.Visible;
RelativePositionAxes = Axes.Both;
RelativeSizeAxes = Axes.X;
2018-04-13 17:19:50 +08:00
Position = new Vector2(0.5f, 0.7f);
Size = new Vector2(1, 100);
2018-04-13 17:19:50 +08:00
Origin = Anchor.Centre;
}
[BackgroundDependencyLoader(true)]
private void load(OsuColour colours, GameplayClock clock)
2018-04-13 17:19:50 +08:00
{
var baseClock = Clock;
if (clock != null)
Clock = clock;
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
fadeContainer = new FadeContainer
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
button = new Button
{
Clock = baseClock,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
remainingTimeBox = new Box
{
Height = 5,
RelativeSizeAxes = Axes.X,
Colour = colours.Yellow,
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
}
}
}
};
}
private const double skip_required_cutoff = 3000;
private const double fade_time = 300;
private double beginFadeTime => startTime - skip_required_cutoff - fade_time;
protected override void LoadComplete()
{
base.LoadComplete();
if (startTime < skip_required_cutoff)
{
Alpha = 0;
Expire();
return;
}
this.FadeInFromZero(fade_time);
using (BeginAbsoluteSequence(beginFadeTime))
this.FadeOut(fade_time);
button.Action = () => RequestSeek?.Invoke(startTime - skip_required_cutoff - fade_time);
2018-04-13 17:19:50 +08:00
displayTime = Time.Current;
Expire();
}
protected override void PopIn() => this.FadeIn();
protected override void PopOut() => this.FadeOut();
protected override void Update()
{
base.Update();
remainingTimeBox.ResizeWidthTo((float)Math.Max(0, 1 - (Time.Current - displayTime) / (beginFadeTime - displayTime)), 120, Easing.OutQuint);
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
2018-04-13 17:19:50 +08:00
{
if (!e.HasAnyButtonPressed)
2018-04-13 17:19:50 +08:00
fadeContainer.State = Visibility.Visible;
2018-10-02 11:02:47 +08:00
return base.OnMouseMove(e);
2018-04-13 17:19:50 +08:00
}
public bool OnPressed(GlobalAction action)
{
switch (action)
{
case GlobalAction.SkipCutscene:
button.Click();
2018-04-13 17:19:50 +08:00
return true;
}
return false;
}
public bool OnReleased(GlobalAction action) => false;
private class FadeContainer : Container, IStateful<Visibility>
{
public event Action<Visibility> StateChanged;
private Visibility state;
private ScheduledDelegate scheduledHide;
public Visibility State
{
get => state;
2018-04-13 17:19:50 +08:00
set
{
bool stateChanged = value != state;
state = value;
scheduledHide?.Cancel();
switch (state)
{
case Visibility.Visible:
// we may be triggered to become visible mnultiple times but we only want to transform once.
if (stateChanged)
this.FadeIn(500, Easing.OutExpo);
if (!IsHovered && !IsDragged)
using (BeginDelayedSequence(1000))
scheduledHide = Schedule(() => State = Visibility.Hidden);
break;
case Visibility.Hidden:
this.FadeOut(1000, Easing.OutExpo);
break;
}
StateChanged?.Invoke(State);
}
}
protected override void LoadComplete()
{
base.LoadComplete();
State = Visibility.Visible;
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
2018-04-13 17:19:50 +08:00
{
scheduledHide?.Cancel();
2018-10-02 11:02:47 +08:00
return base.OnMouseDown(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseUp(MouseUpEvent e)
2018-04-13 17:19:50 +08:00
{
State = Visibility.Visible;
2018-10-02 11:02:47 +08:00
return base.OnMouseUp(e);
2018-04-13 17:19:50 +08:00
}
}
private class Button : OsuClickableContainer
{
private Color4 colourNormal;
private Color4 colourHover;
private Box box;
private FillFlowContainer flow;
private Box background;
private AspectContainer aspect;
2018-07-23 04:57:55 +08:00
private SampleChannel sampleConfirm;
2018-04-13 17:19:50 +08:00
public Button()
{
RelativeSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
2018-08-31 06:04:40 +08:00
private void load(OsuColour colours, AudioManager audio)
2018-04-13 17:19:50 +08:00
{
colourNormal = colours.Yellow;
colourHover = colours.YellowDark;
2018-08-31 06:04:40 +08:00
sampleConfirm = audio.Sample.Get(@"SongSelect/confirm-selection");
2018-07-23 04:57:55 +08:00
2018-04-13 17:19:50 +08: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,
Children = new[]
{
new SpriteIcon { Size = new Vector2(15), Shadow = true, Icon = FontAwesome.fa_chevron_right },
new SpriteIcon { Size = new Vector2(15), Shadow = true, Icon = FontAwesome.fa_chevron_right },
new SpriteIcon { Size = new Vector2(15), Shadow = true, Icon = FontAwesome.fa_chevron_right },
}
},
new OsuSpriteText
{
Anchor = Anchor.TopCentre,
RelativePositionAxes = Axes.Y,
Y = 0.7f,
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 12),
2018-04-13 17:19:50 +08:00
Origin = Anchor.Centre,
Text = @"SKIP",
},
}
}
};
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
flow.TransformSpacingTo(new Vector2(5), 500, Easing.OutQuint);
box.FadeColour(colourHover, 500, Easing.OutQuint);
background.FadeTo(0.4f, 500, Easing.OutQuint);
return true;
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08: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 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
2018-04-13 17:19:50 +08:00
{
aspect.ScaleTo(0.75f, 2000, Easing.OutQuint);
2018-10-02 11:02:47 +08:00
return base.OnMouseDown(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseUp(MouseUpEvent e)
2018-04-13 17:19:50 +08:00
{
aspect.ScaleTo(1, 1000, Easing.OutElastic);
2018-10-02 11:02:47 +08:00
return base.OnMouseUp(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
2018-04-13 17:19:50 +08:00
{
2019-02-21 17:56:34 +08:00
if (!Enabled.Value)
2018-04-13 17:19:50 +08:00
return false;
2018-07-23 04:57:55 +08:00
sampleConfirm.Play();
2018-04-13 17:19:50 +08:00
box.FlashColour(Color4.White, 500, Easing.OutQuint);
aspect.ScaleTo(1.2f, 2000, Easing.OutQuint);
2018-10-02 11:02:47 +08:00
bool result = base.OnClick(e);
2018-04-13 17:19:50 +08:00
// for now, let's disable the skip button after the first press.
// this will likely need to be contextual in the future (bound from external components).
Enabled.Value = false;
return result;
}
}
}
}