1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 03:57:25 +08:00
osu-lazer/osu.Game/Overlays/Profile/Header/Components/RankGraph.cs

281 lines
9.4 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;
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",
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
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
}
2019-02-21 17:56:34 +08:00
private void userChanged(ValueChangedEvent<User> e)
2018-04-13 17:19:50 +08:00
{
placeholder.FadeIn(fade_duration, Easing.Out);
2019-02-21 17:56:34 +08:00
if (e.NewValue?.Statistics?.Ranks.Global == null)
2018-04-13 17:19:50 +08:00
{
graph.FadeOut(fade_duration, Easing.Out);
ranks = null;
return;
}
2019-02-21 17:56:34 +08:00
int[] userRanks = e.NewValue.RankHistory?.Data ?? new[] { e.NewValue.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);
2018-04-13 17:19:50 +08:00
graph.ShowBall();
}
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.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
{
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)
{
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
}
}