osu/osu.Game/Screens/Select/Details/UserTopScoreContainer.cs

100 lines
3.4 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2019-07-14 09:34:12 +00:00
// See the LICENCE file in the repository root for full licence text.
2019-07-14 09:40:54 +00:00
using osu.Framework.Bindables;
2019-07-14 09:34:12 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Leaderboards;
using osu.Game.Scoring;
using System;
2019-07-22 14:21:07 +00:00
using System.Threading;
2019-07-14 09:34:12 +00:00
namespace osu.Game.Screens.Select.Details
{
public class UserTopScoreContainer : VisibilityContainer
{
private const int height = 90;
private const int duration = 800;
2019-07-14 09:34:12 +00:00
private readonly Container contentContainer;
private readonly Container scoreContainer;
public Bindable<APILegacyUserTopScoreInfo> Score = new Bindable<APILegacyUserTopScoreInfo>();
2019-07-14 09:34:12 +00:00
public Action<ScoreInfo> ScoreSelected;
2019-07-14 09:34:12 +00:00
protected override bool StartHidden => true;
public UserTopScoreContainer()
{
RelativeSizeAxes = Axes.X;
Height = height;
2019-07-22 14:13:48 +00:00
Anchor = Anchor.BottomLeft;
Origin = Anchor.BottomLeft;
2019-07-14 09:34:12 +00:00
Children = new Drawable[]
{
contentContainer = new Container
{
2019-07-22 14:13:48 +00:00
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.Both,
2019-07-14 09:34:12 +00:00
Children = new Drawable[]
{
new OsuSpriteText
{
2019-07-22 14:26:11 +00:00
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Margin = new MarginPadding { Top = 5 },
2019-07-14 09:34:12 +00:00
Text = @"your personal best".ToUpper(),
Font = OsuFont.GetFont(size: 15, weight: FontWeight.Bold),
},
scoreContainer = new Container
{
2019-07-22 14:13:48 +00:00
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
2019-07-14 09:34:12 +00:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
}
}
}
};
2019-07-14 09:40:54 +00:00
2019-07-22 14:13:48 +00:00
Score.BindValueChanged(onScoreChanged);
2019-07-14 09:40:54 +00:00
}
2019-07-22 14:21:07 +00:00
private CancellationTokenSource loadScoreCancellation;
2019-07-22 14:13:48 +00:00
private void onScoreChanged(ValueChangedEvent<APILegacyUserTopScoreInfo> score)
2019-07-14 09:40:54 +00:00
{
2019-07-22 14:13:48 +00:00
var newScore = score.NewValue;
if (newScore == null)
{
2019-07-14 13:37:05 +00:00
Hide();
2019-07-22 14:13:48 +00:00
return;
}
2019-07-22 14:21:07 +00:00
scoreContainer.Clear();
loadScoreCancellation?.Cancel();
LoadComponentAsync(new LeaderboardScore(newScore.Score, newScore.Position)
2019-07-22 14:13:48 +00:00
{
Action = () => ScoreSelected?.Invoke(newScore.Score)
2019-07-22 14:21:07 +00:00
}, drawableScore =>
{
scoreContainer.Child = drawableScore;
Show();
}, (loadScoreCancellation = new CancellationTokenSource()).Token);
2019-07-14 09:34:12 +00:00
}
protected override void PopIn() => this.ResizeHeightTo(height, duration / 4f, Easing.OutQuint).OnComplete(_ => contentContainer.FadeIn(duration, Easing.OutQuint));
2019-07-14 09:34:12 +00:00
protected override void PopOut() => contentContainer.FadeOut(duration, Easing.OutQuint).OnComplete(_ => this.ResizeHeightTo(0));
2019-07-14 09:34:12 +00:00
}
}