Add sorting support

This commit is contained in:
Dean Herbert
2017-12-14 20:40:58 +09:00
parent b4b2f12116
commit 67f05977ea
7 changed files with 204 additions and 153 deletions

View File

@ -4,6 +4,7 @@
using System;
using System.Linq;
using osu.Game.Beatmaps;
using osu.Game.Screens.Select.Filter;
namespace osu.Game.Screens.Select.Carousel
{
@ -32,5 +33,21 @@ namespace osu.Game.Screens.Select.Carousel
Filtered.Value = !match;
}
public override int CompareTo(FilterCriteria criteria, CarouselItem other)
{
if (!(other is CarouselBeatmap otherBeatmap))
return base.CompareTo(criteria, other);
switch (criteria.Sort)
{
default:
case SortMode.Difficulty:
var ruleset = Beatmap.RulesetID.CompareTo(otherBeatmap.Beatmap.RulesetID);
if (ruleset != 0) return ruleset;
return Beatmap.StarDifficulty.CompareTo(otherBeatmap.Beatmap.StarDifficulty);
}
}
}
}

View File

@ -4,52 +4,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Game.Beatmaps;
using osu.Game.Screens.Select.Filter;
namespace osu.Game.Screens.Select.Carousel
{
public class CarouselBeatmapSet : CarouselGroupEagerSelect
{
public readonly List<CarouselBeatmap> Beatmaps;
public IEnumerable<CarouselBeatmap> Beatmaps => InternalChildren.OfType<CarouselBeatmap>();
public BeatmapSetInfo BeatmapSet;
public CarouselBeatmapSet(BeatmapSetInfo beatmapSet)
{
if (beatmapSet == null) throw new ArgumentNullException(nameof(beatmapSet));
BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet));
BeatmapSet = beatmapSet;
Children = Beatmaps = beatmapSet.Beatmaps
.Where(b => !b.Hidden)
.OrderBy(b => b.RulesetID).ThenBy(b => b.StarDifficulty)
.Select(b => new CarouselBeatmap(b))
.ToList();
beatmapSet.Beatmaps
.Where(b => !b.Hidden)
.Select(b => new CarouselBeatmap(b))
.ForEach(AddChild);
}
protected override DrawableCarouselItem CreateDrawableRepresentation() => new DrawableCarouselBeatmapSet(this);
public override void Filter(FilterCriteria criteria)
public override int CompareTo(FilterCriteria criteria, CarouselItem other)
{
base.Filter(criteria);
Filtered.Value = Children.All(i => i.Filtered);
if (!(other is CarouselBeatmapSet otherSet))
return base.CompareTo(criteria, other);
/*switch (criteria.Sort)
switch (criteria.Sort)
{
default:
case SortMode.Artist:
groups.Sort((x, y) => string.Compare(x.BeatmapSet.Metadata.Artist, y.BeatmapSet.Metadata.Artist, StringComparison.InvariantCultureIgnoreCase));
break;
return string.Compare(BeatmapSet.Metadata.Artist, otherSet.BeatmapSet.Metadata.Artist, StringComparison.InvariantCultureIgnoreCase);
case SortMode.Title:
groups.Sort((x, y) => string.Compare(x.BeatmapSet.Metadata.Title, y.BeatmapSet.Metadata.Title, StringComparison.InvariantCultureIgnoreCase));
break;
return string.Compare(BeatmapSet.Metadata.Title, otherSet.BeatmapSet.Metadata.Title, StringComparison.InvariantCultureIgnoreCase);
case SortMode.Author:
groups.Sort((x, y) => string.Compare(x.BeatmapSet.Metadata.Author.Username, y.BeatmapSet.Metadata.Author.Username, StringComparison.InvariantCultureIgnoreCase));
break;
return string.Compare(BeatmapSet.Metadata.Author.Username, otherSet.BeatmapSet.Metadata.Author.Username, StringComparison.InvariantCultureIgnoreCase);
case SortMode.Difficulty:
groups.Sort((x, y) => x.BeatmapSet.MaxStarDifficulty.CompareTo(y.BeatmapSet.MaxStarDifficulty));
break;
}*/
return BeatmapSet.MaxStarDifficulty.CompareTo(otherSet.BeatmapSet.MaxStarDifficulty);
}
}
public override void Filter(FilterCriteria criteria)
{
base.Filter(criteria);
Filtered.Value = InternalChildren.All(i => i.Filtered);
}
}
}

View File

@ -3,7 +3,6 @@
using System.Collections.Generic;
using osu.Framework.Configuration;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Screens.Select.Carousel
{
@ -18,19 +17,15 @@ namespace osu.Game.Screens.Select.Carousel
protected override DrawableCarouselItem CreateDrawableRepresentation() => null;
protected override IEnumerable<CarouselItem> Children
public override void AddChild(CarouselItem i)
{
get { return base.Children; }
set
{
base.Children = value;
value.ForEach(i => i.State.ValueChanged += v => itemStateChanged(i, v));
}
i.State.ValueChanged += v => itemStateChanged(i, v);
base.AddChild(i);
}
public CarouselGroup(List<CarouselItem> items = null)
{
if (items != null) Children = items;
if (items != null) InternalChildren = items;
}
private void itemStateChanged(CarouselItem item, CarouselItemState value)
@ -40,7 +35,7 @@ namespace osu.Game.Screens.Select.Carousel
// ensure we are the only item selected
if (value == CarouselItemState.Selected)
{
foreach (var b in Children)
foreach (var b in InternalChildren)
{
if (item == b) continue;
b.State.Value = CarouselItemState.NotSelected;

View File

@ -16,11 +16,11 @@ namespace osu.Game.Screens.Select.Carousel
{
if (v == CarouselItemState.Selected)
{
foreach (var c in Children.Where(c => c.State.Value == CarouselItemState.Hidden))
foreach (var c in InternalChildren.Where(c => c.State.Value == CarouselItemState.Hidden))
c.State.Value = CarouselItemState.NotSelected;
if (Children.Any(c => c.Visible) && Children.All(c => c.State != CarouselItemState.Selected))
Children.First(c => c.Visible).State.Value = CarouselItemState.Selected;
if (InternalChildren.Any(c => c.Visible) && InternalChildren.All(c => c.State != CarouselItemState.Selected))
InternalChildren.First(c => c.Visible).State.Value = CarouselItemState.Selected;
}
};
}

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using osu.Framework.Configuration;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Screens.Select.Carousel
{
@ -14,45 +13,62 @@ namespace osu.Game.Screens.Select.Carousel
public readonly Bindable<CarouselItemState> State = new Bindable<CarouselItemState>(CarouselItemState.NotSelected);
protected virtual IEnumerable<CarouselItem> Children { get; set; }
public IReadOnlyList<CarouselItem> Children => InternalChildren;
protected List<CarouselItem> InternalChildren { get; set; }
public bool Visible => State.Value != CarouselItemState.Hidden && !Filtered.Value;
public readonly Lazy<IEnumerable<DrawableCarouselItem>> Drawables;
protected CarouselItem()
public IEnumerable<DrawableCarouselItem> Drawables
{
Drawables = new Lazy<IEnumerable<DrawableCarouselItem>>(() =>
get
{
List<DrawableCarouselItem> items = new List<DrawableCarouselItem>();
var self = CreateDrawableRepresentation();
var self = drawableRepresentation.Value;
if (self != null) items.Add(self);
if (Children != null)
foreach (var c in Children)
items.AddRange(c.Drawables.Value);
if (InternalChildren != null)
foreach (var c in InternalChildren)
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);
protected CarouselItem()
{
drawableRepresentation = new Lazy<DrawableCarouselItem>(CreateDrawableRepresentation);
State.ValueChanged += v =>
{
if (Children == null) return;
if (InternalChildren == null) return;
switch (v)
{
case CarouselItemState.Hidden:
case CarouselItemState.NotSelected:
Children.ForEach(c => c.State.Value = CarouselItemState.Hidden);
InternalChildren.ForEach(c => c.State.Value = CarouselItemState.Hidden);
break;
}
};
}
private readonly Lazy<DrawableCarouselItem> drawableRepresentation;
protected abstract DrawableCarouselItem CreateDrawableRepresentation();
public virtual void Filter(FilterCriteria criteria) => Children?.ForEach(c => c.Filter(criteria));
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