From a49a4329eef88c8b89b5cba38be989ad1aca2dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Wed, 22 Dec 2021 13:16:36 +0100 Subject: [PATCH] Add capability to switch between card sizes --- .../Beatmaps/Drawables/Cards/BeatmapCard.cs | 3 +- osu.Game/Overlays/BeatmapListingOverlay.cs | 78 ++++++++++++++----- 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/osu.Game/Beatmaps/Drawables/Cards/BeatmapCard.cs b/osu.Game/Beatmaps/Drawables/Cards/BeatmapCard.cs index d6dfd8cb17..af537b9e44 100644 --- a/osu.Game/Beatmaps/Drawables/Cards/BeatmapCard.cs +++ b/osu.Game/Beatmaps/Drawables/Cards/BeatmapCard.cs @@ -23,7 +23,8 @@ namespace osu.Game.Beatmaps.Drawables.Cards public IBindable Expanded { get; } - protected readonly APIBeatmapSet BeatmapSet; + public readonly APIBeatmapSet BeatmapSet; + protected readonly Bindable FavouriteState; protected abstract Drawable IdleContent { get; } diff --git a/osu.Game/Overlays/BeatmapListingOverlay.cs b/osu.Game/Overlays/BeatmapListingOverlay.cs index dd726ed010..074db5cef9 100644 --- a/osu.Game/Overlays/BeatmapListingOverlay.cs +++ b/osu.Game/Overlays/BeatmapListingOverlay.cs @@ -19,6 +19,7 @@ using osu.Game.Beatmaps.Drawables.Cards; using osu.Game.Graphics; using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Containers; +using osu.Game.Online.API.Requests.Responses; using osu.Game.Overlays.BeatmapListing; using osu.Game.Resources.Localisation.Web; using osuTK; @@ -89,6 +90,12 @@ namespace osu.Game.Overlays }; } + protected override void LoadComplete() + { + base.LoadComplete(); + filterControl.CardSize.BindValueChanged(_ => onCardSizeChanged()); + } + public void ShowWithSearch(string query) { filterControl.Search(query); @@ -135,38 +142,24 @@ namespace osu.Game.Overlays return; } - var newPanels = searchResult.Results.Select(b => BeatmapCard.Create(b, filterControl.CardSize.Value).With(card => - { - card.Anchor = Anchor.TopCentre; - card.Origin = Anchor.TopCentre; - })); + var newCards = createCardsFor(searchResult.Results); if (filterControl.CurrentPage == 0) { //No matches case - if (!newPanels.Any()) + if (!newCards.Any()) { addContentToPlaceholder(notFoundContent); return; } - // spawn new children with the contained so we only clear old content at the last moment. - // reverse ID flow is required for correct Z-ordering of the cards' expandable content (last card should be front-most). - var content = new ReverseChildIDFillFlowContainer - { - RelativeSizeAxes = Axes.X, - AutoSizeAxes = Axes.Y, - Spacing = new Vector2(10), - Alpha = 0, - Margin = new MarginPadding { Vertical = 15 }, - ChildrenEnumerable = newPanels - }; + var content = createCardContainerFor(newCards); panelLoadDelegate = LoadComponentAsync(foundContent = content, addContentToPlaceholder, (cancellationToken = new CancellationTokenSource()).Token); } else { - panelLoadDelegate = LoadComponentsAsync(newPanels, loaded => + panelLoadDelegate = LoadComponentsAsync(newCards, loaded => { lastFetchDisplayedTime = Time.Current; foundContent.AddRange(loaded); @@ -175,6 +168,28 @@ namespace osu.Game.Overlays } } + private BeatmapCard[] createCardsFor(IEnumerable beatmapSets) => beatmapSets.Select(set => BeatmapCard.Create(set, filterControl.CardSize.Value).With(c => + { + c.Anchor = Anchor.TopCentre; + c.Origin = Anchor.TopCentre; + })).ToArray(); + + private static ReverseChildIDFillFlowContainer createCardContainerFor(IEnumerable newCards) + { + // spawn new children with the contained so we only clear old content at the last moment. + // reverse ID flow is required for correct Z-ordering of the cards' expandable content (last card should be front-most). + var content = new ReverseChildIDFillFlowContainer + { + RelativeSizeAxes = Axes.X, + AutoSizeAxes = Axes.Y, + Spacing = new Vector2(10), + Alpha = 0, + Margin = new MarginPadding { Vertical = 15 }, + ChildrenEnumerable = newCards + }; + return content; + } + private void addContentToPlaceholder(Drawable content) { Loading.Hide(); @@ -195,8 +210,14 @@ namespace osu.Game.Overlays // 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. var sequence = lastContent.Delay(25).Schedule(() => lastContent.BypassAutoSizeAxes = Axes.Y); - if (lastContent != notFoundContent && lastContent != supporterRequiredContent) - sequence.Then().Schedule(() => lastContent.Expire()); + if (lastContent == foundContent) + { + sequence.Then().Schedule(() => + { + foundContent.Expire(); + foundContent = null; + }); + } } if (!content.IsAlive) @@ -209,6 +230,23 @@ namespace osu.Game.Overlays currentContent.BypassAutoSizeAxes = Axes.None; } + private void onCardSizeChanged() + { + if (foundContent == null || !foundContent.Any()) + return; + + Loading.Show(); + + var newCards = createCardsFor(foundContent.Reverse().Select(card => card.BeatmapSet)); + + panelLoadDelegate = LoadComponentsAsync(newCards, cards => + { + foundContent.Clear(); + foundContent.AddRange(cards); + Loading.Hide(); + }, (cancellationToken = new CancellationTokenSource()).Token); + } + protected override void Dispose(bool isDisposing) { cancellationToken?.Cancel();