2017-05-23 04:55:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using PKHeX.Core;
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2018-05-05 15:07:22 +00:00
|
|
|
|
public partial class BoxEditor : UserControl, ISlotViewer<PictureBox>
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
private SaveFile SAV => M?.SE.SAV;
|
2018-05-05 15:07:22 +00:00
|
|
|
|
|
|
|
|
|
public IList<PictureBox> SlotPictureBoxes { get; }
|
2017-06-19 05:27:40 +00:00
|
|
|
|
public int BoxSlotCount { get; }
|
|
|
|
|
public SlotChangeManager M { get; set; }
|
|
|
|
|
public bool FlagIllegal { get; set; }
|
2018-11-21 07:53:17 +00:00
|
|
|
|
public bool CanSetCurrentBox { get; set; }
|
2017-12-17 02:29:10 +00:00
|
|
|
|
private const int SlotCount = 30;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
public BoxEditor()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2018-02-22 04:35:07 +00:00
|
|
|
|
SlotPictureBoxes = new List<PictureBox>
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
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,
|
2018-02-22 04:35:07 +00:00
|
|
|
|
};
|
2017-05-23 04:55:05 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 15:07:22 +00:00
|
|
|
|
public SlotChange GetSlotData(PictureBox view)
|
|
|
|
|
{
|
|
|
|
|
int slot = GetSlot(view);
|
|
|
|
|
return new SlotChange
|
|
|
|
|
{
|
|
|
|
|
Slot = GetSlot(view),
|
|
|
|
|
Box = ViewIndex,
|
|
|
|
|
Offset = GetSlotOffset(slot),
|
|
|
|
|
Type = StorageSlotType.Box,
|
|
|
|
|
IsPartyFormat = false,
|
|
|
|
|
Editable = true,
|
|
|
|
|
Parent = FindForm(),
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2018-05-05 15:07:22 +00:00
|
|
|
|
private int GetSlot(PictureBox sender) => SlotPictureBoxes.IndexOf(WinFormsUtil.GetUnderlyingControl(sender) as PictureBox);
|
2018-07-07 18:20:05 +00:00
|
|
|
|
public int GetSlotOffset(int box, int slot) => GetOffset(slot, box);
|
|
|
|
|
public int GetSlotOffset(int slot) => GetSlotOffset(CurrentBox, slot);
|
2018-05-05 15:07:22 +00:00
|
|
|
|
public int ViewIndex => CurrentBox;
|
|
|
|
|
|
2017-11-18 06:19:23 +00:00
|
|
|
|
public bool ControlsVisible
|
|
|
|
|
{
|
|
|
|
|
get => CB_BoxSelect.Enabled;
|
|
|
|
|
set => CB_BoxSelect.Enabled = CB_BoxSelect.Visible = B_BoxLeft.Visible = B_BoxRight.Visible = value;
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2018-01-23 05:38:42 +00:00
|
|
|
|
public bool ControlsEnabled
|
|
|
|
|
{
|
|
|
|
|
get => CB_BoxSelect.Enabled;
|
|
|
|
|
set => CB_BoxSelect.Enabled = B_BoxLeft.Enabled = B_BoxRight.Enabled = value;
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public int CurrentBox
|
|
|
|
|
{
|
|
|
|
|
get => CB_BoxSelect.SelectedIndex;
|
|
|
|
|
set => CB_BoxSelect.SelectedIndex = value;
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public string CurrentBoxName => CB_BoxSelect.Text;
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
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;
|
2018-08-03 03:11:42 +00:00
|
|
|
|
return SAV.GetBoxOffset(box) + (slot * SAV.SIZE_STORED);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public void Setup(SlotChangeManager m)
|
|
|
|
|
{
|
|
|
|
|
M = m;
|
|
|
|
|
M.Boxes.Add(this);
|
|
|
|
|
FlagIllegal = M.SE.FlagIllegal;
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
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;
|
|
|
|
|
|
2018-01-30 01:52:21 +00:00
|
|
|
|
if (M != null && M.ColorizedBox == box && M.ColorizedSlot == slot)
|
|
|
|
|
pb.BackgroundImage = M.ColorizedColor;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 06:37:55 +00:00
|
|
|
|
public void ResetBoxNames(int box = -1)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (!SAV.HasBox)
|
|
|
|
|
return;
|
2017-11-18 06:19:23 +00:00
|
|
|
|
if (!SAV.Exportable)
|
2018-08-13 02:27:11 +00:00
|
|
|
|
{
|
2017-11-18 06:19:23 +00:00
|
|
|
|
getBoxNamesDefault();
|
2018-08-13 02:27:11 +00:00
|
|
|
|
}
|
2017-11-18 06:19:23 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try { getBoxNamesFromSave(); }
|
|
|
|
|
catch { getBoxNamesDefault(); }
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 06:37:55 +00:00
|
|
|
|
if (box < 0 && SAV.CurrentBox < CB_BoxSelect.Items.Count)
|
2017-11-18 06:19:23 +00:00
|
|
|
|
CurrentBox = SAV.CurrentBox; // restore selected box
|
2018-01-24 06:37:55 +00:00
|
|
|
|
else
|
|
|
|
|
CurrentBox = box;
|
2017-11-18 06:19:23 +00:00
|
|
|
|
|
|
|
|
|
void getBoxNamesFromSave()
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
2017-11-18 06:19:23 +00:00
|
|
|
|
void getBoxNamesDefault()
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
CB_BoxSelect.Items.Clear();
|
2018-01-26 02:45:11 +00:00
|
|
|
|
for (int i = 0; i < SAV.BoxCount; i++)
|
2017-11-18 06:19:23 +00:00
|
|
|
|
CB_BoxSelect.Items.Add($"Box {i+1}");
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
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);
|
2018-07-26 02:50:44 +00:00
|
|
|
|
M?.HoverWorker?.Stop();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
2018-01-30 01:52:21 +00:00
|
|
|
|
int slot = M?.ColorizedBox == box ? M.ColorizedSlot : -1;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
2019-01-15 05:31:53 +00:00
|
|
|
|
int index = box * SAV.BoxSlotCount;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
for (int i = 0; i < BoxSlotCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var pb = SlotPictureBoxes[i];
|
2019-01-15 05:31:53 +00:00
|
|
|
|
if (i < SAV.BoxSlotCount && index + i < SAV.SlotCount)
|
2018-08-03 03:11:42 +00:00
|
|
|
|
GetSlotFiller(boxoffset + (SAV.SIZE_STORED * i), pb, box, i);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
else
|
|
|
|
|
pb.Visible = false;
|
2018-01-30 01:52:21 +00:00
|
|
|
|
pb.BackgroundImage = slot == i ? M?.ColorizedColor : null;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public bool SaveBoxBinary()
|
|
|
|
|
{
|
2019-02-04 04:28:03 +00:00
|
|
|
|
var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNoCancel,
|
2018-04-07 04:23:09 +00:00
|
|
|
|
MsgSaveBoxExportYes + Environment.NewLine +
|
|
|
|
|
string.Format(MsgSaveBoxExportNo, CurrentBoxName, CurrentBox + 1) + Environment.NewLine +
|
|
|
|
|
MsgSaveBoxExportCancel);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
if (dr == DialogResult.Yes)
|
|
|
|
|
{
|
2019-02-04 04:28:03 +00:00
|
|
|
|
var sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = "pcdata.bin" };
|
2017-05-23 04:55:05 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2019-02-04 04:28:03 +00:00
|
|
|
|
var sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = $"boxdata {CurrentBoxName}.bin" };
|
2017-05-23 04:55:05 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2018-01-24 06:37:55 +00:00
|
|
|
|
public void ClearEvents()
|
|
|
|
|
{
|
|
|
|
|
B_BoxRight.Click -= ClickBoxRight;
|
|
|
|
|
B_BoxLeft.Click -= ClickBoxLeft;
|
|
|
|
|
CB_BoxSelect.SelectedIndexChanged -= GetBox;
|
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
2018-12-26 21:06:58 +00:00
|
|
|
|
public void Reset()
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
ResetBoxNames();
|
|
|
|
|
ResetSlots();
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void GetBox(object sender, EventArgs e)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2018-11-21 07:53:17 +00:00
|
|
|
|
if (SAV.CurrentBox != CurrentBox && CanSetCurrentBox)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
SAV.CurrentBox = CurrentBox;
|
|
|
|
|
ResetSlots();
|
2018-05-13 15:14:46 +00:00
|
|
|
|
M?.RefreshHoverSlot(this);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2018-07-26 02:29:41 +00:00
|
|
|
|
private void ClickBoxLeft(object sender, EventArgs e) => MoveLeft(ModifierKeys == Keys.Control);
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2018-07-26 02:29:41 +00:00
|
|
|
|
public void MoveLeft(bool max = false)
|
2017-07-06 01:54:21 +00:00
|
|
|
|
{
|
2018-07-26 02:29:41 +00:00
|
|
|
|
CurrentBox = max ? 0 : (CurrentBox + SAV.BoxCount - 1) % SAV.BoxCount;
|
2017-07-06 01:54:21 +00:00
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2018-07-26 02:29:41 +00:00
|
|
|
|
private void ClickBoxRight(object sender, EventArgs e) => MoveRight(ModifierKeys == Keys.Control);
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2018-07-26 02:29:41 +00:00
|
|
|
|
public void MoveRight(bool max = false)
|
2017-07-06 01:54:21 +00:00
|
|
|
|
{
|
2018-07-26 02:29:41 +00:00
|
|
|
|
CurrentBox = max ? SAV.BoxCount - 1 : (CurrentBox + 1) % SAV.BoxCount;
|
2017-07-06 01:54:21 +00:00
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
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
|
|
|
|
{
|
2018-03-18 23:22:21 +00:00
|
|
|
|
if (!SAV.IsPKMPresent(offset))
|
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);
|
2018-05-11 23:38:09 +00:00
|
|
|
|
private void BoxSlot_MouseMove(object sender, MouseEventArgs e) => M?.MouseMove(sender, e);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void BoxSlot_DragEnter(object sender, DragEventArgs e) => M?.DragEnter(sender, e);
|
|
|
|
|
private void BoxSlot_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) => M?.QueryContinueDrag(sender, e);
|
2018-05-11 23:38:09 +00:00
|
|
|
|
private void BoxSlot_DragDrop(object sender, DragEventArgs e) => M?.DragDrop(sender, e);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|