Skip to content

Commit

Permalink
add map sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
flustix committed Jul 19, 2024
1 parent 026c76f commit ae968df
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 15 deletions.
6 changes: 3 additions & 3 deletions fluXis.Game/Screens/Select/List/MapList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ public void Insert(MapListEntry entry)
if (bulkInserting)
return;

sort();
Sort();
}

public void EndBulkInsert()
{
bulkInserting = false;
sort();
Sort();
}

private void sort()
public void Sort()
{
var sorted = Content.Children.ToList();
sorted.Sort((a, b) => a.CompareTo(b));
Expand Down
5 changes: 4 additions & 1 deletion fluXis.Game/Screens/Select/List/MapListEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public partial class MapListEntry : CompositeDrawable, IComparable<MapListEntry>
[Resolved]
private MapStore maps { get; set; }

[Resolved]
private SelectScreen screen { get; set; }

public Action SelectAction;
public Action<RealmMap> EditAction;
public Action<RealmMapSet> ExportAction;
Expand Down Expand Up @@ -105,7 +108,7 @@ protected override void Dispose(bool isDisposing)
base.Dispose(isDisposing);
}

public int CompareTo(MapListEntry other) => MapUtils.CompareSets(MapSet, other.MapSet);
public int CompareTo(MapListEntry other) => MapUtils.CompareSets(MapSet, other.MapSet, screen.SortMode, screen.SortInverse);

private void updateSelected(ValueChangedEvent<RealmMapSet> set)
{
Expand Down
73 changes: 69 additions & 4 deletions fluXis.Game/Screens/Select/SelectScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
using osu.Framework.Allocation;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
Expand Down Expand Up @@ -90,6 +91,9 @@ public partial class SelectScreen : FluXisScreen, IKeyBindingHandler<FluXisGloba
protected MapList MapList { get; private set; }
protected SearchFilters Filters { get; private set; }

public MapUtils.SortingMode SortMode { get; private set; } = MapUtils.SortingMode.Title;
public bool SortInverse { get; private set; } = false;

private BackgroundVideo video;
private Container storyboardContainer;

Expand All @@ -105,8 +109,11 @@ public partial class SelectScreen : FluXisScreen, IKeyBindingHandler<FluXisGloba
private Sample rewindClick;

private SelectNoMaps noMapsContainer;
private LoadingIcon loadingIcon;
private SelectLetter letterContainer;
private LoadingIcon loadingIcon;

private CircularContainer sortModeContainer;
private FluXisSpriteText sortModeText;

private readonly List<RealmMapSet> randomHistory = new();

Expand Down Expand Up @@ -209,6 +216,29 @@ private void load(ISampleStore samples, FluXisConfig config)
{
noMapsContainer = new SelectNoMaps(),
letterContainer = new SelectLetter(),
sortModeContainer = new CircularContainer
{
AutoSizeAxes = Axes.Both,
AutoSizeEasing = Easing.OutQuint,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Masking = true,
Alpha = 0,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Colour4.Black,
Alpha = .5f
},
sortModeText = new FluXisSpriteText
{
Margin = new MarginPadding { Horizontal = 16, Vertical = 8 },
WebFontSize = 20
}
}
},
loadingIcon = new LoadingIcon
{
Anchor = Anchor.Centre,
Expand Down Expand Up @@ -284,6 +314,27 @@ protected override void Dispose(bool isDisposing)

protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) => dependencies = new DependencyContainer(base.CreateChildDependencies(parent));

private void setSortingMode(MapUtils.SortingMode mode)
{
if (mode == SortMode)
SortInverse = !SortInverse;
else // reset when changing modes
SortInverse = false;

SortMode = mode;
Maps.Sort(SortMode, SortInverse);
MapList.Sort();

var str = $"Sorting by {mode.GetDescription()}";

if (SortInverse)
str += " (Inverse)";

sortModeText.Text = str;
sortModeContainer.AutoSizeDuration = sortModeContainer.IsPresent ? 200 : 0;
sortModeContainer.FadeIn().Delay(MOVE_DURATION).FadeOut(FADE_DURATION);
}

private void loadMapSets()
{
var sets = MapStore.MapSets;
Expand All @@ -309,7 +360,7 @@ private void loadMapSets()
Maps.Add(set);
}

Maps.Sort(MapUtils.CompareSets);
Maps.Sort(SortMode, SortInverse);

ScheduleAfterChildren(() => MapList.EndBulkInsert());

Expand All @@ -333,7 +384,7 @@ private void addMapSet(RealmMapSet set)
{
MapList.Insert(entry);
Maps.Add(set);
Maps.Sort(MapUtils.CompareSets);
Maps.Sort(SortMode, SortInverse);
lookup[set] = entry;
noMapsContainer.Hide();
});
Expand Down Expand Up @@ -708,12 +759,26 @@ protected override bool OnKeyDown(KeyDownEvent e)
switch (e.Key)
{
case >= Key.F1 and <= Key.F12:
{
var index = (int)e.Key - (int)Key.F1;

if (index < footer.ButtonContainer.Count)
footer.ButtonContainer[index].TriggerClick();

return true;
}

case >= Key.Number1 and <= Key.Number9:
{
var index = (int)e.Key - (int)Key.Number1;
var values = Enum.GetValues<MapUtils.SortingMode>();

if (index >= values.Length)
break;

setSortingMode(values[index]);
return true;
}

case Key.PageUp:
changeLetter(-1);
Expand Down Expand Up @@ -833,7 +898,7 @@ protected virtual void UpdateSearch()
child.Hide();
}

Maps.Sort(MapUtils.CompareSets);
Maps.Sort(SortMode, SortInverse);

if (!Maps.Any())
noMapsContainer.Show();
Expand Down
60 changes: 53 additions & 7 deletions fluXis.Game/Utils/MapUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
Expand All @@ -12,16 +13,46 @@ namespace fluXis.Game.Utils;

public static class MapUtils
{
public static int CompareSets(RealmMapSet first, RealmMapSet second)
public static void Sort(this List<RealmMapSet> list, SortingMode mode, bool inverse = false)
=> list.Sort((a, b) => CompareSets(a, b, mode, inverse));

public static int CompareSets(RealmMapSet first, RealmMapSet second, SortingMode mode, bool inverse = false)
{
// compare title
var compare = string.Compare(first.Metadata.Title, second.Metadata.Title, StringComparison.OrdinalIgnoreCase);
var result = mode switch
{
SortingMode.Title => compareTitle(first, second),
SortingMode.Artist => compareArtist(first, second),
SortingMode.Length => compareLength(first, second),
SortingMode.DateAdded => second.DateAdded.CompareTo(first.DateAdded),
_ => 0
};

if (inverse)
result = -result;

return result;
}

private static int compareTitle(RealmMapSet first, RealmMapSet second)
{
var result = string.Compare(first.Metadata.Title, second.Metadata.Title, StringComparison.OrdinalIgnoreCase);

// fall back to date added if the title is the same
return result != 0 ? result : CompareSets(first, second, SortingMode.DateAdded);
}

if (compare != 0)
return compare;
private static int compareArtist(RealmMapSet first, RealmMapSet second)
{
var result = string.Compare(first.Metadata.Artist, second.Metadata.Artist, StringComparison.OrdinalIgnoreCase);
return result != 0 ? result : CompareSets(first, second, SortingMode.Title);
}

compare = second.DateAdded.CompareTo(first.DateAdded);
return compare;
private static int compareLength(RealmMapSet first, RealmMapSet second)
{
var firstHighest = first.Maps.MaxBy(x => x.Filters.Length).Filters.Length;
var secondHighest = second.Maps.MaxBy(x => x.Filters.Length).Filters.Length;
var result = firstHighest.CompareTo(secondHighest);
return result != 0 ? result : CompareSets(first, second, SortingMode.Title);
}

public static RealmMapFilters UpdateFilters(this RealmMapFilters filters, MapInfo map, MapEvents events)
Expand Down Expand Up @@ -141,4 +172,19 @@ public static float GetDifficulty(float difficulty, float min, float mid, float
}

private static float getDifficulty(float difficulty) => (difficulty - 5) / 5;

public enum SortingMode
{
[Description("Title")]
Title,

[Description("Artist")]
Artist,

[Description("Length")]
Length,

[Description("Date Added")]
DateAdded
}
}

0 comments on commit ae968df

Please sign in to comment.