2018-01-31 02:52:55 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2018-01-17 03:30:08 +00:00
|
|
|
|
using System.Windows.Forms;
|
2018-01-31 02:52:55 +00:00
|
|
|
|
using PKHeX.Core;
|
2021-11-27 23:48:08 +00:00
|
|
|
|
using PKHeX.Drawing.PokeSprite;
|
2018-01-17 03:30:08 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms.Controls
|
|
|
|
|
{
|
2018-05-05 15:07:22 +00:00
|
|
|
|
public partial class SlotList : UserControl, ISlotViewer<PictureBox>
|
2018-01-17 03:30:08 +00:00
|
|
|
|
{
|
2018-01-31 02:52:55 +00:00
|
|
|
|
private static readonly string[] names = Enum.GetNames(typeof(StorageSlotType));
|
|
|
|
|
private readonly LabelType[] Labels = new LabelType[names.Length];
|
2020-12-22 07:37:07 +00:00
|
|
|
|
private readonly List<PictureBox> slots = new();
|
|
|
|
|
private List<SlotInfoMisc> SlotOffsets = new();
|
2018-01-31 02:52:55 +00:00
|
|
|
|
public int SlotCount { get; private set; }
|
2020-10-18 18:02:39 +00:00
|
|
|
|
public SaveFile SAV { get; set; } = null!;
|
2019-11-16 01:34:18 +00:00
|
|
|
|
public bool FlagIllegal { get; set; }
|
2018-01-17 03:30:08 +00:00
|
|
|
|
|
|
|
|
|
public SlotList()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2018-01-31 02:52:55 +00:00
|
|
|
|
AddLabels();
|
2018-01-17 03:30:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 02:52:55 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the extra slot viewers with a list of offsets and sets up event handling.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="list">Extra slots to show</param>
|
|
|
|
|
/// <param name="enableDragDropContext">Events to set up</param>
|
|
|
|
|
/// <remarks>Uses an object pool for viewers (only generates as needed)</remarks>
|
2019-09-03 02:30:58 +00:00
|
|
|
|
public void Initialize(List<SlotInfoMisc> list, Action<Control> enableDragDropContext)
|
2018-01-17 03:30:08 +00:00
|
|
|
|
{
|
2018-01-31 02:52:55 +00:00
|
|
|
|
SlotOffsets = list;
|
2018-05-05 15:07:22 +00:00
|
|
|
|
LoadSlots(list.Count, enableDragDropContext);
|
2018-01-17 03:30:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 02:52:55 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Hides all slots from the <see cref="SlotList"/>.
|
|
|
|
|
/// </summary>
|
2020-10-18 18:02:39 +00:00
|
|
|
|
public void HideAllSlots() => LoadSlots(0, _ => { });
|
2018-01-31 02:52:55 +00:00
|
|
|
|
|
2019-09-03 02:30:58 +00:00
|
|
|
|
public void NotifySlotOld(ISlotInfo previous)
|
|
|
|
|
{
|
2020-12-23 05:24:41 +00:00
|
|
|
|
if (previous is not SlotInfoMisc m)
|
2019-09-03 02:30:58 +00:00
|
|
|
|
return;
|
|
|
|
|
var index = SlotOffsets.FindIndex(z => m.Equals(z));
|
|
|
|
|
if (index < 0)
|
|
|
|
|
return;
|
|
|
|
|
var pb = slots[index];
|
|
|
|
|
pb.BackgroundImage = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void NotifySlotChanged(ISlotInfo slot, SlotTouchType type, PKM pkm)
|
|
|
|
|
{
|
2020-12-23 05:24:41 +00:00
|
|
|
|
if (slot is not SlotInfoMisc m)
|
2019-09-03 02:30:58 +00:00
|
|
|
|
return;
|
|
|
|
|
var index = GetViewIndex(m);
|
|
|
|
|
if (index < 0)
|
|
|
|
|
return;
|
|
|
|
|
var pb = slots[index];
|
2019-11-16 01:34:18 +00:00
|
|
|
|
SlotUtil.UpdateSlot(pb, slot, pkm, SAV, FlagIllegal, type);
|
2019-09-03 02:30:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetViewIndex(ISlotInfo info) => SlotOffsets.FindIndex(info.Equals);
|
|
|
|
|
|
|
|
|
|
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 GetSlotData(slot);
|
2018-05-05 15:07:22 +00:00
|
|
|
|
}
|
2018-08-13 02:27:11 +00:00
|
|
|
|
|
2019-09-03 02:30:58 +00:00
|
|
|
|
public ISlotInfo GetSlotData(int slot) => SlotOffsets[slot];
|
|
|
|
|
|
2018-05-05 15:07:22 +00:00
|
|
|
|
public IList<PictureBox> SlotPictureBoxes => slots;
|
2020-10-18 18:02:39 +00:00
|
|
|
|
|
|
|
|
|
public int GetSlot(PictureBox sender)
|
|
|
|
|
{
|
|
|
|
|
var view = WinFormsUtil.GetUnderlyingControl<PictureBox>(sender);
|
|
|
|
|
if (view == null)
|
|
|
|
|
return -1;
|
|
|
|
|
return slots.IndexOf(view);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 02:52:55 +00:00
|
|
|
|
public int GetSlotOffset(int slot) => SlotOffsets[slot].Offset;
|
2018-05-05 15:07:22 +00:00
|
|
|
|
public int ViewIndex { get; set; } = -1;
|
2018-01-31 02:52:55 +00:00
|
|
|
|
|
|
|
|
|
private IEnumerable<PictureBox> LoadSlots(int after, Action<Control> enableDragDropContext)
|
2018-01-17 03:30:08 +00:00
|
|
|
|
{
|
2018-01-31 02:52:55 +00:00
|
|
|
|
var generated = new List<PictureBox>();
|
|
|
|
|
int before = SlotCount;
|
|
|
|
|
SlotCount = after;
|
|
|
|
|
int diff = after - before;
|
|
|
|
|
if (diff > 0)
|
2018-01-17 03:30:08 +00:00
|
|
|
|
{
|
2018-01-31 02:52:55 +00:00
|
|
|
|
AddSlots(diff);
|
|
|
|
|
for (int i = before; i < after; i++)
|
|
|
|
|
{
|
|
|
|
|
var slot = slots[i];
|
|
|
|
|
enableDragDropContext(slot);
|
|
|
|
|
FLP_Slots.Controls.Add(slot);
|
|
|
|
|
FLP_Slots.SetFlowBreak(slot, true);
|
|
|
|
|
generated.Add(slot);
|
|
|
|
|
}
|
2018-01-17 03:30:08 +00:00
|
|
|
|
}
|
2018-01-31 02:52:55 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int i = before - 1; i >= after; i--)
|
|
|
|
|
FLP_Slots.Controls.Remove(slots[i]);
|
|
|
|
|
}
|
|
|
|
|
SetLabelVisibility();
|
|
|
|
|
return generated;
|
2018-01-17 03:30:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 02:52:55 +00:00
|
|
|
|
private void AddSlots(int count)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < count; i++)
|
2020-10-24 21:57:53 +00:00
|
|
|
|
slots.Add(GetPictureBox(i, SpriteUtil.Spriter));
|
2018-01-31 02:52:55 +00:00
|
|
|
|
}
|
2018-08-13 02:27:11 +00:00
|
|
|
|
|
2018-01-17 03:30:08 +00:00
|
|
|
|
private const int PadPixels = 2;
|
2018-08-13 02:27:11 +00:00
|
|
|
|
|
2021-12-05 01:56:56 +00:00
|
|
|
|
private static PictureBox GetPictureBox(int index, SpriteBuilder s) => new()
|
2018-01-17 03:30:08 +00:00
|
|
|
|
{
|
2021-12-05 01:56:56 +00:00
|
|
|
|
BorderStyle = BorderStyle.FixedSingle,
|
|
|
|
|
Width = s.Width + 2,
|
|
|
|
|
Height = s.Height + 2,
|
|
|
|
|
AllowDrop = true,
|
|
|
|
|
Margin = new Padding(PadPixels),
|
|
|
|
|
SizeMode = PictureBoxSizeMode.CenterImage,
|
|
|
|
|
Name = $"bpkm{index}",
|
|
|
|
|
};
|
2018-01-31 02:52:55 +00:00
|
|
|
|
|
2020-09-07 20:51:13 +00:00
|
|
|
|
private sealed class LabelType : Label
|
2018-01-31 02:52:55 +00:00
|
|
|
|
{
|
|
|
|
|
public StorageSlotType Type;
|
|
|
|
|
}
|
2018-08-13 02:27:11 +00:00
|
|
|
|
|
2018-01-31 02:52:55 +00:00
|
|
|
|
private void AddLabels()
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < names.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var name = names[i];
|
2022-01-03 05:35:59 +00:00
|
|
|
|
bool result = Enum.TryParse<StorageSlotType>(name, out var value);
|
2020-11-14 16:20:48 +00:00
|
|
|
|
if (!result)
|
|
|
|
|
continue;
|
|
|
|
|
|
2018-01-31 02:52:55 +00:00
|
|
|
|
var label = new LabelType
|
|
|
|
|
{
|
|
|
|
|
Name = $"L_{name}",
|
|
|
|
|
Text = name,
|
2022-01-03 05:35:59 +00:00
|
|
|
|
Type = value,
|
2018-01-31 02:52:55 +00:00
|
|
|
|
AutoSize = true,
|
|
|
|
|
Visible = false,
|
|
|
|
|
};
|
|
|
|
|
Labels[i] = label;
|
|
|
|
|
FLP_Slots.Controls.Add(label);
|
|
|
|
|
FLP_Slots.SetFlowBreak(label, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-13 02:27:11 +00:00
|
|
|
|
|
2018-01-31 02:52:55 +00:00
|
|
|
|
private void SetLabelVisibility()
|
|
|
|
|
{
|
|
|
|
|
foreach (var l in Labels)
|
|
|
|
|
{
|
|
|
|
|
int index = SlotOffsets.FindIndex(z => z.Type == l.Type);
|
|
|
|
|
if (index < 0)
|
|
|
|
|
{
|
|
|
|
|
l.Visible = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
int pos = FLP_Slots.Controls.IndexOf(slots[index]);
|
|
|
|
|
if (pos > FLP_Slots.Controls.IndexOf(l))
|
|
|
|
|
pos--;
|
|
|
|
|
FLP_Slots.Controls.SetChildIndex(l, pos);
|
|
|
|
|
l.Visible = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-17 03:30:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|