Initial carousel infrastructue changes

This commit is contained in:
Dean Herbert
2017-12-12 17:48:38 +09:00
parent 94894bf49b
commit 78dd975a35
17 changed files with 594 additions and 635 deletions

View File

@ -7,7 +7,6 @@ using osu.Framework.Graphics.Containers;
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Configuration;
using osu.Framework.Input;
using OpenTK.Input;
@ -15,17 +14,19 @@ using osu.Framework.MathUtils;
using System.Diagnostics;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Caching;
using osu.Framework.Threading;
using osu.Framework.Configuration;
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
{
public class BeatmapCarousel : OsuScrollContainer
{
public BeatmapInfo SelectedBeatmap => selectedPanel?.Beatmap;
public BeatmapInfo SelectedBeatmap => selectedBeatmap?.Beatmap;
public override bool HandleInput => AllowSelection;
@ -33,27 +34,30 @@ namespace osu.Game.Screens.Select
public IEnumerable<BeatmapSetInfo> Beatmaps
{
get { return groups.Select(g => g.BeatmapSet); }
get { return carouselSets.Select(g => g.BeatmapSet); }
set
{
scrollableContent.Clear(false);
panels.Clear();
groups.Clear();
items.Clear();
carouselSets.Clear();
List<BeatmapGroup> newGroups = null;
List<CarouselBeatmapSet> newSets = null;
Task.Run(() =>
{
newGroups = value.Select(createGroup).Where(g => g != null).ToList();
criteria.Filter(newGroups);
newSets = value.Select(createGroup).Where(g => g != null).ToList();
newSets.ForEach(g => g.Filter(criteria));
}).ContinueWith(t =>
{
Schedule(() =>
{
foreach (var g in newGroups)
foreach (var g in newSets)
addGroup(g);
computeYPositions();
root = new CarouselGroup(newSets.OfType<CarouselItem>().ToList());
items = root.Drawables.Value.ToList();
yPositionsCache.Invalidate();
BeatmapsChanged?.Invoke();
});
});
@ -62,24 +66,19 @@ namespace osu.Game.Screens.Select
private readonly List<float> yPositions = new List<float>();
/// <summary>
/// Required for now unfortunately.
/// </summary>
private BeatmapManager manager;
private readonly Container<DrawableCarouselItem> scrollableContent;
private readonly Container<Panel> scrollableContent;
private readonly List<BeatmapGroup> groups = new List<BeatmapGroup>();
private readonly List<CarouselBeatmapSet> carouselSets = new List<CarouselBeatmapSet>();
private Bindable<SelectionRandomType> randomType;
private readonly List<BeatmapGroup> seenGroups = new List<BeatmapGroup>();
private readonly List<CarouselBeatmapSet> seenSets = new List<CarouselBeatmapSet>();
private readonly List<Panel> panels = new List<Panel>();
private List<DrawableCarouselItem> items = new List<DrawableCarouselItem>();
private CarouselGroup root = new CarouselGroup();
private readonly Stack<KeyValuePair<BeatmapGroup, BeatmapPanel>> randomSelectedBeatmaps = new Stack<KeyValuePair<BeatmapGroup, BeatmapPanel>>();
private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>();
private BeatmapGroup selectedGroup;
private BeatmapPanel selectedPanel;
private CarouselBeatmap selectedBeatmap;
public BeatmapCarousel()
{
@ -87,7 +86,7 @@ namespace osu.Game.Screens.Select
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Child = scrollableContent = new Container<Panel>
Child = scrollableContent = new Container<DrawableCarouselItem>
{
RelativeSizeAxes = Axes.X,
}
@ -96,45 +95,44 @@ namespace osu.Game.Screens.Select
public void RemoveBeatmap(BeatmapSetInfo beatmapSet)
{
Schedule(() => removeGroup(groups.Find(b => b.BeatmapSet.ID == beatmapSet.ID)));
Schedule(() => removeGroup(carouselSets.Find(b => b.BeatmapSet.ID == beatmapSet.ID)));
}
public void UpdateBeatmapSet(BeatmapSetInfo beatmapSet)
{
// todo: this method should be smarter as to not recreate panels that haven't changed, etc.
var oldGroup = groups.Find(b => b.BeatmapSet.ID == beatmapSet.ID);
// 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);
var newGroup = createGroup(beatmapSet);
bool hadSelection = oldGroup?.State == CarouselItemState.Selected;
int index = groups.IndexOf(oldGroup);
var newSet = createGroup(beatmapSet);
int index = carouselSets.IndexOf(oldGroup);
if (index >= 0)
groups.RemoveAt(index);
carouselSets.RemoveAt(index);
if (newGroup != null)
if (newSet != null)
{
if (index >= 0)
groups.Insert(index, newGroup);
carouselSets.Insert(index, newSet);
else
addGroup(newGroup);
addGroup(newSet);
}
bool hadSelection = selectedGroup == oldGroup;
if (hadSelection && newGroup == null)
selectedGroup = null;
if (hadSelection && newSet == null)
SelectNext();
Filter(null, false);
//check if we can/need to maintain our current selection.
if (hadSelection && newGroup != null)
if (hadSelection && newSet != null)
{
var newSelection =
newGroup.BeatmapPanels.Find(p => p.Beatmap.ID == selectedPanel?.Beatmap.ID);
var newSelection = newSet.Beatmaps.Find(b => b.Beatmap.ID == selectedBeatmap?.Beatmap.ID);
if (newSelection == null && oldGroup != null && selectedPanel != null)
newSelection = newGroup.BeatmapPanels[Math.Min(newGroup.BeatmapPanels.Count - 1, oldGroup.BeatmapPanels.IndexOf(selectedPanel))];
if (newSelection == null && selectedBeatmap != null)
newSelection = newSet.Beatmaps[Math.Min(newSet.Beatmaps.Count - 1, oldGroup.Beatmaps.IndexOf(selectedBeatmap))];
selectGroup(newGroup, newSelection);
select(newSelection);
}
}
@ -148,12 +146,12 @@ namespace osu.Game.Screens.Select
if (beatmap == SelectedBeatmap) return;
foreach (BeatmapGroup group in groups)
foreach (CarouselBeatmapSet group in carouselSets)
{
var panel = group.BeatmapPanels.FirstOrDefault(p => p.Beatmap.Equals(beatmap));
if (panel != null)
var item = group.Beatmaps.FirstOrDefault(p => p.Beatmap.Equals(beatmap));
if (item != null)
{
selectGroup(group, panel, animated);
select(item, animated);
return;
}
}
@ -161,20 +159,9 @@ namespace osu.Game.Screens.Select
public Action<BeatmapInfo> SelectionChanged;
public Action StartRequested;
public Action<BeatmapSetInfo> DeleteRequested;
public Action<BeatmapSetInfo> RestoreRequested;
public Action<BeatmapInfo> EditRequested;
public Action<BeatmapInfo> HideDifficultyRequested;
private void selectNullBeatmap()
{
selectedGroup = null;
selectedPanel = null;
selectedBeatmap = null;
SelectionChanged?.Invoke(null);
}
@ -186,90 +173,71 @@ namespace osu.Game.Screens.Select
public void SelectNext(int direction = 1, bool skipDifficulties = true)
{
// todo: we may want to refactor and remove this as an optimisation in the future.
if (groups.All(g => g.State == BeatmapGroupState.Hidden))
if (carouselSets.All(g => g.State == CarouselItemState.Hidden))
{
selectNullBeatmap();
return;
}
int originalIndex = Math.Max(0, groups.IndexOf(selectedGroup));
int originalIndex = Math.Max(0, items.IndexOf(selectedBeatmap?.Drawables.Value.First()));
int currentIndex = originalIndex;
// local function to increment the index in the required direction, wrapping over extremities.
int incrementIndex() => currentIndex = (currentIndex + direction + groups.Count) % groups.Count;
int incrementIndex() => currentIndex = (currentIndex + direction + items.Count) % items.Count;
// in the case we are skipping difficulties, we want to increment the index once before starting to find out new target
// (we don't care about the currently selected group).
if (skipDifficulties)
incrementIndex();
do
while (incrementIndex() != originalIndex)
{
var group = groups[currentIndex];
var item = items[currentIndex].Item;
if (group.State == BeatmapGroupState.Hidden) continue;
if (item.Filtered || item.State == CarouselItemState.Selected) continue;
// we are only interested in non-filtered panels.
IEnumerable<BeatmapPanel> validPanels = group.BeatmapPanels.Where(p => !p.Filtered);
// if we are considering difficulties, we need to do a few extrea steps.
if (!skipDifficulties)
switch (item)
{
// we want to reverse the panel order if we are searching backwards.
if (direction < 0)
validPanels = validPanels.Reverse();
// if we are currently on the selected panel, let's try to find a valid difficulty before leaving to the next group.
// the first valid difficulty is found by skipping to the selected panel and then one further.
if (currentIndex == originalIndex)
validPanels = validPanels.SkipWhile(p => p != selectedPanel).Skip(1);
case CarouselBeatmap beatmap:
if (skipDifficulties) continue;
select(beatmap);
return;
case CarouselBeatmapSet set:
select(set);
return;
}
var next = validPanels.FirstOrDefault();
// at this point, we can perform the selection change if we have a valid new target, else continue to increment in the specified direction.
if (next != null)
{
selectGroup(group, next);
return;
}
} while (incrementIndex() != originalIndex);
}
}
private IEnumerable<BeatmapGroup> getVisibleGroups() => groups.Where(selectGroup => selectGroup.State != BeatmapGroupState.Hidden);
private IEnumerable<CarouselBeatmapSet> getVisibleGroups() => carouselSets.Where(select => select.State != CarouselItemState.NotSelected);
public void SelectNextRandom()
{
if (groups.Count == 0)
if (carouselSets.Count == 0)
return;
var visibleGroups = getVisibleGroups();
if (!visibleGroups.Any())
return;
if (selectedGroup != null)
randomSelectedBeatmaps.Push(new KeyValuePair<BeatmapGroup, BeatmapPanel>(selectedGroup, selectedGroup.SelectedPanel));
if (selectedBeatmap != null)
randomSelectedBeatmaps.Push(selectedBeatmap);
BeatmapGroup group;
CarouselBeatmapSet group;
if (randomType == SelectionRandomType.RandomPermutation)
{
var notSeenGroups = visibleGroups.Except(seenGroups);
var notSeenGroups = visibleGroups.Except(seenSets);
if (!notSeenGroups.Any())
{
seenGroups.Clear();
seenSets.Clear();
notSeenGroups = visibleGroups;
}
group = notSeenGroups.ElementAt(RNG.Next(notSeenGroups.Count()));
seenGroups.Add(group);
seenSets.Add(group);
}
else
group = visibleGroups.ElementAt(RNG.Next(visibleGroups.Count()));
BeatmapPanel panel = group.BeatmapPanels[RNG.Next(group.BeatmapPanels.Count)];
CarouselBeatmap item = group.Beatmaps[RNG.Next(group.Beatmaps.Count)];
selectGroup(group, panel);
select(item);
}
public void SelectPreviousRandom()
@ -277,17 +245,13 @@ namespace osu.Game.Screens.Select
if (!randomSelectedBeatmaps.Any())
return;
var visibleGroups = getVisibleGroups();
if (!visibleGroups.Any())
return;
while (randomSelectedBeatmaps.Any())
{
var beatmapCoordinates = randomSelectedBeatmaps.Pop();
var group = beatmapCoordinates.Key;
if (visibleGroups.Contains(group))
var beatmap = randomSelectedBeatmaps.Pop();
if (beatmap.Visible)
{
selectGroup(group, beatmapCoordinates.Value);
select(beatmap);
break;
}
}
@ -314,25 +278,14 @@ namespace osu.Game.Screens.Select
{
filterTask = null;
criteria.Filter(groups);
carouselSets.ForEach(s => s.Filter(criteria));
var filtered = new List<BeatmapGroup>(groups);
yPositionsCache.Invalidate();
scrollableContent.Clear(false);
panels.Clear();
groups.Clear();
foreach (var g in filtered)
addGroup(g);
computeYPositions();
selectedGroup?.UpdateState();
if (selectedGroup == null || selectedGroup.State == BeatmapGroupState.Hidden)
if (selectedBeatmap?.Visible != true)
SelectNext();
else
selectGroup(selectedGroup, selectedPanel);
select(selectedBeatmap);
};
filterTask?.Cancel();
@ -350,54 +303,60 @@ namespace osu.Game.Screens.Select
ScrollTo(selectedY, animated);
}
private BeatmapGroup createGroup(BeatmapSetInfo beatmapSet)
private CarouselBeatmapSet createGroup(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;
}
return new BeatmapGroup(beatmapSet, manager)
var set = new CarouselBeatmapSet(beatmapSet);
foreach (var c in set.Beatmaps)
{
SelectionChanged = (g, p) => selectGroup(g, p),
StartRequested = b => StartRequested?.Invoke(),
DeleteRequested = b => DeleteRequested?.Invoke(b),
RestoreHiddenRequested = s => RestoreRequested?.Invoke(s),
EditRequested = b => EditRequested?.Invoke(b),
HideDifficultyRequested = b => HideDifficultyRequested?.Invoke(b),
State = BeatmapGroupState.Collapsed
};
c.State.ValueChanged += v =>
{
if (v == CarouselItemState.Selected)
{
selectedBeatmap = c;
SelectionChanged?.Invoke(c.Beatmap);
yPositionsCache.Invalidate();
Schedule(() => ScrollToSelected());
}
};
}
return set;
}
[BackgroundDependencyLoader(permitNulls: true)]
private void load(BeatmapManager manager, OsuConfigManager config)
private void load(OsuConfigManager config)
{
this.manager = manager;
randomType = config.GetBindable<SelectionRandomType>(OsuSetting.SelectionRandomType);
}
private void addGroup(BeatmapGroup group)
private void addGroup(CarouselBeatmapSet set)
{
// prevent duplicates by concurrent independent actions trying to add a group
if (groups.Any(g => g.BeatmapSet.ID == group.BeatmapSet.ID))
//todo: check this
if (carouselSets.Any(g => g.BeatmapSet.ID == set.BeatmapSet.ID))
return;
groups.Add(group);
panels.Add(group.Header);
panels.AddRange(group.BeatmapPanels);
//todo: add to root
carouselSets.Add(set);
}
private void removeGroup(BeatmapGroup group)
private void removeGroup(CarouselBeatmapSet set)
{
if (group == null)
if (set == null)
return;
if (selectedGroup == group)
if (set.State == CarouselItemState.Selected)
{
if (getVisibleGroups().Count() == 1)
selectNullBeatmap();
@ -405,21 +364,23 @@ namespace osu.Game.Screens.Select
SelectNext();
}
groups.Remove(group);
panels.Remove(group.Header);
foreach (var p in group.BeatmapPanels)
panels.Remove(p);
carouselSets.Remove(set);
scrollableContent.Remove(group.Header);
scrollableContent.RemoveRange(group.BeatmapPanels);
foreach (var d in set.Drawables.Value)
{
items.Remove(d);
scrollableContent.Remove(d);
}
computeYPositions();
yPositionsCache.Invalidate();
}
private Cached yPositionsCache = new Cached();
/// <summary>
/// Computes the target Y positions for every panel in the carousel.
/// Computes the target Y positions for every item in the carousel.
/// </summary>
/// <returns>The Y position of the currently selected panel.</returns>
/// <returns>The Y position of the currently selected item.</returns>
private float computeYPositions(bool animated = true)
{
yPositions.Clear();
@ -427,88 +388,61 @@ namespace osu.Game.Screens.Select
float currentY = DrawHeight / 2;
float selectedY = currentY;
foreach (BeatmapGroup group in groups)
float lastSetY = 0;
foreach (DrawableCarouselItem d in items)
{
movePanel(group.Header, group.State != BeatmapGroupState.Hidden, animated, ref currentY);
if (group.State == BeatmapGroupState.Expanded)
switch (d)
{
group.Header.MoveToX(-100, 500, Easing.OutExpo);
var headerY = group.Header.Position.Y;
case DrawableCarouselBeatmapSet set:
set.MoveToX(set.Item.State == CarouselItemState.Selected ? -100 : 0, 500, Easing.OutExpo);
foreach (BeatmapPanel panel in group.BeatmapPanels)
{
if (panel == selectedPanel)
selectedY = currentY + panel.DrawHeight / 2 - DrawHeight / 2;
lastSetY = set.Position.Y;
panel.MoveToX(-50, 500, Easing.OutExpo);
movePanel(set, set.Item.Visible, animated, ref currentY);
break;
case DrawableCarouselBeatmap beatmap:
beatmap.MoveToX(beatmap.Item.State == CarouselItemState.Selected ? -50 : 0, 500, Easing.OutExpo);
bool isHidden = panel.State == PanelSelectedState.Hidden;
if (beatmap.Item == selectedBeatmap)
selectedY = currentY + beatmap.DrawHeight / 2 - DrawHeight / 2;
//on first display we want to begin hidden under our group's header.
if (isHidden || panel.Alpha == 0)
panel.MoveToY(headerY);
// on first display we want to begin hidden under our group's header.
if (animated && !beatmap.IsPresent)
beatmap.MoveToY(lastSetY);
movePanel(panel, !isHidden, animated, ref currentY);
}
}
else
{
group.Header.MoveToX(0, 500, Easing.OutExpo);
foreach (BeatmapPanel panel in group.BeatmapPanels)
{
panel.MoveToX(0, 500, Easing.OutExpo);
movePanel(panel, false, animated, ref currentY);
}
movePanel(beatmap, beatmap.Item.Visible, animated, ref currentY);
break;
}
}
currentY += DrawHeight / 2;
scrollableContent.Height = currentY;
yPositionsCache.Validate();
return selectedY;
}
private void movePanel(Panel panel, bool advance, bool animated, ref float currentY)
private void movePanel(DrawableCarouselItem item, bool advance, bool animated, ref float currentY)
{
yPositions.Add(currentY);
panel.MoveToY(currentY, animated ? 750 : 0, Easing.OutExpo);
item.MoveToY(currentY, animated ? 750 : 0, Easing.OutExpo);
if (advance)
currentY += panel.DrawHeight + 5;
currentY += item.DrawHeight + 5;
}
private void selectGroup(BeatmapGroup group, BeatmapPanel panel = null, bool animated = true)
private void select(CarouselBeatmapSet beatmapSet = null)
{
try
{
if (panel == null || panel.Filtered == true)
panel = group.BeatmapPanels.First(p => !p.Filtered);
if (beatmapSet == null) return;
beatmapSet.State.Value = CarouselItemState.Selected;
}
if (selectedPanel == panel) return;
Trace.Assert(group.BeatmapPanels.Contains(panel), @"Selected panel must be in provided group");
if (selectedGroup != null && selectedGroup != group && selectedGroup.State != BeatmapGroupState.Hidden)
selectedGroup.State = BeatmapGroupState.Collapsed;
group.State = BeatmapGroupState.Expanded;
group.SelectedPanel = panel;
panel.State = PanelSelectedState.Selected;
if (selectedPanel == panel) return;
selectedPanel = panel;
selectedGroup = group;
SelectionChanged?.Invoke(panel.Beatmap);
}
finally
{
ScrollToSelected(animated);
}
private void select(CarouselBeatmap beatmap = null, bool animated = true)
{
if (beatmap == null) return;
beatmap.State.Value = CarouselItemState.Selected;
}
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
@ -547,66 +481,67 @@ namespace osu.Game.Screens.Select
float drawHeight = DrawHeight;
// Remove all panels that should no longer be on-screen
scrollableContent.RemoveAll(delegate(Panel p)
if (!yPositionsCache.IsValid)
computeYPositions();
// Remove all items that should no longer be on-screen
scrollableContent.RemoveAll(delegate (DrawableCarouselItem p)
{
float panelPosY = p.Position.Y;
bool remove = panelPosY < Current - p.DrawHeight || panelPosY > Current + drawHeight || !p.IsPresent;
float itemPosY = p.Position.Y;
bool remove = itemPosY < Current - p.DrawHeight || itemPosY > Current + drawHeight || !p.IsPresent;
return remove;
});
// Find index range of all panels that should be on-screen
Trace.Assert(panels.Count == yPositions.Count);
// Find index range of all items that should be on-screen
Trace.Assert(items.Count == yPositions.Count);
int firstIndex = yPositions.BinarySearch(Current - Panel.MAX_HEIGHT);
int firstIndex = yPositions.BinarySearch(Current - DrawableCarouselItem.MAX_HEIGHT);
if (firstIndex < 0) firstIndex = ~firstIndex;
int lastIndex = yPositions.BinarySearch(Current + drawHeight);
if (lastIndex < 0)
{
lastIndex = ~lastIndex;
// Add the first panel of the last visible beatmap group to preload its data.
if (lastIndex != 0 && panels[lastIndex - 1] is BeatmapSetHeader)
// Add the first item of the last visible beatmap group to preload its data.
if (lastIndex != 0 && items[lastIndex - 1] is DrawableCarouselBeatmapSet)
lastIndex++;
}
// Add those panels within the previously found index range that should be displayed.
// Add those items within the previously found index range that should be displayed.
for (int i = firstIndex; i < lastIndex; ++i)
{
Panel panel = panels[i];
if (panel.State == PanelSelectedState.Hidden)
continue;
DrawableCarouselItem item = items[i];
// Only add if we're not already part of the content.
if (!scrollableContent.Contains(panel))
if (!scrollableContent.Contains(item))
{
// Makes sure headers are always _below_ panels,
// Makes sure headers are always _below_ items,
// and depth flows downward.
panel.Depth = i + (panel is BeatmapSetHeader ? panels.Count : 0);
item.Depth = i + (item is DrawableCarouselBeatmapSet ? items.Count : 0);
switch (panel.LoadState)
switch (item.LoadState)
{
case LoadState.NotLoaded:
LoadComponentAsync(panel);
LoadComponentAsync(item);
break;
case LoadState.Loading:
break;
default:
scrollableContent.Add(panel);
scrollableContent.Add(item);
break;
}
}
}
// Update externally controlled state of currently visible panels
// Update externally controlled state of currently visible items
// (e.g. x-offset and opacity).
float halfHeight = drawHeight / 2;
foreach (Panel p in scrollableContent.Children)
updatePanel(p, halfHeight);
foreach (DrawableCarouselItem p in scrollableContent.Children)
updateItem(p, halfHeight);
}
/// <summary>
/// Computes the x-offset of currently visible panels. Makes the carousel appear round.
/// 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
@ -624,20 +559,20 @@ namespace osu.Game.Screens.Select
}
/// <summary>
/// Update a panel's x position and multiplicative alpha based on its y position and
/// Update a item's x position and multiplicative alpha based on its y position and
/// the current scroll position.
/// </summary>
/// <param name="p">The panel to be updated.</param>
/// <param name="p">The item to be updated.</param>
/// <param name="halfHeight">Half the draw height of the carousel container.</param>
private void updatePanel(Panel p, float halfHeight)
private void updateItem(DrawableCarouselItem p, float halfHeight)
{
var height = p.IsPresent ? p.DrawHeight : 0;
float panelDrawY = p.Position.Y - Current + height / 2;
float dist = Math.Abs(1f - panelDrawY / halfHeight);
float itemDrawY = p.Position.Y - Current + height / 2;
float dist = Math.Abs(1f - itemDrawY / halfHeight);
// Setting the origin position serves as an additive position on top of potential
// local transformation we may want to apply (e.g. when a panel gets selected, we
// local transformation we may want to apply (e.g. when a item gets selected, we
// may want to smoothly transform it leftwards.)
p.OriginPosition = new Vector2(-offsetX(dist, halfHeight), 0);