1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 01:47:24 +08:00
osu-lazer/osu.Game/Overlays/Dashboard/CurrentlyPlayingDisplay.cs

151 lines
5.0 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.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
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.API.Requests.Responses;
using osu.Game.Online.Spectator;
using osu.Game.Screens.OnlinePlay.Match.Components;
using osu.Game.Screens.Play;
using osu.Game.Users;
using osuTK;
namespace osu.Game.Overlays.Dashboard
{
internal class CurrentlyPlayingDisplay : CompositeDrawable
{
private readonly IBindableDictionary<int, SpectatorState> playingUserStates = new BindableDictionary<int, SpectatorState>();
2020-11-02 20:09:47 +08:00
private FillFlowContainer<PlayingUserPanel> userFlow;
[Resolved]
private SpectatorClient spectatorClient { 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),
};
}
2020-11-06 15:38:57 +08:00
[Resolved]
private UserLookupCache users { get; set; }
protected override void LoadComplete()
{
base.LoadComplete();
playingUserStates.BindTo(spectatorClient.PlayingUserStates);
playingUserStates.BindCollectionChanged(onUsersChanged, true);
2020-11-09 11:22:54 +08:00
}
private void onUsersChanged(object sender, NotifyDictionaryChangedEventArgs<int, SpectatorState> e) => Schedule(() =>
2020-11-09 11:22:54 +08:00
{
switch (e.Action)
{
case NotifyDictionaryChangedAction.Add:
Debug.Assert(e.NewItems != null);
foreach ((int userId, _) in e.NewItems)
2020-11-09 11:22:54 +08:00
{
users.GetUserAsync(userId).ContinueWith(task =>
{
2022-01-06 21:54:43 +08:00
var user = task.GetResultSafely();
if (user == null) return;
2020-11-09 11:22:54 +08:00
Schedule(() =>
{
2020-11-09 11:22:54 +08:00
// user may no longer be playing.
if (!playingUserStates.ContainsKey(user.Id))
2020-11-06 16:55:29 +08:00
return;
userFlow.Add(createUserPanel(user));
2020-11-06 15:38:57 +08:00
});
2020-11-09 11:22:54 +08:00
});
}
2020-11-09 11:22:54 +08:00
break;
case NotifyDictionaryChangedAction.Remove:
Debug.Assert(e.OldItems != null);
foreach ((int userId, _) in e.OldItems)
userFlow.FirstOrDefault(card => card.User.Id == userId)?.Expire();
2020-11-09 11:22:54 +08:00
break;
}
});
private PlayingUserPanel createUserPanel(APIUser user) =>
2020-11-02 20:09:47 +08:00
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 APIUser User;
2020-11-02 20:09:47 +08:00
[Resolved(canBeNull: true)]
private OsuGame game { get; set; }
public PlayingUserPanel(APIUser user)
2020-11-02 20:09:47 +08:00
{
User = user;
AutoSizeAxes = Axes.Both;
}
2020-11-02 20:09:47 +08:00
[BackgroundDependencyLoader]
private void load(IAPIProvider api)
{
2020-11-02 20:09:47 +08:00
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)
2020-11-02 20:09:47 +08:00
{
RelativeSizeAxes = Axes.X,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
2020-11-02 20:09:47 +08:00
},
new PurpleTriangleButton
2020-11-02 20:09:47 +08:00
{
RelativeSizeAxes = Axes.X,
2020-11-02 20:09:47 +08:00
Text = "Watch",
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
2021-04-01 21:08:52 +08:00
Action = () => game?.PerformFromScreen(s => s.Push(new SoloSpectator(User))),
Enabled = { Value = User.Id != api.LocalUser.Value.Id }
2020-11-02 20:09:47 +08:00
}
}
},
};
}
}
}
}