Refactor Format to search Context instead

Increase size of left / right buttons to restore << >>
might change them to be icons later
This commit is contained in:
Kurt 2026-02-11 22:21:01 -06:00
parent 20905cbe67
commit 3fa6ab9c23
14 changed files with 116 additions and 71 deletions

View File

@ -11,8 +11,9 @@ namespace PKHeX.Core.Searching;
/// </summary>
public sealed class SearchSettings
{
public byte Format { get; init; }
public EntityContext Context { get; init; }
public byte Generation { get; init; }
public required ushort Species { get; init; }
public string Nickname { get; init; } = string.Empty;
public int Ability { get; init; } = -1;
@ -21,7 +22,7 @@ public sealed class SearchSettings
public GameVersion Version { get; init; }
public int HiddenPowerType { get; init; } = -1;
public SearchComparison SearchFormat { get; init; }
public SearchComparison SearchContext { get; init; }
public SearchComparison SearchLevel { get; init; }
public bool? SearchShiny { get; set; }
@ -161,7 +162,7 @@ private bool IsSearchMatch(PKM pk)
private bool SearchSimple(PKM pk)
{
if (Format > 0 && !SearchUtil.SatisfiesFilterFormat(pk, Format, SearchFormat))
if (SearchContext != SearchComparison.None && Context.IsValid && !SearchUtil.SatisfiesFilterContext(pk, Context, SearchContext))
return false;
if (Species != 0 && pk.Species != Species)
return false;

View File

@ -20,6 +20,24 @@ public static class SearchUtil
_ => true,
};
public static bool SatisfiesFilterContext(PKM pk, EntityContext context, SearchComparison contextOperand) => contextOperand switch
{
SearchComparison.GreaterThanEquals when pk.Context.IsGenerationLessThan(context) => false,
SearchComparison.Equals when pk.Context != context => false,
SearchComparison.LessThanEquals when pk.Context.IsGenerationGreaterThan(context) => false,
_ => contextOperand != SearchComparison.None || CanReachContext(pk, context),
};
private static bool CanReachContext(PKM pk, EntityContext context)
{
var generation = context.Generation;
if (generation <= 2)
return pk.Format <= 2; // 1-2 can reach 1-2
if (generation <= 6)
return pk.Format >= 3; // 3-6 can reach 3-6
return true; // 7+ can reach all contexts
}
public static bool SatisfiesFilterGeneration(PKM pk, byte generation) => generation switch
{
1 => pk.VC || pk.Format < 3,

View File

@ -108,6 +108,12 @@ public static class EntityContextExtensions
/// Determines whether Mega Pokémon forms exist in the specified <see cref="EntityContext"/>.
/// </summary>
public bool IsMegaContext => value is Gen6 or Gen7 or Gen7b or Gen9a;
public int CompareGeneration(EntityContext other) => value.Generation.CompareTo(other.Generation);
public bool IsGenerationLessThan(EntityContext other) => value.Generation < other.Generation;
public bool IsGenerationGreaterThan(EntityContext other) => value.Generation > other.Generation;
public bool IsGenerationLessThanOrEqual(EntityContext other) => value.Generation <= other.Generation;
public bool IsGenerationGreaterThanOrEqual(EntityContext other) => value.Generation >= other.Generation;
}
extension(GameVersion version)

View File

@ -589,7 +589,7 @@ private void InitializeComponent()
TLP_Filters.RowStyles.Add(new System.Windows.Forms.RowStyle());
TLP_Filters.RowStyles.Add(new System.Windows.Forms.RowStyle());
TLP_Filters.RowStyles.Add(new System.Windows.Forms.RowStyle());
TLP_Filters.Size = new System.Drawing.Size(292, 471);
TLP_Filters.Size = new System.Drawing.Size(243, 442);
TLP_Filters.TabIndex = 118;
//
// FLP_Format
@ -656,10 +656,11 @@ private void InitializeComponent()
// EntitySearchControl
//
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
AutoSize = true;
AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
Controls.Add(TLP_Filters);
Name = "EntitySearchControl";
Size = new System.Drawing.Size(292, 471);
Size = new System.Drawing.Size(243, 442);
FLP_Egg.ResumeLayout(false);
FLP_Egg.PerformLayout();
TLP_Filters.ResumeLayout(false);

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using PKHeX.Core;
@ -11,29 +10,8 @@ namespace PKHeX.WinForms.Controls;
public partial class EntitySearchControl : UserControl
{
// don't allow in Designer
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
[Browsable(false)]
public int MaxFormat { private get; set; } = Latest.Generation;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
[Browsable(false)]
public int SaveGeneration { private get; set; } = Latest.Generation;
private EntityContext SaveContext { get; set; } = Latest.Context;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
[Browsable(false)]
public int FormatComparatorSelectedIndex
{
get => CB_FormatComparator.SelectedIndex;
set => CB_FormatComparator.SelectedIndex = value;
}
public EntitySearchControl() => InitializeComponent();
/// <summary>
@ -46,7 +24,7 @@ public int FormatComparatorSelectedIndex
/// <summary>
/// Populates combo box bindings with game data sources.
/// </summary>
public void PopulateComboBoxes()
public void PopulateComboBoxes(FilteredGameDataSource filtered)
{
CB_HeldItem.InitializeBinding();
CB_Species.InitializeBinding();
@ -54,10 +32,10 @@ public void PopulateComboBoxes()
CB_Nature.InitializeBinding();
CB_GameOrigin.InitializeBinding();
CB_HPType.InitializeBinding();
CB_Format.InitializeBinding();
var comboAny = new ComboItem(MsgAny, -1);
var filtered = GameInfo.FilteredSources;
var source = filtered.Source;
var species = new List<ComboItem>(source.SpeciesDataSource)
{
@ -96,14 +74,54 @@ public void PopulateComboBoxes()
cb.DataSource = new BindingSource(moves, string.Empty);
}
var contexts = new List<ComboItem>
{
new(MsgAny, (int)EntityContext.None)
};
foreach (var context in Enum.GetValues<EntityContext>())
{
if (context is EntityContext.None or EntityContext.SplitInvalid or EntityContext.MaxInvalid)
continue;
if (!context.IsValid || !context.ToString().StartsWith("Gen", StringComparison.Ordinal))
continue;
contexts.Add(new ComboItem(context.ToString(), (int)context));
}
CB_Format.DataSource = contexts;
ResetFilters();
}
public void InitializeSelections(SaveFile sav, bool showContext = true)
{
SaveContext = sav.Context;
if (sav.Generation >= 8)
{
CB_FormatComparator.SelectedIndex = 1; // ==
CB_Format.SelectedValue = (int)sav.Context;
}
else
{
CB_FormatComparator.SelectedIndex = 3; // <=
}
L_Format.Visible = CB_FormatComparator.Visible = CB_Format.Visible = showContext;
}
/// <summary>
/// Sets the localized text for the format "Any" option.
/// </summary>
public void SetFormatAnyText(string text)
{
if (CB_Format.DataSource is List<ComboItem> { Count: > 0 } list)
{
list[0] = new ComboItem(text, list[0].Value);
CB_Format.DataSource = null;
CB_Format.DataSource = list;
CB_Format.InitializeBinding();
return;
}
if (CB_Format.Items.Count > 0)
CB_Format.Items[0] = text;
}
@ -152,8 +170,8 @@ public SearchSettings CreateSearchSettings(string batchInstructions)
{
var settings = new SearchSettings
{
Format = (byte)(MaxFormat - CB_Format.SelectedIndex + 1),
SearchFormat = (SearchComparison)CB_FormatComparator.SelectedIndex,
Context = (EntityContext)WinFormsUtil.GetIndex(CB_Format),
SearchContext = (SearchComparison)CB_FormatComparator.SelectedIndex,
Generation = (byte)CB_Generation.SelectedIndex,
Version = (GameVersion)WinFormsUtil.GetIndex(CB_GameOrigin),
@ -229,8 +247,7 @@ private void ChangeFormatFilter(object? sender, EventArgs e)
else
{
CB_Format.Visible = true;
int index = MaxFormat - SaveGeneration + 1;
CB_Format.SelectedIndex = index < CB_Format.Items.Count ? index : 0;
CB_Format.SelectedValue = (int)SaveContext;
}
}
}

View File

@ -40,7 +40,7 @@ private void InitializeComponent()
B_BoxRight.Location = new System.Drawing.Point(200, 0);
B_BoxRight.Margin = new System.Windows.Forms.Padding(0);
B_BoxRight.Name = "B_BoxRight";
B_BoxRight.Size = new System.Drawing.Size(24, 24);
B_BoxRight.Size = new System.Drawing.Size(40, 24);
B_BoxRight.TabIndex = 2;
B_BoxRight.Text = ">>";
B_BoxRight.UseVisualStyleBackColor = true;
@ -49,10 +49,10 @@ private void InitializeComponent()
// B_BoxLeft
//
B_BoxLeft.FlatStyle = System.Windows.Forms.FlatStyle.System;
B_BoxLeft.Location = new System.Drawing.Point(32, 0);
B_BoxLeft.Location = new System.Drawing.Point(16, 0);
B_BoxLeft.Margin = new System.Windows.Forms.Padding(0);
B_BoxLeft.Name = "B_BoxLeft";
B_BoxLeft.Size = new System.Drawing.Size(24, 24);
B_BoxLeft.Size = new System.Drawing.Size(40, 24);
B_BoxLeft.TabIndex = 0;
B_BoxLeft.Text = "<<";
B_BoxLeft.UseVisualStyleBackColor = true;

View File

@ -133,8 +133,10 @@ public void NotifySlotChanged(ISlotInfo slot, SlotTouchType type, PKM pk)
public void ApplyNewFilter(Func<PKM, bool>? filter, bool reload = true)
{
if (filter == _searchFilter)
return;
_searchFilter = filter;
if (reload)
if (reload && SAV.HasBox)
ResetSlots();
}

View File

@ -24,8 +24,10 @@ public PartyEditor()
public void ApplyNewFilter(Func<PKM, bool>? filter, bool reload = true)
{
if (filter == _searchFilter)
return;
_searchFilter = filter;
if (reload)
if (reload && SAV.HasParty)
ResetSlots();
}

View File

@ -78,6 +78,8 @@ public void NotifySlotChanged(ISlotInfo slot, SlotTouchType type, PKM pk)
public void ApplyNewFilter(Func<PKM, bool>? filter, bool reload = true)
{
if (filter == _searchFilter)
return;
_searchFilter = filter;
if (reload)
ResetSlots();

View File

@ -56,7 +56,7 @@ private void InitializeComponent()
TLP_Main.RowCount = 2;
TLP_Main.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
TLP_Main.RowStyles.Add(new System.Windows.Forms.RowStyle());
TLP_Main.Size = new System.Drawing.Size(320, 540);
TLP_Main.Size = new System.Drawing.Size(320, 502);
TLP_Main.TabIndex = 0;
//
// TC_SearchSettings
@ -69,7 +69,7 @@ private void InitializeComponent()
TC_SearchSettings.Name = "TC_SearchSettings";
TC_SearchSettings.Padding = new System.Drawing.Point(0, 0);
TC_SearchSettings.SelectedIndex = 0;
TC_SearchSettings.Size = new System.Drawing.Size(320, 510);
TC_SearchSettings.Size = new System.Drawing.Size(320, 472);
TC_SearchSettings.TabIndex = 2;
//
// Tab_General
@ -78,21 +78,20 @@ private void InitializeComponent()
Tab_General.Location = new System.Drawing.Point(4, 26);
Tab_General.Margin = new System.Windows.Forms.Padding(0);
Tab_General.Name = "Tab_General";
Tab_General.Size = new System.Drawing.Size(312, 480);
Tab_General.Size = new System.Drawing.Size(312, 442);
Tab_General.TabIndex = 0;
Tab_General.Text = "General";
Tab_General.UseVisualStyleBackColor = true;
//
// UC_EntitySearch
//
UC_EntitySearch.AutoSize = true;
UC_EntitySearch.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
UC_EntitySearch.Dock = System.Windows.Forms.DockStyle.Fill;
UC_EntitySearch.Location = new System.Drawing.Point(0, 0);
UC_EntitySearch.Margin = new System.Windows.Forms.Padding(0);
UC_EntitySearch.MaxFormat = 9;
UC_EntitySearch.Name = "UC_EntitySearch";
UC_EntitySearch.SaveGeneration = 9;
UC_EntitySearch.Size = new System.Drawing.Size(312, 480);
UC_EntitySearch.Size = new System.Drawing.Size(312, 442);
UC_EntitySearch.TabIndex = 0;
//
// Tab_Advanced
@ -102,7 +101,7 @@ private void InitializeComponent()
Tab_Advanced.Location = new System.Drawing.Point(4, 26);
Tab_Advanced.Margin = new System.Windows.Forms.Padding(0);
Tab_Advanced.Name = "Tab_Advanced";
Tab_Advanced.Size = new System.Drawing.Size(312, 480);
Tab_Advanced.Size = new System.Drawing.Size(192, 70);
Tab_Advanced.TabIndex = 1;
Tab_Advanced.Text = "Advanced";
Tab_Advanced.UseVisualStyleBackColor = true;
@ -110,7 +109,7 @@ private void InitializeComponent()
// B_Add
//
B_Add.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
B_Add.Location = new System.Drawing.Point(248, 0);
B_Add.Location = new System.Drawing.Point(128, 0);
B_Add.Margin = new System.Windows.Forms.Padding(0);
B_Add.Name = "B_Add";
B_Add.Size = new System.Drawing.Size(66, 27);
@ -125,14 +124,14 @@ private void InitializeComponent()
RTB_Instructions.Location = new System.Drawing.Point(0, 55);
RTB_Instructions.Margin = new System.Windows.Forms.Padding(0);
RTB_Instructions.Name = "RTB_Instructions";
RTB_Instructions.Size = new System.Drawing.Size(312, 425);
RTB_Instructions.Size = new System.Drawing.Size(192, 15);
RTB_Instructions.TabIndex = 0;
RTB_Instructions.Text = "";
//
// B_Search
//
B_Search.Dock = System.Windows.Forms.DockStyle.Fill;
B_Search.Location = new System.Drawing.Point(0, 510);
B_Search.Location = new System.Drawing.Point(0, 472);
B_Search.Margin = new System.Windows.Forms.Padding(0);
B_Search.Name = "B_Search";
B_Search.Size = new System.Drawing.Size(320, 30);
@ -156,7 +155,7 @@ private void InitializeComponent()
// EntitySearchSetup
//
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
ClientSize = new System.Drawing.Size(320, 540);
ClientSize = new System.Drawing.Size(320, 502);
Controls.Add(B_Reset);
Controls.Add(TLP_Main);
FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
@ -170,6 +169,7 @@ private void InitializeComponent()
TLP_Main.ResumeLayout(false);
TC_SearchSettings.ResumeLayout(false);
Tab_General.ResumeLayout(false);
Tab_General.PerformLayout();
Tab_Advanced.ResumeLayout(false);
ResumeLayout(false);
}

View File

@ -37,11 +37,9 @@ public void Initialize(SaveFile sav, IPKMView edit)
{
ArgumentNullException.ThrowIfNull(sav);
UC_EntitySearch.MaxFormat = Latest.Generation;
UC_EntitySearch.SaveGeneration = sav.Generation;
UC_EntitySearch.PopulateComboBoxes();
UC_EntitySearch.PopulateComboBoxes(GameInfo.FilteredSources);
UC_EntitySearch.SetFormatAnyText(MsgAny);
UC_EntitySearch.FormatComparatorSelectedIndex = 3; // <=
UC_EntitySearch.InitializeSelections(sav, showContext: false);
CurrentSave = sav;
EnsureBuilder(edit);
}

View File

@ -69,9 +69,7 @@ public SAV_Database(PKMEditor f1, SAVEditor saveditor)
PKME_Tabs = f1;
// Preset Filters to only show PKM available for loaded save
UC_EntitySearch.MaxFormat = MAXFORMAT;
UC_EntitySearch.SaveGeneration = SAV.Generation;
UC_EntitySearch.FormatComparatorSelectedIndex = 3; // <=
UC_EntitySearch.InitializeSelections(SAV);
var grid = DatabasePokeGrid;
var smallWidth = grid.Width;
@ -130,7 +128,7 @@ public SAV_Database(PKMEditor f1, SAVEditor saveditor)
Counter = L_Count.Text;
Viewed = L_Viewed.Text;
L_Viewed.Text = string.Empty; // invisible for now
UC_EntitySearch.PopulateComboBoxes();
UC_EntitySearch.PopulateComboBoxes(GameInfo.FilteredSources);
// Load Data
B_Search.Enabled = false;

View File

@ -424,7 +424,7 @@ private SearchSettings GetSearchSettings()
{
var settings = new SearchSettings
{
Format = SAV.Generation, // 0->(n-1) => 1->n
Context = SAV.Context,
Generation = SAV.Generation,
Species = GetU16(CB_Species),

View File

@ -34,18 +34,18 @@ private void InitializeComponent()
CB_BoxSelect = new System.Windows.Forms.ComboBox();
mnu = new System.Windows.Forms.ContextMenuStrip(components);
mnuView = new System.Windows.Forms.ToolStripMenuItem();
Box = new Controls.PokeGrid();
Box = new PKHeX.WinForms.Controls.PokeGrid();
mnu.SuspendLayout();
SuspendLayout();
//
// B_BoxRight
//
B_BoxRight.FlatStyle = System.Windows.Forms.FlatStyle.System;
B_BoxRight.Location = new System.Drawing.Point(240, 3);
B_BoxRight.Location = new System.Drawing.Point(243, 3);
B_BoxRight.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
B_BoxRight.Name = "B_BoxRight";
B_BoxRight.Size = new System.Drawing.Size(31, 27);
B_BoxRight.TabIndex = 68;
B_BoxRight.Size = new System.Drawing.Size(40, 24);
B_BoxRight.TabIndex = 3;
B_BoxRight.Text = ">>";
B_BoxRight.UseVisualStyleBackColor = true;
B_BoxRight.Click += B_BoxRight_Click;
@ -53,11 +53,11 @@ private void InitializeComponent()
// B_BoxLeft
//
B_BoxLeft.FlatStyle = System.Windows.Forms.FlatStyle.System;
B_BoxLeft.Location = new System.Drawing.Point(19, 3);
B_BoxLeft.Location = new System.Drawing.Point(10, 3);
B_BoxLeft.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
B_BoxLeft.Name = "B_BoxLeft";
B_BoxLeft.Size = new System.Drawing.Size(31, 27);
B_BoxLeft.TabIndex = 67;
B_BoxLeft.Size = new System.Drawing.Size(40, 24);
B_BoxLeft.TabIndex = 2;
B_BoxLeft.Text = "<<";
B_BoxLeft.UseVisualStyleBackColor = true;
B_BoxLeft.Click += B_BoxLeft_Click;
@ -67,24 +67,24 @@ private void InitializeComponent()
CB_BoxSelect.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
CB_BoxSelect.DropDownWidth = 252;
CB_BoxSelect.FormattingEnabled = true;
CB_BoxSelect.Location = new System.Drawing.Point(56, 5);
CB_BoxSelect.Location = new System.Drawing.Point(58, 5);
CB_BoxSelect.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
CB_BoxSelect.Name = "CB_BoxSelect";
CB_BoxSelect.Size = new System.Drawing.Size(177, 23);
CB_BoxSelect.TabIndex = 66;
CB_BoxSelect.Size = new System.Drawing.Size(177, 25);
CB_BoxSelect.TabIndex = 1;
CB_BoxSelect.SelectedIndexChanged += CB_BoxSelect_SelectedIndexChanged;
//
// mnu
//
mnu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { mnuView });
mnu.Name = "mnu";
mnu.Size = new System.Drawing.Size(100, 26);
mnu.Size = new System.Drawing.Size(104, 26);
//
// mnuView
//
mnuView.Image = Properties.Resources.other;
mnuView.Name = "mnuView";
mnuView.Size = new System.Drawing.Size(99, 22);
mnuView.Size = new System.Drawing.Size(103, 22);
mnuView.Text = "View";
mnuView.Click += ClickView;
//