1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-24 16:13:05 +08:00
osu-lazer/osu.Game/Overlays/Profile/Header/Components/RankGraph.cs

289 lines
9.9 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;
using osu.Framework.Allocation;
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.Framework.Graphics.Containers;
2018-12-23 04:50:25 +08:00
using osu.Framework.Graphics.Cursor;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Shapes;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Users;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
2019-04-26 12:49:44 +08:00
namespace osu.Game.Overlays.Profile.Header.Components
2018-04-13 17:19:50 +08:00
{
2018-12-23 04:50:25 +08:00
public class RankGraph : Container, IHasCustomTooltip
2018-04-13 17:19:50 +08:00
{
private const float secondary_textsize = 13;
private const float padding = 10;
private const float fade_duration = 150;
private const int ranked_days = 88;
private readonly RankChartLineGraph graph;
private readonly OsuSpriteText placeholder;
private KeyValuePair<int, int>[] ranks;
2018-12-23 04:50:25 +08:00
private int dayIndex;
public readonly Bindable<UserStatistics> Statistics = new Bindable<UserStatistics>();
2018-04-13 17:19:50 +08:00
public RankGraph()
{
Padding = new MarginPadding { Vertical = padding };
Children = new Drawable[]
{
placeholder = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = "No recent plays",
2019-04-25 17:05:46 +08:00
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Regular)
2018-04-13 17:19:50 +08:00
},
graph = new RankChartLineGraph
{
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
2018-12-23 04:50:25 +08:00
RelativeSizeAxes = Axes.Both,
2018-04-13 17:19:50 +08:00
Y = -secondary_textsize,
Alpha = 0,
}
};
2018-12-23 04:50:25 +08:00
graph.OnBallMove += i => dayIndex = i;
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2018-12-23 04:50:25 +08:00
graph.LineColour = colours.Yellow;
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);
if (statistics?.Ranks.Global == null)
2018-04-13 17:19:50 +08:00
{
graph.FadeOut(fade_duration, Easing.Out);
ranks = null;
return;
}
int[] userRanks = statistics.RankHistory?.Data ?? new[] { statistics.Ranks.Global.Value };
2018-04-13 17:19:50 +08:00
ranks = userRanks.Select((x, index) => new KeyValuePair<int, int>(index, x)).Where(x => x.Value != 0).ToArray();
if (ranks.Length > 1)
{
placeholder.FadeOut(fade_duration, Easing.Out);
graph.DefaultValueCount = ranks.Length;
graph.Values = ranks.Select(x => -(float)Math.Log(x.Value));
}
graph.FadeTo(ranks.Length > 1 ? 1 : 0, fade_duration, Easing.Out);
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
if (ranks?.Length > 1)
{
graph.UpdateBallPosition(e.MousePosition.X);
graph.ShowBar();
2018-04-13 17:19:50 +08:00
}
2019-02-28 12:31:40 +08:00
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 bool OnMouseMove(MouseMoveEvent e)
2018-04-13 17:19:50 +08:00
{
if (ranks?.Length > 1)
graph.UpdateBallPosition(e.MousePosition.X);
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
return base.OnMouseMove(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
{
if (ranks?.Length > 1)
{
graph.HideBar();
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
private class RankChartLineGraph : LineGraph
{
private readonly CircularContainer movingBall;
private readonly Container bar;
2018-12-23 04:50:25 +08:00
private readonly Box ballBg;
private readonly Box line;
2018-04-13 17:19:50 +08:00
public Action<int> OnBallMove;
public RankChartLineGraph()
{
Add(bar = new Container
2018-04-13 17:19:50 +08:00
{
2018-12-23 04:50:25 +08:00
Origin = Anchor.TopCentre,
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
2018-12-23 04:50:25 +08:00
Alpha = 0,
2018-04-13 17:19:50 +08:00
RelativePositionAxes = Axes.Both,
Children = new Drawable[]
{
line = new Box
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Y,
Width = 1.5f,
},
movingBall = new CircularContainer
{
Anchor = Anchor.TopCentre,
Origin = Anchor.Centre,
Size = new Vector2(18),
Masking = true,
BorderThickness = 4,
RelativePositionAxes = Axes.Y,
Child = ballBg = new Box { RelativeSizeAxes = Axes.Both }
}
}
2018-04-13 17:19:50 +08:00
});
}
2018-12-23 04:50:25 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-05-20 14:58:40 +08:00
ballBg.Colour = colours.GreySeafoamDarker;
movingBall.BorderColour = line.Colour = colours.Yellow;
2018-12-23 04:50:25 +08:00
}
2018-04-13 17:19:50 +08:00
public void UpdateBallPosition(float mouseXPosition)
{
2019-05-25 01:01:47 +08:00
const int duration = 200;
2018-04-13 17:19:50 +08:00
int index = calculateIndex(mouseXPosition);
Vector2 position = calculateBallPosition(index);
movingBall.MoveToY(position.Y, duration, Easing.OutQuint);
bar.MoveToX(position.X, duration, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
OnBallMove.Invoke(index);
}
public void ShowBar() => bar.FadeIn(fade_duration);
2018-04-13 17:19:50 +08:00
public void HideBar() => bar.FadeOut(fade_duration);
2018-04-13 17:19:50 +08:00
private int calculateIndex(float mouseXPosition) => (int)Math.Round(mouseXPosition / DrawWidth * (DefaultValueCount - 1));
private Vector2 calculateBallPosition(int index)
{
float y = GetYPosition(Values.ElementAt(index));
return new Vector2(index / (float)(DefaultValueCount - 1), y);
}
}
2018-12-23 04:50:25 +08:00
public string TooltipText => Statistics.Value?.Ranks.Global == null ? "" : $"#{ranks[dayIndex].Value:#,##0}|{ranked_days - ranks[dayIndex].Key + 1}";
2018-12-23 04:50:25 +08:00
2018-12-23 05:50:19 +08:00
public ITooltip GetCustomTooltip() => new RankGraphTooltip();
2018-12-23 04:50:25 +08:00
public class RankGraphTooltip : VisibilityContainer, ITooltip
{
private readonly OsuSpriteText globalRankingText, timeText;
private readonly Box background;
public string TooltipText { get; set; }
2018-12-23 05:50:19 +08:00
public RankGraphTooltip()
2018-12-23 04:50:25 +08:00
{
AutoSizeAxes = Axes.Both;
Masking = true;
CornerRadius = 10;
Children = new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Padding = new MarginPadding(10),
Children = new Drawable[]
{
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
new OsuSpriteText
{
2019-04-25 17:05:46 +08:00
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
2018-12-23 04:50:25 +08:00
Text = "Global Ranking "
},
globalRankingText = new OsuSpriteText
{
2019-04-25 17:05:46 +08:00
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Regular),
2018-12-25 08:09:49 +08:00
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
2018-12-23 04:50:25 +08:00
}
}
},
timeText = new OsuSpriteText
{
2019-04-25 17:05:46 +08:00
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Regular),
2018-12-23 04:50:25 +08:00
}
}
}
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-05-20 14:58:40 +08:00
background.Colour = colours.GreySeafoamDark;
2018-12-23 04:50:25 +08:00
}
public void Refresh()
{
var info = TooltipText.Split('|');
globalRankingText.Text = info[0];
timeText.Text = info[1] == "0" ? "now" : $"{info[1]} days ago";
}
private bool instantMove = true;
public void Move(Vector2 pos)
{
if (instantMove)
{
Position = pos;
instantMove = false;
}
else
this.MoveTo(pos, 200, Easing.OutQuint);
}
protected override void PopIn() => this.FadeIn(200, Easing.OutQuint);
protected override void PopOut() => this.FadeOut(200, Easing.OutQuint);
}
2018-04-13 17:19:50 +08:00
}
}