PKHeX/PKHeX.WinForms/Controls/Slots/SummaryPreviewer.cs
Kurt 6441bdadd8
Add specialized struct for Moveset and IV specs (#3572)
`Moveset` struct stores 4 moves, and exposes methods to interact with a moveset.
`IndividualValueSet` stores a 6 IV template (signed).

Performance impact:
* Less allocating on the heap: Moves - (8 bytes member ptr, 20 bytes heap->8 bytes member)
* Less allocating on the heap: IVs - (8 bytes member ptr, 28 bytes heap->8 bytes member)
* No heap pointers, no need to jump to grab data.
* Easy to inline logic for checking if moves are present (no linq usage with temporary collections).

End result is faster ctor times, less memory used, faster program.
2022-08-21 17:34:32 -07:00

103 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PKHeX.Core;
using static PKHeX.Core.LegalityCheckStrings;
namespace PKHeX.WinForms.Controls;
public sealed class SummaryPreviewer
{
private readonly ToolTip ShowSet = new() { InitialDelay = 200, IsBalloon = false, AutoPopDelay = 32_767 };
private readonly CryPlayer Cry = new();
public void Show(Control pb, PKM pk)
{
if (pk.Species == 0)
{
Clear();
return;
}
if (Main.Settings.Hover.HoverSlotShowText)
{
var text = ShowdownParsing.GetLocalizedPreviewText(pk, Main.Settings.Startup.Language);
var la = new LegalityAnalysis(pk);
var result = new List<string> { text, string.Empty };
LegalityFormatting.AddEncounterInfo(la, result);
ShowSet.SetToolTip(pb, string.Join(Environment.NewLine, result));
}
if (Main.Settings.Hover.HoverSlotPlayCry)
Cry.PlayCry(pk, pk.Format);
}
public void Show(Control pb, IEncounterInfo enc)
{
if (enc.Species == 0)
{
Clear();
return;
}
if (Main.Settings.Hover.HoverSlotShowText)
{
var lines = GetTextLines(enc);
var text = string.Join(Environment.NewLine, lines);
ShowSet.SetToolTip(pb, text);
}
if (Main.Settings.Hover.HoverSlotPlayCry)
Cry.PlayCry(enc, enc.Generation);
}
public static IEnumerable<string> GetTextLines(IEncounterInfo enc)
{
var lines = new List<string>();
var str = GameInfo.Strings.Species;
var name = (uint) enc.Species < str.Count ? str[enc.Species] : enc.Species.ToString();
var EncounterName = $"{(enc is IEncounterable ie ? ie.LongName : "Special")} ({name})";
lines.Add(string.Format(L_FEncounterType_0, EncounterName));
if (enc is MysteryGift mg)
{
lines.AddRange(mg.GetDescription());
}
else if (enc is IMoveset m)
{
var moves = m.Moves;
if (moves.HasMoves)
{
string result = moves.GetMovesetLine(GameInfo.Strings.movelist);
lines.Add(result);
}
}
var el = enc as ILocation;
var loc = el?.GetEncounterLocation(enc.Generation, (int) enc.Version);
if (!string.IsNullOrEmpty(loc))
lines.Add(string.Format(L_F0_1, "Location", loc));
lines.Add(string.Format(L_F0_1, nameof(GameVersion), enc.Version));
lines.Add(enc.LevelMin == enc.LevelMax
? $"Level: {enc.LevelMin}"
: $"Level: {enc.LevelMin}-{enc.LevelMax}");
#if DEBUG
// Record types! Can get a nice summary.
// Won't work neatly for Mystery Gift types since those aren't record types.
if (enc is not MysteryGift)
{
// ReSharper disable once ConstantNullCoalescingCondition
var raw = enc.ToString() ?? throw new ArgumentNullException(nameof(enc));
lines.AddRange(raw.Split(',', '}', '{'));
}
#endif
return lines;
}
public void Clear()
{
ShowSet.RemoveAll();
Cry.Stop();
}
}