2018-01-05 19:21:19 +08:00
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-12-12 16:48:38 +08:00
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
2017-12-16 16:32:21 +08:00
|
|
|
using System;
|
2017-12-12 16:48:38 +08:00
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Select.Carousel
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A group which ensures at least one child is selected (if the group itself is selected).
|
|
|
|
/// </summary>
|
|
|
|
public class CarouselGroupEagerSelect : CarouselGroup
|
|
|
|
{
|
|
|
|
public CarouselGroupEagerSelect()
|
|
|
|
{
|
|
|
|
State.ValueChanged += v =>
|
|
|
|
{
|
|
|
|
if (v == CarouselItemState.Selected)
|
2017-12-16 15:14:37 +08:00
|
|
|
attemptSelection();
|
2017-12-12 16:48:38 +08:00
|
|
|
};
|
|
|
|
}
|
2017-12-15 23:33:09 +08:00
|
|
|
|
2017-12-16 16:32:21 +08:00
|
|
|
/// <summary>
|
|
|
|
/// We need to keep track of the index for cases where the selection is removed but we want to select a new item based on its old location.
|
|
|
|
/// </summary>
|
2017-12-16 15:14:37 +08:00
|
|
|
private int lastSelectedIndex;
|
|
|
|
|
2017-12-16 16:32:21 +08:00
|
|
|
private CarouselItem lastSelected;
|
|
|
|
|
2017-12-17 03:30:56 +08:00
|
|
|
/// <summary>
|
|
|
|
/// To avoid overhead during filter operations, we don't attempt any selections until after all
|
|
|
|
/// children have been filtered. This bool will be true during the base <see cref="Filter(FilterCriteria)"/>
|
|
|
|
/// operation.
|
|
|
|
/// </summary>
|
|
|
|
private bool filteringChildren;
|
|
|
|
|
2017-12-16 15:14:37 +08:00
|
|
|
public override void Filter(FilterCriteria criteria)
|
|
|
|
{
|
2017-12-17 03:30:56 +08:00
|
|
|
filteringChildren = true;
|
2017-12-16 15:14:37 +08:00
|
|
|
base.Filter(criteria);
|
2017-12-17 03:30:56 +08:00
|
|
|
filteringChildren = false;
|
|
|
|
|
2017-12-16 15:14:37 +08:00
|
|
|
attemptSelection();
|
|
|
|
}
|
|
|
|
|
2017-12-16 16:32:21 +08:00
|
|
|
public override void RemoveChild(CarouselItem i)
|
|
|
|
{
|
|
|
|
base.RemoveChild(i);
|
|
|
|
|
|
|
|
if (i != lastSelected)
|
|
|
|
updateSelectedIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void AddChild(CarouselItem i)
|
|
|
|
{
|
|
|
|
base.AddChild(i);
|
2017-12-18 06:58:34 +08:00
|
|
|
attemptSelection();
|
2017-12-16 16:32:21 +08:00
|
|
|
}
|
|
|
|
|
2017-12-16 15:14:37 +08:00
|
|
|
protected override void ChildItemStateChanged(CarouselItem item, CarouselItemState value)
|
2017-12-15 23:33:09 +08:00
|
|
|
{
|
2017-12-16 15:14:37 +08:00
|
|
|
base.ChildItemStateChanged(item, value);
|
2017-12-15 23:33:09 +08:00
|
|
|
|
2017-12-16 15:14:37 +08:00
|
|
|
switch (value)
|
2017-12-15 23:33:09 +08:00
|
|
|
{
|
2017-12-16 15:14:37 +08:00
|
|
|
case CarouselItemState.Selected:
|
2017-12-16 16:32:21 +08:00
|
|
|
updateSelected(item);
|
2017-12-16 15:14:37 +08:00
|
|
|
break;
|
|
|
|
case CarouselItemState.NotSelected:
|
2017-12-17 04:53:13 +08:00
|
|
|
case CarouselItemState.Collapsed:
|
2017-12-16 15:14:37 +08:00
|
|
|
attemptSelection();
|
|
|
|
break;
|
2017-12-15 23:33:09 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-16 15:14:37 +08:00
|
|
|
|
|
|
|
private void attemptSelection()
|
|
|
|
{
|
2017-12-17 03:30:56 +08:00
|
|
|
if (filteringChildren) return;
|
|
|
|
|
2017-12-16 15:14:37 +08:00
|
|
|
// we only perform eager selection if we are a currently selected group.
|
|
|
|
if (State != CarouselItemState.Selected) return;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
if (nextToSelect != null)
|
|
|
|
nextToSelect.State.Value = CarouselItemState.Selected;
|
2017-12-16 16:32:21 +08:00
|
|
|
else
|
|
|
|
updateSelected(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateSelected(CarouselItem newSelection)
|
|
|
|
{
|
|
|
|
lastSelected = newSelection;
|
|
|
|
updateSelectedIndex();
|
2017-12-16 15:14:37 +08:00
|
|
|
}
|
2017-12-16 16:32:21 +08:00
|
|
|
|
2017-12-18 06:58:34 +08:00
|
|
|
private void updateSelectedIndex() => lastSelectedIndex = lastSelected == null ? 0 : Math.Max(0, InternalChildren.IndexOf(lastSelected));
|
2017-12-12 16:48:38 +08:00
|
|
|
}
|
|
|
|
}
|