1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 16:57:25 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs

114 lines
4.2 KiB
C#
Raw Normal View History

// 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
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 override float TotalHeight
{
get
{
switch (State.Value)
{
case CarouselItemState.Selected:
return DrawableCarouselBeatmapSet.HEIGHT + Children.Count(c => c.Visible) * DrawableCarouselBeatmap.HEIGHT;
default:
return DrawableCarouselBeatmapSet.HEIGHT;
}
}
}
2018-04-13 17:19:50 +08:00
public IEnumerable<CarouselBeatmap> Beatmaps => InternalChildren.OfType<CarouselBeatmap>();
public BeatmapSetInfo BeatmapSet;
public Func<IEnumerable<BeatmapInfo>, BeatmapInfo> GetRecommendedBeatmap;
public CarouselBeatmapSet(BeatmapSetInfo beatmapSet)
2018-04-13 17:19:50 +08:00
{
BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet));
beatmapSet.Beatmaps
.Where(b => !b.Hidden)
.Select(b => new CarouselBeatmap(b))
.ForEach(AddChild);
}
2020-03-26 06:19:54 +08:00
protected override CarouselItem GetNextToSelect()
{
if (LastSelected == null || LastSelected.Filtered.Value)
2020-03-26 06:19:54 +08:00
{
if (GetRecommendedBeatmap?.Invoke(Children.OfType<CarouselBeatmap>().Where(b => !b.Filtered.Value).Select(b => b.Beatmap)) is BeatmapInfo recommended)
return Children.OfType<CarouselBeatmap>().First(b => b.Beatmap == recommended);
2020-03-26 06:19:54 +08:00
}
return base.GetNextToSelect();
}
2018-04-13 17:19:50 +08:00
public override int CompareTo(FilterCriteria criteria, CarouselItem other)
{
if (!(other is CarouselBeatmapSet otherSet))
return base.CompareTo(criteria, other);
switch (criteria.Sort)
{
default:
case SortMode.Artist:
2019-11-28 22:39:09 +08:00
return string.Compare(BeatmapSet.Metadata.Artist, otherSet.BeatmapSet.Metadata.Artist, StringComparison.OrdinalIgnoreCase);
2019-04-01 11:44:46 +08:00
2018-04-13 17:19:50 +08:00
case SortMode.Title:
2019-11-28 22:39:09 +08:00
return string.Compare(BeatmapSet.Metadata.Title, otherSet.BeatmapSet.Metadata.Title, StringComparison.OrdinalIgnoreCase);
2019-04-01 11:44:46 +08:00
2018-04-13 17:19:50 +08:00
case SortMode.Author:
2019-11-28 22:39:09 +08:00
return string.Compare(BeatmapSet.Metadata.Author.Username, otherSet.BeatmapSet.Metadata.Author.Username, StringComparison.OrdinalIgnoreCase);
2019-04-01 11:44:46 +08:00
case SortMode.DateAdded:
return otherSet.BeatmapSet.DateAdded.CompareTo(BeatmapSet.DateAdded);
2019-07-07 23:14:23 +08:00
case SortMode.BPM:
return compareUsingAggregateMax(otherSet, b => b.BPM);
2019-07-07 23:14:23 +08:00
2019-07-07 23:26:56 +08:00
case SortMode.Length:
return compareUsingAggregateMax(otherSet, b => b.Length);
2019-07-07 23:26:56 +08:00
2018-04-13 17:19:50 +08:00
case SortMode.Difficulty:
return compareUsingAggregateMax(otherSet, b => b.StarDifficulty);
2018-04-13 17:19:50 +08:00
}
}
/// <summary>
/// All beatmaps which are not filtered and valid for display.
/// </summary>
protected IEnumerable<BeatmapInfo> ValidBeatmaps => Beatmaps.Where(b => !b.Filtered.Value || b.State.Value == CarouselItemState.Selected).Select(b => b.Beatmap);
private int compareUsingAggregateMax(CarouselBeatmapSet other, Func<BeatmapInfo, double> func)
{
var ourBeatmaps = ValidBeatmaps.Any();
var otherBeatmaps = other.ValidBeatmaps.Any();
if (!ourBeatmaps && !otherBeatmaps) return 0;
if (!ourBeatmaps) return -1;
if (!otherBeatmaps) return 1;
return ValidBeatmaps.Max(func).CompareTo(other.ValidBeatmaps.Max(func));
}
2018-04-13 17:19:50 +08:00
public override void Filter(FilterCriteria criteria)
{
base.Filter(criteria);
2019-02-21 17:56:34 +08:00
Filtered.Value = InternalChildren.All(i => i.Filtered.Value);
2018-04-13 17:19:50 +08:00
}
public override string ToString() => BeatmapSet.ToString();
}
}