2020-02-19 14:40:54 +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-06 00:09:43 +00:00
using System.Collections.Generic ;
2020-02-19 14:40:54 +00:00
using System.Linq ;
2020-03-06 00:09:43 +00:00
using System.Threading ;
2020-05-14 06:35:11 +00:00
using System.Threading.Tasks ;
2020-02-19 14:40:54 +00:00
using osu.Framework.Allocation ;
2020-05-14 06:35:11 +00:00
using osu.Framework.Extensions.IEnumerableExtensions ;
2020-02-19 14:40:54 +00:00
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
using osu.Framework.Graphics.Shapes ;
2020-02-19 15:17:02 +00:00
using osu.Framework.Graphics.Sprites ;
using osu.Framework.Graphics.Textures ;
2020-04-21 06:47:43 +00:00
using osu.Framework.Input.Events ;
2020-02-19 14:40:54 +00:00
using osu.Game.Audio ;
using osu.Game.Beatmaps ;
using osu.Game.Graphics.Containers ;
using osu.Game.Graphics.Sprites ;
2020-02-21 07:13:24 +00:00
using osu.Game.Graphics.UserInterface ;
2020-02-19 14:40:54 +00:00
using osu.Game.Overlays.BeatmapListing ;
2020-04-21 07:03:18 +00:00
using osu.Game.Overlays.BeatmapListing.Panels ;
2020-02-19 14:40:54 +00:00
using osuTK ;
namespace osu.Game.Overlays
{
2020-09-03 07:27:31 +00:00
public class BeatmapListingOverlay : FullscreenOverlay < BeatmapListingHeader >
2020-02-19 14:40:54 +00:00
{
[Resolved]
private PreviewTrackManager previewTrackManager { get ; set ; }
2020-02-20 02:08:42 +00:00
private Drawable currentContent ;
2020-03-06 00:09:43 +00:00
private LoadingLayer loadingLayer ;
private Container panelTarget ;
2020-05-11 18:18:47 +00:00
private FillFlowContainer < BeatmapPanel > foundContent ;
private NotFoundDrawable notFoundContent ;
private OverlayScrollContainer resultScrollContainer ;
2020-02-19 14:40:54 +00:00
public BeatmapListingOverlay ( )
2020-09-03 07:27:31 +00:00
: base ( OverlayColourScheme . Blue , new BeatmapListingHeader ( ) )
2020-02-19 14:40:54 +00:00
{
}
2020-05-12 16:14:11 +00:00
private BeatmapListingFilterControl filterControl ;
2020-04-21 06:47:43 +00:00
2020-02-19 14:40:54 +00:00
[BackgroundDependencyLoader]
private void load ( )
{
Children = new Drawable [ ]
{
new Box
{
RelativeSizeAxes = Axes . Both ,
Colour = ColourProvider . Background6
} ,
2020-05-11 18:18:47 +00:00
resultScrollContainer = new OverlayScrollContainer
2020-02-19 14:40:54 +00:00
{
RelativeSizeAxes = Axes . Both ,
ScrollbarVisible = false ,
Child = new ReverseChildIDFillFlowContainer < Drawable >
{
AutoSizeAxes = Axes . Y ,
RelativeSizeAxes = Axes . X ,
Direction = FillDirection . Vertical ,
Children = new Drawable [ ]
{
2020-09-03 07:27:31 +00:00
Header ,
2020-04-21 06:47:43 +00:00
filterControl = new BeatmapListingFilterControl
2020-02-19 14:40:54 +00:00
{
2020-03-06 00:09:43 +00:00
SearchStarted = onSearchStarted ,
SearchFinished = onSearchFinished ,
2020-02-19 14:40:54 +00:00
} ,
new Container
{
AutoSizeAxes = Axes . Y ,
RelativeSizeAxes = Axes . X ,
Children = new Drawable [ ]
{
new Box
{
RelativeSizeAxes = Axes . Both ,
Colour = ColourProvider . Background4 ,
} ,
2020-03-06 00:09:43 +00:00
panelTarget = new Container
2020-02-19 14:40:54 +00:00
{
AutoSizeAxes = Axes . Y ,
2020-03-06 00:09:43 +00:00
RelativeSizeAxes = Axes . X ,
2020-05-11 18:18:47 +00:00
Padding = new MarginPadding { Horizontal = 20 } ,
Children = new Drawable [ ]
{
foundContent = new FillFlowContainer < BeatmapPanel > ( ) ,
notFoundContent = new NotFoundDrawable ( ) ,
loadingLayer = new LoadingLayer ( panelTarget )
}
}
2020-02-19 14:40:54 +00:00
}
2020-03-06 00:09:43 +00:00
} ,
2020-02-19 14:40:54 +00:00
}
}
}
} ;
}
2020-04-21 06:47:43 +00:00
protected override void OnFocus ( FocusEvent e )
{
base . OnFocus ( e ) ;
filterControl . TakeFocus ( ) ;
}
2020-03-06 00:09:43 +00:00
private CancellationTokenSource cancellationToken ;
2020-02-19 23:54:35 +00:00
2020-03-06 00:09:43 +00:00
private void onSearchStarted ( )
2020-02-19 23:54:35 +00:00
{
2020-03-06 00:09:43 +00:00
cancellationToken ? . Cancel ( ) ;
2020-02-19 14:40:54 +00:00
previewTrackManager . StopAnyPlaying ( this ) ;
2020-03-06 00:09:43 +00:00
if ( panelTarget . Any ( ) )
loadingLayer . Show ( ) ;
2020-02-19 14:40:54 +00:00
}
2020-05-14 06:35:11 +00:00
private Task panelLoadDelegate ;
2020-03-06 00:09:43 +00:00
private void onSearchFinished ( List < BeatmapSetInfo > beatmaps )
2020-02-19 14:40:54 +00:00
{
2020-05-14 06:35:11 +00:00
var newPanels = beatmaps . Select < BeatmapSetInfo , BeatmapPanel > ( b = > new GridBeatmapPanel ( b )
2020-02-19 14:40:54 +00:00
{
2020-05-14 06:35:11 +00:00
Anchor = Anchor . TopCentre ,
Origin = Anchor . TopCentre ,
} ) ;
2020-02-19 14:40:54 +00:00
2020-05-14 06:35:11 +00:00
if ( filterControl . CurrentPage = = 0 )
2020-02-19 14:40:54 +00:00
{
2020-05-14 06:35:11 +00:00
//No matches case
if ( ! newPanels . Any ( ) )
{
LoadComponentAsync ( notFoundContent , addContentToPlaceholder , ( cancellationToken = new CancellationTokenSource ( ) ) . Token ) ;
return ;
}
// spawn new children with the contained so we only clear old content at the last moment.
var content = new FillFlowContainer < BeatmapPanel >
2020-02-19 14:40:54 +00:00
{
2020-05-11 18:18:47 +00:00
RelativeSizeAxes = Axes . X ,
AutoSizeAxes = Axes . Y ,
Spacing = new Vector2 ( 10 ) ,
Alpha = 0 ,
Margin = new MarginPadding { Vertical = 15 } ,
2020-05-14 06:35:11 +00:00
ChildrenEnumerable = newPanels
2020-05-11 18:18:47 +00:00
} ;
2020-05-14 06:35:11 +00:00
panelLoadDelegate = LoadComponentAsync ( foundContent = content , addContentToPlaceholder , ( cancellationToken = new CancellationTokenSource ( ) ) . Token ) ;
2020-05-11 18:18:47 +00:00
}
else
{
2020-05-14 06:35:11 +00:00
panelLoadDelegate = LoadComponentsAsync ( newPanels , loaded = >
2020-05-11 18:18:47 +00:00
{
2020-05-14 06:35:11 +00:00
lastFetchDisplayedTime = Time . Current ;
foundContent . AddRange ( loaded ) ;
loaded . ForEach ( p = > p . FadeIn ( 200 , Easing . OutQuint ) ) ;
2020-05-11 18:18:47 +00:00
} ) ;
}
2020-02-19 15:17:02 +00:00
}
private void addContentToPlaceholder ( Drawable content )
{
2020-02-21 07:13:24 +00:00
loadingLayer . Hide ( ) ;
2020-05-14 06:35:11 +00:00
lastFetchDisplayedTime = Time . Current ;
2020-02-21 07:13:24 +00:00
2020-03-06 00:09:43 +00:00
var lastContent = currentContent ;
2020-02-20 04:48:34 +00:00
if ( lastContent ! = null )
{
lastContent . FadeOut ( 100 , Easing . OutQuint ) . Expire ( ) ;
// Consider the case when the new content is smaller than the last content.
// If the auto-size computation is delayed until fade out completes, the background remain high for too long making the resulting transition to the smaller height look weird.
// At the same time, if the last content's height is bypassed immediately, there is a period where the new content is at Alpha = 0 when the auto-sized height will be 0.
// To resolve both of these issues, the bypass is delayed until a point when the content transitions (fade-in and fade-out) overlap and it looks good to do so.
2020-05-13 16:57:03 +00:00
lastContent . Delay ( 25 ) . Schedule ( ( ) = > lastContent . BypassAutoSizeAxes = Axes . Y ) . Then ( ) . Schedule ( ( ) = > panelTarget . Remove ( lastContent ) ) ;
2020-02-20 04:48:34 +00:00
}
2020-05-11 18:18:47 +00:00
if ( ! content . IsAlive )
panelTarget . Add ( content ) ;
content . FadeIn ( 200 , Easing . OutQuint ) ;
currentContent = content ;
2020-02-19 15:17:02 +00:00
}
protected override void Dispose ( bool isDisposing )
{
2020-03-06 00:09:43 +00:00
cancellationToken ? . Cancel ( ) ;
2020-02-19 15:17:02 +00:00
base . Dispose ( isDisposing ) ;
}
2020-02-19 23:43:13 +00:00
private class NotFoundDrawable : CompositeDrawable
2020-02-19 15:17:02 +00:00
{
2020-02-19 23:43:13 +00:00
public NotFoundDrawable ( )
2020-02-19 15:17:02 +00:00
{
RelativeSizeAxes = Axes . X ;
Height = 250 ;
2020-02-20 02:08:42 +00:00
Alpha = 0 ;
2020-02-19 15:17:02 +00:00
Margin = new MarginPadding { Top = 15 } ;
}
[BackgroundDependencyLoader]
private void load ( TextureStore textures )
{
AddInternal ( new FillFlowContainer
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
RelativeSizeAxes = Axes . Y ,
AutoSizeAxes = Axes . X ,
Direction = FillDirection . Horizontal ,
Spacing = new Vector2 ( 10 , 0 ) ,
Children = new Drawable [ ]
{
new Sprite
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
RelativeSizeAxes = Axes . Both ,
FillMode = FillMode . Fit ,
Texture = textures . Get ( @"Online/not-found" )
} ,
new OsuSpriteText
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
2020-02-19 23:43:13 +00:00
Text = @"... nope, nothing found." ,
2020-02-19 15:17:02 +00:00
}
}
} ) ;
}
2020-02-19 14:40:54 +00:00
}
2020-05-11 18:18:47 +00:00
2020-05-14 06:35:11 +00:00
private const double time_between_fetches = 500 ;
private double lastFetchDisplayedTime ;
2020-05-11 18:18:47 +00:00
protected override void Update ( )
{
base . Update ( ) ;
2020-05-14 06:35:11 +00:00
const int pagination_scroll_distance = 500 ;
bool shouldShowMore = panelLoadDelegate ? . IsCompleted ! = false
& & Time . Current - lastFetchDisplayedTime > time_between_fetches
& & ( resultScrollContainer . ScrollableExtent > 0 & & resultScrollContainer . IsScrolledToEnd ( pagination_scroll_distance ) ) ;
if ( shouldShowMore )
filterControl . FetchNextPage ( ) ;
2020-05-11 18:18:47 +00:00
}
2020-02-19 14:40:54 +00:00
}
}