1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 22:42:57 +08:00

Make selection at random when last was null

This commit is contained in:
Endrik Tombak 2018-03-30 00:15:32 +03:00
parent 33b38b68cc
commit 3f65e3a7e3
2 changed files with 21 additions and 6 deletions

View File

@ -66,7 +66,7 @@ namespace osu.Game.Screens.Select
get { return beatmapSets.Select(g => g.BeatmapSet); }
set
{
CarouselGroup newRoot = new CarouselGroupEagerSelect();
CarouselGroup newRoot = new CarouselGroupEagerSelect(true);
Task.Run(() =>
{
@ -102,7 +102,7 @@ namespace osu.Game.Screens.Select
private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>();
protected List<DrawableCarouselItem> Items = new List<DrawableCarouselItem>();
private CarouselGroup root = new CarouselGroupEagerSelect();
private CarouselGroup root = new CarouselGroupEagerSelect(true);
public BeatmapCarousel()
{

View File

@ -1,6 +1,7 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.MathUtils;
using System;
using System.Linq;
@ -11,8 +12,11 @@ namespace osu.Game.Screens.Select.Carousel
/// </summary>
public class CarouselGroupEagerSelect : CarouselGroup
{
public CarouselGroupEagerSelect()
private readonly bool isRootSelector;
public CarouselGroupEagerSelect(bool isRootSelector = false)
{
this.isRootSelector = isRootSelector;
State.ValueChanged += v =>
{
if (v == CarouselItemState.Selected)
@ -83,9 +87,20 @@ namespace osu.Game.Screens.Select.Carousel
// we only perform eager selection if none of our children are in a selected state already.
if (Children.Any(i => i.State == CarouselItemState.Selected)) return;
CarouselItem nextToSelect =
Children.Skip(lastSelectedIndex).FirstOrDefault(i => !i.Filtered) ??
Children.Reverse().Skip(InternalChildren.Count - lastSelectedIndex).FirstOrDefault(i => !i.Filtered);
CarouselItem nextToSelect = null;
if (isRootSelector && lastSelected == null)
{
var selectables = Children.Where(i => !i.Filtered).ToList();
if (selectables.Any())
nextToSelect = selectables[RNG.Next(selectables.Count)];
}
else
{
nextToSelect =
Children.Skip(lastSelectedIndex).FirstOrDefault(i => !i.Filtered) ??
Children.Reverse().Skip(InternalChildren.Count - lastSelectedIndex).FirstOrDefault(i => !i.Filtered);
}
if (nextToSelect != null)
nextToSelect.State.Value = CarouselItemState.Selected;