diff --git a/PKHeX.Core/PKM/Searching/SearchUtil.cs b/PKHeX.Core/PKM/Searching/SearchUtil.cs index bd732605d..98d4b7826 100644 --- a/PKHeX.Core/PKM/Searching/SearchUtil.cs +++ b/PKHeX.Core/PKM/Searching/SearchUtil.cs @@ -155,29 +155,39 @@ public static bool SatisfiesFilterNickname(PKM pk, ReadOnlySpan nicknameSu return name.Contains(nicknameSubstring, StringComparison.OrdinalIgnoreCase); } - public static bool TrySeekNext(SaveFile sav, Func searchFilter, out (int Box, int Slot) result, int current = -1) + public static bool TrySeekNext(SaveFile sav, Func 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. diff --git a/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs index 292e9479a..860065eaf 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs @@ -355,18 +355,22 @@ public bool InitializeFromSAV(SaveFile sav) private Func? _searchFilter; - public void ApplySearchFilter(Func? searchFilter, bool isInit = false) + public void ApplySearchFilter(Func? searchFilter, bool reload = true) { _searchFilter = searchFilter; - if (isInit) + _lastSearchResult = null; + if (!reload) return; ResetSlots(); } - public void SeekNext(Func searchFilter) + private (int Box, int Slot)? _lastSearchResult; + + public void SeekNext(Func 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 searchFilter) } CurrentBox = result.Box; BoxPokeGrid.Entries[result.Slot].Focus(); + _lastSearchResult = result; + System.Media.SystemSounds.Asterisk.Play(); } } diff --git a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs index 8d5cccb5b..710c2f624 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs @@ -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? filter, bool reload = true) => Box.ApplySearchFilter(filter, reload);