1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-24 04:07:26 +08:00
osu-lazer/osu.Game/Overlays/Pause/PauseOverlay.cs

155 lines
4.2 KiB
C#
Raw Normal View History

2017-01-27 17:24:49 +08:00
using System;
using OpenTK;
using OpenTK.Input;
using OpenTK.Graphics;
using osu.Game.Graphics;
using osu.Framework.Audio;
2017-01-27 17:24:49 +08:00
using osu.Framework.Input;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Transformations;
namespace osu.Game.Overlays.Pause
{
public class PauseOverlay : OverlayContainer
{
public event Action OnPause;
public event Action OnResume;
2017-01-27 17:24:49 +08:00
public event Action OnRetry;
public event Action OnQuit;
public bool isPaused = false;
private int fadeDuration = 100;
private double pauseDisableTime = 1000;
private double lastActionTime = -1000;
private PauseButton resumeButton;
private PauseButton retryButton;
private PauseButton quitButton;
2017-01-27 17:24:49 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio, OsuColour colours)
{
var sampleHover = audio.Sample.Get(@"Menu/menuclick");
var sampleBack = audio.Sample.Get(@"Menu/menuback");
var samplePlayClick = audio.Sample.Get(@"Menu/menu-play-click");
2017-01-27 17:24:49 +08:00
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
Alpha = 0.6f,
},
resumeButton = new PauseButton
{
2017-01-27 17:24:49 +08:00
Text = @"Resume",
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Position = new Vector2(0, -200),
Action = Resume,
2017-01-27 17:24:49 +08:00
},
retryButton = new PauseButton
2017-01-27 17:24:49 +08:00
{
Text = @"Retry",
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Action = Retry,
2017-01-27 17:24:49 +08:00
},
quitButton = new PauseButton
2017-01-27 17:24:49 +08:00
{
Text = @"Quit",
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Position = new Vector2(0, 200),
Action = Quit,
},
};
resumeButton.sampleHover = sampleHover;
resumeButton.sampleClick = sampleBack;
retryButton.sampleHover = sampleHover;
retryButton.sampleClick = samplePlayClick;
quitButton.sampleHover = sampleHover;
quitButton.sampleClick = sampleBack;
2017-01-27 17:24:49 +08:00
}
protected override void PopIn()
{
FadeTo(1, fadeDuration, EasingTypes.In);
isPaused = true;
2017-01-27 17:24:49 +08:00
}
protected override void PopOut()
{
FadeTo(0, fadeDuration, EasingTypes.In);
isPaused = false;
2017-01-27 17:24:49 +08:00
}
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
switch (args.Key)
{
case Key.Escape:
TogglePaused();
2017-01-27 17:24:49 +08:00
return true;
}
return base.OnKeyDown(state, args);
}
public void Pause()
{
// Only allow pausing once a second
if (Time.Current >= (lastActionTime + pauseDisableTime))
{
lastActionTime = Time.Current;
Show();
OnPause?.Invoke();
}
else
{
isPaused = false;
}
2017-01-27 17:24:49 +08:00
}
public void Resume()
{
lastActionTime = Time.Current;
Hide();
OnResume?.Invoke();
}
public void TogglePaused()
{
isPaused = !isPaused;
(isPaused ? (Action)Pause : Resume)?.Invoke();
2017-01-27 17:24:49 +08:00
}
private void Retry()
{
Hide();
2017-01-27 17:24:49 +08:00
OnRetry?.Invoke();
}
private void Quit()
{
Hide();
2017-01-27 17:24:49 +08:00
OnQuit?.Invoke();
}
public PauseOverlay()
{
RelativeSizeAxes = Axes.Both;
AutoSizeAxes = Axes.Both;
Depth = -1;
2017-01-27 17:24:49 +08:00
}
}
2017-01-27 17:39:15 +08:00
}