osu/osu.Game/Overlays/UserProfileOverlay.cs

216 lines
7.1 KiB
C#
Raw Normal View History

2020-01-29 20:37:51 +00: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.
2018-04-13 09:19:50 +00:00
2019-11-28 13:41:29 +00:00
using System;
2018-04-13 09:19:50 +00:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
2019-06-21 22:11:04 +00:00
using osu.Framework.Graphics.Containers;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
2018-04-13 09:19:50 +00:00
using osu.Game.Graphics.Containers;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
2018-04-13 09:19:50 +00:00
using osu.Game.Overlays.Profile;
using osu.Game.Overlays.Profile.Sections;
using osu.Game.Users;
2018-11-20 07:51:59 +00:00
using osuTK;
2021-01-18 07:48:12 +00:00
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Overlays
{
public class UserProfileOverlay : FullscreenOverlay<ProfileHeader>
2018-04-13 09:19:50 +00:00
{
private ProfileSection lastSection;
private ProfileSection[] sections;
private GetUserRequest userReq;
2019-06-21 22:11:04 +00:00
private ProfileSectionsContainer sectionsContainer;
private ProfileSectionTabControl tabs;
2018-04-13 09:19:50 +00:00
2018-12-22 15:51:24 +00:00
public const float CONTENT_X_MARGIN = 70;
2018-04-13 09:19:50 +00:00
2020-01-24 09:24:35 +00:00
public UserProfileOverlay()
2021-01-18 07:48:12 +00:00
: base(OverlayColourScheme.Pink)
2020-01-24 09:24:35 +00:00
{
}
2021-01-18 07:48:12 +00:00
protected override ProfileHeader CreateHeader() => new ProfileHeader();
protected override Color4 BackgroundColour => ColourProvider.Background6;
2021-01-18 07:48:12 +00:00
2021-11-05 04:52:42 +00:00
public void ShowUser(IUser user)
2018-04-13 09:19:50 +00:00
{
if (user == APIUser.SYSTEM_USER)
return;
Show();
if (user.OnlineID == Header?.User.Value?.Id)
return;
2020-09-03 08:11:34 +00:00
if (sectionsContainer != null)
sectionsContainer.ExpandableHeader = null;
2018-04-13 09:19:50 +00:00
userReq?.Cancel();
Clear();
lastSection = null;
2019-09-27 06:46:11 +00:00
sections = !user.IsBot
? new ProfileSection[]
2019-09-27 06:32:46 +00:00
{
//new AboutSection(),
new RecentSection(),
new RanksSection(),
//new MedalsSection(),
new HistoricalSection(),
new BeatmapsSection(),
new KudosuSection()
2019-09-27 06:46:11 +00:00
}
2019-11-28 13:41:29 +00:00
: Array.Empty<ProfileSection>();
tabs = new ProfileSectionTabControl
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.X,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
};
2019-06-21 22:11:04 +00:00
Add(sectionsContainer = new ProfileSectionsContainer
2018-04-13 09:19:50 +00:00
{
ExpandableHeader = Header,
2018-04-13 09:19:50 +00:00
FixedHeader = tabs,
HeaderBackground = new Box
{
2020-01-29 20:37:51 +00:00
// this is only visible as the ProfileTabControl background
2020-01-30 03:50:15 +00:00
Colour = ColourProvider.Background5,
2018-04-13 09:19:50 +00:00
RelativeSizeAxes = Axes.Both
},
2018-04-13 09:19:50 +00:00
});
sectionsContainer.SelectedSection.ValueChanged += section =>
2018-04-13 09:19:50 +00:00
{
if (lastSection != section.NewValue)
2018-04-13 09:19:50 +00:00
{
lastSection = section.NewValue;
2018-04-13 09:19:50 +00:00
tabs.Current.Value = lastSection;
}
};
tabs.Current.ValueChanged += section =>
2018-04-13 09:19:50 +00:00
{
if (lastSection == null)
{
lastSection = sectionsContainer.Children.FirstOrDefault();
if (lastSection != null)
tabs.Current.Value = lastSection;
return;
}
2019-02-28 04:31:40 +00:00
if (lastSection != section.NewValue)
2018-04-13 09:19:50 +00:00
{
lastSection = section.NewValue;
2018-04-13 09:19:50 +00:00
sectionsContainer.ScrollTo(lastSection);
}
};
2021-11-05 04:52:42 +00:00
sectionsContainer.ScrollToTop();
// Check arbitrarily whether this user has already been populated.
// This is only generally used by tests, but should be quite safe unless we want to force a refresh on loading a previous user in the future.
if (user is APIUser apiUser && apiUser.JoinDate != default)
2018-04-13 09:19:50 +00:00
{
userReq = null;
2021-11-05 04:52:42 +00:00
userLoadComplete(apiUser);
return;
2018-04-13 09:19:50 +00:00
}
2021-11-05 04:52:42 +00:00
userReq = user.OnlineID > 1 ? new GetUserRequest(user.OnlineID) : new GetUserRequest(user.Username);
userReq.Success += userLoadComplete;
API.Queue(userReq);
2018-04-13 09:19:50 +00:00
}
private void userLoadComplete(APIUser user)
2018-04-13 09:19:50 +00:00
{
2019-03-09 22:58:14 +00:00
Header.User.Value = user;
2018-04-13 09:19:50 +00:00
if (user.ProfileOrder != null)
{
foreach (string id in user.ProfileOrder)
{
var sec = sections.FirstOrDefault(s => s.Identifier == id);
2019-04-01 03:16:05 +00:00
2018-04-13 09:19:50 +00:00
if (sec != null)
{
sec.User.Value = user;
sectionsContainer.Add(sec);
tabs.AddItem(sec);
}
}
}
}
private class ProfileSectionTabControl : OverlayTabControl<ProfileSection>
2018-04-13 09:19:50 +00:00
{
private const float bar_height = 2;
public ProfileSectionTabControl()
2018-04-13 09:19:50 +00:00
{
TabContainer.RelativeSizeAxes &= ~Axes.X;
TabContainer.AutoSizeAxes |= Axes.X;
TabContainer.Anchor |= Anchor.x1;
TabContainer.Origin |= Anchor.x1;
2020-03-26 15:44:46 +00:00
Height = 36 + bar_height;
BarHeight = bar_height;
2018-04-13 09:19:50 +00:00
}
protected override TabItem<ProfileSection> CreateTabItem(ProfileSection value) => new ProfileSectionTabItem(value)
{
AccentColour = AccentColour,
};
2018-04-13 09:19:50 +00:00
[BackgroundDependencyLoader]
2020-01-29 20:35:17 +00:00
private void load(OverlayColourProvider colourProvider)
{
2020-01-29 20:35:17 +00:00
AccentColour = colourProvider.Highlight1;
}
2018-04-13 09:19:50 +00:00
protected override bool OnClick(ClickEvent e) => true;
protected override bool OnHover(HoverEvent e) => true;
private class ProfileSectionTabItem : OverlayTabItem
2018-04-13 09:19:50 +00:00
{
public ProfileSectionTabItem(ProfileSection value)
2019-02-28 04:31:40 +00:00
: base(value)
2018-04-13 09:19:50 +00:00
{
Text.Text = value.Title;
2020-03-26 15:44:46 +00:00
Text.Font = Text.Font.With(size: 16);
Text.Margin = new MarginPadding { Bottom = 10 + bar_height };
Bar.ExpandedSize = 10;
Bar.Margin = new MarginPadding { Bottom = bar_height };
2018-04-13 09:19:50 +00:00
}
}
}
2019-06-21 22:11:04 +00:00
private class ProfileSectionsContainer : SectionsContainer<ProfileSection>
{
public ProfileSectionsContainer()
2018-04-13 09:19:50 +00:00
{
2019-06-21 22:11:04 +00:00
RelativeSizeAxes = Axes.Both;
2018-04-13 09:19:50 +00:00
}
2019-06-21 22:11:04 +00:00
protected override UserTrackingScrollContainer CreateScrollContainer() => new OverlayScrollContainer();
2019-06-23 14:08:18 +00:00
protected override FlowContainer<ProfileSection> CreateScrollContentContainer() => new FillFlowContainer<ProfileSection>
2019-06-21 22:11:04 +00:00
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Spacing = new Vector2(0, 20),
};
2018-04-13 09:19:50 +00:00
}
}
}