PKHeX/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs

579 lines
19 KiB
C#
Raw Normal View History

2018-07-29 23:39:15 +00:00
using PKHeX.Core;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
2014-06-28 21:22:05 +00:00
using System.Windows.Forms;
using static PKHeX.Core.MessageStrings;
2014-06-28 21:22:05 +00:00
namespace PKHeX.WinForms
2014-06-28 21:22:05 +00:00
{
public partial class SAV_Wondercard : Form
{
private readonly SaveFile Origin;
private readonly SaveFile SAV;
2018-07-29 23:39:15 +00:00
public SAV_Wondercard(SaveFile sav, MysteryGift g = null)
2014-06-28 21:22:05 +00:00
{
InitializeComponent();
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
2018-04-03 03:36:13 +00:00
SAV = (Origin = sav).Clone();
mga = SAV.GiftAlbum;
2018-05-12 15:13:39 +00:00
switch (SAV.Generation)
{
case 4:
pba = PopulateViewGiftsG4().ToArray();
break;
case 5:
case 6:
case 7:
pba = PopulateViewGiftsG567().ToArray();
break;
default:
throw new ArgumentException("Game not supported.");
}
foreach (PictureBox pb in pba)
{
pb.AllowDrop = true;
pb.DragDrop += BoxSlot_DragDrop;
pb.DragEnter += BoxSlot_DragEnter;
pb.MouseDown += BoxSlot_MouseDown;
pb.ContextMenuStrip = mnuVSD;
}
SetGiftBoxes();
GetReceivedFlags();
2018-05-12 15:13:39 +00:00
2014-06-28 21:22:05 +00:00
if (LB_Received.Items.Count > 0)
2014-12-10 23:24:34 +00:00
LB_Received.SelectedIndex = 0;
DragEnter += Main_DragEnter;
DragDrop += Main_DragDrop;
2015-04-01 00:58:23 +00:00
if (g == null)
ClickView(pba[0], null);
else
ViewGiftData(g);
2014-06-28 21:22:05 +00:00
}
2018-07-29 23:39:15 +00:00
private readonly MysteryGiftAlbum mga;
private MysteryGift mg;
private readonly PictureBox[] pba;
2014-06-28 21:22:05 +00:00
// Repopulation Functions
private void SetBackground(int index, Image bg)
{
for (int i = 0; i < mga.Gifts.Length; i++)
pba[i].BackgroundImage = index == i ? bg : null;
}
2018-07-29 23:39:15 +00:00
private void SetGiftBoxes()
2014-06-28 21:22:05 +00:00
{
for (int i = 0; i < mga.Gifts.Length; i++)
2014-06-28 21:22:05 +00:00
{
MysteryGift m = mga.Gifts[i];
pba[i].Image = m.Sprite();
2014-06-28 21:22:05 +00:00
}
}
2018-07-29 23:39:15 +00:00
private void ViewGiftData(MysteryGift g)
2014-06-28 21:22:05 +00:00
{
2014-12-11 06:50:40 +00:00
try
2014-06-28 21:22:05 +00:00
{
// only check if the form is visible (not opening)
if (Visible && g.GiftUsed && DialogResult.Yes == WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgMsyteryGiftUsedAlert, MsgMysteryGiftUsedFix))
g.GiftUsed = false;
2018-04-21 16:55:27 +00:00
RTB.Lines = g.GetDescription().ToArray();
PB_Preview.Image = g.Sprite();
mg = g;
2014-06-28 21:22:05 +00:00
}
catch (Exception e)
{
WinFormsUtil.Error(MsgMysteryGiftParseTypeUnknown, e);
RTB.Clear();
2014-06-28 21:22:05 +00:00
}
}
2018-07-29 23:39:15 +00:00
private void GetReceivedFlags()
2014-06-28 21:22:05 +00:00
{
LB_Received.Items.Clear();
for (int i = 1; i < mga.Flags.Length; i++)
2018-07-29 23:39:15 +00:00
{
if (mga.Flags[i])
2014-06-28 21:22:05 +00:00
LB_Received.Items.Add(i.ToString("0000"));
2018-07-29 23:39:15 +00:00
}
if (LB_Received.Items.Count > 0)
LB_Received.SelectedIndex = 0;
2014-06-28 21:22:05 +00:00
}
2018-07-29 23:39:15 +00:00
private void SetCardID(int cardID)
{
if (cardID <= 0 || cardID >= 0x100 * 8) return;
2014-06-28 21:22:05 +00:00
string card = cardID.ToString("0000");
if (!LB_Received.Items.Contains(card))
LB_Received.Items.Add(card);
LB_Received.SelectedIndex = LB_Received.Items.IndexOf(card);
}
// Mystery Gift IO (.file<->window)
2014-06-28 21:22:05 +00:00
private void B_Import_Click(object sender, EventArgs e)
{
OpenFileDialog import = new OpenFileDialog {Filter = WinFormsUtil.GetMysterGiftFilter(SAV.Generation, SAV.Version) };
if (import.ShowDialog() != DialogResult.OK) return;
string path = import.FileName;
MysteryGift g = MysteryGift.GetMysteryGift(File.ReadAllBytes(path), Path.GetExtension(path));
if (g == null)
2014-06-28 21:22:05 +00:00
{
WinFormsUtil.Error(MsgMysteryGiftInvalid, path);
return;
2014-06-28 21:22:05 +00:00
}
ViewGiftData(g);
2014-06-28 21:22:05 +00:00
}
2018-07-29 23:39:15 +00:00
2014-06-28 21:22:05 +00:00
private void B_Output_Click(object sender, EventArgs e)
{
WinFormsUtil.ExportMGDialog(mg, SAV.Version);
2014-06-28 21:22:05 +00:00
}
private static int GetLastUnfilledByType(MysteryGift Gift, MysteryGiftAlbum Album)
{
for (int i = 0; i < Album.Gifts.Length; i++)
{
if (!Album.Gifts[i].Empty)
continue;
if (Album.Gifts[i].Type != Gift.Type)
continue;
return i;
}
return -1;
}
// Mystery Gift RW (window<->sav)
private void ClickView(object sender, EventArgs e)
2014-06-28 21:22:05 +00:00
{
sender = WinFormsUtil.GetUnderlyingControl(sender);
2016-02-01 07:15:54 +00:00
int index = Array.IndexOf(pba, sender);
SetBackground(index, Properties.Resources.slotView);
ViewGiftData(mga.Gifts[index]);
2014-06-28 21:22:05 +00:00
}
2018-07-29 23:39:15 +00:00
private void ClickSet(object sender, EventArgs e)
2014-06-28 21:22:05 +00:00
{
2018-04-21 16:55:27 +00:00
if (!mg.IsCardCompatible(SAV, out var msg))
{
WinFormsUtil.Alert(MsgMysteryGiftSlotFail, msg);
return;
2018-04-21 16:55:27 +00:00
}
sender = WinFormsUtil.GetUnderlyingControl(sender);
2016-02-01 07:15:54 +00:00
int index = Array.IndexOf(pba, sender);
// Hijack to the latest unfilled slot if index creates interstitial empty slots.
int lastUnfilled = GetLastUnfilledByType(mg, mga);
if (lastUnfilled > -1 && lastUnfilled < index)
index = lastUnfilled;
if (mg is PCD pcd && mga.Gifts[index] is PGT)
2018-07-29 23:39:15 +00:00
{
mg = pcd.Gift;
2018-07-29 23:39:15 +00:00
}
else if (mg.Type != mga.Gifts[index].Type)
{
WinFormsUtil.Alert(MsgMysteryGiftSlotFail, $"{mg.Type} != {mga.Gifts[index].Type}");
return;
}
SetBackground(index, Properties.Resources.slotSet);
mga.Gifts[index] = mg.Clone();
SetGiftBoxes();
SetCardID(mg.CardID);
2014-06-28 21:22:05 +00:00
}
2018-07-29 23:39:15 +00:00
private void ClickDelete(object sender, EventArgs e)
2014-06-28 21:22:05 +00:00
{
sender = WinFormsUtil.GetUnderlyingControl(sender);
2016-02-01 07:15:54 +00:00
int index = Array.IndexOf(pba, sender);
mga.Gifts[index].Data = new byte[mga.Gifts[index].Data.Length];
// Shuffle blank card down
int i = index;
while (i < mga.Gifts.Length - 1)
{
if (mga.Gifts[i+1].Empty)
break;
if (mga.Gifts[i+1].Type != mga.Gifts[i].Type)
break;
i++;
var mg1 = mga.Gifts[i];
var mg2 = mga.Gifts[i-1];
mga.Gifts[i-1] = mg1;
mga.Gifts[i] = mg2;
}
SetBackground(i, Properties.Resources.slotDel);
SetGiftBoxes();
2014-06-28 21:22:05 +00:00
}
// Close Window
private void B_Cancel_Click(object sender, EventArgs e)
{
Close();
}
2018-07-29 23:39:15 +00:00
2014-06-28 21:22:05 +00:00
private void B_Save_Click(object sender, EventArgs e)
{
// Make sure all of the Received Flags are flipped!
bool[] flags = new bool[mga.Flags.Length];
foreach (var o in LB_Received.Items)
flags[Util.ToUInt32(o.ToString())] = true;
2014-06-28 21:22:05 +00:00
mga.Flags = flags;
SAV.GiftAlbum = mga;
Origin.SetData(SAV.Data, 0);
2014-06-28 21:22:05 +00:00
Close();
}
// Delete Received Flag
private void ClearRecievedFlag(object sender, EventArgs e)
2014-06-28 21:22:05 +00:00
{
2016-02-01 07:15:54 +00:00
if (LB_Received.SelectedIndex < 0) return;
if (LB_Received.Items.Count > 0)
LB_Received.Items.Remove(LB_Received.Items[LB_Received.SelectedIndex]);
if (LB_Received.Items.Count > 0)
LB_Received.SelectedIndex = 0;
2014-06-28 21:22:05 +00:00
}
2014-12-10 23:24:34 +00:00
// Drag & Drop Wonder Cards
private static void Main_DragEnter(object sender, DragEventArgs e)
2014-12-10 23:24:34 +00:00
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
2018-07-29 23:39:15 +00:00
private void Main_DragDrop(object sender, DragEventArgs e)
2014-12-10 23:24:34 +00:00
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
2015-01-31 23:59:23 +00:00
// Check for multiple wondercards
if (Directory.Exists(files[0]))
files = Directory.GetFiles(files[0], "*", SearchOption.AllDirectories);
if (files.Length == 1 && !Directory.Exists(files[0]))
2014-12-10 23:24:34 +00:00
{
2015-01-31 23:59:23 +00:00
string path = files[0]; // open first D&D
2018-07-29 23:39:15 +00:00
if (!MysteryGift.IsMysteryGift(new FileInfo(path).Length)) // arbitrary
2015-01-31 23:59:23 +00:00
{
WinFormsUtil.Alert(MsgMysteryGiftInvalid, path);
2015-01-31 23:59:23 +00:00
return;
}
2018-07-29 23:39:15 +00:00
var gift = MysteryGift.GetMysteryGift(File.ReadAllBytes(path), Path.GetExtension(path));
if (gift == null)
{
WinFormsUtil.Error(MsgMysteryGiftInvalid, path);
return;
}
2018-07-29 23:39:15 +00:00
ViewGiftData(gift);
return;
}
SetGiftBoxes();
2014-12-10 23:24:34 +00:00
}
2018-05-12 15:13:39 +00:00
private void ClickQR(object sender, EventArgs e)
{
if (ModifierKeys == Keys.Alt)
{
string url = Clipboard.GetText();
if (!string.IsNullOrWhiteSpace(url))
{
ImportQRToView(url);
return;
}
}
ExportQRFromView();
}
2018-07-29 23:39:15 +00:00
private void ExportQRFromView()
{
2018-03-01 05:05:50 +00:00
if (mg.Data.All(z => z == 0))
{
WinFormsUtil.Alert(MsgMysteryGiftSlotNone);
return;
}
if (SAV.Generation == 6 && mg.ItemID == 726 && mg.IsItem)
{
WinFormsUtil.Alert(MsgMysteryGiftQREonTicket, MsgMysteryGiftQREonTicketAdvice);
return;
}
var qrmessage = QRMessageUtil.GetMessage(mg);
Image qr = QR.GetQRImage(qrmessage);
if (qr == null)
return;
2018-04-21 16:55:27 +00:00
string desc = $"({mg.Type}) {string.Join(Environment.NewLine, mg.GetDescription())}";
new QR(qr, PB_Preview.Image, null, desc + Environment.NewLine + "PKHeX Wonder Card @ ProjectPokemon.org").ShowDialog();
}
2018-07-29 23:39:15 +00:00
private void ImportQRToView(string url)
{
byte[] data = QR.GetQRData(url);
if (data.Length == 0)
return;
string[] types = mga.Gifts.Select(g => g.Type).Distinct().ToArray();
MysteryGift gift = MysteryGift.GetMysteryGift(data);
string giftType = gift.Type;
if (mga.Gifts.All(card => card.Data.Length != data.Length))
WinFormsUtil.Alert(MsgMysteryGiftQRTypeLength, string.Format(MsgQRDecodeSize, $"0x{data.Length:X}"));
else if (types.All(type => type != giftType))
2018-07-29 23:39:15 +00:00
WinFormsUtil.Alert(MsgMysteryGiftTypeIncompatible, $"{MsgMysteryGiftQRRecieved} {gift.Type}{Environment.NewLine}{MsgMysteryGiftTypeUnexpected} {string.Join(", ", types)}");
else if (!SAV.CanRecieveGift(gift))
WinFormsUtil.Alert(MsgMysteryGiftTypeDetails);
else
ViewGiftData(gift);
}
private void BoxSlot_MouseDown(object sender, MouseEventArgs e)
{
switch (ModifierKeys)
{
case Keys.Control: ClickView(sender, e); return;
case Keys.Shift: ClickSet(sender, e); return;
case Keys.Alt: ClickDelete(sender, e); return;
}
2018-07-29 23:39:15 +00:00
var pb = sender as PictureBox;
if (pb?.Image == null)
return;
if (e.Button != MouseButtons.Left || e.Clicks != 1) return;
2016-02-01 07:15:54 +00:00
int index = Array.IndexOf(pba, sender);
2015-12-19 06:17:01 +00:00
wc_slot = index;
// Create Temp File to Drag
Cursor.Current = Cursors.Hand;
// Make File
2018-07-29 23:39:15 +00:00
var gift = mga.Gifts[index];
string newfile = Path.Combine(Path.GetTempPath(), Util.CleanFileName(gift.FileName));
try
{
2018-07-29 23:39:15 +00:00
File.WriteAllBytes(newfile, gift.Data);
DoDragDrop(new DataObject(DataFormats.FileDrop, new[] { newfile }), DragDropEffects.Move);
}
catch (Exception x)
{ WinFormsUtil.Error("Drag & Drop Error", x); }
File.Delete(newfile);
2015-12-19 06:17:01 +00:00
wc_slot = -1;
}
2018-07-29 23:39:15 +00:00
private void BoxSlot_DragDrop(object sender, DragEventArgs e)
{
2016-02-01 07:15:54 +00:00
int index = Array.IndexOf(pba, sender);
// Hijack to the latest unfilled slot if index creates interstitial empty slots.
int lastUnfilled = GetLastUnfilledByType(mg, mga);
if (lastUnfilled > -1 && lastUnfilled < index && mga.Gifts[lastUnfilled].Type == mga.Gifts[index].Type)
index = lastUnfilled;
2018-05-12 15:13:39 +00:00
if (wc_slot == -1) // dropped
2015-12-19 06:17:01 +00:00
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length < 1)
2015-12-19 06:17:01 +00:00
return;
2018-07-29 23:39:15 +00:00
if (!MysteryGift.IsMysteryGift(new FileInfo(files[0]).Length))
{ WinFormsUtil.Alert(MsgFileUnsupported, files[0]); return; }
2018-05-12 15:13:39 +00:00
byte[] data = File.ReadAllBytes(files[0]);
MysteryGift gift = MysteryGift.GetMysteryGift(data, new FileInfo(files[0]).Extension);
2018-05-13 02:07:32 +00:00
if (gift is PCD pcd && mga.Gifts[index] is PGT)
2018-07-29 23:39:15 +00:00
{
2018-05-13 02:07:32 +00:00
gift = pcd.Gift;
2018-07-29 23:39:15 +00:00
}
else if (gift.Type != mga.Gifts[index].Type)
{
WinFormsUtil.Alert(MsgMysteryGiftSlotFail, $"{gift.Type} != {mga.Gifts[index].Type}");
return;
}
SetBackground(index, Properties.Resources.slotSet);
mga.Gifts[index] = gift.Clone();
2018-05-12 15:13:39 +00:00
SetCardID(mga.Gifts[index].CardID);
ViewGiftData(mga.Gifts[index]);
2015-12-19 06:17:01 +00:00
}
else // Swap Data
{
MysteryGift s1 = mga.Gifts[index];
MysteryGift s2 = mga.Gifts[wc_slot];
if (s2 is PCD && s1 is PGT)
{
// set the PGT to the PGT slot instead
ViewGiftData(s2);
ClickSet(pba[index], null);
{ WinFormsUtil.Alert(string.Format(MsgMysteryGiftSlotAlternate, s2.Type, s1.Type)); return; }
}
if (s1.Type != s2.Type)
{ WinFormsUtil.Alert(string.Format(MsgMysteryGiftSlotFailSwap, s2.Type, s1.Type)); return; }
mga.Gifts[wc_slot] = s1;
mga.Gifts[index] = s2;
if (mga.Gifts[wc_slot].Empty) // empty slot created, slide down
{
int i = wc_slot;
while (i < index)
{
if (mga.Gifts[i + 1].Empty)
break;
if (mga.Gifts[i + 1].Type != mga.Gifts[i].Type)
break;
i++;
var mg1 = mga.Gifts[i];
var mg2 = mga.Gifts[i - 1];
mga.Gifts[i - 1] = mg1;
mga.Gifts[i] = mg2;
}
index = i-1;
}
2015-12-19 06:17:01 +00:00
}
SetBackground(index, Properties.Resources.slotView);
SetGiftBoxes();
}
2018-07-29 23:39:15 +00:00
private static void BoxSlot_DragEnter(object sender, DragEventArgs e)
{
if (e.AllowedEffect == (DragDropEffects.Copy | DragDropEffects.Link)) // external file
e.Effect = DragDropEffects.Copy;
else if (e.Data != null) // within
e.Effect = DragDropEffects.Move;
}
2018-07-29 23:39:15 +00:00
2015-12-19 06:17:01 +00:00
private int wc_slot = -1;
// UI Generation
private List<PictureBox> PopulateViewGiftsG4()
{
List<PictureBox> pb = new List<PictureBox>();
// Row 1
var f1 = GetFlowLayoutPanel();
f1.Controls.Add(GetLabel($"{nameof(PGT)} 1-6"));
for (int i = 0; i < 6; i++)
{
var p = GetPictureBox();
f1.Controls.Add(p);
pb.Add(p);
}
// Row 2
var f2 = GetFlowLayoutPanel();
f2.Controls.Add(GetLabel($"{nameof(PGT)} 7-8"));
for (int i = 6; i < 8; i++)
{
var p = GetPictureBox();
f2.Controls.Add(p);
pb.Add(p);
}
// Row 3
var f3 = GetFlowLayoutPanel();
f3.Margin = new Padding(0, f3.Height, 0, 0);
f3.Controls.Add(GetLabel($"{nameof(PCD)} 1-3"));
for (int i = 8; i < 11; i++)
{
var p = GetPictureBox();
f3.Controls.Add(p);
pb.Add(p);
}
FLP_Gifts.Controls.Add(f1);
FLP_Gifts.Controls.Add(f2);
FLP_Gifts.Controls.Add(f3);
return pb;
}
2018-07-29 23:39:15 +00:00
private List<PictureBox> PopulateViewGiftsG567()
{
2018-07-29 23:39:15 +00:00
var pb = new List<PictureBox>();
const int cellsPerRow = 6;
int rows = (int)Math.Ceiling(mga.Gifts.Length / (decimal)cellsPerRow);
int countRemaining = mga.Gifts.Length;
for (int i = 0; i < rows; i++)
{
var row = GetFlowLayoutPanel();
int count = cellsPerRow >= countRemaining ? countRemaining : cellsPerRow;
countRemaining -= count;
int start = (i * cellsPerRow) + 1;
row.Controls.Add(GetLabel($"{start}-{start + count - 1}"));
for (int j = 0; j < count; j++)
{
var p = GetPictureBox();
row.Controls.Add(p);
pb.Add(p);
}
FLP_Gifts.Controls.Add(row);
}
return pb;
}
2018-07-29 23:39:15 +00:00
private static FlowLayoutPanel GetFlowLayoutPanel()
{
return new FlowLayoutPanel
{
Width = 305,
Height = 34,
Padding = new Padding(0),
Margin = new Padding(0),
};
}
2018-07-29 23:39:15 +00:00
private static Label GetLabel(string text)
{
return new Label
{
Size = new Size(40, 34),
AutoSize = false,
TextAlign = ContentAlignment.MiddleRight,
Text = text,
Padding = new Padding(0),
Margin = new Padding(0),
};
}
2018-07-29 23:39:15 +00:00
private static PictureBox GetPictureBox()
{
return new PictureBox
{
Size = new Size(42, 32),
SizeMode = PictureBoxSizeMode.CenterImage,
BorderStyle = BorderStyle.FixedSingle,
BackColor = Color.Transparent,
Padding = new Padding(0),
Margin = new Padding(1),
};
}
private void B_ModifyAll_Click(object sender, EventArgs e)
{
foreach (var g in mga.Gifts)
g.GiftUsed = sender == B_UsedAll;
SetGiftBoxes();
System.Media.SystemSounds.Asterisk.Play();
}
}
}