-
-
Notifications
You must be signed in to change notification settings - Fork 666
/
SlotList.cs
171 lines (154 loc) · 5.61 KB
/
SlotList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PKHeX.Core;
namespace PKHeX.WinForms.Controls
{
public partial class SlotList : UserControl, ISlotViewer<PictureBox>
{
private static readonly string[] names = Enum.GetNames(typeof(StorageSlotType));
private readonly LabelType[] Labels = new LabelType[names.Length];
private readonly List<PictureBox> slots = new List<PictureBox>();
private List<SlotInfoMisc> SlotOffsets = new List<SlotInfoMisc>();
public int SlotCount { get; private set; }
public SaveFile SAV { get; set; }
public SlotList()
{
InitializeComponent();
AddLabels();
}
/// <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>
public void Initialize(List<SlotInfoMisc> list, Action<Control> enableDragDropContext)
{
SlotOffsets = list;
LoadSlots(list.Count, enableDragDropContext);
}
/// <summary>
/// Hides all slots from the <see cref="SlotList"/>.
/// </summary>
public void HideAllSlots() => LoadSlots(0, null);
public void NotifySlotOld(ISlotInfo previous)
{
if (!(previous is SlotInfoMisc m))
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)
{
if (!(slot is SlotInfoMisc m))
return;
var index = GetViewIndex(m);
if (index < 0)
return;
var pb = slots[index];
SlotUtil.UpdateSlot(pb, slot, pkm, SAV, true, type);
}
public int GetViewIndex(ISlotInfo info) => SlotOffsets.FindIndex(info.Equals);
public ISlotInfo GetSlotData(PictureBox view)
{
int slot = GetSlot(view);
return GetSlotData(slot);
}
public ISlotInfo GetSlotData(int slot) => SlotOffsets[slot];
public IList<PictureBox> SlotPictureBoxes => slots;
public int GetSlot(PictureBox sender) => slots.IndexOf(WinFormsUtil.GetUnderlyingControl<PictureBox>(sender));
public int GetSlotOffset(int slot) => SlotOffsets[slot].Offset;
public int ViewIndex { get; set; } = -1;
private IEnumerable<PictureBox> LoadSlots(int after, Action<Control> enableDragDropContext)
{
var generated = new List<PictureBox>();
int before = SlotCount;
SlotCount = after;
int diff = after - before;
if (diff > 0)
{
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);
}
}
else
{
for (int i = before - 1; i >= after; i--)
FLP_Slots.Controls.Remove(slots[i]);
}
SetLabelVisibility();
return generated;
}
private void AddSlots(int count)
{
for (int i = 0; i < count; i++)
slots.Add(GetPictureBox(i));
}
private const int PadPixels = 2;
private const int SlotWidth = 40;
private const int SlotHeight = 30;
private static PictureBox GetPictureBox(int index)
{
return new PictureBox
{
BorderStyle = BorderStyle.FixedSingle,
Width = SlotWidth + 2,
Height = SlotHeight + 2,
AllowDrop = true,
Margin = new Padding(PadPixels),
SizeMode = PictureBoxSizeMode.CenterImage,
Name = $"bpkm{index}",
};
}
private class LabelType : Label
{
public StorageSlotType Type;
}
private void AddLabels()
{
for (var i = 0; i < names.Length; i++)
{
var name = names[i];
Enum.TryParse<StorageSlotType>(name, out var val);
var label = new LabelType
{
Name = $"L_{name}",
Text = name,
Type = val,
AutoSize = true,
Visible = false,
};
Labels[i] = label;
FLP_Slots.Controls.Add(label);
FLP_Slots.SetFlowBreak(label, true);
}
}
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;
}
}
}
}