osu/osu.Game/Overlays/BeatmapListingOverlay.cs

341 lines
12 KiB
C#
Raw Normal View History

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.
using System.Collections.Generic;
2020-02-19 14:40:54 +00:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2020-02-19 14:40:54 +00:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
2021-06-22 05:53:21 +00:00
using osu.Framework.Localisation;
2020-02-19 14:40:54 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input.Events;
2020-02-19 14:40:54 +00:00
using osu.Game.Audio;
using osu.Game.Beatmaps;
2021-06-19 12:54:24 +00:00
using osu.Game.Graphics;
2020-02-19 14:40:54 +00:00
using osu.Game.Graphics.Sprites;
2021-06-19 12:54:24 +00:00
using osu.Game.Graphics.Containers;
2020-02-19 14:40:54 +00:00
using osu.Game.Overlays.BeatmapListing;
using osu.Game.Overlays.BeatmapListing.Panels;
using osu.Game.Resources.Localisation.Web;
2020-02-19 14:40:54 +00:00
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Overlays
{
2021-01-18 08:13:38 +00:00
public class BeatmapListingOverlay : OnlineOverlay<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;
private Container panelTarget;
private FillFlowContainer<BeatmapPanel> foundContent;
private NotFoundDrawable notFoundContent;
2021-06-19 12:54:24 +00:00
private SupporterRequiredDrawable supporterRequiredContent;
2021-01-18 07:48:12 +00:00
private BeatmapListingFilterControl filterControl;
2020-02-19 14:40:54 +00:00
public BeatmapListingOverlay()
: base(OverlayColourScheme.Blue)
{
}
[BackgroundDependencyLoader]
private void load()
{
2021-01-18 07:48:12 +00:00
Child = new FillFlowContainer
2020-02-19 14:40:54 +00:00
{
2021-01-18 07:48:12 +00:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
2020-02-19 14:40:54 +00:00
{
2021-01-18 07:48:12 +00:00
filterControl = new BeatmapListingFilterControl
{
TypingStarted = onTypingStarted,
SearchStarted = onSearchStarted,
SearchFinished = onSearchFinished,
},
new Container
2020-02-19 14:40:54 +00:00
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Children = new Drawable[]
{
2021-01-18 07:48:12 +00:00
new Box
2020-02-19 14:40:54 +00:00
{
2021-01-18 07:48:12 +00:00
RelativeSizeAxes = Axes.Both,
Colour = ColourProvider.Background4,
2020-02-19 14:40:54 +00:00
},
2021-01-18 07:48:12 +00:00
panelTarget = new Container
2020-02-19 14:40:54 +00:00
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
2021-01-18 07:48:12 +00:00
Padding = new MarginPadding { Horizontal = 20 },
2020-02-19 14:40:54 +00:00
Children = new Drawable[]
{
2021-01-18 07:48:12 +00:00
foundContent = new FillFlowContainer<BeatmapPanel>(),
notFoundContent = new NotFoundDrawable(),
2021-06-19 12:54:24 +00:00
supporterRequiredContent = new SupporterRequiredDrawable(),
2020-02-19 14:40:54 +00:00
}
}
2021-01-18 07:48:12 +00:00
},
},
2020-02-19 14:40:54 +00:00
}
};
}
2021-07-01 10:45:17 +00:00
public void ShowWithSearch(string query)
{
filterControl.Search(query);
Show();
}
2020-02-19 14:40:54 +00:00
2021-01-18 07:48:12 +00:00
protected override BeatmapListingHeader CreateHeader() => new BeatmapListingHeader();
2020-02-19 14:40:54 +00:00
protected override Color4 BackgroundColour => ColourProvider.Background6;
2020-02-19 14:40:54 +00:00
private void onTypingStarted()
{
// temporary until the textbox/header is updated to always stay on screen.
2021-01-18 07:48:12 +00:00
ScrollFlow.ScrollToStart();
2020-02-19 23:54:35 +00:00
}
protected override void OnFocus(FocusEvent e)
2020-02-19 23:54:35 +00:00
{
base.OnFocus(e);
2020-02-19 23:54:35 +00:00
filterControl.TakeFocus();
2020-02-19 14:40:54 +00:00
}
private CancellationTokenSource cancellationToken;
2020-02-19 14:40:54 +00:00
private void onSearchStarted()
2020-02-19 23:54:35 +00:00
{
cancellationToken?.Cancel();
2020-02-19 14:40:54 +00:00
previewTrackManager.StopAnyPlaying(this);
if (panelTarget.Any())
2021-01-18 07:48:12 +00:00
Loading.Show();
2020-02-19 14:40:54 +00:00
}
private Task panelLoadDelegate;
2021-06-22 05:53:21 +00:00
private void onSearchFinished(BeatmapListingFilterControl.SearchResult searchResult)
2020-02-19 14:40:54 +00:00
{
if (searchResult.Type == BeatmapListingFilterControl.SearchResultType.SupporterOnlyFilters)
2020-02-19 14:40:54 +00:00
{
supporterRequiredContent.UpdateText(searchResult.SupporterOnlyFiltersUsed);
2021-06-22 05:53:21 +00:00
addContentToPlaceholder(supporterRequiredContent);
2020-02-19 14:40:54 +00:00
return;
}
2021-06-22 05:53:21 +00:00
var newPanels = searchResult.Results.Select<BeatmapSetInfo, BeatmapPanel>(b => new GridBeatmapPanel(b)
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
});
if (filterControl.CurrentPage == 0)
2020-02-19 14:40:54 +00:00
{
//No matches case
if (!newPanels.Any())
2020-02-19 14:40:54 +00:00
{
2021-06-22 05:53:21 +00:00
addContentToPlaceholder(notFoundContent);
return;
}
2020-02-19 14:40:54 +00:00
// 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
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(10),
Alpha = 0,
Margin = new MarginPadding { Vertical = 15 },
ChildrenEnumerable = newPanels
};
panelLoadDelegate = LoadComponentAsync(foundContent = content, addContentToPlaceholder, (cancellationToken = new CancellationTokenSource()).Token);
}
else
2020-02-19 14:40:54 +00:00
{
panelLoadDelegate = LoadComponentsAsync(newPanels, loaded =>
{
lastFetchDisplayedTime = Time.Current;
foundContent.AddRange(loaded);
loaded.ForEach(p => p.FadeIn(200, Easing.OutQuint));
});
}
}
private void addContentToPlaceholder(Drawable content)
{
2021-01-18 07:48:12 +00:00
Loading.Hide();
lastFetchDisplayedTime = Time.Current;
if (content == currentContent)
return;
var lastContent = currentContent;
2020-02-20 04:48:34 +00:00
if (lastContent != null)
{
var transform = lastContent.FadeOut(100, Easing.OutQuint);
2020-02-20 04:48:34 +00:00
2021-06-22 05:53:21 +00:00
if (lastContent == notFoundContent || lastContent == supporterRequiredContent)
{
// the placeholders may be used multiple times, so don't expire/dispose them.
transform.Schedule(() => panelTarget.Remove(lastContent));
}
else
{
// 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.
lastContent.Delay(25).Schedule(() => lastContent.BypassAutoSizeAxes = Axes.Y).Then().Schedule(() => lastContent.Expire());
}
2020-02-20 04:48:34 +00:00
}
if (!content.IsAlive)
panelTarget.Add(content);
content.FadeInFromZero(200, Easing.OutQuint);
currentContent = content;
}
protected override void Dispose(bool isDisposing)
{
cancellationToken?.Cancel();
base.Dispose(isDisposing);
}
public class NotFoundDrawable : CompositeDrawable
{
public NotFoundDrawable()
{
RelativeSizeAxes = Axes.X;
Height = 250;
2020-02-20 02:08:42 +00:00
Alpha = 0;
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,
Text = BeatmapsStrings.ListingSearchNotFoundQuote,
}
}
});
}
2020-02-19 14:40:54 +00:00
}
// TODO: localisation requires Text/LinkFlowContainer support for localising strings with links inside
// (https://github.com/ppy/osu-framework/issues/4530)
2021-06-19 12:54:24 +00:00
public class SupporterRequiredDrawable : CompositeDrawable
{
2021-06-24 05:45:38 +00:00
private LinkFlowContainer supporterRequiredText;
2021-06-19 12:54:24 +00:00
public SupporterRequiredDrawable()
{
RelativeSizeAxes = Axes.X;
2021-06-20 09:17:07 +00:00
Height = 225;
2021-06-19 12:54:24 +00:00
Alpha = 0;
}
[BackgroundDependencyLoader]
private void load(TextureStore textures)
{
AddInternal(new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Direction = FillDirection.Horizontal,
2021-06-24 05:45:38 +00:00
Children = new Drawable[]
2021-06-19 12:54:24 +00:00
{
new Sprite
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
FillMode = FillMode.Fit,
Texture = textures.Get(@"Online/supporter-required"),
},
2021-06-24 05:45:38 +00:00
supporterRequiredText = new LinkFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Margin = new MarginPadding { Bottom = 10 },
},
2021-06-19 12:54:24 +00:00
}
});
}
2021-06-22 05:53:21 +00:00
public void UpdateText(List<LocalisableString> filters)
2021-06-21 07:31:47 +00:00
{
2021-06-24 05:45:38 +00:00
supporterRequiredText.Clear();
2021-06-19 12:54:24 +00:00
2021-06-24 05:45:38 +00:00
supporterRequiredText.AddText(
BeatmapsStrings.ListingSearchSupporterFilterQuoteDefault(string.Join(" and ", filters), "").ToString(),
2021-06-22 05:53:21 +00:00
t =>
{
t.Font = OsuFont.GetFont(size: 16);
t.Colour = Colour4.White;
}
2021-06-24 05:45:38 +00:00
);
2021-06-22 05:53:21 +00:00
supporterRequiredText.AddLink(BeatmapsStrings.ListingSearchSupporterFilterQuoteLinkText.ToString(), @"/store/products/supporter-tag");
2021-06-19 12:54:24 +00:00
}
}
private const double time_between_fetches = 500;
private double lastFetchDisplayedTime;
protected override void Update()
{
base.Update();
const int pagination_scroll_distance = 500;
bool shouldShowMore = panelLoadDelegate?.IsCompleted != false
&& Time.Current - lastFetchDisplayedTime > time_between_fetches
2021-01-18 07:48:12 +00:00
&& (ScrollFlow.ScrollableExtent > 0 && ScrollFlow.IsScrolledToEnd(pagination_scroll_distance));
if (shouldShowMore)
filterControl.FetchNextPage();
}
2020-02-19 14:40:54 +00:00
}
}