1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 07:53:38 +08:00
osu-lazer/osu.Game/Screens/Select/BeatmapCarousel.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1061 lines
40 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
using System.Collections.Generic;
using System.Diagnostics;
2020-10-13 17:18:22 +08:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2017-12-12 16:48:38 +08:00
using osu.Framework.Caching;
using osu.Framework.Extensions.EnumExtensions;
2020-10-13 17:18:22 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-10-12 14:36:03 +08:00
using osu.Framework.Graphics.Pooling;
using osu.Framework.Input.Bindings;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Layout;
2020-10-13 17:18:22 +08:00
using osu.Framework.Threading;
using osu.Framework.Utils;
2017-07-26 12:22:46 +08:00
using osu.Game.Beatmaps;
2020-10-13 17:18:22 +08:00
using osu.Game.Configuration;
2021-11-08 16:41:42 +08:00
using osu.Game.Database;
using osu.Game.Graphics.Containers;
2017-09-14 14:41:32 +08:00
using osu.Game.Graphics.Cursor;
using osu.Game.Input.Bindings;
2017-12-12 16:48:38 +08:00
using osu.Game.Screens.Select.Carousel;
2020-10-13 17:18:22 +08:00
using osuTK;
using osuTK.Input;
2021-11-08 16:41:42 +08:00
using Realms;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Select
{
public class BeatmapCarousel : CompositeDrawable, IKeyBindingHandler<GlobalAction>
{
2020-04-21 03:42:43 +08:00
/// <summary>
/// Height of the area above the carousel that should be treated as visible due to transparency of elements in front of it.
/// </summary>
2020-04-21 03:43:07 +08:00
public float BleedTop { get; set; }
2020-04-21 03:42:43 +08:00
/// <summary>
/// Height of the area below the carousel that should be treated as visible due to transparency of elements in front of it.
/// </summary>
2020-04-21 03:43:07 +08:00
public float BleedBottom { get; set; }
2017-12-13 18:56:16 +08:00
/// <summary>
/// Triggered when the <see cref="BeatmapSets"/> loaded change and are completely loaded.
2017-12-13 18:56:16 +08:00
/// </summary>
public Action BeatmapSetsChanged;
2018-04-13 17:19:50 +08:00
2017-12-13 18:56:16 +08:00
/// <summary>
/// The currently selected beatmap.
/// </summary>
public BeatmapInfo SelectedBeatmapInfo => selectedBeatmap?.BeatmapInfo;
2018-04-13 17:19:50 +08:00
2019-02-21 17:56:34 +08:00
private CarouselBeatmap selectedBeatmap => selectedBeatmapSet?.Beatmaps.FirstOrDefault(s => s.State.Value == CarouselItemState.Selected);
2018-04-13 17:19:50 +08:00
/// <summary>
/// The currently selected beatmap set.
/// </summary>
public BeatmapSetInfo SelectedBeatmapSet => selectedBeatmapSet?.BeatmapSet;
2018-04-13 17:19:50 +08:00
/// <summary>
/// A function to optionally decide on a recommended difficulty from a beatmap set.
/// </summary>
public Func<IEnumerable<BeatmapInfo>, BeatmapInfo> GetRecommendedBeatmap;
private CarouselBeatmapSet selectedBeatmapSet;
2018-04-13 17:19:50 +08:00
2017-12-13 18:56:16 +08:00
/// <summary>
/// Raised when the <see cref="SelectedBeatmapInfo"/> is changed.
2017-12-13 18:56:16 +08:00
/// </summary>
public Action<BeatmapInfo> SelectionChanged;
2018-04-13 17:19:50 +08:00
public override bool HandleNonPositionalInput => AllowSelection;
public override bool HandlePositionalInput => AllowSelection;
2018-04-13 17:19:50 +08:00
2020-01-27 13:55:47 +08:00
public override bool PropagatePositionalInputSubTree => AllowSelection;
public override bool PropagateNonPositionalInputSubTree => AllowSelection;
2020-10-13 18:10:35 +08:00
private (int first, int last) displayedRange;
/// <summary>
/// Extend the range to retain already loaded pooled drawables.
/// </summary>
private const float distance_offscreen_before_unload = 1024;
/// <summary>
/// Extend the range to update positions / retrieve pooled drawables outside of visible range.
/// </summary>
private const float distance_offscreen_to_preload = 512; // todo: adjust this appropriately once we can make set panel contents load while off-screen.
/// <summary>
2019-03-21 19:51:06 +08:00
/// Whether carousel items have completed asynchronously loaded.
/// </summary>
2019-03-21 19:51:06 +08:00
public bool BeatmapSetsLoaded { get; private set; }
2018-04-13 17:19:50 +08:00
protected readonly CarouselScrollContainer Scroll;
private readonly NoResultsPlaceholder noResultsPlaceholder;
private IEnumerable<CarouselBeatmapSet> beatmapSets => root.Items.OfType<CarouselBeatmapSet>();
2018-04-13 17:19:50 +08:00
// todo: only used for testing, maybe remove.
2022-01-10 13:52:59 +08:00
private bool loadedTestBeatmaps;
public IEnumerable<BeatmapSetInfo> BeatmapSets
{
get => beatmapSets.Select(g => g.BeatmapSet);
2022-01-10 13:52:59 +08:00
set
{
loadedTestBeatmaps = true;
Schedule(() => loadBeatmapSets(value));
2022-01-10 13:52:59 +08:00
}
}
private void loadBeatmapSets(IEnumerable<BeatmapSetInfo> beatmapSets)
{
CarouselRoot newRoot = new CarouselRoot(this);
newRoot.AddItems(beatmapSets.Select(s => createCarouselSet(s.Detach())).Where(g => g != null));
2018-04-13 17:19:50 +08:00
root = newRoot;
if (selectedBeatmapSet != null && !beatmapSets.Contains(selectedBeatmapSet.BeatmapSet))
selectedBeatmapSet = null;
Scroll.Clear(false);
itemsCache.Invalidate();
ScrollToSelected();
applyActiveCriteria(false);
if (loadedTestBeatmaps)
signalBeatmapsLoaded();
}
2018-04-13 17:19:50 +08:00
private readonly List<CarouselItem> visibleItems = new List<CarouselItem>();
private readonly Cached itemsCache = new Cached();
private PendingScrollOperation pendingScrollOperation = PendingScrollOperation.None;
2018-04-13 17:19:50 +08:00
public Bindable<bool> RightClickScrollingEnabled = new Bindable<bool>();
public Bindable<RandomSelectAlgorithm> RandomAlgorithm = new Bindable<RandomSelectAlgorithm>();
private readonly List<CarouselBeatmapSet> previouslyVisitedRandomSets = new List<CarouselBeatmapSet>();
private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>();
2018-04-13 17:19:50 +08:00
private CarouselRoot root;
2020-04-11 15:41:11 +08:00
private IDisposable subscriptionSets;
2022-01-12 00:03:59 +08:00
private IDisposable subscriptionDeletedSets;
private IDisposable subscriptionBeatmaps;
2022-01-12 00:03:59 +08:00
private IDisposable subscriptionHiddenBeatmaps;
2020-10-12 14:36:03 +08:00
private readonly DrawablePool<DrawableCarouselBeatmapSet> setPool = new DrawablePool<DrawableCarouselBeatmapSet>(100);
private Sample spinSample;
private Sample randomSelectSample;
private int visibleSetsCount;
2017-03-17 18:12:54 +08:00
public BeatmapCarousel()
2017-01-18 08:18:15 +08:00
{
root = new CarouselRoot(this);
InternalChild = new OsuContextMenuContainer
2017-01-18 08:18:15 +08:00
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2017-09-14 14:41:32 +08:00
{
setPool,
Scroll = new CarouselScrollContainer
{
RelativeSizeAxes = Axes.Both,
},
noResultsPlaceholder = new NoResultsPlaceholder()
2017-09-14 14:41:32 +08:00
}
2017-12-14 12:49:43 +08:00
};
2017-01-18 08:18:15 +08:00
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(OsuConfigManager config, AudioManager audio)
{
spinSample = audio.Samples.Get("SongSelect/random-spin");
randomSelectSample = audio.Samples.Get(@"SongSelect/select-random");
config.BindWith(OsuSetting.RandomSelectAlgorithm, RandomAlgorithm);
2018-04-18 18:26:54 +08:00
config.BindWith(OsuSetting.SongSelectRightMouseScroll, RightClickScrollingEnabled);
RightClickScrollingEnabled.ValueChanged += enabled => Scroll.RightMouseScrollbar = enabled.NewValue;
RightClickScrollingEnabled.TriggerChange();
if (!loadedTestBeatmaps)
{
realm.Run(r => loadBeatmapSets(getBeatmapSets(r)));
}
2021-11-08 16:41:42 +08:00
}
[Resolved]
private RealmAccess realm { get; set; }
2021-11-08 16:41:42 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
subscriptionSets = realm.RegisterForNotifications(getBeatmapSets, beatmapSetsChanged);
subscriptionBeatmaps = realm.RegisterForNotifications(r => r.All<BeatmapInfo>().Where(b => !b.Hidden), beatmapsChanged);
2022-01-12 00:03:59 +08:00
// Can't use main subscriptions because we can't lookup deleted indices.
// https://github.com/realm/realm-dotnet/discussions/2634#discussioncomment-1605595.
subscriptionDeletedSets = realm.RegisterForNotifications(r => r.All<BeatmapSetInfo>().Where(s => s.DeletePending && !s.Protected), deletedBeatmapSetsChanged);
subscriptionHiddenBeatmaps = realm.RegisterForNotifications(r => r.All<BeatmapInfo>().Where(b => b.Hidden), beatmapsChanged);
2022-01-12 00:03:59 +08:00
}
private void deletedBeatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet changes, Exception error)
{
// If loading test beatmaps, avoid overwriting with realm subscription callbacks.
if (loadedTestBeatmaps)
return;
if (changes == null)
return;
foreach (int i in changes.InsertedIndices)
removeBeatmapSet(sender[i].ID);
}
2018-04-13 17:19:50 +08:00
2021-11-08 16:41:42 +08:00
private void beatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet changes, Exception error)
{
2022-01-10 13:52:59 +08:00
// If loading test beatmaps, avoid overwriting with realm subscription callbacks.
if (loadedTestBeatmaps)
return;
2021-11-08 16:41:42 +08:00
if (changes == null)
{
// During initial population, we must manually account for the fact that our original query was done on an async thread.
// Since then, there may have been imports or deletions.
// Here we manually catch up on any changes.
var realmSets = new HashSet<Guid>();
for (int i = 0; i < sender.Count; i++)
realmSets.Add(sender[i].ID);
foreach (var id in realmSets)
{
if (!root.BeatmapSetsByID.ContainsKey(id))
UpdateBeatmapSet(realm.Realm.Find<BeatmapSetInfo>(id).Detach());
}
foreach (var id in root.BeatmapSetsByID.Keys)
{
if (!realmSets.Contains(id))
removeBeatmapSet(id);
}
signalBeatmapsLoaded();
2021-11-08 16:41:42 +08:00
return;
}
foreach (int i in changes.NewModifiedIndices)
UpdateBeatmapSet(sender[i].Detach());
2021-11-08 16:41:42 +08:00
2022-01-12 00:03:59 +08:00
foreach (int i in changes.InsertedIndices)
UpdateBeatmapSet(sender[i].Detach());
2021-11-08 16:41:42 +08:00
}
private void beatmapsChanged(IRealmCollection<BeatmapInfo> sender, ChangeSet changes, Exception error)
{
// we only care about actual changes in hidden status.
if (changes == null)
return;
2022-01-12 00:03:59 +08:00
foreach (int i in changes.InsertedIndices)
{
var beatmapInfo = sender[i];
var beatmapSet = beatmapInfo.BeatmapSet;
Debug.Assert(beatmapSet != null);
// Only require to action here if the beatmap is missing.
// This avoids processing these events unnecessarily when new beatmaps are imported, for example.
if (root.BeatmapSetsByID.TryGetValue(beatmapSet.ID, out var existingSet)
&& existingSet.BeatmapSet.Beatmaps.All(b => b.ID != beatmapInfo.ID))
{
UpdateBeatmapSet(beatmapSet.Detach());
}
}
2021-11-08 16:41:42 +08:00
}
private IQueryable<BeatmapSetInfo> getBeatmapSets(Realm realm) => realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending && !s.Protected);
public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet) =>
removeBeatmapSet(beatmapSet.ID);
2018-04-13 17:19:50 +08:00
private void removeBeatmapSet(Guid beatmapSetID) => Schedule(() =>
{
if (!root.BeatmapSetsByID.TryGetValue(beatmapSetID, out var existingSet))
return;
2018-04-13 17:19:50 +08:00
root.RemoveItem(existingSet);
itemsCache.Invalidate();
if (!Scroll.UserScrolling)
ScrollToSelected(true);
});
2018-04-13 17:19:50 +08:00
public void UpdateBeatmapSet(BeatmapSetInfo beatmapSet) => Schedule(() =>
{
Guid? previouslySelectedID = null;
// If the selected beatmap is about to be removed, store its ID so it can be re-selected if required
if (selectedBeatmapSet?.BeatmapSet.ID == beatmapSet.ID)
previouslySelectedID = selectedBeatmap?.BeatmapInfo.ID;
2018-04-13 17:19:50 +08:00
var newSet = createCarouselSet(beatmapSet);
var removedSet = root.RemoveChild(beatmapSet.ID);
2018-04-13 17:19:50 +08:00
// If we don't remove this here, it may remain in a hidden state until scrolled off screen.
// Doesn't really affect anything during actual user interaction, but makes testing annoying.
var removedDrawable = Scroll.FirstOrDefault(c => c.Item == removedSet);
if (removedDrawable != null)
expirePanelImmediately(removedDrawable);
2018-04-13 17:19:50 +08:00
if (newSet != null)
{
root.AddItem(newSet);
2018-04-13 17:19:50 +08:00
// check if we can/need to maintain our current selection.
if (previouslySelectedID != null)
select((CarouselItem)newSet.Beatmaps.FirstOrDefault(b => b.BeatmapInfo.ID == previouslySelectedID) ?? newSet);
}
2018-04-13 17:19:50 +08:00
itemsCache.Invalidate();
2018-04-13 17:19:50 +08:00
if (!Scroll.UserScrolling)
ScrollToSelected(true);
2018-04-13 17:19:50 +08:00
BeatmapSetsChanged?.Invoke();
});
2018-04-13 17:19:50 +08:00
/// <summary>
/// Selects a given beatmap on the carousel.
/// </summary>
2021-10-02 23:55:29 +08:00
/// <param name="beatmapInfo">The beatmap to select.</param>
/// <param name="bypassFilters">Whether to select the beatmap even if it is filtered (i.e., not visible on carousel).</param>
/// <returns>True if a selection was made, False if it wasn't.</returns>
2021-10-02 23:55:29 +08:00
public bool SelectBeatmap(BeatmapInfo beatmapInfo, bool bypassFilters = true)
{
// ensure that any pending events from BeatmapManager have been run before attempting a selection.
Scheduler.Update();
2021-10-02 23:55:29 +08:00
if (beatmapInfo?.Hidden != false)
return false;
2018-04-13 17:19:50 +08:00
foreach (CarouselBeatmapSet set in beatmapSets)
{
2019-02-21 17:56:34 +08:00
if (!bypassFilters && set.Filtered.Value)
continue;
2018-04-13 17:19:50 +08:00
2021-10-02 23:55:29 +08:00
var item = set.Beatmaps.FirstOrDefault(p => p.BeatmapInfo.Equals(beatmapInfo));
2018-04-13 17:19:50 +08:00
if (item == null)
// The beatmap that needs to be selected doesn't exist in this set
continue;
2018-04-13 17:19:50 +08:00
2019-02-21 17:56:34 +08:00
if (!bypassFilters && item.Filtered.Value)
return false;
2018-04-13 17:19:50 +08:00
select(item);
// if we got here and the set is filtered, it means we were bypassing filters.
// in this case, reapplying the filter is necessary to ensure the panel is in the correct place
// (since it is forcefully being included in the carousel).
if (set.Filtered.Value)
{
Debug.Assert(bypassFilters);
applyActiveCriteria(false);
}
return true;
}
2018-04-13 17:19:50 +08:00
return false;
}
2018-04-13 17:19:50 +08:00
/// <summary>
/// Increment selection in the carousel in a chosen direction.
/// </summary>
/// <param name="direction">The direction to increment. Negative is backwards.</param>
/// <param name="skipDifficulties">Whether to skip individual difficulties and only increment over full groups.</param>
public void SelectNext(int direction = 1, bool skipDifficulties = true)
{
2020-03-29 02:21:21 +08:00
if (beatmapSets.All(s => s.Filtered.Value))
return;
2018-04-13 17:19:50 +08:00
2020-03-28 18:54:48 +08:00
if (skipDifficulties)
selectNextSet(direction, true);
else
selectNextDifficulty(direction);
}
2018-04-13 17:19:50 +08:00
2020-03-28 18:54:48 +08:00
private void selectNextSet(int direction, bool skipDifficulties)
{
2020-03-29 23:07:48 +08:00
var unfilteredSets = beatmapSets.Where(s => !s.Filtered.Value).ToList();
2018-04-13 17:19:50 +08:00
2020-03-29 23:07:48 +08:00
var nextSet = unfilteredSets[(unfilteredSets.IndexOf(selectedBeatmapSet) + direction + unfilteredSets.Count) % unfilteredSets.Count];
2018-04-13 17:19:50 +08:00
2020-03-28 18:54:48 +08:00
if (skipDifficulties)
2020-03-28 19:23:31 +08:00
select(nextSet);
2020-03-28 18:54:48 +08:00
else
2020-03-28 19:23:31 +08:00
select(direction > 0 ? nextSet.Beatmaps.First(b => !b.Filtered.Value) : nextSet.Beatmaps.Last(b => !b.Filtered.Value));
2020-03-28 18:54:48 +08:00
}
2018-04-13 17:19:50 +08:00
2020-03-28 18:54:48 +08:00
private void selectNextDifficulty(int direction)
{
if (selectedBeatmap == null)
return;
var unfilteredDifficulties = selectedBeatmapSet.Items.Where(s => !s.Filtered.Value).ToList();
2018-04-13 17:19:50 +08:00
2020-03-29 23:07:48 +08:00
int index = unfilteredDifficulties.IndexOf(selectedBeatmap);
2018-04-13 17:19:50 +08:00
2020-03-29 23:07:48 +08:00
if (index + direction < 0 || index + direction >= unfilteredDifficulties.Count)
2020-03-28 18:54:48 +08:00
selectNextSet(direction, false);
else
2020-03-29 23:07:48 +08:00
select(unfilteredDifficulties[index + direction]);
}
2018-04-13 17:19:50 +08:00
/// <summary>
/// Select the next beatmap in the random sequence.
/// </summary>
/// <returns>True if a selection could be made, else False.</returns>
public bool SelectNextRandom()
{
if (!AllowSelection)
return false;
2019-02-21 17:56:34 +08:00
var visibleSets = beatmapSets.Where(s => !s.Filtered.Value).ToList();
visibleSetsCount = visibleSets.Count;
if (!visibleSets.Any())
return false;
2018-04-13 17:19:50 +08:00
2017-12-12 16:48:38 +08:00
if (selectedBeatmap != null)
{
2017-12-12 16:48:38 +08:00
randomSelectedBeatmaps.Push(selectedBeatmap);
2018-04-13 17:19:50 +08:00
// when performing a random, we want to add the current set to the previously visited list
// else the user may be "randomised" to the existing selection.
if (previouslyVisitedRandomSets.LastOrDefault() != selectedBeatmapSet)
previouslyVisitedRandomSets.Add(selectedBeatmapSet);
}
2018-04-13 17:19:50 +08:00
CarouselBeatmapSet set;
2018-04-13 17:19:50 +08:00
2019-02-21 17:56:34 +08:00
if (RandomAlgorithm.Value == RandomSelectAlgorithm.RandomPermutation)
{
var notYetVisitedSets = visibleSets.Except(previouslyVisitedRandomSets).ToList();
2019-04-01 11:16:05 +08:00
if (!notYetVisitedSets.Any())
2017-06-01 02:31:05 +08:00
{
previouslyVisitedRandomSets.RemoveAll(s => visibleSets.Contains(s));
notYetVisitedSets = visibleSets;
}
2018-04-13 17:19:50 +08:00
set = notYetVisitedSets.ElementAt(RNG.Next(notYetVisitedSets.Count));
previouslyVisitedRandomSets.Add(set);
}
else
set = visibleSets.ElementAt(RNG.Next(visibleSets.Count));
2018-04-13 17:19:50 +08:00
if (selectedBeatmapSet != null)
playSpinSample(distanceBetween(set, selectedBeatmapSet));
select(set);
return true;
}
2018-04-13 17:19:50 +08:00
2017-06-05 17:24:28 +08:00
public void SelectPreviousRandom()
2017-06-02 01:54:42 +08:00
{
2017-06-05 17:24:28 +08:00
while (randomSelectedBeatmaps.Any())
2017-06-02 01:54:42 +08:00
{
2017-12-12 16:48:38 +08:00
var beatmap = randomSelectedBeatmaps.Pop();
2018-04-13 17:19:50 +08:00
2019-02-21 17:56:34 +08:00
if (!beatmap.Filtered.Value)
2017-06-02 01:54:42 +08:00
{
2019-02-21 17:56:34 +08:00
if (RandomAlgorithm.Value == RandomSelectAlgorithm.RandomPermutation)
previouslyVisitedRandomSets.Remove(selectedBeatmapSet);
if (selectedBeatmapSet != null)
playSpinSample(distanceBetween(beatmap, selectedBeatmapSet));
2017-12-12 16:48:38 +08:00
select(beatmap);
2017-06-02 01:54:42 +08:00
break;
}
}
}
2018-04-13 17:19:50 +08:00
private double distanceBetween(CarouselItem item1, CarouselItem item2) => Math.Ceiling(Math.Abs(item1.CarouselYPosition - item2.CarouselYPosition) / DrawableCarouselItem.MAX_HEIGHT);
private void playSpinSample(double distance)
{
var chan = spinSample.GetChannel();
chan.Frequency.Value = 1f + Math.Min(1f, distance / visibleSetsCount);
chan.Play();
randomSelectSample?.Play();
}
private void select(CarouselItem item)
{
if (!AllowSelection)
return;
if (item == null) return;
2019-02-28 12:31:40 +08:00
item.State.Value = CarouselItemState.Selected;
}
2018-04-13 17:19:50 +08:00
2017-12-16 15:27:39 +08:00
private FilterCriteria activeCriteria = new FilterCriteria();
2018-04-13 17:19:50 +08:00
2018-07-18 09:12:14 +08:00
protected ScheduledDelegate PendingFilter;
2018-04-13 17:19:50 +08:00
public bool AllowSelection = true;
2018-04-13 17:19:50 +08:00
/// <summary>
2019-07-26 14:22:29 +08:00
/// Half the height of the visible content.
/// <remarks>
/// This is different from the height of <see cref="ScrollContainer{T}"/>.displayableContent, since
/// the beatmap carousel bleeds into the <see cref="FilterControl"/> and the <see cref="Footer"/>
/// </remarks>
/// </summary>
2020-04-19 23:29:06 +08:00
private float visibleHalfHeight => (DrawHeight + BleedBottom + BleedTop) / 2;
/// <summary>
/// The position of the lower visible bound with respect to the current scroll position.
/// </summary>
private float visibleBottomBound => Scroll.Current + DrawHeight + BleedBottom;
/// <summary>
/// The position of the upper visible bound with respect to the current scroll position.
/// </summary>
private float visibleUpperBound => Scroll.Current - BleedTop;
2017-12-16 15:27:39 +08:00
public void FlushPendingFilterOperations()
2017-07-21 16:20:52 +08:00
{
2018-07-18 09:12:14 +08:00
if (PendingFilter?.Completed == false)
{
applyActiveCriteria(false);
Update();
}
2017-07-21 16:20:52 +08:00
}
2018-04-13 17:19:50 +08:00
2017-12-16 15:27:39 +08:00
public void Filter(FilterCriteria newCriteria, bool debounce = true)
{
if (newCriteria != null)
2017-12-16 15:27:39 +08:00
activeCriteria = newCriteria;
2018-04-13 17:19:50 +08:00
applyActiveCriteria(debounce);
2017-12-16 15:27:39 +08:00
}
2018-04-13 17:19:50 +08:00
private void applyActiveCriteria(bool debounce, bool alwaysResetScrollPosition = true)
2017-12-16 15:27:39 +08:00
{
PendingFilter?.Cancel();
PendingFilter = null;
if (debounce)
PendingFilter = Scheduler.AddDelayed(perform, 250);
else
{
// if initial load is not yet finished, this will be run inline in loadBeatmapSets to ensure correct order of operation.
if (!BeatmapSetsLoaded)
PendingFilter = Schedule(perform);
else
perform();
}
2018-04-13 17:19:50 +08:00
void perform()
2016-11-25 17:14:56 +08:00
{
2018-07-18 09:12:14 +08:00
PendingFilter = null;
2018-04-13 17:19:50 +08:00
2017-12-16 15:27:39 +08:00
root.Filter(activeCriteria);
itemsCache.Invalidate();
if (alwaysResetScrollPosition || !Scroll.UserScrolling)
ScrollToSelected(true);
}
}
2018-04-13 17:19:50 +08:00
private void signalBeatmapsLoaded()
{
if (!BeatmapSetsLoaded)
{
BeatmapSetsChanged?.Invoke();
BeatmapSetsLoaded = true;
}
itemsCache.Invalidate();
}
2017-12-17 01:33:01 +08:00
private float? scrollTarget;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Scroll to the current <see cref="SelectedBeatmapInfo"/>.
/// </summary>
/// <param name="immediate">
/// Whether the scroll position should immediately be shifted to the target, delegating animation to visible panels.
/// This should be true for operations like filtering - where panels are changing visibility state - to avoid large jumps in animation.
/// </param>
public void ScrollToSelected(bool immediate = false) =>
pendingScrollOperation = immediate ? PendingScrollOperation.Immediate : PendingScrollOperation.Standard;
2018-04-13 17:19:50 +08:00
#region Button selection logic
2021-09-16 17:26:12 +08:00
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
2021-09-16 17:26:12 +08:00
switch (e.Action)
{
case GlobalAction.SelectNext:
case GlobalAction.SelectNextGroup:
SelectNext(1, e.Action == GlobalAction.SelectNextGroup);
return true;
case GlobalAction.SelectPrevious:
case GlobalAction.SelectPreviousGroup:
SelectNext(-1, e.Action == GlobalAction.SelectPreviousGroup);
return true;
}
2018-04-13 17:19:50 +08:00
return false;
}
2018-04-13 17:19:50 +08:00
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
#endregion
protected override bool OnInvalidate(Invalidation invalidation, InvalidationSource source)
{
// handles the vertical size of the carousel changing (ie. on window resize when aspect ratio has changed).
if (invalidation.HasFlagFast(Invalidation.DrawSize))
itemsCache.Invalidate();
return base.OnInvalidate(invalidation, source);
}
protected override void Update()
{
base.Update();
2018-04-13 17:19:50 +08:00
2020-10-12 13:46:51 +08:00
bool revalidateItems = !itemsCache.IsValid;
// First we iterate over all non-filtered carousel items and populate their
// vertical position data.
2020-10-12 13:46:51 +08:00
if (revalidateItems)
{
updateYPositions();
2018-04-13 17:19:50 +08:00
if (visibleItems.Count == 0)
{
noResultsPlaceholder.Filter = activeCriteria;
noResultsPlaceholder.Show();
}
else
noResultsPlaceholder.Hide();
}
// if there is a pending scroll action we apply it without animation and transfer the difference in position to the panels.
// this is intentionally applied before updating the visible range below, to avoid animating new items (sourced from pool) from locations off-screen, as it looks bad.
if (pendingScrollOperation != PendingScrollOperation.None)
updateScrollPosition();
// This data is consumed to find the currently displayable range.
// This is the range we want to keep drawables for, and should exceed the visible range slightly to avoid drawable churn.
var newDisplayRange = getDisplayRange();
// If the filtered items or visible range has changed, pooling requirements need to be checked.
// This involves fetching new items from the pool, returning no-longer required items.
if (revalidateItems || newDisplayRange != displayedRange)
2020-10-12 13:46:51 +08:00
{
displayedRange = newDisplayRange;
2018-04-13 17:19:50 +08:00
if (visibleItems.Count > 0)
{
var toDisplay = visibleItems.GetRange(displayedRange.first, displayedRange.last - displayedRange.first + 1);
2020-10-12 14:36:03 +08:00
foreach (var panel in Scroll.Children)
2020-10-13 13:23:29 +08:00
{
if (toDisplay.Remove(panel.Item))
{
// panel already displayed.
continue;
}
// panel loaded as drawable but not required by visible range.
// remove but only if too far off-screen
if (panel.Y + panel.DrawHeight < visibleUpperBound - distance_offscreen_before_unload || panel.Y > visibleBottomBound + distance_offscreen_before_unload)
expirePanelImmediately(panel);
2020-10-13 13:23:29 +08:00
}
2018-04-13 17:19:50 +08:00
// Add those items within the previously found index range that should be displayed.
foreach (var item in toDisplay)
{
var panel = setPool.Get(p => p.Item = item);
panel.Depth = item.CarouselYPosition;
panel.Y = item.CarouselYPosition;
Scroll.Add(panel);
}
}
}
2020-10-12 13:46:51 +08:00
// Update externally controlled state of currently visible items (e.g. x-offset and opacity).
// This is a per-frame update on all drawable panels.
foreach (DrawableCarouselItem item in Scroll.Children)
{
updateItem(item);
if (item is DrawableCarouselBeatmapSet set)
{
foreach (var diff in set.DrawableBeatmaps)
updateItem(diff, item);
}
}
}
2018-04-13 17:19:50 +08:00
private static void expirePanelImmediately(DrawableCarouselItem panel)
{
// may want a fade effect here (could be seen if a huge change happens, like a set with 20 difficulties becomes selected).
panel.ClearTransforms();
panel.Expire();
}
private readonly CarouselBoundsItem carouselBoundsItem = new CarouselBoundsItem();
private (int firstIndex, int lastIndex) getDisplayRange()
{
// Find index range of all items that should be on-screen
carouselBoundsItem.CarouselYPosition = visibleUpperBound - distance_offscreen_to_preload;
int firstIndex = visibleItems.BinarySearch(carouselBoundsItem);
if (firstIndex < 0) firstIndex = ~firstIndex;
carouselBoundsItem.CarouselYPosition = visibleBottomBound + distance_offscreen_to_preload;
int lastIndex = visibleItems.BinarySearch(carouselBoundsItem);
if (lastIndex < 0) lastIndex = ~lastIndex;
// as we can't be 100% sure on the size of individual carousel drawables,
// always play it safe and extend bounds by one.
firstIndex = Math.Max(0, firstIndex - 1);
lastIndex = Math.Clamp(lastIndex + 1, firstIndex, Math.Max(0, visibleItems.Count - 1));
return (firstIndex, lastIndex);
}
private CarouselBeatmapSet createCarouselSet(BeatmapSetInfo beatmapSet)
{
2022-01-20 16:50:17 +08:00
// This can be moved to the realm query if required using:
2022-01-20 17:36:20 +08:00
// .Filter("DeletePending == false && Protected == false && ANY Beatmaps.Hidden == false")
2022-01-20 16:50:17 +08:00
//
// As long as we are detaching though, it makes more sense to do it here as adding to the realm query has an overhead
// as seen at https://github.com/realm/realm-dotnet/discussions/2773#discussioncomment-2004275.
if (beatmapSet.Beatmaps.All(b => b.Hidden))
return null;
2018-04-13 17:19:50 +08:00
var set = new CarouselBeatmapSet(beatmapSet)
{
GetRecommendedBeatmap = beatmaps => GetRecommendedBeatmap?.Invoke(beatmaps)
};
2018-04-13 17:19:50 +08:00
2017-12-12 16:48:38 +08:00
foreach (var c in set.Beatmaps)
{
c.State.ValueChanged += state =>
2017-12-12 16:48:38 +08:00
{
if (state.NewValue == CarouselItemState.Selected)
2017-12-12 16:48:38 +08:00
{
selectedBeatmapSet = set;
SelectionChanged?.Invoke(c.BeatmapInfo);
2018-04-13 17:19:50 +08:00
itemsCache.Invalidate();
ScrollToSelected();
2017-12-12 16:48:38 +08:00
}
};
}
2018-04-13 17:19:50 +08:00
2017-12-12 16:48:38 +08:00
return set;
}
2018-04-13 17:19:50 +08:00
2020-10-12 17:11:41 +08:00
private const float panel_padding = 5;
/// <summary>
2017-12-12 16:48:38 +08:00
/// Computes the target Y positions for every item in the carousel.
/// </summary>
2017-12-12 16:48:38 +08:00
/// <returns>The Y position of the currently selected item.</returns>
private void updateYPositions()
{
visibleItems.Clear();
2018-04-13 17:19:50 +08:00
2019-07-26 14:22:29 +08:00
float currentY = visibleHalfHeight;
2018-04-13 17:19:50 +08:00
2017-12-17 01:33:01 +08:00
scrollTarget = null;
2018-04-13 17:19:50 +08:00
foreach (CarouselItem item in root.Items)
2017-12-12 16:48:38 +08:00
{
if (item.Filtered.Value)
continue;
switch (item)
{
case CarouselBeatmapSet set:
2017-12-17 01:33:01 +08:00
{
visibleItems.Add(set);
set.CarouselYPosition = currentY;
2020-10-12 17:32:29 +08:00
if (item.State.Value == CarouselItemState.Selected)
{
// scroll position at currentY makes the set panel appear at the very top of the carousel's screen space
// move down by half of visible height (height of the carousel's visible extent, including semi-transparent areas)
// then reapply the top semi-transparent area (because carousel's screen space starts below it)
scrollTarget = currentY + DrawableCarouselBeatmapSet.HEIGHT - visibleHalfHeight + BleedTop;
foreach (var b in set.Beatmaps)
{
if (!b.Visible)
continue;
2020-10-12 17:32:29 +08:00
if (b.State.Value == CarouselItemState.Selected)
{
scrollTarget += b.TotalHeight / 2;
break;
}
scrollTarget += b.TotalHeight;
2020-10-12 17:32:29 +08:00
}
}
2020-10-12 17:11:41 +08:00
currentY += set.TotalHeight + panel_padding;
break;
2017-12-17 01:33:01 +08:00
}
}
}
2018-04-13 17:19:50 +08:00
2019-07-26 14:22:29 +08:00
currentY += visibleHalfHeight;
Scroll.ScrollContent.Height = currentY;
2018-04-13 17:19:50 +08:00
itemsCache.Validate();
// update and let external consumers know about selection loss.
if (BeatmapSetsLoaded)
{
bool selectionLost = selectedBeatmapSet != null && selectedBeatmapSet.State.Value != CarouselItemState.Selected;
2018-04-13 17:19:50 +08:00
if (selectionLost)
{
selectedBeatmapSet = null;
SelectionChanged?.Invoke(null);
}
}
2016-11-21 14:59:46 +08:00
}
2018-04-13 17:19:50 +08:00
private bool firstScroll = true;
private void updateScrollPosition()
{
if (scrollTarget != null)
{
if (firstScroll)
{
// reduce movement when first displaying the carousel.
Scroll.ScrollTo(scrollTarget.Value - 200, false);
firstScroll = false;
}
switch (pendingScrollOperation)
{
case PendingScrollOperation.Standard:
Scroll.ScrollTo(scrollTarget.Value);
break;
case PendingScrollOperation.Immediate:
// in order to simplify animation logic, rather than using the animated version of ScrollTo,
// we take the difference in scroll height and apply to all visible panels.
// this avoids edge cases like when the visible panels is reduced suddenly, causing ScrollContainer
// to enter clamp-special-case mode where it animates completely differently to normal.
float scrollChange = scrollTarget.Value - Scroll.Current;
Scroll.ScrollTo(scrollTarget.Value, false);
foreach (var i in Scroll.Children)
i.Y += scrollChange;
break;
}
pendingScrollOperation = PendingScrollOperation.None;
}
}
2018-04-13 17:19:50 +08:00
/// <summary>
2017-12-12 16:48:38 +08:00
/// Computes the x-offset of currently visible items. Makes the carousel appear round.
/// </summary>
/// <param name="dist">
/// Vertical distance from the center of the carousel container
/// ranging from -1 to 1.
/// </param>
/// <param name="halfHeight">Half the height of the carousel container.</param>
private static float offsetX(float dist, float halfHeight)
{
// The radius of the circle the carousel moves on.
const float circle_radius = 3;
float discriminant = MathF.Max(0, circle_radius * circle_radius - dist * dist);
float x = (circle_radius - MathF.Sqrt(discriminant)) * halfHeight;
2018-04-13 17:19:50 +08:00
return 125 + x;
}
2018-04-13 17:19:50 +08:00
/// <summary>
2017-12-12 16:48:38 +08:00
/// Update a item's x position and multiplicative alpha based on its y position and
/// the current scroll position.
/// </summary>
/// <param name="item">The item to be updated.</param>
/// <param name="parent">For nested items, the parent of the item to be updated.</param>
private void updateItem(DrawableCarouselItem item, DrawableCarouselItem parent = null)
{
Vector2 posInScroll = Scroll.ScrollContent.ToLocalSpace(item.Header.ScreenSpaceDrawQuad.Centre);
float itemDrawY = posInScroll.Y - visibleUpperBound;
2019-07-26 14:22:29 +08:00
float dist = Math.Abs(1f - itemDrawY / visibleHalfHeight);
2018-04-13 17:19:50 +08:00
// adjusting the item's overall X position can cause it to become masked away when
// child items (difficulties) are still visible.
item.Header.X = offsetX(dist, visibleHalfHeight) - (parent?.X ?? 0);
2018-04-13 17:19:50 +08:00
// We are applying a multiplicative alpha (which is internally done by nesting an
// additional container and setting that container's alpha) such that we can
// layer alpha transformations on top.
item.SetMultiplicativeAlpha(Math.Clamp(1.75f - 1.5f * dist, 0, 1));
}
2018-04-13 17:19:50 +08:00
private enum PendingScrollOperation
{
None,
Standard,
Immediate,
}
/// <summary>
/// A carousel item strictly used for binary search purposes.
/// </summary>
private class CarouselBoundsItem : CarouselItem
{
public override DrawableCarouselItem CreateDrawableRepresentation() =>
throw new NotImplementedException();
}
2018-04-02 14:23:58 +08:00
private class CarouselRoot : CarouselGroupEagerSelect
{
private readonly BeatmapCarousel carousel;
2018-04-13 17:19:50 +08:00
public readonly Dictionary<Guid, CarouselBeatmapSet> BeatmapSetsByID = new Dictionary<Guid, CarouselBeatmapSet>();
2018-04-02 14:23:58 +08:00
public CarouselRoot(BeatmapCarousel carousel)
{
// root should always remain selected. if not, PerformSelection will not be called.
State.Value = CarouselItemState.Selected;
2022-06-24 20:25:23 +08:00
State.ValueChanged += _ => State.Value = CarouselItemState.Selected;
2018-04-02 14:23:58 +08:00
this.carousel = carousel;
}
2018-04-13 17:19:50 +08:00
public override void AddItem(CarouselItem i)
{
CarouselBeatmapSet set = (CarouselBeatmapSet)i;
2022-01-21 14:29:21 +08:00
BeatmapSetsByID.Add(set.BeatmapSet.ID, set);
base.AddItem(i);
}
public CarouselBeatmapSet RemoveChild(Guid beatmapSetID)
{
if (BeatmapSetsByID.TryGetValue(beatmapSetID, out var carouselBeatmapSet))
{
RemoveItem(carouselBeatmapSet);
return carouselBeatmapSet;
}
return null;
}
public override void RemoveItem(CarouselItem i)
{
CarouselBeatmapSet set = (CarouselBeatmapSet)i;
BeatmapSetsByID.Remove(set.BeatmapSet.ID);
base.RemoveItem(i);
}
2018-04-02 14:23:58 +08:00
protected override void PerformSelection()
{
if (LastSelected == null || LastSelected.Filtered.Value)
carousel?.SelectNextRandom();
2018-04-02 14:23:58 +08:00
else
base.PerformSelection();
}
}
protected class CarouselScrollContainer : UserTrackingScrollContainer<DrawableCarouselItem>
{
private bool rightMouseScrollBlocked;
public CarouselScrollContainer()
{
// size is determined by the carousel itself, due to not all content necessarily being loaded.
ScrollContent.AutoSizeAxes = Axes.None;
// the scroll container may get pushed off-screen by global screen changes, but we still want panels to display outside of the bounds.
Masking = false;
}
protected override bool OnMouseDown(MouseDownEvent e)
{
if (e.Button == MouseButton.Right)
{
// we need to block right click absolute scrolling when hovering a carousel item so context menus can display.
// this can be reconsidered when we have an alternative to right click scrolling.
if (GetContainingInputManager().HoveredDrawables.OfType<DrawableCarouselItem>().Any())
{
rightMouseScrollBlocked = true;
return false;
}
}
rightMouseScrollBlocked = false;
return base.OnMouseDown(e);
}
protected override bool OnDragStart(DragStartEvent e)
{
if (rightMouseScrollBlocked)
return false;
return base.OnDragStart(e);
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
subscriptionSets?.Dispose();
2022-01-12 00:03:59 +08:00
subscriptionDeletedSets?.Dispose();
subscriptionBeatmaps?.Dispose();
2022-01-12 00:03:59 +08:00
subscriptionHiddenBeatmaps?.Dispose();
}
}
}