PKHeX/PKHeX.Core/Editing/PKM/LegalMoveComboSource.cs
Kurt 3c232505e5
Refactoring: Narrow some value types (Species, Move, Form) (#3575)
In this pull request I've changed a ton of method signatures to reflect the more-narrow types of Species, Move# and Form; additionally, I've narrowed other large collections that stored lists of species / permitted values, and reworked them to be more performant with the latest API spaghetti that PKHeX provides. Roamer met locations, usually in a range of [max-min]<64, can be quickly checked using a bitflag operation on a UInt64. Other collections (like "Is this from Colosseum or XD") were eliminated -- shadow state is not transferred COLO<->XD, so having a Shadow ID or matching the met location from a gift/wild encounter is a sufficient check for "originated in XD".
2022-08-26 23:43:36 -07:00

51 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core;
/// <summary>
/// Tracks the state of displaying selectable movesets, and indicates if they are ordered.
/// </summary>
public sealed class LegalMoveComboSource : ILegalMoveDisplaySource<ComboItem>
{
private readonly bool[] IsMoveBoxOrdered = new bool[4];
private ComboItem[] MoveDataAllowed = Array.Empty<ComboItem>();
public IReadOnlyList<ComboItem> DataSource => (ComboItem[])MoveDataAllowed.Clone();
/// <summary>
/// Resets the <see cref="MoveDataAllowed"/> data source with an updated collection.
/// </summary>
public void ReloadMoves(IReadOnlyList<ComboItem> moves)
{
MoveDataAllowed = moves.ToArray();
ClearUpdateCheck();
}
public bool GetIsMoveBoxOrdered(int index) => IsMoveBoxOrdered[index];
public void SetIsMoveBoxOrdered(int index, bool value) => IsMoveBoxOrdered[index] = value;
public void ReloadMoves(LegalMoveInfo info)
{
ClearUpdateCheck();
SortMoves(info);
}
private void SortMoves(LegalMoveInfo info) => Array.Sort(MoveDataAllowed, (i1, i2) => Compare(i1, i2, info.CanLearn));
// defer re-population until dropdown is opened; handled by dropdown event
private void ClearUpdateCheck() => Array.Clear(IsMoveBoxOrdered, 0, IsMoveBoxOrdered.Length);
private static int Compare(ComboItem i1, ComboItem i2, Func<ushort, bool> check)
{
// split into 2 groups: Allowed & Not, and sort each sublist
var (strA, value1) = i1;
var (strB, value2) = i2;
var c1 = check((ushort)value1);
var c2 = check((ushort)value2);
if (c1)
return c2 ? string.CompareOrdinal(strA, strB) : -1;
return c2 ? 1 : string.CompareOrdinal(strA, strB);
}
}