2019-01-24 08:43:03 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
using System;
|
|
|
|
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()
|
|
|
|
{
|
2019-02-22 08:51:39 +00:00
|
|
|
State.ValueChanged += state =>
|
2018-04-13 09:19:50 +00:00
|
|
|
{
|
2019-02-22 08:51:39 +00:00
|
|
|
if (state.NewValue == CarouselItemState.Selected)
|
2018-04-13 09:19:50 +00:00
|
|
|
attemptSelection();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The last selected item.
|
|
|
|
/// </summary>
|
|
|
|
protected CarouselItem LastSelected { get; private set; }
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
private int lastSelectedIndex;
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
|
|
public override void Filter(FilterCriteria criteria)
|
|
|
|
{
|
|
|
|
filteringChildren = true;
|
|
|
|
base.Filter(criteria);
|
|
|
|
filteringChildren = false;
|
|
|
|
|
|
|
|
attemptSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void RemoveChild(CarouselItem i)
|
|
|
|
{
|
|
|
|
base.RemoveChild(i);
|
|
|
|
|
|
|
|
if (i != LastSelected)
|
|
|
|
updateSelectedIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void AddChild(CarouselItem i)
|
|
|
|
{
|
|
|
|
base.AddChild(i);
|
|
|
|
attemptSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ChildItemStateChanged(CarouselItem item, CarouselItemState value)
|
|
|
|
{
|
|
|
|
base.ChildItemStateChanged(item, value);
|
|
|
|
|
|
|
|
switch (value)
|
|
|
|
{
|
|
|
|
case CarouselItemState.Selected:
|
|
|
|
updateSelected(item);
|
|
|
|
break;
|
2019-04-01 03:44:46 +00:00
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
case CarouselItemState.NotSelected:
|
|
|
|
case CarouselItemState.Collapsed:
|
|
|
|
attemptSelection();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void attemptSelection()
|
|
|
|
{
|
|
|
|
if (filteringChildren) return;
|
|
|
|
|
|
|
|
// we only perform eager selection if we are a currently selected group.
|
2019-02-21 09:56:34 +00:00
|
|
|
if (State.Value != CarouselItemState.Selected) return;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
// we only perform eager selection if none of our children are in a selected state already.
|
2019-02-21 09:56:34 +00:00
|
|
|
if (Children.Any(i => i.State.Value == CarouselItemState.Selected)) return;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
PerformSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void PerformSelection()
|
|
|
|
{
|
|
|
|
CarouselItem nextToSelect =
|
2019-02-21 09:56:34 +00:00
|
|
|
Children.Skip(lastSelectedIndex).FirstOrDefault(i => !i.Filtered.Value) ??
|
|
|
|
Children.Reverse().Skip(InternalChildren.Count - lastSelectedIndex).FirstOrDefault(i => !i.Filtered.Value);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
if (nextToSelect != null)
|
|
|
|
nextToSelect.State.Value = CarouselItemState.Selected;
|
|
|
|
else
|
|
|
|
updateSelected(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateSelected(CarouselItem newSelection)
|
|
|
|
{
|
2020-03-19 09:07:39 +00:00
|
|
|
if (newSelection != null)
|
|
|
|
LastSelected = newSelection;
|
2018-04-13 09:19:50 +00:00
|
|
|
updateSelectedIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateSelectedIndex() => lastSelectedIndex = LastSelected == null ? 0 : Math.Max(0, InternalChildren.IndexOf(LastSelected));
|
|
|
|
}
|
|
|
|
}
|