osu/osu.Game/Screens/Ranking/ResultsScreen.cs

383 lines
14 KiB
C#
Raw Normal View History

2020-03-17 08:43:16 +00: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.
2020-03-18 09:28:42 +00:00
using System;
2020-05-26 08:00:41 +00:00
using System.Collections.Generic;
using System.Linq;
2020-03-17 08:43:16 +00:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2020-03-17 08:43:16 +00:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Bindings;
2020-03-17 08:43:16 +00:00
using osu.Framework.Screens;
using osu.Game.Audio;
using osu.Game.Graphics;
2020-03-17 13:21:16 +00:00
using osu.Game.Graphics.Containers;
2020-03-17 08:43:16 +00:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings;
using osu.Game.Online.API;
using osu.Game.Rulesets.Mods;
2020-03-17 08:43:16 +00:00
using osu.Game.Scoring;
2020-03-17 08:45:25 +00:00
using osu.Game.Screens.Play;
using osu.Game.Screens.Ranking.Expanded.Accuracy;
using osu.Game.Screens.Ranking.Statistics;
using osu.Game.Skinning;
2020-03-17 08:43:16 +00:00
using osuTK;
namespace osu.Game.Screens.Ranking
{
public abstract class ResultsScreen : ScreenWithBeatmapBackground, IKeyBindingHandler<GlobalAction>
2020-03-17 08:43:16 +00:00
{
/// <summary>
/// Delay before the default applause sound should be played, in order to match the grade display timing in <see cref="AccuracyCircle"/>.
/// </summary>
public const double APPLAUSE_DELAY = AccuracyCircle.ACCURACY_TRANSFORM_DELAY + AccuracyCircle.TEXT_APPEAR_DELAY + ScorePanel.RESIZE_DURATION + ScorePanel.TOP_LAYER_EXPAND_DELAY - 1440;
2020-03-17 08:43:16 +00:00
protected const float BACKGROUND_BLUR = 20;
private static readonly float screen_height = 768 - TwoLayerButton.SIZE_EXTENDED.Y;
2020-03-17 08:43:16 +00:00
public override bool DisallowExternalBeatmapRulesetChanges => true;
// Temporary for now to stop dual transitions. Should respect the current toolbar mode, but there's no way to do so currently.
public override bool HideOverlaysOnEnter => true;
public readonly Bindable<ScoreInfo> SelectedScore = new Bindable<ScoreInfo>();
public readonly ScoreInfo Score;
2020-07-31 10:57:05 +00:00
protected ScorePanelList ScorePanelList { get; private set; }
2020-03-17 08:45:25 +00:00
[Resolved(CanBeNull = true)]
private Player player { get; set; }
[Resolved]
private IAPIProvider api { get; set; }
private StatisticsPanel statisticsPanel;
2020-03-17 08:43:16 +00:00
private Drawable bottomPanel;
private Container<ScorePanel> detachedPanelContainer;
2020-03-17 08:43:16 +00:00
2020-07-28 11:58:13 +00:00
private bool fetchedInitialScores;
private APIRequest nextPageRequest;
2020-07-31 10:57:05 +00:00
private readonly bool allowRetry;
private readonly bool allowWatchingReplay;
2020-07-31 10:57:05 +00:00
private SkinnableSound applauseSound;
protected ResultsScreen(ScoreInfo score, bool allowRetry, bool allowWatchingReplay = true)
2020-03-17 08:43:16 +00:00
{
2020-03-29 14:50:16 +00:00
Score = score;
this.allowRetry = allowRetry;
this.allowWatchingReplay = allowWatchingReplay;
SelectedScore.Value = score;
2020-03-17 08:43:16 +00:00
}
[BackgroundDependencyLoader]
private void load()
{
FillFlowContainer buttons;
InternalChild = new GridContainer
2020-03-17 08:43:16 +00:00
{
RelativeSizeAxes = Axes.Both,
Content = new[]
2020-03-17 08:43:16 +00:00
{
new Drawable[]
2020-03-17 13:21:16 +00:00
{
new VerticalScrollContainer
2020-03-17 08:43:16 +00:00
{
2020-06-17 13:29:00 +00:00
RelativeSizeAxes = Axes.Both,
ScrollbarVisible = false,
Child = new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
statisticsPanel = new StatisticsPanel
{
RelativeSizeAxes = Axes.Both,
Score = { BindTarget = SelectedScore }
},
2020-07-31 10:57:05 +00:00
ScorePanelList = new ScorePanelList
{
RelativeSizeAxes = Axes.Both,
SelectedScore = { BindTarget = SelectedScore },
PostExpandAction = () => statisticsPanel.ToggleVisibility()
},
detachedPanelContainer = new Container<ScorePanel>
{
RelativeSizeAxes = Axes.Both
},
}
}
},
},
new[]
{
bottomPanel = new Container
2020-03-17 08:43:16 +00:00
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
Height = TwoLayerButton.SIZE_EXTENDED.Y,
Alpha = 0,
2020-03-17 08:43:16 +00:00
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4Extensions.FromHex("#333")
},
buttons = new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Spacing = new Vector2(5),
Direction = FillDirection.Horizontal
}
2020-03-17 08:43:16 +00:00
}
}
}
},
RowDimensions = new[]
{
new Dimension(),
new Dimension(GridSizeMode.AutoSize)
2020-03-17 08:43:16 +00:00
}
};
2020-03-17 08:45:25 +00:00
if (Score != null)
{
// only show flair / animation when arriving after watching a play that isn't autoplay.
bool shouldFlair = player != null && !Score.Mods.Any(m => m is ModAutoplay);
ScorePanelList.AddScore(Score, shouldFlair);
if (shouldFlair)
{
AddInternal(applauseSound = Score.Rank >= ScoreRank.A
? new SkinnableSound(new SampleInfo("Results/rankpass", "applause"))
: new SkinnableSound(new SampleInfo("Results/rankfail")));
}
}
if (allowWatchingReplay)
{
buttons.Add(new ReplayDownloadButton(null)
{
Score = { BindTarget = SelectedScore },
Width = 300
});
}
2020-03-30 09:56:35 +00:00
if (player != null && allowRetry)
2020-03-17 08:45:25 +00:00
{
2020-03-30 09:56:35 +00:00
buttons.Add(new RetryButton { Width = 300 });
2020-03-30 09:56:35 +00:00
AddInternal(new HotkeyRetryOverlay
{
Action = () =>
2020-03-17 08:45:25 +00:00
{
2020-03-30 09:56:35 +00:00
if (!this.IsCurrentScreen()) return;
2020-03-17 08:45:25 +00:00
2020-03-30 09:56:35 +00:00
player?.Restart();
},
});
2020-03-17 08:45:25 +00:00
}
2020-03-17 08:43:16 +00:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2020-07-22 11:24:55 +00:00
var req = FetchScores(fetchScoresCallback);
2020-05-26 08:00:41 +00:00
if (req != null)
api.Queue(req);
2020-06-18 13:27:27 +00:00
statisticsPanel.State.BindValueChanged(onStatisticsStateChanged, true);
using (BeginDelayedSequence(APPLAUSE_DELAY))
Schedule(() => applauseSound?.Play());
}
2020-07-22 11:24:55 +00:00
protected override void Update()
{
base.Update();
2020-07-28 11:58:13 +00:00
if (fetchedInitialScores && nextPageRequest == null)
2020-07-22 11:24:55 +00:00
{
2020-07-31 10:57:05 +00:00
if (ScorePanelList.IsScrolledToStart)
2020-07-22 11:24:55 +00:00
nextPageRequest = FetchNextPage(-1, fetchScoresCallback);
2020-07-31 10:57:05 +00:00
else if (ScorePanelList.IsScrolledToEnd)
2020-07-22 11:24:55 +00:00
nextPageRequest = FetchNextPage(1, fetchScoresCallback);
if (nextPageRequest != null)
{
// Scheduled after children to give the list a chance to update its scroll position and not potentially trigger a second request too early.
nextPageRequest.Success += () => ScheduleAfterChildren(() => nextPageRequest = null);
nextPageRequest.Failure += _ => ScheduleAfterChildren(() => nextPageRequest = null);
2020-07-22 11:24:55 +00:00
api.Queue(nextPageRequest);
}
}
}
2020-05-26 08:00:41 +00:00
/// <summary>
/// Performs a fetch/refresh of scores to be displayed.
/// </summary>
/// <param name="scoresCallback">A callback which should be called when fetching is completed. Scheduling is not required.</param>
/// <returns>An <see cref="APIRequest"/> responsible for the fetch operation. This will be queued and performed automatically.</returns>
protected virtual APIRequest FetchScores(Action<IEnumerable<ScoreInfo>> scoresCallback) => null;
2020-07-28 11:58:13 +00:00
/// <summary>
/// Performs a fetch of the next page of scores. This is invoked every frame until a non-null <see cref="APIRequest"/> is returned.
/// </summary>
/// <param name="direction">The fetch direction. -1 to fetch scores greater than the current start of the list, and 1 to fetch scores lower than the current end of the list.</param>
/// <param name="scoresCallback">A callback which should be called when fetching is completed. Scheduling is not required.</param>
/// <returns>An <see cref="APIRequest"/> responsible for the fetch operation. This will be queued and performed automatically.</returns>
2020-07-22 11:24:55 +00:00
protected virtual APIRequest FetchNextPage(int direction, Action<IEnumerable<ScoreInfo>> scoresCallback) => null;
private void fetchScoresCallback(IEnumerable<ScoreInfo> scores) => Schedule(() =>
{
foreach (var s in scores)
addScore(s);
2020-07-28 11:58:13 +00:00
fetchedInitialScores = true;
2020-07-22 11:24:55 +00:00
});
2020-03-17 08:43:16 +00:00
public override void OnEntering(IScreen last)
{
base.OnEntering(last);
ApplyToBackground(b =>
{
b.BlurAmount.Value = BACKGROUND_BLUR;
b.FadeColour(OsuColour.Gray(0.5f), 250);
});
2020-03-17 08:43:16 +00:00
bottomPanel.FadeTo(1, 250);
}
public override bool OnExiting(IScreen next)
{
return base.OnExiting(next);
}
public override bool OnBackButton()
2020-03-17 08:43:16 +00:00
{
2020-06-18 13:27:27 +00:00
if (statisticsPanel.State.Value == Visibility.Visible)
{
statisticsPanel.Hide();
return true;
}
return false;
2020-03-17 08:43:16 +00:00
}
2020-03-17 13:21:16 +00:00
private void addScore(ScoreInfo score)
{
2020-07-31 10:57:05 +00:00
var panel = ScorePanelList.AddScore(score);
if (detachedPanel != null)
panel.Alpha = 0;
}
private ScorePanel detachedPanel;
2020-06-18 13:27:27 +00:00
private void onStatisticsStateChanged(ValueChangedEvent<Visibility> state)
2020-03-17 13:21:16 +00:00
{
if (state.NewValue == Visibility.Visible)
2020-03-17 13:21:16 +00:00
{
// Detach the panel in its original location, and move into the desired location in the local container.
2020-07-31 10:57:05 +00:00
var expandedPanel = ScorePanelList.GetPanelForScore(SelectedScore.Value);
var screenSpacePos = expandedPanel.ScreenSpaceDrawQuad.TopLeft;
2020-06-18 13:27:27 +00:00
// Detach and move into the local container.
2020-07-31 10:57:05 +00:00
ScorePanelList.Detach(expandedPanel);
detachedPanelContainer.Add(expandedPanel);
2020-06-22 06:42:55 +00:00
// Move into its original location in the local container first, then to the final location.
var origLocation = detachedPanelContainer.ToLocalSpace(screenSpacePos).X;
expandedPanel.MoveToX(origLocation)
2020-06-22 06:42:55 +00:00
.Then()
.MoveToX(StatisticsPanel.SIDE_PADDING, 150, Easing.OutQuint);
// Hide contracted panels.
2020-07-31 10:57:05 +00:00
foreach (var contracted in ScorePanelList.GetScorePanels().Where(p => p.State == PanelState.Contracted))
contracted.FadeOut(150, Easing.OutQuint);
2020-07-31 10:57:05 +00:00
ScorePanelList.HandleInput = false;
// Dim background.
ApplyToBackground(b => b.FadeTo(0.1f, 150));
detachedPanel = expandedPanel;
}
else if (detachedPanel != null)
{
var screenSpacePos = detachedPanel.ScreenSpaceDrawQuad.TopLeft;
2020-06-18 13:27:27 +00:00
// Remove from the local container and re-attach.
detachedPanelContainer.Remove(detachedPanel);
2020-07-31 10:57:05 +00:00
ScorePanelList.Attach(detachedPanel);
2020-06-22 06:42:55 +00:00
// Move into its original location in the attached container first, then to the final location.
var origLocation = detachedPanel.Parent.ToLocalSpace(screenSpacePos);
2020-06-22 06:42:55 +00:00
detachedPanel.MoveTo(origLocation)
.Then()
.MoveTo(new Vector2(0, origLocation.Y), 150, Easing.OutQuint);
// Show contracted panels.
2020-07-31 10:57:05 +00:00
foreach (var contracted in ScorePanelList.GetScorePanels().Where(p => p.State == PanelState.Contracted))
contracted.FadeIn(150, Easing.OutQuint);
2020-07-31 10:57:05 +00:00
ScorePanelList.HandleInput = true;
// Un-dim background.
ApplyToBackground(b => b.FadeTo(0.5f, 150));
detachedPanel = null;
2020-03-17 13:21:16 +00:00
}
}
public bool OnPressed(GlobalAction action)
{
switch (action)
{
case GlobalAction.Select:
statisticsPanel.ToggleVisibility();
return true;
}
return false;
}
public void OnReleased(GlobalAction action)
{
}
private class VerticalScrollContainer : OsuScrollContainer
{
protected override Container<Drawable> Content => content;
private readonly Container content;
public VerticalScrollContainer()
{
base.Content.Add(content = new Container { RelativeSizeAxes = Axes.X });
}
protected override void Update()
{
base.Update();
content.Height = Math.Max(screen_height, DrawHeight);
}
}
2020-03-17 08:43:16 +00:00
}
}