2019-01-24 16:43:03 +08: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 17:19:50 +08:00
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2022-01-21 12:09:03 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
namespace osu.Game.Screens.Select.Carousel
|
|
|
|
{
|
|
|
|
/// <summary>
|
2022-07-26 14:39:30 +08:00
|
|
|
/// A group which ensures only one item is selected.
|
2018-04-13 17:19:50 +08:00
|
|
|
/// </summary>
|
|
|
|
public class CarouselGroup : CarouselItem
|
|
|
|
{
|
2022-01-21 12:09:03 +08:00
|
|
|
public override DrawableCarouselItem? CreateDrawableRepresentation() => null;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-07-21 15:06:06 +08:00
|
|
|
public IReadOnlyList<CarouselItem> Items => items;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2023-12-18 19:44:08 +08:00
|
|
|
public int TotalItemsNotFiltered { get; private set; }
|
|
|
|
|
2022-08-02 00:13:57 +08:00
|
|
|
private readonly List<CarouselItem> items = new List<CarouselItem>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-04-23 18:01:01 +08:00
|
|
|
/// <summary>
|
2022-07-26 14:39:30 +08:00
|
|
|
/// Used to assign a monotonically increasing ID to items as they are added. This member is
|
|
|
|
/// incremented whenever an item is added.
|
2018-04-23 18:01:01 +08:00
|
|
|
/// </summary>
|
2022-07-26 14:39:30 +08:00
|
|
|
private ulong currentItemID;
|
2018-04-23 18:01:01 +08:00
|
|
|
|
2022-01-21 12:09:03 +08:00
|
|
|
private Comparer<CarouselItem>? criteriaComparer;
|
|
|
|
private FilterCriteria? lastCriteria;
|
|
|
|
|
2022-07-21 15:06:06 +08:00
|
|
|
protected int GetIndexOfItem(CarouselItem lastSelected) => items.IndexOf(lastSelected);
|
|
|
|
|
|
|
|
public virtual void RemoveItem(CarouselItem i)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2022-07-21 15:06:06 +08:00
|
|
|
items.Remove(i);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2023-12-18 19:44:08 +08:00
|
|
|
if (!i.Filtered.Value)
|
|
|
|
TotalItemsNotFiltered--;
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
// it's important we do the deselection after removing, so any further actions based on
|
|
|
|
// State.ValueChanged make decisions post-removal.
|
|
|
|
i.State.Value = CarouselItemState.Collapsed;
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:06:06 +08:00
|
|
|
public virtual void AddItem(CarouselItem i)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2019-02-22 16:51:39 +08:00
|
|
|
i.State.ValueChanged += state => ChildItemStateChanged(i, state.NewValue);
|
2022-07-26 14:39:30 +08:00
|
|
|
i.ItemID = ++currentItemID;
|
2022-01-21 11:37:17 +08:00
|
|
|
|
|
|
|
if (lastCriteria != null)
|
|
|
|
{
|
|
|
|
i.Filter(lastCriteria);
|
|
|
|
|
2022-07-21 15:06:06 +08:00
|
|
|
int index = items.BinarySearch(i, criteriaComparer);
|
2022-01-21 11:37:17 +08:00
|
|
|
if (index < 0) index = ~index; // BinarySearch hacks multiple return values with 2's complement.
|
|
|
|
|
2022-07-21 15:06:06 +08:00
|
|
|
items.Insert(index, i);
|
2022-01-21 11:37:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// criteria may be null for initial population. the filtering will be applied post-add.
|
2022-07-21 15:06:06 +08:00
|
|
|
items.Add(i);
|
2022-01-21 11:37:17 +08:00
|
|
|
}
|
2023-12-18 19:44:08 +08:00
|
|
|
|
|
|
|
if (!i.Filtered.Value)
|
2023-12-18 21:24:57 +08:00
|
|
|
TotalItemsNotFiltered++;
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
|
2022-01-21 12:09:03 +08:00
|
|
|
public CarouselGroup(List<CarouselItem>? items = null)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2022-07-21 15:06:06 +08:00
|
|
|
if (items != null) this.items = items;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-02-22 16:51:39 +08:00
|
|
|
State.ValueChanged += state =>
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2019-02-22 16:51:39 +08:00
|
|
|
switch (state.NewValue)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
|
|
|
case CarouselItemState.Collapsed:
|
|
|
|
case CarouselItemState.NotSelected:
|
2022-07-21 15:06:06 +08:00
|
|
|
this.items.ForEach(c => c.State.Value = CarouselItemState.Collapsed);
|
2018-04-13 17:19:50 +08:00
|
|
|
break;
|
2019-04-01 11:44:46 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
case CarouselItemState.Selected:
|
2022-07-21 15:06:06 +08:00
|
|
|
this.items.ForEach(c =>
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2019-02-21 17:56:34 +08:00
|
|
|
if (c.State.Value == CarouselItemState.Collapsed) c.State.Value = CarouselItemState.NotSelected;
|
2018-04-13 17:19:50 +08:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Filter(FilterCriteria criteria)
|
|
|
|
{
|
|
|
|
base.Filter(criteria);
|
2018-07-23 19:11:06 +08:00
|
|
|
|
2023-12-18 19:44:08 +08:00
|
|
|
TotalItemsNotFiltered = 0;
|
|
|
|
|
|
|
|
foreach (var c in items)
|
|
|
|
{
|
|
|
|
c.Filter(criteria);
|
|
|
|
if (!c.Filtered.Value)
|
|
|
|
TotalItemsNotFiltered++;
|
|
|
|
}
|
2022-01-21 11:37:17 +08:00
|
|
|
|
2023-12-05 13:15:05 +08:00
|
|
|
// Sorting is expensive, so only perform if it's actually changed.
|
2023-12-19 14:09:03 +08:00
|
|
|
if (lastCriteria?.RequiresSorting(criteria) != false)
|
2022-08-02 00:13:57 +08:00
|
|
|
{
|
2023-12-05 13:15:05 +08:00
|
|
|
criteriaComparer = Comparer<CarouselItem>.Create((x, y) =>
|
|
|
|
{
|
|
|
|
int comparison = x.CompareTo(criteria, y);
|
|
|
|
if (comparison != 0)
|
|
|
|
return comparison;
|
2022-08-02 00:13:57 +08:00
|
|
|
|
2023-12-05 13:15:05 +08:00
|
|
|
return x.ItemID.CompareTo(y.ItemID);
|
|
|
|
});
|
2022-08-02 00:13:57 +08:00
|
|
|
|
2023-12-05 13:15:05 +08:00
|
|
|
items.Sort(criteriaComparer);
|
|
|
|
}
|
2022-01-21 11:37:17 +08:00
|
|
|
|
|
|
|
lastCriteria = criteria;
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void ChildItemStateChanged(CarouselItem item, CarouselItemState value)
|
|
|
|
{
|
|
|
|
// ensure we are the only item selected
|
|
|
|
if (value == CarouselItemState.Selected)
|
|
|
|
{
|
2022-07-21 15:06:06 +08:00
|
|
|
foreach (var b in items)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
|
|
|
if (item == b) continue;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
b.State.Value = CarouselItemState.NotSelected;
|
|
|
|
}
|
|
|
|
|
|
|
|
State.Value = CarouselItemState.Selected;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|