2017-05-22 21:55:05 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using PKHeX.Core;
|
|
|
|
|
|
2018-04-06 21:23:09 -07:00
|
|
|
|
using static PKHeX.Core.MessageStrings;
|
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
namespace PKHeX.WinForms.Controls
|
|
|
|
|
{
|
|
|
|
|
public partial class ContextMenuSAV : UserControl
|
|
|
|
|
{
|
2019-02-03 10:28:33 -08:00
|
|
|
|
public ContextMenuSAV() => InitializeComponent();
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
2019-09-02 19:30:58 -07:00
|
|
|
|
public SaveDataEditor<PictureBox> Editor { private get; set; }
|
|
|
|
|
public SlotChangeManager Manager { get; set; }
|
|
|
|
|
|
2017-06-04 13:35:51 -07:00
|
|
|
|
public event LegalityRequest RequestEditorLegality;
|
2017-12-16 14:11:41 -08:00
|
|
|
|
public delegate void LegalityRequest(object sender, EventArgs e, PKM pkm);
|
2017-06-04 13:35:51 -07:00
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
public void OmniClick(object sender, EventArgs e, Keys z)
|
|
|
|
|
{
|
|
|
|
|
switch (z)
|
|
|
|
|
{
|
|
|
|
|
case Keys.Control: ClickView(sender, e); break;
|
|
|
|
|
case Keys.Shift: ClickSet(sender, e); break;
|
|
|
|
|
case Keys.Alt: ClickDelete(sender, e); break;
|
2019-09-02 19:30:58 -07:00
|
|
|
|
default:
|
|
|
|
|
return;
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2019-09-02 19:30:58 -07:00
|
|
|
|
|
|
|
|
|
// restart hovering since the mouse event isn't fired
|
|
|
|
|
Manager.MouseEnter(sender, e);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2018-08-12 19:27:11 -07:00
|
|
|
|
|
2018-04-21 22:31:11 -07:00
|
|
|
|
private void ClickView(object sender, EventArgs e)
|
2017-05-22 21:55:05 -07:00
|
|
|
|
{
|
2019-09-02 19:30:58 -07:00
|
|
|
|
var info = GetSenderInfo(ref sender);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
if ((sender as PictureBox)?.Image == null)
|
|
|
|
|
{ System.Media.SystemSounds.Asterisk.Play(); return; }
|
|
|
|
|
|
2019-09-02 19:30:58 -07:00
|
|
|
|
Manager.Hover.Stop();
|
|
|
|
|
var pkm = Editor.Slots.Get(info.Slot);
|
|
|
|
|
Editor.PKMEditor.PopulateFields(pkm, false, true);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2018-08-12 19:27:11 -07:00
|
|
|
|
|
2018-04-21 22:31:11 -07:00
|
|
|
|
private void ClickSet(object sender, EventArgs e)
|
2017-05-22 21:55:05 -07:00
|
|
|
|
{
|
2019-09-02 19:30:58 -07:00
|
|
|
|
var editor = Editor.PKMEditor;
|
|
|
|
|
if (!editor.EditsComplete)
|
2017-05-22 21:55:05 -07:00
|
|
|
|
return;
|
2019-09-02 19:30:58 -07:00
|
|
|
|
PKM pk = editor.PreparePKM();
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
2019-09-02 19:30:58 -07:00
|
|
|
|
var info = GetSenderInfo(ref sender);
|
|
|
|
|
var sav = info.View.SAV;
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
2019-09-02 19:30:58 -07:00
|
|
|
|
if (!CheckDest(info, sav, pk))
|
2017-11-18 08:42:20 -08:00
|
|
|
|
return;
|
|
|
|
|
|
2018-07-20 21:32:33 -07:00
|
|
|
|
var errata = sav.IsPKMCompatible(pk);
|
|
|
|
|
if (errata.Count > 0 && DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo, string.Join(Environment.NewLine, errata), MsgContinue))
|
2017-05-22 21:55:05 -07:00
|
|
|
|
return;
|
|
|
|
|
|
2019-09-02 19:30:58 -07:00
|
|
|
|
Manager.Hover.Stop();
|
|
|
|
|
Editor.Slots.Set(info.Slot, pk);
|
|
|
|
|
Manager.SE.UpdateUndoRedo();
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2018-08-12 19:27:11 -07:00
|
|
|
|
|
2018-04-21 22:31:11 -07:00
|
|
|
|
private void ClickDelete(object sender, EventArgs e)
|
2017-05-22 21:55:05 -07:00
|
|
|
|
{
|
2019-09-02 19:30:58 -07:00
|
|
|
|
var info = GetSenderInfo(ref sender);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
if ((sender as PictureBox)?.Image == null)
|
|
|
|
|
{ System.Media.SystemSounds.Asterisk.Play(); return; }
|
2019-10-07 18:40:09 -07:00
|
|
|
|
|
2019-09-02 19:30:58 -07:00
|
|
|
|
var sav = info.View.SAV;
|
|
|
|
|
var pk = sav.BlankPKM;
|
|
|
|
|
if (!CheckDest(info, sav, pk))
|
|
|
|
|
return;
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
2019-09-02 19:30:58 -07:00
|
|
|
|
Manager.Hover.Stop();
|
|
|
|
|
Editor.Slots.Delete(info.Slot);
|
|
|
|
|
Manager.SE.UpdateUndoRedo();
|
|
|
|
|
}
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
2019-09-02 19:30:58 -07: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;
|
2018-07-28 09:56:13 -07:00
|
|
|
|
|
2019-09-02 19:30:58 -07:00
|
|
|
|
switch (msg)
|
2018-08-12 19:27:11 -07:00
|
|
|
|
{
|
2019-09-02 19:30:58 -07:00
|
|
|
|
case WriteBlockedMessage.InvalidPartyConfiguration:
|
|
|
|
|
WinFormsUtil.Alert(MsgSaveSlotEmpty);
|
|
|
|
|
break;
|
|
|
|
|
case WriteBlockedMessage.IncompatibleFormat:
|
|
|
|
|
break;
|
|
|
|
|
case WriteBlockedMessage.InvalidDestination:
|
|
|
|
|
WinFormsUtil.Alert(MsgSaveSlotLocked);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
2018-08-12 19:27:11 -07:00
|
|
|
|
}
|
2019-09-02 19:30:58 -07:00
|
|
|
|
return false;
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2018-08-12 19:27:11 -07:00
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
private void ClickShowLegality(object sender, EventArgs e)
|
|
|
|
|
{
|
2019-09-02 19:30:58 -07:00
|
|
|
|
var info = GetSenderInfo(ref sender);
|
|
|
|
|
var sav = info.View.SAV;
|
|
|
|
|
var pk = info.Slot.Read(sav);
|
2017-06-04 13:35:51 -07:00
|
|
|
|
RequestEditorLegality?.Invoke(sender, e, pk);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2018-08-12 19:27:11 -07:00
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
private void MenuOpening(object sender, CancelEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var items = ((ContextMenuStrip)sender).Items;
|
|
|
|
|
|
|
|
|
|
object ctrl = ((ContextMenuStrip)sender).SourceControl;
|
2019-09-02 19:30:58 -07:00
|
|
|
|
var info = GetSenderInfo(ref ctrl);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
bool SlotFull = (ctrl as PictureBox)?.Image != null;
|
2019-09-02 19:30:58 -07:00
|
|
|
|
bool Editable = info.Slot.CanWriteTo(info.View.SAV);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
bool legality = ModifierKeys == Keys.Control;
|
|
|
|
|
ToggleItem(items, mnuSet, Editable);
|
|
|
|
|
ToggleItem(items, mnuDelete, Editable && SlotFull);
|
2017-06-04 13:35:51 -07:00
|
|
|
|
ToggleItem(items, mnuLegality, legality && SlotFull && RequestEditorLegality != null);
|
2017-05-29 22:34:45 -07:00
|
|
|
|
ToggleItem(items, mnuView, SlotFull || !Editable, true);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
|
|
|
|
if (items.Count == 0)
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 19:30:58 -07:00
|
|
|
|
private static SlotViewInfo<PictureBox> GetSenderInfo(ref object sender)
|
2017-05-22 21:55:05 -07:00
|
|
|
|
{
|
2019-09-24 18:14:15 -07:00
|
|
|
|
var pb = WinFormsUtil.GetUnderlyingControl<PictureBox>(sender);
|
2018-05-05 08:07:22 -07:00
|
|
|
|
var view = WinFormsUtil.FindFirstControlOfType<ISlotViewer<PictureBox>>(pb);
|
2019-09-02 19:30:58 -07:00
|
|
|
|
var loc = view.GetSlotData(pb);
|
2019-09-28 19:29:15 -07:00
|
|
|
|
sender = pb;
|
2019-09-02 19:30:58 -07:00
|
|
|
|
return new SlotViewInfo<PictureBox> {Slot = loc, View = view};
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2018-08-12 19:27:11 -07:00
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
private static void ToggleItem(ToolStripItemCollection items, ToolStripItem item, bool visible, bool first = false)
|
|
|
|
|
{
|
|
|
|
|
if (visible)
|
|
|
|
|
{
|
|
|
|
|
if (first)
|
|
|
|
|
items.Insert(0, item);
|
|
|
|
|
else
|
|
|
|
|
items.Add(item);
|
|
|
|
|
}
|
|
|
|
|
else if (items.Contains(item))
|
2018-08-12 19:27:11 -07:00
|
|
|
|
{
|
2017-05-22 21:55:05 -07:00
|
|
|
|
items.Remove(item);
|
2018-08-12 19:27:11 -07:00
|
|
|
|
}
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|