1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 17:27:39 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/CarouselItem.cs

104 lines
3.7 KiB
C#

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using osu.Framework.Configuration;
namespace osu.Game.Screens.Select.Carousel
{
public abstract class CarouselItem
{
public readonly BindableBool Filtered = new BindableBool();
public readonly Bindable<CarouselItemState> State = new Bindable<CarouselItemState>(CarouselItemState.NotSelected);
public IReadOnlyList<CarouselItem> Children => InternalChildren;
protected List<CarouselItem> InternalChildren { get; set; }
/// <summary>
/// This item is not in a hidden state.
/// </summary>
public bool Visible => State.Value != CarouselItemState.Collapsed && !Filtered;
public IEnumerable<DrawableCarouselItem> Drawables
{
get
{
List<DrawableCarouselItem> items = new List<DrawableCarouselItem>();
var self = drawableRepresentation.Value;
if (self != null) items.Add(self);
if (InternalChildren != null)
foreach (var c in InternalChildren)
// if (!c.Filtered) <- potential optimisation at the cost of no fade out animations.
items.AddRange(c.Drawables);
return items;
}
}
public virtual void AddChild(CarouselItem i) => (InternalChildren ?? (InternalChildren = new List<CarouselItem>())).Add(i);
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.
if (i.State.Value == CarouselItemState.Selected) i.State.Value = CarouselItemState.NotSelected;
}
protected CarouselItem()
{
drawableRepresentation = new Lazy<DrawableCarouselItem>(CreateDrawableRepresentation);
State.ValueChanged += v =>
{
if (InternalChildren == null) return;
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;
}
};
Filtered.ValueChanged += v =>
{
if (v && State == CarouselItemState.Selected)
State.Value = CarouselItemState.NotSelected;
};
}
private readonly Lazy<DrawableCarouselItem> drawableRepresentation;
protected abstract DrawableCarouselItem CreateDrawableRepresentation();
public virtual void Filter(FilterCriteria criteria)
{
InternalChildren?.Sort((x, y) => x.CompareTo(criteria, y));
InternalChildren?.ForEach(c => c.Filter(criteria));
}
public virtual int CompareTo(FilterCriteria criteria, CarouselItem other) => GetHashCode().CompareTo(other.GetHashCode());
}
public enum CarouselItemState
{
Collapsed,
NotSelected,
Selected,
}
}