1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 17:17:26 +08:00
osu-lazer/osu.Game/Screens/Select/BeatmapCarousel.cs

602 lines
21 KiB
C#
Raw Normal View History

// 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
using OpenTK;
using osu.Framework.Graphics;
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;
using osu.Framework.MathUtils;
using System.Diagnostics;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Threading;
using osu.Framework.Configuration;
2017-07-26 12:22:46 +08:00
using osu.Game.Beatmaps;
using osu.Game.Graphics.Containers;
2016-11-25 17:14:56 +08:00
namespace osu.Game.Screens.Select
{
internal class BeatmapCarousel : OsuScrollContainer
{
public BeatmapInfo SelectedBeatmap => selectedPanel?.Beatmap;
public override bool HandleInput => AllowSelection;
public Action BeatmapsChanged;
public IEnumerable<BeatmapSetInfo> Beatmaps
{
get
{
return groups.Select(g => g.BeatmapSet);
}
set
{
scrollableContent.Clear(false);
panels.Clear();
groups.Clear();
List<BeatmapGroup> newGroups = null;
Task.Run(() =>
{
newGroups = value.Select(createGroup).ToList();
criteria.Filter(newGroups);
}).ContinueWith(t =>
{
Schedule(() =>
{
foreach (var g in newGroups)
addGroup(g);
computeYPositions();
BeatmapsChanged?.Invoke();
});
});
}
}
private readonly List<float> yPositions = new List<float>();
/// <summary>
/// Required for now unfortunately.
/// </summary>
private BeatmapManager manager;
private readonly Container<Panel> scrollableContent;
private readonly List<BeatmapGroup> groups = new List<BeatmapGroup>();
private Bindable<SelectionRandomType> randomType;
private readonly List<BeatmapGroup> seenGroups = new List<BeatmapGroup>();
private readonly List<Panel> panels = new List<Panel>();
2016-11-21 14:59:46 +08:00
2017-06-15 11:37:20 +08:00
private readonly Stack<KeyValuePair<BeatmapGroup, BeatmapPanel>> randomSelectedBeatmaps = new Stack<KeyValuePair<BeatmapGroup, BeatmapPanel>>();
2017-06-02 01:54:42 +08:00
private BeatmapGroup selectedGroup;
private BeatmapPanel selectedPanel;
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
{
Add(scrollableContent = new Container<Panel>
2017-01-18 08:18:15 +08:00
{
RelativeSizeAxes = Axes.X,
});
}
public void AddBeatmap(BeatmapSetInfo beatmapSet)
{
var group = createGroup(beatmapSet);
2016-11-25 17:14:56 +08:00
//for the time being, let's completely load the difficulty panels in the background.
//this likely won't scale so well, but allows us to completely async the loading flow.
Schedule(delegate
{
addGroup(group);
computeYPositions();
if (selectedGroup == null)
selectGroup(group);
});
}
public void RemoveBeatmap(BeatmapSetInfo beatmapSet) => removeGroup(groups.Find(b => b.BeatmapSet.ID == beatmapSet.ID));
internal void UpdateBeatmap(BeatmapInfo beatmap)
{
// todo: this method should not run more than once for the same BeatmapSetInfo.
var set = manager.Refresh(beatmap.BeatmapSet);
// todo: this method should be smarter as to not recreate panels that haven't changed, etc.
var group = groups.Find(b => b.BeatmapSet.ID == set.ID);
if (group == null)
return;
var newGroup = createGroup(set);
int i = groups.IndexOf(group);
groups.RemoveAt(i);
groups.Insert(i, newGroup);
if (selectedGroup == group && newGroup.BeatmapPanels.Count == 0)
selectedGroup = null;
Filter(null, false);
//check if we can/need to maintain our current selection.
if (selectedGroup == group && newGroup.BeatmapPanels.Count > 0)
{
var newSelection =
newGroup.BeatmapPanels.Find(p => p.Beatmap.ID == selectedPanel?.Beatmap.ID) ??
newGroup.BeatmapPanels[Math.Min(newGroup.BeatmapPanels.Count - 1, group.BeatmapPanels.IndexOf(selectedPanel))];
selectGroup(newGroup, newSelection);
}
}
public void SelectBeatmap(BeatmapInfo beatmap, bool animated = true)
{
if (beatmap == null || beatmap.Hidden)
{
SelectNext();
return;
}
if (beatmap == SelectedBeatmap) return;
foreach (BeatmapGroup group in groups)
{
var panel = group.BeatmapPanels.FirstOrDefault(p => p.Beatmap.Equals(beatmap));
if (panel != null)
{
selectGroup(group, panel, animated);
return;
}
}
}
public Action<BeatmapInfo> SelectionChanged;
public Action StartRequested;
public Action<BeatmapSetInfo> DeleteRequested;
public Action<BeatmapSetInfo> RestoreRequested;
2017-09-04 06:59:32 +08:00
public Action<BeatmapInfo> HideDifficultyRequested;
2017-08-30 19:41:41 +08:00
public void SelectNext(int direction = 1, bool skipDifficulties = true)
{
2017-07-20 14:34:44 +08:00
if (groups.All(g => g.State == BeatmapGroupState.Hidden))
{
selectedGroup = null;
selectedPanel = null;
2017-07-19 16:56:43 +08:00
SelectionChanged?.Invoke(null);
return;
}
if (!skipDifficulties && selectedGroup != null)
{
int i = selectedGroup.BeatmapPanels.IndexOf(selectedPanel) + direction;
if (i >= 0 && i < selectedGroup.BeatmapPanels.Count)
{
//changing difficulty panel, not set.
selectGroup(selectedGroup, selectedGroup.BeatmapPanels[i]);
return;
}
}
2017-04-18 09:04:32 +08:00
int startIndex = Math.Max(0, groups.IndexOf(selectedGroup));
int index = startIndex;
do
{
index = (index + direction + groups.Count) % groups.Count;
if (groups[index].State != BeatmapGroupState.Hidden)
{
2017-05-22 07:53:36 +08:00
if (skipDifficulties)
SelectBeatmap(groups[index].SelectedPanel != null ? groups[index].SelectedPanel.Beatmap : groups[index].BeatmapPanels.First().Beatmap);
else
SelectBeatmap(direction == 1 ? groups[index].BeatmapPanels.First().Beatmap : groups[index].BeatmapPanels.Last().Beatmap);
return;
}
} while (index != startIndex);
}
2017-06-12 03:04:35 +08:00
private IEnumerable<BeatmapGroup> getVisibleGroups() => groups.Where(selectGroup => selectGroup.State != BeatmapGroupState.Hidden);
2017-06-05 17:24:28 +08:00
public void SelectNextRandom()
{
if (groups.Count == 0)
return;
2017-06-02 01:54:42 +08:00
var visibleGroups = getVisibleGroups();
if (!visibleGroups.Any())
return;
if (selectedGroup != null)
randomSelectedBeatmaps.Push(new KeyValuePair<BeatmapGroup, BeatmapPanel>(selectedGroup, selectedGroup.SelectedPanel));
BeatmapGroup group;
2017-06-02 01:54:42 +08:00
if (randomType == SelectionRandomType.RandomPermutation)
{
2017-06-02 01:54:42 +08:00
var notSeenGroups = visibleGroups.Except(seenGroups);
if (!notSeenGroups.Any())
2017-06-01 02:31:05 +08:00
{
seenGroups.Clear();
notSeenGroups = visibleGroups;
}
group = notSeenGroups.ElementAt(RNG.Next(notSeenGroups.Count()));
seenGroups.Add(group);
}
else
group = visibleGroups.ElementAt(RNG.Next(visibleGroups.Count()));
2017-05-22 07:59:10 +08:00
BeatmapPanel panel = group.BeatmapPanels[RNG.Next(group.BeatmapPanels.Count)];
selectGroup(group, panel);
}
2017-06-05 17:24:28 +08:00
public void SelectPreviousRandom()
2017-06-02 01:54:42 +08:00
{
if (!randomSelectedBeatmaps.Any())
return;
var visibleGroups = getVisibleGroups();
if (!visibleGroups.Any())
return;
2017-06-05 17:24:28 +08:00
while (randomSelectedBeatmaps.Any())
2017-06-02 01:54:42 +08:00
{
2017-06-15 11:37:20 +08:00
var beatmapCoordinates = randomSelectedBeatmaps.Pop();
var group = beatmapCoordinates.Key;
2017-06-05 17:24:28 +08:00
if (visibleGroups.Contains(group))
2017-06-02 01:54:42 +08:00
{
2017-06-15 11:37:20 +08:00
selectGroup(group, beatmapCoordinates.Value);
2017-06-02 01:54:42 +08:00
break;
}
}
}
private FilterCriteria criteria = new FilterCriteria();
private ScheduledDelegate filterTask;
public bool AllowSelection = true;
2017-07-21 16:20:52 +08:00
public void FlushPendingFilters()
{
if (filterTask?.Completed == false)
Filter(null, false);
}
public void Filter(FilterCriteria newCriteria = null, bool debounce = true)
{
if (newCriteria != null)
criteria = newCriteria;
if (!IsLoaded) return;
Action perform = delegate
2016-11-25 17:14:56 +08:00
{
filterTask = null;
criteria.Filter(groups);
var filtered = new List<BeatmapGroup>(groups);
scrollableContent.Clear(false);
panels.Clear();
groups.Clear();
foreach (var g in filtered)
addGroup(g);
computeYPositions();
2017-03-17 19:09:23 +08:00
if (selectedGroup == null || selectedGroup.State == BeatmapGroupState.Hidden)
SelectNext();
else
selectGroup(selectedGroup, selectedPanel);
};
filterTask?.Cancel();
filterTask = null;
if (debounce)
filterTask = Scheduler.AddDelayed(perform, 250);
else
perform();
}
2017-07-29 21:03:17 +08:00
public void ScrollToSelected(bool animated = true)
{
float selectedY = computeYPositions(animated);
ScrollTo(selectedY, animated);
}
private BeatmapGroup createGroup(BeatmapSetInfo beatmapSet)
{
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
}
return new BeatmapGroup(beatmapSet, manager)
{
SelectionChanged = (g, p) => selectGroup(g, p),
StartRequested = b => StartRequested?.Invoke(),
2017-08-30 19:41:41 +08:00
DeleteRequested = b => DeleteRequested?.Invoke(b),
RestoreHiddenRequested = s => RestoreRequested?.Invoke(s),
2017-09-04 06:59:32 +08:00
HideDifficultyRequested = b => HideDifficultyRequested?.Invoke(b),
State = BeatmapGroupState.Collapsed
};
}
[BackgroundDependencyLoader(permitNulls: true)]
private void load(BeatmapManager manager, OsuConfigManager config)
{
this.manager = manager;
2017-07-18 17:43:28 +08:00
randomType = config.GetBindable<SelectionRandomType>(OsuSetting.SelectionRandomType);
}
private void addGroup(BeatmapGroup group)
{
groups.Add(group);
panels.Add(group.Header);
panels.AddRange(group.BeatmapPanels);
}
private void removeGroup(BeatmapGroup group)
{
if (group == null)
return;
groups.Remove(group);
2017-03-14 04:35:43 +08:00
panels.Remove(group.Header);
2017-03-04 16:34:28 +08:00
foreach (var p in group.BeatmapPanels)
panels.Remove(p);
scrollableContent.Remove(group.Header);
2017-07-11 21:58:06 +08:00
scrollableContent.RemoveRange(group.BeatmapPanels);
if (selectedGroup == group)
SelectNext();
computeYPositions();
}
/// <summary>
/// Computes the target Y positions for every panel in the carousel.
/// </summary>
/// <returns>The Y position of the currently selected panel.</returns>
private float computeYPositions(bool animated = true)
{
yPositions.Clear();
2016-11-21 14:59:46 +08:00
float currentY = DrawHeight / 2;
float selectedY = currentY;
foreach (BeatmapGroup group in groups)
{
movePanel(group.Header, group.State != BeatmapGroupState.Hidden, animated, ref currentY);
if (group.State == BeatmapGroupState.Expanded)
{
2017-07-23 02:50:25 +08:00
group.Header.MoveToX(-100, 500, Easing.OutExpo);
var headerY = group.Header.Position.Y;
foreach (BeatmapPanel panel in group.BeatmapPanels)
{
if (panel == selectedPanel)
selectedY = currentY + panel.DrawHeight / 2 - DrawHeight / 2;
2016-11-21 14:59:46 +08:00
2017-07-23 02:50:25 +08:00
panel.MoveToX(-50, 500, Easing.OutExpo);
//on first display we want to begin hidden under our group's header.
2017-02-02 08:15:22 +08:00
if (panel.Alpha == 0)
panel.MoveToY(headerY);
movePanel(panel, true, animated, ref currentY);
}
}
else
{
2017-07-23 02:50:25 +08:00
group.Header.MoveToX(0, 500, Easing.OutExpo);
foreach (BeatmapPanel panel in group.BeatmapPanels)
{
2017-07-23 02:50:25 +08:00
panel.MoveToX(0, 500, Easing.OutExpo);
movePanel(panel, false, animated, ref currentY);
}
}
}
2016-11-21 14:59:46 +08:00
currentY += DrawHeight / 2;
scrollableContent.Height = currentY;
return selectedY;
2016-11-21 14:59:46 +08:00
}
private void movePanel(Panel panel, bool advance, bool animated, ref float currentY)
{
yPositions.Add(currentY);
2017-07-23 02:50:25 +08:00
panel.MoveToY(currentY, animated ? 750 : 0, Easing.OutExpo);
if (advance)
currentY += panel.DrawHeight + 5;
}
private void selectGroup(BeatmapGroup group, BeatmapPanel panel = null, bool animated = true)
{
try
{
if (panel == null)
panel = group.BeatmapPanels.First();
if (selectedPanel == panel) return;
Trace.Assert(group.BeatmapPanels.Contains(panel), @"Selected panel must be in provided group");
2017-03-04 21:05:02 +08:00
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
{
2017-07-29 21:03:17 +08:00
ScrollToSelected(animated);
}
}
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
2017-03-02 21:01:53 +08:00
{
int direction = 0;
bool skipDifficulties = false;
switch (args.Key)
2017-03-02 21:01:53 +08:00
{
case Key.Up:
direction = -1;
2017-02-18 00:41:53 +08:00
break;
case Key.Down:
direction = 1;
2017-02-18 00:41:53 +08:00
break;
case Key.Left:
direction = -1;
skipDifficulties = true;
2017-02-18 00:41:53 +08:00
break;
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
if (direction == 0)
return base.OnKeyDown(state, args);
SelectNext(direction, skipDifficulties);
return true;
}
protected override void Update()
{
base.Update();
float drawHeight = DrawHeight;
2016-11-25 17:14:56 +08:00
// Remove all panels that should no longer be on-screen
scrollableContent.RemoveAll(delegate (Panel p)
2017-01-18 08:18:15 +08:00
{
float panelPosY = p.Position.Y;
bool remove = panelPosY < Current - p.DrawHeight || panelPosY > Current + drawHeight || !p.IsPresent;
return remove;
});
// Find index range of all panels that should be on-screen
Trace.Assert(panels.Count == yPositions.Count);
int firstIndex = yPositions.BinarySearch(Current - Panel.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)
lastIndex++;
}
// Add those panels 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)
{
Panel panel = panels[i];
if (panel.State == PanelSelectedState.Hidden)
continue;
2017-03-04 16:34:39 +08:00
// Only add if we're not already part of the content.
if (!scrollableContent.Contains(panel))
{
2017-03-04 16:34:39 +08:00
// Makes sure headers are always _below_ panels,
// and depth flows downward.
panel.Depth = i + (panel is BeatmapSetHeader ? panels.Count : 0);
scrollableContent.Add(panel);
}
}
2017-03-04 16:34:39 +08:00
// Update externally controlled state of currently visible panels
// (e.g. x-offset and opacity).
float halfHeight = drawHeight / 2;
foreach (Panel p in scrollableContent.Children)
updatePanel(p, halfHeight);
}
/// <summary>
/// Computes the x-offset of currently visible panels. 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;
double discriminant = Math.Max(0, circle_radius * circle_radius - dist * dist);
float x = (circle_radius - (float)Math.Sqrt(discriminant)) * halfHeight;
return 125 + x;
}
/// <summary>
/// Update a panel'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="halfHeight">Half the draw height of the carousel container.</param>
private void updatePanel(Panel 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);
2017-03-04 21:05:02 +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 panel gets selected, we
// may want to smoothly transform it leftwards.)
p.OriginPosition = new Vector2(-offsetX(dist, halfHeight), 0);
2017-03-04 21:05:02 +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));
}
}
}