// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Linq; namespace osu.Game.Screens.Select.Carousel { /// /// A group which ensures at least one child is selected (if the group itself is selected). /// public class CarouselGroupEagerSelect : CarouselGroup { public CarouselGroupEagerSelect() { State.ValueChanged += state => { if (state.NewValue == CarouselItemState.Selected) attemptSelection(); }; } /// /// The last selected item. /// protected CarouselItem LastSelected { get; private set; } /// /// 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. /// private int lastSelectedIndex; /// /// 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 /// operation. /// 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 void AddChildren(IEnumerable items) { foreach (var i in items) base.AddChild(i); attemptSelection(); } 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; 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. if (State.Value != 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.Value == CarouselItemState.Selected)) return; PerformSelection(); } /// /// Finds the item this group would select next if it attempted selection /// /// An unfiltered item nearest to the last selected one or null if all items are filtered protected virtual CarouselItem GetNextToSelect() { int forwardsIndex = lastSelectedIndex; int backwardsIndex = lastSelectedIndex; while (true) { if (forwardsIndex >= Children.Count) return Children.Reverse().Skip(Children.Count - backwardsIndex).FirstOrDefault(item => !item.Filtered.Value); if (backwardsIndex < 0) return Children.Skip(forwardsIndex).FirstOrDefault(item => !item.Filtered.Value); if (!Children[forwardsIndex].Filtered.Value) return Children[forwardsIndex]; if (!Children[backwardsIndex].Filtered.Value) return Children[backwardsIndex]; forwardsIndex++; backwardsIndex--; } } protected virtual void PerformSelection() { CarouselItem nextToSelect = GetNextToSelect(); if (nextToSelect != null) nextToSelect.State.Value = CarouselItemState.Selected; else updateSelected(null); } private void updateSelected(CarouselItem newSelection) { if (newSelection != null) LastSelected = newSelection; updateSelectedIndex(); } private void updateSelectedIndex() => lastSelectedIndex = LastSelected == null ? 0 : Math.Max(0, InternalChildren.IndexOf(LastSelected)); } }