From 33e2ae2f2383d4ed85e7c53d8931fa9ab893c68f Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 26 Nov 2024 14:13:07 +0900 Subject: [PATCH] Refactor `MedalOverlay` to be more readable Shouldn't really have any functionality changes, just fixing some old code that I can't easily parse these days. --- osu.Game/Overlays/MedalOverlay.cs | 74 ++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 26 deletions(-) diff --git a/osu.Game/Overlays/MedalOverlay.cs b/osu.Game/Overlays/MedalOverlay.cs index 19f61cb91029..f06f8aab752d 100644 --- a/osu.Game/Overlays/MedalOverlay.cs +++ b/osu.Game/Overlays/MedalOverlay.cs @@ -35,7 +35,7 @@ public partial class MedalOverlay : OsuFocusedOverlayContainer private IAPIProvider api { get; set; } = null!; private Container medalContainer = null!; - private MedalAnimation? lastAnimation; + private MedalAnimation? currentMedalDisplay; [BackgroundDependencyLoader] private void load() @@ -54,11 +54,7 @@ protected override void LoadComplete() { base.LoadComplete(); - OverlayActivationMode.BindValueChanged(val => - { - if (val.NewValue == OverlayActivation.All && (queuedMedals.Any() || medalContainer.Any() || lastAnimation?.IsLoaded == false)) - Show(); - }, true); + OverlayActivationMode.BindValueChanged(_ => displayIfReady(), true); } private void handleMedalMessages(SocketMessage obj) @@ -86,43 +82,69 @@ private void handleMedalMessages(SocketMessage obj) queuedMedals.Enqueue(medalAnimation); Logger.Log($"Queueing medal unlock for \"{medal.Name}\" ({queuedMedals.Count} to display)"); - if (OverlayActivationMode.Value == OverlayActivation.All) - Scheduler.AddOnce(Show); + Schedule(displayIfReady); + } + + protected override bool OnClick(ClickEvent e) + { + dismissDisplayedMedal(); + loadNextMedal(); + return true; + } + + public override bool OnPressed(KeyBindingPressEvent e) + { + if (e.Action == GlobalAction.Back) + { + dismissDisplayedMedal(); + loadNextMedal(); + return true; + } + + return base.OnPressed(e); } - protected override void Update() + private void dismissDisplayedMedal() { - base.Update(); + currentMedalDisplay?.Dismiss(); + currentMedalDisplay = null; + } - if (medalContainer.Any() || lastAnimation?.IsLoaded == false) + private void displayIfReady() + { + if (OverlayActivationMode.Value != OverlayActivation.All) return; - if (!queuedMedals.TryDequeue(out lastAnimation)) + if (currentMedalDisplay != null) { - Logger.Log("All queued medals have been displayed!"); - Hide(); + Show(); return; } - Logger.Log($"Preparing to display \"{lastAnimation.Medal.Name}\""); - LoadComponentAsync(lastAnimation, medalContainer.Add); + if (queuedMedals.Any()) + { + Show(); + loadNextMedal(); + } } - protected override bool OnClick(ClickEvent e) + private void loadNextMedal() { - lastAnimation?.Dismiss(); - return true; - } + if (currentMedalDisplay != null) + return; - public override bool OnPressed(KeyBindingPressEvent e) - { - if (e.Action == GlobalAction.Back) + if (!queuedMedals.TryDequeue(out currentMedalDisplay)) { - lastAnimation?.Dismiss(); - return true; + Logger.Log("All queued medals have been displayed!"); + Hide(); + return; } - return base.OnPressed(e); + Logger.Log($"Preparing to display \"{currentMedalDisplay.Medal.Name}\""); + LoadComponentAsync(currentMedalDisplay, m => + { + medalContainer.Add(m); + }); } protected override void Dispose(bool isDisposing)