2019-01-24 16:43:03 +08: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.
2018-04-13 17:19:50 +08:00
2017-02-02 16:33:39 +08:00
using System ;
using System.Collections.Generic ;
2024-08-27 17:13:52 +08:00
using System.Collections.Specialized ;
2017-03-04 15:54:14 +08:00
using System.Diagnostics ;
2020-10-13 17:18:22 +08:00
using System.Linq ;
2024-08-28 15:17:03 +08:00
using System.Threading ;
2017-03-17 18:12:15 +08:00
using osu.Framework.Allocation ;
2022-01-28 12:27:12 +08:00
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 ;
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 ;
2020-03-02 17:55:28 +08:00
using osu.Framework.Input.Bindings ;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events ;
2021-01-02 21:05:41 +08:00
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 ;
2024-08-27 17:13:52 +08:00
using osu.Game.Extensions ;
2017-07-21 18:13:53 +08:00
using osu.Game.Graphics.Containers ;
2020-03-02 17:55:28 +08:00
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
2016-11-21 03:34:16 +08:00
namespace osu.Game.Screens.Select
{
2020-03-02 17:55:28 +08:00
public partial class BeatmapCarousel : CompositeDrawable , IKeyBindingHandler < GlobalAction >
2016-11-21 03:34:16 +08:00
{
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 ; }
2019-07-26 12:07:28 +08:00
2017-12-13 18:56:16 +08:00
/// <summary>
2023-04-11 02:28:23 +08:00
/// Triggered when <see cref="BeatmapSets"/> finish loading, or are subsequently changed.
2017-12-13 18:56:16 +08:00
/// </summary>
2022-09-07 13:04:51 +08:00
public Action ? BeatmapSetsChanged ;
2018-04-13 17:19:50 +08:00
2023-03-03 14:25:55 +08:00
/// <summary>
/// Triggered after filter conditions have finished being applied to the model hierarchy.
/// </summary>
public Action ? FilterApplied ;
2017-12-13 18:56:16 +08:00
/// <summary>
/// The currently selected beatmap.
/// </summary>
2022-09-07 13:04:51 +08:00
public BeatmapInfo ? SelectedBeatmapInfo = > selectedBeatmap ? . BeatmapInfo ;
2018-04-13 17:19:50 +08:00
2022-09-07 13:04:51 +08:00
private CarouselBeatmap ? selectedBeatmap = > selectedBeatmapSet ? . Beatmaps . FirstOrDefault ( s = > s . State . Value = = CarouselItemState . Selected ) ;
2018-04-13 17:19:50 +08:00
2023-03-03 14:25:55 +08:00
/// <summary>
/// The total count of non-filtered beatmaps displayed.
/// </summary>
2023-12-18 19:44:08 +08:00
public int CountDisplayed = > beatmapSets . Where ( s = > ! s . Filtered . Value ) . Sum ( s = > s . TotalItemsNotFiltered ) ;
2023-03-03 14:25:55 +08:00
2017-12-14 11:59:35 +08:00
/// <summary>
/// The currently selected beatmap set.
/// </summary>
2022-09-07 13:04:51 +08:00
public BeatmapSetInfo ? SelectedBeatmapSet = > selectedBeatmapSet ? . BeatmapSet ;
2018-04-13 17:19:50 +08:00
2020-04-11 15:58:13 +08:00
/// <summary>
/// A function to optionally decide on a recommended difficulty from a beatmap set.
/// </summary>
2023-01-09 02:02:48 +08:00
public Func < IEnumerable < BeatmapInfo > , BeatmapInfo ? > ? GetRecommendedBeatmap ;
2020-04-11 15:58:13 +08:00
2022-09-07 13:04:51 +08:00
private CarouselBeatmapSet ? selectedBeatmapSet ;
2018-04-13 17:19:50 +08:00
2017-12-13 18:56:16 +08:00
/// <summary>
2021-10-02 11:44:22 +08:00
/// Raised when the <see cref="SelectedBeatmapInfo"/> is changed.
2017-12-13 18:56:16 +08:00
/// </summary>
2022-09-07 13:04:51 +08:00
public Action < BeatmapInfo ? > ? SelectionChanged ;
2018-04-13 17:19:50 +08:00
2018-09-26 13:01:15 +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>
2024-01-04 17:48:13 +08:00
private const float distance_offscreen_before_unload = 2048 ;
2020-10-13 18:10:35 +08:00
/// <summary>
/// Extend the range to update positions / retrieve pooled drawables outside of visible range.
/// </summary>
2024-01-04 17:48:13 +08:00
private const float distance_offscreen_to_preload = 768 ;
2020-10-13 18:10:35 +08:00
2017-12-27 11:30:30 +08:00
/// <summary>
2019-03-21 19:51:06 +08:00
/// Whether carousel items have completed asynchronously loaded.
2017-12-27 11:30:30 +08:00
/// </summary>
2019-03-21 19:51:06 +08:00
public bool BeatmapSetsLoaded { get ; private set ; }
2018-04-13 17:19:50 +08:00
2024-01-04 18:13:36 +08:00
[Cached]
2020-11-26 17:28:52 +08:00
protected readonly CarouselScrollContainer Scroll ;
2019-08-15 13:00:12 +08:00
2024-08-27 16:37:15 +08:00
[Resolved]
2024-08-27 17:13:52 +08:00
private RealmAccess realm { get ; set ; } = null ! ;
[Resolved]
private DetachedBeatmapStore ? detachedBeatmapStore { get ; set ; }
2024-08-28 19:06:44 +08:00
private IBindableList < BeatmapSetInfo > ? detachedBeatmapSets ;
2024-08-27 16:37:15 +08:00
2022-06-07 15:32:15 +08:00
private readonly NoResultsPlaceholder noResultsPlaceholder ;
2022-07-21 15:06:06 +08:00
private IEnumerable < CarouselBeatmapSet > beatmapSets = > root . Items . OfType < CarouselBeatmapSet > ( ) ;
2018-04-13 17:19:50 +08:00
2024-08-30 17:44:04 +08:00
internal IEnumerable < BeatmapSetInfo > BeatmapSets
2017-03-17 18:12:15 +08:00
{
2018-08-29 00:42:25 +08:00
get = > beatmapSets . Select ( g = > g . BeatmapSet ) ;
2022-01-10 13:52:59 +08:00
set
{
2024-08-28 18:35:28 +08:00
if ( LoadState ! = LoadState . NotLoaded )
throw new InvalidOperationException ( "If not using a realm source, beatmap sets must be set before load." ) ;
detachedBeatmapSets = new BindableList < BeatmapSetInfo > ( value ) ;
Schedule ( loadNewRoot ) ;
2022-01-10 13:52:59 +08:00
}
2018-08-29 00:42:25 +08:00
}
2024-08-28 18:35:28 +08:00
private void loadNewRoot ( )
2018-08-29 00:42:25 +08:00
{
2024-09-07 21:05:33 +08:00
beatmapsSplitOut = activeCriteria . SplitOutDifficulties ;
2024-08-28 15:30:09 +08:00
// Ensure no changes are made to the list while we are initialising items.
// We'll catch up on changes via subscriptions anyway.
2024-08-28 19:06:44 +08:00
BeatmapSetInfo [ ] loadableSets = detachedBeatmapSets ! . ToArray ( ) ;
2024-08-28 15:30:09 +08:00
Fix selection being dropped when changing carousel sort mode from difficulty sort
Closes https://github.com/ppy/osu/issues/29738.
This "regressed" in https://github.com/ppy/osu/pull/29639, but if I
didn't know better, I'd go as far as saying that this looks like a .NET
bug, because the fact that PR broke it looks not sane.
The TL;DR on this is that before the pull in question, the offending
`.Contains()` check that this commit modifies was called on a
`List<BeatmapSetInfo>`. The pull changed the collection type to
`BeatmapSetInfo[]`. That said, the call is a LINQ call, so all good,
right?
Not really. First off, the default overload resolution order means that
the previous code would call `List<T>.Contains()`, and not
`Enumerable.Contains<T>()`. Then again, why would that matter? In both
cases `T` is still `BeatmapSetInfo`, right? Well... about that...
It is difficult to tell for sure what precisely is happening here,
because of what looks like runtime magic. The end *symptom* is that the
old code ended up calling `Array<BeatmapSetInfo>.IndexOf()`, and the new
code ends up calling... `Array<object>.IndexOf()`.
So while yes, `BeatmapSetInfo` implements `IEquatable` and
the expectation would be that `Equals<BeatmapSetInfo>()` should be
getting called, the type elision to `object` means that we're back to
reference equality semantics, because that's what
`EqualityComparer.Default<object>` is.
A five-minute github search across dotnet/runtime yields this:
https://github.com/dotnet/runtime/blob/c4792a228ea36792b90f87ddf7fce2477e827822/src/coreclr/vm/array.cpp#L984-L990
Now again, if I didn't know better, I'd see that "OPTIMIZATION:"
comment, see what transpired in this scenario, and call that
optimisation invalid, because it changes semantics. But I *probably*
know that the dotnet team knows better and am probably just going to
take it for what it is, because blame on that code looks to be years
old and it's probably not a new behaviour. (I haven't tested empirically
if it is.)
Instead the fix is just to tell the `.Contains()` method to use the
correct comparer.
2024-09-08 21:51:59 +08:00
if ( selectedBeatmapSet ! = null & & ! loadableSets . Contains ( selectedBeatmapSet . BeatmapSet , EqualityComparer < BeatmapSetInfo > . Default ) )
2023-08-22 17:40:34 +08:00
selectedBeatmapSet = null ;
2023-08-24 23:52:54 +08:00
var selectedBeatmapBefore = selectedBeatmap ? . BeatmapInfo ;
2023-08-22 17:40:34 +08:00
2018-08-29 00:42:25 +08:00
CarouselRoot newRoot = new CarouselRoot ( this ) ;
2023-08-22 16:31:19 +08:00
if ( beatmapsSplitOut )
{
2024-08-28 18:35:28 +08:00
var carouselBeatmapSets = loadableSets . SelectMany ( s = > s . Beatmaps ) . Select ( b = >
2023-08-22 16:31:19 +08:00
{
2023-08-22 17:38:23 +08:00
return createCarouselSet ( new BeatmapSetInfo ( new [ ] { b } )
{
ID = b . BeatmapSet ! . ID ,
2023-09-06 14:25:19 +08:00
OnlineID = b . BeatmapSet ! . OnlineID ,
Status = b . BeatmapSet ! . Status ,
2023-08-22 17:38:23 +08:00
} ) ;
2023-08-22 16:31:19 +08:00
} ) . OfType < CarouselBeatmapSet > ( ) ;
newRoot . AddItems ( carouselBeatmapSets ) ;
}
else
{
2024-08-28 18:35:28 +08:00
var carouselBeatmapSets = loadableSets . Select ( createCarouselSet ) . OfType < CarouselBeatmapSet > ( ) ;
2023-08-22 17:40:34 +08:00
2023-08-22 16:31:19 +08:00
newRoot . AddItems ( carouselBeatmapSets ) ;
}
2018-04-13 17:19:50 +08:00
2019-06-30 21:23:48 +08:00
root = newRoot ;
2024-08-28 17:22:05 +08:00
root . Filter ( activeCriteria ) ;
2022-01-20 20:58:16 +08:00
2020-11-26 17:28:52 +08:00
Scroll . Clear ( false ) ;
2019-06-30 21:23:48 +08:00
itemsCache . Invalidate ( ) ;
2020-11-27 12:54:36 +08:00
ScrollToSelected ( ) ;
2019-06-30 21:23:48 +08:00
2023-08-22 17:40:34 +08:00
// Restore selection
2023-08-24 23:52:54 +08:00
if ( selectedBeatmapBefore ! = null & & newRoot . BeatmapSetsByID . TryGetValue ( selectedBeatmapBefore . BeatmapSet ! . ID , out var newSelectionCandidates ) )
2023-08-22 17:40:34 +08:00
{
2023-08-24 23:52:54 +08:00
CarouselBeatmap ? found = newSelectionCandidates . SelectMany ( s = > s . Beatmaps ) . SingleOrDefault ( b = > b . BeatmapInfo . ID = = selectedBeatmapBefore . ID ) ;
2023-08-22 17:40:34 +08:00
if ( found ! = null )
found . State . Value = CarouselItemState . Selected ;
}
2024-08-27 17:13:52 +08:00
Schedule ( ( ) = >
{
invalidateAfterChange ( ) ;
BeatmapSetsLoaded = true ;
} ) ;
2017-03-17 18:12:15 +08:00
}
2018-04-13 17:19:50 +08:00
2020-10-12 13:23:18 +08:00
private readonly List < CarouselItem > visibleItems = new List < CarouselItem > ( ) ;
2019-08-09 18:12:29 +08:00
private readonly Cached itemsCache = new Cached ( ) ;
2020-11-27 12:54:36 +08:00
private PendingScrollOperation pendingScrollOperation = PendingScrollOperation . None ;
2018-04-13 17:19:50 +08:00
2018-04-13 18:50:37 +08:00
public Bindable < bool > RightClickScrollingEnabled = new Bindable < bool > ( ) ;
2017-12-14 11:59:35 +08:00
public Bindable < RandomSelectAlgorithm > RandomAlgorithm = new Bindable < RandomSelectAlgorithm > ( ) ;
private readonly List < CarouselBeatmapSet > previouslyVisitedRandomSets = new List < CarouselBeatmapSet > ( ) ;
2023-06-15 16:22:11 +08:00
private readonly List < CarouselBeatmap > randomSelectedBeatmaps = new List < CarouselBeatmap > ( ) ;
2018-04-13 17:19:50 +08:00
2018-04-02 14:16:10 +08:00
private CarouselRoot root ;
2020-04-11 15:41:11 +08:00
2022-09-07 13:04:51 +08:00
private IDisposable ? subscriptionBeatmaps ;
2020-05-19 15:44:22 +08:00
2020-10-12 14:36:03 +08:00
private readonly DrawablePool < DrawableCarouselBeatmapSet > setPool = new DrawablePool < DrawableCarouselBeatmapSet > ( 100 ) ;
2022-09-07 13:04:51 +08:00
private Sample ? spinSample ;
private Sample ? randomSelectSample ;
2022-01-28 12:27:12 +08:00
private int visibleSetsCount ;
2024-11-11 15:59:02 +08:00
public BeatmapCarousel ( FilterCriteria initialCriteria )
2017-01-18 08:18:15 +08:00
{
2018-04-02 14:16:10 +08:00
root = new CarouselRoot ( this ) ;
2024-08-12 19:28:21 +08:00
InternalChild = new Container
2017-01-18 08:18:15 +08:00
{
2019-08-15 13:00:12 +08:00
RelativeSizeAxes = Axes . Both ,
2024-01-25 14:21:19 +08:00
Padding = new MarginPadding
{
// Avoid clash between scrollbar and osu! logo.
Top = 10 ,
Bottom = 100 ,
} ,
2020-11-26 17:28:52 +08:00
Children = new Drawable [ ]
2017-09-14 14:41:32 +08:00
{
2020-11-26 17:28:52 +08:00
setPool ,
Scroll = new CarouselScrollContainer
2019-08-15 13:00:12 +08:00
{
2020-11-26 17:28:52 +08:00
RelativeSizeAxes = Axes . Both ,
2022-06-07 15:32:15 +08:00
} ,
noResultsPlaceholder = new NoResultsPlaceholder ( )
2017-09-14 14:41:32 +08:00
}
2017-12-14 12:49:43 +08:00
} ;
2024-08-28 17:22:05 +08:00
2024-11-11 15:59:02 +08:00
activeCriteria = initialCriteria ;
2017-01-18 08:18:15 +08:00
}
2018-04-13 17:19:50 +08:00
2021-12-17 18:01:19 +08:00
[BackgroundDependencyLoader]
2024-08-28 18:19:04 +08:00
private void load ( OsuConfigManager config , AudioManager audio , CancellationToken ? cancellationToken )
2017-12-18 11:30:39 +08:00
{
2022-01-28 12:27:12 +08:00
spinSample = audio . Samples . Get ( "SongSelect/random-spin" ) ;
2022-02-04 14:42:52 +08:00
randomSelectSample = audio . Samples . Get ( @"SongSelect/select-random" ) ;
2022-01-28 12:27:12 +08:00
2017-12-18 11:30:39 +08:00
config . BindWith ( OsuSetting . RandomSelectAlgorithm , RandomAlgorithm ) ;
2018-04-18 18:26:54 +08:00
config . BindWith ( OsuSetting . SongSelectRightMouseScroll , RightClickScrollingEnabled ) ;
2018-04-13 18:50:37 +08:00
2020-11-26 17:28:52 +08:00
RightClickScrollingEnabled . ValueChanged + = enabled = > Scroll . RightMouseScrollbar = enabled . NewValue ;
2018-04-13 18:50:37 +08:00
RightClickScrollingEnabled . TriggerChange ( ) ;
2019-06-30 21:23:48 +08:00
2024-08-28 19:06:44 +08:00
if ( detachedBeatmapStore ! = null & & detachedBeatmapSets = = null )
2022-01-19 16:47:46 +08:00
{
2024-07-08 18:29:03 +08:00
// This is performing an unnecessary second lookup on realm (in addition to the subscription), but for performance reasons
// we require it to be separate: the subscription's initial callback (with `ChangeSet` of `null`) will run on the update
// thread. If we attempt to detach beatmaps in this callback the game will fall over (it takes time).
2024-08-28 18:19:04 +08:00
detachedBeatmapSets = detachedBeatmapStore . GetDetachedBeatmaps ( cancellationToken ) ;
2024-08-27 17:13:52 +08:00
detachedBeatmapSets . BindCollectionChanged ( beatmapSetsChanged ) ;
2024-08-28 18:35:28 +08:00
loadNewRoot ( ) ;
2022-01-19 16:47:46 +08:00
}
2021-11-08 16:41:42 +08:00
}
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
2020-01-29 15:51:14 +08:00
2022-01-25 12:09:47 +08:00
subscriptionBeatmaps = realm . RegisterForNotifications ( r = > r . All < BeatmapInfo > ( ) . Where ( b = > ! b . Hidden ) , beatmapsChanged ) ;
2017-12-18 11:30:39 +08:00
}
2018-04-13 17:19:50 +08:00
2024-08-27 17:13:52 +08:00
private readonly HashSet < BeatmapSetInfo > setsRequiringUpdate = new HashSet < BeatmapSetInfo > ( ) ;
private readonly HashSet < BeatmapSetInfo > setsRequiringRemoval = new HashSet < BeatmapSetInfo > ( ) ;
2024-07-08 18:29:03 +08:00
2024-08-27 17:13:52 +08:00
private void beatmapSetsChanged ( object? beatmaps , NotifyCollectionChangedEventArgs changed )
2017-10-18 17:36:35 +08:00
{
2024-08-28 15:19:17 +08:00
IEnumerable < BeatmapSetInfo > ? newBeatmapSets = changed . NewItems ? . Cast < BeatmapSetInfo > ( ) ;
2024-08-27 17:13:52 +08:00
switch ( changed . Action )
{
case NotifyCollectionChangedAction . Add :
2024-08-28 15:19:17 +08:00
HashSet < Guid > newBeatmapSetIDs = newBeatmapSets ! . Select ( s = > s . ID ) . ToHashSet ( ) ;
2024-08-27 17:13:52 +08:00
setsRequiringRemoval . RemoveWhere ( s = > newBeatmapSetIDs . Contains ( s . ID ) ) ;
2024-08-28 15:19:17 +08:00
setsRequiringUpdate . AddRange ( newBeatmapSets ! ) ;
2024-08-27 17:13:52 +08:00
break ;
case NotifyCollectionChangedAction . Remove :
2024-08-28 15:19:17 +08:00
IEnumerable < BeatmapSetInfo > oldBeatmapSets = changed . OldItems ! . Cast < BeatmapSetInfo > ( ) ;
HashSet < Guid > oldBeatmapSetIDs = oldBeatmapSets . Select ( s = > s . ID ) . ToHashSet ( ) ;
2024-08-27 17:13:52 +08:00
setsRequiringUpdate . RemoveWhere ( s = > oldBeatmapSetIDs . Contains ( s . ID ) ) ;
setsRequiringRemoval . AddRange ( oldBeatmapSets ) ;
break ;
case NotifyCollectionChangedAction . Replace :
2024-08-28 15:19:17 +08:00
setsRequiringUpdate . AddRange ( newBeatmapSets ! ) ;
2024-08-27 17:13:52 +08:00
break ;
case NotifyCollectionChangedAction . Move :
2024-08-28 15:19:17 +08:00
setsRequiringUpdate . AddRange ( newBeatmapSets ! ) ;
2024-08-27 17:13:52 +08:00
break ;
case NotifyCollectionChangedAction . Reset :
setsRequiringRemoval . Clear ( ) ;
setsRequiringUpdate . Clear ( ) ;
2024-08-28 18:35:28 +08:00
loadNewRoot ( ) ;
2024-08-27 17:13:52 +08:00
break ;
}
2024-07-08 18:29:03 +08:00
Scheduler . AddOnce ( processBeatmapChanges ) ;
}
// All local operations must be scheduled.
//
// If we don't schedule, beatmaps getting changed while song select is suspended (ie. last played being updated)
// will cause unexpected sounds and operations to occur in the background.
private void processBeatmapChanges ( )
{
try
2022-07-27 12:30:38 +08:00
{
2024-10-22 16:53:34 +08:00
// To handle the beatmap update flow, attempt to track selection changes across delete-insert transactions.
// When an update occurs, the previous beatmap set is either soft or hard deleted.
// Check if the current selection was potentially deleted by re-querying its validity.
bool selectedSetMarkedDeleted = SelectedBeatmapSet ! = null & & fetchFromID ( SelectedBeatmapSet . ID ) ? . DeletePending ! = false ;
2024-08-27 17:13:52 +08:00
foreach ( var set in setsRequiringRemoval ) removeBeatmapSet ( set . ID ) ;
2022-07-27 12:30:38 +08:00
2024-08-27 17:13:52 +08:00
foreach ( var set in setsRequiringUpdate ) updateBeatmapSet ( set ) ;
2022-08-16 15:21:35 +08:00
2024-07-08 18:29:03 +08:00
if ( setsRequiringRemoval . Count > 0 & & SelectedBeatmapInfo ! = null )
{
// If SelectedBeatmapInfo is non-null, the set should also be non-null.
Debug . Assert ( SelectedBeatmapSet ! = null ) ;
2022-07-27 12:30:38 +08:00
2024-07-08 18:29:03 +08:00
if ( selectedSetMarkedDeleted & & setsRequiringUpdate . Any ( ) )
{
// If it is no longer valid, make the bold assumption that an updated version will be available in the modified/inserted indices.
// This relies on the full update operation being in a single transaction, so please don't change that.
foreach ( var set in setsRequiringUpdate )
2023-12-20 16:31:08 +08:00
{
2024-08-27 17:13:52 +08:00
foreach ( var beatmapInfo in set . Beatmaps )
2022-07-27 12:30:38 +08:00
{
2024-07-08 18:29:03 +08:00
if ( ! ( ( IBeatmapMetadataInfo ) beatmapInfo . Metadata ) . Equals ( SelectedBeatmapInfo . Metadata ) ) continue ;
// Best effort matching. We can't use ID because in the update flow a new version will get its own GUID.
if ( beatmapInfo . DifficultyName = = SelectedBeatmapInfo . DifficultyName )
2023-12-20 16:31:08 +08:00
{
2024-07-08 18:29:03 +08:00
SelectBeatmap ( beatmapInfo ) ;
return ;
2023-12-20 16:31:08 +08:00
}
2022-07-27 12:30:38 +08:00
}
}
2024-07-08 18:29:03 +08:00
// If a direct selection couldn't be made, it's feasible that the difficulty name (or beatmap metadata) changed.
// Let's attempt to follow set-level selection anyway.
2024-08-27 17:13:52 +08:00
SelectBeatmap ( setsRequiringUpdate . First ( ) . Beatmaps . First ( ) ) ;
2022-07-27 12:30:38 +08:00
}
}
2024-07-08 18:29:03 +08:00
}
finally
{
BeatmapSetsLoaded = true ;
invalidateAfterChange ( ) ;
}
setsRequiringRemoval . Clear ( ) ;
setsRequiringUpdate . Clear ( ) ;
2024-07-09 16:04:51 +08:00
BeatmapSetInfo ? fetchFromID ( Guid id ) = > realm . Realm . Find < BeatmapSetInfo > ( id ) ;
2021-11-08 16:41:42 +08:00
}
2023-07-06 12:37:42 +08:00
private void beatmapsChanged ( IRealmCollection < BeatmapInfo > sender , ChangeSet ? changes )
2021-11-08 16:41:42 +08:00
{
// we only care about actual changes in hidden status.
if ( changes = = null )
return ;
2023-12-18 19:09:09 +08:00
bool changed = false ;
2022-01-12 00:03:59 +08:00
foreach ( int i in changes . InsertedIndices )
2022-01-20 20:58:16 +08:00
{
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.
2023-08-22 16:31:19 +08:00
if ( root . BeatmapSetsByID . TryGetValue ( beatmapSet . ID , out var existingSets )
& & existingSets . SelectMany ( s = > s . Beatmaps ) . All ( b = > b . BeatmapInfo . ID ! = beatmapInfo . ID ) )
2022-01-20 20:58:16 +08:00
{
2024-08-30 17:50:00 +08:00
updateBeatmapSet ( beatmapSet . Detach ( ) ) ;
2023-12-18 19:09:09 +08:00
changed = true ;
2022-01-20 20:58:16 +08:00
}
}
2023-12-18 19:09:09 +08:00
if ( changed )
2023-12-18 19:10:31 +08:00
invalidateAfterChange ( ) ;
2021-11-08 16:41:42 +08:00
}
2020-03-12 14:26:22 +08:00
2023-12-18 19:09:09 +08:00
public void RemoveBeatmapSet ( BeatmapSetInfo beatmapSet ) = > Schedule ( ( ) = >
{
2022-01-20 20:58:16 +08:00
removeBeatmapSet ( beatmapSet . ID ) ;
2023-12-18 19:10:31 +08:00
invalidateAfterChange ( ) ;
2023-12-18 19:09:09 +08:00
} ) ;
2018-04-13 17:19:50 +08:00
2023-12-18 19:09:09 +08:00
private void removeBeatmapSet ( Guid beatmapSetID )
2022-01-20 20:58:16 +08:00
{
2023-08-22 16:31:19 +08:00
if ( ! root . BeatmapSetsByID . TryGetValue ( beatmapSetID , out var existingSets ) )
2019-07-25 11:18:18 +08:00
return ;
2018-04-13 17:19:50 +08:00
2023-08-22 16:31:19 +08:00
foreach ( var set in existingSets )
{
foreach ( var beatmap in set . Beatmaps )
randomSelectedBeatmaps . Remove ( beatmap ) ;
previouslyVisitedRandomSets . Remove ( set ) ;
2023-06-15 16:22:11 +08:00
2023-08-22 16:31:19 +08:00
root . RemoveItem ( set ) ;
}
2023-12-18 19:09:09 +08:00
}
2023-06-15 16:22:11 +08:00
2024-08-27 17:13:52 +08:00
public void UpdateBeatmapSet ( BeatmapSetInfo beatmapSet ) = > Schedule ( ( ) = >
2023-12-18 19:09:09 +08:00
{
2024-08-27 17:13:52 +08:00
updateBeatmapSet ( beatmapSet ) ;
invalidateAfterChange ( ) ;
} ) ;
2018-04-13 17:19:50 +08:00
2023-12-18 19:09:09 +08:00
private void updateBeatmapSet ( BeatmapSetInfo beatmapSet )
2017-07-28 13:22:14 +08:00
{
2023-12-20 18:09:07 +08:00
var newSets = new List < CarouselBeatmapSet > ( ) ;
2018-04-13 17:19:50 +08:00
2023-08-22 17:38:43 +08:00
if ( beatmapsSplitOut )
2019-06-26 10:40:33 +08:00
{
2023-08-22 17:38:43 +08:00
foreach ( var beatmap in beatmapSet . Beatmaps )
{
var newSet = createCarouselSet ( new BeatmapSetInfo ( new [ ] { beatmap } )
{
2023-08-28 16:02:30 +08:00
ID = beatmapSet . ID ,
2023-09-06 14:25:19 +08:00
OnlineID = beatmapSet . OnlineID ,
Status = beatmapSet . Status ,
2023-08-22 17:38:43 +08:00
} ) ;
2018-04-13 17:19:50 +08:00
2023-08-22 17:38:43 +08:00
if ( newSet ! = null )
2023-08-28 16:06:26 +08:00
newSets . Add ( newSet ) ;
}
2023-08-22 17:38:43 +08:00
}
else
{
var newSet = createCarouselSet ( beatmapSet ) ;
if ( newSet ! = null )
2023-12-20 18:09:07 +08:00
newSets . Add ( newSet ) ;
}
2023-08-22 17:38:43 +08:00
2023-12-20 18:09:07 +08:00
var removedSets = root . ReplaceItem ( beatmapSet , newSets ) ;
// If we don't remove these 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.
foreach ( var removedSet in removedSets )
{
var removedDrawable = Scroll . FirstOrDefault ( c = > c . Item = = removedSet ) ;
if ( removedDrawable ! = null )
expirePanelImmediately ( removedDrawable ) ;
2022-01-20 20:58:16 +08:00
}
2023-12-18 19:09:09 +08:00
}
2018-04-13 17:19:50 +08:00
2018-03-09 18:21:00 +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>
2018-03-09 22:09:28 +08:00
/// <param name="bypassFilters">Whether to select the beatmap even if it is filtered (i.e., not visible on carousel).</param>
2018-03-09 18:52:59 +08:00
/// <returns>True if a selection was made, False if it wasn't.</returns>
2022-09-07 13:04:51 +08:00
public bool SelectBeatmap ( BeatmapInfo ? beatmapInfo , bool bypassFilters = true )
2017-03-17 18:12:15 +08:00
{
2020-04-16 17:10:35 +08:00
// 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 )
2018-03-09 18:21:00 +08:00
return false ;
2018-04-13 17:19:50 +08:00
2018-03-09 18:52:59 +08:00
foreach ( CarouselBeatmapSet set in beatmapSets )
{
2019-02-21 17:56:34 +08:00
if ( ! bypassFilters & & set . Filtered . Value )
2018-03-09 18:52:59 +08:00
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
2018-03-09 18:52:59 +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 )
2020-03-12 14:52:03 +08:00
return false ;
2018-04-13 17:19:50 +08:00
2020-03-12 14:52:03 +08:00
select ( item ) ;
2020-02-10 15:31:52 +08:00
2020-03-12 14:52:03 +08:00
// 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 ) ;
2020-02-10 15:31:52 +08:00
2020-03-12 14:52:03 +08:00
applyActiveCriteria ( false ) ;
2018-03-09 18:52:59 +08:00
}
2020-03-12 14:52:03 +08:00
return true ;
2018-03-09 18:52:59 +08:00
}
2018-04-13 17:19:50 +08:00
2018-03-09 18:52:59 +08:00
return false ;
2017-03-17 18:12:15 +08:00
}
2018-04-13 17:19:50 +08:00
2017-12-11 19:22:10 +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>
2017-03-17 18:12:15 +08:00
public void SelectNext ( int direction = 1 , bool skipDifficulties = true )
{
2020-03-29 02:21:21 +08:00
if ( beatmapSets . All ( s = > s . Filtered . Value ) )
2017-12-23 19:53:11 +08:00
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 )
{
2022-09-07 13:08:07 +08:00
if ( selectedBeatmap = = null | | selectedBeatmapSet = = null )
return ;
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 )
{
2022-09-07 13:08:07 +08:00
if ( selectedBeatmap = = null | | selectedBeatmapSet = = null )
2020-06-26 20:03:34 +08:00
return ;
2022-07-21 15:06:06 +08:00
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 ] ) ;
2017-03-17 18:12:15 +08:00
}
2018-04-13 17:19:50 +08:00
2017-12-31 11:55:53 +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 ( )
2017-03-17 18:12:15 +08:00
{
2020-07-12 21:21:16 +08:00
if ( ! AllowSelection )
return false ;
2019-02-21 17:56:34 +08:00
var visibleSets = beatmapSets . Where ( s = > ! s . Filtered . Value ) . ToList ( ) ;
2022-01-28 12:27:12 +08:00
visibleSetsCount = visibleSets . Count ;
2017-12-31 09:10:54 +08:00
if ( ! visibleSets . Any ( ) )
2017-12-31 11:55:53 +08:00
return false ;
2018-04-13 17:19:50 +08:00
2022-09-07 13:08:07 +08:00
if ( selectedBeatmap ! = null & & selectedBeatmapSet ! = null )
2017-12-14 11:59:35 +08:00
{
2023-06-15 16:22:11 +08:00
randomSelectedBeatmaps . Add ( selectedBeatmap ) ;
2018-04-13 17:19:50 +08:00
2017-12-14 11:59:35 +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
2017-12-14 11:59:35 +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 )
2017-06-01 00:41:15 +08:00
{
2017-12-31 09:10:54 +08:00
var notYetVisitedSets = visibleSets . Except ( previouslyVisitedRandomSets ) . ToList ( ) ;
2019-04-01 11:16:05 +08:00
2017-12-14 11:59:35 +08:00
if ( ! notYetVisitedSets . Any ( ) )
2017-06-01 02:31:05 +08:00
{
2017-12-31 20:47:27 +08:00
previouslyVisitedRandomSets . RemoveAll ( s = > visibleSets . Contains ( s ) ) ;
2017-12-31 09:10:54 +08:00
notYetVisitedSets = visibleSets ;
2017-06-01 00:41:15 +08:00
}
2018-04-13 17:19:50 +08:00
2017-12-14 15:59:08 +08:00
set = notYetVisitedSets . ElementAt ( RNG . Next ( notYetVisitedSets . Count ) ) ;
2017-12-14 11:59:35 +08:00
previouslyVisitedRandomSets . Add ( set ) ;
2017-06-01 00:41:15 +08:00
}
else
2017-12-31 09:10:54 +08:00
set = visibleSets . ElementAt ( RNG . Next ( visibleSets . Count ) ) ;
2018-04-13 17:19:50 +08:00
2022-01-28 12:27:12 +08:00
if ( selectedBeatmapSet ! = null )
playSpinSample ( distanceBetween ( set , selectedBeatmapSet ) ) ;
2020-03-20 12:01:24 +08:00
select ( set ) ;
2017-12-31 11:55:53 +08:00
return true ;
2017-03-17 18:12:15 +08:00
}
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
{
2023-06-15 16:22:11 +08:00
var beatmap = randomSelectedBeatmaps [ ^ 1 ] ;
2023-12-26 07:09:39 +08:00
randomSelectedBeatmaps . RemoveAt ( randomSelectedBeatmaps . Count - 1 ) ;
2018-04-13 17:19:50 +08:00
2023-06-15 16:22:11 +08:00
if ( ! beatmap . Filtered . Value & & beatmap . BeatmapInfo . BeatmapSet ? . DeletePending ! = true )
2017-06-02 01:54:42 +08:00
{
2022-01-28 12:27:12 +08:00
if ( selectedBeatmapSet ! = null )
2022-09-07 13:08:07 +08:00
{
if ( RandomAlgorithm . Value = = RandomSelectAlgorithm . RandomPermutation )
previouslyVisitedRandomSets . Remove ( selectedBeatmapSet ) ;
2022-01-28 12:27:12 +08:00
playSpinSample ( distanceBetween ( beatmap , selectedBeatmapSet ) ) ;
2022-09-07 13:08:07 +08:00
}
2022-01-28 12:27:12 +08:00
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
2022-01-28 12:27:12 +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 )
{
2022-09-07 13:08:07 +08:00
var chan = spinSample ? . GetChannel ( ) ;
if ( chan ! = null )
{
chan . Frequency . Value = 1f + Math . Min ( 1f , distance / visibleSetsCount ) ;
chan . Play ( ) ;
}
2022-02-04 14:42:52 +08:00
randomSelectSample ? . Play ( ) ;
2022-01-28 12:27:12 +08:00
}
2022-09-07 13:04:51 +08:00
private void select ( CarouselItem ? item )
2017-12-18 11:30:39 +08:00
{
2019-03-21 20:02:45 +08:00
if ( ! AllowSelection )
return ;
2017-12-18 11:30:39 +08:00
if ( item = = null ) return ;
2019-02-28 12:31:40 +08:00
2017-12-18 11:30:39 +08:00
item . State . Value = CarouselItemState . Selected ;
}
2018-04-13 17:19:50 +08:00
2024-08-28 17:22:05 +08:00
private FilterCriteria activeCriteria ;
2018-04-13 17:19:50 +08:00
2022-09-07 13:04:51 +08:00
protected ScheduledDelegate ? PendingFilter ;
2018-04-13 17:19:50 +08:00
2017-07-20 10:50:31 +08:00
public bool AllowSelection = true ;
2018-04-13 17:19:50 +08:00
2019-07-26 12:07:28 +08:00
/// <summary>
2019-07-26 14:22:29 +08:00
/// Half the height of the visible content.
2019-07-26 14:13:10 +08:00
/// <remarks>
2019-11-17 20:55:40 +08:00
/// This is different from the height of <see cref="ScrollContainer{T}"/>.displayableContent, since
2019-07-26 14:13:10 +08:00
/// the beatmap carousel bleeds into the <see cref="FilterControl"/> and the <see cref="Footer"/>
/// </remarks>
2019-07-26 12:07:28 +08:00
/// </summary>
2020-04-19 23:29:06 +08:00
private float visibleHalfHeight = > ( DrawHeight + BleedBottom + BleedTop ) / 2 ;
2019-07-26 12:07:28 +08:00
2019-07-26 14:13:10 +08:00
/// <summary>
/// The position of the lower visible bound with respect to the current scroll position.
/// </summary>
2020-11-26 17:28:52 +08:00
private float visibleBottomBound = > Scroll . Current + DrawHeight + BleedBottom ;
2019-07-26 14:13:10 +08:00
/// <summary>
/// The position of the upper visible bound with respect to the current scroll position.
/// </summary>
2020-11-26 17:28:52 +08:00
private float visibleUpperBound = > Scroll . Current - BleedTop ;
2019-07-26 14:13:10 +08:00
2017-12-16 15:27:39 +08:00
public void FlushPendingFilterOperations ( )
2017-07-21 16:20:52 +08:00
{
2023-01-18 11:00:47 +08:00
if ( ! IsLoaded )
return ;
2018-07-18 09:12:14 +08:00
if ( PendingFilter ? . Completed = = false )
2018-03-23 00:06:05 +08:00
{
2020-03-10 18:59:49 +08:00
applyActiveCriteria ( false ) ;
2018-03-23 00:06:05 +08:00
Update ( ) ;
}
2017-07-21 16:20:52 +08:00
}
2018-04-13 17:19:50 +08:00
2024-08-28 17:56:09 +08:00
public void Filter ( FilterCriteria ? newCriteria )
2017-03-17 18:12:15 +08:00
{
2017-03-29 18:47:53 +08:00
if ( newCriteria ! = null )
2017-12-16 15:27:39 +08:00
activeCriteria = newCriteria ;
2018-04-13 17:19:50 +08:00
2024-08-28 17:56:09 +08:00
applyActiveCriteria ( true ) ;
2017-12-16 15:27:39 +08:00
}
2018-04-13 17:19:50 +08:00
2023-08-22 16:31:19 +08:00
private bool beatmapsSplitOut ;
2024-09-18 16:21:43 +08:00
private void applyActiveCriteria ( bool debounce )
2017-12-16 15:27:39 +08:00
{
2020-07-12 21:21:16 +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
2017-12-16 22:55:55 +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
2023-08-22 16:31:19 +08:00
if ( activeCriteria . SplitOutDifficulties ! = beatmapsSplitOut )
{
2024-08-28 18:35:28 +08:00
loadNewRoot ( ) ;
2023-08-22 16:31:19 +08:00
return ;
}
2017-12-16 15:27:39 +08:00
root . Filter ( activeCriteria ) ;
2017-12-18 01:23:03 +08:00
itemsCache . Invalidate ( ) ;
2020-03-13 10:51:26 +08:00
2024-09-18 16:21:43 +08:00
ScrollToSelected ( true ) ;
2023-03-03 14:25:55 +08:00
FilterApplied ? . Invoke ( ) ;
2017-12-16 22:55:55 +08:00
}
2016-11-21 03:34:16 +08:00
}
2018-04-13 17:19:50 +08:00
2023-12-18 19:10:31 +08:00
private void invalidateAfterChange ( )
2022-01-20 15:39:42 +08:00
{
itemsCache . Invalidate ( ) ;
2023-12-18 19:00:57 +08:00
if ( ! Scroll . UserScrolling )
ScrollToSelected ( true ) ;
2023-12-18 19:10:31 +08:00
BeatmapSetsChanged ? . Invoke ( ) ;
2022-01-20 15:39:42 +08:00
}
2017-12-17 01:33:01 +08:00
private float? scrollTarget ;
2018-04-13 17:19:50 +08:00
2020-03-13 10:51:26 +08:00
/// <summary>
2021-10-02 11:44:22 +08:00
/// Scroll to the current <see cref="SelectedBeatmapInfo"/>.
2020-03-13 10:51:26 +08:00
/// </summary>
2020-11-27 12:54:36 +08:00
/// <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
2022-05-04 08:52:10 +08:00
#region Button selection logic
2020-06-25 18:47:23 +08:00
2021-09-16 17:26:12 +08:00
public bool OnPressed ( KeyBindingPressEvent < GlobalAction > e )
2020-03-02 17:55:28 +08:00
{
2021-09-16 17:26:12 +08:00
switch ( e . Action )
2020-03-02 17:55:28 +08:00
{
case GlobalAction . SelectNext :
2022-05-04 21:46:23 +08:00
case GlobalAction . SelectNextGroup :
SelectNext ( 1 , e . Action = = GlobalAction . SelectNextGroup ) ;
2020-03-02 17:55:28 +08:00
return true ;
case GlobalAction . SelectPrevious :
2022-05-04 21:46:23 +08:00
case GlobalAction . SelectPreviousGroup :
SelectNext ( - 1 , e . Action = = GlobalAction . SelectPreviousGroup ) ;
2020-03-02 17:55:28 +08:00
return true ;
2017-12-18 11:30:39 +08:00
}
2018-04-13 17:19:50 +08:00
2020-03-02 17:55:28 +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 )
2020-03-02 17:55:28 +08:00
{
2020-06-25 18:47:23 +08:00
}
#endregion
2021-01-02 21:05:41 +08:00
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).
2024-07-02 23:19:04 +08:00
if ( invalidation . HasFlag ( Invalidation . DrawSize ) )
2021-01-02 21:05:41 +08:00
itemsCache . Invalidate ( ) ;
return base . OnInvalidate ( invalidation , source ) ;
}
2017-12-18 11:30:39 +08:00
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 ;
2020-10-13 12:44:32 +08:00
// 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 )
2022-06-07 15:32:15 +08:00
{
2020-10-12 18:55:17 +08:00
updateYPositions ( ) ;
2018-04-13 17:19:50 +08:00
2022-06-07 15:32:15 +08:00
if ( visibleItems . Count = = 0 )
{
noResultsPlaceholder . Filter = activeCriteria ;
noResultsPlaceholder . Show ( ) ;
}
else
noResultsPlaceholder . Hide ( ) ;
}
2020-11-26 17:42:51 +08:00
// if there is a pending scroll action we apply it without animation and transfer the difference in position to the panels.
2020-11-27 12:54:36 +08:00
// 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 )
2020-11-26 17:33:37 +08:00
updateScrollPosition ( ) ;
2020-10-13 12:44:32 +08:00
// 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 ( ) ;
2020-10-12 18:12:00 +08:00
2020-10-13 12:44:32 +08:00
// 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
{
2020-10-13 12:44:32 +08:00
displayedRange = newDisplayRange ;
2018-04-13 17:19:50 +08:00
2020-10-19 18:55:20 +08:00
if ( visibleItems . Count > 0 )
2017-12-18 11:30:39 +08:00
{
2020-10-19 18:55:20 +08:00
var toDisplay = visibleItems . GetRange ( displayedRange . first , displayedRange . last - displayedRange . first + 1 ) ;
2020-10-12 14:36:03 +08:00
2024-01-22 14:56:16 +08:00
foreach ( var panel in Scroll )
2020-10-13 13:23:29 +08:00
{
2023-01-10 03:59:28 +08:00
Debug . Assert ( panel . Item ! = null ) ;
if ( toDisplay . Remove ( panel . Item ) )
2020-10-19 18:55:20 +08:00
{
// 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 )
2022-07-21 15:24:46 +08:00
expirePanelImmediately ( panel ) ;
2020-10-13 13:23:29 +08:00
}
2018-04-13 17:19:50 +08:00
2020-10-19 18:55:20 +08:00
// Add those items within the previously found index range that should be displayed.
foreach ( var item in toDisplay )
{
2024-09-04 13:31:59 +08:00
var panel = setPool . Get ( ) ;
2020-10-13 12:51:27 +08:00
2024-09-04 13:31:59 +08:00
panel . Item = item ;
2020-10-19 18:55:20 +08:00
panel . Y = item . CarouselYPosition ;
2020-10-13 12:51:27 +08:00
2020-11-26 17:28:52 +08:00
Scroll . Add ( panel ) ;
2020-10-19 18:55:20 +08:00
}
2020-10-13 12:44:32 +08:00
}
}
2020-10-12 13:46:51 +08:00
2020-10-13 12:44:32 +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.
2024-01-22 14:54:24 +08:00
foreach ( DrawableCarouselItem item in Scroll )
2020-10-13 12:44:32 +08:00
{
2020-10-13 13:37:44 +08:00
updateItem ( item ) ;
2023-01-10 01:36:55 +08:00
Debug . Assert ( item . Item ! = null ) ;
if ( item . Item . Visible )
2022-11-04 14:14:09 +08:00
{
bool isSelected = item . Item . State . Value = = CarouselItemState . Selected ;
2024-06-26 20:10:29 +08:00
bool hasPassedSelection = item . Item . CarouselYPosition < selectedBeatmapSet ? . CarouselYPosition ;
2022-11-04 14:14:09 +08:00
// Cheap way of doing animations when entering / exiting song select.
const double half_time = 50 ;
const float panel_x_offset_when_inactive = 200 ;
if ( isSelected | | AllowSelection )
{
item . Alpha = ( float ) Interpolation . DampContinuously ( item . Alpha , 1 , half_time , Clock . ElapsedFrameTime ) ;
item . X = ( float ) Interpolation . DampContinuously ( item . X , 0 , half_time , Clock . ElapsedFrameTime ) ;
}
else
{
item . Alpha = ( float ) Interpolation . DampContinuously ( item . Alpha , 0 , half_time , Clock . ElapsedFrameTime ) ;
item . X = ( float ) Interpolation . DampContinuously ( item . X , panel_x_offset_when_inactive , half_time , Clock . ElapsedFrameTime ) ;
}
2024-06-26 20:10:29 +08:00
Scroll . ChangeChildDepth ( item , hasPassedSelection ? - item . Item . CarouselYPosition : item . Item . CarouselYPosition ) ;
2022-11-04 14:14:09 +08:00
}
2024-09-04 14:25:36 +08:00
if ( item is DrawableCarouselBeatmapSet set )
2020-10-13 13:37:44 +08:00
{
2024-09-04 14:25:36 +08:00
for ( int i = 0 ; i < set . DrawableBeatmaps . Count ; i + + )
updateItem ( set . DrawableBeatmaps [ i ] , item ) ;
2020-10-13 13:37:44 +08:00
}
2020-10-13 12:44:32 +08:00
}
2017-12-18 11:30:39 +08:00
}
2018-04-13 17:19:50 +08:00
2022-07-21 15:24:46 +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 ( ) ;
}
2020-10-13 17:33:31 +08:00
private readonly CarouselBoundsItem carouselBoundsItem = new CarouselBoundsItem ( ) ;
2020-10-13 12:21:21 +08:00
private ( int firstIndex , int lastIndex ) getDisplayRange ( )
{
// Find index range of all items that should be on-screen
2020-10-13 17:33:31 +08:00
carouselBoundsItem . CarouselYPosition = visibleUpperBound - distance_offscreen_to_preload ;
int firstIndex = visibleItems . BinarySearch ( carouselBoundsItem ) ;
2020-10-13 12:21:21 +08:00
if ( firstIndex < 0 ) firstIndex = ~ firstIndex ;
2020-10-13 17:33:31 +08:00
carouselBoundsItem . CarouselYPosition = visibleBottomBound + distance_offscreen_to_preload ;
int lastIndex = visibleItems . BinarySearch ( carouselBoundsItem ) ;
2020-10-13 12:21:21 +08:00
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 ) ;
2020-10-19 18:10:01 +08:00
lastIndex = Math . Clamp ( lastIndex + 1 , firstIndex , Math . Max ( 0 , visibleItems . Count - 1 ) ) ;
2020-10-13 17:33:31 +08:00
2020-10-13 12:21:21 +08:00
return ( firstIndex , lastIndex ) ;
}
2022-09-07 13:04:51 +08:00
private CarouselBeatmapSet ? createCarouselSet ( BeatmapSetInfo beatmapSet )
2017-03-17 18:12:15 +08:00
{
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.
2017-10-20 09:50:00 +08:00
if ( beatmapSet . Beatmaps . All ( b = > b . Hidden ) )
return null ;
2018-04-13 17:19:50 +08:00
2020-04-11 15:58:13 +08:00
var set = new CarouselBeatmapSet ( beatmapSet )
2020-04-09 23:47:28 +08:00
{
2020-04-11 15:58:13 +08:00
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 )
2017-03-17 18:12:15 +08:00
{
2019-02-22 16:51:39 +08:00
c . State . ValueChanged + = state = >
2017-12-12 16:48:38 +08:00
{
2019-02-22 16:51:39 +08:00
if ( state . NewValue = = CarouselItemState . Selected )
2017-12-12 16:48:38 +08:00
{
2017-12-17 04:53:13 +08:00
selectedBeatmapSet = set ;
2021-10-02 11:44:22 +08:00
SelectionChanged ? . Invoke ( c . BeatmapInfo ) ;
2018-04-13 17:19:50 +08:00
2017-12-18 01:23:03 +08:00
itemsCache . Invalidate ( ) ;
2020-03-13 10:51:26 +08:00
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 ;
2017-03-17 18:12:15 +08:00
}
2018-04-13 17:19:50 +08:00
2016-11-24 00:42:21 +08:00
/// <summary>
2017-12-12 16:48:38 +08:00
/// Computes the target Y positions for every item in the carousel.
2016-11-24 00:42:21 +08:00
/// </summary>
2017-12-12 16:48:38 +08:00
/// <returns>The Y position of the currently selected item.</returns>
2020-10-12 18:55:17 +08:00
private void updateYPositions ( )
2016-11-21 03:34:16 +08:00
{
2020-10-12 13:23:18 +08:00
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
2022-07-21 15:06:06 +08:00
foreach ( CarouselItem item in root . Items )
2017-12-12 16:48:38 +08:00
{
2020-10-12 13:23:18 +08:00
if ( item . Filtered . Value )
continue ;
switch ( item )
2016-11-21 03:34:16 +08:00
{
2020-10-12 13:23:18 +08:00
case CarouselBeatmapSet set :
2017-12-17 01:33:01 +08:00
{
2024-06-26 10:56:58 +08:00
bool isSelected = item . State . Value = = CarouselItemState . Selected ;
float padding = isSelected ? 5 : - 5 ;
if ( isSelected )
// double padding because we want to cancel the negative padding from the last item.
currentY + = padding * 2 ;
2020-10-12 13:23:18 +08:00
visibleItems . Add ( set ) ;
2020-10-13 12:21:21 +08:00
set . CarouselYPosition = currentY ;
2020-10-12 13:23:18 +08:00
2024-06-26 10:56:58 +08:00
if ( isSelected )
2020-10-12 17:32:29 +08:00
{
// 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 )
{
2020-10-12 19:02:45 +08:00
if ( ! b . Visible )
continue ;
2020-10-12 17:32:29 +08:00
if ( b . State . Value = = CarouselItemState . Selected )
{
scrollTarget + = b . TotalHeight / 2 ;
break ;
}
2020-10-13 17:13:36 +08:00
scrollTarget + = b . TotalHeight ;
2020-10-12 17:32:29 +08:00
}
}
2024-06-26 10:56:58 +08:00
currentY + = set . TotalHeight + padding ;
2020-10-12 13:23:18 +08:00
break ;
2017-12-17 01:33:01 +08:00
}
2020-10-12 13:23:18 +08:00
}
2016-11-21 03:34:16 +08:00
}
2018-04-13 17:19:50 +08:00
2019-07-26 14:22:29 +08:00
currentY + = visibleHalfHeight ;
2020-11-26 17:28:52 +08:00
Scroll . ScrollContent . Height = currentY ;
2018-04-13 17:19:50 +08:00
2021-01-03 11:53:25 +08:00
itemsCache . Validate ( ) ;
// update and let external consumers know about selection loss.
2024-09-18 14:50:55 +08:00
if ( BeatmapSetsLoaded & & AllowSelection )
2017-12-18 01:23:03 +08:00
{
2021-01-03 11:53:25 +08:00
bool selectionLost = selectedBeatmapSet ! = null & & selectedBeatmapSet . State . Value ! = CarouselItemState . Selected ;
2018-04-13 17:19:50 +08:00
2021-01-03 21:44:30 +08:00
if ( selectionLost )
2021-01-03 11:53:25 +08:00
{
selectedBeatmapSet = null ;
SelectionChanged ? . Invoke ( null ) ;
}
}
2016-11-21 14:59:46 +08:00
}
2018-04-13 17:19:50 +08:00
2019-11-22 09:51:49 +08:00
private bool firstScroll = true ;
2017-12-18 11:30:39 +08:00
private void updateScrollPosition ( )
2017-12-17 04:53:13 +08:00
{
2019-11-20 18:38:39 +08:00
if ( scrollTarget ! = null )
{
2019-11-22 09:51:49 +08:00
if ( firstScroll )
{
// reduce movement when first displaying the carousel.
2020-11-26 17:28:52 +08:00
Scroll . ScrollTo ( scrollTarget . Value - 200 , false ) ;
2019-11-22 09:51:49 +08:00
firstScroll = false ;
}
2020-11-27 12:54:36 +08:00
switch ( pendingScrollOperation )
{
case PendingScrollOperation . Standard :
Scroll . ScrollTo ( scrollTarget . Value ) ;
break ;
case PendingScrollOperation . Immediate :
2021-01-03 11:53:25 +08:00
2020-11-27 12:54:36 +08:00
// 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 ) ;
2024-01-22 14:56:16 +08:00
foreach ( var i in Scroll )
2020-11-27 12:54:36 +08:00
i . Y + = scrollChange ;
break ;
}
2020-11-26 17:42:51 +08:00
2020-11-27 12:54:36 +08:00
pendingScrollOperation = PendingScrollOperation . None ;
2019-11-20 18:38:39 +08:00
}
2017-12-17 04:53:13 +08:00
}
2018-04-13 17:19:50 +08:00
2017-03-17 18:12:15 +08:00
/// <summary>
2017-12-12 16:48:38 +08:00
/// Computes the x-offset of currently visible items. Makes the carousel appear round.
2017-03-17 18:12:15 +08:00
/// </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 )
2016-12-15 21:57:14 +08:00
{
2017-03-17 18:12:15 +08:00
// The radius of the circle the carousel moves on.
const float circle_radius = 3 ;
2019-11-25 07:45:42 +08:00
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
2017-03-17 18:12:15 +08:00
return 125 + x ;
2017-02-24 13:37:54 +08:00
}
2018-04-13 17:19:50 +08:00
2017-03-17 18:12:15 +08:00
/// <summary>
2024-09-04 13:31:59 +08:00
/// Update an item's x position and multiplicative alpha based on its y position and
2017-03-17 18:12:15 +08:00
/// the current scroll position.
/// </summary>
2020-10-13 13:37:44 +08:00
/// <param name="item">The item to be updated.</param>
/// <param name="parent">For nested items, the parent of the item to be updated.</param>
2022-09-07 13:04:51 +08:00
private void updateItem ( DrawableCarouselItem item , DrawableCarouselItem ? parent = null )
2017-02-24 13:37:54 +08:00
{
2020-11-26 17:28:52 +08:00
Vector2 posInScroll = Scroll . ScrollContent . ToLocalSpace ( item . Header . ScreenSpaceDrawQuad . Centre ) ;
2020-10-13 13:37:44 +08:00
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
2020-10-13 16:33:35 +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 ) ;
2016-12-15 21:57:14 +08:00
}
2018-04-13 17:19:50 +08:00
2020-11-27 12:54:36 +08:00
private enum PendingScrollOperation
{
None ,
Standard ,
Immediate ,
}
2020-10-13 12:21:21 +08:00
/// <summary>
/// A carousel item strictly used for binary search purposes.
/// </summary>
private class CarouselBoundsItem : CarouselItem
{
2022-09-07 13:04:51 +08:00
public override DrawableCarouselItem CreateDrawableRepresentation ( ) = > throw new NotImplementedException ( ) ;
2020-10-13 12:21:21 +08:00
}
2018-04-02 14:23:58 +08:00
private class CarouselRoot : CarouselGroupEagerSelect
{
2022-09-07 13:27:24 +08:00
// May only be null during construction (State.Value set causes PerformSelection to be triggered).
private readonly BeatmapCarousel ? carousel ;
2018-04-13 17:19:50 +08:00
2023-08-22 16:31:19 +08:00
public readonly Dictionary < Guid , List < CarouselBeatmapSet > > BeatmapSetsByID = new Dictionary < Guid , List < CarouselBeatmapSet > > ( ) ;
2022-01-20 20:58:16 +08:00
2018-04-02 14:23:58 +08:00
public CarouselRoot ( BeatmapCarousel carousel )
{
2020-03-21 23:32:53 +08:00
// root should always remain selected. if not, PerformSelection will not be called.
2020-03-20 14:01:26 +08:00
State . Value = CarouselItemState . Selected ;
2022-06-24 20:25:23 +08:00
State . ValueChanged + = _ = > State . Value = CarouselItemState . Selected ;
2020-03-20 14:01:26 +08:00
2018-04-02 14:23:58 +08:00
this . carousel = carousel ;
}
2018-04-13 17:19:50 +08:00
2022-07-21 15:06:06 +08:00
public override void AddItem ( CarouselItem i )
2022-01-20 20:58:16 +08:00
{
CarouselBeatmapSet set = ( CarouselBeatmapSet ) i ;
2023-08-22 16:31:19 +08:00
if ( BeatmapSetsByID . TryGetValue ( set . BeatmapSet . ID , out var sets ) )
sets . Add ( set ) ;
else
BeatmapSetsByID . Add ( set . BeatmapSet . ID , new List < CarouselBeatmapSet > { set } ) ;
2022-01-20 20:58:16 +08:00
2022-07-21 15:06:06 +08:00
base . AddItem ( i ) ;
2022-01-20 20:58:16 +08:00
}
2023-12-20 18:09:07 +08:00
/// <summary>
/// A special method to handle replace operations (general for updating a beatmap).
/// Avoids event-driven selection flip-flopping during the remove/add process.
/// </summary>
/// <param name="oldItem">The beatmap set to be replaced.</param>
/// <param name="newItems">All new items to replace the removed beatmap set.</param>
/// <returns>All removed items, for any further processing.</returns>
public IEnumerable < CarouselBeatmapSet > ReplaceItem ( BeatmapSetInfo oldItem , List < CarouselBeatmapSet > newItems )
{
2023-12-20 18:27:57 +08:00
var previousSelection = ( LastSelected as CarouselBeatmapSet ) ? . Beatmaps
. FirstOrDefault ( s = > s . State . Value = = CarouselItemState . Selected )
? . BeatmapInfo ;
bool wasSelected = previousSelection ? . BeatmapSet ? . ID = = oldItem . ID ;
2023-12-20 18:09:07 +08:00
// Without doing this, the removal of the old beatmap will cause carousel's eager selection
// logic to invoke, causing one unnecessary selection.
DisableSelection = true ;
var removedSets = RemoveItemsByID ( oldItem . ID ) ;
DisableSelection = false ;
foreach ( var set in newItems )
AddItem ( set ) ;
2023-12-20 18:27:57 +08:00
// Check if we can/need to maintain our current selection.
if ( wasSelected )
2023-12-20 18:09:07 +08:00
{
2023-12-20 18:27:57 +08:00
CarouselBeatmap ? matchingBeatmap = newItems . SelectMany ( s = > s . Beatmaps )
. FirstOrDefault ( b = > b . BeatmapInfo . ID = = previousSelection ? . ID ) ;
2023-12-20 18:09:07 +08:00
2023-12-20 18:27:57 +08:00
if ( matchingBeatmap ! = null )
matchingBeatmap . State . Value = CarouselItemState . Selected ;
2023-12-20 18:09:07 +08:00
}
return removedSets ;
}
2023-08-25 17:10:54 +08:00
public IEnumerable < CarouselBeatmapSet > RemoveItemsByID ( Guid beatmapSetID )
2022-01-20 20:58:16 +08:00
{
2023-08-22 16:31:19 +08:00
if ( BeatmapSetsByID . TryGetValue ( beatmapSetID , out var carouselBeatmapSets ) )
2022-07-21 15:24:46 +08:00
{
2023-08-22 16:31:19 +08:00
foreach ( var set in carouselBeatmapSets )
RemoveItem ( set ) ;
return carouselBeatmapSets ;
2022-07-21 15:24:46 +08:00
}
2023-08-22 16:31:19 +08:00
return Enumerable . Empty < CarouselBeatmapSet > ( ) ;
2022-01-20 20:58:16 +08:00
}
2022-07-21 15:06:06 +08:00
public override void RemoveItem ( CarouselItem i )
2022-01-20 20:58:16 +08:00
{
CarouselBeatmapSet set = ( CarouselBeatmapSet ) i ;
BeatmapSetsByID . Remove ( set . BeatmapSet . ID ) ;
2022-07-21 15:06:06 +08:00
base . RemoveItem ( i ) ;
2022-01-20 20:58:16 +08:00
}
2018-04-02 14:23:58 +08:00
protected override void PerformSelection ( )
{
2021-08-26 02:42:15 +08:00
if ( LastSelected = = null )
2020-03-20 14:01:26 +08:00
carousel ? . SelectNextRandom ( ) ;
2018-04-02 14:23:58 +08:00
else
base . PerformSelection ( ) ;
}
}
2019-08-15 17:27:45 +08:00
2024-01-04 18:13:36 +08:00
public partial class CarouselScrollContainer : UserTrackingScrollContainer < DrawableCarouselItem >
2019-08-15 17:27:45 +08:00
{
private bool rightMouseScrollBlocked ;
2023-10-20 19:04:11 +08:00
public override bool ReceivePositionalInputAt ( Vector2 screenSpacePos ) = > true ;
2020-11-26 17:28:52 +08:00
public CarouselScrollContainer ( )
{
// size is determined by the carousel itself, due to not all content necessarily being loaded.
ScrollContent . AutoSizeAxes = Axes . None ;
2020-12-03 12:26:28 +08:00
// 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 ;
2020-11-26 17:28:52 +08:00
}
2019-08-15 17:27:45 +08:00
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.
2024-05-27 17:23:32 +08:00
if ( GetContainingInputManager ( ) ! . HoveredDrawables . OfType < DrawableCarouselItem > ( ) . Any ( ) )
2019-08-15 17:27:45 +08:00
{
rightMouseScrollBlocked = true ;
return false ;
}
}
2019-08-15 18:25:33 +08:00
rightMouseScrollBlocked = false ;
2019-08-15 17:27:45 +08:00
return base . OnMouseDown ( e ) ;
}
protected override bool OnDragStart ( DragStartEvent e )
{
if ( rightMouseScrollBlocked )
return false ;
return base . OnDragStart ( e ) ;
}
}
2021-11-05 17:05:31 +08:00
protected override void Dispose ( bool isDisposing )
{
base . Dispose ( isDisposing ) ;
2021-12-17 18:01:19 +08:00
subscriptionBeatmaps ? . Dispose ( ) ;
2021-11-05 17:05:31 +08:00
}
2016-11-21 03:34:16 +08:00
}
}