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
|
|
|
|
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Game.Configuration;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK.Input;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.MathUtils;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Caching;
|
|
|
|
|
using osu.Framework.Threading;
|
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2018-10-02 11:02:47 +08:00
|
|
|
|
using osu.Framework.Input.Events;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
|
using osu.Game.Graphics.Cursor;
|
|
|
|
|
using osu.Game.Screens.Select.Carousel;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Select
|
|
|
|
|
{
|
2019-08-15 13:00:12 +08:00
|
|
|
|
public class BeatmapCarousel : CompositeDrawable
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-07-26 12:07:28 +08:00
|
|
|
|
private const float bleed_top = FilterControl.HEIGHT;
|
|
|
|
|
private const float bleed_bottom = Footer.HEIGHT;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Triggered when the <see cref="BeatmapSets"/> loaded change and are completely loaded.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action BeatmapSetsChanged;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The currently selected beatmap.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public BeatmapInfo SelectedBeatmap => selectedBeatmap?.Beatmap;
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
private CarouselBeatmapSet selectedBeatmapSet;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raised when the <see cref="SelectedBeatmap"/> is changed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<BeatmapInfo> SelectionChanged;
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-03-21 19:51:06 +08:00
|
|
|
|
/// Whether carousel items have completed asynchronously loaded.
|
2018-04-13 17:19:50 +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
|
|
|
|
|
2019-08-15 13:00:12 +08:00
|
|
|
|
private readonly OsuScrollContainer scroll;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
private IEnumerable<CarouselBeatmapSet> beatmapSets => root.Children.OfType<CarouselBeatmapSet>();
|
|
|
|
|
|
|
|
|
|
public IEnumerable<BeatmapSetInfo> BeatmapSets
|
|
|
|
|
{
|
2018-08-29 00:42:25 +08:00
|
|
|
|
get => beatmapSets.Select(g => g.BeatmapSet);
|
2019-06-30 21:23:48 +08:00
|
|
|
|
set => loadBeatmapSets(value);
|
2018-08-29 00:42:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 21:23:48 +08:00
|
|
|
|
private void loadBeatmapSets(IEnumerable<BeatmapSetInfo> beatmapSets)
|
2018-08-29 00:42:25 +08:00
|
|
|
|
{
|
|
|
|
|
CarouselRoot newRoot = new CarouselRoot(this);
|
|
|
|
|
|
2019-06-30 21:23:48 +08:00
|
|
|
|
beatmapSets.Select(createCarouselSet).Where(g => g != null).ForEach(newRoot.AddChild);
|
|
|
|
|
newRoot.Filter(activeCriteria);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-30 21:23:48 +08:00
|
|
|
|
// preload drawables as the ctor overhead is quite high currently.
|
|
|
|
|
var _ = newRoot.Drawables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-30 21:23:48 +08:00
|
|
|
|
root = newRoot;
|
2019-09-25 01:42:12 +08:00
|
|
|
|
if (selectedBeatmapSet != null && !beatmapSets.Contains(selectedBeatmapSet.BeatmapSet))
|
|
|
|
|
selectedBeatmapSet = null;
|
|
|
|
|
|
2019-06-30 21:23:48 +08:00
|
|
|
|
scrollableContent.Clear(false);
|
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
scrollPositionCache.Invalidate();
|
|
|
|
|
|
2019-07-25 11:18:18 +08:00
|
|
|
|
// Run on late scheduler want to ensure this runs after all pending UpdateBeatmapSet / RemoveBeatmapSet operations are run.
|
|
|
|
|
SchedulerAfterChildren.Add(() =>
|
2019-06-30 21:23:48 +08:00
|
|
|
|
{
|
|
|
|
|
BeatmapSetsChanged?.Invoke();
|
|
|
|
|
BeatmapSetsLoaded = true;
|
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly List<float> yPositions = new List<float>();
|
2019-08-09 18:12:29 +08:00
|
|
|
|
private readonly Cached itemsCache = new Cached();
|
|
|
|
|
private readonly Cached scrollPositionCache = new Cached();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
private readonly Container<DrawableCarouselItem> scrollableContent;
|
|
|
|
|
|
2018-04-13 18:50:37 +08:00
|
|
|
|
public Bindable<bool> RightClickScrollingEnabled = new Bindable<bool>();
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public Bindable<RandomSelectAlgorithm> RandomAlgorithm = new Bindable<RandomSelectAlgorithm>();
|
|
|
|
|
private readonly List<CarouselBeatmapSet> previouslyVisitedRandomSets = new List<CarouselBeatmapSet>();
|
|
|
|
|
private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>();
|
|
|
|
|
|
|
|
|
|
protected List<DrawableCarouselItem> Items = new List<DrawableCarouselItem>();
|
|
|
|
|
private CarouselRoot root;
|
|
|
|
|
|
|
|
|
|
public BeatmapCarousel()
|
|
|
|
|
{
|
|
|
|
|
root = new CarouselRoot(this);
|
2019-08-15 13:00:12 +08:00
|
|
|
|
InternalChild = new OsuContextMenuContainer
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-08-15 13:00:12 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2019-08-15 17:27:45 +08:00
|
|
|
|
Child = scroll = new CarouselScrollContainer
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-08-15 13:00:12 +08:00
|
|
|
|
Masking = false,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Child = scrollableContent = new Container<DrawableCarouselItem>
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader(permitNulls: true)]
|
2019-06-30 21:23:48 +08:00
|
|
|
|
private void load(OsuConfigManager config, BeatmapManager beatmaps)
|
2018-04-13 17:19:50 +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
|
|
|
|
|
2019-08-15 13:00:12 +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
|
|
|
|
|
|
|
|
|
loadBeatmapSets(beatmaps.GetAllUsableBeatmapSetsEnumerable());
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 11:18:18 +08:00
|
|
|
|
public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet) => Schedule(() =>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-07-25 11:18:18 +08:00
|
|
|
|
var existingSet = beatmapSets.FirstOrDefault(b => b.BeatmapSet.ID == beatmapSet.ID);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-07-25 11:18:18 +08:00
|
|
|
|
if (existingSet == null)
|
|
|
|
|
return;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-07-25 11:18:18 +08:00
|
|
|
|
root.RemoveChild(existingSet);
|
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-26 10:40:33 +08:00
|
|
|
|
public void UpdateBeatmapSet(BeatmapSetInfo beatmapSet) => Schedule(() =>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-06-26 10:40:33 +08:00
|
|
|
|
int? previouslySelectedID = null;
|
|
|
|
|
CarouselBeatmapSet existingSet = beatmapSets.FirstOrDefault(b => b.BeatmapSet.ID == beatmapSet.ID);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-26 10:40:33 +08:00
|
|
|
|
// 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?.Beatmap.ID;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-26 10:40:33 +08:00
|
|
|
|
var newSet = createCarouselSet(beatmapSet);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-26 10:40:33 +08:00
|
|
|
|
if (existingSet != null)
|
|
|
|
|
root.RemoveChild(existingSet);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-26 10:40:33 +08:00
|
|
|
|
if (newSet == null)
|
|
|
|
|
{
|
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-26 10:40:33 +08:00
|
|
|
|
root.AddChild(newSet);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-26 10:40:33 +08:00
|
|
|
|
applyActiveCriteria(false, false);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-26 10:40:33 +08:00
|
|
|
|
//check if we can/need to maintain our current selection.
|
|
|
|
|
if (previouslySelectedID != null)
|
|
|
|
|
select((CarouselItem)newSet.Beatmaps.FirstOrDefault(b => b.Beatmap.ID == previouslySelectedID) ?? newSet);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-26 10:40:33 +08:00
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
Schedule(() => BeatmapSetsChanged?.Invoke());
|
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Selects a given beatmap on the carousel.
|
|
|
|
|
///
|
|
|
|
|
/// If bypassFilters is false, we will try to select another unfiltered beatmap in the same set. If the
|
|
|
|
|
/// entire set is filtered, no selection is made.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="beatmap">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>
|
|
|
|
|
public bool SelectBeatmap(BeatmapInfo beatmap, bool bypassFilters = true)
|
|
|
|
|
{
|
|
|
|
|
if (beatmap?.Hidden != false)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
foreach (CarouselBeatmapSet set in beatmapSets)
|
|
|
|
|
{
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (!bypassFilters && set.Filtered.Value)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var item = set.Beatmaps.FirstOrDefault(p => p.Beatmap.Equals(beatmap));
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
// The beatmap that needs to be selected doesn't exist in this set
|
|
|
|
|
continue;
|
|
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (!bypassFilters && item.Filtered.Value)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
// The beatmap exists in this set but is filtered, so look for the first unfiltered map in the set
|
2019-02-21 17:56:34 +08:00
|
|
|
|
item = set.Beatmaps.FirstOrDefault(b => !b.Filtered.Value);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
|
|
|
|
select(item);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2019-02-21 17:56:34 +08:00
|
|
|
|
var visibleItems = Items.Where(s => !s.Item.Filtered.Value).ToList();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
if (!visibleItems.Any())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
DrawableCarouselItem drawable = null;
|
|
|
|
|
|
|
|
|
|
if (selectedBeatmap != null && (drawable = selectedBeatmap.Drawables.FirstOrDefault()) == null)
|
|
|
|
|
// if the selected beatmap isn't present yet, we can't correctly change selection.
|
|
|
|
|
// we can fix this by changing this method to not reference drawables / Items in the first place.
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
int originalIndex = visibleItems.IndexOf(drawable);
|
|
|
|
|
int currentIndex = originalIndex;
|
|
|
|
|
|
|
|
|
|
// local function to increment the index in the required direction, wrapping over extremities.
|
|
|
|
|
int incrementIndex() => currentIndex = (currentIndex + direction + visibleItems.Count) % visibleItems.Count;
|
|
|
|
|
|
|
|
|
|
while (incrementIndex() != originalIndex)
|
|
|
|
|
{
|
|
|
|
|
var item = visibleItems[currentIndex].Item;
|
|
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (item.Filtered.Value || item.State.Value == CarouselItemState.Selected) continue;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
switch (item)
|
|
|
|
|
{
|
|
|
|
|
case CarouselBeatmap beatmap:
|
|
|
|
|
if (skipDifficulties) continue;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
select(beatmap);
|
|
|
|
|
return;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
case CarouselBeatmapSet set:
|
|
|
|
|
if (skipDifficulties)
|
|
|
|
|
select(set);
|
|
|
|
|
else
|
2019-02-21 17:56:34 +08:00
|
|
|
|
select(direction > 0 ? set.Beatmaps.First(b => !b.Filtered.Value) : set.Beatmaps.Last(b => !b.Filtered.Value));
|
2018-04-13 17:19:50 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Select the next beatmap in the random sequence.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if a selection could be made, else False.</returns>
|
|
|
|
|
public bool SelectNextRandom()
|
|
|
|
|
{
|
2019-02-21 17:56:34 +08:00
|
|
|
|
var visibleSets = beatmapSets.Where(s => !s.Filtered.Value).ToList();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (RandomAlgorithm.Value == RandomSelectAlgorithm.RandomPermutation)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
var notYetVisitedSets = visibleSets.Except(previouslyVisitedRandomSets).ToList();
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
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));
|
|
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
|
var visibleBeatmaps = set.Beatmaps.Where(s => !s.Filtered.Value).ToList();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
select(visibleBeatmaps[RNG.Next(visibleBeatmaps.Count)]);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPreviousRandom()
|
|
|
|
|
{
|
|
|
|
|
while (randomSelectedBeatmaps.Any())
|
|
|
|
|
{
|
|
|
|
|
var beatmap = randomSelectedBeatmaps.Pop();
|
|
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (!beatmap.Filtered.Value)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (RandomAlgorithm.Value == RandomSelectAlgorithm.RandomPermutation)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
previouslyVisitedRandomSets.Remove(selectedBeatmapSet);
|
|
|
|
|
select(beatmap);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void select(CarouselItem item)
|
|
|
|
|
{
|
2019-03-21 20:02:45 +08:00
|
|
|
|
if (!AllowSelection)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
if (item == null) return;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
item.State.Value = CarouselItemState.Selected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private FilterCriteria activeCriteria = new FilterCriteria();
|
|
|
|
|
|
2018-07-18 09:12:14 +08:00
|
|
|
|
protected ScheduledDelegate PendingFilter;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
public bool AllowSelection = true;
|
|
|
|
|
|
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>
|
2019-07-26 14:22:29 +08:00
|
|
|
|
private float visibleHalfHeight => (DrawHeight + bleed_bottom + bleed_top) / 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>
|
2019-08-15 13:00:12 +08:00
|
|
|
|
private float visibleBottomBound => scroll.Current + DrawHeight + bleed_bottom;
|
2019-07-26 14:13:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The position of the upper visible bound with respect to the current scroll position.
|
|
|
|
|
/// </summary>
|
2019-08-15 13:00:12 +08:00
|
|
|
|
private float visibleUpperBound => scroll.Current - bleed_top;
|
2019-07-26 14:13:10 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public void FlushPendingFilterOperations()
|
|
|
|
|
{
|
2018-07-18 09:12:14 +08:00
|
|
|
|
if (PendingFilter?.Completed == false)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
applyActiveCriteria(false, false);
|
|
|
|
|
Update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Filter(FilterCriteria newCriteria, bool debounce = true)
|
|
|
|
|
{
|
|
|
|
|
if (newCriteria != null)
|
|
|
|
|
activeCriteria = newCriteria;
|
|
|
|
|
|
|
|
|
|
applyActiveCriteria(debounce, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void applyActiveCriteria(bool debounce, bool scroll)
|
|
|
|
|
{
|
|
|
|
|
if (root.Children.Any() != true) return;
|
|
|
|
|
|
|
|
|
|
void perform()
|
|
|
|
|
{
|
2018-07-18 09:12:14 +08:00
|
|
|
|
PendingFilter = null;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
root.Filter(activeCriteria);
|
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
if (scroll) scrollPositionCache.Invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 09:12:14 +08:00
|
|
|
|
PendingFilter?.Cancel();
|
|
|
|
|
PendingFilter = null;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
if (debounce)
|
2018-07-18 09:12:14 +08:00
|
|
|
|
PendingFilter = Scheduler.AddDelayed(perform, 250);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
else
|
|
|
|
|
perform();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float? scrollTarget;
|
|
|
|
|
|
|
|
|
|
public void ScrollToSelected() => scrollPositionCache.Invalidate();
|
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
int direction = 0;
|
|
|
|
|
bool skipDifficulties = false;
|
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
switch (e.Key)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
case Key.Up:
|
|
|
|
|
direction = -1;
|
|
|
|
|
break;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
case Key.Down:
|
|
|
|
|
direction = 1;
|
|
|
|
|
break;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
case Key.Left:
|
|
|
|
|
direction = -1;
|
|
|
|
|
skipDifficulties = true;
|
|
|
|
|
break;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
case Key.Right:
|
|
|
|
|
direction = 1;
|
|
|
|
|
skipDifficulties = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (direction == 0)
|
2018-10-02 11:02:47 +08:00
|
|
|
|
return base.OnKeyDown(e);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
SelectNext(direction, skipDifficulties);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 11:56:55 +08:00
|
|
|
|
protected override bool ReceivePositionalInputAtSubTree(Vector2 screenSpacePos) => ReceivePositionalInputAt(screenSpacePos);
|
2019-07-26 14:13:10 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
|
|
if (!itemsCache.IsValid)
|
|
|
|
|
updateItems();
|
|
|
|
|
|
|
|
|
|
// Remove all items that should no longer be on-screen
|
2019-07-26 14:13:10 +08:00
|
|
|
|
scrollableContent.RemoveAll(p => p.Y < visibleUpperBound - p.DrawHeight || p.Y > visibleBottomBound || !p.IsPresent);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
// Find index range of all items that should be on-screen
|
|
|
|
|
Trace.Assert(Items.Count == yPositions.Count);
|
|
|
|
|
|
2019-07-26 14:13:10 +08:00
|
|
|
|
int firstIndex = yPositions.BinarySearch(visibleUpperBound - DrawableCarouselItem.MAX_HEIGHT);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
if (firstIndex < 0) firstIndex = ~firstIndex;
|
2019-07-26 14:13:10 +08:00
|
|
|
|
int lastIndex = yPositions.BinarySearch(visibleBottomBound);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
if (lastIndex < 0) lastIndex = ~lastIndex;
|
|
|
|
|
|
|
|
|
|
int notVisibleCount = 0;
|
|
|
|
|
|
|
|
|
|
// Add those items within the previously found index range that should be displayed.
|
|
|
|
|
for (int i = firstIndex; i < lastIndex; ++i)
|
|
|
|
|
{
|
|
|
|
|
DrawableCarouselItem item = Items[i];
|
|
|
|
|
|
|
|
|
|
if (!item.Item.Visible)
|
|
|
|
|
{
|
|
|
|
|
if (!item.IsPresent)
|
|
|
|
|
notVisibleCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float depth = i + (item is DrawableCarouselBeatmapSet ? -Items.Count : 0);
|
|
|
|
|
|
|
|
|
|
// Only add if we're not already part of the content.
|
|
|
|
|
if (!scrollableContent.Contains(item))
|
|
|
|
|
{
|
|
|
|
|
// Makes sure headers are always _below_ items,
|
|
|
|
|
// and depth flows downward.
|
|
|
|
|
item.Depth = depth;
|
|
|
|
|
|
|
|
|
|
switch (item.LoadState)
|
|
|
|
|
{
|
|
|
|
|
case LoadState.NotLoaded:
|
|
|
|
|
LoadComponentAsync(item);
|
|
|
|
|
break;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
case LoadState.Loading:
|
|
|
|
|
break;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
default:
|
|
|
|
|
scrollableContent.Add(item);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
scrollableContent.ChangeChildDepth(item, depth);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this is not actually useful right now, but once we have groups may well be.
|
|
|
|
|
if (notVisibleCount > 50)
|
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
|
|
|
|
|
// Update externally controlled state of currently visible items
|
|
|
|
|
// (e.g. x-offset and opacity).
|
|
|
|
|
foreach (DrawableCarouselItem p in scrollableContent.Children)
|
2019-07-26 14:22:29 +08:00
|
|
|
|
updateItem(p);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 18:38:39 +08:00
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
|
{
|
|
|
|
|
base.UpdateAfterChildren();
|
|
|
|
|
|
|
|
|
|
if (!scrollPositionCache.IsValid)
|
|
|
|
|
updateScrollPosition();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 12:27:17 +08:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
|
|
// aggressively dispose "off-screen" items to reduce GC pressure.
|
|
|
|
|
foreach (var i in Items)
|
|
|
|
|
i.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (b.Metadata == null)
|
|
|
|
|
b.Metadata = beatmapSet.Metadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var set = new CarouselBeatmapSet(beatmapSet);
|
|
|
|
|
|
|
|
|
|
foreach (var c in set.Beatmaps)
|
|
|
|
|
{
|
2019-02-22 16:51:39 +08:00
|
|
|
|
c.State.ValueChanged += state =>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-02-22 16:51:39 +08:00
|
|
|
|
if (state.NewValue == CarouselItemState.Selected)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
selectedBeatmapSet = set;
|
|
|
|
|
SelectionChanged?.Invoke(c.Beatmap);
|
|
|
|
|
|
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
scrollPositionCache.Invalidate();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Computes the target Y positions for every item in the carousel.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The Y position of the currently selected item.</returns>
|
|
|
|
|
private void updateItems()
|
|
|
|
|
{
|
|
|
|
|
Items = root.Drawables.ToList();
|
|
|
|
|
|
|
|
|
|
yPositions.Clear();
|
|
|
|
|
|
2019-07-26 14:22:29 +08:00
|
|
|
|
float currentY = visibleHalfHeight;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
DrawableCarouselBeatmapSet lastSet = null;
|
|
|
|
|
|
|
|
|
|
scrollTarget = null;
|
|
|
|
|
|
|
|
|
|
foreach (DrawableCarouselItem d in Items)
|
|
|
|
|
{
|
|
|
|
|
if (d.IsPresent)
|
|
|
|
|
{
|
|
|
|
|
switch (d)
|
|
|
|
|
{
|
|
|
|
|
case DrawableCarouselBeatmapSet set:
|
|
|
|
|
lastSet = set;
|
|
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
|
set.MoveToX(set.Item.State.Value == CarouselItemState.Selected ? -100 : 0, 500, Easing.OutExpo);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
set.MoveToY(currentY, 750, Easing.OutExpo);
|
|
|
|
|
break;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
case DrawableCarouselBeatmap beatmap:
|
|
|
|
|
if (beatmap.Item.State.Value == CarouselItemState.Selected)
|
|
|
|
|
scrollTarget = currentY + beatmap.DrawHeight / 2 - DrawHeight / 2;
|
|
|
|
|
|
|
|
|
|
void performMove(float y, float? startY = null)
|
|
|
|
|
{
|
|
|
|
|
if (startY != null) beatmap.MoveTo(new Vector2(0, startY.Value));
|
2019-02-21 17:56:34 +08:00
|
|
|
|
beatmap.MoveToX(beatmap.Item.State.Value == CarouselItemState.Selected ? -50 : 0, 500, Easing.OutExpo);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
beatmap.MoveToY(y, 750, Easing.OutExpo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Assert(lastSet != null);
|
|
|
|
|
|
|
|
|
|
float? setY = null;
|
|
|
|
|
if (!d.IsLoaded || beatmap.Alpha == 0) // can't use IsPresent due to DrawableCarouselItem override.
|
|
|
|
|
setY = lastSet.Y + lastSet.DrawHeight + 5;
|
|
|
|
|
|
|
|
|
|
if (d.IsLoaded)
|
|
|
|
|
performMove(currentY, setY);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
float y = currentY;
|
2019-03-17 12:43:23 +08:00
|
|
|
|
d.OnLoadComplete += _ => performMove(y, setY);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yPositions.Add(currentY);
|
|
|
|
|
|
|
|
|
|
if (d.Item.Visible)
|
|
|
|
|
currentY += d.DrawHeight + 5;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 14:22:29 +08:00
|
|
|
|
currentY += visibleHalfHeight;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
scrollableContent.Height = currentY;
|
|
|
|
|
|
2019-03-21 19:51:06 +08:00
|
|
|
|
if (BeatmapSetsLoaded && (selectedBeatmapSet == null || selectedBeatmap == null || selectedBeatmapSet.State.Value != CarouselItemState.Selected))
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
selectedBeatmapSet = null;
|
|
|
|
|
SelectionChanged?.Invoke(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
itemsCache.Validate();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-22 09:51:49 +08:00
|
|
|
|
private bool firstScroll = true;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
private void updateScrollPosition()
|
|
|
|
|
{
|
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.
|
|
|
|
|
scroll.ScrollTo(scrollTarget.Value - 200, false);
|
|
|
|
|
firstScroll = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 18:38:39 +08:00
|
|
|
|
scroll.ScrollTo(scrollTarget.Value);
|
|
|
|
|
scrollPositionCache.Validate();
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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;
|
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
|
|
|
|
|
|
|
|
|
return 125 + x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update a item's x position and multiplicative alpha based on its y position and
|
|
|
|
|
/// the current scroll position.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p">The item to be updated.</param>
|
2019-07-26 14:22:29 +08:00
|
|
|
|
private void updateItem(DrawableCarouselItem p)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-07-26 14:13:10 +08:00
|
|
|
|
float itemDrawY = p.Position.Y - visibleUpperBound + p.DrawHeight / 2;
|
2019-07-26 14:22:29 +08:00
|
|
|
|
float dist = Math.Abs(1f - itemDrawY / visibleHalfHeight);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
// Setting the origin position serves as an additive position on top of potential
|
|
|
|
|
// local transformation we may want to apply (e.g. when a item gets selected, we
|
|
|
|
|
// may want to smoothly transform it leftwards.)
|
2019-07-26 14:22:29 +08:00
|
|
|
|
p.OriginPosition = new Vector2(-offsetX(dist, visibleHalfHeight), 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 transformations on top, with a similar reasoning to the previous comment.
|
2019-11-20 20:19:49 +08:00
|
|
|
|
p.SetMultiplicativeAlpha(Math.Clamp(1.75f - 1.5f * dist, 0, 1));
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class CarouselRoot : CarouselGroupEagerSelect
|
|
|
|
|
{
|
|
|
|
|
private readonly BeatmapCarousel carousel;
|
|
|
|
|
|
|
|
|
|
public CarouselRoot(BeatmapCarousel carousel)
|
|
|
|
|
{
|
|
|
|
|
this.carousel = carousel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PerformSelection()
|
|
|
|
|
{
|
|
|
|
|
if (LastSelected == null)
|
|
|
|
|
carousel.SelectNextRandom();
|
|
|
|
|
else
|
|
|
|
|
base.PerformSelection();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-15 17:27:45 +08:00
|
|
|
|
|
|
|
|
|
private class CarouselScrollContainer : OsuScrollContainer
|
|
|
|
|
{
|
|
|
|
|
private bool rightMouseScrollBlocked;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|