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-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-12 16:48:38 +08:00
|
|
|
|
public BeatmapInfo SelectedBeatmap => selectedBeatmap?.Beatmap;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-07-20 10:50:31 +08:00
|
|
|
|
public override bool HandleInput => AllowSelection;
|
|
|
|
|
|
2017-03-17 18:12:15 +08:00
|
|
|
|
public Action BeatmapsChanged;
|
|
|
|
|
|
|
|
|
|
public IEnumerable<BeatmapSetInfo> Beatmaps
|
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
get { return carouselSets.Select(g => g.BeatmapSet); }
|
2017-03-17 18:12:15 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
scrollableContent.Clear(false);
|
2017-12-12 16:48:38 +08:00
|
|
|
|
items.Clear();
|
|
|
|
|
carouselSets.Clear();
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
List<CarouselBeatmapSet> newSets = null;
|
2017-12-09 15:42:59 +08:00
|
|
|
|
|
2017-03-17 18:12:15 +08:00
|
|
|
|
Task.Run(() =>
|
2017-12-09 15:42:59 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
newSets = value.Select(createGroup).Where(g => g != null).ToList();
|
|
|
|
|
newSets.ForEach(g => g.Filter(criteria));
|
2017-12-09 15:42:59 +08:00
|
|
|
|
}).ContinueWith(t =>
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
|
|
|
|
Schedule(() =>
|
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
foreach (var g in newSets)
|
2017-12-13 11:46:02 +08:00
|
|
|
|
addBeatmapSet(g);
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
root = new CarouselGroup(newSets.OfType<CarouselItem>().ToList());
|
|
|
|
|
items = root.Drawables.Value.ToList();
|
|
|
|
|
|
|
|
|
|
yPositionsCache.Invalidate();
|
2017-03-17 18:12:15 +08:00
|
|
|
|
BeatmapsChanged?.Invoke();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 12:41:50 +08:00
|
|
|
|
private readonly List<float> yPositions = new List<float>();
|
2017-12-13 11:46:02 +08:00
|
|
|
|
private Cached yPositionsCache = 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-12 16:48:38 +08:00
|
|
|
|
private readonly List<CarouselBeatmapSet> carouselSets = new List<CarouselBeatmapSet>();
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-13 11:46:02 +08:00
|
|
|
|
private Bindable<SongSelectRandomMode> randomType;
|
2017-12-12 16:48:38 +08:00
|
|
|
|
private readonly List<CarouselBeatmapSet> seenSets = new List<CarouselBeatmapSet>();
|
2017-06-01 00:41:15 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
private List<DrawableCarouselItem> items = new List<DrawableCarouselItem>();
|
|
|
|
|
private CarouselGroup root = new CarouselGroup();
|
2016-11-21 14:59:46 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>();
|
2016-11-23 03:10:04 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
private CarouselBeatmap selectedBeatmap;
|
2017-01-18 08:18:15 +08:00
|
|
|
|
|
2017-03-17 18:12:54 +08:00
|
|
|
|
public BeatmapCarousel()
|
2017-01-18 08:18:15 +08:00
|
|
|
|
{
|
2017-09-14 14:41:32 +08:00
|
|
|
|
Add(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-01-18 08:18:15 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 17:36:35 +08:00
|
|
|
|
public void RemoveBeatmap(BeatmapSetInfo beatmapSet)
|
|
|
|
|
{
|
2017-12-13 11:46:02 +08:00
|
|
|
|
Schedule(() => removeBeatmapSet(carouselSets.Find(b => b.BeatmapSet.ID == beatmapSet.ID)));
|
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-12 16:48:38 +08:00
|
|
|
|
// todo: this method should be smarter as to not recreate items that haven't changed, etc.
|
|
|
|
|
var oldGroup = carouselSets.Find(b => b.BeatmapSet.ID == beatmapSet.ID);
|
|
|
|
|
|
|
|
|
|
bool hadSelection = oldGroup?.State == CarouselItemState.Selected;
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
var newSet = createGroup(beatmapSet);
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
int index = carouselSets.IndexOf(oldGroup);
|
2017-12-08 05:02:53 +08:00
|
|
|
|
if (index >= 0)
|
2017-12-12 16:48:38 +08:00
|
|
|
|
carouselSets.RemoveAt(index);
|
2017-10-20 09:50:00 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (newSet != null)
|
2017-11-29 04:38:11 +08:00
|
|
|
|
{
|
2017-12-08 05:02:53 +08:00
|
|
|
|
if (index >= 0)
|
2017-12-12 16:48:38 +08:00
|
|
|
|
carouselSets.Insert(index, newSet);
|
2017-11-29 04:38:11 +08:00
|
|
|
|
else
|
2017-12-13 11:46:02 +08:00
|
|
|
|
addBeatmapSet(newSet);
|
2017-11-29 04:26:13 +08:00
|
|
|
|
}
|
2017-10-20 09:50:00 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (hadSelection && newSet == null)
|
|
|
|
|
SelectNext();
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
|
|
|
|
Filter(null, false);
|
|
|
|
|
|
|
|
|
|
//check if we can/need to maintain our current selection.
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (hadSelection && newSet != null)
|
2017-07-28 13:22:14 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
var newSelection = newSet.Beatmaps.Find(b => b.Beatmap.ID == selectedBeatmap?.Beatmap.ID);
|
2017-12-04 18:47:27 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (newSelection == null && selectedBeatmap != null)
|
|
|
|
|
newSelection = newSet.Beatmaps[Math.Min(newSet.Beatmaps.Count - 1, oldGroup.Beatmaps.IndexOf(selectedBeatmap))];
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
select(newSelection);
|
2017-08-31 14:49:56 +08:00
|
|
|
|
}
|
2017-07-28 13:22:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 18:12:15 +08:00
|
|
|
|
public void SelectBeatmap(BeatmapInfo beatmap, bool animated = true)
|
|
|
|
|
{
|
2017-09-01 17:13:21 +08:00
|
|
|
|
if (beatmap == null || beatmap.Hidden)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
|
|
|
|
SelectNext();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 16:15:25 +08:00
|
|
|
|
if (beatmap == SelectedBeatmap) return;
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
foreach (CarouselBeatmapSet group in carouselSets)
|
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-04-28 19:43:42 +08:00
|
|
|
|
public Action<BeatmapInfo> SelectionChanged;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-10-14 10:05:38 +08:00
|
|
|
|
private void selectNullBeatmap()
|
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
selectedBeatmap = null;
|
2017-10-14 10:05:38 +08:00
|
|
|
|
SelectionChanged?.Invoke(null);
|
|
|
|
|
}
|
|
|
|
|
|
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-11 19:22:10 +08:00
|
|
|
|
// todo: we may want to refactor and remove this as an optimisation in the future.
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (carouselSets.All(g => g.State == CarouselItemState.Hidden))
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-10-14 10:05:38 +08:00
|
|
|
|
selectNullBeatmap();
|
2017-03-17 18:12:15 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
int originalIndex = Math.Max(0, items.IndexOf(selectedBeatmap?.Drawables.Value.First()));
|
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-12 16:48:38 +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-12 16:48:38 +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:
|
|
|
|
|
select(set);
|
|
|
|
|
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-12 16:48:38 +08:00
|
|
|
|
private IEnumerable<CarouselBeatmapSet> getVisibleGroups() => carouselSets.Where(select => select.State != CarouselItemState.NotSelected);
|
2017-06-12 03:04:35 +08:00
|
|
|
|
|
2017-06-05 17:24:28 +08:00
|
|
|
|
public void SelectNextRandom()
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (carouselSets.Count == 0)
|
2017-07-24 15:57:12 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2017-06-02 01:54:42 +08:00
|
|
|
|
var visibleGroups = getVisibleGroups();
|
2017-06-01 14:54:48 +08:00
|
|
|
|
if (!visibleGroups.Any())
|
2017-03-17 18:12:15 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (selectedBeatmap != null)
|
|
|
|
|
randomSelectedBeatmaps.Push(selectedBeatmap);
|
2017-07-29 22:42:32 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
CarouselBeatmapSet group;
|
2017-06-02 01:54:42 +08:00
|
|
|
|
|
2017-12-13 11:46:02 +08:00
|
|
|
|
if (randomType == SongSelectRandomMode.RandomPermutation)
|
2017-06-01 00:41:15 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
var notSeenGroups = visibleGroups.Except(seenSets);
|
2017-06-01 00:41:15 +08:00
|
|
|
|
if (!notSeenGroups.Any())
|
2017-06-01 02:31:05 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
seenSets.Clear();
|
2017-06-01 00:41:15 +08:00
|
|
|
|
notSeenGroups = visibleGroups;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 14:54:48 +08:00
|
|
|
|
group = notSeenGroups.ElementAt(RNG.Next(notSeenGroups.Count()));
|
2017-12-12 16:48:38 +08:00
|
|
|
|
seenSets.Add(group);
|
2017-06-01 00:41:15 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2017-06-01 14:54:48 +08:00
|
|
|
|
group = visibleGroups.ElementAt(RNG.Next(visibleGroups.Count()));
|
2017-06-01 00:41:15 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
CarouselBeatmap item = group.Beatmaps[RNG.Next(group.Beatmaps.Count)];
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
select(item);
|
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
|
|
|
|
{
|
|
|
|
|
if (!randomSelectedBeatmaps.Any())
|
|
|
|
|
return;
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
if (beatmap.Visible)
|
2017-06-02 01:54:42 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
select(beatmap);
|
2017-06-02 01:54:42 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 18:54:51 +08:00
|
|
|
|
private FilterCriteria criteria = new FilterCriteria();
|
|
|
|
|
|
|
|
|
|
private ScheduledDelegate filterTask;
|
|
|
|
|
|
2017-07-20 10:50:31 +08:00
|
|
|
|
public bool AllowSelection = true;
|
|
|
|
|
|
2017-07-21 16:20:52 +08:00
|
|
|
|
public void FlushPendingFilters()
|
|
|
|
|
{
|
|
|
|
|
if (filterTask?.Completed == false)
|
|
|
|
|
Filter(null, false);
|
|
|
|
|
}
|
2017-07-21 04:58:58 +08:00
|
|
|
|
|
2017-03-17 18:54:51 +08:00
|
|
|
|
public void Filter(FilterCriteria newCriteria = null, bool debounce = true)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-03-29 18:47:53 +08:00
|
|
|
|
if (newCriteria != null)
|
|
|
|
|
criteria = newCriteria;
|
2017-03-17 18:54:51 +08:00
|
|
|
|
|
|
|
|
|
Action perform = delegate
|
2016-11-25 17:14:56 +08:00
|
|
|
|
{
|
2017-03-17 18:54:51 +08:00
|
|
|
|
filterTask = null;
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
carouselSets.ForEach(s => s.Filter(criteria));
|
2016-11-21 03:34:16 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
yPositionsCache.Invalidate();
|
2017-03-17 18:54:51 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (selectedBeatmap?.Visible != true)
|
2017-03-17 18:54:51 +08:00
|
|
|
|
SelectNext();
|
2017-03-29 19:01:46 +08:00
|
|
|
|
else
|
2017-12-12 16:48:38 +08:00
|
|
|
|
select(selectedBeatmap);
|
2017-03-17 18:54:51 +08:00
|
|
|
|
};
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-03-17 18:54:51 +08:00
|
|
|
|
filterTask?.Cancel();
|
2017-07-21 04:58:58 +08:00
|
|
|
|
filterTask = null;
|
|
|
|
|
|
2017-03-17 18:54:51 +08:00
|
|
|
|
if (debounce)
|
|
|
|
|
filterTask = Scheduler.AddDelayed(perform, 250);
|
|
|
|
|
else
|
|
|
|
|
perform();
|
2016-11-21 03:34:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-29 21:03:17 +08:00
|
|
|
|
public void ScrollToSelected(bool animated = true)
|
|
|
|
|
{
|
|
|
|
|
float selectedY = computeYPositions(animated);
|
|
|
|
|
ScrollTo(selectedY, animated);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
private CarouselBeatmapSet createGroup(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)
|
|
|
|
|
{
|
|
|
|
|
selectedBeatmap = c;
|
|
|
|
|
SelectionChanged?.Invoke(c.Beatmap);
|
|
|
|
|
yPositionsCache.Invalidate();
|
|
|
|
|
Schedule(() => ScrollToSelected());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return set;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader(permitNulls: true)]
|
2017-12-12 16:48:38 +08:00
|
|
|
|
private void load(OsuConfigManager config)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-12-13 11:46:02 +08:00
|
|
|
|
randomType = config.GetBindable<SongSelectRandomMode>(OsuSetting.SelectionRandomType);
|
2017-03-17 18:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-13 11:46:02 +08:00
|
|
|
|
private void addBeatmapSet(CarouselBeatmapSet set)
|
2017-03-17 18:12:15 +08:00
|
|
|
|
{
|
2017-12-04 18:47:27 +08:00
|
|
|
|
// prevent duplicates by concurrent independent actions trying to add a group
|
2017-12-12 16:48:38 +08:00
|
|
|
|
//todo: check this
|
|
|
|
|
if (carouselSets.Any(g => g.BeatmapSet.ID == set.BeatmapSet.ID))
|
2017-12-04 18:47:27 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
//todo: add to root
|
|
|
|
|
carouselSets.Add(set);
|
2017-03-17 18:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-13 11:46:02 +08:00
|
|
|
|
private void removeBeatmapSet(CarouselBeatmapSet set)
|
2017-02-24 13:37:54 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (set == null)
|
2017-07-24 16:25:33 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
carouselSets.Remove(set);
|
2017-03-04 16:34:28 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
foreach (var d in set.Drawables.Value)
|
|
|
|
|
{
|
|
|
|
|
items.Remove(d);
|
|
|
|
|
scrollableContent.Remove(d);
|
|
|
|
|
}
|
2017-02-24 13:37:54 +08:00
|
|
|
|
|
2017-12-13 11:46:02 +08:00
|
|
|
|
if (set.State == CarouselItemState.Selected)
|
|
|
|
|
SelectNext();
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
yPositionsCache.Invalidate();
|
2016-11-23 03:10:04 +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-02-02 16:33:39 +08:00
|
|
|
|
private float computeYPositions(bool animated = true)
|
2016-11-21 03:34:16 +08:00
|
|
|
|
{
|
2016-11-23 04:40:47 +08:00
|
|
|
|
yPositions.Clear();
|
|
|
|
|
|
2016-11-21 14:59:46 +08:00
|
|
|
|
float currentY = DrawHeight / 2;
|
|
|
|
|
float selectedY = currentY;
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
float lastSetY = 0;
|
2016-11-21 03:34:16 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
foreach (DrawableCarouselItem d in items)
|
|
|
|
|
{
|
|
|
|
|
switch (d)
|
2016-11-21 03:34:16 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
case DrawableCarouselBeatmapSet set:
|
|
|
|
|
set.MoveToX(set.Item.State == CarouselItemState.Selected ? -100 : 0, 500, Easing.OutExpo);
|
|
|
|
|
lastSetY = set.Position.Y;
|
|
|
|
|
break;
|
|
|
|
|
case DrawableCarouselBeatmap beatmap:
|
|
|
|
|
beatmap.MoveToX(beatmap.Item.State == CarouselItemState.Selected ? -50 : 0, 500, Easing.OutExpo);
|
2017-12-11 20:31:26 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (beatmap.Item == selectedBeatmap)
|
|
|
|
|
selectedY = currentY + beatmap.DrawHeight / 2 - DrawHeight / 2;
|
2016-11-25 11:50:18 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
// on first display we want to begin hidden under our group's header.
|
|
|
|
|
if (animated && !beatmap.IsPresent)
|
|
|
|
|
beatmap.MoveToY(lastSetY);
|
|
|
|
|
break;
|
2016-11-23 03:10:04 +08:00
|
|
|
|
}
|
2017-12-13 11:46:02 +08:00
|
|
|
|
|
|
|
|
|
yPositions.Add(currentY);
|
|
|
|
|
d.MoveToY(currentY, animated ? 750 : 0, Easing.OutExpo);
|
|
|
|
|
|
|
|
|
|
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-12 16:48:38 +08:00
|
|
|
|
yPositionsCache.Validate();
|
|
|
|
|
|
2016-11-24 00:42:21 +08:00
|
|
|
|
return selectedY;
|
2016-11-21 14:59:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-13 11:46:02 +08:00
|
|
|
|
private void select(CarouselItem item)
|
2016-11-21 03:34:16 +08:00
|
|
|
|
{
|
2017-12-13 11:46:02 +08:00
|
|
|
|
if (item == null) return;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
|
2017-12-13 11:46:02 +08:00
|
|
|
|
item.State.Value = CarouselItemState.Selected;
|
2016-11-21 03:34:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 18:12:15 +08:00
|
|
|
|
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
|
2017-03-02 21:01:53 +08:00
|
|
|
|
{
|
2017-03-17 18:12:15 +08:00
|
|
|
|
int direction = 0;
|
|
|
|
|
bool skipDifficulties = false;
|
|
|
|
|
|
|
|
|
|
switch (args.Key)
|
2017-03-02 21:01:53 +08:00
|
|
|
|
{
|
2017-03-17 18:12:15 +08:00
|
|
|
|
case Key.Up:
|
|
|
|
|
direction = -1;
|
2017-02-18 00:41:53 +08:00
|
|
|
|
break;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
case Key.Down:
|
|
|
|
|
direction = 1;
|
2017-02-18 00:41:53 +08:00
|
|
|
|
break;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
case Key.Left:
|
|
|
|
|
direction = -1;
|
|
|
|
|
skipDifficulties = true;
|
2017-02-18 00:41:53 +08:00
|
|
|
|
break;
|
2017-03-17 18:12:15 +08:00
|
|
|
|
case Key.Right:
|
|
|
|
|
direction = 1;
|
|
|
|
|
skipDifficulties = true;
|
2017-03-16 08:18:20 +08:00
|
|
|
|
break;
|
2017-02-18 00:41:53 +08:00
|
|
|
|
}
|
2017-02-18 06:32:14 +08:00
|
|
|
|
|
2017-03-17 18:12:15 +08:00
|
|
|
|
if (direction == 0)
|
|
|
|
|
return base.OnKeyDown(state, args);
|
2016-12-10 18:30:22 +08:00
|
|
|
|
|
2017-03-17 18:12:15 +08:00
|
|
|
|
SelectNext(direction, skipDifficulties);
|
|
|
|
|
return true;
|
2016-12-10 18:30:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 03:34:16 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2016-11-23 04:40:47 +08:00
|
|
|
|
float drawHeight = DrawHeight;
|
2016-11-25 17:14:56 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (!yPositionsCache.IsValid)
|
|
|
|
|
computeYPositions();
|
|
|
|
|
|
|
|
|
|
// Remove all items that should no longer be on-screen
|
|
|
|
|
scrollableContent.RemoveAll(delegate (DrawableCarouselItem p)
|
2017-01-18 08:18:15 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
float itemPosY = p.Position.Y;
|
|
|
|
|
bool remove = itemPosY < Current - p.DrawHeight || itemPosY > Current + drawHeight || !p.IsPresent;
|
2017-03-04 15:54:14 +08:00
|
|
|
|
return remove;
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
// Find index range of all items that should be on-screen
|
|
|
|
|
Trace.Assert(items.Count == yPositions.Count);
|
2016-11-21 03:34:16 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
int firstIndex = yPositions.BinarySearch(Current - DrawableCarouselItem.MAX_HEIGHT);
|
2016-11-23 04:40:47 +08:00
|
|
|
|
if (firstIndex < 0) firstIndex = ~firstIndex;
|
|
|
|
|
int lastIndex = yPositions.BinarySearch(Current + drawHeight);
|
2017-05-21 09:04:12 +08:00
|
|
|
|
if (lastIndex < 0)
|
|
|
|
|
{
|
|
|
|
|
lastIndex = ~lastIndex;
|
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
// Add the first item of the last visible beatmap group to preload its data.
|
|
|
|
|
if (lastIndex != 0 && items[lastIndex - 1] is DrawableCarouselBeatmapSet)
|
2017-05-21 09:04:12 +08:00
|
|
|
|
lastIndex++;
|
|
|
|
|
}
|
2016-11-21 03:34:16 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
// Add those items within the previously found index range that should be displayed.
|
2016-11-25 17:14:56 +08:00
|
|
|
|
for (int i = firstIndex; i < lastIndex; ++i)
|
2016-11-21 03:34:16 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
DrawableCarouselItem item = items[i];
|
2017-03-04 15:54:14 +08:00
|
|
|
|
|
2017-03-04 16:34:39 +08:00
|
|
|
|
// Only add if we're not already part of the content.
|
2017-12-12 16:48:38 +08:00
|
|
|
|
if (!scrollableContent.Contains(item))
|
2017-03-04 15:54:14 +08:00
|
|
|
|
{
|
2017-12-12 16:48:38 +08:00
|
|
|
|
// Makes sure headers are always _below_ items,
|
2017-03-04 16:34:39 +08:00
|
|
|
|
// and depth flows downward.
|
2017-12-12 16:48:38 +08:00
|
|
|
|
item.Depth = i + (item is DrawableCarouselBeatmapSet ? items.Count : 0);
|
2017-11-21 21:27:56 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
switch (item.LoadState)
|
2017-11-21 21:27:56 +08:00
|
|
|
|
{
|
|
|
|
|
case LoadState.NotLoaded:
|
2017-12-12 16:48:38 +08:00
|
|
|
|
LoadComponentAsync(item);
|
2017-11-21 21:27:56 +08:00
|
|
|
|
break;
|
|
|
|
|
case LoadState.Loading:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2017-12-12 16:48:38 +08:00
|
|
|
|
scrollableContent.Add(item);
|
2017-11-21 21:27:56 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2017-03-04 15:54:14 +08:00
|
|
|
|
}
|
2016-11-21 03:34:16 +08:00
|
|
|
|
}
|
2017-03-04 15:54:14 +08:00
|
|
|
|
|
2017-12-12 16:48:38 +08:00
|
|
|
|
// Update externally controlled state of currently visible items
|
2017-03-04 16:34:39 +08:00
|
|
|
|
// (e.g. x-offset and opacity).
|
2017-03-04 15:54:14 +08:00
|
|
|
|
float halfHeight = drawHeight / 2;
|
2017-12-12 16:48:38 +08:00
|
|
|
|
foreach (DrawableCarouselItem p in scrollableContent.Children)
|
|
|
|
|
updateItem(p, halfHeight);
|
2016-11-21 03:34:16 +08:00
|
|
|
|
}
|
2016-12-15 21:57:14 +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
|
|
|
|
}
|
|
|
|
|
}
|