Skip to content

Commit

Permalink
Refactor MedalOverlay to be more readable
Browse files Browse the repository at this point in the history
Shouldn't really have any functionality changes, just fixing some old
code that I can't easily parse these days.
  • Loading branch information
peppy committed Nov 26, 2024
1 parent 8585327 commit 33e2ae2
Showing 1 changed file with 48 additions and 26 deletions.
74 changes: 48 additions & 26 deletions osu.Game/Overlays/MedalOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public partial class MedalOverlay : OsuFocusedOverlayContainer
private IAPIProvider api { get; set; } = null!;

private Container<Drawable> medalContainer = null!;
private MedalAnimation? lastAnimation;
private MedalAnimation? currentMedalDisplay;

[BackgroundDependencyLoader]
private void load()
Expand All @@ -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)
Expand Down Expand Up @@ -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<GlobalAction> 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<GlobalAction> 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)
Expand Down

0 comments on commit 33e2ae2

Please sign in to comment.