// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Caching; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Pooling; using osu.Framework.Input.Bindings; using osu.Framework.Input.Events; using osu.Framework.Layout; using osu.Framework.Threading; using osu.Framework.Utils; using osu.Game.Beatmaps; using osu.Game.Configuration; using osu.Game.Graphics.Containers; using osu.Game.Graphics.Cursor; using osu.Game.Input.Bindings; using osu.Game.Screens.Select.Carousel; using osuTK; using osuTK.Input; namespace osu.Game.Screens.Select { public class BeatmapCarousel : CompositeDrawable, IKeyBindingHandler { /// /// Height of the area above the carousel that should be treated as visible due to transparency of elements in front of it. /// public float BleedTop { get; set; } /// /// Height of the area below the carousel that should be treated as visible due to transparency of elements in front of it. /// public float BleedBottom { get; set; } /// /// Triggered when the loaded change and are completely loaded. /// public Action BeatmapSetsChanged; /// /// The currently selected beatmap. /// public BeatmapInfo SelectedBeatmapInfo => selectedBeatmap?.BeatmapInfo; private CarouselBeatmap selectedBeatmap => selectedBeatmapSet?.Beatmaps.FirstOrDefault(s => s.State.Value == CarouselItemState.Selected); /// /// The currently selected beatmap set. /// public BeatmapSetInfo SelectedBeatmapSet => selectedBeatmapSet?.BeatmapSet; /// /// A function to optionally decide on a recommended difficulty from a beatmap set. /// public Func, BeatmapInfo> GetRecommendedBeatmap; private CarouselBeatmapSet selectedBeatmapSet; /// /// Raised when the is changed. /// public Action SelectionChanged; public override bool HandleNonPositionalInput => AllowSelection; public override bool HandlePositionalInput => AllowSelection; public override bool PropagatePositionalInputSubTree => AllowSelection; public override bool PropagateNonPositionalInputSubTree => AllowSelection; private (int first, int last) displayedRange; /// /// Extend the range to retain already loaded pooled drawables. /// private const float distance_offscreen_before_unload = 1024; /// /// Extend the range to update positions / retrieve pooled drawables outside of visible range. /// private const float distance_offscreen_to_preload = 512; // todo: adjust this appropriately once we can make set panel contents load while off-screen. /// /// Whether carousel items have completed asynchronously loaded. /// public bool BeatmapSetsLoaded { get; private set; } protected readonly CarouselScrollContainer Scroll; private IEnumerable beatmapSets => root.Children.OfType(); // todo: only used for testing, maybe remove. public IEnumerable BeatmapSets { get => beatmapSets.Select(g => g.BeatmapSet); set => loadBeatmapSets(value); } private void loadBeatmapSets(IEnumerable beatmapSets) { CarouselRoot newRoot = new CarouselRoot(this); newRoot.AddChildren(beatmapSets.Select(createCarouselSet).Where(g => g != null)); root = newRoot; if (selectedBeatmapSet != null && !beatmapSets.Contains(selectedBeatmapSet.BeatmapSet)) selectedBeatmapSet = null; Scroll.Clear(false); itemsCache.Invalidate(); ScrollToSelected(); // apply any pending filter operation that may have been delayed (see applyActiveCriteria's scheduling behaviour when BeatmapSetsLoaded is false). FlushPendingFilterOperations(); // Run on late scheduler want to ensure this runs after all pending UpdateBeatmapSet / RemoveBeatmapSet operations are run. SchedulerAfterChildren.Add(() => { BeatmapSetsChanged?.Invoke(); BeatmapSetsLoaded = true; itemsCache.Invalidate(); }); } private readonly List visibleItems = new List(); private readonly Cached itemsCache = new Cached(); private PendingScrollOperation pendingScrollOperation = PendingScrollOperation.None; public Bindable RightClickScrollingEnabled = new Bindable(); public Bindable RandomAlgorithm = new Bindable(); private readonly List previouslyVisitedRandomSets = new List(); private readonly Stack randomSelectedBeatmaps = new Stack(); private CarouselRoot root; private readonly DrawablePool setPool = new DrawablePool(100); public BeatmapCarousel() { root = new CarouselRoot(this); InternalChild = new OsuContextMenuContainer { RelativeSizeAxes = Axes.Both, Children = new Drawable[] { setPool, Scroll = new CarouselScrollContainer { RelativeSizeAxes = Axes.Both, } } }; } [Resolved] private BeatmapManager beatmaps { get; set; } [BackgroundDependencyLoader(permitNulls: true)] private void load(OsuConfigManager config) { config.BindWith(OsuSetting.RandomSelectAlgorithm, RandomAlgorithm); config.BindWith(OsuSetting.SongSelectRightMouseScroll, RightClickScrollingEnabled); RightClickScrollingEnabled.ValueChanged += enabled => Scroll.RightMouseScrollbar = enabled.NewValue; RightClickScrollingEnabled.TriggerChange(); beatmaps.ItemUpdated += beatmapUpdated; beatmaps.ItemRemoved += beatmapRemoved; beatmaps.BeatmapHidden += beatmapHidden; beatmaps.BeatmapRestored += beatmapRestored; if (!beatmapSets.Any()) loadBeatmapSets(GetLoadableBeatmaps()); } protected virtual IEnumerable GetLoadableBeatmaps() => beatmaps.GetAllUsableBeatmapSetsEnumerable(IncludedDetails.AllButFiles); public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet) => Schedule(() => { var existingSet = beatmapSets.FirstOrDefault(b => b.BeatmapSet.ID == beatmapSet.ID); if (existingSet == null) return; root.RemoveChild(existingSet); itemsCache.Invalidate(); }); public void UpdateBeatmapSet(BeatmapSetInfo beatmapSet) => Schedule(() => { int? previouslySelectedID = null; CarouselBeatmapSet existingSet = beatmapSets.FirstOrDefault(b => b.BeatmapSet.ID == beatmapSet.ID); // If the selected beatmap is about to be removed, store its ID so it can be re-selected if required if (existingSet?.State?.Value == CarouselItemState.Selected) previouslySelectedID = selectedBeatmap?.BeatmapInfo.ID; var newSet = createCarouselSet(beatmapSet); if (existingSet != null) root.RemoveChild(existingSet); if (newSet == null) { itemsCache.Invalidate(); return; } root.AddChild(newSet); // only reset scroll position if already near the scroll target. // without this, during a large beatmap import it is impossible to navigate the carousel. applyActiveCriteria(false, alwaysResetScrollPosition: false); // check if we can/need to maintain our current selection. if (previouslySelectedID != null) select((CarouselItem)newSet.Beatmaps.FirstOrDefault(b => b.BeatmapInfo.ID == previouslySelectedID) ?? newSet); itemsCache.Invalidate(); Schedule(() => BeatmapSetsChanged?.Invoke()); }); /// /// Selects a given beatmap on the carousel. /// /// The beatmap to select. /// Whether to select the beatmap even if it is filtered (i.e., not visible on carousel). /// True if a selection was made, False if it wasn't. public bool SelectBeatmap(BeatmapInfo beatmapInfo, bool bypassFilters = true) { // ensure that any pending events from BeatmapManager have been run before attempting a selection. Scheduler.Update(); if (beatmapInfo?.Hidden != false) return false; foreach (CarouselBeatmapSet set in beatmapSets) { if (!bypassFilters && set.Filtered.Value) continue; var item = set.Beatmaps.FirstOrDefault(p => p.BeatmapInfo.Equals(beatmapInfo)); if (item == null) // The beatmap that needs to be selected doesn't exist in this set continue; if (!bypassFilters && item.Filtered.Value) return false; 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; } return false; } /// /// Increment selection in the carousel in a chosen direction. /// /// The direction to increment. Negative is backwards. /// Whether to skip individual difficulties and only increment over full groups. public void SelectNext(int direction = 1, bool skipDifficulties = true) { if (beatmapSets.All(s => s.Filtered.Value)) return; if (skipDifficulties) selectNextSet(direction, true); else selectNextDifficulty(direction); } private void selectNextSet(int direction, bool skipDifficulties) { var unfilteredSets = beatmapSets.Where(s => !s.Filtered.Value).ToList(); var nextSet = unfilteredSets[(unfilteredSets.IndexOf(selectedBeatmapSet) + direction + unfilteredSets.Count) % unfilteredSets.Count]; if (skipDifficulties) select(nextSet); else select(direction > 0 ? nextSet.Beatmaps.First(b => !b.Filtered.Value) : nextSet.Beatmaps.Last(b => !b.Filtered.Value)); } private void selectNextDifficulty(int direction) { if (selectedBeatmap == null) return; var unfilteredDifficulties = selectedBeatmapSet.Children.Where(s => !s.Filtered.Value).ToList(); int index = unfilteredDifficulties.IndexOf(selectedBeatmap); if (index + direction < 0 || index + direction >= unfilteredDifficulties.Count) selectNextSet(direction, false); else select(unfilteredDifficulties[index + direction]); } /// /// Select the next beatmap in the random sequence. /// /// True if a selection could be made, else False. public bool SelectNextRandom() { if (!AllowSelection) return false; var visibleSets = beatmapSets.Where(s => !s.Filtered.Value).ToList(); if (!visibleSets.Any()) return false; if (selectedBeatmap != null) { randomSelectedBeatmaps.Push(selectedBeatmap); // 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); } CarouselBeatmapSet set; if (RandomAlgorithm.Value == RandomSelectAlgorithm.RandomPermutation) { var notYetVisitedSets = visibleSets.Except(previouslyVisitedRandomSets).ToList(); if (!notYetVisitedSets.Any()) { previouslyVisitedRandomSets.RemoveAll(s => visibleSets.Contains(s)); notYetVisitedSets = visibleSets; } set = notYetVisitedSets.ElementAt(RNG.Next(notYetVisitedSets.Count)); previouslyVisitedRandomSets.Add(set); } else set = visibleSets.ElementAt(RNG.Next(visibleSets.Count)); select(set); return true; } public void SelectPreviousRandom() { while (randomSelectedBeatmaps.Any()) { var beatmap = randomSelectedBeatmaps.Pop(); if (!beatmap.Filtered.Value) { if (RandomAlgorithm.Value == RandomSelectAlgorithm.RandomPermutation) previouslyVisitedRandomSets.Remove(selectedBeatmapSet); select(beatmap); break; } } } private void select(CarouselItem item) { if (!AllowSelection) return; if (item == null) return; item.State.Value = CarouselItemState.Selected; } private FilterCriteria activeCriteria = new FilterCriteria(); protected ScheduledDelegate PendingFilter; public bool AllowSelection = true; /// /// Half the height of the visible content. /// /// This is different from the height of .displayableContent, since /// the beatmap carousel bleeds into the and the /// /// private float visibleHalfHeight => (DrawHeight + BleedBottom + BleedTop) / 2; /// /// The position of the lower visible bound with respect to the current scroll position. /// private float visibleBottomBound => Scroll.Current + DrawHeight + BleedBottom; /// /// The position of the upper visible bound with respect to the current scroll position. /// private float visibleUpperBound => Scroll.Current - BleedTop; public void FlushPendingFilterOperations() { if (PendingFilter?.Completed == false) { applyActiveCriteria(false); Update(); } } public void Filter(FilterCriteria newCriteria, bool debounce = true) { if (newCriteria != null) activeCriteria = newCriteria; applyActiveCriteria(debounce); } private void applyActiveCriteria(bool debounce, bool alwaysResetScrollPosition = true) { 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(); } void perform() { PendingFilter = null; root.Filter(activeCriteria); itemsCache.Invalidate(); if (alwaysResetScrollPosition || !Scroll.UserScrolling) ScrollToSelected(true); } } private float? scrollTarget; /// /// Scroll to the current . /// /// /// 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. /// public void ScrollToSelected(bool immediate = false) => pendingScrollOperation = immediate ? PendingScrollOperation.Immediate : PendingScrollOperation.Standard; #region Key / button selection logic protected override bool OnKeyDown(KeyDownEvent e) { switch (e.Key) { case Key.Left: SelectNext(-1); return true; case Key.Right: SelectNext(); return true; } return false; } public bool OnPressed(KeyBindingPressEvent e) { switch (e.Action) { case GlobalAction.SelectNext: SelectNext(1, false); return true; case GlobalAction.SelectPrevious: SelectNext(-1, false); return true; } return false; } public void OnReleased(KeyBindingReleaseEvent 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 & Invalidation.Layout) > 0) itemsCache.Invalidate(); return base.OnInvalidate(invalidation, source); } protected override void Update() { base.Update(); bool revalidateItems = !itemsCache.IsValid; // First we iterate over all non-filtered carousel items and populate their // vertical position data. if (revalidateItems) updateYPositions(); // 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) { displayedRange = newDisplayRange; if (visibleItems.Count > 0) { var toDisplay = visibleItems.GetRange(displayedRange.first, displayedRange.last - displayedRange.first + 1); foreach (var panel in Scroll.Children) { 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) { // 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(); } } // 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); } } } // 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); } } } 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 void beatmapRemoved(BeatmapSetInfo item) => RemoveBeatmapSet(item); private void beatmapUpdated(BeatmapSetInfo item) => UpdateBeatmapSet(item); private void beatmapRestored(BeatmapInfo b) => UpdateBeatmapSet(beatmaps.QueryBeatmapSet(s => s.ID == b.BeatmapSetInfoID)); private void beatmapHidden(BeatmapInfo b) => UpdateBeatmapSet(beatmaps.QueryBeatmapSet(s => s.ID == b.BeatmapSetInfoID)); private CarouselBeatmapSet createCarouselSet(BeatmapSetInfo beatmapSet) { if (beatmapSet.Beatmaps.All(b => b.Hidden)) return null; // todo: remove the need for this. foreach (var b in beatmapSet.Beatmaps) b.Metadata ??= beatmapSet.Metadata; var set = new CarouselBeatmapSet(beatmapSet) { GetRecommendedBeatmap = beatmaps => GetRecommendedBeatmap?.Invoke(beatmaps) }; foreach (var c in set.Beatmaps) { c.State.ValueChanged += state => { if (state.NewValue == CarouselItemState.Selected) { selectedBeatmapSet = set; SelectionChanged?.Invoke(c.BeatmapInfo); itemsCache.Invalidate(); ScrollToSelected(); } }; } return set; } private const float panel_padding = 5; /// /// Computes the target Y positions for every item in the carousel. /// /// The Y position of the currently selected item. private void updateYPositions() { visibleItems.Clear(); float currentY = visibleHalfHeight; scrollTarget = null; foreach (CarouselItem item in root.Children) { if (item.Filtered.Value) continue; switch (item) { case CarouselBeatmapSet set: { visibleItems.Add(set); set.CarouselYPosition = currentY; 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; if (b.State.Value == CarouselItemState.Selected) { scrollTarget += b.TotalHeight / 2; break; } scrollTarget += b.TotalHeight; } } currentY += set.TotalHeight + panel_padding; break; } } } currentY += visibleHalfHeight; Scroll.ScrollContent.Height = currentY; itemsCache.Validate(); // update and let external consumers know about selection loss. if (BeatmapSetsLoaded) { bool selectionLost = selectedBeatmapSet != null && selectedBeatmapSet.State.Value != CarouselItemState.Selected; if (selectionLost) { selectedBeatmapSet = null; SelectionChanged?.Invoke(null); } } } 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; } } /// /// Computes the x-offset of currently visible items. Makes the carousel appear round. /// /// /// Vertical distance from the center of the carousel container /// ranging from -1 to 1. /// /// Half the height of the carousel container. 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; return 125 + x; } /// /// Update a item's x position and multiplicative alpha based on its y position and /// the current scroll position. /// /// The item to be updated. /// For nested items, the parent of the item to be updated. private void updateItem(DrawableCarouselItem item, DrawableCarouselItem parent = null) { Vector2 posInScroll = Scroll.ScrollContent.ToLocalSpace(item.Header.ScreenSpaceDrawQuad.Centre); float itemDrawY = posInScroll.Y - visibleUpperBound; float dist = Math.Abs(1f - itemDrawY / visibleHalfHeight); // 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); // 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)); } private enum PendingScrollOperation { None, Standard, Immediate, } /// /// A carousel item strictly used for binary search purposes. /// private class CarouselBoundsItem : CarouselItem { public override DrawableCarouselItem CreateDrawableRepresentation() => throw new NotImplementedException(); } private class CarouselRoot : CarouselGroupEagerSelect { private readonly BeatmapCarousel carousel; public CarouselRoot(BeatmapCarousel carousel) { // root should always remain selected. if not, PerformSelection will not be called. State.Value = CarouselItemState.Selected; State.ValueChanged += state => State.Value = CarouselItemState.Selected; this.carousel = carousel; } protected override void PerformSelection() { if (LastSelected == null || LastSelected.Filtered.Value) carousel?.SelectNextRandom(); else base.PerformSelection(); } } protected class CarouselScrollContainer : UserTrackingScrollContainer { 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().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); if (beatmaps != null) { beatmaps.ItemUpdated -= beatmapUpdated; beatmaps.ItemRemoved -= beatmapRemoved; beatmaps.BeatmapHidden -= beatmapHidden; beatmaps.BeatmapRestored -= beatmapRestored; } } } }