PKHeX/Subforms/Save Editors/SAV_Inventory.cs

232 lines
8.3 KiB
C#
Raw Normal View History

2014-06-28 21:22:05 +00:00
using System;
using System.Linq;
2014-06-28 21:22:05 +00:00
using System.Windows.Forms;
namespace PKHeX
{
public partial class SAV_Inventory : Form
{
public SAV_Inventory()
2014-06-28 21:22:05 +00:00
{
InitializeComponent();
Util.TranslateInterface(this, Main.curlanguage);
2014-11-21 23:34:10 +00:00
Pouches = SAV.Inventory;
getBags();
2014-06-28 21:22:05 +00:00
}
2016-02-10 08:33:42 +00:00
private readonly SaveFile SAV = Main.SAV.Clone();
private readonly InventoryPouch[] Pouches;
private const string TabPrefix = "TAB_";
private const string DGVPrefix = "DGV_";
2014-06-28 21:22:05 +00:00
private void B_Cancel_Click(object sender, EventArgs e)
2014-06-28 21:22:05 +00:00
{
Close();
2014-06-28 21:22:05 +00:00
}
private void B_Save_Click(object sender, EventArgs e)
2014-06-28 21:22:05 +00:00
{
packBags();
SAV.Inventory = Pouches;
Array.Copy(SAV.Data, Main.SAV.Data, SAV.Data.Length);
Main.SAV.Edited = true;
Close();
}
2014-06-28 21:22:05 +00:00
private void getBags()
{
for (int i = 0; i < Pouches.Length; i++)
2014-06-28 21:22:05 +00:00
{
// Add Tab
tabControl1.TabPages.Add(new TabPage
{
// Text = Pouches[i].Type.ToString(),
ImageIndex = (int)Pouches[i].Type
});
// Add DataGrid
DataGridView dgv = new DataGridView
{
Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right, // All
Height = tabControl1.TabPages[i].Height,
Width = tabControl1.TabPages[i].Width,
Text = Pouches[i].Type.ToString(),
Name = DGVPrefix + Pouches[i].Type,
AllowUserToAddRows = false,
AllowUserToDeleteRows = false,
AllowUserToResizeRows = false,
AllowUserToResizeColumns = false,
RowHeadersVisible = false,
ColumnHeadersVisible = false,
MultiSelect = false,
ShowEditingIcon = false,
EditMode = DataGridViewEditMode.EditOnEnter,
ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single,
ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize,
SelectionMode = DataGridViewSelectionMode.CellSelect,
CellBorderStyle = DataGridViewCellBorderStyle.None,
};
tabControl1.TabPages[i].Controls.Add(dgv);
spillBag(dgv, i);
2014-06-28 21:22:05 +00:00
}
}
private void spillBag(DataGridView dgv, int bag)
{
var pouch = Pouches[bag];
var itemcount = Pouches[bag].Items.Length;
string[] itemarr = getItems(pouch.LegalItems);
dgv.Rows.Clear();
dgv.Columns.Clear();
DataGridViewComboBoxColumn dgvItemVal = new DataGridViewComboBoxColumn
{
2015-12-15 06:04:37 +00:00
DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
DisplayIndex = 0,
Width = 135,
FlatStyle = FlatStyle.Flat
};
DataGridViewColumn dgvIndex = new DataGridViewTextBoxColumn();
{
dgvIndex.HeaderText = "CNT";
dgvIndex.DisplayIndex = 1;
dgvIndex.Width = 45;
dgvIndex.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
2015-12-15 06:04:37 +00:00
foreach (string t in itemarr)
dgvItemVal.Items.Add(t); // add only the Item Names
2014-11-21 23:34:10 +00:00
dgv.Columns.Add(dgvItemVal);
dgv.Columns.Add(dgvIndex);
2014-06-28 21:22:05 +00:00
dgv.Rows.Add(itemcount > 0 ? itemcount : itemarr.Length);
dgv.CancelEdit();
2014-06-28 21:22:05 +00:00
string itemname = "";
string err = "";
for (int i = 0; i < pouch.Items.Length; i++)
2014-06-28 21:22:05 +00:00
{
int itemvalue = pouch.Items[i].Index;
try { itemname = Main.itemlist[itemvalue]; }
2014-06-28 21:22:05 +00:00
catch
{
2014-12-11 06:50:40 +00:00
Util.Error("Unknown item detected.", "Item ID: " + itemvalue, "Item is after: " + itemname);
2014-06-28 21:22:05 +00:00
continue;
}
2015-12-15 06:04:37 +00:00
int itemarrayval = Array.IndexOf(itemarr, itemname);
2014-08-14 00:46:09 +00:00
if (itemarrayval == -1)
{
dgv.Rows[i].Cells[0].Value = itemarr[0];
dgv.Rows[i].Cells[1].Value = 0;
err += itemname + ", ";
2014-08-14 00:46:09 +00:00
}
else
{
dgv.Rows[i].Cells[0].Value = itemname;
dgv.Rows[i].Cells[1].Value = pouch.Items[i].Count;
2014-08-14 00:46:09 +00:00
}
2014-06-28 21:22:05 +00:00
}
if (err.Length > 0)
Util.Alert($"The following items have been removed from {Pouches[bag].Type} pouch.", err,
"If you save changes, the item(s) will no longer be in the pouch.");
2014-06-28 21:22:05 +00:00
}
private void packBags()
2014-06-28 21:22:05 +00:00
{
for (int p = 0; p < Pouches.Length; p++)
2014-06-28 21:22:05 +00:00
{
// Get DataGridView
DataGridView dgv = Controls.Find(DGVPrefix + Pouches[p].Type, true).FirstOrDefault() as DataGridView;
int ctr = 0;
for (int i = 0; i < dgv.Rows.Count; i++)
2014-06-28 21:22:05 +00:00
{
string item = dgv.Rows[i].Cells[0].Value.ToString();
int itemindex = Array.IndexOf(Main.itemlist, item);
int itemcnt;
try
{ itemcnt = Convert.ToUInt16(dgv.Rows[i].Cells[1].Value.ToString()); }
catch { itemcnt = 0; }
if (itemcnt == 0)
itemcnt++; // No 0 count of items
else if (itemcnt > 995)
itemcnt = 995; // cap out
if (itemindex == 0) // Compression of Empty Slots
continue;
Pouches[p].Items[ctr++] = new InventoryItem {Index = itemindex, Count = itemcnt};
2014-06-28 21:22:05 +00:00
}
for (int i = ctr; i < Pouches[p].Items.Length; i++)
Pouches[p].Items[i] = new InventoryItem(); // Empty Slots at the end
2014-06-28 21:22:05 +00:00
}
}
// Initialize String Tables
private string[] getItems(ushort[] items, bool sort = true)
2014-06-28 21:22:05 +00:00
{
string[] res = new string[items.Length + 1];
for (int i = 0; i < res.Length - 1; i++)
res[i] = Main.itemlist[items[i]];
res[items.Length] = Main.itemlist[0];
if (sort)
Array.Sort(res);
return res;
2014-06-28 21:22:05 +00:00
}
// User Cheats
private void B_GiveAll_Click(object sender, EventArgs e)
2014-06-28 21:22:05 +00:00
{
// Get Current Pouch
int pouch = tabControl1.SelectedIndex;
if (pouch < 0)
return;
ushort[] legalitems = Pouches[pouch].LegalItems;
DataGridView dgv = Controls.Find(DGVPrefix + Pouches[pouch].Type, true).FirstOrDefault() as DataGridView;
2014-11-25 03:53:10 +00:00
if (ModifierKeys == Keys.Alt)
{
for (int i = 0; i < legalitems.Length; i++)
{
dgv.Rows[i].Cells[0].Value = Main.itemlist[0];
dgv.Rows[i].Cells[1].Value = 0;
}
Util.Alert("Items cleared.");
return;
}
int Count = ModifierKeys == Keys.Control ? 1 : Pouches[pouch].MaxCount;
for (int i = 0; i < legalitems.Length; i++)
{
int item = legalitems[i];
string itemname;
int c = Count;
// Override for HMs
switch (SAV.Generation)
{
case 3: {
itemname = Main.itemlist[item];
if (Legal.Pouch_HM_RS.Contains(legalitems[i])) c = 1;
break;
}
default: {
itemname = Main.itemlist[item];
if (new[] { 420, 421, 422, 423, 423, 424, 425, 426, 427, 737 }.Contains(legalitems[i])) c = 1;
break;
}
}
dgv.Rows[i].Cells[0].Value = itemname;
dgv.Rows[i].Cells[1].Value = c;
}
System.Media.SystemSounds.Asterisk.Play();
2014-06-28 21:22:05 +00:00
}
}
}