Rank Line Graph improvements

This commit is contained in:
EVAST9919
2017-11-07 02:39:48 +03:00
parent 4f52000f5f
commit d10dcd82bc

View File

@ -24,6 +24,7 @@ namespace osu.Game.Overlays.Profile
private readonly RankChartLineGraph graph; private readonly RankChartLineGraph graph;
private readonly int[] ranks; private readonly int[] ranks;
private readonly int rankedDays;
private const float primary_textsize = 25, secondary_textsize = 13, padding = 10; private const float primary_textsize = 25, secondary_textsize = 13, padding = 10;
@ -33,6 +34,10 @@ namespace osu.Game.Overlays.Profile
{ {
this.user = user; this.user = user;
int[] userRanks = user.RankHistory?.Data ?? new[] { user.Statistics.Rank };
ranks = userRanks.SkipWhile(x => x == 0).ToArray();
rankedDays = ranks.Length;
Padding = new MarginPadding { Vertical = padding }; Padding = new MarginPadding { Vertical = padding };
Children = new Drawable[] Children = new Drawable[]
{ {
@ -64,13 +69,12 @@ namespace osu.Game.Overlays.Profile
Origin = Anchor.BottomCentre, Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Y = -secondary_textsize, Y = -secondary_textsize,
DefaultValueCount = 90, DefaultValueCount = rankedDays,
BallRelease = updateRankTexts,
BallMove = showHistoryRankTexts
} }
}; };
ranks = user.RankHistory?.Data ?? new[] { user.Statistics.Rank }; graph.OnBallMove += showHistoryRankTexts;
graph.OnReset += updateRankTexts;
} }
private void updateRankTexts() private void updateRankTexts()
@ -82,8 +86,8 @@ namespace osu.Game.Overlays.Profile
private void showHistoryRankTexts(int dayIndex) private void showHistoryRankTexts(int dayIndex)
{ {
rankText.Text = ranks[dayIndex] > 0 ? $"#{ranks[dayIndex]:#,0}" : "no rank"; rankText.Text = $"#{ranks[dayIndex]:#,0}";
relativeText.Text = dayIndex == ranks.Length ? "Now" : $"{ranks.Length - dayIndex} days ago"; relativeText.Text = dayIndex == rankedDays ? "Now" : $"{rankedDays - dayIndex} days ago";
//plural should be handled in a general way //plural should be handled in a general way
} }
@ -96,7 +100,8 @@ namespace osu.Game.Overlays.Profile
{ {
// use logarithmic coordinates // use logarithmic coordinates
graph.Values = ranks.Select(x => -(float)Math.Log(x)); graph.Values = ranks.Select(x => -(float)Math.Log(x));
graph.ResetBall(); graph.SetStaticBallPosition();
updateRankTexts();
} }
} }
@ -110,67 +115,90 @@ namespace osu.Game.Overlays.Profile
return base.Invalidate(invalidation, source, shallPropagate); return base.Invalidate(invalidation, source, shallPropagate);
} }
protected override bool OnHover(InputState state)
{
graph.ShowBall(ToLocalSpace(state.Mouse.NativeState.Position).X);
return base.OnHover(state);
}
protected override bool OnMouseMove(InputState state)
{
graph.MoveBall(ToLocalSpace(state.Mouse.NativeState.Position).X);
return base.OnMouseMove(state);
}
protected override void OnHoverLost(InputState state)
{
graph.HideBall();
updateRankTexts();
base.OnHoverLost(state);
}
private class RankChartLineGraph : LineGraph private class RankChartLineGraph : LineGraph
{ {
private readonly CircularContainer ball; private const double fade_duration = 300;
private bool ballShown; private const double move_duration = 100;
private const double transform_duration = 100; private readonly CircularContainer staticBall;
private readonly CircularContainer movingBall;
public Action<int> BallMove; public Action<int> OnBallMove;
public Action BallRelease; public Action OnReset;
public RankChartLineGraph() public RankChartLineGraph()
{ {
Add(ball = new CircularContainer Add(staticBall = new CircularContainer
{ {
Origin = Anchor.Centre,
Size = new Vector2(8), Size = new Vector2(8),
Masking = true, Masking = true,
Origin = Anchor.Centre,
Alpha = 0,
RelativePositionAxes = Axes.Both, RelativePositionAxes = Axes.Both,
Children = new Drawable[] Child = new Box { RelativeSizeAxes = Axes.Both }
{ });
new Box { RelativeSizeAxes = Axes.Both } Add(movingBall = new CircularContainer
} {
Origin = Anchor.Centre,
Size = new Vector2(8),
Alpha = 0,
Masking = true,
RelativePositionAxes = Axes.Both,
Child = new Box { RelativeSizeAxes = Axes.Both }
}); });
} }
public void ResetBall() public void SetStaticBallPosition()
{ {
ball.MoveTo(new Vector2(1, GetYPosition(Values.Last())), ballShown ? transform_duration : 0, Easing.OutQuint); staticBall.Position = new Vector2(1, GetYPosition(Values.Last()));
ball.Show(); OnReset.Invoke();
BallRelease();
ballShown = true;
} }
protected override bool OnMouseMove(InputState state) public void ShowBall(float mouseXPosition)
{ {
if (ballShown) int index = calculateIndex(mouseXPosition);
{ movingBall.Position = calculateBallPosition(mouseXPosition, index);
var values = (IList<float>)Values; movingBall.FadeIn(fade_duration);
var position = ToLocalSpace(state.Mouse.NativeState.Position); OnBallMove.Invoke(index);
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;
float y = GetYPosition(values[i]);
if (Math.Abs(y * DrawHeight - position.Y) <= 8f)
{
ball.MoveTo(new Vector2(index / (float)(count - 1), y), transform_duration, Easing.OutQuint);
BallMove(i);
}
}
}
return base.OnMouseMove(state);
} }
protected override void OnHoverLost(InputState state) public void MoveBall(float mouseXPosition)
{ {
if (ballShown) int index = calculateIndex(mouseXPosition);
ResetBall(); movingBall.MoveTo(calculateBallPosition(mouseXPosition, index), move_duration, Easing.OutQuint);
base.OnHoverLost(state); OnBallMove.Invoke(index);
}
public void HideBall()
{
movingBall.FadeOut(fade_duration);
OnReset.Invoke();
}
private int calculateIndex(float mouseXPosition) => (int)Math.Round(mouseXPosition / DrawWidth * (DefaultValueCount - 1));
private Vector2 calculateBallPosition(float mouseXPosition, int index)
{
float y = GetYPosition(Values.ElementAt(index));
return new Vector2(index / (float)(DefaultValueCount - 1), y);
} }
} }
} }