2017-05-22 21:55:05 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-11-15 17:34:18 -08:00
|
|
|
|
using System.Drawing;
|
2017-05-22 21:55:05 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using PKHeX.Core;
|
2019-09-29 09:47:06 -07:00
|
|
|
|
using PKHeX.Drawing;
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
2018-04-06 21:23:09 -07:00
|
|
|
|
using static PKHeX.Core.MessageStrings;
|
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
namespace PKHeX.WinForms.Controls
|
|
|
|
|
{
|
2018-05-05 08:07:22 -07:00
|
|
|
|
public partial class BoxEditor : UserControl, ISlotViewer<PictureBox>
|
2017-05-22 21:55:05 -07:00
|
|
|
|
{
|
2019-10-02 20:04:12 -07:00
|
|
|
|
public IList<PictureBox> SlotPictureBoxes { get; private set; }
|
2019-09-02 19:30:58 -07:00
|
|
|
|
public SaveFile SAV => M?.SE.SAV;
|
|
|
|
|
|
2019-10-02 20:04:12 -07:00
|
|
|
|
public int BoxSlotCount { get; private set; }
|
2017-06-18 22:27:40 -07:00
|
|
|
|
public SlotChangeManager M { get; set; }
|
|
|
|
|
public bool FlagIllegal { get; set; }
|
2018-11-20 23:53:17 -08:00
|
|
|
|
public bool CanSetCurrentBox { get; set; }
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
|
|
|
|
public BoxEditor()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2019-10-02 20:04:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool InitializeGrid()
|
|
|
|
|
{
|
|
|
|
|
var count = SAV.BoxSlotCount;
|
|
|
|
|
var width = count / 5;
|
|
|
|
|
var height = count / width;
|
2019-11-15 17:34:18 -08:00
|
|
|
|
if (!BoxPokeGrid.InitializeGrid(width, height, SpriteUtil.Spriter))
|
|
|
|
|
return false;
|
|
|
|
|
RecenterControls();
|
|
|
|
|
InitializeSlots();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RecenterControls()
|
|
|
|
|
{
|
2019-11-23 08:51:54 -08:00
|
|
|
|
if (Width < BoxPokeGrid.Width)
|
|
|
|
|
Width = BoxPokeGrid.Width;
|
2019-11-15 17:34:18 -08: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-02 20:04:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeSlots()
|
|
|
|
|
{
|
2019-11-15 17:34:18 -08:00
|
|
|
|
SlotPictureBoxes = BoxPokeGrid.Entries;
|
2017-05-22 21:55:05 -07:00
|
|
|
|
BoxSlotCount = SlotPictureBoxes.Count;
|
|
|
|
|
foreach (var pb in SlotPictureBoxes)
|
|
|
|
|
{
|
2017-06-17 18:37:19 -07: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-05 23:05:49 -07:00
|
|
|
|
pb.GiveFeedback += (sender, e) => e.UseDefaultCursors = false;
|
2017-05-22 21:55:05 -07:00
|
|
|
|
pb.AllowDrop = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 19:30:58 -07: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 08:07:22 -07:00
|
|
|
|
{
|
|
|
|
|
int slot = GetSlot(view);
|
2019-09-02 19:30:58 -07:00
|
|
|
|
return new SlotInfoBox(ViewIndex, slot);
|
2018-05-05 08:07:22 -07:00
|
|
|
|
}
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2019-09-02 19:30:58 -07:00
|
|
|
|
private int GetSlot(PictureBox sender) => SlotPictureBoxes.IndexOf(sender);
|
2018-05-05 08:07:22 -07:00
|
|
|
|
public int ViewIndex => CurrentBox;
|
|
|
|
|
|
2017-11-17 22:19:23 -08:00
|
|
|
|
public bool ControlsVisible
|
|
|
|
|
{
|
|
|
|
|
get => CB_BoxSelect.Enabled;
|
|
|
|
|
set => CB_BoxSelect.Enabled = CB_BoxSelect.Visible = B_BoxLeft.Visible = B_BoxRight.Visible = value;
|
|
|
|
|
}
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2018-01-22 21:38:42 -08:00
|
|
|
|
public bool ControlsEnabled
|
|
|
|
|
{
|
|
|
|
|
get => CB_BoxSelect.Enabled;
|
|
|
|
|
set => CB_BoxSelect.Enabled = B_BoxLeft.Enabled = B_BoxRight.Enabled = value;
|
|
|
|
|
}
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
public int CurrentBox
|
|
|
|
|
{
|
|
|
|
|
get => CB_BoxSelect.SelectedIndex;
|
2019-08-20 19:50:28 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
CB_BoxSelect.SelectedIndex = value;
|
|
|
|
|
if (value < 0)
|
|
|
|
|
return;
|
|
|
|
|
Editor.LoadBox(value);
|
|
|
|
|
}
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
public string CurrentBoxName => CB_BoxSelect.Text;
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
public void Setup(SlotChangeManager m)
|
|
|
|
|
{
|
|
|
|
|
M = m;
|
|
|
|
|
M.Boxes.Add(this);
|
|
|
|
|
FlagIllegal = M.SE.FlagIllegal;
|
|
|
|
|
}
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2018-01-23 22:37:55 -08:00
|
|
|
|
public void ResetBoxNames(int box = -1)
|
2017-05-22 21:55:05 -07:00
|
|
|
|
{
|
|
|
|
|
if (!SAV.HasBox)
|
|
|
|
|
return;
|
2020-09-18 22:11:13 -07:00
|
|
|
|
|
|
|
|
|
CB_BoxSelect.Items.Clear();
|
|
|
|
|
CB_BoxSelect.Items.AddRange(BoxUtil.GetBoxNames(SAV));
|
2017-11-17 22:19:23 -08:00
|
|
|
|
|
2019-09-02 20:25:38 -07:00
|
|
|
|
if (box < 0 && (uint)SAV.CurrentBox < CB_BoxSelect.Items.Count)
|
2017-11-17 22:19:23 -08:00
|
|
|
|
CurrentBox = SAV.CurrentBox; // restore selected box
|
2018-01-23 22:37:55 -08:00
|
|
|
|
else
|
|
|
|
|
CurrentBox = box;
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
public void ResetSlots()
|
|
|
|
|
{
|
2019-09-09 20:23:18 -07:00
|
|
|
|
Editor.Reload();
|
2017-05-22 21:55:05 -07:00
|
|
|
|
int box = CurrentBox;
|
2019-11-15 17:34:18 -08:00
|
|
|
|
BoxPokeGrid.SetBackground(SAV.WallpaperImage(box));
|
2019-09-02 19:30:58 -07:00
|
|
|
|
M.Hover.Stop();
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
2019-01-14 21:31:53 -08:00
|
|
|
|
int index = box * SAV.BoxSlotCount;
|
2017-05-22 21:55:05 -07:00
|
|
|
|
for (int i = 0; i < BoxSlotCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var pb = SlotPictureBoxes[i];
|
2019-09-03 18:44:35 -07:00
|
|
|
|
if (i >= SAV.BoxSlotCount || index + i >= SAV.SlotCount)
|
|
|
|
|
{
|
2017-05-22 21:55:05 -07:00
|
|
|
|
pb.Visible = false;
|
2019-09-03 18:44:35 -07:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
pb.Visible = true;
|
|
|
|
|
SlotUtil.UpdateSlot(pb, (SlotInfoBox) GetSlotData(pb), Editor[i], SAV, FlagIllegal);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2019-09-02 19:30:58 -07:00
|
|
|
|
|
|
|
|
|
if (M.Env.Slots.Publisher.Previous is SlotInfoBox b && b.Box == CurrentBox)
|
|
|
|
|
SlotPictureBoxes[b.Slot].BackgroundImage = SlotUtil.GetTouchTypeBackground(M.Env.Slots.Publisher.PreviousType);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2017-05-22 21:55:05 -07:00
|
|
|
|
public bool SaveBoxBinary()
|
|
|
|
|
{
|
2019-02-03 20:28:03 -08:00
|
|
|
|
var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNoCancel,
|
2018-04-06 21:23:09 -07:00
|
|
|
|
MsgSaveBoxExportYes + Environment.NewLine +
|
|
|
|
|
string.Format(MsgSaveBoxExportNo, CurrentBoxName, CurrentBox + 1) + Environment.NewLine +
|
|
|
|
|
MsgSaveBoxExportCancel);
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
|
|
|
|
if (dr == DialogResult.Yes)
|
|
|
|
|
{
|
2019-10-07 18:40:09 -07:00
|
|
|
|
using var sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = "pcdata.bin" };
|
2017-05-22 21:55:05 -07:00
|
|
|
|
if (sfd.ShowDialog() != DialogResult.OK)
|
|
|
|
|
return false;
|
2019-10-03 17:45:19 -07:00
|
|
|
|
File.WriteAllBytes(sfd.FileName, SAV.GetPCBinary());
|
2017-05-22 21:55:05 -07:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (dr == DialogResult.No)
|
|
|
|
|
{
|
2019-10-07 18:40:09 -07:00
|
|
|
|
using var sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = $"boxdata {CurrentBoxName}.bin" };
|
2017-05-22 21:55:05 -07:00
|
|
|
|
if (sfd.ShowDialog() != DialogResult.OK)
|
|
|
|
|
return false;
|
2017-06-17 18:37:19 -07:00
|
|
|
|
File.WriteAllBytes(sfd.FileName, SAV.GetBoxBinary(CurrentBox));
|
2017-05-22 21:55:05 -07:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2018-01-23 22:37:55 -08:00
|
|
|
|
public void ClearEvents()
|
|
|
|
|
{
|
|
|
|
|
B_BoxRight.Click -= ClickBoxRight;
|
|
|
|
|
B_BoxLeft.Click -= ClickBoxLeft;
|
|
|
|
|
CB_BoxSelect.SelectedIndexChanged -= GetBox;
|
|
|
|
|
}
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
2018-12-26 13:06:58 -08:00
|
|
|
|
public void Reset()
|
2017-05-22 21:55:05 -07:00
|
|
|
|
{
|
|
|
|
|
ResetBoxNames();
|
|
|
|
|
ResetSlots();
|
|
|
|
|
}
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2017-06-17 18:37:19 -07:00
|
|
|
|
private void GetBox(object sender, EventArgs e)
|
2017-05-22 21:55:05 -07:00
|
|
|
|
{
|
2019-08-20 19:50:28 -07:00
|
|
|
|
CurrentBox = CB_BoxSelect.SelectedIndex;
|
2018-11-20 23:53:17 -08:00
|
|
|
|
if (SAV.CurrentBox != CurrentBox && CanSetCurrentBox)
|
2017-05-22 21:55:05 -07:00
|
|
|
|
SAV.CurrentBox = CurrentBox;
|
|
|
|
|
ResetSlots();
|
2019-08-20 19:50:28 -07:00
|
|
|
|
M?.Hover.Stop();
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
2018-08-02 20:11:42 -07:00
|
|
|
|
|
2019-08-20 19:50:28 -07: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-02 20:11:42 -07:00
|
|
|
|
|
2019-08-20 19:50:28 -07:00
|
|
|
|
public BoxEdit Editor { get; set; }
|
2017-05-22 21:55:05 -07:00
|
|
|
|
|
|
|
|
|
// Drag & Drop Handling
|
2017-06-17 18:37:19 -07: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 16:38:09 -07:00
|
|
|
|
private void BoxSlot_MouseMove(object sender, MouseEventArgs e) => M?.MouseMove(sender, e);
|
2017-06-17 18:37:19 -07: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 16:38:09 -07:00
|
|
|
|
private void BoxSlot_DragDrop(object sender, DragEventArgs e) => M?.DragDrop(sender, e);
|
2019-08-20 19:50:28 -07:00
|
|
|
|
|
2019-10-02 20:04:12 -07:00
|
|
|
|
public bool InitializeFromSAV(SaveFile sav)
|
2019-08-20 19:50:28 -07:00
|
|
|
|
{
|
|
|
|
|
Editor = new BoxEdit(sav);
|
2019-10-02 20:04:12 -07:00
|
|
|
|
bool result = InitializeGrid();
|
|
|
|
|
|
2019-08-23 18:03:10 -07:00
|
|
|
|
int box = sav.CurrentBox;
|
|
|
|
|
if ((uint)box >= sav.BoxCount)
|
|
|
|
|
box = 0;
|
|
|
|
|
Editor.LoadBox(box);
|
2019-08-20 19:50:28 -07:00
|
|
|
|
ResetBoxNames(); // Display the Box Names
|
2019-10-02 20:04:12 -07:00
|
|
|
|
return result;
|
2019-08-20 19:50:28 -07:00
|
|
|
|
}
|
2017-05-22 21:55:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|