2017-05-23 04:55:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Media;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using PKHeX.Core;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms.Controls
|
|
|
|
|
{
|
|
|
|
|
public partial class BoxEditor : UserControl
|
|
|
|
|
{
|
|
|
|
|
private SaveFile SAV => M?.SE.SAV;
|
2017-06-19 05:27:40 +00:00
|
|
|
|
public List<PictureBox> SlotPictureBoxes { get; }
|
|
|
|
|
public int BoxSlotCount { get; }
|
|
|
|
|
public SlotChangeManager M { get; set; }
|
|
|
|
|
public bool FlagIllegal { get; set; }
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
public BoxEditor()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
SlotPictureBoxes = new List<PictureBox>();
|
|
|
|
|
SlotPictureBoxes.AddRange(new[]
|
|
|
|
|
{
|
|
|
|
|
bpkx1, bpkx2, bpkx3, bpkx4, bpkx5, bpkx6,
|
|
|
|
|
bpkx7, bpkx8, bpkx9, bpkx10,bpkx11,bpkx12,
|
|
|
|
|
bpkx13,bpkx14,bpkx15,bpkx16,bpkx17,bpkx18,
|
|
|
|
|
bpkx19,bpkx20,bpkx21,bpkx22,bpkx23,bpkx24,
|
|
|
|
|
bpkx25,bpkx26,bpkx27,bpkx28,bpkx29,bpkx30,
|
|
|
|
|
});
|
|
|
|
|
BoxSlotCount = SlotPictureBoxes.Count;
|
|
|
|
|
foreach (var pb in SlotPictureBoxes)
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
pb.MouseEnter += BoxSlot_MouseEnter;
|
|
|
|
|
pb.MouseLeave += BoxSlot_MouseLeave;
|
|
|
|
|
pb.MouseClick += BoxSlot_MouseClick;
|
|
|
|
|
pb.MouseMove += BoxSlot_MouseMove;
|
|
|
|
|
pb.MouseDown += BoxSlot_MouseDown;
|
|
|
|
|
pb.MouseUp += BoxSlot_MouseUp;
|
|
|
|
|
|
|
|
|
|
pb.DragEnter += BoxSlot_DragEnter;
|
|
|
|
|
pb.DragDrop += BoxSlot_DragDrop;
|
|
|
|
|
pb.QueryContinueDrag += BoxSlot_QueryContinueDrag;
|
2017-07-06 06:05:49 +00:00
|
|
|
|
pb.GiveFeedback += (sender, e) => e.UseDefaultCursors = false;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
pb.AllowDrop = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int CurrentBox
|
|
|
|
|
{
|
|
|
|
|
get => CB_BoxSelect.SelectedIndex;
|
|
|
|
|
set => CB_BoxSelect.SelectedIndex = value;
|
|
|
|
|
}
|
|
|
|
|
public string CurrentBoxName => CB_BoxSelect.Text;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public int GetOffset(int slot, int box)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (box < 0)
|
|
|
|
|
box = CurrentBox;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return SAV.GetBoxOffset(box) + slot * SAV.SIZE_STORED;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
public void Setup(SlotChangeManager m)
|
|
|
|
|
{
|
|
|
|
|
M = m;
|
|
|
|
|
M.Boxes.Add(this);
|
|
|
|
|
FlagIllegal = M.SE.FlagIllegal;
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public void SetSlotFiller(PKM p, int box = -1, int slot = -1, PictureBox pb = null)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (pb == null)
|
|
|
|
|
pb = SlotPictureBoxes[slot];
|
|
|
|
|
if (!p.Valid) // Invalid
|
|
|
|
|
{
|
|
|
|
|
// Bad Egg present in slot.
|
|
|
|
|
pb.Image = null;
|
|
|
|
|
pb.BackColor = Color.Red;
|
|
|
|
|
pb.Visible = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pb.Image = p.Sprite(SAV, box, slot, FlagIllegal);
|
|
|
|
|
pb.BackColor = Color.Transparent;
|
|
|
|
|
pb.Visible = true;
|
|
|
|
|
|
|
|
|
|
if (M != null && M.colorizedbox == box && M.colorizedslot == slot)
|
|
|
|
|
pb.BackgroundImage = M.colorizedcolor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetBoxNames()
|
|
|
|
|
{
|
|
|
|
|
if (!SAV.HasBox)
|
|
|
|
|
return;
|
|
|
|
|
// Build ComboBox Dropdown Items
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CB_BoxSelect.Items.Clear();
|
|
|
|
|
for (int i = 0; i < SAV.BoxCount; i++)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
CB_BoxSelect.Items.Add(SAV.GetBoxName(i));
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
CB_BoxSelect.Items.Clear();
|
|
|
|
|
for (int i = 1; i <= SAV.BoxCount; i++)
|
|
|
|
|
CB_BoxSelect.Items.Add($"BOX {i}");
|
|
|
|
|
}
|
|
|
|
|
if (SAV.CurrentBox < CB_BoxSelect.Items.Count)
|
|
|
|
|
CurrentBox = SAV.CurrentBox; // restore selected box
|
|
|
|
|
}
|
|
|
|
|
public void ResetSlots()
|
|
|
|
|
{
|
|
|
|
|
int box = CurrentBox;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int boxoffset = SAV.GetBoxOffset(box);
|
|
|
|
|
int boxbgval = SAV.GetBoxWallpaper(box);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
PAN_Box.BackgroundImage = SAV.WallpaperImage(boxbgval);
|
|
|
|
|
|
|
|
|
|
int slot = M?.colorizedbox == box ? M.colorizedslot : -1;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < BoxSlotCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var pb = SlotPictureBoxes[i];
|
|
|
|
|
if (i < SAV.BoxSlotCount)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
GetSlotFiller(boxoffset + SAV.SIZE_STORED * i, pb, box, i);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
else
|
|
|
|
|
pb.Visible = false;
|
|
|
|
|
pb.BackgroundImage = slot == i ? M?.colorizedcolor : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public bool SaveBoxBinary()
|
|
|
|
|
{
|
|
|
|
|
DialogResult dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNoCancel,
|
|
|
|
|
"Yes: Export All Boxes" + Environment.NewLine +
|
|
|
|
|
$"No: Export {CurrentBoxName} (Box {CurrentBox + 1})" + Environment.NewLine +
|
|
|
|
|
"Cancel: Abort");
|
|
|
|
|
|
|
|
|
|
if (dr == DialogResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
SaveFileDialog sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = "pcdata.bin" };
|
|
|
|
|
if (sfd.ShowDialog() != DialogResult.OK)
|
|
|
|
|
return false;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
File.WriteAllBytes(sfd.FileName, SAV.PCBinary);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (dr == DialogResult.No)
|
|
|
|
|
{
|
|
|
|
|
SaveFileDialog sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = $"boxdata {CurrentBoxName}.bin" };
|
|
|
|
|
if (sfd.ShowDialog() != DialogResult.OK)
|
|
|
|
|
return false;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
File.WriteAllBytes(sfd.FileName, SAV.GetBoxBinary(CurrentBox));
|
2017-05-23 04:55:05 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public int GetSlot(object sender) => SlotPictureBoxes.IndexOf(WinFormsUtil.GetUnderlyingControl(sender) as PictureBox);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
private void Reset()
|
|
|
|
|
{
|
|
|
|
|
ResetBoxNames();
|
|
|
|
|
ResetSlots();
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void GetBox(object sender, EventArgs e)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (SAV.CurrentBox != CurrentBox)
|
|
|
|
|
SAV.CurrentBox = CurrentBox;
|
|
|
|
|
ResetSlots();
|
|
|
|
|
}
|
2017-07-06 01:54:21 +00:00
|
|
|
|
private void ClickBoxLeft(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (ModifierKeys == Keys.Control)
|
|
|
|
|
CurrentBox = 0;
|
|
|
|
|
else
|
|
|
|
|
CurrentBox = (CurrentBox + SAV.BoxCount - 1) % SAV.BoxCount;
|
|
|
|
|
}
|
|
|
|
|
private void ClickBoxRight(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (ModifierKeys == Keys.Control)
|
|
|
|
|
CurrentBox = SAV.BoxCount - 1;
|
|
|
|
|
else
|
|
|
|
|
CurrentBox = (CurrentBox + 1) % SAV.BoxCount;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void GetSlotFiller(int offset, PictureBox pb, int box = -1, int slot = -1)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (SAV.GetData(offset, SAV.SIZE_STORED).SequenceEqual(new byte[SAV.SIZE_STORED]))
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
// 00s present in slot.
|
|
|
|
|
pb.Image = null;
|
|
|
|
|
pb.BackColor = Color.Transparent;
|
|
|
|
|
pb.Visible = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
PKM p = SAV.GetStoredSlot(offset);
|
|
|
|
|
SetSlotFiller(p, box, slot, pb);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Drag & Drop Handling
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void BoxSlot_MouseEnter(object sender, EventArgs e) => M?.MouseEnter(sender, e);
|
|
|
|
|
private void BoxSlot_MouseLeave(object sender, EventArgs e) => M?.MouseLeave(sender, e);
|
|
|
|
|
private void BoxSlot_MouseClick(object sender, MouseEventArgs e) => M?.MouseClick(sender, e);
|
|
|
|
|
private void BoxSlot_MouseUp(object sender, MouseEventArgs e) => M?.MouseUp(sender, e);
|
|
|
|
|
private void BoxSlot_MouseDown(object sender, MouseEventArgs e) => M?.MouseDown(sender, e);
|
|
|
|
|
private void BoxSlot_DragEnter(object sender, DragEventArgs e) => M?.DragEnter(sender, e);
|
|
|
|
|
private void BoxSlot_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) => M?.QueryContinueDrag(sender, e);
|
|
|
|
|
private void BoxSlot_MouseMove(object sender, MouseEventArgs e)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (M == null || M.DragActive)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Abort if there is no Pokemon in the given slot.
|
|
|
|
|
PictureBox pb = (PictureBox)sender;
|
|
|
|
|
if (pb.Image == null)
|
|
|
|
|
return;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int slot = GetSlot(pb);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
int box = slot >= 30 ? -1 : CurrentBox;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (SAV.IsSlotLocked(box, slot))
|
2017-05-23 04:55:05 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
bool encrypt = ModifierKeys == Keys.Control;
|
|
|
|
|
M.HandleMovePKM(pb, slot, box, encrypt);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void BoxSlot_DragDrop(object sender, DragEventArgs e)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (M == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Abort if there is no Pokemon in the given slot.
|
|
|
|
|
PictureBox pb = (PictureBox)sender;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int slot = GetSlot(pb);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
int box = slot >= 30 ? -1 : CurrentBox;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (SAV.IsSlotLocked(box, slot) || slot >= 36)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
SystemSounds.Asterisk.Play();
|
|
|
|
|
e.Effect = DragDropEffects.Copy;
|
|
|
|
|
M.DragInfo.Reset();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool overwrite = ModifierKeys == Keys.Alt;
|
|
|
|
|
bool clone = ModifierKeys == Keys.Control;
|
|
|
|
|
M.DragInfo.Destination.Parent = FindForm();
|
2017-06-18 01:37:19 +00:00
|
|
|
|
M.DragInfo.Destination.Slot = GetSlot(sender);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
M.DragInfo.Destination.Box = M.DragInfo.Destination.IsParty ? -1 : CurrentBox;
|
|
|
|
|
M.HandleDropPKM(sender, e, overwrite, clone);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|