2019-01-24 16:43:03 +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
|
|
|
|
|
|
|
|
|
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;
|
2018-04-16 05:44:59 +08:00
|
|
|
|
using osu.Framework.Configuration;
|
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
|
|
|
|
|
2018-04-16 05:44:59 +08:00
|
|
|
|
namespace osu.Game.Overlays.Profile.Header
|
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;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public Bindable<User> User = new Bindable<User>();
|
|
|
|
|
|
|
|
|
|
public RankGraph()
|
|
|
|
|
{
|
|
|
|
|
Padding = new MarginPadding { Vertical = padding };
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
placeholder = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Text = "No recent plays",
|
2018-12-23 04:50:25 +08:00
|
|
|
|
TextSize = 12,
|
|
|
|
|
Font = @"Exo2.0-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
|
|
|
|
|
|
|
|
|
User.ValueChanged += userChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void userChanged(User user)
|
|
|
|
|
{
|
|
|
|
|
placeholder.FadeIn(fade_duration, Easing.Out);
|
|
|
|
|
|
|
|
|
|
if (user?.Statistics?.Ranks.Global == null)
|
|
|
|
|
{
|
|
|
|
|
graph.FadeOut(fade_duration, Easing.Out);
|
|
|
|
|
ranks = null;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int[] userRanks = user.RankHistory?.Data ?? new[] { user.Statistics.Ranks.Global.Value };
|
|
|
|
|
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)
|
|
|
|
|
{
|
2018-09-19 19:52:57 +08:00
|
|
|
|
graph.UpdateBallPosition(e.MousePosition.X);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
graph.ShowBall();
|
|
|
|
|
}
|
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)
|
2018-09-19 19:52:57 +08:00
|
|
|
|
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.HideBall();
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2018-12-23 04:50:25 +08:00
|
|
|
|
private readonly Box ballBg;
|
|
|
|
|
private readonly Box movingBar;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
public Action<int> OnBallMove;
|
|
|
|
|
|
|
|
|
|
public RankChartLineGraph()
|
|
|
|
|
{
|
2018-12-23 04:50:25 +08:00
|
|
|
|
Add(movingBar = new Box
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2018-12-23 04:50:25 +08:00
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
|
Width = 1.5f,
|
|
|
|
|
Alpha = 0,
|
2018-04-13 17:19:50 +08:00
|
|
|
|
RelativePositionAxes = Axes.Both,
|
|
|
|
|
});
|
2018-12-23 04:50:25 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
Add(movingBall = new CircularContainer
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.Centre,
|
2018-12-23 04:50:25 +08:00
|
|
|
|
Size = new Vector2(18),
|
2018-04-13 17:19:50 +08:00
|
|
|
|
Alpha = 0,
|
|
|
|
|
Masking = true,
|
2018-12-23 04:50:25 +08:00
|
|
|
|
BorderThickness = 4,
|
2018-04-13 17:19:50 +08:00
|
|
|
|
RelativePositionAxes = Axes.Both,
|
2018-12-23 04:50:25 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
ballBg.Colour = colours.CommunityUserGrayGreenDarkest;
|
|
|
|
|
movingBall.BorderColour = colours.Yellow;
|
|
|
|
|
movingBar.Colour = colours.Yellow;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
public void UpdateBallPosition(float mouseXPosition)
|
|
|
|
|
{
|
|
|
|
|
int index = calculateIndex(mouseXPosition);
|
|
|
|
|
movingBall.Position = calculateBallPosition(index);
|
2018-12-23 04:50:25 +08:00
|
|
|
|
movingBar.X = movingBall.X;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
OnBallMove.Invoke(index);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-23 04:50:25 +08:00
|
|
|
|
public void ShowBall()
|
|
|
|
|
{
|
|
|
|
|
movingBall.FadeIn(fade_duration);
|
|
|
|
|
movingBar.FadeIn(fade_duration);
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-12-23 04:50:25 +08:00
|
|
|
|
public void HideBall()
|
|
|
|
|
{
|
|
|
|
|
movingBall.FadeOut(fade_duration);
|
|
|
|
|
movingBar.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 => User.Value?.Statistics?.Ranks.Global == null ? "" : $"{ranks[dayIndex].Value:#,##0}|{ranked_days - ranks[dayIndex].Key + 1}";
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
Font = "Exo2.0-Bold",
|
|
|
|
|
TextSize = 12,
|
|
|
|
|
Text = "Global Ranking "
|
|
|
|
|
},
|
|
|
|
|
globalRankingText = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Font = "Exo2.0-Regular",
|
|
|
|
|
TextSize = 12,
|
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
|
|
|
|
|
{
|
|
|
|
|
TextSize = 12,
|
|
|
|
|
Font = "Exo2.0-Regular"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
|
|
|
|
background.Colour = colours.CommunityUserGrayGreenDarker;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|