1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 06:07:25 +08:00
osu-lazer/osu.Game/Overlays/Profile/Header/Components/RankGraph.cs

103 lines
3.1 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;
2018-04-16 05:44:59 +08:00
using System.Collections.Generic;
2018-04-13 17:19:50 +08:00
using System.Linq;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Users;
2019-04-26 12:49:44 +08:00
namespace osu.Game.Overlays.Profile.Header.Components
2018-04-13 17:19:50 +08:00
{
public class RankGraph : UserGraph<int, int>
2018-04-13 17:19:50 +08:00
{
private const int ranked_days = 88;
public readonly Bindable<UserStatistics> Statistics = new Bindable<UserStatistics>();
2018-04-13 17:19:50 +08:00
private readonly OsuSpriteText placeholder;
2018-04-13 17:19:50 +08:00
public RankGraph()
{
Add(placeholder = new OsuSpriteText
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = "No recent plays",
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Regular)
});
2018-04-13 17:19:50 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2019-08-05 17:49:54 +08:00
Statistics.BindValueChanged(statistics => updateStatistics(statistics.NewValue), true);
}
private void updateStatistics(UserStatistics statistics)
2018-04-13 17:19:50 +08:00
{
placeholder.FadeIn(FADE_DURATION, Easing.Out);
2018-04-13 17:19:50 +08:00
if (statistics?.Ranks.Global == null)
2018-04-13 17:19:50 +08:00
{
Graph.FadeOut(FADE_DURATION, Easing.Out);
Data = null;
2018-04-13 17:19:50 +08:00
return;
}
int[] userRanks = statistics.RankHistory?.Data ?? new[] { statistics.Ranks.Global.Value };
Data = userRanks.Select((x, index) => new KeyValuePair<int, int>(index, x)).Where(x => x.Value != 0).ToArray();
2018-04-13 17:19:50 +08:00
if (Data.Length > 1)
2018-04-13 17:19:50 +08:00
{
placeholder.FadeOut(FADE_DURATION, Easing.Out);
2018-04-13 17:19:50 +08:00
Graph.DefaultValueCount = Data.Length;
Graph.Values = Data.Select(x => -MathF.Log(x.Value));
2018-04-13 17:19:50 +08:00
}
Graph.FadeTo(Data.Length > 1 ? 1 : 0, FADE_DURATION, Easing.Out);
2018-04-13 17:19:50 +08:00
}
protected override object GetTooltipContent()
2018-04-13 17:19:50 +08:00
{
if (Statistics.Value?.Ranks.Global == null)
return null;
2018-04-13 17:19:50 +08:00
var days = ranked_days - Data[DataIndex].Key + 1;
2018-04-13 17:19:50 +08:00
return new TooltipDisplayContent
2018-04-13 17:19:50 +08:00
{
Rank = $"#{Data[DataIndex].Value:#,##0}",
Time = days == 0 ? "now" : $"{days} days ago"
};
2019-08-16 18:47:35 +08:00
}
2018-12-23 04:50:25 +08:00
protected override UserGraphTooltip GetTooltip() => new RankGraphTooltip();
2018-12-23 04:50:25 +08:00
private class RankGraphTooltip : UserGraphTooltip
2018-12-23 04:50:25 +08:00
{
protected override string TooltipCounterName => @"Global Ranking";
2018-12-23 04:50:25 +08:00
public override bool SetContent(object content)
2018-12-23 04:50:25 +08:00
{
2019-08-16 18:47:35 +08:00
if (!(content is TooltipDisplayContent info))
return false;
Counter.Text = info.Rank;
BottomText.Text = info.Time;
2019-08-16 18:47:35 +08:00
return true;
2018-12-23 04:50:25 +08:00
}
}
2019-08-16 18:47:35 +08:00
private class TooltipDisplayContent
{
public string Rank;
public string Time;
}
2018-04-13 17:19:50 +08:00
}
}