1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 21:47:25 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/CarouselItem.cs

90 lines
3.0 KiB
C#
Raw Normal View History

2017-12-12 16:48:38 +08:00
// 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;
using osu.Framework.Logging;
2017-12-12 16:48:38 +08:00
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);
2017-12-14 19:40:58 +08:00
public IReadOnlyList<CarouselItem> Children => InternalChildren;
2017-12-12 16:48:38 +08:00
2017-12-14 19:40:58 +08:00
protected List<CarouselItem> InternalChildren { get; set; }
2017-12-12 16:48:38 +08:00
2017-12-14 19:40:58 +08:00
public bool Visible => State.Value != CarouselItemState.Hidden && !Filtered.Value;
2017-12-12 16:48:38 +08:00
2017-12-14 19:40:58 +08:00
public IEnumerable<DrawableCarouselItem> Drawables
2017-12-12 16:48:38 +08:00
{
2017-12-14 19:40:58 +08:00
get
2017-12-12 16:48:38 +08:00
{
List<DrawableCarouselItem> items = new List<DrawableCarouselItem>();
2017-12-14 19:40:58 +08:00
var self = drawableRepresentation.Value;
2017-12-12 16:48:38 +08:00
if (self != null) items.Add(self);
2017-12-14 19:40:58 +08:00
if (InternalChildren != null)
foreach (var c in InternalChildren)
items.AddRange(c.Drawables);
2017-12-12 16:48:38 +08:00
return items;
2017-12-14 19:40:58 +08:00
}
}
public virtual void AddChild(CarouselItem i) => (InternalChildren ?? (InternalChildren = new List<CarouselItem>())).Add(i);
public virtual void RemoveChild(CarouselItem i) => InternalChildren?.Remove(i);
protected CarouselItem()
{
drawableRepresentation = new Lazy<DrawableCarouselItem>(CreateDrawableRepresentation);
2017-12-12 16:48:38 +08:00
State.ValueChanged += v =>
{
2017-12-14 19:40:58 +08:00
if (InternalChildren == null) return;
2017-12-12 16:48:38 +08:00
Logger.Log($"State changed to {v}");
2017-12-12 16:48:38 +08:00
switch (v)
{
case CarouselItemState.Hidden:
case CarouselItemState.NotSelected:
2017-12-14 19:40:58 +08:00
InternalChildren.ForEach(c => c.State.Value = CarouselItemState.Hidden);
2017-12-12 16:48:38 +08:00
break;
}
};
Filtered.ValueChanged += v =>
{
if (v && State == CarouselItemState.Selected)
State.Value = CarouselItemState.NotSelected;
};
2017-12-12 16:48:38 +08:00
}
2017-12-14 19:40:58 +08:00
private readonly Lazy<DrawableCarouselItem> drawableRepresentation;
2017-12-12 16:48:38 +08:00
protected abstract DrawableCarouselItem CreateDrawableRepresentation();
2017-12-14 19:40:58 +08:00
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());
2017-12-12 16:48:38 +08:00
}
2017-12-13 18:56:16 +08:00
public enum CarouselItemState
{
Hidden,
NotSelected,
Selected,
}
2017-12-12 16:48:38 +08:00
}