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
|
|
|
|
2019-10-28 06:14:14 +08:00
|
|
|
using System;
|
2018-04-13 17:19:50 +08:00
|
|
|
using System.Collections.Generic;
|
2019-10-28 06:14:14 +08:00
|
|
|
using System.Linq;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public IReadOnlyList<CarouselItem> Children => InternalChildren;
|
|
|
|
|
|
|
|
protected List<CarouselItem> InternalChildren = new List<CarouselItem>();
|
|
|
|
|
2018-04-23 18:01:01 +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;
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
public override List<DrawableCarouselItem> Drawables
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var drawables = base.Drawables;
|
2018-04-24 00:52:25 +08:00
|
|
|
|
|
|
|
// if we are explicitly not present, don't ever present children.
|
|
|
|
// without this check, children drawables can potentially be presented without their group header.
|
|
|
|
if (DrawableRepresentation.Value?.IsPresent == false) return drawables;
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
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)
|
|
|
|
{
|
2019-02-22 16:51:39 +08:00
|
|
|
i.State.ValueChanged += state => ChildItemStateChanged(i, state.NewValue);
|
2018-04-23 18:01:01 +08:00
|
|
|
i.ChildID = ++currentChildID;
|
2018-04-13 17:19:50 +08:00
|
|
|
InternalChildren.Add(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
public CarouselGroup(List<CarouselItem> items = null)
|
|
|
|
{
|
|
|
|
if (items != null) InternalChildren = items;
|
|
|
|
|
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:
|
|
|
|
InternalChildren.ForEach(c => c.State.Value = CarouselItemState.Collapsed);
|
|
|
|
break;
|
2019-04-01 11:44:46 +08:00
|
|
|
|
2018-04-13 17:19:50 +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;
|
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
|
|
|
|
2019-10-28 06:14:14 +08:00
|
|
|
InternalChildren.ForEach(c => c.Filter(criteria));
|
|
|
|
// IEnumerable<T>.OrderBy() is used instead of List<T>.Sort() to ensure sorting stability
|
|
|
|
InternalChildren = InternalChildren.OrderBy(c => c, new CriteriaComparer(criteria)).ToList();
|
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)
|
|
|
|
{
|
|
|
|
foreach (var b in InternalChildren)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 06:14:14 +08:00
|
|
|
|
|
|
|
private class CriteriaComparer : IComparer<CarouselItem>
|
|
|
|
{
|
|
|
|
private readonly FilterCriteria criteria;
|
|
|
|
|
|
|
|
public CriteriaComparer(FilterCriteria criteria)
|
|
|
|
{
|
|
|
|
this.criteria = criteria;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Compare(CarouselItem x, CarouselItem y)
|
|
|
|
{
|
|
|
|
if (x != null && y != null)
|
|
|
|
return x.CompareTo(criteria, y);
|
|
|
|
|
|
|
|
throw new ArgumentNullException();
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
}
|