PKHeX/PKHeX.WinForms/Subforms/EntitySearchSetup.cs
Kurt 244b34b8d3 Misc translatable util update
Allow EntitySearchSetup to be translated (rearrange the initialization, no need to retain/defer).
Rename Gen9a's gender label (previously blacklisted as an "auto-updating" label).

Other gender/Stat labels currently blacklisted in DevUtil probably need to get refactored to be enums/etc, but I currently lack the time/patience to understand those editors well enough to properly support the refactoring needed.

json exports: add newline at end to match the default editorconfig settings and general convention. we don't want textfiles loading in as a string[] with an empty string last entry.
2026-03-05 21:57:08 -06:00

154 lines
4.4 KiB
C#

using System;
using System.Windows.Forms;
using PKHeX.Core;
using PKHeX.WinForms.Controls;
using static PKHeX.Core.MessageStrings;
namespace PKHeX.WinForms;
public partial class EntitySearchSetup : Form
{
private EntityInstructionBuilder? UC_Builder;
private SaveFile CurrentSave;
public Func<PKM, bool>? SearchFilter { get; private set; }
public EntitySearchSetup(IPKMView edit, SaveFile sav)
{
CurrentSave = sav;
InitializeComponent();
Initialize(sav);
EnsureBuilder(edit);
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
}
/// <summary>
/// Occurs when the Search action is requested.
/// </summary>
public event EventHandler? SearchRequested;
/// <summary>
/// Occurs when the Reset action is requested.
/// </summary>
public event EventHandler? ResetRequested;
/// <summary>
/// Occurs when the next item in a sequence is sought.
/// </summary>
public event EventHandler? SeekNext;
/// <summary>
/// Occurs when the Seek Previous action is requested.
/// </summary>
public event EventHandler? SeekPrevious;
/// <summary>
/// Initializes the search setup controls using the provided save file.
/// </summary>
/// <param name="sav">Save file used to configure search settings.</param>
private void Initialize(SaveFile sav)
{
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
UC_EntitySearch.PopulateComboBoxes(GameInfo.FilteredSources);
UC_EntitySearch.SetFormatAnyText(MsgAny);
UC_EntitySearch.InitializeSelections(sav, showContext: false);
CurrentSave = sav;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
UC_EntitySearch.ResetComboBoxSelections();
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (RTB_Instructions.Focused)
return;
B_Search_Click(this, EventArgs.Empty);
e.Handled = true;
}
// Quick close with Ctrl+W
if (e.KeyCode == Keys.W && ModifierKeys == Keys.Control)
Hide();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
return;
}
CurrentSave = null;
SearchFilter = null;
base.OnFormClosing(e);
}
private void EnsureBuilder(IPKMView edit)
{
if (UC_Builder is not null)
return;
UC_Builder = new EntityInstructionBuilder(() => edit.PreparePKM())
{
Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right,
Dock = DockStyle.Top,
ReadOnly = true,
};
Tab_Advanced.Controls.Add(UC_Builder);
UC_Builder.SendToBack();
}
private void B_Search_Click(object? sender, EventArgs e)
{
SearchFilter = UC_EntitySearch.GetFilter(RTB_Instructions.Text);
SearchRequested?.Invoke(this, EventArgs.Empty);
B_Next.Visible = B_Previous.Visible = true;
System.Media.SystemSounds.Asterisk.Play();
}
private void B_Reset_Click(object? sender, EventArgs e)
{
ForceReset();
System.Media.SystemSounds.Asterisk.Play();
}
private void B_Add_Click(object? sender, EventArgs e)
{
if (UC_Builder is null)
return;
var s = UC_Builder.Create();
if (s.Length == 0)
{
WinFormsUtil.Alert(MsgBEPropertyInvalid);
return;
}
var tb = RTB_Instructions;
var batchText = tb.Text;
if (batchText.Length != 0 && !batchText.EndsWith('\n'))
tb.AppendText(Environment.NewLine);
tb.AppendText(s);
}
public bool IsSameSaveFile(SaveFile sav) => CurrentSave is not null && CurrentSave == sav;
public void ForceReset()
{
SearchFilter = null;
UC_EntitySearch.ResetFilters();
RTB_Instructions.Clear();
B_Next.Visible = B_Previous.Visible = false;
ResetRequested?.Invoke(this, EventArgs.Empty);
}
private void B_Next_Click(object sender, EventArgs e) => SeekNext?.Invoke(this, EventArgs.Empty);
private void B_Previous_Click(object sender, EventArgs e) => SeekPrevious?.Invoke(this, EventArgs.Empty);
}