PKHeX/PKHeX.WinForms/Controls/Slots/SlotUtil.cs
Kurt 0e097b1fc6 Minor slot hover performance improvements
Skip repaint on cursor moving the hover window
Cache reference to the slot interaction types and "nothing" slot image
Dispose of slot sprites when updating with a new one
If scrolling box/group, auto-update hover with the newly displayed slot's content instead of hiding
2026-03-19 17:25:06 -05:00

83 lines
2.9 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
using PKHeX.Core;
using PKHeX.Drawing.PokeSprite;
namespace PKHeX.WinForms.Controls;
/// <summary>
/// Utility logic for drawing individual Slot views that represent underlying <see cref="PKM"/> data.
/// </summary>
public static class SlotUtil
{
/// <summary>
/// Gets the background image for a slot based on the provided <see cref="type"/>.
/// </summary>
public static Bitmap? GetTouchTypeBackground(SlotTouchType type) => type switch
{
SlotTouchType.None => null,
SlotTouchType.Get => SpriteUtil.Spriter.View,
SlotTouchType.Set => SpriteUtil.Spriter.Set,
SlotTouchType.Delete => SpriteUtil.Spriter.Delete,
SlotTouchType.Swap => SpriteUtil.Spriter.Set,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null),
};
/// <summary>
/// Gets the type of action that should be performed for a Drag &amp; Drop request.
/// </summary>
public static DropModifier GetDropModifier() => Control.ModifierKeys switch
{
Keys.Shift => DropModifier.Clone,
Keys.Alt => DropModifier.Overwrite,
_ => DropModifier.None,
};
public static readonly Color GoodDataColor = Color.Transparent;
public static Color BadDataColor => WinFormsUtil.ColorWarn;
/// <summary>
/// Refreshes a <see cref="PictureBox"/> with the appropriate display content.
/// </summary>
public static void UpdateSlot(PictureBox pb, ISlotInfo info, PKM pk, SaveFile sav, SlotVisibilityType flags, SlotTouchType t = SlotTouchType.None)
{
pb.Image?.Dispose();
pb.BackgroundImage?.Dispose();
pb.BackgroundImage = GetTouchTypeBackground(t);
if (pk.Species == 0) // Nothing in slot
{
pb.Image = null;
pb.BackColor = GoodDataColor;
pb.AccessibleDescription = null;
return;
}
if (!pk.Valid) // Invalid
{
// Bad Egg present in slot.
pb.Image = null;
pb.BackColor = BadDataColor;
pb.AccessibleDescription = null;
return;
}
pb.Image = GetImage(info, pk, sav, flags);
pb.BackColor = GoodDataColor;
// Get an accessible description for the slot (for screen readers)
var x = Main.Settings;
var programLanguage = Language.GetLanguageValue(x.Startup.Language);
var cfg = x.BattleTemplate;
var settings = cfg.Hover.GetSettings(programLanguage, pk.Context);
pb.AccessibleDescription = ShowdownParsing.GetLocalizedPreviewText(pk, settings);
}
private static Bitmap GetImage(ISlotInfo info, PKM pk, SaveFile sav, SlotVisibilityType flags) => info switch
{
SlotInfoBox b => pk.Sprite(sav, b.Box, b.Slot, flags, b.Type),
SlotInfoParty ps => pk.Sprite(sav, -1, ps.Slot, flags, ps.Type),
_ => pk.Sprite(sav, -1, -1, flags, info.Type),
};
}