1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 10:27:35 +08:00
osu-lazer/osu.Game/Overlays/Dashboard/CurrentlyPlayingDisplay.cs

139 lines
4.6 KiB
C#
Raw Normal View History

// 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.Specialized;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Screens;
2020-11-06 15:38:57 +08:00
using osu.Game.Database;
using osu.Game.Online.API;
using osu.Game.Online.Spectator;
2020-11-02 20:09:47 +08:00
using osu.Game.Screens.Multi.Match.Components;
using osu.Game.Screens.Play;
using osu.Game.Users;
using osuTK;
namespace osu.Game.Overlays.Dashboard
{
internal class CurrentlyPlayingDisplay : CompositeDrawable
{
private readonly IBindableList<int> playingUsers = new BindableList<int>();
2020-11-02 20:09:47 +08:00
private FillFlowContainer<PlayingUserPanel> userFlow;
[Resolved]
private SpectatorStreamingClient spectatorStreaming { get; set; }
[BackgroundDependencyLoader]
private void load()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2020-11-02 20:09:47 +08:00
InternalChild = userFlow = new FillFlowContainer<PlayingUserPanel>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2020-11-06 12:08:07 +08:00
Padding = new MarginPadding(10),
Spacing = new Vector2(10),
};
}
[Resolved]
private IAPIProvider api { get; set; }
2020-11-06 15:38:57 +08:00
[Resolved]
private UserLookupCache users { get; set; }
protected override void LoadComplete()
{
base.LoadComplete();
playingUsers.BindTo(spectatorStreaming.PlayingUsers);
playingUsers.BindCollectionChanged((sender, e) => Schedule(() =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
2020-11-06 15:38:57 +08:00
foreach (var id in e.NewItems.OfType<int>().ToArray())
{
2020-11-06 15:38:57 +08:00
users.GetUser(id).ContinueWith(u =>
{
2020-11-06 15:38:57 +08:00
Schedule(() =>
{
if (playingUsers.Contains(u.Result.Id))
userFlow.Add(createUserPanel(u.Result));
});
});
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (var u in e.OldItems.OfType<int>())
userFlow.FirstOrDefault(card => card.User.Id == u)?.Expire();
break;
case NotifyCollectionChangedAction.Reset:
userFlow.Clear();
break;
}
}), true);
}
2020-11-02 20:09:47 +08:00
private PlayingUserPanel createUserPanel(User user) =>
new PlayingUserPanel(user).With(panel =>
{
panel.Anchor = Anchor.TopCentre;
panel.Origin = Anchor.TopCentre;
});
2020-11-02 20:09:47 +08:00
private class PlayingUserPanel : CompositeDrawable
{
public readonly User User;
[Resolved(canBeNull: true)]
private OsuGame game { get; set; }
public PlayingUserPanel(User user)
{
User = user;
AutoSizeAxes = Axes.Both;
InternalChildren = new Drawable[]
{
new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
2020-11-02 20:09:47 +08:00
Direction = FillDirection.Vertical,
Spacing = new Vector2(2),
Width = 290,
2020-11-02 20:09:47 +08:00
Children = new Drawable[]
{
new UserGridPanel(user)
{
RelativeSizeAxes = Axes.X,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
2020-11-02 20:09:47 +08:00
},
new PurpleTriangleButton
{
RelativeSizeAxes = Axes.X,
2020-11-02 20:09:47 +08:00
Text = "Watch",
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
2020-11-02 20:09:47 +08:00
Action = () => game?.PerformFromScreen(s => s.Push(new Spectator(user)))
}
}
},
};
}
}
}
}