1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 20:47:25 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/HoldToQuit.cs

130 lines
4.3 KiB
C#
Raw Normal View History

2018-04-21 23:24:31 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
using osu.Framework.Threading;
2018-04-22 00:25:21 +08:00
using osu.Framework.Timing;
2018-04-21 23:24:31 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using OpenTK;
namespace osu.Game.Screens.Play.HUD
{
public class HoldToQuit : Container
{
private readonly OsuSpriteText text;
private readonly HoldToQuitButton button;
public HoldToQuit()
{
Children = new Drawable[]
{
text = new OsuSpriteText
{
Text = "Hold to Quit",
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft
},
button = new HoldToQuitButton(text)
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight
}
};
AutoSizeAxes = Axes.Both;
}
private class HoldToQuitButton : CircularContainer
{
private readonly OsuSpriteText text;
private SpriteIcon icon;
private CircularProgress progress;
private Action exitAction;
private ScheduledDelegate scheduledExitAction;
2018-04-22 00:25:21 +08:00
private readonly Scheduler scheduler;
private readonly StopwatchClock stopwatchClock;
2018-04-21 23:24:31 +08:00
private const int fade_duration = 200;
2018-04-22 00:25:21 +08:00
private const int text_display_time = 5000;
2018-04-21 23:24:31 +08:00
public HoldToQuitButton(OsuSpriteText text)
{
this.text = text;
scheduler = new Scheduler();
2018-04-22 00:25:21 +08:00
stopwatchClock = new StopwatchClock();
2018-04-21 23:24:31 +08:00
// TODO provide action
exitAction = () => Thread.Sleep(1);
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Masking = true;
Size = new Vector2(60);
AddRange(new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.Gray1,
Alpha = 0.8f,
},
icon = new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(15),
Icon = FontAwesome.fa_close
},
2018-04-22 00:25:21 +08:00
progress = new CircularProgress { RelativeSizeAxes = Axes.Both, InnerRadius = 0.1f }
2018-04-21 23:24:31 +08:00
});
2018-04-22 00:25:21 +08:00
scheduler.AddDelayed(() => text.FadeOut(fade_duration), text_display_time);
2018-04-21 23:24:31 +08:00
}
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
{
icon.ScaleTo(1.5f);
text.FadeIn(fade_duration);
2018-04-22 00:25:21 +08:00
stopwatchClock.Restart();
2018-04-21 23:24:31 +08:00
scheduledExitAction = scheduler.AddDelayed(exitAction, 1000);
2018-04-22 00:25:21 +08:00
2018-04-21 23:24:31 +08:00
return base.OnMouseDown(state, args);
}
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
{
icon.ScaleTo(1f);
2018-04-22 00:25:21 +08:00
scheduler.AddDelayed(() => text.FadeOut(fade_duration), text_display_time);
stopwatchClock.Stop();
2018-04-21 23:24:31 +08:00
if (scheduledExitAction != null && !scheduledExitAction.Completed)
scheduledExitAction.Cancel();
2018-04-22 00:25:21 +08:00
progress.Current.SetDefault();
2018-04-21 23:24:31 +08:00
return base.OnMouseUp(state, args);
}
protected override void Update()
{
scheduler.Update();
2018-04-22 00:25:21 +08:00
if (stopwatchClock.IsRunning)
{
var clampedTime = MathHelper.Clamp(stopwatchClock.CurrentTime, 0, 1000);
progress.Current.Value = clampedTime / 1000;
}
2018-04-21 23:24:31 +08:00
base.Update();
}
}
}
}