2022-08-22 00:34:32 +00:00
|
|
|
using System;
|
2021-02-07 06:33:13 +00:00
|
|
|
using System.Collections.Generic;
|
2023-11-15 03:36:11 +00:00
|
|
|
using System.Drawing;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-02-07 06:33:13 +00:00
|
|
|
using System.Windows.Forms;
|
2019-08-21 02:50:28 +00:00
|
|
|
using PKHeX.Core;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.WinForms.Controls;
|
|
|
|
|
|
|
|
public sealed class SummaryPreviewer
|
2019-08-21 02:50:28 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
private readonly ToolTip ShowSet = new() { InitialDelay = 200, IsBalloon = false, AutoPopDelay = 32_767 };
|
|
|
|
private readonly CryPlayer Cry = new();
|
2023-11-15 03:36:11 +00:00
|
|
|
private readonly PokePreview Previewer = new();
|
|
|
|
private CancellationTokenSource _source = new();
|
|
|
|
private static HoverSettings Settings => Main.Settings.Hover;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
public void Show(Control pb, PKM pk)
|
2019-08-21 02:50:28 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Species == 0)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
return;
|
|
|
|
}
|
2019-08-21 02:50:28 +00:00
|
|
|
|
2023-11-15 03:36:11 +00:00
|
|
|
if (Settings.HoverSlotShowPreview && Control.ModifierKeys != Keys.Alt)
|
|
|
|
UpdatePreview(pb, pk);
|
|
|
|
else if (Settings.HoverSlotShowText)
|
|
|
|
ShowSet.SetToolTip(pb, GetPreviewText(pk, new LegalityAnalysis(pk)));
|
|
|
|
if (Settings.HoverSlotPlayCry)
|
2022-12-18 08:16:29 +00:00
|
|
|
Cry.PlayCry(pk, pk.Context);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2021-04-12 01:09:54 +00:00
|
|
|
|
2023-11-15 03:36:11 +00:00
|
|
|
private void UpdatePreview(Control pb, PKM pk)
|
|
|
|
{
|
|
|
|
_source.Cancel();
|
|
|
|
_source = new();
|
|
|
|
UpdatePreviewPosition(new());
|
|
|
|
Previewer.Populate(pk);
|
|
|
|
Previewer.Show();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdatePreviewPosition(Point location)
|
|
|
|
{
|
|
|
|
var cLoc = Cursor.Position;
|
|
|
|
var shift = Settings.PreviewCursorShift;
|
|
|
|
cLoc.Offset(shift);
|
|
|
|
Previewer.Location = cLoc;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void Show(Control pb, IEncounterInfo enc)
|
|
|
|
{
|
|
|
|
if (enc.Species == 0)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
return;
|
2019-08-21 02:50:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 03:36:11 +00:00
|
|
|
if (Settings.HoverSlotShowText)
|
2024-09-23 05:47:43 +00:00
|
|
|
ShowSet.SetToolTip(pb, GetPreviewText(enc, Settings.HoverSlotShowEncounterVerbose));
|
2023-11-15 03:36:11 +00:00
|
|
|
if (Settings.HoverSlotPlayCry)
|
2022-12-18 08:16:29 +00:00
|
|
|
Cry.PlayCry(enc, enc.Context);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2021-04-12 01:09:54 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void Clear()
|
|
|
|
{
|
2023-11-15 03:36:11 +00:00
|
|
|
var src = _source;
|
|
|
|
Task.Run(async () =>
|
|
|
|
{
|
2024-01-10 07:49:00 +00:00
|
|
|
if (!Previewer.IsHandleCreated)
|
|
|
|
return; // not shown ever
|
|
|
|
|
|
|
|
// Give a little bit of fade-out delay
|
2023-11-15 03:36:11 +00:00
|
|
|
await Task.Delay(50, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
if (!src.IsCancellationRequested)
|
|
|
|
Previewer.Invoke(Previewer.Hide);
|
|
|
|
}, src.Token);
|
2022-06-18 18:04:24 +00:00
|
|
|
ShowSet.RemoveAll();
|
|
|
|
Cry.Stop();
|
2019-08-21 02:50:28 +00:00
|
|
|
}
|
2023-11-15 03:36:11 +00:00
|
|
|
|
|
|
|
public static string GetPreviewText(PKM pk, LegalityAnalysis la)
|
|
|
|
{
|
|
|
|
var text = ShowdownParsing.GetLocalizedPreviewText(pk, Main.Settings.Startup.Language);
|
|
|
|
if (!Main.Settings.Hover.HoverSlotShowEncounter)
|
|
|
|
return text;
|
|
|
|
var result = new List<string> { text, string.Empty };
|
|
|
|
LegalityFormatting.AddEncounterInfo(la, result);
|
|
|
|
return string.Join(Environment.NewLine, result);
|
|
|
|
}
|
|
|
|
|
2024-09-23 05:47:43 +00:00
|
|
|
private static string GetPreviewText(IEncounterInfo enc, bool verbose = false)
|
2023-11-15 03:36:11 +00:00
|
|
|
{
|
2024-09-23 05:47:43 +00:00
|
|
|
var lines = enc.GetTextLines(GameInfo.Strings, verbose);
|
2023-11-15 03:36:11 +00:00
|
|
|
return string.Join(Environment.NewLine, lines);
|
|
|
|
}
|
2021-02-07 06:33:13 +00:00
|
|
|
}
|