2017-05-23 04:55:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using PKHeX.Core;
|
|
|
|
|
using PKHeX.WinForms.Properties;
|
|
|
|
|
|
2018-04-07 04:23:09 +00:00
|
|
|
|
using static PKHeX.Core.MessageStrings;
|
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
namespace PKHeX.WinForms.Controls
|
|
|
|
|
{
|
|
|
|
|
public partial class ContextMenuSAV : UserControl
|
|
|
|
|
{
|
|
|
|
|
public ContextMenuSAV()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-04 20:35:51 +00:00
|
|
|
|
public event LegalityRequest RequestEditorLegality;
|
2017-12-16 22:11:41 +00:00
|
|
|
|
public delegate void LegalityRequest(object sender, EventArgs e, PKM pkm);
|
2017-06-04 20:35:51 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00: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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static void ClickView(object sender, EventArgs e)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
SlotChangeManager m = GetSenderInfo(ref sender, out SlotChange info);
|
|
|
|
|
if (m == null)
|
|
|
|
|
return;
|
|
|
|
|
if ((sender as PictureBox)?.Image == null)
|
|
|
|
|
{ System.Media.SystemSounds.Asterisk.Play(); return; }
|
|
|
|
|
|
2017-10-28 21:26:27 +00:00
|
|
|
|
m.SE.PKME_Tabs.PopulateFields(m.GetPKM(info), false, true);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
m.SetColor(info.Box, info.Slot, Resources.slotView);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static void ClickSet(object sender, EventArgs e)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
SlotChangeManager m = GetSenderInfo(ref sender, out SlotChange info);
|
|
|
|
|
if (m == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var editor = m.SE.PKME_Tabs;
|
|
|
|
|
var sav = m.SE.SAV;
|
2017-06-22 03:24:42 +00:00
|
|
|
|
if (info.IsParty && editor.IsEmptyOrEgg && sav.IsPartyAllEggs(info.Slot - 30) && !m.SE.HaX)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
{ WinFormsUtil.Alert(MsgSaveSlotEmpty); return; }
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (m.SE.SAV.IsSlotLocked(info.Box, info.Slot))
|
2018-04-07 04:23:09 +00:00
|
|
|
|
{ WinFormsUtil.Alert(MsgSaveSlotLocked); return; }
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
2017-11-18 16:42:20 +00:00
|
|
|
|
if (!editor.VerifiedPKM())
|
|
|
|
|
return;
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
PKM pk = editor.PreparePKM();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
string[] errata = sav.IsPKMCompatible(pk);
|
2018-04-07 04:23:09 +00:00
|
|
|
|
if (errata.Length > 0 && DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo, string.Join(Environment.NewLine, errata), MsgContinue))
|
2017-05-23 04:55:05 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (info.Slot >= 30)
|
|
|
|
|
info.Box = -1;
|
|
|
|
|
if (info.Slot >= 30 && info.Slot < 36) // Party
|
|
|
|
|
{
|
|
|
|
|
// If info.Slot isn't overwriting existing PKM, make it write to the lowest empty PKM info.Slot
|
|
|
|
|
if (sav.PartyCount < info.Slot + 1 - 30)
|
|
|
|
|
{
|
|
|
|
|
info.Slot = sav.PartyCount + 30;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
info.Offset = m.SE.GetPKMOffset(info.Slot);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
m.SetPKM(pk, info, true, Resources.slotSet);
|
|
|
|
|
}
|
|
|
|
|
else if (info.Slot < 30 || m.SE.HaX)
|
|
|
|
|
{
|
|
|
|
|
if (info.Slot < 30)
|
|
|
|
|
{
|
2018-03-26 02:05:49 +00:00
|
|
|
|
m.SE.UndoStack.Push(new SlotChange(info, sav));
|
2017-05-23 04:55:05 +00:00
|
|
|
|
m.SE.Menu_Undo.Enabled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.SetPKM(pk, info, true, Resources.slotSet);
|
|
|
|
|
}
|
2018-01-31 02:52:55 +00:00
|
|
|
|
else
|
|
|
|
|
return;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
2017-06-19 05:27:40 +00:00
|
|
|
|
editor.LastData = pk.Data;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
m.SE.RedoStack.Clear(); m.SE.Menu_Redo.Enabled = false;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static void ClickDelete(object sender, EventArgs e)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
SlotChangeManager m = GetSenderInfo(ref sender, out SlotChange info);
|
|
|
|
|
if (m == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if ((sender as PictureBox)?.Image == null)
|
|
|
|
|
{ System.Media.SystemSounds.Asterisk.Play(); return; }
|
|
|
|
|
|
|
|
|
|
var sav = m.SE.SAV;
|
2017-06-22 03:24:42 +00:00
|
|
|
|
if (info.IsParty && sav.IsPartyAllEggs(info.Slot - 30) && !m.SE.HaX)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
{ WinFormsUtil.Alert(MsgSaveSlotEmpty); return; }
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (sav.IsSlotLocked(info.Box, info.Slot))
|
2018-04-07 04:23:09 +00:00
|
|
|
|
{ WinFormsUtil.Alert(MsgSaveSlotLocked); return; }
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
if (info.Slot >= 30 && info.Slot < 36) // Party
|
|
|
|
|
{
|
|
|
|
|
m.SetPKM(sav.BlankPKM, info, true, Resources.slotDel);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (info.Slot < 30 || m.SE.HaX)
|
|
|
|
|
{
|
|
|
|
|
if (info.Slot < 30)
|
|
|
|
|
{
|
2018-03-26 02:05:49 +00:00
|
|
|
|
m.SE.UndoStack.Push(new SlotChange(info, sav));
|
2017-05-23 04:55:05 +00:00
|
|
|
|
m.SE.Menu_Undo.Enabled = true;
|
|
|
|
|
}
|
|
|
|
|
m.SetPKM(sav.BlankPKM, info, true, Resources.slotDel);
|
|
|
|
|
}
|
|
|
|
|
else return;
|
|
|
|
|
|
|
|
|
|
m.SE.RedoStack.Clear(); m.SE.Menu_Redo.Enabled = false;
|
|
|
|
|
}
|
|
|
|
|
private void ClickShowLegality(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
SlotChangeManager m = GetSenderInfo(ref sender, out SlotChange info);
|
|
|
|
|
if (m == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var pk = m.GetPKM(info);
|
2017-06-04 20:35:51 +00:00
|
|
|
|
RequestEditorLegality?.Invoke(sender, e, pk);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
private void MenuOpening(object sender, CancelEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var items = ((ContextMenuStrip)sender).Items;
|
|
|
|
|
|
|
|
|
|
object ctrl = ((ContextMenuStrip)sender).SourceControl;
|
|
|
|
|
GetSenderInfo(ref ctrl, out SlotChange info);
|
|
|
|
|
bool SlotFull = (ctrl as PictureBox)?.Image != null;
|
|
|
|
|
bool Editable = info.Slot < 36;
|
|
|
|
|
bool legality = ModifierKeys == Keys.Control;
|
|
|
|
|
ToggleItem(items, mnuSet, Editable);
|
|
|
|
|
ToggleItem(items, mnuDelete, Editable && SlotFull);
|
2017-06-04 20:35:51 +00:00
|
|
|
|
ToggleItem(items, mnuLegality, legality && SlotFull && RequestEditorLegality != null);
|
2017-05-30 05:34:45 +00:00
|
|
|
|
ToggleItem(items, mnuView, SlotFull || !Editable, true);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
if (items.Count == 0)
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static SlotChangeManager GetSenderInfo(ref object sender, out SlotChange loc)
|
|
|
|
|
{
|
|
|
|
|
loc = new SlotChange();
|
|
|
|
|
var ctrl = WinFormsUtil.GetUnderlyingControl(sender);
|
|
|
|
|
var obj = ctrl.Parent.Parent;
|
|
|
|
|
if (obj is BoxEditor b)
|
|
|
|
|
{
|
|
|
|
|
loc.Box = b.CurrentBox;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
loc.Slot = b.GetSlot(sender);
|
|
|
|
|
loc.Offset = b.GetOffset(loc.Slot, loc.Box);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
loc.Parent = b.FindForm();
|
|
|
|
|
sender = ctrl;
|
|
|
|
|
return b.M;
|
|
|
|
|
}
|
2018-01-31 02:52:55 +00:00
|
|
|
|
if (obj is SlotList s)
|
|
|
|
|
{
|
|
|
|
|
const int shift = 44;
|
|
|
|
|
loc.Slot = s.GetSlot(sender) + shift;
|
|
|
|
|
loc.Offset = s.GetSlotOffset(loc.Slot - shift);
|
|
|
|
|
loc.Parent = s.FindForm();
|
|
|
|
|
sender = ctrl;
|
|
|
|
|
return s.M;
|
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
obj = obj.Parent.Parent;
|
|
|
|
|
if (obj is SAVEditor z)
|
|
|
|
|
{
|
|
|
|
|
loc.Box = z.Box.CurrentBox;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
loc.Slot = z.GetSlot(sender);
|
|
|
|
|
loc.Offset = z.GetPKMOffset(loc.Slot, loc.Box);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
loc.Parent = z.FindForm();
|
|
|
|
|
sender = ctrl;
|
|
|
|
|
return z.M;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
|
items.Remove(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|