From feba3f35babbd6aefe5e0282b485783aa9573f37 Mon Sep 17 00:00:00 2001 From: DrabWeb Date: Fri, 27 Jan 2017 15:28:39 -0400 Subject: [PATCH] Changed the roles of PauseOverlay and player in pausing, PauseOverlay is now only the UI portion and doesn't do things like actually pause the game, and only calls actions and hides itself, whereas Player actually pauses the game and brings up the pause overlay in the first place --- .../Tests/TestCasePauseOverlay.cs | 9 +- osu.Game/Overlays/Pause/PauseOverlay.cs | 85 ++++++------------- osu.Game/Screens/Play/Player.cs | 63 +++++++++++++- 3 files changed, 89 insertions(+), 68 deletions(-) diff --git a/osu.Desktop.VisualTests/Tests/TestCasePauseOverlay.cs b/osu.Desktop.VisualTests/Tests/TestCasePauseOverlay.cs index c1f81810ab..be1f9d57d7 100644 --- a/osu.Desktop.VisualTests/Tests/TestCasePauseOverlay.cs +++ b/osu.Desktop.VisualTests/Tests/TestCasePauseOverlay.cs @@ -38,13 +38,8 @@ public override void Reset() Width = 100, Height = 50, Colour = Color4.Black, - Action = (() => pauseOverlay.Pause()), - }); - - pauseOverlay.OnPause += (() => Logger.Log(@"Pause")); - pauseOverlay.OnResume += (() => Logger.Log(@"Resume")); - pauseOverlay.OnRetry += (() => Logger.Log(@"Retry")); - pauseOverlay.OnQuit += (() => Logger.Log(@"Quit")); + Action = (() => pauseOverlay.Show()), + }); } } } diff --git a/osu.Game/Overlays/Pause/PauseOverlay.cs b/osu.Game/Overlays/Pause/PauseOverlay.cs index bbf424773e..1590b8b726 100644 --- a/osu.Game/Overlays/Pause/PauseOverlay.cs +++ b/osu.Game/Overlays/Pause/PauseOverlay.cs @@ -3,30 +3,24 @@ using OpenTK.Input; using OpenTK.Graphics; using osu.Game.Graphics; -using osu.Framework.Audio; +using osu.Game.Screens.Menu; +using osu.Game.Screens.Play; 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; -using osu.Game.Graphics.Backgrounds; -using osu.Game.Screens.Menu; namespace osu.Game.Overlays.Pause { public class PauseOverlay : OverlayContainer { - public event Action OnPause; - public event Action OnResume; - public event Action OnRetry; - public event Action OnQuit; - - public bool isPaused = false; - private int fadeDuration = 100; - private double pauseCooldown = 1000; - private double lastActionTime = 0; + + public Action OnResume; + public Action OnRetry; + public Action OnQuit; [BackgroundDependencyLoader] private void load(OsuColour colours) @@ -96,23 +90,34 @@ private void load(OsuColour colours) Type = PauseButtonType.Resume, Origin = Anchor.TopCentre, Anchor = Anchor.TopCentre, - Action = Resume, + Action = (delegate + { + Hide(); + OnResume?.Invoke(); + }), }, new PauseButton { Type = PauseButtonType.Retry, Origin = Anchor.TopCentre, Anchor = Anchor.TopCentre, - Action = Retry, + Action = (delegate + { + Hide(); + OnRetry?.Invoke(); + }), }, new PauseButton { Type = PauseButtonType.Quit, Origin = Anchor.TopCentre, Anchor = Anchor.TopCentre, - Action = Quit, - }, - new Button(@"solo", @"freeplay", FontAwesome.fa_user, new Color4(102, 68, 204, 255), () => OnPause?.Invoke(), 300, Key.P), + Action = (delegate + { + Hide(); + OnQuit?.Invoke(); + }), + }, } }, }; @@ -121,65 +126,25 @@ private void load(OsuColour colours) protected override void PopIn() { FadeTo(1, fadeDuration, EasingTypes.In); - isPaused = true; } protected override void PopOut() { FadeTo(0, fadeDuration, EasingTypes.In); - isPaused = false; } protected override bool OnKeyDown(InputState state, KeyDownEventArgs args) { switch (args.Key) { - case Key.Escape: - TogglePaused(); + case Key.Escape: + Hide(); + OnResume?.Invoke(); return true; } return base.OnKeyDown(state, args); } - public void Pause() - { - if (Time.Current >= (lastActionTime + pauseCooldown)) - { - lastActionTime = Time.Current; - Show(); - OnPause?.Invoke(); - } - else - { - isPaused = false; - } - } - - public void Resume() - { - lastActionTime = Time.Current; - Hide(); - OnResume?.Invoke(); - } - - public void TogglePaused() - { - isPaused = !isPaused; - (isPaused ? (Action)Pause : Resume)?.Invoke(); - } - - private void Retry() - { - Hide(); - OnRetry?.Invoke(); - } - - private void Quit() - { - Hide(); - OnQuit?.Invoke(); - } - public PauseOverlay() { RelativeSizeAxes = Axes.Both; diff --git a/osu.Game/Screens/Play/Player.cs b/osu.Game/Screens/Play/Player.cs index 751f03f1f8..82cff73508 100644 --- a/osu.Game/Screens/Play/Player.cs +++ b/osu.Game/Screens/Play/Player.cs @@ -22,6 +22,7 @@ using osu.Game.Modes.UI; using osu.Game.Screens.Ranking; using osu.Game.Configuration; +using osu.Game.Overlays.Pause; using osu.Framework.Configuration; using System; using OpenTK.Graphics; @@ -40,6 +41,11 @@ public class Player : OsuGameMode public PlayMode PreferredPlayMode; + public bool isPaused; + + private double pauseCooldown = 1000; + private double lastActionTime = 0; + private IAdjustableClock sourceClock; private Ruleset ruleset; @@ -48,6 +54,9 @@ public class Player : OsuGameMode private HitRenderer hitRenderer; private Bindable dimLevel; + private ScoreOverlay scoreOverlay; + private PauseOverlay pauseOverlay; + [BackgroundDependencyLoader] private void load(AudioManager audio, BeatmapDatabase beatmaps, OsuGameBase game, OsuConfigManager config) { @@ -92,9 +101,14 @@ private void load(AudioManager audio, BeatmapDatabase beatmaps, OsuGameBase game ruleset = Ruleset.GetRuleset(usablePlayMode); - var scoreOverlay = ruleset.CreateScoreOverlay(); + scoreOverlay = ruleset.CreateScoreOverlay(); scoreOverlay.BindProcessor(scoreProcessor = ruleset.CreateScoreProcessor(beatmap.HitObjects.Count)); + pauseOverlay = new PauseOverlay(); + pauseOverlay.OnResume = Resume; + //pauseOverlay.OnRetry = Retry; Add when retrying is implemented + pauseOverlay.OnQuit = Exit; + hitRenderer = ruleset.CreateHitRendererWith(beatmap.HitObjects); //bind HitRenderer to ScoreProcessor and ourselves (for a pass situation) @@ -119,9 +133,41 @@ private void load(AudioManager audio, BeatmapDatabase beatmaps, OsuGameBase game } }, scoreOverlay, + pauseOverlay }; } + public void Pause() + { + if (Time.Current >= (lastActionTime + pauseCooldown)) + { + lastActionTime = Time.Current; + isPaused = true; + scoreOverlay.KeyCounter.IsCounting = false; + pauseOverlay.Show(); + sourceClock.Stop(); + } + else + { + isPaused = false; + } + } + + public void Resume() + { + lastActionTime = Time.Current; + isPaused = false; + scoreOverlay.KeyCounter.IsCounting = true; + pauseOverlay.Hide(); + sourceClock.Start(); + } + + public void TogglePaused() + { + isPaused = !isPaused; + (isPaused ? (Action)Pause : Resume)?.Invoke(); + } + protected override void LoadComplete() { base.LoadComplete(); @@ -181,6 +227,21 @@ protected override bool OnExiting(GameMode next) return base.OnExiting(next); } + protected override bool OnKeyDown(InputState state, KeyDownEventArgs args) + { + switch (args.Key) + { + case Key.Escape: + if (!isPaused) + { + Pause(); + return true; + } + else { return false; } + } + return base.OnKeyDown(state, args); + } + private void dimChanged(object sender, EventArgs e) { Background?.FadeTo((100f - dimLevel) / 100, 800);