1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 17:22:54 +08:00
osu-lazer/osu.Game/Overlays/Profile/Header/BottomHeaderContainer.cs

171 lines
6.5 KiB
C#
Raw Normal View History

2019-01-28 06:45:00 +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;
using System.Linq;
using osu.Framework.Allocation;
2019-03-10 06:58:14 +08:00
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.Graphics.Sprites;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Users;
using osuTK;
using osuTK.Graphics;
2020-01-31 03:51:33 +08:00
using static osu.Framework.Graphics.Containers.TextFlowContainer;
namespace osu.Game.Overlays.Profile.Header
{
2019-03-10 06:58:14 +08:00
public class BottomHeaderContainer : CompositeDrawable
{
2019-04-25 17:42:19 +08:00
public readonly Bindable<User> User = new Bindable<User>();
private LinkFlowContainer topLinkContainer;
private LinkFlowContainer bottomLinkContainer;
2019-04-25 17:42:19 +08:00
private Color4 iconColour;
2019-04-25 17:42:19 +08:00
public BottomHeaderContainer()
{
2019-03-10 06:58:14 +08:00
AutoSizeAxes = Axes.Y;
2019-04-25 17:42:19 +08:00
}
2019-03-10 06:58:14 +08:00
2019-04-25 17:42:19 +08:00
[BackgroundDependencyLoader]
2020-01-30 05:10:19 +08:00
private void load(OverlayColourProvider colourProvider)
2019-04-25 17:42:19 +08:00
{
2020-01-30 05:10:19 +08:00
iconColour = colourProvider.Foreground1;
2019-04-25 19:05:59 +08:00
2019-03-10 06:58:14 +08:00
InternalChildren = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
2020-01-30 05:10:19 +08:00
Colour = colourProvider.Background4
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Padding = new MarginPadding { Horizontal = UserProfileOverlay.CONTENT_X_MARGIN, Vertical = 10 },
Spacing = new Vector2(0, 10),
Children = new Drawable[]
{
2019-04-25 17:42:19 +08:00
topLinkContainer = new LinkFlowContainer(text => text.Font = text.Font.With(size: 12))
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
},
2019-04-25 17:05:46 +08:00
bottomLinkContainer = new LinkFlowContainer(text => text.Font = text.Font.With(size: 12))
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
}
}
}
};
2019-04-25 17:42:19 +08:00
User.BindValueChanged(user => updateDisplay(user.NewValue));
}
2019-03-10 06:58:14 +08:00
private void updateDisplay(User user)
{
2019-04-25 17:42:19 +08:00
topLinkContainer.Clear();
bottomLinkContainer.Clear();
if (user == null) return;
if (user.JoinDate.ToUniversalTime().Year < 2008)
2019-04-25 17:42:19 +08:00
topLinkContainer.AddText("Here since the beginning");
else
{
2019-04-25 17:42:19 +08:00
topLinkContainer.AddText("Joined ");
topLinkContainer.AddText(new DrawableDate(user.JoinDate), embolden);
}
2019-04-25 17:42:19 +08:00
addSpacer(topLinkContainer);
2019-06-19 00:13:21 +08:00
if (user.IsOnline)
{
topLinkContainer.AddText("Currently online");
addSpacer(topLinkContainer);
}
2019-06-19 02:04:36 +08:00
else if (user.LastVisit.HasValue)
{
topLinkContainer.AddText("Last seen ");
topLinkContainer.AddText(new DrawableDate(user.LastVisit.Value), embolden);
2019-04-25 17:42:19 +08:00
addSpacer(topLinkContainer);
}
if (user.PlayStyles?.Length > 0)
{
topLinkContainer.AddText("Plays with ");
topLinkContainer.AddText(string.Join(", ", user.PlayStyles.Select(style => style.GetDescription())), embolden);
2019-04-25 17:42:19 +08:00
addSpacer(topLinkContainer);
}
2019-04-25 17:42:19 +08:00
topLinkContainer.AddText("Contributed ");
topLinkContainer.AddLink($@"{user.PostCount:#,##0} forum posts", $"https://osu.ppy.sh/users/{user.Id}/posts", creationParameters: embolden);
2020-01-31 00:46:59 +08:00
string websiteWithoutProtocol = user.Website;
2020-01-31 00:46:59 +08:00
if (!string.IsNullOrEmpty(websiteWithoutProtocol))
{
2020-01-31 00:46:59 +08:00
if (Uri.TryCreate(websiteWithoutProtocol, UriKind.Absolute, out var uri))
2019-04-25 17:42:19 +08:00
{
2020-01-31 00:46:59 +08:00
websiteWithoutProtocol = uri.Host + uri.PathAndQuery + uri.Fragment;
websiteWithoutProtocol = websiteWithoutProtocol.TrimEnd('/');
2019-04-25 17:42:19 +08:00
}
}
2019-04-04 06:24:42 +08:00
tryAddInfo(FontAwesome.Solid.MapMarker, user.Location);
tryAddInfo(OsuIcon.Heart, user.Interests);
tryAddInfo(FontAwesome.Solid.Suitcase, user.Occupation);
bottomLinkContainer.NewLine();
if (!string.IsNullOrEmpty(user.Twitter))
2019-04-04 06:24:42 +08:00
tryAddInfo(FontAwesome.Brands.Twitter, "@" + user.Twitter, $@"https://twitter.com/{user.Twitter}");
tryAddInfo(FontAwesome.Brands.Discord, user.Discord);
tryAddInfo(FontAwesome.Brands.Skype, user.Skype, @"skype:" + user.Skype + @"?chat");
tryAddInfo(FontAwesome.Brands.Lastfm, user.Lastfm, $@"https://last.fm/users/{user.Lastfm}");
2020-01-31 00:46:59 +08:00
tryAddInfo(FontAwesome.Solid.Link, websiteWithoutProtocol, user.Website);
2020-01-31 03:51:33 +08:00
// Hide the container to prevent adding unnecessary padding if it has no children other than the NewLineContainer
2020-01-31 04:45:42 +08:00
if (bottomLinkContainer.Children.All(child => child is NewLineContainer))
bottomLinkContainer.Hide();
2020-01-31 03:51:33 +08:00
else
// this is needed if user gets changed without the whole header being reloaded
bottomLinkContainer.Show();
}
2019-04-25 17:42:19 +08:00
private void addSpacer(OsuTextFlowContainer textFlow) => textFlow.AddArbitraryDrawable(new Container { Width = 15 });
private void tryAddInfo(IconUsage icon, string content, string link = null)
{
if (string.IsNullOrEmpty(content)) return;
2019-08-15 13:38:49 +08:00
// newlines could be contained in API returned user content.
content = content.Replace("\n", " ");
2019-04-25 17:42:19 +08:00
bottomLinkContainer.AddIcon(icon, text =>
{
text.Font = text.Font.With(size: 10);
text.Colour = iconColour;
});
if (link != null)
bottomLinkContainer.AddLink(" " + content, link, creationParameters: embolden);
else
bottomLinkContainer.AddText(" " + content, embolden);
addSpacer(bottomLinkContainer);
}
private void embolden(SpriteText text) => text.Font = text.Font.With(weight: FontWeight.Bold);
}
}