PKHeX/PKHeX.WinForms/Controls/PKM Editor/GenderToggle.cs
Kurt 95fbf66a6e
Refactor: Gen3/4 Lead Encounters, property fixing (#4193)
In addition to the Method 1 (and other sibling PIDIV types) correlation, an encounter can only be triggered if the calls prior land on the Method {1} seed. The RNG community has dubbed these patterns as "Method J" (D/P/Pt), "Method K" (HG/SS), and "Method H" (Gen3, coined by yours truly). The basic gist of these is that they are pre-requisites, like the Shadow locks of Colosseum/XD. 

Rename/re-type a bunch of properties to get the codebase more in line with correct property names & more obvious underlying types.
2024-02-22 21:20:54 -06:00

128 lines
3.5 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Automation;
using static PKHeX.WinForms.Properties.Resources;
namespace PKHeX.WinForms.Controls;
public partial class GenderToggle : UserControl, IGenderToggle
{
public bool AllowClick { get; set; } = true;
private int Value = -1; // Initial load will trigger gender to appear (-1 => 0)
private string? InitialAccessible;
public static int FocusBorderDeflate { get; set; }
public byte Gender
{
get => (byte)Value;
set => Value = SetGender(value);
}
public GenderToggle() => InitializeComponent();
protected override void OnMouseDown(MouseEventArgs e)
{
Focus();
base.OnMouseDown(e);
}
protected override void OnEnter(EventArgs e)
{
Invalidate();
base.OnEnter(e);
AccessibilityObject.RaiseAutomationNotification(AutomationNotificationKind.Other,
AutomationNotificationProcessing.All, AccessibleDescription ?? AccessibleName ?? "");
}
protected override void OnLeave(EventArgs e)
{
Invalidate();
base.OnLeave(e);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (!Focused)
return;
var rc = ClientRectangle;
rc.Inflate(-FocusBorderDeflate, -FocusBorderDeflate);
ControlPaint.DrawFocusRectangle(pe.Graphics, rc);
}
private static readonly Image[] GenderImages =
[
gender_0,
gender_1,
gender_2,
];
private int SetGender(int value)
{
if ((uint)value > 2)
value = 2;
if (Value == value)
return value;
BackgroundImage = GenderImages[value];
AccessibleName = (InitialAccessible ??= AccessibleName) + $" ({value})";
AccessibleDescription = (InitialAccessible ??= AccessibleName) + $" ({value})";
return value;
}
private void GenderToggle_Click(object sender, EventArgs e)
{
if (!AllowClick)
return;
TryToggle();
}
private void GenderToggle_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode is not (Keys.Enter or Keys.Space))
return;
TryToggle();
}
private void TryToggle()
{
if (AllowClick && ToggleGender().CanToggle)
{
AccessibilityObject.RaiseAutomationNotification(AutomationNotificationKind.Other,
AutomationNotificationProcessing.All, $"Gender changed to {Gender}.");
return;
}
AccessibilityObject.RaiseAutomationNotification(AutomationNotificationKind.Other,
AutomationNotificationProcessing.All, $"Cannot change gender. Current value is {Gender}.");
}
public (bool CanToggle, int Value) ToggleGender()
{
if (CanToggle())
return (true, Gender ^= 1);
return (false, Gender);
}
public bool CanToggle() => (uint)Gender < 2;
}
public interface IGenderToggle
{
/// <summary>
/// Enables use of the built-in click action.
/// </summary>
bool AllowClick { get; set; }
/// <summary>
/// Get or set the value the control displays.
/// </summary>
byte Gender { get; set; }
/// <summary>
/// Manually flips the gender state if possible.
/// </summary>
/// <returns>True if the gender was toggled, and the current Gender value after the operation.</returns>
(bool CanToggle, int Value) ToggleGender();
}