1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 05:57:29 +08:00
osu-lazer/osu.Game/Online/Leaderboards/LeaderboardScore.cs

436 lines
19 KiB
C#
Raw Normal View History

2022-02-20 03:47:30 +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.
2018-04-13 17:19:50 +08:00
2022-02-17 06:18:14 +08:00
using System;
using System.Collections.Generic;
2018-11-28 15:33:42 +08:00
using System.Linq;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-06-04 09:26:30 +08:00
using osu.Framework.Graphics.Cursor;
2019-04-02 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Framework.Platform;
using osu.Game.Database;
using osu.Game.Extensions;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Select;
2018-11-28 15:12:57 +08:00
using osu.Game.Scoring;
using osu.Game.Users.Drawables;
using osuTK;
using osuTK.Graphics;
using osu.Game.Online.API;
2020-12-19 22:02:56 +08:00
using osu.Game.Utils;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Online.Leaderboards
2018-04-13 17:19:50 +08:00
{
public class LeaderboardScore : OsuClickableContainer, IHasContextMenu, IHasCustomTooltip<ScoreInfo>
{
2018-12-22 14:20:35 +08:00
public const float HEIGHT = 60;
public readonly ScoreInfo Score;
2018-04-13 17:19:50 +08:00
private const float corner_radius = 5;
private const float edge_margin = 5;
private const float background_alpha = 0.25f;
private const float rank_width = 35;
2018-04-13 17:19:50 +08:00
protected Container RankContainer { get; private set; }
2020-02-20 13:51:25 +08:00
private readonly int? rank;
private readonly bool isOnlineScope;
2018-04-13 17:19:50 +08:00
private Box background;
private Container content;
private Drawable avatar;
private Drawable scoreRank;
2018-04-13 17:19:50 +08:00
private OsuSpriteText nameLabel;
public GlowingSpriteText ScoreText { get; private set; }
2022-02-17 17:12:35 +08:00
private FillFlowContainer flagBadgeAndDateContainer;
2018-04-13 17:19:50 +08:00
private FillFlowContainer<ModIcon> modsContainer;
private List<ScoreComponentLabel> statisticsLabels;
2020-02-13 18:04:23 +08:00
[Resolved(CanBeNull = true)]
private DialogOverlay dialogOverlay { get; set; }
[Resolved(CanBeNull = true)]
private SongSelect songSelect { get; set; }
[Resolved]
private Storage storage { get; set; }
public ITooltip<ScoreInfo> GetCustomTooltip() => new LeaderboardScoreTooltip();
public virtual ScoreInfo TooltipContent => Score;
public LeaderboardScore(ScoreInfo score, int? rank, bool isOnlineScope = true)
2018-04-13 17:19:50 +08:00
{
2021-09-07 16:45:21 +08:00
Score = score;
this.rank = rank;
this.isOnlineScope = isOnlineScope;
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.X;
2018-12-22 14:20:35 +08:00
Height = HEIGHT;
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
2020-08-28 18:16:46 +08:00
private void load(IAPIProvider api, OsuColour colour, ScoreManager scoreManager)
2018-04-13 17:19:50 +08:00
{
var user = Score.User;
statisticsLabels = GetStatistics(Score).Select(s => new ScoreComponentLabel(s)).ToList();
2020-12-26 20:54:10 +08:00
ClickableAvatar innerAvatar;
2019-03-17 12:43:23 +08:00
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
new RankLabel(rank)
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Y,
Width = rank_width,
},
content = new Container
{
RelativeSizeAxes = Axes.Both,
2022-02-17 17:12:35 +08:00
Padding = new MarginPadding { Left = rank_width },
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Both,
CornerRadius = corner_radius,
Masking = true,
Children = new[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = user.OnlineID == api.LocalUser.Value.Id && isOnlineScope ? colour.Green : Color4.Black,
2018-04-13 17:19:50 +08:00
Alpha = background_alpha,
},
},
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding(edge_margin),
Children = new[]
{
avatar = new DelayedLoadWrapper(
2020-12-26 20:54:10 +08:00
innerAvatar = new ClickableAvatar(user)
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
CornerRadius = corner_radius,
Masking = true,
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Radius = 1,
Colour = Color4.Black.Opacity(0.2f),
},
})
{
RelativeSizeAxes = Axes.None,
2018-12-22 14:20:35 +08:00
Size = new Vector2(HEIGHT - edge_margin * 2, HEIGHT - edge_margin * 2),
2018-04-13 17:19:50 +08:00
},
new Container
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
2018-12-22 14:20:35 +08:00
Position = new Vector2(HEIGHT - edge_margin, 0f),
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
nameLabel = new OsuSpriteText
{
Text = user.Username,
Font = OsuFont.GetFont(size: 23, weight: FontWeight.Bold, italics: true)
2018-04-13 17:19:50 +08:00
},
new FillFlowContainer
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
2018-04-13 17:19:50 +08:00
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(10f, 0f),
Children = new Drawable[]
{
2022-02-17 17:12:35 +08:00
flagBadgeAndDateContainer = new FillFlowContainer
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
RelativeSizeAxes = Axes.Y,
2022-02-17 17:12:35 +08:00
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5f, 0f),
Width = 87f,
2018-04-13 17:19:50 +08:00
Masking = true,
Children = new Drawable[]
{
new UpdateableFlag(user.Country)
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Size = new Vector2(30f, 20f),
2018-04-13 17:19:50 +08:00
},
2022-02-17 17:12:35 +08:00
new DateLabel(Score.Date)
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
2022-02-17 17:12:35 +08:00
},
2018-04-13 17:19:50 +08:00
},
},
new FillFlowContainer
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
2018-04-13 17:19:50 +08:00
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
2018-06-04 09:22:44 +08:00
Margin = new MarginPadding { Left = edge_margin },
Children = statisticsLabels
2018-04-13 17:19:50 +08:00
},
},
},
},
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5f, 0f),
Children = new Drawable[]
{
ScoreText = new GlowingSpriteText
{
TextColour = Color4.White,
GlowColour = Color4Extensions.FromHex(@"83ccfa"),
Current = scoreManager.GetBindableTotalScoreString(Score),
Font = OsuFont.Numeric.With(size: 23),
},
RankContainer = new Container
2018-04-13 17:19:50 +08:00
{
Size = new Vector2(40f, 20f),
Children = new[]
{
scoreRank = new UpdateableRank(Score.Rank)
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(40f)
2018-04-13 17:19:50 +08:00
},
},
},
},
},
modsContainer = new FillFlowContainer<ModIcon>
{
Anchor = Anchor.BottomRight,
Origin = Anchor.BottomRight,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
ChildrenEnumerable = Score.Mods.Select(mod => new ModIcon(mod) { Scale = new Vector2(0.375f) })
2018-04-13 17:19:50 +08:00
},
},
},
},
},
};
2019-03-17 12:43:23 +08:00
innerAvatar.OnLoadComplete += d => d.FadeInFromZero(200);
2018-04-13 17:19:50 +08:00
}
public override void Show()
{
2022-02-17 17:12:35 +08:00
foreach (var d in new[] { avatar, nameLabel, ScoreText, scoreRank, flagBadgeAndDateContainer, modsContainer }.Concat(statisticsLabels))
2018-04-13 17:19:50 +08:00
d.FadeOut();
Alpha = 0;
content.MoveToY(75);
avatar.MoveToX(75);
nameLabel.MoveToX(150);
this.FadeIn(200);
content.MoveToY(0, 800, Easing.OutQuint);
2021-07-05 23:52:39 +08:00
using (BeginDelayedSequence(100))
2018-04-13 17:19:50 +08:00
{
avatar.FadeIn(300, Easing.OutQuint);
nameLabel.FadeIn(350, Easing.OutQuint);
avatar.MoveToX(0, 300, Easing.OutQuint);
nameLabel.MoveToX(0, 350, Easing.OutQuint);
2021-07-05 23:52:39 +08:00
using (BeginDelayedSequence(250))
2018-04-13 17:19:50 +08:00
{
ScoreText.FadeIn(200);
2018-04-13 17:19:50 +08:00
scoreRank.FadeIn(200);
2021-07-05 23:52:39 +08:00
using (BeginDelayedSequence(50))
2018-04-13 17:19:50 +08:00
{
2022-02-17 17:12:35 +08:00
var drawables = new Drawable[] { flagBadgeAndDateContainer, modsContainer }.Concat(statisticsLabels).ToArray();
2018-04-13 17:19:50 +08:00
for (int i = 0; i < drawables.Length; i++)
drawables[i].FadeIn(100 + i * 50);
}
}
}
}
2018-12-22 14:23:32 +08:00
protected virtual IEnumerable<LeaderboardScoreStatistic> GetStatistics(ScoreInfo model) => new[]
{
2019-04-02 18:55:24 +08:00
new LeaderboardScoreStatistic(FontAwesome.Solid.Link, "Max Combo", model.MaxCombo.ToString()),
new LeaderboardScoreStatistic(FontAwesome.Solid.Crosshairs, "Accuracy", model.DisplayAccuracy)
2018-12-22 14:23:32 +08:00
};
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
background.FadeTo(0.5f, 300, Easing.OutQuint);
2018-10-02 11:02:47 +08:00
return base.OnHover(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
background.FadeTo(background_alpha, 200, Easing.OutQuint);
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
2018-06-04 09:26:30 +08:00
private class ScoreComponentLabel : Container, IHasTooltip
2018-04-13 17:19:50 +08:00
{
2018-06-04 09:22:44 +08:00
private const float icon_size = 20;
private readonly FillFlowContainer content;
2018-06-04 09:26:30 +08:00
public override bool Contains(Vector2 screenSpacePos) => content.Contains(screenSpacePos);
2018-06-04 09:22:44 +08:00
public LocalisableString TooltipText { get; }
2018-06-04 09:26:30 +08:00
2018-12-22 14:20:35 +08:00
public ScoreComponentLabel(LeaderboardScoreStatistic statistic)
2018-04-13 17:19:50 +08:00
{
2019-11-12 17:45:42 +08:00
TooltipText = statistic.Name;
AutoSizeAxes = Axes.Both;
2018-04-13 17:19:50 +08:00
2018-06-04 09:26:30 +08:00
Child = content = new FillFlowContainer
2018-04-13 17:19:50 +08:00
{
2018-06-04 09:22:44 +08:00
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Padding = new MarginPadding { Right = 10 },
2018-06-04 09:22:44 +08:00
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
2018-06-04 09:22:44 +08:00
new Container
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Both,
Children = new[]
{
new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(icon_size),
Rotation = 45,
Colour = Color4Extensions.FromHex(@"3087ac"),
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.Square,
2018-06-04 09:22:44 +08:00
Shadow = true,
},
new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(icon_size - 6),
Colour = Color4Extensions.FromHex(@"a4edff"),
2018-12-22 14:20:35 +08:00
Icon = statistic.Icon,
2018-06-04 09:22:44 +08:00
},
},
},
new GlowingSpriteText
2018-06-04 09:22:44 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
TextColour = Color4.White,
GlowColour = Color4Extensions.FromHex(@"83ccfa"),
Text = statistic.Value,
Font = OsuFont.GetFont(size: 17, weight: FontWeight.Bold),
2018-06-04 09:22:44 +08:00
},
2018-04-13 17:19:50 +08:00
},
};
}
}
private class RankLabel : Container, IHasTooltip
{
public RankLabel(int? rank)
{
if (rank >= 1000)
TooltipText = $"#{rank:N0}";
Child = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 20, italics: true),
2020-12-19 22:02:56 +08:00
Text = rank == null ? "-" : rank.Value.FormatRank()
};
}
public LocalisableString TooltipText { get; }
2018-04-13 17:19:50 +08:00
}
2018-12-22 14:20:35 +08:00
2022-02-17 06:18:14 +08:00
private class DateLabel : DrawableDate
{
public DateLabel(DateTimeOffset date)
2022-02-17 17:12:35 +08:00
: base(date)
2022-02-17 06:18:14 +08:00
{
2022-02-17 17:12:35 +08:00
Font = OsuFont.GetFont(size: 17, weight: FontWeight.Bold, italics: true);
2022-02-17 06:18:14 +08:00
}
protected override string Format() => Date.ToShortRelativeTime(TimeSpan.FromSeconds(30));
2022-02-17 06:18:14 +08:00
}
2018-12-22 14:20:35 +08:00
public class LeaderboardScoreStatistic
{
public IconUsage Icon;
2021-07-24 04:37:08 +08:00
public LocalisableString Value;
2018-12-22 14:20:35 +08:00
public string Name;
2021-07-24 04:37:08 +08:00
public LeaderboardScoreStatistic(IconUsage icon, string name, LocalisableString value)
2018-12-22 14:20:35 +08:00
{
Icon = icon;
Name = name;
Value = value;
}
}
public MenuItem[] ContextMenuItems
{
2019-12-19 14:41:07 +08:00
get
{
2019-12-19 14:41:07 +08:00
List<MenuItem> items = new List<MenuItem>();
if (Score.Mods.Length > 0 && modsContainer.Any(s => s.IsHovered) && songSelect != null)
items.Add(new OsuMenuItem("Use these mods", MenuItemType.Highlighted, () => songSelect.Mods.Value = Score.Mods));
if (Score.Files.Count > 0)
items.Add(new OsuMenuItem("Export", MenuItemType.Standard, () => new LegacyScoreExporter(storage).Export(Score)));
2021-04-02 12:20:15 +08:00
if (!isOnlineScope)
items.Add(new OsuMenuItem("Delete", MenuItemType.Destructive, () => dialogOverlay?.Push(new LocalScoreDeleteDialog(Score))));
2019-12-19 14:41:07 +08:00
return items.ToArray();
}
}
2018-04-13 17:19:50 +08:00
}
}