2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2023-05-05 07:08:26 +00:00
|
|
|
using System.Collections;
|
2017-05-23 04:55:05 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using PKHeX.Core;
|
2021-11-27 23:48:08 +00:00
|
|
|
using PKHeX.Drawing.Misc;
|
|
|
|
using PKHeX.Drawing.PokeSprite;
|
2018-04-07 04:23:09 +00:00
|
|
|
using static PKHeX.Core.MessageStrings;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.WinForms.Controls;
|
2019-09-03 02:30:58 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public partial class BoxEditor : UserControl, ISlotViewer<PictureBox>
|
|
|
|
{
|
|
|
|
public IList<PictureBox> SlotPictureBoxes { get; private set; } = Array.Empty<PictureBox>();
|
|
|
|
public SaveFile SAV => M?.SE.SAV ?? throw new ArgumentNullException(nameof(SAV));
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public int BoxSlotCount { get; private set; }
|
|
|
|
public SlotChangeManager? M { get; set; }
|
|
|
|
public bool FlagIllegal { get; set; }
|
|
|
|
public bool CanSetCurrentBox { get; set; }
|
2020-10-18 18:02:39 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public BoxEdit Editor { get; set; } = null!;
|
2019-10-03 03:04:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public BoxEditor()
|
|
|
|
{
|
|
|
|
InitializeComponent();
|
|
|
|
}
|
2019-11-16 01:34:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
internal bool InitializeGrid()
|
|
|
|
{
|
|
|
|
var count = SAV.BoxSlotCount;
|
|
|
|
var width = count / 5;
|
|
|
|
var height = count / width;
|
|
|
|
if (!BoxPokeGrid.InitializeGrid(width, height, SpriteUtil.Spriter))
|
|
|
|
return false;
|
|
|
|
RecenterControls();
|
|
|
|
InitializeSlots();
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-16 01:34:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void RecenterControls()
|
|
|
|
{
|
|
|
|
if (Width < BoxPokeGrid.Width)
|
|
|
|
Width = BoxPokeGrid.Width;
|
|
|
|
BoxPokeGrid.HorizontallyCenter(this);
|
|
|
|
int p1 = CB_BoxSelect.Location.X;
|
|
|
|
CB_BoxSelect.HorizontallyCenter(this);
|
|
|
|
int p2 = CB_BoxSelect.Location.X;
|
|
|
|
|
|
|
|
var delta = p2 - p1;
|
|
|
|
if (delta == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
B_BoxLeft.SetBounds(B_BoxLeft.Location.X + delta, 0, 0, 0, BoundsSpecified.X);
|
|
|
|
B_BoxRight.SetBounds(B_BoxRight.Location.X + delta, 0, 0, 0, BoundsSpecified.X);
|
|
|
|
}
|
2019-10-03 03:04:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void InitializeSlots()
|
|
|
|
{
|
|
|
|
SlotPictureBoxes = BoxPokeGrid.Entries;
|
|
|
|
BoxSlotCount = SlotPictureBoxes.Count;
|
|
|
|
foreach (var pb in SlotPictureBoxes)
|
2019-10-03 03:04:12 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pb.MouseEnter += (o, args) => BoxSlot_MouseEnter(pb, args);
|
|
|
|
pb.MouseLeave += (o, args) => BoxSlot_MouseLeave(pb, args);
|
|
|
|
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;
|
|
|
|
pb.GiveFeedback += (sender, e) => e.UseDefaultCursors = false;
|
|
|
|
pb.AllowDrop = true;
|
2017-05-23 04:55:05 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void NotifySlotOld(ISlotInfo previous)
|
|
|
|
{
|
|
|
|
if (previous is not SlotInfoBox b || b.Box != CurrentBox)
|
|
|
|
return;
|
2019-09-03 02:30:58 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var pb = SlotPictureBoxes[previous.Slot];
|
|
|
|
pb.BackgroundImage = null;
|
|
|
|
}
|
2019-09-03 02:30:58 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void NotifySlotChanged(ISlotInfo slot, SlotTouchType type, PKM pk)
|
|
|
|
{
|
|
|
|
int index = GetViewIndex(slot);
|
|
|
|
if (index < 0)
|
|
|
|
return;
|
2019-09-03 02:30:58 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var pb = SlotPictureBoxes[index];
|
|
|
|
SlotUtil.UpdateSlot(pb, slot, pk, SAV, FlagIllegal, type);
|
|
|
|
}
|
2019-09-03 02:30:58 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public int GetViewIndex(ISlotInfo slot)
|
|
|
|
{
|
|
|
|
if (slot is not SlotInfoBox b || b.Box != CurrentBox)
|
|
|
|
return -1;
|
|
|
|
return slot.Slot;
|
|
|
|
}
|
2019-09-03 02:30:58 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public ISlotInfo GetSlotData(PictureBox view)
|
|
|
|
{
|
|
|
|
int slot = GetSlot(view);
|
|
|
|
return new SlotInfoBox(ViewIndex, slot);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private int GetSlot(PictureBox sender) => SlotPictureBoxes.IndexOf(sender);
|
|
|
|
public int ViewIndex => CurrentBox;
|
2018-05-05 15:07:22 +00:00
|
|
|
|
2022-06-18 18:04:24 +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
|
|
|
|
2022-06-18 18:04:24 +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
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public int CurrentBox
|
|
|
|
{
|
|
|
|
get => CB_BoxSelect.SelectedIndex;
|
|
|
|
set
|
2017-05-23 04:55:05 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
CB_BoxSelect.SelectedIndex = value;
|
|
|
|
if (value < 0)
|
|
|
|
return;
|
|
|
|
Editor.LoadBox(value);
|
2017-05-23 04:55:05 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public string CurrentBoxName => CB_BoxSelect.Text;
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +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
|
|
|
|
2023-05-07 02:42:48 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Updates the list of Box Names to select from, and selects the box index specified. If no box is specified, the previous index is used.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="box">Box to display after reload.</param>
|
2022-06-18 18:04:24 +00:00
|
|
|
public void ResetBoxNames(int box = -1)
|
|
|
|
{
|
|
|
|
if (!SAV.HasBox)
|
|
|
|
return;
|
2020-09-19 05:11:13 +00:00
|
|
|
|
2023-05-05 07:08:26 +00:00
|
|
|
var currentIndex = CurrentBox;
|
2023-05-07 02:42:48 +00:00
|
|
|
if (box < 0)
|
|
|
|
box = currentIndex;
|
|
|
|
|
2023-05-05 07:08:26 +00:00
|
|
|
var update = BoxUtil.GetBoxNames(SAV);
|
|
|
|
var current = CB_BoxSelect.Items;
|
|
|
|
if (!GetIsSame(update, current))
|
|
|
|
{
|
|
|
|
// try to keep list elements if same length
|
|
|
|
if (update.Length == current.Count)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < update.Length; i++)
|
|
|
|
current[i] = update[i];
|
|
|
|
}
|
|
|
|
else // rebuild completely
|
|
|
|
{
|
|
|
|
current.Clear();
|
|
|
|
current.AddRange(update);
|
|
|
|
}
|
|
|
|
}
|
2017-11-18 06:19:23 +00:00
|
|
|
|
2023-05-05 07:08:26 +00:00
|
|
|
box = Math.Clamp(box, 0, current.Count - 1);
|
|
|
|
if (box != CurrentBox)
|
2022-06-18 18:04:24 +00:00
|
|
|
CurrentBox = box;
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2023-05-05 07:08:26 +00:00
|
|
|
private static bool GetIsSame(IReadOnlyList<string> a, IList b)
|
|
|
|
{
|
|
|
|
if (a.Count != b.Count)
|
|
|
|
return false;
|
|
|
|
for (int i = 0; i < a.Count; i++)
|
|
|
|
{
|
|
|
|
if (b[i] is not string s || s != a[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void ResetSlots()
|
|
|
|
{
|
|
|
|
Editor.Reload();
|
|
|
|
int box = CurrentBox;
|
|
|
|
BoxPokeGrid.SetBackground(SAV.WallpaperImage(box));
|
|
|
|
M?.Hover.Stop();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
int index = box * SAV.BoxSlotCount;
|
|
|
|
for (int i = 0; i < BoxSlotCount; i++)
|
|
|
|
{
|
|
|
|
var pb = SlotPictureBoxes[i];
|
|
|
|
if (i >= SAV.BoxSlotCount || index + i >= SAV.SlotCount)
|
2017-05-23 04:55:05 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pb.Visible = false;
|
|
|
|
continue;
|
2017-05-23 04:55:05 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
pb.Visible = true;
|
2023-04-22 03:47:04 +00:00
|
|
|
SlotUtil.UpdateSlot(pb, (SlotInfoBox)GetSlotData(pb), Editor[i], SAV, FlagIllegal);
|
2017-05-23 04:55:05 +00:00
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00: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-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public bool SaveBoxBinary()
|
|
|
|
{
|
|
|
|
var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNoCancel,
|
|
|
|
MsgSaveBoxExportYes + Environment.NewLine +
|
|
|
|
string.Format(MsgSaveBoxExportNo, CurrentBoxName, CurrentBox + 1) + Environment.NewLine +
|
|
|
|
MsgSaveBoxExportCancel);
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (dr == DialogResult.Yes)
|
2018-01-24 06:37:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
using var sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = "pcdata.bin" };
|
|
|
|
if (sfd.ShowDialog() != DialogResult.OK)
|
|
|
|
return false;
|
|
|
|
File.WriteAllBytes(sfd.FileName, SAV.GetPCBinary());
|
|
|
|
return true;
|
2018-01-24 06:37:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
if (dr == DialogResult.No)
|
2017-05-23 04:55:05 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
using var sfd = new SaveFileDialog { Filter = "Box Data|*.bin", FileName = $"boxdata {CurrentBoxName}.bin" };
|
|
|
|
if (sfd.ShowDialog() != DialogResult.OK)
|
|
|
|
return false;
|
|
|
|
File.WriteAllBytes(sfd.FileName, SAV.GetBoxBinary(CurrentBox));
|
|
|
|
return true;
|
2017-05-23 04:55:05 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void ClearEvents()
|
|
|
|
{
|
|
|
|
B_BoxRight.Click -= ClickBoxRight;
|
|
|
|
B_BoxLeft.Click -= ClickBoxLeft;
|
|
|
|
CB_BoxSelect.SelectedIndexChanged -= GetBox;
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void Reset()
|
|
|
|
{
|
|
|
|
ResetBoxNames();
|
|
|
|
ResetSlots();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void GetBox(object? sender, EventArgs e)
|
|
|
|
{
|
|
|
|
CurrentBox = CB_BoxSelect.SelectedIndex;
|
|
|
|
if (SAV.CurrentBox != CurrentBox && CanSetCurrentBox)
|
|
|
|
SAV.CurrentBox = CurrentBox;
|
|
|
|
ResetSlots();
|
|
|
|
M?.Hover.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Drag & Drop Handling
|
|
|
|
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_MouseMove(object? sender, MouseEventArgs e) => M?.MouseMove(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_DragDrop(object? sender, DragEventArgs e) => M?.DragDrop(sender, e);
|
|
|
|
|
|
|
|
public bool InitializeFromSAV(SaveFile sav)
|
|
|
|
{
|
|
|
|
Editor = new BoxEdit(sav);
|
|
|
|
bool result = InitializeGrid();
|
|
|
|
|
|
|
|
int box = sav.CurrentBox;
|
|
|
|
if ((uint)box >= sav.BoxCount)
|
|
|
|
box = 0;
|
2023-05-07 02:42:48 +00:00
|
|
|
|
|
|
|
// Display the Box Names
|
|
|
|
ResetBoxNames(box);
|
2022-06-18 18:04:24 +00:00
|
|
|
Editor.LoadBox(box);
|
|
|
|
return result;
|
2017-05-23 04:55:05 +00:00
|
|
|
}
|
|
|
|
}
|