1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 06:37:43 +08:00
osu-lazer/osu.Game/Overlays/Profile/RankGraph.cs

218 lines
7.6 KiB
C#
Raw Normal View History

2017-06-15 00:48:01 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Linq;
2017-06-15 02:17:48 +08:00
using OpenTK;
2017-06-15 00:48:01 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2017-06-21 16:03:47 +08:00
using osu.Framework.Graphics.Shapes;
2017-06-15 00:48:01 +08:00
using osu.Framework.Graphics.Sprites;
2017-06-15 02:17:48 +08:00
using osu.Framework.Input;
2017-06-15 00:48:01 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
2017-06-16 16:36:23 +08:00
using osu.Game.Users;
2017-11-21 15:15:16 +08:00
using System.Collections.Generic;
2017-11-21 18:33:34 +08:00
using osu.Framework.Configuration;
2017-06-15 00:48:01 +08:00
2017-06-16 16:36:23 +08:00
namespace osu.Game.Overlays.Profile
2017-06-15 00:48:01 +08:00
{
2017-12-22 18:25:18 +08:00
public class RankGraph : Container
2017-06-15 00:48:01 +08:00
{
2017-11-21 15:15:16 +08:00
private const float primary_textsize = 25;
private const float secondary_textsize = 13;
private const float padding = 10;
private const float fade_duration = 150;
2017-11-21 15:36:53 +08:00
private const int ranked_days = 88;
2017-11-21 15:15:16 +08:00
2017-06-15 02:17:48 +08:00
private readonly SpriteText rankText, performanceText, relativeText;
private readonly RankChartLineGraph graph;
2017-11-21 15:15:16 +08:00
private readonly OsuSpriteText placeholder;
2017-06-15 00:48:01 +08:00
2017-11-21 15:15:16 +08:00
private KeyValuePair<int, int>[] ranks;
2017-11-21 18:33:34 +08:00
public Bindable<User> User = new Bindable<User>();
2017-06-15 16:57:34 +08:00
2017-12-22 18:25:18 +08:00
public RankGraph()
2017-06-15 00:48:01 +08:00
{
2017-06-15 16:57:34 +08:00
Padding = new MarginPadding { Vertical = padding };
2017-06-15 00:48:01 +08:00
Children = new Drawable[]
{
2017-11-21 15:15:16 +08:00
placeholder = new OsuSpriteText
{
2017-12-22 18:25:18 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2017-11-21 15:15:16 +08:00
Text = "No recent plays",
TextSize = 14,
Font = @"Exo2.0-RegularItalic",
},
2017-06-15 02:17:48 +08:00
rankText = new OsuSpriteText
2017-06-15 00:48:01 +08:00
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Font = @"Exo2.0-RegularItalic",
2017-06-15 16:57:34 +08:00
TextSize = primary_textsize
2017-06-15 00:48:01 +08:00
},
2017-06-15 02:17:48 +08:00
relativeText = new OsuSpriteText
2017-06-15 00:48:01 +08:00
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Font = @"Exo2.0-RegularItalic",
Y = 25,
2017-06-15 16:57:34 +08:00
TextSize = secondary_textsize
2017-06-15 00:48:01 +08:00
},
2017-06-15 02:17:48 +08:00
performanceText = new OsuSpriteText
2017-06-15 00:48:01 +08:00
{
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
Font = @"Exo2.0-RegularItalic",
2017-06-15 16:57:34 +08:00
TextSize = secondary_textsize
2017-06-15 00:48:01 +08:00
},
2017-11-21 15:15:16 +08:00
graph = new RankChartLineGraph
2017-06-15 00:48:01 +08:00
{
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X,
2017-12-22 18:25:18 +08:00
Height = 75,
2017-06-15 16:57:34 +08:00
Y = -secondary_textsize,
2017-11-21 15:15:16 +08:00
Alpha = 0,
}
};
2017-06-26 15:26:50 +08:00
2017-11-21 15:15:16 +08:00
graph.OnBallMove += showHistoryRankTexts;
2017-11-21 18:33:34 +08:00
User.ValueChanged += userChanged;
2017-06-15 00:48:01 +08:00
}
2017-11-21 15:15:16 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
2017-06-15 07:58:20 +08:00
{
2017-11-21 15:15:16 +08:00
graph.Colour = colours.Yellow;
2017-06-15 07:58:20 +08:00
}
2017-12-22 18:25:59 +08:00
private void userChanged(User user)
2017-06-15 07:58:20 +08:00
{
2017-12-22 18:25:18 +08:00
placeholder.FadeIn(fade_duration, Easing.Out);
2017-11-21 15:15:16 +08:00
2017-12-22 18:25:59 +08:00
if (user == null)
2017-11-21 15:15:16 +08:00
{
rankText.Text = string.Empty;
performanceText.Text = string.Empty;
relativeText.Text = string.Empty;
graph.FadeOut(fade_duration, Easing.Out);
ranks = null;
return;
}
2017-12-22 18:25:59 +08:00
int[] userRanks = user.RankHistory?.Data ?? new[] { user.Statistics.Rank };
2017-11-21 15:30:12 +08:00
ranks = userRanks.Select((x, index) => new KeyValuePair<int, int>(index, x)).Where(x => x.Value != 0).ToArray();
2017-11-21 15:15:16 +08:00
if (ranks.Length > 1)
2017-06-16 14:47:14 +08:00
{
2017-12-22 18:25:18 +08:00
placeholder.FadeOut(fade_duration, Easing.Out);
2017-11-21 15:15:16 +08:00
graph.DefaultValueCount = ranks.Length;
graph.Values = ranks.Select(x => -(float)Math.Log(x.Value));
2017-11-07 07:39:48 +08:00
graph.SetStaticBallPosition();
2017-06-16 14:47:14 +08:00
}
2017-11-21 18:33:34 +08:00
graph.FadeTo(ranks.Length > 1 ? 1 : 0, fade_duration, Easing.Out);
updateRankTexts();
2017-06-15 00:48:01 +08:00
}
2017-11-21 15:15:16 +08:00
private void updateRankTexts()
{
2017-11-21 18:33:34 +08:00
rankText.Text = User.Value.Statistics.Rank > 0 ? $"#{User.Value.Statistics.Rank:#,0}" : "no rank";
performanceText.Text = User.Value.Statistics.PP != null ? $"{User.Value.Statistics.PP:#,0}pp" : string.Empty;
relativeText.Text = $"{User.Value.Country?.FullName} #{User.Value.CountryRank:#,0}";
2017-11-21 15:15:16 +08:00
}
private void showHistoryRankTexts(int dayIndex)
{
rankText.Text = $"#{ranks[dayIndex].Value:#,0}";
2017-11-21 15:36:53 +08:00
relativeText.Text = dayIndex + 1 == ranks.Length ? "Now" : $"{ranked_days - ranks[dayIndex].Key} days ago";
2017-11-21 15:15:16 +08:00
}
2017-11-07 07:39:48 +08:00
protected override bool OnHover(InputState state)
{
2017-11-21 18:42:42 +08:00
if (ranks?.Length > 1)
2017-11-21 15:15:16 +08:00
{
graph.UpdateBallPosition(state.Mouse.Position.X);
graph.ShowBall();
}
2017-11-07 07:39:48 +08:00
return base.OnHover(state);
}
protected override bool OnMouseMove(InputState state)
{
2017-11-21 18:42:42 +08:00
if (ranks?.Length > 1)
2017-11-21 15:15:16 +08:00
graph.UpdateBallPosition(state.Mouse.Position.X);
2017-11-07 07:39:48 +08:00
return base.OnMouseMove(state);
}
protected override void OnHoverLost(InputState state)
{
2017-11-21 18:42:42 +08:00
if (ranks?.Length > 1)
{
graph.HideBall();
updateRankTexts();
}
2017-11-21 15:15:16 +08:00
2017-11-07 07:39:48 +08:00
base.OnHoverLost(state);
}
2017-06-15 02:17:48 +08:00
private class RankChartLineGraph : LineGraph
{
2017-11-07 07:39:48 +08:00
private readonly CircularContainer staticBall;
private readonly CircularContainer movingBall;
2017-06-15 02:17:48 +08:00
2017-11-07 07:39:48 +08:00
public Action<int> OnBallMove;
2017-06-15 02:17:48 +08:00
public RankChartLineGraph()
{
2017-11-07 07:39:48 +08:00
Add(staticBall = new CircularContainer
2017-06-15 02:17:48 +08:00
{
2017-11-07 07:39:48 +08:00
Origin = Anchor.Centre,
2017-06-15 02:17:48 +08:00
Size = new Vector2(8),
Masking = true,
2017-11-07 07:39:48 +08:00
RelativePositionAxes = Axes.Both,
Child = new Box { RelativeSizeAxes = Axes.Both }
});
Add(movingBall = new CircularContainer
{
2017-06-15 02:17:48 +08:00
Origin = Anchor.Centre,
2017-11-07 07:39:48 +08:00
Size = new Vector2(8),
2017-06-15 02:17:48 +08:00
Alpha = 0,
2017-11-07 07:39:48 +08:00
Masking = true,
2017-06-15 02:17:48 +08:00
RelativePositionAxes = Axes.Both,
2017-11-07 07:39:48 +08:00
Child = new Box { RelativeSizeAxes = Axes.Both }
2017-06-15 02:17:48 +08:00
});
}
public void SetStaticBallPosition() => staticBall.Position = new Vector2(1, GetYPosition(Values.Last()));
2017-06-15 02:17:48 +08:00
2017-11-07 18:43:02 +08:00
public void UpdateBallPosition(float mouseXPosition)
2017-06-15 02:17:48 +08:00
{
2017-11-07 07:39:48 +08:00
int index = calculateIndex(mouseXPosition);
2017-11-07 08:05:12 +08:00
movingBall.Position = calculateBallPosition(index);
2017-11-07 07:39:48 +08:00
OnBallMove.Invoke(index);
}
2017-11-07 18:43:02 +08:00
public void ShowBall() => movingBall.FadeIn(fade_duration);
2017-11-07 07:39:48 +08:00
2017-11-07 18:43:02 +08:00
public void HideBall() => movingBall.FadeOut(fade_duration);
2017-06-15 02:17:48 +08:00
2017-11-07 07:39:48 +08:00
private int calculateIndex(float mouseXPosition) => (int)Math.Round(mouseXPosition / DrawWidth * (DefaultValueCount - 1));
2017-11-07 08:05:12 +08:00
private Vector2 calculateBallPosition(int index)
2017-06-15 02:17:48 +08:00
{
2017-11-07 07:39:48 +08:00
float y = GetYPosition(Values.ElementAt(index));
return new Vector2(index / (float)(DefaultValueCount - 1), y);
2017-06-15 02:17:48 +08:00
}
}
2017-06-15 00:48:01 +08:00
}
}