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

90 lines
3.1 KiB
C#
Raw Normal View History

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
using System.Collections.Generic;
namespace osu.Game.Screens.Select.Carousel
{
/// <summary>
/// A group which ensures only one child is selected.
/// </summary>
public class CarouselGroup : CarouselItem
{
protected override DrawableCarouselItem CreateDrawableRepresentation() => null;
2017-12-18 10:57:13 +08:00
public IReadOnlyList<CarouselItem> Children => InternalChildren;
protected List<CarouselItem> InternalChildren = new List<CarouselItem>();
public override List<DrawableCarouselItem> Drawables
{
get
{
var drawables = base.Drawables;
foreach (var c in InternalChildren)
drawables.AddRange(c.Drawables);
return drawables;
}
}
public virtual void RemoveChild(CarouselItem i)
{
InternalChildren.Remove(i);
// 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;
}
public virtual void AddChild(CarouselItem i)
2017-12-12 16:48:38 +08:00
{
i.State.ValueChanged += v => ChildItemStateChanged(i, v);
2017-12-18 10:57:13 +08:00
InternalChildren.Add(i);
2017-12-12 16:48:38 +08:00
}
public CarouselGroup(List<CarouselItem> items = null)
{
2017-12-14 19:40:58 +08:00
if (items != null) InternalChildren = items;
2017-12-18 10:57:13 +08:00
State.ValueChanged += v =>
{
switch (v)
{
case CarouselItemState.Collapsed:
case CarouselItemState.NotSelected:
InternalChildren.ForEach(c => c.State.Value = CarouselItemState.Collapsed);
break;
case CarouselItemState.Selected:
InternalChildren.ForEach(c =>
{
if (c.State == CarouselItemState.Collapsed) c.State.Value = CarouselItemState.NotSelected;
});
break;
}
};
}
public override void Filter(FilterCriteria criteria)
{
base.Filter(criteria);
InternalChildren.Sort((x, y) => x.CompareTo(criteria, y));
InternalChildren.ForEach(c => c.Filter(criteria));
2017-12-12 16:48:38 +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;
b.State.Value = CarouselItemState.NotSelected;
}
State.Value = CarouselItemState.Selected;
}
}
}
}