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 ( Form1 frm1 )
{
InitializeComponent ( ) ;
2014-12-20 04:19:41 +00:00
Util . TranslateInterface ( this , Form1 . curlanguage ) ;
2014-06-28 21:22:05 +00:00
m_parent = frm1 ;
Array . Copy ( m_parent . savefile , sav , 0x100000 ) ;
savindex = m_parent . savindex ;
shiftval = savindex * 0x7F000 ;
2014-11-21 23:34:10 +00:00
if ( m_parent . savegame_oras )
{
2015-03-11 01:44:51 +00:00
bagoffsets = new [ ]
2014-11-21 23:34:10 +00:00
{
0x05800 ,
0x05E40 ,
0x05FC0 ,
0x06170 ,
0x06270 ,
} ;
}
2014-06-28 21:22:05 +00:00
getListItems ( ) ;
getListKeyItems ( ) ;
2014-08-14 00:46:09 +00:00
getListTMHM ( ) ;
2014-06-28 21:22:05 +00:00
getListMedicine ( ) ;
getListBerries ( ) ;
B_DisplayItems . ForeColor = Color . Red ;
popItems ( ) ;
2014-10-10 02:59:57 +00:00
B_DisplayItems . Text = Form1 . itempouch [ 0 ] ;
B_DisplayMedicine . Text = Form1 . itempouch [ 1 ] ;
B_DisplayTMHM . Text = Form1 . itempouch [ 2 ] ;
B_DisplayBerries . Text = Form1 . itempouch [ 3 ] ;
B_DisplayKeyItems . Text = Form1 . itempouch [ 4 ] ;
2014-06-28 21:22:05 +00:00
}
Form1 m_parent ;
2014-12-12 05:45:01 +00:00
public byte [ ] sav = new byte [ 0x100000 ] ;
2014-06-28 21:22:05 +00:00
public int savindex ; int shiftval ;
public bool editing = false ;
public string [ ] item_val ;
public string [ ] keyitem_val ;
public string [ ] tmhm_val ;
public string [ ] medicine_val ;
public string [ ] berries_val ;
2015-03-11 01:44:51 +00:00
public int [ ] bagoffsets =
{
0x05800 ,
0x05E40 ,
0x05FC0 ,
0x06168 ,
0x06268 ,
} ;
2014-11-21 23:34:10 +00:00
2014-06-28 21:22:05 +00:00
// Initialize String Tables
private void getListItems ( )
{
2014-12-14 19:06:17 +00:00
ushort [ ] itemlist = ( m_parent . savegame_oras ) ? Legal . Pouch_Items_ORAS : Legal . Pouch_Items_XY ;
2014-07-26 21:56:06 +00:00
item_val = new string [ itemlist . Length ] ;
for ( int i = 0 ; i < itemlist . Length ; i + + )
2014-10-10 02:59:57 +00:00
item_val [ i ] = Form1 . itemlist [ itemlist [ i ] ] ;
2014-07-26 21:56:06 +00:00
Array . Sort ( item_val ) ;
2014-06-28 21:22:05 +00:00
}
private void getListKeyItems ( )
{
2014-12-14 19:06:17 +00:00
ushort [ ] itemlist = ( m_parent . savegame_oras ) ? Legal . Pouch_Key_ORAS : Legal . Pouch_Key_XY ;
2014-07-26 21:56:06 +00:00
keyitem_val = new string [ itemlist . Length ] ;
for ( int i = 0 ; i < itemlist . Length ; i + + )
2014-10-10 02:59:57 +00:00
keyitem_val [ i ] = Form1 . itemlist [ itemlist [ i ] ] ;
2014-07-26 21:56:06 +00:00
Array . Sort ( keyitem_val ) ;
2014-06-28 21:22:05 +00:00
}
2014-08-14 00:46:09 +00:00
private void getListTMHM ( )
2014-06-28 21:22:05 +00:00
{
2014-12-14 19:06:17 +00:00
ushort [ ] itemlist = ( m_parent . savegame_oras ) ? Legal . Pouch_TMHM_ORAS : Legal . Pouch_TMHM_XY ;
2014-07-26 21:56:06 +00:00
tmhm_val = new string [ itemlist . Length ] ;
for ( int i = 0 ; i < itemlist . Length ; i + + )
2014-10-10 02:59:57 +00:00
tmhm_val [ i ] = Form1 . itemlist [ itemlist [ i ] ] ;
2014-07-26 21:56:06 +00:00
// Array.Sort(tmhm_val); Already sorted, keep HMs last.
2014-06-28 21:22:05 +00:00
}
private void getListMedicine ( )
{
2015-01-03 19:20:21 +00:00
ushort [ ] itemlist = ( m_parent . savegame_oras ) ? Legal . Pouch_Medicine_ORAS : Legal . Pouch_Medicine_XY ;
2014-07-26 21:56:06 +00:00
medicine_val = new string [ itemlist . Length ] ;
for ( int i = 0 ; i < itemlist . Length ; i + + )
2014-10-10 02:59:57 +00:00
medicine_val [ i ] = Form1 . itemlist [ itemlist [ i ] ] ;
2014-07-26 21:56:06 +00:00
Array . Sort ( medicine_val ) ;
2014-06-28 21:22:05 +00:00
}
private void getListBerries ( )
{
2014-12-14 19:06:17 +00:00
ushort [ ] itemlist = Legal . Pouch_Berry_XY ;
2014-07-26 21:56:06 +00:00
berries_val = new string [ itemlist . Length ] ;
for ( int i = 0 ; i < itemlist . Length ; i + + )
2014-10-10 02:59:57 +00:00
berries_val [ i ] = Form1 . itemlist [ itemlist [ i ] ] ;
2014-07-26 21:56:06 +00:00
Array . Sort ( berries_val ) ;
2014-06-28 21:22:05 +00:00
}
// Populate DataGrid
private void popItems ( )
{
2014-11-21 23:34:10 +00:00
int offset = bagoffsets [ 0 ] + shiftval ;
2014-12-14 19:06:17 +00:00
populateList ( item_val , offset , item_val . Length - 1 ) ; // max 400
2014-06-28 21:22:05 +00:00
}
private void popKeyItems ( )
{
2014-11-21 23:34:10 +00:00
int offset = bagoffsets [ 1 ] + shiftval ;
2014-12-14 19:06:17 +00:00
populateList ( keyitem_val , offset , keyitem_val . Length - 1 ) ; // max 96
2014-06-28 21:22:05 +00:00
}
private void popTMHM ( )
{
2014-11-21 23:34:10 +00:00
int offset = bagoffsets [ 2 ] + shiftval ;
2014-12-14 19:06:17 +00:00
populateList ( tmhm_val , offset , tmhm_val . Length - 1 ) ;
2014-06-28 21:22:05 +00:00
}
private void popMedicine ( )
{
2014-11-21 23:34:10 +00:00
int offset = bagoffsets [ 3 ] + shiftval ;
2014-12-14 19:06:17 +00:00
populateList ( medicine_val , offset , medicine_val . Length - 1 ) ; // 64 total slots
2014-06-28 21:22:05 +00:00
}
private void popBerries ( )
{
2014-11-21 23:34:10 +00:00
int offset = bagoffsets [ 4 ] + shiftval ;
2014-12-14 19:06:17 +00:00
populateList ( berries_val , offset , berries_val . Length - 1 ) ; // 102 slots
2014-06-28 21:22:05 +00:00
}
private void populateList ( string [ ] itemarr , int offset , int itemcount )
{
dataGridView1 . Rows . Clear ( ) ;
dataGridView1 . Columns . Clear ( ) ;
DataGridViewColumn dgvIndex = new DataGridViewTextBoxColumn ( ) ;
{
dgvIndex . HeaderText = "CNT" ;
dgvIndex . DisplayIndex = 1 ;
dgvIndex . Width = 45 ;
dgvIndex . DefaultCellStyle . Alignment = DataGridViewContentAlignment . MiddleCenter ;
}
2015-03-11 01:44:51 +00:00
DataGridViewComboBoxColumn dgvItemVal = new DataGridViewComboBoxColumn
{
DisplayStyle = DataGridViewComboBoxDisplayStyle . Nothing
} ;
2014-06-28 21:22:05 +00:00
{
2015-03-11 01:44:51 +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
dgvItemVal . DisplayIndex = 0 ;
dgvItemVal . Width = 135 ;
dgvItemVal . FlatStyle = FlatStyle . Flat ;
}
dataGridView1 . Columns . Add ( dgvItemVal ) ;
dataGridView1 . Columns . Add ( dgvIndex ) ;
dataGridView1 . Rows . Add ( itemcount ) ;
2015-01-04 01:44:06 +00:00
dataGridView1 . CancelEdit ( ) ;
2014-06-28 21:22:05 +00:00
string itemname = "" ;
for ( int i = 0 ; i < itemcount ; i + + )
{
int itemvalue = BitConverter . ToUInt16 ( sav , offset + i * 4 ) ;
2014-11-25 03:53:10 +00:00
try { itemname = Form1 . 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 ;
}
2014-07-26 21:56:06 +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 ;
2014-12-11 06:50:40 +00:00
Util . Alert ( itemname + " removed from item pouch." , "If you exit the Item Editor by saving 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 )
{
ComboBox comboBox = ( ComboBox ) dataGridView1 . EditingControl ;
comboBox . DroppedDown = true ;
}
}
private void saveBag ( object sender )
{
int offset = 0 ;
if ( B_DisplayItems . ForeColor = = Color . Red )
{
2014-11-21 23:34:10 +00:00
offset = bagoffsets [ 0 ] + shiftval ;
2014-06-28 21:22:05 +00:00
}
else if ( B_DisplayKeyItems . ForeColor = = Color . Red )
{
2014-11-21 23:34:10 +00:00
offset = bagoffsets [ 1 ] + shiftval ;
2014-06-28 21:22:05 +00:00
}
else if ( B_DisplayTMHM . ForeColor = = Color . Red )
{
2014-11-21 23:34:10 +00:00
offset = bagoffsets [ 2 ] + shiftval ;
2014-06-28 21:22:05 +00:00
}
else if ( B_DisplayMedicine . ForeColor = = Color . Red )
{
2014-11-21 23:34:10 +00:00
offset = bagoffsets [ 3 ] + shiftval ;
2014-06-28 21:22:05 +00:00
}
else if ( B_DisplayBerries . ForeColor = = Color . Red )
{
2014-11-21 23:34:10 +00:00
offset = bagoffsets [ 4 ] + shiftval ;
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 ( ) ;
2014-10-10 02:59:57 +00:00
int itemindex = Array . IndexOf ( Form1 . itemlist , item ) ;
2015-03-11 01:44:51 +00:00
int itemcnt ;
2014-11-21 23:34:10 +00:00
try
2014-11-30 01:52:20 +00:00
{ 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 ;
}
2015-03-11 01:44:51 +00:00
if ( itemcnt = = 0 )
2014-07-26 21:56:06 +00:00
itemcnt + + ; // No 0 count of items
else if ( itemcnt > 995 )
itemcnt = 995 ;
2014-06-28 21:22:05 +00:00
// Write Data into Save File
2014-11-30 01:52:20 +00:00
Array . Copy ( BitConverter . GetBytes ( itemindex ) , 0 , sav , offset + 4 * ( i - emptyslots ) , 2 ) ; // item #
Array . Copy ( BitConverter . GetBytes ( itemcnt ) , 0 , sav , offset + 4 * ( i - emptyslots ) + 2 , 2 ) ; // count
2014-06-28 21:22:05 +00:00
}
// Delete Empty Trash
for ( int i = itemcount - emptyslots ; i < itemcount ; i + + )
{
2014-12-20 04:19:41 +00:00
Array . Copy ( BitConverter . GetBytes ( 0 ) , 0 , sav , offset + 4 * i + 0 , 2 ) ; // item #
Array . Copy ( BitConverter . GetBytes ( 0 ) , 0 , sav , offset + 4 * i + 2 , 2 ) ; // count
2014-06-28 21:22:05 +00:00
}
// Load New Button Color, after finished we'll load the new data.
Button btn = sender as Button ;
2014-12-20 04:19:41 +00:00
B_DisplayItems . ForeColor =
B_DisplayKeyItems . ForeColor =
B_DisplayTMHM . ForeColor =
B_DisplayMedicine . ForeColor =
B_DisplayBerries . ForeColor = Form1 . defaultControlText ;
2014-06-28 21:22:05 +00:00
btn . ForeColor = Color . Red ;
}
2014-11-25 03:53:10 +00:00
private void giveAll ( string [ ] inarray , int count )
2014-08-31 20:32:04 +00:00
{
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-08-31 20:32:04 +00:00
}
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 ) ;
popItems ( ) ;
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 ) ;
popKeyItems ( ) ;
2014-12-14 19:06:17 +00:00
if ( ModifierKeys = = Keys . Alt & & Util . Prompt ( MessageBoxButtons . YesNo , String . Format ( "Warning: Adding all {0} is dangerous." , B_DisplayKeyItems . Text ) , "Continue?" ) = = DialogResult . Yes )
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 ) ;
popTMHM ( ) ;
2014-12-14 19:06:17 +00:00
if ( ModifierKeys = = Keys . Alt & & Util . Prompt ( MessageBoxButtons . YesNo , String . Format ( "Warning: Adding all {0} is dangerous." , B_DisplayTMHM . Text ) , "Continue?" ) = = DialogResult . Yes )
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 ) ;
popMedicine ( ) ;
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 ) ;
popBerries ( ) ;
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 , m_parent . savefile , 0x100000 ) ;
m_parent . savedited = true ;
Close ( ) ;
}
}
}