1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 11:12:57 +08:00
osu-lazer/osu.Game/Overlays/SocialOverlay.cs

214 lines
6.7 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.
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
using System.Linq;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Overlays.SearchableList;
using osu.Game.Overlays.Social;
using osu.Game.Users;
using osu.Framework.Threading;
namespace osu.Game.Overlays
{
2019-05-14 13:18:06 +08:00
public class SocialOverlay : SearchableListOverlay<SocialTab, SocialSortCriteria, SortDirection>
2018-04-13 17:19:50 +08:00
{
private readonly LoadingAnimation loading;
private FillFlowContainer<SocialPanel> panels;
protected override Color4 BackgroundColour => OsuColour.FromHex(@"60284b");
protected override Color4 TrianglesColourLight => OsuColour.FromHex(@"672b51");
protected override Color4 TrianglesColourDark => OsuColour.FromHex(@"5c2648");
protected override SearchableListHeader<SocialTab> CreateHeader() => new Header();
protected override SearchableListFilterControl<SocialSortCriteria, SortDirection> CreateFilterControl() => new FilterControl();
private IEnumerable<User> users;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public IEnumerable<User> Users
{
get => users;
2018-04-13 17:19:50 +08:00
set
{
if (ReferenceEquals(users, value))
2018-04-13 17:19:50 +08:00
return;
users = value?.ToList();
}
}
public SocialOverlay()
{
Waves.FirstWaveColour = OsuColour.FromHex(@"cb5fa0");
Waves.SecondWaveColour = OsuColour.FromHex(@"b04384");
Waves.ThirdWaveColour = OsuColour.FromHex(@"9b2b6e");
Waves.FourthWaveColour = OsuColour.FromHex(@"6d214d");
2018-04-13 17:19:50 +08:00
Add(loading = new LoadingAnimation());
Filter.Search.Current.ValueChanged += text =>
2018-04-13 17:19:50 +08:00
{
if (!string.IsNullOrEmpty(text.NewValue))
2018-04-13 17:19:50 +08:00
{
// force searching in players until searching for friends is supported
Header.Tabs.Current.Value = SocialTab.AllPlayers;
if (Filter.Tabs.Current.Value != SocialSortCriteria.Rank)
Filter.Tabs.Current.Value = SocialSortCriteria.Rank;
}
};
Header.Tabs.Current.ValueChanged += _ => queueUpdate();
2018-04-13 17:19:50 +08:00
Filter.Tabs.Current.ValueChanged += _ => queueUpdate();
2018-04-13 17:19:50 +08:00
Filter.DisplayStyleControl.DisplayStyle.ValueChanged += style => recreatePanels(style.NewValue);
Filter.DisplayStyleControl.Dropdown.Current.ValueChanged += _ => queueUpdate();
2018-04-13 17:19:50 +08:00
currentQuery.BindTo(Filter.Search.Current);
currentQuery.ValueChanged += query =>
2018-04-13 17:19:50 +08:00
{
queryChangedDebounce?.Cancel();
if (string.IsNullOrEmpty(query.NewValue))
queueUpdate();
2018-04-13 17:19:50 +08:00
else
queryChangedDebounce = Scheduler.AddDelayed(updateSearch, 500);
};
}
2018-04-13 17:19:50 +08:00
private APIRequest getUsersRequest;
private readonly Bindable<string> currentQuery = new Bindable<string>();
private ScheduledDelegate queryChangedDebounce;
private void queueUpdate() => Scheduler.AddOnce(updateSearch);
private void updateSearch()
{
queryChangedDebounce?.Cancel();
if (!IsLoaded)
return;
Users = null;
clearPanels();
loading.Hide();
getUsersRequest?.Cancel();
if (API?.IsLoggedIn != true)
return;
switch (Header.Tabs.Current.Value)
{
case SocialTab.Friends:
var friendRequest = new GetFriendsRequest(); // TODO filter arguments?
friendRequest.Success += updateUsers;
API.Queue(getUsersRequest = friendRequest);
break;
default:
var userRequest = new GetUsersRequest(); // TODO filter arguments!
userRequest.Success += res => updateUsers(res.Users.Select(r => r.User));
API.Queue(getUsersRequest = userRequest);
break;
}
loading.Show();
2018-04-13 17:19:50 +08:00
}
private void recreatePanels(PanelDisplayStyle displayStyle)
{
clearPanels();
if (Users == null)
return;
var newPanels = new FillFlowContainer<SocialPanel>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(10f),
Margin = new MarginPadding { Top = 10 },
ChildrenEnumerable = Users.Select(u =>
{
SocialPanel panel;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
switch (displayStyle)
{
case PanelDisplayStyle.Grid:
panel = new SocialGridPanel(u)
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre
};
break;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
default:
panel = new SocialListPanel(u);
break;
}
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
panel.Status.BindTo(u.Status);
panel.Activity.BindTo(u.Activity);
2018-04-13 17:19:50 +08:00
return panel;
})
};
LoadComponentAsync(newPanels, f =>
{
2019-02-28 12:31:40 +08:00
if (panels != null)
2018-04-13 17:19:50 +08:00
ScrollFlow.Remove(panels);
ScrollFlow.Add(panels = newPanels);
});
}
private void updateUsers(IEnumerable<User> newUsers)
{
Users = newUsers;
loading.Hide();
recreatePanels(Filter.DisplayStyleControl.DisplayStyle.Value);
}
private void clearPanels()
{
if (panels != null)
{
panels.Expire();
panels = null;
}
}
2019-05-14 13:18:06 +08:00
public override void APIStateChanged(IAPIProvider api, APIState state)
2018-04-13 17:19:50 +08:00
{
switch (state)
{
case APIState.Online:
queueUpdate();
2018-04-13 17:19:50 +08:00
break;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
default:
Users = null;
clearPanels();
break;
}
}
}
public enum SortDirection
{
Ascending,
Descending
}
}