1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 17:57:38 +08:00
osu-lazer/osu.Game/Overlays/Dashboard/Friends/FriendDisplay.cs

262 lines
9.5 KiB
C#
Raw Normal View History

2020-03-16 14:42:21 +08: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;
using System.Linq;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2020-03-16 14:42:21 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
2020-03-16 14:42:21 +08:00
using osu.Game.Users;
using osuTK;
namespace osu.Game.Overlays.Dashboard.Friends
{
public class FriendDisplay : CompositeDrawable
2020-03-16 14:42:21 +08:00
{
private List<APIUser> users = new List<APIUser>();
2020-03-16 14:42:21 +08:00
public List<APIUser> Users
2020-03-16 14:42:21 +08:00
{
get => users;
set
{
users = value;
2020-03-23 09:47:27 +08:00
onlineStreamControl.Populate(value);
2020-03-16 14:42:21 +08:00
}
}
private CancellationTokenSource cancellationToken;
private Drawable currentContent;
2020-04-20 14:25:58 +08:00
private FriendOnlineStreamControl onlineStreamControl;
private Box background;
private Box controlBackground;
private UserListToolbar userListToolbar;
private Container itemsPlaceholder;
private LoadingLayer loading;
2020-03-16 14:42:21 +08:00
private readonly IBindableList<APIUser> apiFriends = new BindableList<APIUser>();
public FriendDisplay()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
}
[BackgroundDependencyLoader(true)]
private void load(OverlayColourProvider colourProvider, IAPIProvider api)
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
InternalChild = new FillFlowContainer
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
new Container
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
controlBackground = new Box
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
RelativeSizeAxes = Axes.Both
2020-03-16 14:42:21 +08:00
},
2020-04-20 17:25:39 +08:00
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
Top = 20,
Horizontal = 45
},
Child = onlineStreamControl = new FriendOnlineStreamControl(),
}
2020-03-16 14:42:21 +08:00
}
2020-04-20 17:25:39 +08:00
},
new Container
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
Name = "User List",
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
2020-04-16 16:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
background = new Box
{
RelativeSizeAxes = Axes.Both
},
new FillFlowContainer
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Margin = new MarginPadding { Bottom = 20 },
Children = new Drawable[]
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
new Container
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
Horizontal = 40,
Vertical = 20
},
Child = userListToolbar = new UserListToolbar
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
}
2020-03-16 14:42:21 +08:00
},
2020-04-20 17:25:39 +08:00
new Container
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
2020-03-16 14:42:21 +08:00
{
2020-04-20 17:25:39 +08:00
itemsPlaceholder = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Horizontal = 50 }
},
loading = new LoadingLayer(true)
2020-04-20 17:25:39 +08:00
}
2020-03-16 14:42:21 +08:00
}
}
}
}
}
}
2020-04-20 14:25:58 +08:00
};
2020-03-16 14:42:21 +08:00
background.Colour = colourProvider.Background4;
controlBackground.Colour = colourProvider.Background5;
apiFriends.BindTo(api.Friends);
apiFriends.BindCollectionChanged((_, __) => Schedule(() => Users = apiFriends.ToList()), true);
2020-03-16 14:42:21 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2020-03-23 09:47:27 +08:00
onlineStreamControl.Current.BindValueChanged(_ => recreatePanels());
2020-03-16 14:42:21 +08:00
userListToolbar.DisplayStyle.BindValueChanged(_ => recreatePanels());
userListToolbar.SortCriteria.BindValueChanged(_ => recreatePanels());
2020-03-22 01:37:21 +08:00
}
2020-03-16 14:42:21 +08:00
private void recreatePanels()
{
2020-03-17 06:53:53 +08:00
if (!users.Any())
2020-03-16 14:42:21 +08:00
return;
cancellationToken?.Cancel();
if (itemsPlaceholder.Any())
loading.Show();
2020-03-23 01:13:54 +08:00
var sortedUsers = sortUsers(getUsersInCurrentGroup());
2020-03-16 14:42:21 +08:00
LoadComponentAsync(createTable(sortedUsers), addContentToPlaceholder, (cancellationToken = new CancellationTokenSource()).Token);
}
private List<APIUser> getUsersInCurrentGroup()
2020-03-23 01:13:54 +08:00
{
2020-03-23 09:47:27 +08:00
switch (onlineStreamControl.Current.Value?.Status)
2020-03-23 01:13:54 +08:00
{
default:
2020-03-23 09:47:27 +08:00
case OnlineStatus.All:
2020-03-23 01:13:54 +08:00
return users;
2020-03-23 09:47:27 +08:00
case OnlineStatus.Offline:
2020-03-23 01:13:54 +08:00
return users.Where(u => !u.IsOnline).ToList();
2020-03-23 09:47:27 +08:00
case OnlineStatus.Online:
2020-03-23 01:13:54 +08:00
return users.Where(u => u.IsOnline).ToList();
}
}
2020-03-16 14:42:21 +08:00
private void addContentToPlaceholder(Drawable content)
{
loading.Hide();
var lastContent = currentContent;
if (lastContent != null)
{
lastContent.FadeOut(100, Easing.OutQuint).Expire();
lastContent.Delay(25).Schedule(() => lastContent.BypassAutoSizeAxes = Axes.Y);
}
itemsPlaceholder.Add(currentContent = content);
currentContent.FadeIn(200, Easing.OutQuint);
}
private FillFlowContainer createTable(List<APIUser> users)
2020-03-16 14:42:21 +08:00
{
var style = userListToolbar.DisplayStyle.Value;
return new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(style == OverlayPanelDisplayStyle.Card ? 10 : 2),
Children = users.Select(u => createUserPanel(u, style)).ToList()
};
}
private UserPanel createUserPanel(APIUser user, OverlayPanelDisplayStyle style)
2020-03-16 14:42:21 +08:00
{
switch (style)
{
default:
case OverlayPanelDisplayStyle.Card:
return new UserGridPanel(user).With(panel =>
{
panel.Anchor = Anchor.TopCentre;
panel.Origin = Anchor.TopCentre;
panel.Width = 290;
});
case OverlayPanelDisplayStyle.List:
return new UserListPanel(user);
2020-07-19 01:24:38 +08:00
case OverlayPanelDisplayStyle.Brick:
return new UserBrickPanel(user);
2020-03-16 14:42:21 +08:00
}
}
private List<APIUser> sortUsers(List<APIUser> unsorted)
2020-03-16 14:42:21 +08:00
{
switch (userListToolbar.SortCriteria.Value)
{
default:
case UserSortCriteria.LastVisit:
2020-03-17 06:56:10 +08:00
return unsorted.OrderByDescending(u => u.LastVisit).ToList();
2020-03-16 14:42:21 +08:00
case UserSortCriteria.Rank:
return unsorted.OrderByDescending(u => u.Statistics.GlobalRank.HasValue).ThenBy(u => u.Statistics.GlobalRank ?? 0).ToList();
2020-03-16 14:42:21 +08:00
case UserSortCriteria.Username:
return unsorted.OrderBy(u => u.Username).ToList();
}
}
protected override void Dispose(bool isDisposing)
{
cancellationToken?.Cancel();
base.Dispose(isDisposing);
}
}
}