2020-12-15 14:27:26 +08:00
|
|
|
// 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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2020-12-18 16:07:38 +08:00
|
|
|
using System;
|
2020-12-15 14:27:26 +08:00
|
|
|
using System.Linq;
|
2020-12-28 20:00:05 +08:00
|
|
|
using JetBrains.Annotations;
|
2021-02-19 14:09:41 +08:00
|
|
|
using osu.Framework.Bindables;
|
2020-12-24 20:49:38 +08:00
|
|
|
using osu.Framework.Caching;
|
2021-08-12 15:17:14 +08:00
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2020-12-15 14:27:26 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-08-12 15:17:14 +08:00
|
|
|
using osu.Framework.Graphics.Colour;
|
2020-12-15 14:27:26 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-08-12 15:17:14 +08:00
|
|
|
using osu.Game.Graphics.Containers;
|
2021-11-04 17:02:44 +08:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2020-12-15 14:27:26 +08:00
|
|
|
using osuTK;
|
2021-08-12 15:17:14 +08:00
|
|
|
using osuTK.Graphics;
|
2020-12-15 14:27:26 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play.HUD
|
|
|
|
{
|
2021-08-12 15:17:14 +08:00
|
|
|
public class GameplayLeaderboard : CompositeDrawable
|
2020-12-15 14:27:26 +08:00
|
|
|
{
|
2021-08-12 15:17:14 +08:00
|
|
|
private readonly int maxPanels;
|
2020-12-24 20:49:38 +08:00
|
|
|
private readonly Cached sorting = new Cached();
|
|
|
|
|
2021-02-19 14:09:41 +08:00
|
|
|
public Bindable<bool> Expanded = new Bindable<bool>();
|
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
protected readonly FillFlowContainer<GameplayLeaderboardScore> Flow;
|
|
|
|
|
|
|
|
private bool requiresScroll;
|
|
|
|
private readonly OsuScrollContainer scroll;
|
|
|
|
|
|
|
|
private GameplayLeaderboardScore trackedScore;
|
2020-12-15 14:44:56 +08:00
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Create a new leaderboard.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="maxPanels">The maximum panels to show at once. Defines the maximum height of this component.</param>
|
|
|
|
public GameplayLeaderboard(int maxPanels = 8)
|
|
|
|
{
|
|
|
|
this.maxPanels = maxPanels;
|
2020-12-15 14:44:56 +08:00
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
Width = GameplayLeaderboardScore.EXTENDED_WIDTH + GameplayLeaderboardScore.SHEAR_WIDTH;
|
2020-12-15 14:44:56 +08:00
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
2021-08-13 13:46:28 +08:00
|
|
|
scroll = new InputDisabledScrollContainer
|
2021-08-12 15:17:14 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Child = Flow = new FillFlowContainer<GameplayLeaderboardScore>
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
X = GameplayLeaderboardScore.SHEAR_WIDTH,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Spacing = new Vector2(2.5f),
|
|
|
|
LayoutDuration = 450,
|
|
|
|
LayoutEasing = Easing.OutQuint,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-12-15 14:27:26 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 20:49:38 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
Scheduler.AddDelayed(sort, 1000, true);
|
|
|
|
}
|
|
|
|
|
2020-12-19 21:55:58 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a player to the leaderboard.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="user">The player.</param>
|
|
|
|
/// <param name="isTracked">
|
|
|
|
/// Whether the player should be tracked on the leaderboard.
|
|
|
|
/// Set to <c>true</c> for the local player or a player whose replay is currently being played.
|
|
|
|
/// </param>
|
2021-11-04 17:02:44 +08:00
|
|
|
public ILeaderboardScore Add([CanBeNull] APIUser user, bool isTracked)
|
2020-12-15 14:27:26 +08:00
|
|
|
{
|
2021-08-09 15:59:24 +08:00
|
|
|
var drawable = CreateLeaderboardScoreDrawable(user, isTracked);
|
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
if (isTracked)
|
|
|
|
{
|
|
|
|
if (trackedScore != null)
|
2021-08-13 13:46:57 +08:00
|
|
|
throw new InvalidOperationException("Cannot track more than one score.");
|
2021-08-12 15:17:14 +08:00
|
|
|
|
|
|
|
trackedScore = drawable;
|
|
|
|
}
|
|
|
|
|
2021-08-09 15:59:24 +08:00
|
|
|
drawable.Expanded.BindTo(Expanded);
|
2020-12-18 16:30:11 +08:00
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
Flow.Add(drawable);
|
2020-12-24 20:49:38 +08:00
|
|
|
drawable.TotalScore.BindValueChanged(_ => sorting.Invalidate(), true);
|
2020-12-18 16:07:38 +08:00
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
int displayCount = Math.Min(Flow.Count, maxPanels);
|
|
|
|
Height = displayCount * (GameplayLeaderboardScore.PANEL_HEIGHT + Flow.Spacing.Y);
|
|
|
|
requiresScroll = displayCount != Flow.Count;
|
2020-12-18 16:25:48 +08:00
|
|
|
|
2020-12-18 16:07:38 +08:00
|
|
|
return drawable;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
Flow.Clear();
|
|
|
|
trackedScore = null;
|
|
|
|
scroll.ScrollToStart(false);
|
|
|
|
}
|
|
|
|
|
2021-11-04 17:02:44 +08:00
|
|
|
protected virtual GameplayLeaderboardScore CreateLeaderboardScoreDrawable(APIUser user, bool isTracked) =>
|
2021-08-09 15:59:24 +08:00
|
|
|
new GameplayLeaderboardScore(user, isTracked);
|
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
protected override void Update()
|
2020-12-18 16:07:38 +08:00
|
|
|
{
|
2021-08-12 15:17:14 +08:00
|
|
|
base.Update();
|
|
|
|
|
|
|
|
if (requiresScroll && trackedScore != null)
|
|
|
|
{
|
|
|
|
float scrollTarget = scroll.GetChildPosInContent(trackedScore) + trackedScore.DrawHeight / 2 - scroll.DrawHeight / 2;
|
|
|
|
scroll.ScrollTo(scrollTarget, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const float panel_height = GameplayLeaderboardScore.PANEL_HEIGHT;
|
|
|
|
|
|
|
|
float fadeBottom = scroll.Current + scroll.DrawHeight;
|
|
|
|
float fadeTop = scroll.Current + panel_height;
|
|
|
|
|
|
|
|
if (scroll.Current <= 0) fadeTop -= panel_height;
|
|
|
|
if (!scroll.IsScrolledToEnd()) fadeBottom -= panel_height;
|
|
|
|
|
|
|
|
// logic is mostly shared with Leaderboard, copied here for simplicity.
|
|
|
|
foreach (var c in Flow.Children)
|
|
|
|
{
|
|
|
|
float topY = c.ToSpaceOfOtherDrawable(Vector2.Zero, Flow).Y;
|
|
|
|
float bottomY = topY + panel_height;
|
|
|
|
|
|
|
|
bool requireTopFade = requiresScroll && topY <= fadeTop;
|
|
|
|
bool requireBottomFade = requiresScroll && bottomY >= fadeBottom;
|
|
|
|
|
|
|
|
if (!requireTopFade && !requireBottomFade)
|
|
|
|
c.Colour = Color4.White;
|
|
|
|
else if (topY > fadeBottom + panel_height || bottomY < fadeTop - panel_height)
|
|
|
|
c.Colour = Color4.Transparent;
|
|
|
|
else
|
|
|
|
{
|
2021-08-13 13:50:59 +08:00
|
|
|
if (requireBottomFade)
|
2021-08-12 15:17:14 +08:00
|
|
|
{
|
|
|
|
c.Colour = ColourInfo.GradientVertical(
|
|
|
|
Color4.White.Opacity(Math.Min(1 - (topY - fadeBottom) / panel_height, 1)),
|
|
|
|
Color4.White.Opacity(Math.Min(1 - (bottomY - fadeBottom) / panel_height, 1)));
|
|
|
|
}
|
|
|
|
else if (requiresScroll)
|
|
|
|
{
|
|
|
|
c.Colour = ColourInfo.GradientVertical(
|
|
|
|
Color4.White.Opacity(Math.Min(1 - (fadeTop - topY) / panel_height, 1)),
|
|
|
|
Color4.White.Opacity(Math.Min(1 - (fadeTop - bottomY) / panel_height, 1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 14:27:26 +08:00
|
|
|
}
|
|
|
|
|
2020-12-18 16:07:38 +08:00
|
|
|
private void sort()
|
2020-12-15 14:27:26 +08:00
|
|
|
{
|
2020-12-24 20:49:38 +08:00
|
|
|
if (sorting.IsValid)
|
|
|
|
return;
|
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
var orderedByScore = Flow.OrderByDescending(i => i.TotalScore.Value).ToList();
|
2020-12-15 14:27:26 +08:00
|
|
|
|
2021-08-12 15:17:14 +08:00
|
|
|
for (int i = 0; i < Flow.Count; i++)
|
2020-12-15 14:27:26 +08:00
|
|
|
{
|
2021-08-12 15:17:14 +08:00
|
|
|
Flow.SetLayoutPosition(orderedByScore[i], i);
|
2020-12-15 14:44:56 +08:00
|
|
|
orderedByScore[i].ScorePosition = i + 1;
|
2020-12-15 14:27:26 +08:00
|
|
|
}
|
2020-12-24 20:49:38 +08:00
|
|
|
|
|
|
|
sorting.Validate();
|
2020-12-15 14:27:26 +08:00
|
|
|
}
|
2021-08-12 15:17:14 +08:00
|
|
|
|
2021-08-13 13:46:28 +08:00
|
|
|
private class InputDisabledScrollContainer : OsuScrollContainer
|
2021-08-12 15:17:14 +08:00
|
|
|
{
|
2021-08-13 13:46:28 +08:00
|
|
|
public InputDisabledScrollContainer()
|
2021-08-12 15:17:14 +08:00
|
|
|
{
|
|
|
|
ScrollbarVisible = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool HandlePositionalInput => false;
|
|
|
|
public override bool HandleNonPositionalInput => false;
|
|
|
|
}
|
2020-12-15 14:27:26 +08:00
|
|
|
}
|
|
|
|
}
|