PKHeX/SAV/SAV_Inventory.cs

229 lines
9.1 KiB
C#
Raw Normal View History

2014-06-28 21:22:05 +00:00
using System;
using System.Drawing;
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
2015-12-15 06:04:37 +00:00
item_val = getItems(Main.SAV.ORAS ? Legal.Pouch_Items_ORAS : Legal.Pouch_Items_XY);
keyitem_val = getItems(Main.SAV.ORAS ? Legal.Pouch_Key_ORAS : Legal.Pouch_Key_XY);
tmhm_val = getItems(Main.SAV.ORAS ? Legal.Pouch_TMHM_ORAS : Legal.Pouch_TMHM_XY, sort: false);
medicine_val = getItems(Main.SAV.ORAS ? Legal.Pouch_Medicine_ORAS : Legal.Pouch_Medicine_XY);
berries_val = getItems(Legal.Pouch_Berry_XY);
2014-06-28 21:22:05 +00:00
B_DisplayItems.ForeColor = Color.Red;
2015-12-15 06:04:37 +00:00
// Load Items
populateList(item_val, Main.SAV.Items.HeldItem);
2014-06-28 21:22:05 +00:00
B_DisplayItems.Text = Main.itempouch[0];
B_DisplayMedicine.Text = Main.itempouch[1];
B_DisplayTMHM.Text = Main.itempouch[2];
B_DisplayBerries.Text = Main.itempouch[3];
B_DisplayKeyItems.Text = Main.itempouch[4];
2014-06-28 21:22:05 +00:00
}
2016-02-10 08:33:42 +00:00
private readonly byte[] sav = (byte[])Main.SAV.Data.Clone();
2016-02-10 08:33:42 +00:00
private readonly string[] item_val, keyitem_val, tmhm_val, medicine_val, berries_val;
2014-06-28 21:22:05 +00:00
2014-11-21 23:34:10 +00:00
2014-06-28 21:22:05 +00:00
// Initialize String Tables
2015-12-15 06:04:37 +00:00
private string[] getItems(ushort[] items, bool sort = true)
2014-06-28 21:22:05 +00:00
{
2015-12-15 06:04:37 +00:00
string[] res = new string[items.Length];
for (int i = 0; i < res.Length; i++)
res[i] = Main.itemlist[items[i]];
if (sort)
Array.Sort(res);
return res;
2014-06-28 21:22:05 +00:00
}
// Populate DataGrid
2015-12-15 06:04:37 +00:00
private void populateList(string[] itemarr, int offset, int itemcount = -1)
2014-06-28 21:22:05 +00:00
{
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
DataGridViewColumn dgvIndex = new DataGridViewTextBoxColumn();
{
dgvIndex.HeaderText = "CNT";
dgvIndex.DisplayIndex = 1;
dgvIndex.Width = 45;
dgvIndex.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
DataGridViewComboBoxColumn dgvItemVal = new DataGridViewComboBoxColumn
{
2015-12-15 06:04:37 +00:00
DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
DisplayIndex = 0,
Width = 135,
FlatStyle = FlatStyle.Flat
};
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
2014-06-28 21:22:05 +00:00
dataGridView1.Columns.Add(dgvItemVal);
dataGridView1.Columns.Add(dgvIndex);
2015-12-22 04:45:44 +00:00
dataGridView1.Rows.Add(itemcount > 0 ? itemcount : itemarr.Length);
dataGridView1.CancelEdit();
2014-06-28 21:22:05 +00:00
string itemname = "";
2015-12-22 04:45:44 +00:00
for (int i = 0; i < dataGridView1.Rows.Count; i++)
2014-06-28 21:22:05 +00:00
{
int itemvalue = BitConverter.ToUInt16(sav, offset + i*4);
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)
{
dataGridView1.Rows[i].Cells[0].Value = itemarr[0];
dataGridView1.Rows[i].Cells[1].Value = 0;
2015-12-15 06:04:37 +00:00
Util.Alert(itemname + " removed from item pouch.", "If you save changes the item will no longer be in the pouch.");
2014-08-14 00:46:09 +00:00
}
else
{
dataGridView1.Rows[i].Cells[0].Value = itemarr[itemarrayval];
dataGridView1.Rows[i].Cells[1].Value = BitConverter.ToUInt16(sav, offset + i * 4 + 2);
}
2014-06-28 21:22:05 +00:00
}
}
private void dropclick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != 0) return;
ComboBox comboBox = (ComboBox)dataGridView1.EditingControl;
comboBox.DroppedDown = true;
2014-06-28 21:22:05 +00:00
}
private void saveBag(object sender)
{
int offset = 0;
if (B_DisplayItems.ForeColor == Color.Red)
offset = Main.SAV.Items.HeldItem;
2014-06-28 21:22:05 +00:00
else if (B_DisplayKeyItems.ForeColor == Color.Red)
offset = Main.SAV.Items.KeyItem;
2014-06-28 21:22:05 +00:00
else if (B_DisplayTMHM.ForeColor == Color.Red)
offset = Main.SAV.Items.TMHM;
2014-06-28 21:22:05 +00:00
else if (B_DisplayMedicine.ForeColor == Color.Red)
offset = Main.SAV.Items.Medicine;
2014-06-28 21:22:05 +00:00
else if (B_DisplayBerries.ForeColor == Color.Red)
offset = Main.SAV.Items.Berry;
2014-06-28 21:22:05 +00:00
// Fetch Data
int itemcount = dataGridView1.Rows.Count;
int emptyslots = 0;
for (int i = 0; i < itemcount; i++)
{
string item = dataGridView1.Rows[i].Cells[0].Value.ToString();
int itemindex = Array.IndexOf(Main.itemlist, item);
int itemcnt;
2014-11-21 23:34:10 +00:00
try
{ itemcnt = Convert.ToUInt16(dataGridView1.Rows[i].Cells[1].Value.ToString()); }
2014-11-21 23:34:10 +00:00
catch { itemcnt = 0; }
2014-06-28 21:22:05 +00:00
if (itemindex == 0) // Compression of Empty Slots
{
emptyslots++;
continue;
}
if (itemcnt == 0)
itemcnt++; // No 0 count of items
else if (itemcnt > 995)
itemcnt = 995; // cap out
2014-06-28 21:22:05 +00:00
// Write Data into Save File
2015-12-15 06:04:37 +00:00
BitConverter.GetBytes((ushort)itemindex).CopyTo(sav, offset + 4 * (i - emptyslots)); // item #
BitConverter.GetBytes((ushort)itemcnt).CopyTo(sav, offset + 4 * (i - emptyslots) + 2); // count
2014-06-28 21:22:05 +00:00
}
// Delete Empty Trash
for (int i = itemcount - emptyslots; i < itemcount; i++)
{
2015-12-15 06:04:37 +00:00
BitConverter.GetBytes((ushort)0).CopyTo(sav, offset + 4 * i + 0); // item #
BitConverter.GetBytes((ushort)0).CopyTo(sav, offset + 4 * i + 2); // count
2014-06-28 21:22:05 +00:00
}
// Load New Button Color, after finished we'll load the new data.
2014-12-20 04:19:41 +00:00
B_DisplayItems.ForeColor =
B_DisplayKeyItems.ForeColor =
B_DisplayTMHM.ForeColor =
B_DisplayMedicine.ForeColor =
B_DisplayBerries.ForeColor = Main.defaultControlText;
2014-06-28 21:22:05 +00:00
(sender as Button).ForeColor = Color.Red;
2014-06-28 21:22:05 +00:00
}
2014-11-25 03:53:10 +00:00
private void giveAll(string[] inarray, int count)
{
2014-11-25 03:53:10 +00:00
for (int i = 0; i < inarray.Length - 1; i++)
{
string itemname = inarray[i+1];
int itemarrayval = Array.IndexOf(inarray, itemname);
dataGridView1.Rows[i].Cells[0].Value = inarray[itemarrayval];
dataGridView1.Rows[i].Cells[1].Value = count;
}
}
2014-06-28 21:22:05 +00:00
private void B_DisplayItems_Click(object sender, EventArgs e)
{
// Store Current Items back to the save file
saveBag(sender);
2015-12-15 06:04:37 +00:00
populateList(item_val, Main.SAV.Items.HeldItem);
2014-11-25 03:53:10 +00:00
if (ModifierKeys == Keys.Alt)
giveAll(item_val, 995);
2014-06-28 21:22:05 +00:00
}
private void B_DisplayKeyItems_Click(object sender, EventArgs e)
{
// Store Current Items back to the save file
saveBag(sender);
2015-12-15 06:04:37 +00:00
populateList(keyitem_val, Main.SAV.Items.KeyItem);
if (ModifierKeys == Keys.Alt && Util.Prompt(MessageBoxButtons.YesNo,
$"Warning: Adding all {B_DisplayKeyItems.Text} is dangerous.", "Continue?") == DialogResult.Yes)
2014-12-14 19:06:17 +00:00
giveAll(keyitem_val, 1);
2014-06-28 21:22:05 +00:00
}
private void B_DisplayTMHM_Click(object sender, EventArgs e)
{
// Store Current Items back to the save file
saveBag(sender);
2015-12-15 06:04:37 +00:00
populateList(tmhm_val, Main.SAV.Items.TMHM);
if (ModifierKeys == Keys.Alt && Util.Prompt(MessageBoxButtons.YesNo,
$"Warning: Adding all {B_DisplayTMHM.Text} is dangerous.", "Continue?") == DialogResult.Yes)
2014-12-14 19:06:17 +00:00
giveAll(tmhm_val, 1);
2014-06-28 21:22:05 +00:00
}
private void B_DisplayMedicine_Click(object sender, EventArgs e)
{
// Store Current Items back to the save file
saveBag(sender);
2015-12-15 06:04:37 +00:00
populateList(medicine_val, Main.SAV.Items.Medicine);
2014-11-25 03:53:10 +00:00
if (ModifierKeys == Keys.Alt)
giveAll(medicine_val, 995);
2014-06-28 21:22:05 +00:00
}
private void B_DisplayBerries_Click(object sender, EventArgs e)
{
// Store Current Items back to the save file
saveBag(sender);
2015-12-15 06:04:37 +00:00
populateList(berries_val, Main.SAV.Items.Berry);
2014-11-25 03:53:10 +00:00
if (ModifierKeys == Keys.Alt)
giveAll(berries_val, 995);
2014-06-28 21:22:05 +00:00
}
private void B_Cancel_Click(object sender, EventArgs e)
{
Close();
}
private void B_Save_Click(object sender, EventArgs e)
{
saveBag(sender);
Array.Copy(sav, Main.SAV.Data, Main.SAV.Data.Length);
Main.SAV.Edited = true;
2014-06-28 21:22:05 +00:00
Close();
}
}
}