2017-05-23 04:55:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-11-16 01:34:18 +00:00
|
|
|
|
using System.Drawing;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using PKHeX.Core;
|
2019-09-29 16:47:06 +00:00
|
|
|
|
using PKHeX.Drawing;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
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
|
|
|
|
{
|
2020-10-18 18:02:39 +00:00
|
|
|
|
public IList<PictureBox> SlotPictureBoxes { get; private set; } = Array.Empty<PictureBox>();
|
|
|
|
|
public SaveFile SAV => M?.SE.SAV ?? throw new ArgumentNullException(nameof(SAV));
|
2019-09-03 02:30:58 +00:00
|
|
|
|
|
2019-10-03 03:04:12 +00:00
|
|
|
|
public int BoxSlotCount { get; private set; }
|
2020-10-18 18:02:39 +00:00
|
|
|
|
public SlotChangeManager? M { get; set; }
|
2017-06-19 05:27:40 +00:00
|
|
|
|
public bool FlagIllegal { get; set; }
|
2018-11-21 07:53:17 +00:00
|
|
|
|
public bool CanSetCurrentBox { get; set; }
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
2020-10-18 18:02:39 +00:00
|
|
|
|
public BoxEdit Editor { get; set; } = null!;
|
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public BoxEditor()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2019-10-03 03:04:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool InitializeGrid()
|
|
|
|
|
{
|
|
|
|
|
var count = SAV.BoxSlotCount;
|
|
|
|
|
var width = count / 5;
|
|
|
|
|
var height = count / width;
|
2019-11-16 01:34:18 +00:00
|
|
|
|
if (!BoxPokeGrid.InitializeGrid(width, height, SpriteUtil.Spriter))
|
|
|
|
|
return false;
|
|
|
|
|
RecenterControls();
|
|
|
|
|
InitializeSlots();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RecenterControls()
|
|
|
|
|
{
|
2019-11-23 16:51:54 +00:00
|
|
|
|
if (Width < BoxPokeGrid.Width)
|
|
|
|
|
Width = BoxPokeGrid.Width;
|
2019-11-16 01:34:18 +00:00
|
|
|
|
BoxPokeGrid.HorizontallyCenter(this);
|
|
|
|
|
int p1 = CB_BoxSelect.Location.X;
|
|
|
|
|
CB_BoxSelect.HorizontallyCenter(this);
|
|
|
|
|
int p2 = CB_BoxSelect.Location.X;
|
|
|
|
|
if (p1 == p2)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
B_BoxLeft.Location = new Point(B_BoxLeft.Location.X + p2 - p1, B_BoxLeft.Location.Y);
|
|
|
|
|
B_BoxRight.Location = new Point(B_BoxRight.Location.X + p2 - p1, B_BoxRight.Location.Y);
|
2019-10-03 03:04:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeSlots()
|
|
|
|
|
{
|
2019-11-16 01:34:18 +00:00
|
|
|
|
SlotPictureBoxes = BoxPokeGrid.Entries;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
BoxSlotCount = SlotPictureBoxes.Count;
|
|
|
|
|
foreach (var pb in SlotPictureBoxes)
|
|
|
|
|
{
|
2020-10-18 18:02:39 +00:00
|
|
|
|
pb.MouseEnter += (o, args) => BoxSlot_MouseEnter(pb, args);
|
|
|
|
|
pb.MouseLeave += (o, args) => BoxSlot_MouseLeave(pb, args);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-03 02:30:58 +00:00
|
|
|
|
public void NotifySlotOld(ISlotInfo previous)
|
|
|
|
|
{
|
|
|
|
|
if (!(previous is SlotInfoBox b) || b.Box != CurrentBox)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var pb = SlotPictureBoxes[previous.Slot];
|
|
|
|
|
pb.BackgroundImage = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void NotifySlotChanged(ISlotInfo slot, SlotTouchType type, PKM pkm)
|
|
|
|
|
{
|
|
|
|
|
int index = GetViewIndex(slot);
|
|
|
|
|
if (index < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var pb = SlotPictureBoxes[index];
|
|
|
|
|
SlotUtil.UpdateSlot(pb, slot, pkm, SAV, FlagIllegal, type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetViewIndex(ISlotInfo slot)
|
|
|
|
|
{
|
|
|
|
|
if (!(slot is SlotInfoBox b) || b.Box != CurrentBox)
|
|
|
|
|
return -1;
|
|
|
|
|
return slot.Slot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ISlotInfo GetSlotData(PictureBox view)
|
2018-05-05 15:07:22 +00:00
|
|
|
|
{
|
|
|
|
|
int slot = GetSlot(view);
|
2019-09-03 02:30:58 +00:00
|
|
|
|
return new SlotInfoBox(ViewIndex, slot);
|
2018-05-05 15:07:22 +00:00
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2019-09-03 02:30:58 +00:00
|
|
|
|
private int GetSlot(PictureBox sender) => SlotPictureBoxes.IndexOf(sender);
|
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;
|
2019-08-21 02:50:28 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
CB_BoxSelect.SelectedIndex = value;
|
|
|
|
|
if (value < 0)
|
|
|
|
|
return;
|
|
|
|
|
Editor.LoadBox(value);
|
|
|
|
|
}
|
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 string CurrentBoxName => CB_BoxSelect.Text;
|
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
|
|
|
|
|
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;
|
2020-09-19 05:11:13 +00:00
|
|
|
|
|
|
|
|
|
CB_BoxSelect.Items.Clear();
|
|
|
|
|
CB_BoxSelect.Items.AddRange(BoxUtil.GetBoxNames(SAV));
|
2017-11-18 06:19:23 +00:00
|
|
|
|
|
2019-09-03 03:25:38 +00:00
|
|
|
|
if (box < 0 && (uint)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-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()
|
|
|
|
|
{
|
2019-09-10 03:23:18 +00:00
|
|
|
|
Editor.Reload();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
int box = CurrentBox;
|
2019-11-16 01:34:18 +00:00
|
|
|
|
BoxPokeGrid.SetBackground(SAV.WallpaperImage(box));
|
2020-10-18 18:02:39 +00:00
|
|
|
|
M?.Hover.Stop();
|
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-09-04 01:44:35 +00:00
|
|
|
|
if (i >= SAV.BoxSlotCount || index + i >= SAV.SlotCount)
|
|
|
|
|
{
|
2017-05-23 04:55:05 +00:00
|
|
|
|
pb.Visible = false;
|
2019-09-04 01:44:35 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
pb.Visible = true;
|
|
|
|
|
SlotUtil.UpdateSlot(pb, (SlotInfoBox) GetSlotData(pb), Editor[i], SAV, FlagIllegal);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2019-09-03 02:30:58 +00:00
|
|
|
|
|
2020-10-18 18:02:39 +00:00
|
|
|
|
if (M?.Env.Slots.Publisher.Previous is SlotInfoBox b && b.Box == CurrentBox)
|
2019-09-03 02:30:58 +00:00
|
|
|
|
SlotPictureBoxes[b.Slot].BackgroundImage = SlotUtil.GetTouchTypeBackground(M.Env.Slots.Publisher.PreviousType);
|
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-10-08 01:40:09 +00:00
|
|
|
|
using 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;
|
2019-10-04 00:45:19 +00:00
|
|
|
|
File.WriteAllBytes(sfd.FileName, SAV.GetPCBinary());
|
2017-05-23 04:55:05 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (dr == DialogResult.No)
|
|
|
|
|
{
|
2019-10-08 01:40:09 +00:00
|
|
|
|
using 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()
|
|
|
|
|
{
|
2020-10-18 18:02:39 +00:00
|
|
|
|
#pragma warning disable CS8622 // Nullability of reference types in type of parameter doesn't match the target delegate.
|
2018-01-24 06:37:55 +00:00
|
|
|
|
B_BoxRight.Click -= ClickBoxRight;
|
|
|
|
|
B_BoxLeft.Click -= ClickBoxLeft;
|
|
|
|
|
CB_BoxSelect.SelectedIndexChanged -= GetBox;
|
2020-10-18 18:02:39 +00:00
|
|
|
|
#pragma warning restore CS8622 // Nullability of reference types in type of parameter doesn't match the target delegate.
|
2018-01-24 06:37:55 +00:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2019-08-21 02:50:28 +00:00
|
|
|
|
CurrentBox = CB_BoxSelect.SelectedIndex;
|
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();
|
2019-08-21 02:50:28 +00:00
|
|
|
|
M?.Hover.Stop();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2019-08-21 02:50:28 +00:00
|
|
|
|
private void ClickBoxLeft(object sender, EventArgs e) => CurrentBox = Editor.MoveLeft(ModifierKeys == Keys.Control);
|
|
|
|
|
private void ClickBoxRight(object sender, EventArgs e) => CurrentBox = Editor.MoveRight(ModifierKeys == Keys.Control);
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
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);
|
2019-08-21 02:50:28 +00:00
|
|
|
|
|
2019-10-03 03:04:12 +00:00
|
|
|
|
public bool InitializeFromSAV(SaveFile sav)
|
2019-08-21 02:50:28 +00:00
|
|
|
|
{
|
|
|
|
|
Editor = new BoxEdit(sav);
|
2019-10-03 03:04:12 +00:00
|
|
|
|
bool result = InitializeGrid();
|
|
|
|
|
|
2019-08-24 01:03:10 +00:00
|
|
|
|
int box = sav.CurrentBox;
|
|
|
|
|
if ((uint)box >= sav.BoxCount)
|
|
|
|
|
box = 0;
|
|
|
|
|
Editor.LoadBox(box);
|
2019-08-21 02:50:28 +00:00
|
|
|
|
ResetBoxNames(); // Display the Box Names
|
2019-10-03 03:04:12 +00:00
|
|
|
|
return result;
|
2019-08-21 02:50:28 +00:00
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|