osu/osu.Game/Overlays/MedalOverlay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
3.5 KiB
C#
Raw Normal View History

2024-02-20 11:53:46 +00:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
2024-02-20 11:53:46 +00:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Game.Graphics.Containers;
using osu.Game.Input.Bindings;
using osu.Game.Online.API;
using osu.Game.Online.Notifications.WebSocket;
using osu.Game.Online.Notifications.WebSocket.Events;
using osu.Game.Users;
namespace osu.Game.Overlays
{
public partial class MedalOverlay : OsuFocusedOverlayContainer
{
protected override string? PopInSampleName => null;
protected override string? PopOutSampleName => null;
protected override void PopIn() => this.FadeIn();
protected override void PopOut()
{
showingMedals = false;
this.FadeOut();
}
private readonly Queue<MedalAnimation> queuedMedals = new Queue<MedalAnimation>();
2024-02-20 11:53:46 +00:00
[Resolved]
private IAPIProvider api { get; set; } = null!;
private Container<Drawable> medalContainer = null!;
private bool showingMedals;
[BackgroundDependencyLoader]
private void load()
{
RelativeSizeAxes = Axes.Both;
api.NotificationsClient.MessageReceived += handleMedalMessages;
Add(medalContainer = new Container
{
RelativeSizeAxes = Axes.Both
});
}
private void handleMedalMessages(SocketMessage obj)
{
if (obj.Event != @"new")
return;
var data = obj.Data?.ToObject<NewPrivateNotificationEvent>();
if (data == null || data.Name != @"user_achievement_unlock")
return;
var details = data.Details?.ToObject<UserAchievementUnlock>();
if (details == null)
return;
var medal = new Medal
{
Name = details.Title,
InternalName = details.Slug,
Description = details.Description,
};
Show();
LoadComponentAsync(new MedalAnimation(medal), animation =>
{
queuedMedals.Enqueue(animation);
2024-02-20 11:53:46 +00:00
showingMedals = true;
});
}
protected override void Update()
{
base.Update();
if (!showingMedals || medalContainer.Any())
return;
if (queuedMedals.TryDequeue(out var nextMedal))
medalContainer.Add(nextMedal);
else
2024-02-20 11:53:46 +00:00
Hide();
}
protected override bool OnClick(ClickEvent e)
{
(medalContainer.FirstOrDefault(anim => anim.IsAlive) as MedalAnimation)?.Dismiss();
return true;
}
public override bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Action == GlobalAction.Back)
{
(medalContainer.FirstOrDefault(anim => anim.IsAlive) as MedalAnimation)?.Dismiss();
return true;
}
return base.OnPressed(e);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (api.IsNotNull())
api.NotificationsClient.MessageReceived -= handleMedalMessages;
}
}
}