Add reverse search, remember result

ctrl+shift => reverse
shift => forwards
This commit is contained in:
Kurt 2026-02-11 22:59:33 -06:00
parent 3fa6ab9c23
commit 4ecd51e826
3 changed files with 41 additions and 25 deletions

View File

@ -155,29 +155,39 @@ public static bool SatisfiesFilterNickname(PKM pk, ReadOnlySpan<char> nicknameSu
return name.Contains(nicknameSubstring, StringComparison.OrdinalIgnoreCase);
}
public static bool TrySeekNext(SaveFile sav, Func<PKM, bool> searchFilter, out (int Box, int Slot) result, int current = -1)
public static bool TrySeekNext(SaveFile sav, Func<PKM, bool> searchFilter, out (int Box, int Slot) result, int currentBox = -1, int currentSlot = -1, bool reverse = false)
{
// Search from next box, wrapping around
var boxCount = sav.BoxCount;
var boxSlotCount = sav.BoxSlotCount;
var startBox = (current + 1) % boxCount;
for (int i = 0; i < boxCount; i++)
// Search from next slot, wrapping around
if (currentBox == -1)
currentBox = 0;
var step = reverse ? -1 : 1;
if (currentSlot == -1)
currentSlot = 0;
else
currentSlot += step;
var totalSlots = sav.SlotCount;
var index = currentBox * sav.BoxSlotCount + currentSlot;
if (index < 0)
index = totalSlots - 1;
else if (index >= totalSlots)
index = 0;
for (var i = 0; i < totalSlots; i++)
{
var box = (startBox + i) % boxCount;
var actualIndex = (index + i * step + totalSlots) % totalSlots;
var pk = sav.GetBoxSlotAtIndex(actualIndex);
if (pk.Species == 0)
continue;
for (int slot = 0; slot < boxSlotCount; slot++)
{
var pk = sav.GetBoxSlotAtIndex(box, slot);
if (pk.Species == 0)
continue;
if (!searchFilter(pk))
continue;
if (!searchFilter(pk))
continue;
// Match found. Seek to the box, and Focus on the slot.
result = (box, slot);
return true;
}
// Match found. Seek to the box, and Focus on the slot.
sav.GetBoxSlotFromIndex(actualIndex, out var box, out var slot);
result = (box, slot);
return true;
}
// None found.

View File

@ -355,18 +355,22 @@ public bool InitializeFromSAV(SaveFile sav)
private Func<PKM, bool>? _searchFilter;
public void ApplySearchFilter(Func<PKM, bool>? searchFilter, bool isInit = false)
public void ApplySearchFilter(Func<PKM, bool>? searchFilter, bool reload = true)
{
_searchFilter = searchFilter;
if (isInit)
_lastSearchResult = null;
if (!reload)
return;
ResetSlots();
}
public void SeekNext(Func<PKM, bool> searchFilter)
private (int Box, int Slot)? _lastSearchResult;
public void SeekNext(Func<PKM, bool> searchFilter, bool reverse = false)
{
// Search from next box, wrapping around
if (!SearchUtil.TrySeekNext(SAV, searchFilter, out var result, CurrentBox))
var (box, slot) = _lastSearchResult ?? (CurrentBox, -1);
if (!SearchUtil.TrySeekNext(SAV, searchFilter, out var result, box, slot, reverse))
{
// Not found
System.Media.SystemSounds.Exclamation.Play();
@ -374,6 +378,8 @@ public void SeekNext(Func<PKM, bool> searchFilter)
}
CurrentBox = result.Box;
BoxPokeGrid.Entries[result.Slot].Focus();
_lastSearchResult = result;
System.Media.SystemSounds.Asterisk.Play();
}
}

View File

@ -1565,7 +1565,7 @@ private void B_SearchBox_Click(object sender, EventArgs e)
_searchForm.Hide();
return;
}
if (ModifierKeys == Keys.Shift)
if (ModifierKeys.HasFlag(Keys.Shift))
{
BoxSearchSeek();
return;
@ -1632,7 +1632,7 @@ private void BoxSearchSeek()
{
if (_searchFilter is null)
return;
Box.SeekNext(_searchFilter);
Box.SeekNext(_searchFilter, reverse: ModifierKeys.HasFlag(Keys.Control));
}
public void ApplyNewFilter(Func<PKM, bool>? filter, bool reload = true) => Box.ApplySearchFilter(filter, reload);