2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2017-05-23 04:55:05 +00:00
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using PKHeX.Core;
|
|
|
|
|
2018-04-07 04:23:09 +00:00
|
|
|
using static PKHeX.Core.MessageStrings;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.WinForms.Controls;
|
|
|
|
|
|
|
|
public partial class ContextMenuSAV : UserControl
|
2017-05-23 04:55:05 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public ContextMenuSAV() => InitializeComponent();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public SaveDataEditor<PictureBox> Editor { private get; set; } = null!;
|
2023-01-22 04:02:33 +00:00
|
|
|
public required SlotChangeManager Manager { get; init; }
|
2019-09-03 02:30:58 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public Action<LegalityAnalysis>? RequestEditorLegality;
|
2017-06-04 20:35:51 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void OmniClick(object sender, EventArgs e, Keys z)
|
|
|
|
{
|
|
|
|
switch (z)
|
2017-05-23 04:55:05 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
case Keys.Control: ClickView(sender, e); break;
|
|
|
|
case Keys.Shift: ClickSet(sender, e); break;
|
|
|
|
case Keys.Alt: ClickDelete(sender, e); break;
|
|
|
|
default:
|
|
|
|
return;
|
2017-05-23 04:55:05 +00:00
|
|
|
}
|
2018-08-13 02:27:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// restart hovering since the mouse event isn't fired
|
|
|
|
Manager.MouseEnter(sender, e);
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void ClickView(object sender, EventArgs e)
|
|
|
|
{
|
2023-05-07 18:28:28 +00:00
|
|
|
var info = GetSenderInfo(sender);
|
|
|
|
if (info.IsEmpty())
|
2022-06-18 18:04:24 +00:00
|
|
|
{ System.Media.SystemSounds.Asterisk.Play(); return; }
|
2018-08-13 02:27:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
Manager.Hover.Stop();
|
|
|
|
var pk = Editor.Slots.Get(info.Slot);
|
|
|
|
Editor.PKMEditor.PopulateFields(pk, false, true);
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void ClickSet(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
var editor = Editor.PKMEditor;
|
|
|
|
if (!editor.EditsComplete)
|
|
|
|
return;
|
|
|
|
PKM pk = editor.PreparePKM();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2023-05-07 18:28:28 +00:00
|
|
|
var info = GetSenderInfo(sender);
|
2022-06-18 18:04:24 +00:00
|
|
|
var sav = info.View.SAV;
|
2017-11-18 16:42:20 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!CheckDest(info, sav, pk))
|
|
|
|
return;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var errata = sav.EvaluateCompatibility(pk);
|
2023-05-07 18:28:28 +00:00
|
|
|
if (errata.Count != 0)
|
|
|
|
{
|
|
|
|
var msg = string.Join(Environment.NewLine, errata);
|
|
|
|
var prompt = WinFormsUtil.Prompt(MessageBoxButtons.YesNo, msg, MsgContinue);
|
|
|
|
if (prompt != DialogResult.Yes)
|
|
|
|
return;
|
|
|
|
}
|
2018-08-13 02:27:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
Manager.Hover.Stop();
|
|
|
|
Editor.Slots.Set(info.Slot, pk);
|
|
|
|
Manager.SE.UpdateUndoRedo();
|
|
|
|
}
|
2019-10-08 01:40:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void ClickDelete(object sender, EventArgs e)
|
|
|
|
{
|
2023-05-07 18:28:28 +00:00
|
|
|
var info = GetSenderInfo(sender);
|
|
|
|
if (info.IsEmpty())
|
2022-06-18 18:04:24 +00:00
|
|
|
{ System.Media.SystemSounds.Asterisk.Play(); return; }
|
|
|
|
|
|
|
|
var sav = info.View.SAV;
|
|
|
|
var pk = sav.BlankPKM;
|
|
|
|
if (!CheckDest(info, sav, pk))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Manager.Hover.Stop();
|
|
|
|
Editor.Slots.Delete(info.Slot);
|
|
|
|
Manager.SE.UpdateUndoRedo();
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool CheckDest(SlotViewInfo<PictureBox> info, SaveFile sav, PKM pk)
|
|
|
|
{
|
|
|
|
var msg = info.Slot.CanWriteTo(sav, pk);
|
|
|
|
if (msg == WriteBlockedMessage.None)
|
|
|
|
return true;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
switch (msg)
|
2019-09-03 02:30:58 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
case WriteBlockedMessage.InvalidPartyConfiguration:
|
|
|
|
WinFormsUtil.Alert(MsgSaveSlotEmpty);
|
|
|
|
break;
|
|
|
|
case WriteBlockedMessage.IncompatibleFormat:
|
|
|
|
break;
|
|
|
|
case WriteBlockedMessage.InvalidDestination:
|
|
|
|
WinFormsUtil.Alert(MsgSaveSlotLocked);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IndexOutOfRangeException(nameof(msg));
|
2017-05-23 04:55:05 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-13 02:27:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void ClickShowLegality(object sender, EventArgs e)
|
|
|
|
{
|
2023-05-07 18:28:28 +00:00
|
|
|
var info = GetSenderInfo(sender);
|
2022-06-18 18:04:24 +00:00
|
|
|
var sav = info.View.SAV;
|
|
|
|
var pk = info.Slot.Read(sav);
|
2024-06-15 05:14:49 +00:00
|
|
|
var type = info.Slot.Type;
|
2022-06-18 18:04:24 +00:00
|
|
|
var la = new LegalityAnalysis(pk, sav.Personal, type);
|
|
|
|
RequestEditorLegality?.Invoke(la);
|
|
|
|
}
|
2018-08-13 02:27:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void MenuOpening(object sender, CancelEventArgs e)
|
|
|
|
{
|
2024-06-15 05:14:49 +00:00
|
|
|
var info = GetSenderInfo(sender);
|
|
|
|
bool canView = !info.IsEmpty() || Main.HaX;
|
|
|
|
bool canSet = info.CanWriteTo();
|
|
|
|
bool canDelete = canSet && canView;
|
|
|
|
bool canLegality = ModifierKeys == Keys.Control && canView && RequestEditorLegality != null;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2024-06-15 05:14:49 +00:00
|
|
|
ToggleItem(mnuView, canView);
|
|
|
|
ToggleItem(mnuSet, canSet);
|
|
|
|
ToggleItem(mnuDelete, canDelete);
|
|
|
|
ToggleItem(mnuLegality, canLegality);
|
|
|
|
|
|
|
|
if (!canView && !canSet && !canDelete)
|
2022-06-18 18:04:24 +00:00
|
|
|
e.Cancel = true;
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2023-05-07 18:28:28 +00:00
|
|
|
private static SlotViewInfo<PictureBox> GetSenderInfo(object sender)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var pb = WinFormsUtil.GetUnderlyingControl<PictureBox>(sender);
|
2023-12-04 04:13:20 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(pb);
|
2022-06-18 18:04:24 +00:00
|
|
|
var view = WinFormsUtil.FindFirstControlOfType<ISlotViewer<PictureBox>>(pb);
|
2023-12-04 04:13:20 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(view);
|
2022-06-18 18:04:24 +00:00
|
|
|
var loc = view.GetSlotData(pb);
|
|
|
|
return new SlotViewInfo<PictureBox>(loc, view);
|
|
|
|
}
|
|
|
|
|
2024-06-15 05:14:49 +00:00
|
|
|
private static void ToggleItem(ToolStripItem item, bool visible)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2024-06-15 05:14:49 +00:00
|
|
|
item.Visible = visible;
|
2017-05-23 04:55:05 +00:00
|
|
|
}
|
|
|
|
}
|