From 7af65a468312a1f03c069e1c81d9e4578b56479c Mon Sep 17 00:00:00 2001 From: javierhimura Date: Sun, 19 Mar 2023 20:33:12 +0100 Subject: [PATCH] Filter Block Dump data (#3840) * Allow the filter results in BlockDump combobox. Write a text in the dropdown --- .../Save Editors/Gen8/SAV_BlockDump8.cs | 49 +++++++++++++++++-- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs index 9564939ab..e6f1bc781 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_BlockDump8.cs @@ -13,8 +13,10 @@ public partial class SAV_BlockDump8 : Form { private readonly ISCBlockArray SAV; private readonly SCBlockMetadata Metadata; + private readonly ComboItem[] SortedBlockKeys; private SCBlock CurrentBlock = null!; + private string Filter = string.Empty; public SAV_BlockDump8(ISCBlockArray sav) { @@ -29,7 +31,8 @@ public partial class SAV_BlockDump8 : Form Metadata = new SCBlockMetadata(SAV.Accessor, extra, Main.Settings.Advanced.GetExclusionList8()); CB_Key.InitializeBinding(); - CB_Key.DataSource = Metadata.GetSortedBlockKeyList().ToArray(); + SortedBlockKeys = Metadata.GetSortedBlockKeyList().ToArray(); + CB_Key.DataSource = SortedBlockKeys; CB_TypeToggle.InitializeBinding(); CB_TypeToggle.DataSource = new[] @@ -38,6 +41,7 @@ public partial class SAV_BlockDump8 : Form new ComboItem(nameof(SCTypeCode.Bool2), (int)SCTypeCode.Bool2), }; CB_TypeToggle.SelectedIndexChanged += CB_TypeToggle_SelectedIndexChanged; + CB_Key.KeyDown += WinFormsUtil.RemoveDropCB; CB_Key.SelectedIndex = 0; } @@ -267,10 +271,45 @@ public partial class SAV_BlockDump8 : Form if (e.KeyCode != Keys.Enter) return; - var text = CB_Key.Text; - if (text.Length != 8) - return; + var text = CB_Key.Text.Trim(); + if (text.Length == 8) + { + var hex = (int)Util.GetHexValue(text); + if (hex != 0) + { + if (!string.IsNullOrEmpty(Filter)) + { + // Clear the filter + CB_Key.DataSource = SortedBlockKeys; + Filter = string.Empty; + } + // Input is hexadecimal number, select the item + CB_Key.SelectedValue = hex; + return; + } + } - CB_Key.SelectedValue = (int)Util.GetHexValue(text); + if (CB_Key.SelectedItem != null && text.Equals(CB_Key.SelectedText)) + return; // User press enter on selected item + + if (Filter.Equals(text, StringComparison.InvariantCultureIgnoreCase)) + return; // Filter hasn't changed + + Filter = text; + if (string.IsNullOrEmpty(text)) + { + // User has cleared the filter. Restore original metadata + CB_Key.DataSource = SortedBlockKeys; + CB_Key.SelectedIndex = 0; + return; + } + + // Filter combo items that contains input text + var filtered = Array.FindAll(SortedBlockKeys, x => x.Text.Contains(text, StringComparison.InvariantCultureIgnoreCase)); + if (filtered.Length == 0) + return; // no results + + CB_Key.DataSource = filtered; + CB_Key.SelectedIndex = 0; } }