1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-29 01:02:55 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/CarouselGroup.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
3.9 KiB
C#
Raw Normal View History

// 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
2017-12-12 16:48:38 +08:00
using System.Collections.Generic;
using System.Linq;
2022-01-21 12:09:03 +08:00
2017-12-12 16:48:38 +08:00
namespace osu.Game.Screens.Select.Carousel
{
/// <summary>
/// A group which ensures only one child is selected.
/// </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
2017-12-18 10:57:13 +08:00
public IReadOnlyList<CarouselItem> Children => InternalChildren;
2018-04-13 17:19:50 +08:00
2017-12-18 10:57:13 +08:00
protected List<CarouselItem> InternalChildren = new List<CarouselItem>();
2018-04-13 17:19:50 +08:00
/// <summary>
/// Used to assign a monotonically increasing ID to children as they are added. This member is
/// incremented whenever a child is added.
/// </summary>
private ulong currentChildID;
2022-01-21 12:09:03 +08:00
private Comparer<CarouselItem>? criteriaComparer;
private FilterCriteria? lastCriteria;
2017-12-18 10:57:13 +08:00
public virtual void RemoveChild(CarouselItem i)
{
InternalChildren.Remove(i);
2018-04-13 17:19:50 +08:00
2017-12-18 10:57:13 +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;
}
2018-04-13 17:19:50 +08:00
2017-12-18 10:57:13 +08:00
public virtual void AddChild(CarouselItem i)
2017-12-12 16:48:38 +08:00
{
i.State.ValueChanged += state => ChildItemStateChanged(i, state.NewValue);
i.ChildID = ++currentChildID;
if (lastCriteria != null)
{
i.Filter(lastCriteria);
int index = InternalChildren.BinarySearch(i, criteriaComparer);
if (index < 0) index = ~index; // BinarySearch hacks multiple return values with 2's complement.
InternalChildren.Insert(index, i);
}
else
{
// criteria may be null for initial population. the filtering will be applied post-add.
InternalChildren.Add(i);
}
2017-12-12 16:48:38 +08:00
}
2018-04-13 17:19:50 +08:00
2022-01-21 12:09:03 +08:00
public CarouselGroup(List<CarouselItem>? items = null)
2017-12-12 16:48:38 +08:00
{
2017-12-14 19:40:58 +08:00
if (items != null) InternalChildren = items;
2018-04-13 17:19:50 +08:00
State.ValueChanged += state =>
2017-12-18 10:57:13 +08:00
{
switch (state.NewValue)
2017-12-18 10:57:13 +08:00
{
case CarouselItemState.Collapsed:
case CarouselItemState.NotSelected:
InternalChildren.ForEach(c => c.State.Value = CarouselItemState.Collapsed);
break;
2019-04-01 11:44:46 +08:00
2017-12-18 10:57:13 +08:00
case CarouselItemState.Selected:
InternalChildren.ForEach(c =>
{
2019-02-21 17:56:34 +08:00
if (c.State.Value == CarouselItemState.Collapsed) c.State.Value = CarouselItemState.NotSelected;
2017-12-18 10:57:13 +08:00
});
break;
}
};
}
2018-04-13 17:19:50 +08:00
2017-12-18 10:57:13 +08:00
public override void Filter(FilterCriteria criteria)
{
base.Filter(criteria);
InternalChildren.ForEach(c => c.Filter(criteria));
// IEnumerable<T>.OrderBy() is used instead of List<T>.Sort() to ensure sorting stability
criteriaComparer = Comparer<CarouselItem>.Create((x, y) => x.CompareTo(criteria, y));
InternalChildren = InternalChildren.OrderBy(c => c, criteriaComparer).ToList();
lastCriteria = criteria;
2017-12-12 16:48:38 +08:00
}
2018-04-13 17:19:50 +08:00
protected virtual void ChildItemStateChanged(CarouselItem item, CarouselItemState value)
2017-12-12 16:48:38 +08:00
{
// ensure we are the only item selected
if (value == CarouselItemState.Selected)
{
2017-12-14 19:40:58 +08:00
foreach (var b in InternalChildren)
2017-12-12 16:48:38 +08:00
{
if (item == b) continue;
2019-02-28 12:31:40 +08:00
2017-12-12 16:48:38 +08:00
b.State.Value = CarouselItemState.NotSelected;
}
2018-04-13 17:19:50 +08:00
2017-12-12 16:48:38 +08:00
State.Value = CarouselItemState.Selected;
}
}
}
}