1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 09:32:53 +08:00
osu-lazer/osu.Game/Overlays/Dashboard/CurrentlyPlayingDisplay.cs

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

222 lines
7.4 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.
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Collections.Generic;
2022-02-02 22:19:43 +08:00
using System.Collections.Specialized;
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.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Framework.Screens;
2020-11-06 15:38:57 +08:00
using osu.Game.Database;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Spectator;
using osu.Game.Resources.Localisation.Web;
using osu.Game.Screens;
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 const float search_textbox_height = 40;
private const float padding = 10;
2022-02-02 22:19:43 +08:00
private readonly IBindableList<int> playingUsers = new BindableList<int>();
private SearchContainer<PlayingUserPanel> userFlow;
private BasicSearchTextBox searchTextBox;
[Resolved]
private SpectatorClient spectatorClient { get; set; }
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2022-05-27 22:38:54 +08:00
InternalChildren = new Drawable[]
{
2022-05-27 22:38:54 +08:00
new Box
{
RelativeSizeAxes = Axes.X,
Height = padding * 2 + search_textbox_height,
2022-05-27 22:38:54 +08:00
Colour = colourProvider.Background4,
},
new Container<BasicSearchTextBox>
{
2022-05-27 22:38:54 +08:00
RelativeSizeAxes = Axes.X,
Padding = new MarginPadding(padding),
Child = searchTextBox = new BasicSearchTextBox
2022-05-27 22:38:54 +08:00
{
RelativeSizeAxes = Axes.X,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Height = search_textbox_height,
ReleaseFocusOnCommit = false,
HoldFocus = true,
PlaceholderText = HomeStrings.SearchPlaceholder,
2022-05-27 22:38:54 +08:00
},
},
2022-05-27 22:38:54 +08:00
userFlow = new SearchContainer<PlayingUserPanel>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(10),
2022-05-27 22:38:54 +08:00
Padding = new MarginPadding
{
Top = padding * 3 + search_textbox_height,
Bottom = padding,
Right = padding,
Left = padding,
2022-05-27 22:38:54 +08:00
},
},
};
searchTextBox.Current.ValueChanged += text => userFlow.SearchTerm = text.NewValue;
}
2020-11-06 15:38:57 +08:00
[Resolved]
private UserLookupCache users { get; set; }
protected override void LoadComplete()
{
base.LoadComplete();
2022-02-02 22:19:43 +08:00
playingUsers.BindTo(spectatorClient.PlayingUsers);
playingUsers.BindCollectionChanged(onPlayingUsersChanged, true);
2020-11-09 11:22:54 +08:00
}
protected override void OnFocus(FocusEvent e)
{
base.OnFocus(e);
searchTextBox.TakeFocus();
}
2022-02-02 22:19:43 +08:00
private void onPlayingUsersChanged(object sender, NotifyCollectionChangedEventArgs e) => Schedule(() =>
2020-11-09 11:22:54 +08:00
{
switch (e.Action)
{
2022-02-02 22:19:43 +08:00
case NotifyCollectionChangedAction.Add:
Debug.Assert(e.NewItems != null);
2022-02-02 22:19:43 +08:00
foreach (int userId in e.NewItems)
2022-05-27 22:38:54 +08:00
{
users.GetUserAsync(userId).ContinueWith(task =>
{
var user = task.GetResultSafely();
if (user == null)
return;
Schedule(() =>
{
// user may no longer be playing.
if (!playingUsers.Contains(user.Id))
return;
userFlow.Add(createUserPanel(user));
});
});
}
2020-11-09 11:22:54 +08:00
break;
2022-02-02 22:19:43 +08:00
case NotifyCollectionChangedAction.Remove:
Debug.Assert(e.OldItems != null);
2022-02-02 22:19:43 +08:00
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
2022-05-27 22:38:54 +08:00
public class PlayingUserPanel : CompositeDrawable, IFilterable
2020-11-02 20:09:47 +08:00
{
public readonly APIUser User;
public IEnumerable<LocalisableString> FilterTerms { get; }
2020-11-02 20:09:47 +08:00
[Resolved(canBeNull: true)]
private IPerformFromScreenRunner performer { get; set; }
2022-05-27 22:38:54 +08:00
public bool FilteringActive { set; get; }
public bool MatchingFilter
{
set
{
if (value)
Show();
else
Hide();
}
}
public PlayingUserPanel(APIUser user)
2020-11-02 20:09:47 +08:00
{
User = user;
2022-05-27 22:38:54 +08:00
FilterTerms = new LocalisableString[] { User.Username };
2020-11-02 20:09:47 +08:00
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,
Text = "Spectate",
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Action = () => performer?.PerformFromScreen(s => s.Push(new SoloSpectator(User))),
Enabled = { Value = User.Id != api.LocalUser.Value.Id }
2020-11-02 20:09:47 +08:00
}
}
},
};
}
}
}
}