2013-10-05 15:45:39 +00:00
using System ;
2014-07-28 02:15:21 +00:00
using System.Linq ;
2013-10-05 15:45:39 +00:00
using System.Windows.Forms ;
2013-10-25 00:57:23 +00:00
using BizHawk.Client.Common ;
2013-11-04 01:39:19 +00:00
using BizHawk.Emulation.Common ;
2013-10-25 00:57:23 +00:00
2013-11-03 03:54:37 +00:00
namespace BizHawk.Client.EmuHawk
2013-10-05 15:45:39 +00:00
{
public partial class CheatEdit : UserControl
{
2015-01-14 21:55:48 +00:00
public IMemoryDomains MemoryDomains { get ; set ; }
2014-09-01 18:43:41 +00:00
2013-10-05 15:45:39 +00:00
public CheatEdit ( )
{
InitializeComponent ( ) ;
2013-10-05 20:22:55 +00:00
AddressBox . Nullable = false ;
ValueBox . Nullable = false ;
2013-10-05 15:45:39 +00:00
}
2013-10-05 20:22:55 +00:00
#region Privates
private const string HexInd = "0x" ;
2014-01-01 15:50:33 +00:00
private Cheat _cheat ;
private bool _loading ;
private bool _editmode ;
private Action _addCallback ;
private Action _editCallback ;
2013-10-05 20:22:55 +00:00
2013-10-05 15:45:39 +00:00
private void CheatEdit_Load ( object sender , EventArgs e )
{
2015-01-14 21:55:48 +00:00
if ( MemoryDomains ! = null ) // the designer needs this check
2013-10-05 20:22:55 +00:00
{
2014-07-28 02:15:21 +00:00
DomainDropDown . Items . Clear ( ) ;
2015-01-14 21:55:48 +00:00
DomainDropDown . Items . AddRange ( MemoryDomains
2014-07-28 02:15:21 +00:00
. Select ( d = > d . ToString ( ) )
. ToArray ( ) ) ;
2014-09-13 14:21:08 +00:00
2015-01-24 15:49:02 +00:00
if ( MemoryDomains . HasSystemBus )
2014-09-13 14:21:08 +00:00
{
2015-01-24 15:49:02 +00:00
DomainDropDown . SelectedItem = MemoryDomains . SystemBus . ToString ( ) ;
2014-09-13 14:21:08 +00:00
}
else
{
2015-01-14 21:55:48 +00:00
DomainDropDown . SelectedItem = MemoryDomains . MainMemory . ToString ( ) ;
2014-09-13 14:21:08 +00:00
}
2013-10-05 20:22:55 +00:00
}
2014-01-01 15:50:33 +00:00
2013-10-05 15:45:39 +00:00
SetFormToDefault ( ) ;
}
private void SetFormToCheat ( )
{
_loading = true ;
SetSizeSelected ( _cheat . Size ) ;
PopulateTypeDropdown ( ) ;
SetTypeSelected ( _cheat . Type ) ;
SetDomainSelected ( _cheat . Domain ) ;
AddressBox . SetHexProperties ( _cheat . Domain . Size ) ;
NameBox . Text = _cheat . Name ;
AddressBox . Text = _cheat . AddressStr ;
ValueBox . Text = _cheat . ValueStr ;
CompareBox . Text = _cheat . Compare . HasValue ? _cheat . CompareStr : String . Empty ;
ValueBox . ByteSize =
CompareBox . ByteSize =
_cheat . Size ;
ValueBox . Type =
CompareBox . Type =
_cheat . Type ;
ValueHexIndLabel . Text =
CompareHexIndLabel . Text =
2014-11-23 16:03:04 +00:00
_cheat . Type = = Watch . DisplayType . Hex ? HexInd : string . Empty ;
2013-10-05 15:45:39 +00:00
BigEndianCheckBox . Checked = _cheat . BigEndian . Value ;
CheckFormState ( ) ;
2013-11-05 23:14:22 +00:00
if ( ! _cheat . Compare . HasValue )
{
2014-01-01 15:50:33 +00:00
CompareBox . Text = String . Empty ; // Necessary hack until WatchValueBox.ToRawInt() becomes nullable
2013-11-05 23:14:22 +00:00
}
2014-01-01 15:50:33 +00:00
2013-10-05 15:45:39 +00:00
_loading = false ;
}
private void SetFormToDefault ( )
{
_loading = true ;
SetSizeSelected ( Watch . WatchSize . Byte ) ;
PopulateTypeDropdown ( ) ;
2014-11-23 16:03:04 +00:00
NameBox . Text = string . Empty ;
2013-10-05 20:22:55 +00:00
2015-01-14 21:55:48 +00:00
if ( MemoryDomains ! = null )
2013-10-05 20:22:55 +00:00
{
2015-01-24 15:49:02 +00:00
AddressBox . SetHexProperties ( MemoryDomains . SystemBus . Size ) ;
2013-10-05 20:22:55 +00:00
}
2013-10-05 15:45:39 +00:00
ValueBox . ByteSize =
CompareBox . ByteSize =
Watch . WatchSize . Byte ;
ValueBox . Type =
CompareBox . Type =
Watch . DisplayType . Hex ;
ValueBox . ResetText ( ) ;
CompareBox . ResetText ( ) ;
ValueHexIndLabel . Text =
CompareHexIndLabel . Text =
HexInd ;
BigEndianCheckBox . Checked = false ;
2013-10-05 20:22:55 +00:00
SetTypeSelected ( Watch . DisplayType . Hex ) ;
2013-10-05 15:45:39 +00:00
CheckFormState ( ) ;
2014-09-01 18:43:41 +00:00
CompareBox . Text = string . Empty ; // TODO: A needed hack until WatchValueBox.ToRawInt() becomes nullable
2013-10-05 15:45:39 +00:00
_loading = false ;
}
private void SetSizeSelected ( Watch . WatchSize size )
{
switch ( size )
{
default :
case Watch . WatchSize . Byte :
SizeDropDown . SelectedIndex = 0 ;
break ;
case Watch . WatchSize . Word :
SizeDropDown . SelectedIndex = 1 ;
break ;
case Watch . WatchSize . DWord :
SizeDropDown . SelectedIndex = 2 ;
break ;
}
}
private void SetTypeSelected ( Watch . DisplayType type )
{
foreach ( var item in DisplayTypeDropDown . Items )
{
if ( item . ToString ( ) = = Watch . DisplayTypeToString ( type ) )
{
DisplayTypeDropDown . SelectedItem = item ;
return ;
}
}
}
private void SetDomainSelected ( MemoryDomain domain )
{
foreach ( var item in DomainDropDown . Items )
{
if ( item . ToString ( ) = = domain . Name )
{
DomainDropDown . SelectedItem = item ;
return ;
}
}
}
private void PopulateTypeDropdown ( )
{
DisplayTypeDropDown . Items . Clear ( ) ;
switch ( SizeDropDown . SelectedIndex )
{
default :
case 0 :
2014-01-01 15:50:33 +00:00
DisplayTypeDropDown . Items . AddRange ( ByteWatch . ValidTypes . ConvertAll ( e = > Watch . DisplayTypeToString ( e ) ) . ToArray ( ) ) ;
2013-10-05 15:45:39 +00:00
break ;
case 1 :
2014-01-01 15:50:33 +00:00
DisplayTypeDropDown . Items . AddRange ( WordWatch . ValidTypes . ConvertAll ( e = > Watch . DisplayTypeToString ( e ) ) . ToArray ( ) ) ;
2013-10-05 15:45:39 +00:00
break ;
case 2 :
2014-01-01 15:50:33 +00:00
DisplayTypeDropDown . Items . AddRange ( DWordWatch . ValidTypes . ConvertAll ( e = > Watch . DisplayTypeToString ( e ) ) . ToArray ( ) ) ;
2013-10-05 15:45:39 +00:00
break ;
}
2014-01-01 15:50:33 +00:00
2013-10-05 15:45:39 +00:00
DisplayTypeDropDown . SelectedItem = DisplayTypeDropDown . Items [ 0 ] ;
}
private void CheckFormState ( )
{
2014-01-01 15:50:33 +00:00
var valid = ! String . IsNullOrWhiteSpace ( AddressBox . Text ) & & ! String . IsNullOrWhiteSpace ( ValueBox . Text ) ;
2013-10-05 20:22:55 +00:00
AddButton . Enabled = valid ;
EditButton . Enabled = _editmode & & valid ;
2013-10-05 15:45:39 +00:00
}
private void SizeDropDown_SelectedIndexChanged ( object sender , EventArgs e )
{
if ( ! _loading )
{
PopulateTypeDropdown ( ) ;
ValueBox . ByteSize =
CompareBox . ByteSize =
GetCurrentSize ( ) ;
}
}
private void DomainDropDown_SelectedIndexChanged ( object sender , EventArgs e )
{
if ( ! _loading )
{
2015-01-14 21:55:48 +00:00
var domain = MemoryDomains [ DomainDropDown . SelectedItem . ToString ( ) ] ;
2013-10-05 15:45:39 +00:00
AddressBox . SetHexProperties ( domain . Size ) ;
}
}
private Watch . WatchSize GetCurrentSize ( )
{
switch ( SizeDropDown . SelectedIndex )
{
default :
case 0 :
return Watch . WatchSize . Byte ;
case 1 :
return Watch . WatchSize . Word ;
case 2 :
return Watch . WatchSize . DWord ;
}
}
2013-10-05 20:22:55 +00:00
private void DisplayTypeDropDown_SelectedIndexChanged ( object sender , EventArgs e )
{
ValueBox . Type =
CompareBox . Type =
Watch . StringToDisplayType ( DisplayTypeDropDown . SelectedItem . ToString ( ) ) ;
}
private void AddButton_Click ( object sender , EventArgs e )
{
if ( _addCallback ! = null )
{
_addCallback ( ) ;
}
}
private void EditButton_Click ( object sender , EventArgs e )
{
if ( _editCallback ! = null )
{
_editCallback ( ) ;
}
}
#endregion
2013-10-05 15:45:39 +00:00
#region API
2013-10-06 16:40:51 +00:00
public void SetCheat ( Cheat cheat )
2013-10-05 15:45:39 +00:00
{
2013-10-05 20:22:55 +00:00
_editmode = true ;
2013-10-05 15:45:39 +00:00
_cheat = cheat ;
if ( cheat . IsSeparator )
{
SetFormToDefault ( ) ;
}
else
{
SetFormToCheat ( ) ;
}
}
2013-10-05 20:22:55 +00:00
public void ClearForm ( )
{
2013-10-06 16:40:51 +00:00
_cheat = Cheat . Separator ;
2013-10-05 23:16:35 +00:00
_editmode = false ;
SetFormToDefault ( ) ;
2013-10-05 20:22:55 +00:00
}
2014-01-01 15:50:33 +00:00
public Cheat OriginalCheat
{
get { return _cheat ; }
}
2013-10-06 16:40:51 +00:00
public Cheat Cheat
2013-10-05 20:22:55 +00:00
{
get
{
2015-01-14 21:55:48 +00:00
var domain = MemoryDomains [ DomainDropDown . SelectedItem . ToString ( ) ] ;
2014-03-23 15:58:44 +00:00
var address = AddressBox . ToRawInt ( ) . Value ;
2014-07-03 14:51:03 +00:00
//var address = AddressBox.ToRawInt() ?? 0;
2014-03-23 15:58:44 +00:00
if ( address < domain . Size )
{
var watch = Watch . GenerateWatch (
2015-01-14 21:55:48 +00:00
MemoryDomains [ DomainDropDown . SelectedItem . ToString ( ) ] ,
2014-03-23 15:58:44 +00:00
AddressBox . ToRawInt ( ) . Value ,
GetCurrentSize ( ) ,
Watch . StringToDisplayType ( DisplayTypeDropDown . SelectedItem . ToString ( ) ) ,
NameBox . Text ,
BigEndianCheckBox . Checked ) ;
return new Cheat (
watch ,
ValueBox . ToRawInt ( ) . Value ,
CompareBox . ToRawInt ( )
) ;
}
else
{
MessageBox . Show ( address . ToString ( ) + " is not a valid address for the domain " + domain . Name , "Index out of range" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
return Cheat . Separator ;
}
2013-10-05 20:22:55 +00:00
}
}
2014-01-01 15:50:33 +00:00
public void SetAddEvent ( Action addCallback )
2013-10-05 20:22:55 +00:00
{
2014-01-01 15:50:33 +00:00
_addCallback = addCallback ;
2013-10-05 20:22:55 +00:00
}
2013-10-05 15:45:39 +00:00
2014-01-01 15:50:33 +00:00
public void SetEditEvent ( Action editCallback )
2013-10-05 20:22:55 +00:00
{
2014-01-01 15:50:33 +00:00
_editCallback = editCallback ;
2013-10-05 20:22:55 +00:00
}
2013-10-05 15:45:39 +00:00
2013-10-05 20:22:55 +00:00
#endregion
2013-10-05 15:45:39 +00:00
}
}