1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 23:27:24 +08:00
osu-lazer/osu.Game/Users/Profile/RankChart.cs

178 lines
6.5 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;
2017-06-15 02:17:48 +08:00
using System.Collections.Generic;
2017-06-15 00:48:01 +08:00
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;
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;
namespace osu.Game.Users.Profile
{
public class RankChart : Container
{
2017-06-15 02:17:48 +08:00
private readonly SpriteText rankText, performanceText, relativeText;
private readonly RankChartLineGraph graph;
2017-06-15 00:48:01 +08:00
private int[] ranks;
private decimal?[] performances;
2017-06-15 02:17:48 +08:00
2017-06-15 16:57:34 +08:00
private const float primary_textsize = 25, secondary_textsize = 13, padding = 10;
2017-06-15 02:17:48 +08:00
private readonly User user;
2017-06-15 00:48:01 +08:00
public RankChart(User user)
{
2017-06-15 02:17:48 +08:00
this.user = user;
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-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-06-15 02:17:48 +08:00
graph = new RankChartLineGraph
2017-06-15 00:48:01 +08:00
{
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X,
2017-06-15 16:57:34 +08:00
Y = -secondary_textsize,
2017-06-15 02:17:48 +08:00
DefaultValueCount = 90,
2017-06-15 07:58:20 +08:00
BallRelease = updateRankTexts,
BallMove = showHistoryRankTexts
2017-06-15 00:48:01 +08:00
}
};
ranks = new[] { user.Statistics.Rank };
performances = new decimal?[] { user.Statistics.PP };
2017-06-15 00:48:01 +08:00
}
2017-06-15 07:58:20 +08:00
private void updateRankTexts()
{
rankText.Text = user.Statistics.Rank > 0 ? $"#{user.Statistics.Rank:#,0}" : "no rank";
performanceText.Text = user.Statistics.PP != null ? $"{user.Statistics.PP:#,0}pp" : string.Empty;
//relativeText.Text = $"{user.Country?.FullName} #{countryRank:#,0}";
2017-06-15 07:58:20 +08:00
}
private void showHistoryRankTexts(int dayIndex)
{
rankText.Text = ranks[dayIndex] > 0 ? $"#{ranks[dayIndex]:#,0}" : "no rank";
performanceText.Text = performances[dayIndex] != null ? $"{performances[dayIndex]:#,0}pp" : string.Empty;
//relativeText.Text = dayIndex == ranks.Length ? "Now" : $"{ranks.Length - dayIndex} days ago";
2017-06-15 07:58:20 +08:00
//plural should be handled in a general way
}
2017-06-15 00:48:01 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
graph.Colour = colours.Yellow;
2017-06-16 14:47:14 +08:00
if (user.Statistics.Rank > 0)
{
// use logarithmic coordinates
graph.Values = ranks.Select(x => -(float)Math.Log(x));
graph.ResetBall();
}
2017-06-15 00:48:01 +08:00
}
public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true)
{
if ((invalidation & Invalidation.DrawSize) != 0)
{
2017-06-15 16:57:34 +08:00
graph.Height = DrawHeight - padding * 2 - primary_textsize - secondary_textsize * 2;
2017-06-15 00:48:01 +08:00
}
return base.Invalidate(invalidation, source, shallPropagate);
}
2017-06-15 02:17:48 +08:00
private class RankChartLineGraph : LineGraph
{
private readonly CircularContainer ball;
private bool ballShown;
private const double transform_duration = 100;
public Action<int> BallMove;
public Action BallRelease;
public RankChartLineGraph()
{
Add(ball = new CircularContainer
{
Size = new Vector2(8),
Masking = true,
Origin = Anchor.Centre,
Alpha = 0,
RelativePositionAxes = Axes.Both,
Children = new Drawable[]
{
new Box { RelativeSizeAxes = Axes.Both }
}
});
}
public void ResetBall()
{
2017-06-16 14:47:14 +08:00
ball.MoveTo(new Vector2(1, GetYPosition(Values.Last())), ballShown ? transform_duration : 0, EasingTypes.OutQuint);
2017-06-15 02:17:48 +08:00
ball.Show();
BallRelease();
ballShown = true;
}
protected override bool OnMouseMove(InputState state)
{
if (ballShown)
{
2017-06-15 07:58:20 +08:00
var values = (IList<float>)Values;
2017-06-15 02:17:48 +08:00
var position = ToLocalSpace(state.Mouse.NativeState.Position);
int count = Math.Max(values.Count, DefaultValueCount);
int index = (int)Math.Round(position.X / DrawWidth * (count - 1));
if (index >= count - values.Count)
{
int i = index + values.Count - count;
2017-06-16 14:47:14 +08:00
float y = GetYPosition(values[i]);
2017-06-15 02:17:48 +08:00
if (Math.Abs(y * DrawHeight - position.Y) <= 8f)
{
ball.MoveTo(new Vector2(index / (float)(count - 1), y), transform_duration, EasingTypes.OutQuint);
BallMove(i);
}
}
}
return base.OnMouseMove(state);
}
protected override void OnHoverLost(InputState state)
{
if (ballShown)
ResetBall();
base.OnHoverLost(state);
}
}
2017-06-15 00:48:01 +08:00
}
}