2017-02-07 12:59:30 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-12-06 17:56:20 +08:00
|
|
|
|
|
2017-02-02 16:33:39 +08:00
|
|
|
|
using OpenTK;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-06-01 00:41:15 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2016-12-15 21:57:14 +08:00
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using OpenTK.Input;
|
2017-02-02 16:33:39 +08:00
|
|
|
|
using osu.Framework.MathUtils;
|
2017-03-04 15:54:14 +08:00
|
|
|
|
using System.Diagnostics;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using osu.Framework.Allocation;
|
2017-12-12 16:48:38 +08:00
|
|
|
|
using osu.Framework.Caching;
|
2017-03-17 18:54:51 +08:00
|
|
|
|
using osu.Framework.Threading;
|
2017-06-01 00:41:15 +08:00
|
|
|
|
using osu.Framework.Configuration;
|
2017-12-14 19:40:58 +08:00
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2017-07-26 12:22:46 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
2017-07-21 18:13:53 +08:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2017-09-14 14:41:32 +08:00
|
|
|
|
using osu.Game.Graphics.Cursor;
|
2017-12-12 16:48:38 +08:00
|
|
|
|
using osu.Game.Screens.Select.Carousel;
|
2016-11-25 17:14:56 +08:00
|
|
|
|
|
2016-11-21 03:34:16 +08:00
|
|
|
|
namespace osu.Game.Screens.Select
|
|
|
|
|
{
|
2017-11-21 10:49:42 +08:00
|
|
|
|
public class BeatmapCarousel : OsuScrollContainer
|
2016-11-21 03:34:16 +08:00
|
|
|
|
{
|
2017-12-13 18:56:16 +08:00
|
|
|
|
/// <summary>
|
2017-12-14 11:59:35 +08:00
|
|
|
|
/// Triggered when the <see cref="BeatmapSets"/> loaded change and are completely loaded.
|
2017-12-13 18:56:16 +08:00
|
|
|
|
/// </summary>
|
2017-12-14 11:59:35 +08:00
|
|
|
|
public Action BeatmapSetsChanged;
|
2017-12-13 18:56:16 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The currently selected beatmap.
|
|
|
|
|
/// </summary>
|
2017-12-12 16:48:38 +08:00
|
|
|
|
public BeatmapInfo SelectedBeatmap => selectedBeatmap?.Beatmap;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-14 11:59:35 +08:00
|
|
|
|
private CarouselBeatmap selectedBeatmap => selectedBeatmapSet?.Beatmaps.FirstOrDefault(s => s.State == CarouselItemState.Selected);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The currently selected beatmap set.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public BeatmapSetInfo SelectedBeatmapSet => selectedBeatmapSet?.BeatmapSet;
|
|
|
|
|
|
2017-12-17 04:53:13 +08:00
|
|
|
|
private CarouselBeatmapSet selectedBeatmapSet;
|
2017-12-14 11:59:35 +08:00
|
|
|
|
|
2017-12-13 18:56:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raised when the <see cref="SelectedBeatmap"/> is changed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<BeatmapInfo> SelectionChanged;
|
2017-07-20 10:50:31 +08:00
|
|
|
|
|
2017-12-13 18:56:16 +08:00
|
|
|
|
public override bool HandleInput => AllowSelection;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-27 11:30:30 +08:00
|
|
|
|
/// <summary>
|
2017-12-27 22:14:48 +08:00
|
|
|
|
/// Used to avoid firing null selections before the initial beatmaps have been loaded via <see cref="BeatmapSets"/>.
|
2017-12-27 11:30:30 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
private bool initialLoadComplete;
|
|
|
|
|
|
2017-12-18 10:57:13 +08:00
|
|
|
|
private IEnumerable<CarouselBeatmapSet> beatmapSets => root.Children.OfType<CarouselBeatmapSet>();
|
2017-12-14 12:49:43 +08:00
|
|
|
|
|
2017-12-14 11:59:35 +08:00
|
|
|
|
public IEnumerable<BeatmapSetInfo> BeatmapSets
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-12-14 12:49:43 +08:00
|
|
|
|
get { return beatmapSets.Select(g => g.BeatmapSet); }
|
2017-03-17 18:12:15 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2017-12-15 23:33:09 +08:00
|
|
|
|
CarouselGroup newRoot = new CarouselGroupEagerSelect();
|
2017-12-14 19:40:58 +08:00
|
|
|
|
|
2017-03-17 18:12:15 +08:00
|
|
|
|
Task.Run(() =>
|
2017-12-09 15:42:59 +08:00
|
|
|
|
{
|
2017-12-14 19:40:58 +08:00
|
|
|
|
value.Select(createCarouselSet).Where(g => g != null).ForEach(newRoot.AddChild);
|
2017-12-16 15:27:39 +08:00
|
|
|
|
newRoot.Filter(activeCriteria);
|
2017-12-18 05:59:32 +08:00
|
|
|
|
|
|
|
|
|
// preload drawables as the ctor overhead is quite high currently.
|
2017-12-18 07:05:57 +08:00
|
|
|
|
var _ = newRoot.Drawables;
|
|
|
|
|
}).ContinueWith(_ => Schedule(() =>
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-12-18 07:05:57 +08:00
|
|
|
|
root = newRoot;
|
|
|
|
|
scrollableContent.Clear(false);
|
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
scrollPositionCache.Invalidate();
|
2017-12-24 16:02:46 +08:00
|
|
|
|
|
2017-12-27 11:30:30 +08:00
|
|
|
|
Schedule(() =>
|
|
|
|
|
{
|
|
|
|
|
BeatmapSetsChanged?.Invoke();
|
|
|
|
|
initialLoadComplete = true;
|
|
|
|
|
});
|
2017-12-18 07:05:57 +08:00
|
|
|
|
}));
|
2017-03-17 18:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 12:41:50 +08:00
|
|
|
|
private readonly List<float> yPositions = new List<float>();
|
2017-12-18 01:23:03 +08:00
|
|
|
|
private Cached itemsCache = new Cached();
|
|
|
|
|
private Cached scrollPositionCache = new Cached();
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
private readonly Container<DrawableCarouselItem> scrollableContent;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-14 11:59:35 +08:00
|
|
|
|
public Bindable<RandomSelectAlgorithm> RandomAlgorithm = new Bindable<RandomSelectAlgorithm>();
|
|
|
|
|
private readonly List<CarouselBeatmapSet> previouslyVisitedRandomSets = new List<CarouselBeatmapSet>();
|
2017-12-14 15:59:08 +08:00
|
|
|
|
private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>();
|
2017-06-01 00:41:15 +08:00
|
|
|
|
|
2017-12-13 20:08:12 +08:00
|
|
|
|
protected List<DrawableCarouselItem> Items = new List<DrawableCarouselItem>();
|
2017-12-15 23:33:09 +08:00
|
|
|
|
private CarouselGroup root = new CarouselGroupEagerSelect();
|
2016-11-21 14:59:46 +08:00
|
|
|
|
|
2017-03-17 18:12:54 +08:00
|
|
|
|
public BeatmapCarousel()
|
2017-01-18 08:18:15 +08:00
|
|
|
|
{
|
2017-12-14 12:49:43 +08:00
|
|
|
|
Child = new OsuContextMenuContainer
|
2017-01-18 08:18:15 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2017-09-14 14:41:32 +08:00
|
|
|
|
AutoSizeAxes = Axes.Y,
|
2017-12-12 16:48:38 +08:00
|
|
|
|
Child = scrollableContent = new Container<DrawableCarouselItem>
|
2017-09-14 14:41:32 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
}
|
2017-12-14 12:49:43 +08:00
|
|
|
|
};
|
2017-01-18 08:18:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-18 11:30:39 +08:00
|
|
|
|
[BackgroundDependencyLoader(permitNulls: true)]
|
|
|
|
|
private void load(OsuConfigManager config)
|
|
|
|
|
{
|
|
|
|
|
config.BindWith(OsuSetting.RandomSelectAlgorithm, RandomAlgorithm);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-13 18:56:16 +08:00
|
|
|
|
public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet)
|
2017-10-18 17:36:35 +08:00
|
|
|
|
{
|
2017-12-18 01:46:53 +08:00
|
|
|
|
Schedule(() =>
|
|
|
|
|
{
|
|
|
|
|
var existingSet = beatmapSets.FirstOrDefault(b => b.BeatmapSet.ID == beatmapSet.ID);
|
2017-12-14 15:59:08 +08:00
|
|
|
|
|
2017-12-18 01:46:53 +08:00
|
|
|
|
if (existingSet == null)
|
|
|
|
|
return;
|
2017-12-14 15:59:08 +08:00
|
|
|
|
|
2017-12-18 01:46:53 +08:00
|
|
|
|
root.RemoveChild(existingSet);
|
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
});
|
2017-10-18 17:36:35 +08:00
|
|
|
|
}
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
2017-12-08 05:02:53 +08:00
|
|
|
|
public void UpdateBeatmapSet(BeatmapSetInfo beatmapSet)
|
2017-07-28 13:22:14 +08:00
|
|
|
|
{
|
2017-12-18 01:46:53 +08:00
|
|
|
|
Schedule(() =>
|
|
|
|
|
{
|
|
|
|
|
CarouselBeatmapSet existingSet = beatmapSets.FirstOrDefault(b => b.BeatmapSet.ID == beatmapSet.ID);
|
2017-12-12 16:48:38 +08:00
|
|
|
|
|
2017-12-18 01:46:53 +08:00
|
|
|
|
bool hadSelection = existingSet?.State?.Value == CarouselItemState.Selected;
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
2017-12-18 01:46:53 +08:00
|
|
|
|
var newSet = createCarouselSet(beatmapSet);
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
2017-12-18 01:46:53 +08:00
|
|
|
|
if (existingSet != null)
|
|
|
|
|
root.RemoveChild(existingSet);
|
2017-10-20 09:50:00 +08:00
|
|
|
|
|
2017-12-18 01:46:53 +08:00
|
|
|
|
if (newSet == null)
|
|
|
|
|
{
|
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-10-20 09:50:00 +08:00
|
|
|
|
|
2017-12-18 01:46:53 +08:00
|
|
|
|
root.AddChild(newSet);
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
2017-12-18 01:46:53 +08:00
|
|
|
|
applyActiveCriteria(false, false);
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
2017-12-18 01:46:53 +08:00
|
|
|
|
//check if we can/need to maintain our current selection.
|
|
|
|
|
if (hadSelection)
|
|
|
|
|
select((CarouselItem)newSet.Beatmaps.FirstOrDefault(b => b.Beatmap.ID == selectedBeatmap?.Beatmap.ID) ?? newSet);
|
2017-12-14 15:59:08 +08:00
|
|
|
|
|
2017-12-18 01:46:53 +08:00
|
|
|
|
itemsCache.Invalidate();
|
2017-12-24 16:02:46 +08:00
|
|
|
|
Schedule(() => BeatmapSetsChanged?.Invoke());
|
2017-12-18 01:46:53 +08:00
|
|
|
|
});
|
2017-07-28 13:22:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 15:27:39 +08:00
|
|
|
|
public void SelectBeatmap(BeatmapInfo beatmap)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-12-16 20:25:54 +08:00
|
|
|
|
if (beatmap?.Hidden != false)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
return;
|
2017-05-17 16:15:25 +08:00
|
|
|
|
|
2017-12-14 12:49:43 +08:00
|
|
|
|
foreach (CarouselBeatmapSet group in beatmapSets)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
var item = group.Beatmaps.FirstOrDefault(p => p.Beatmap.Equals(beatmap));
|
|
|
|
|
if (item != null)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-12-13 11:46:02 +08:00
|
|
|
|
select(item);
|
2017-03-17 18:12:15 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2017-12-23 19:53:11 +08:00
|
|
|
|
if (!Items.Any())
|
|
|
|
|
return;
|
|
|
|
|
|
2017-12-28 11:38:40 +08:00
|
|
|
|
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.
|
2017-12-28 10:35:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2017-12-28 11:38:40 +08:00
|
|
|
|
int originalIndex = Items.IndexOf(drawable);
|
2017-12-11 19:22:10 +08:00
|
|
|
|
int currentIndex = originalIndex;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-11 19:22:10 +08:00
|
|
|
|
// local function to increment the index in the required direction, wrapping over extremities.
|
2017-12-13 20:08:12 +08:00
|
|
|
|
int incrementIndex() => currentIndex = (currentIndex + direction + Items.Count) % Items.Count;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
while (incrementIndex() != originalIndex)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-12-13 20:08:12 +08:00
|
|
|
|
var item = Items[currentIndex].Item;
|
2017-05-22 07:53:36 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (item.Filtered || item.State == CarouselItemState.Selected) continue;
|
2017-12-11 19:22:10 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
switch (item)
|
2017-12-11 19:22:10 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
case CarouselBeatmap beatmap:
|
|
|
|
|
if (skipDifficulties) continue;
|
|
|
|
|
select(beatmap);
|
|
|
|
|
return;
|
|
|
|
|
case CarouselBeatmapSet set:
|
2017-12-17 04:53:13 +08:00
|
|
|
|
if (skipDifficulties)
|
|
|
|
|
select(set);
|
|
|
|
|
else
|
2017-12-18 10:43:10 +08:00
|
|
|
|
select(direction > 0 ? set.Beatmaps.First(b => !b.Filtered) : set.Beatmaps.Last(b => !b.Filtered));
|
2017-12-12 16:48:38 +08:00
|
|
|
|
return;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
}
|
2017-12-12 16:48:38 +08:00
|
|
|
|
}
|
2017-03-17 18:12:15 +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
|
|
|
|
{
|
2017-12-17 04:53:13 +08:00
|
|
|
|
var visible = beatmapSets.Where(s => !s.Filtered).ToList();
|
2017-12-14 11:59:35 +08:00
|
|
|
|
if (!visible.Any())
|
2017-12-31 11:55:53 +08:00
|
|
|
|
return false;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (selectedBeatmap != null)
|
2017-12-14 11:59:35 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
randomSelectedBeatmaps.Push(selectedBeatmap);
|
2017-07-29 22:42:32 +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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CarouselBeatmapSet set;
|
2017-06-02 01:54:42 +08:00
|
|
|
|
|
2017-12-14 11:59:35 +08:00
|
|
|
|
if (RandomAlgorithm == RandomSelectAlgorithm.RandomPermutation)
|
2017-06-01 00:41:15 +08:00
|
|
|
|
{
|
2017-12-14 11:59:35 +08:00
|
|
|
|
var notYetVisitedSets = visible.Except(previouslyVisitedRandomSets).ToList();
|
|
|
|
|
if (!notYetVisitedSets.Any())
|
2017-06-01 02:31:05 +08:00
|
|
|
|
{
|
2017-12-14 11:59:35 +08:00
|
|
|
|
previouslyVisitedRandomSets.Clear();
|
|
|
|
|
notYetVisitedSets = visible;
|
2017-06-01 00:41:15 +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-14 15:59:08 +08:00
|
|
|
|
set = visible.ElementAt(RNG.Next(visible.Count));
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-14 19:40:58 +08:00
|
|
|
|
select(set.Beatmaps.Skip(RNG.Next(set.Beatmaps.Count())).FirstOrDefault());
|
2017-12-31 11:55:53 +08:00
|
|
|
|
return true;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-05 17:24:28 +08:00
|
|
|
|
public void SelectPreviousRandom()
|
2017-06-02 01:54:42 +08:00
|
|
|
|
{
|
2017-06-05 17:24:28 +08:00
|
|
|
|
while (randomSelectedBeatmaps.Any())
|
2017-06-02 01:54:42 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
var beatmap = randomSelectedBeatmaps.Pop();
|
|
|
|
|
|
2017-12-14 11:59:35 +08:00
|
|
|
|
if (!beatmap.Filtered)
|
2017-06-02 01:54:42 +08:00
|
|
|
|
{
|
2017-12-14 11:59:35 +08:00
|
|
|
|
if (RandomAlgorithm == RandomSelectAlgorithm.RandomPermutation)
|
|
|
|
|
previouslyVisitedRandomSets.Remove(selectedBeatmapSet);
|
2017-12-12 16:48:38 +08:00
|
|
|
|
select(beatmap);
|
2017-06-02 01:54:42 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-18 11:30:39 +08:00
|
|
|
|
private void select(CarouselItem item)
|
|
|
|
|
{
|
|
|
|
|
if (item == null) return;
|
|
|
|
|
item.State.Value = CarouselItemState.Selected;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 15:27:39 +08:00
|
|
|
|
private FilterCriteria activeCriteria = new FilterCriteria();
|
2017-03-17 18:54:51 +08:00
|
|
|
|
|
2017-12-13 20:08:12 +08:00
|
|
|
|
protected ScheduledDelegate FilterTask;
|
2017-03-17 18:54:51 +08:00
|
|
|
|
|
2017-07-20 10:50:31 +08:00
|
|
|
|
public bool AllowSelection = true;
|
|
|
|
|
|
2017-12-16 15:27:39 +08:00
|
|
|
|
public void FlushPendingFilterOperations()
|
2017-07-21 16:20:52 +08:00
|
|
|
|
{
|
2017-12-13 20:08:12 +08:00
|
|
|
|
if (FilterTask?.Completed == false)
|
2017-12-16 15:44:42 +08:00
|
|
|
|
applyActiveCriteria(false, false);
|
2017-07-21 16:20:52 +08:00
|
|
|
|
}
|
2017-07-21 04:58:58 +08:00
|
|
|
|
|
2017-12-16 15:27:39 +08:00
|
|
|
|
public void Filter(FilterCriteria newCriteria, bool debounce = true)
|
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;
|
|
|
|
|
|
2017-12-16 15:44:42 +08:00
|
|
|
|
applyActiveCriteria(debounce, true);
|
2017-12-16 15:27:39 +08:00
|
|
|
|
}
|
2017-03-17 18:54:51 +08:00
|
|
|
|
|
2017-12-16 15:44:42 +08:00
|
|
|
|
private void applyActiveCriteria(bool debounce, bool scroll)
|
2017-12-16 15:27:39 +08:00
|
|
|
|
{
|
2017-12-18 10:57:13 +08:00
|
|
|
|
if (root.Children.Any() != true) return;
|
2017-12-16 22:55:55 +08:00
|
|
|
|
|
|
|
|
|
void perform()
|
2016-11-25 17:14:56 +08:00
|
|
|
|
{
|
2017-12-13 20:08:12 +08:00
|
|
|
|
FilterTask = null;
|
2017-03-17 18:54:51 +08:00
|
|
|
|
|
2017-12-16 15:27:39 +08:00
|
|
|
|
root.Filter(activeCriteria);
|
2017-12-18 01:23:03 +08:00
|
|
|
|
itemsCache.Invalidate();
|
2017-12-18 11:30:39 +08:00
|
|
|
|
if (scroll) scrollPositionCache.Invalidate();
|
2017-12-16 22:55:55 +08:00
|
|
|
|
}
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-13 20:08:12 +08:00
|
|
|
|
FilterTask?.Cancel();
|
|
|
|
|
FilterTask = null;
|
2017-07-21 04:58:58 +08:00
|
|
|
|
|
2017-03-17 18:54:51 +08:00
|
|
|
|
if (debounce)
|
2017-12-13 20:08:12 +08:00
|
|
|
|
FilterTask = Scheduler.AddDelayed(perform, 250);
|
2017-03-17 18:54:51 +08:00
|
|
|
|
else
|
|
|
|
|
perform();
|
2016-11-21 03:34:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-17 01:33:01 +08:00
|
|
|
|
private float? scrollTarget;
|
|
|
|
|
|
2017-12-18 01:23:03 +08:00
|
|
|
|
public void ScrollToSelected() => scrollPositionCache.Invalidate();
|
2017-07-29 21:03:17 +08:00
|
|
|
|
|
2017-12-18 11:30:39 +08:00
|
|
|
|
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
int direction = 0;
|
|
|
|
|
bool skipDifficulties = false;
|
|
|
|
|
|
|
|
|
|
switch (args.Key)
|
|
|
|
|
{
|
|
|
|
|
case Key.Up:
|
|
|
|
|
direction = -1;
|
|
|
|
|
break;
|
|
|
|
|
case Key.Down:
|
|
|
|
|
direction = 1;
|
|
|
|
|
break;
|
|
|
|
|
case Key.Left:
|
|
|
|
|
direction = -1;
|
|
|
|
|
skipDifficulties = true;
|
|
|
|
|
break;
|
|
|
|
|
case Key.Right:
|
|
|
|
|
direction = 1;
|
|
|
|
|
skipDifficulties = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (direction == 0)
|
|
|
|
|
return base.OnKeyDown(state, args);
|
|
|
|
|
|
|
|
|
|
SelectNext(direction, skipDifficulties);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
|
|
if (!itemsCache.IsValid)
|
|
|
|
|
updateItems();
|
|
|
|
|
|
|
|
|
|
if (!scrollPositionCache.IsValid)
|
|
|
|
|
updateScrollPosition();
|
|
|
|
|
|
|
|
|
|
float drawHeight = DrawHeight;
|
|
|
|
|
|
|
|
|
|
// Remove all items that should no longer be on-screen
|
|
|
|
|
scrollableContent.RemoveAll(p => p.Y < Current - p.DrawHeight || p.Y > Current + drawHeight || !p.IsPresent);
|
|
|
|
|
|
|
|
|
|
// Find index range of all items that should be on-screen
|
|
|
|
|
Trace.Assert(Items.Count == yPositions.Count);
|
|
|
|
|
|
|
|
|
|
int firstIndex = yPositions.BinarySearch(Current - DrawableCarouselItem.MAX_HEIGHT);
|
|
|
|
|
if (firstIndex < 0) firstIndex = ~firstIndex;
|
|
|
|
|
int lastIndex = yPositions.BinarySearch(Current + drawHeight);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 = i + (item is DrawableCarouselBeatmapSet ? -Items.Count : 0);
|
|
|
|
|
|
|
|
|
|
switch (item.LoadState)
|
|
|
|
|
{
|
|
|
|
|
case LoadState.NotLoaded:
|
|
|
|
|
LoadComponentAsync(item);
|
|
|
|
|
break;
|
|
|
|
|
case LoadState.Loading:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
scrollableContent.Add(item);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
|
float halfHeight = drawHeight / 2;
|
|
|
|
|
foreach (DrawableCarouselItem p in scrollableContent.Children)
|
|
|
|
|
updateItem(p, halfHeight);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-14 15:59:08 +08:00
|
|
|
|
private CarouselBeatmapSet createCarouselSet(BeatmapSetInfo beatmapSet)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-10-20 09:50:00 +08:00
|
|
|
|
if (beatmapSet.Beatmaps.All(b => b.Hidden))
|
|
|
|
|
return null;
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
// todo: remove the need for this.
|
2017-06-07 19:53:37 +08:00
|
|
|
|
foreach (var b in beatmapSet.Beatmaps)
|
2017-04-18 09:04:32 +08:00
|
|
|
|
{
|
|
|
|
|
if (b.Metadata == null)
|
|
|
|
|
b.Metadata = beatmapSet.Metadata;
|
2017-05-06 15:37:53 +08:00
|
|
|
|
}
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
var set = new CarouselBeatmapSet(beatmapSet);
|
|
|
|
|
|
|
|
|
|
foreach (var c in set.Beatmaps)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
c.State.ValueChanged += v =>
|
|
|
|
|
{
|
|
|
|
|
if (v == CarouselItemState.Selected)
|
|
|
|
|
{
|
2017-12-17 04:53:13 +08:00
|
|
|
|
selectedBeatmapSet = set;
|
2017-12-12 16:48:38 +08:00
|
|
|
|
SelectionChanged?.Invoke(c.Beatmap);
|
2017-12-18 01:23:03 +08:00
|
|
|
|
|
|
|
|
|
itemsCache.Invalidate();
|
|
|
|
|
scrollPositionCache.Invalidate();
|
2017-12-12 16:48:38 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return set;
|
2017-03-17 18:12:15 +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>
|
2017-12-18 11:30:39 +08:00
|
|
|
|
private void updateItems()
|
2016-11-21 03:34:16 +08:00
|
|
|
|
{
|
2017-12-18 01:23:03 +08:00
|
|
|
|
Items = root.Drawables.ToList();
|
|
|
|
|
|
2016-11-23 04:40:47 +08:00
|
|
|
|
yPositions.Clear();
|
|
|
|
|
|
2016-11-21 14:59:46 +08:00
|
|
|
|
float currentY = DrawHeight / 2;
|
2017-12-17 01:33:01 +08:00
|
|
|
|
DrawableCarouselBeatmapSet lastSet = null;
|
2016-11-21 14:59:46 +08:00
|
|
|
|
|
2017-12-17 01:33:01 +08:00
|
|
|
|
scrollTarget = null;
|
2017-12-14 23:23:39 +08:00
|
|
|
|
|
2017-12-13 20:08:12 +08:00
|
|
|
|
foreach (DrawableCarouselItem d in Items)
|
2017-12-12 16:48:38 +08:00
|
|
|
|
{
|
2017-12-17 01:33:01 +08:00
|
|
|
|
if (d.IsPresent)
|
2016-11-21 03:34:16 +08:00
|
|
|
|
{
|
2017-12-17 01:33:01 +08:00
|
|
|
|
switch (d)
|
|
|
|
|
{
|
|
|
|
|
case DrawableCarouselBeatmapSet set:
|
|
|
|
|
lastSet = set;
|
|
|
|
|
|
|
|
|
|
set.MoveToX(set.Item.State == CarouselItemState.Selected ? -100 : 0, 500, Easing.OutExpo);
|
2017-12-18 11:30:39 +08:00
|
|
|
|
set.MoveToY(currentY, 750, Easing.OutExpo);
|
2017-12-17 01:33:01 +08:00
|
|
|
|
break;
|
|
|
|
|
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));
|
|
|
|
|
beatmap.MoveToX(beatmap.Item.State == CarouselItemState.Selected ? -50 : 0, 500, Easing.OutExpo);
|
2017-12-18 11:30:39 +08:00
|
|
|
|
beatmap.MoveToY(y, 750, Easing.OutExpo);
|
2017-12-17 01:33:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
d.OnLoadComplete = _ => performMove(y, setY);
|
|
|
|
|
}
|
2017-12-18 11:30:39 +08:00
|
|
|
|
|
2017-12-17 01:33:01 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2016-11-23 03:10:04 +08:00
|
|
|
|
}
|
2017-12-13 11:46:02 +08:00
|
|
|
|
|
|
|
|
|
yPositions.Add(currentY);
|
|
|
|
|
|
|
|
|
|
if (d.Item.Visible)
|
|
|
|
|
currentY += d.DrawHeight + 5;
|
2016-11-21 03:34:16 +08:00
|
|
|
|
}
|
2016-11-21 14:59:46 +08:00
|
|
|
|
|
|
|
|
|
currentY += DrawHeight / 2;
|
|
|
|
|
scrollableContent.Height = currentY;
|
|
|
|
|
|
2017-12-27 11:30:30 +08:00
|
|
|
|
if (initialLoadComplete && (selectedBeatmapSet == null || selectedBeatmap == null || selectedBeatmapSet.State.Value != CarouselItemState.Selected))
|
2017-12-18 01:23:03 +08:00
|
|
|
|
{
|
|
|
|
|
selectedBeatmapSet = null;
|
|
|
|
|
SelectionChanged?.Invoke(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
itemsCache.Validate();
|
2016-11-21 14:59:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-18 11:30:39 +08:00
|
|
|
|
private void updateScrollPosition()
|
2017-12-17 04:53:13 +08:00
|
|
|
|
{
|
2017-12-18 11:30:39 +08:00
|
|
|
|
if (scrollTarget != null) ScrollTo(scrollTarget.Value);
|
2017-12-18 01:23:03 +08:00
|
|
|
|
scrollPositionCache.Validate();
|
2017-12-17 04:53:13 +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;
|
|
|
|
|
double discriminant = Math.Max(0, circle_radius * circle_radius - dist * dist);
|
|
|
|
|
float x = (circle_radius - (float)Math.Sqrt(discriminant)) * halfHeight;
|
2017-02-02 18:37:35 +08:00
|
|
|
|
|
2017-03-17 18:12:15 +08:00
|
|
|
|
return 125 + x;
|
2017-02-24 13:37:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 18:12:15 +08:00
|
|
|
|
/// <summary>
|
2017-12-12 16:48:38 +08:00
|
|
|
|
/// Update a item's x position and multiplicative alpha based on its y position and
|
2017-03-17 18:12:15 +08:00
|
|
|
|
/// the current scroll position.
|
|
|
|
|
/// </summary>
|
2017-12-12 16:48:38 +08:00
|
|
|
|
/// <param name="p">The item to be updated.</param>
|
2017-03-17 18:12:15 +08:00
|
|
|
|
/// <param name="halfHeight">Half the draw height of the carousel container.</param>
|
2017-12-12 16:48:38 +08:00
|
|
|
|
private void updateItem(DrawableCarouselItem p, float halfHeight)
|
2017-02-24 13:37:54 +08:00
|
|
|
|
{
|
2017-03-17 18:12:15 +08:00
|
|
|
|
var height = p.IsPresent ? p.DrawHeight : 0;
|
2017-02-02 16:33:39 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
float itemDrawY = p.Position.Y - Current + height / 2;
|
|
|
|
|
float dist = Math.Abs(1f - itemDrawY / halfHeight);
|
2017-03-04 21:05:02 +08:00
|
|
|
|
|
2017-03-17 18:12:15 +08:00
|
|
|
|
// Setting the origin position serves as an additive position on top of potential
|
2017-12-12 16:48:38 +08:00
|
|
|
|
// local transformation we may want to apply (e.g. when a item gets selected, we
|
2017-03-17 18:12:15 +08:00
|
|
|
|
// may want to smoothly transform it leftwards.)
|
|
|
|
|
p.OriginPosition = new Vector2(-offsetX(dist, halfHeight), 0);
|
2017-03-04 21:05:02 +08:00
|
|
|
|
|
2017-03-17 18:12:15 +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.
|
|
|
|
|
p.SetMultiplicativeAlpha(MathHelper.Clamp(1.75f - 1.5f * dist, 0, 1));
|
2016-12-15 21:57:14 +08:00
|
|
|
|
}
|
2016-11-21 03:34:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|