From a939a2dbd624f00009252276782fbb5c38dd89d2 Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 6 Aug 2021 10:09:16 -0700 Subject: [PATCH] Add pkmdb sorting rules for loaded database set --- .../Editing/Saves/Slots/Info/SlotCache.cs | 37 ++++++++++++++++++- PKHeX.WinForms/Properties/PKHeXSettings.cs | 10 +++++ PKHeX.WinForms/Subforms/SAV_Database.cs | 6 +++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/PKHeX.Core/Editing/Saves/Slots/Info/SlotCache.cs b/PKHeX.Core/Editing/Saves/Slots/Info/SlotCache.cs index 7767be25f..cc784bb69 100644 --- a/PKHeX.Core/Editing/Saves/Slots/Info/SlotCache.cs +++ b/PKHeX.Core/Editing/Saves/Slots/Info/SlotCache.cs @@ -5,7 +5,7 @@ namespace PKHeX.Core /// /// Contains slot data and metadata indicating where the originated from. /// - public class SlotCache + public class SlotCache : IComparable { /// /// Information regarding how the was obtained. @@ -60,5 +60,40 @@ namespace PKHeX.Core var e = Entity; return e.Species != 0 && e.ChecksumValid && (e.Sanity == 0 || e is BK4); } + + public int CompareTo(SlotCache? other) + { + if (other is null) + return -1; + return string.CompareOrdinal(Identify(), other.Identify()); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(this, obj)) + return true; + return obj is SlotCache c && c.Identify() == Identify(); + } + + public override int GetHashCode() => Identify().GetHashCode(); + public static bool operator ==(SlotCache left, SlotCache right) => left.Equals(right); + public static bool operator !=(SlotCache left, SlotCache right) => !(left == right); + public static bool operator <(SlotCache left, SlotCache right) => left.CompareTo(right) < 0; + public static bool operator <=(SlotCache left, SlotCache right) => left.CompareTo(right) <= 0; + public static bool operator >(SlotCache left, SlotCache right) => left.CompareTo(right) > 0; + public static bool operator >=(SlotCache left, SlotCache right) => left.CompareTo(right) >= 0; + + public int CompareToSpeciesForm(SlotCache other) + { + var s1 = Entity; + var s2 = other.Entity; + var c1 = s1.Species.CompareTo(s2.Species); + if (c1 != 0) + return c1; + var c2 = s1.Form.CompareTo(s2.Form); + if (c2 != 0) + return c2; + return CompareTo(other); + } } } diff --git a/PKHeX.WinForms/Properties/PKHeXSettings.cs b/PKHeX.WinForms/Properties/PKHeXSettings.cs index 2bb7220a1..256a83b7c 100644 --- a/PKHeX.WinForms/Properties/PKHeXSettings.cs +++ b/PKHeX.WinForms/Properties/PKHeXSettings.cs @@ -229,10 +229,20 @@ namespace PKHeX.WinForms [LocalizedDescription("When loading content for the PKM Database, search subfolders within OtherBackupPaths.")] public bool SearchExtraSavesDeep { get; set; } = true; + [LocalizedDescription("When loading content for the PKM database, the list will be ordered by this option.")] + public DatabaseSortMode InitialSortMode { get; set; } + [LocalizedDescription("Hides unavailable Species if the currently loaded save file cannot import them.")] public bool FilterUnavailableSpecies { get; set; } = true; } + public enum DatabaseSortMode + { + None, + SpeciesForm, + SlotIdentity, + } + [Serializable] public class EncounterDatabaseSettings { diff --git a/PKHeX.WinForms/Subforms/SAV_Database.cs b/PKHeX.WinForms/Subforms/SAV_Database.cs index b1d47435b..500f2e901 100644 --- a/PKHeX.WinForms/Subforms/SAV_Database.cs +++ b/PKHeX.WinForms/Subforms/SAV_Database.cs @@ -369,6 +369,12 @@ namespace PKHeX.WinForms result.RemoveAll(z => !(z.Entity is PK8 || ((PersonalInfoSWSH) PersonalTable.SWSH.GetFormEntry(z.Entity.Species, z.Entity.Form)).IsPresentInGame)); } + var sort = Main.Settings.EntityDb.InitialSortMode; + if (sort is DatabaseSortMode.SlotIdentity) + result.Sort(); + else if (sort is DatabaseSortMode.SpeciesForm) + result.Sort((first, second) => first.CompareToSpeciesForm(second)); + // Finalize the Database return result; }