Refactor WatchValueBox to better handle the Nullable property, fixes a lot of issues in the cheat form among other places

This commit is contained in:
adelikat 2013-11-08 23:55:45 +00:00
parent 8562276320
commit 999c884f13
6 changed files with 109 additions and 71 deletions

View File

@ -8,8 +8,8 @@ namespace BizHawk.Client.EmuHawk
{ {
public interface INumberBox public interface INumberBox
{ {
int ToRawInt(); int? ToRawInt();
void SetFromRawInt(int rawint); void SetFromRawInt(int? rawint);
bool Nullable { get; } bool Nullable { get; }
} }
@ -124,11 +124,18 @@ namespace BizHawk.Client.EmuHawk
base.OnTextChanged(e); base.OnTextChanged(e);
} }
public int ToRawInt() public int? ToRawInt()
{ {
if (String.IsNullOrWhiteSpace(Text)) if (String.IsNullOrWhiteSpace(Text))
{ {
return 0; if (Nullable)
{
return null;
}
else
{
return 0;
}
} }
else else
{ {
@ -136,9 +143,16 @@ namespace BizHawk.Client.EmuHawk
} }
} }
public void SetFromRawInt(int val) public void SetFromRawInt(int? val)
{ {
Text = String.Format(_addressFormatStr, val); if (val.HasValue)
{
Text = String.Empty;
}
else
{
Text = String.Format(_addressFormatStr, val);
}
} }
} }
@ -227,11 +241,18 @@ namespace BizHawk.Client.EmuHawk
} }
} }
public int ToRawInt() public int? ToRawInt()
{ {
if (String.IsNullOrWhiteSpace(Text)) if (String.IsNullOrWhiteSpace(Text))
{ {
return 0; if (Nullable)
{
return null;
}
else
{
return 0;
}
} }
else else
{ {
@ -239,9 +260,9 @@ namespace BizHawk.Client.EmuHawk
} }
} }
public void SetFromRawInt(int val) public void SetFromRawInt(int? val)
{ {
Text = val.ToString(); Text = val.HasValue ? val.ToString() : String.Empty;
} }
} }
} }

View File

@ -273,7 +273,7 @@ namespace BizHawk.Client.EmuHawk
{ {
Watch watch = Watch.GenerateWatch( Watch watch = Watch.GenerateWatch(
Global.Emulator.MemoryDomains[DomainDropDown.SelectedItem.ToString()], Global.Emulator.MemoryDomains[DomainDropDown.SelectedItem.ToString()],
AddressBox.ToRawInt(), AddressBox.ToRawInt().Value,
GetCurrentSize(), GetCurrentSize(),
Watch.StringToDisplayType(DisplayTypeDropDown.SelectedItem.ToString()), Watch.StringToDisplayType(DisplayTypeDropDown.SelectedItem.ToString()),
NameBox.Text, NameBox.Text,
@ -281,8 +281,8 @@ namespace BizHawk.Client.EmuHawk
return new Cheat( return new Cheat(
watch, watch,
ValueBox.ToRawInt(), ValueBox.ToRawInt().Value,
!String.IsNullOrWhiteSpace(CompareBox.Text) ? CompareBox.ToRawInt() : (int?)null CompareBox.ToRawInt()
); );
} }
} }

View File

@ -269,7 +269,7 @@ namespace BizHawk.Client.EmuHawk
if (!String.IsNullOrWhiteSpace(ValueBox.Text)) if (!String.IsNullOrWhiteSpace(ValueBox.Text))
{ {
VALUE = ValueBox.ToRawInt(); VALUE = ValueBox.ToRawInt().Value;
} }
for (int i = 0; i < Global.Emulator.MemoryDomains.Count; i++) for (int i = 0; i < Global.Emulator.MemoryDomains.Count; i++)

View File

@ -151,22 +151,16 @@ namespace BizHawk.Client.EmuHawk
{ {
Watch watch = Watch.GenerateWatch( Watch watch = Watch.GenerateWatch(
Global.Emulator.MemoryDomains[1], /*System Bus*/ Global.Emulator.MemoryDomains[1], /*System Bus*/
AddressBox.ToRawInt(), AddressBox.ToRawInt().Value,
Watch.WatchSize.Byte, Watch.WatchSize.Byte,
Watch.DisplayType.Hex, Watch.DisplayType.Hex,
GameGenieCode.Text, GameGenieCode.Text,
false); false);
int? compare = null;
if (!String.IsNullOrWhiteSpace(CompareBox.Text))
{
compare = CompareBox.ToRawInt();
}
Global.CheatList.Add(new Cheat( Global.CheatList.Add(new Cheat(
watch, watch,
ValueBox.ToRawInt(), ValueBox.ToRawInt().Value,
compare CompareBox.ToRawInt()
)); ));
} }
} }

View File

@ -200,7 +200,7 @@ namespace BizHawk.Client.EmuHawk
default: default:
case Mode.New: case Mode.New:
var domain = Global.Emulator.MemoryDomains.FirstOrDefault(d => d.Name == DomainDropDown.SelectedItem.ToString()); var domain = Global.Emulator.MemoryDomains.FirstOrDefault(d => d.Name == DomainDropDown.SelectedItem.ToString());
var address = AddressBox.ToRawInt(); var address = AddressBox.ToRawInt().Value;
var notes = NotesBox.Text; var notes = NotesBox.Text;
var type = Watch.StringToDisplayType(DisplayTypeDropDown.SelectedItem.ToString()); var type = Watch.StringToDisplayType(DisplayTypeDropDown.SelectedItem.ToString());
var bigendian = BigEndianCheckBox.Checked; var bigendian = BigEndianCheckBox.Checked;
@ -268,7 +268,7 @@ namespace BizHawk.Client.EmuHawk
string tempNotes = _watchList[i].Notes; string tempNotes = _watchList[i].Notes;
_watchList[i] = Watch.GenerateWatch( _watchList[i] = Watch.GenerateWatch(
_watchList[i].Domain, _watchList[i].Domain,
_watchList.Count == 1 ? AddressBox.ToRawInt() : _watchList[i].Address.Value, _watchList.Count == 1 ? AddressBox.ToRawInt().Value : _watchList[i].Address.Value,
size, size,
_watchList[i].Type, _watchList[i].Type,
_watchList[i].Notes, _watchList[i].Notes,

View File

@ -75,7 +75,7 @@ namespace BizHawk.Client.EmuHawk
get { return _type; } get { return _type; }
set set
{ {
int val = ToRawInt(); int? val = ToRawInt();
_type = value; _type = value;
SetMaxLength(); SetMaxLength();
SetFromRawInt(val); SetFromRawInt(val);
@ -227,14 +227,19 @@ namespace BizHawk.Client.EmuHawk
protected override void OnKeyDown(KeyEventArgs e) protected override void OnKeyDown(KeyEventArgs e)
{ {
string text = String.IsNullOrWhiteSpace(Text) ? "0" : Text;
if (e.KeyCode == Keys.Up) if (e.KeyCode == Keys.Up)
{ {
switch (_type) switch (_type)
{ {
default: default:
case Watch.DisplayType.Signed: case Watch.DisplayType.Signed:
int val = ToRawInt(); int? val = ToRawInt() ?? 0;
if (val == MaxSignedInt) if (!val.HasValue)
{
Text = String.Empty;
}
else if (val == MaxSignedInt)
{ {
val = 0; val = 0;
} }
@ -245,7 +250,7 @@ namespace BizHawk.Client.EmuHawk
Text = val.ToString(); Text = val.ToString();
break; break;
case Watch.DisplayType.Unsigned: case Watch.DisplayType.Unsigned:
uint uval = (uint)ToRawInt(); uint uval = (uint)(ToRawInt() ?? 0);
if (uval == MaxUnsignedInt) if (uval == MaxUnsignedInt)
{ {
uval = 0; uval = 0;
@ -257,7 +262,7 @@ namespace BizHawk.Client.EmuHawk
Text = uval.ToString(); Text = uval.ToString();
break; break;
case Watch.DisplayType.Binary: case Watch.DisplayType.Binary:
uint bval = (uint)ToRawInt(); uint bval = (uint)(ToRawInt() ?? 0);
if (bval == MaxUnsignedInt) if (bval == MaxUnsignedInt)
{ {
bval = 0; bval = 0;
@ -270,7 +275,7 @@ namespace BizHawk.Client.EmuHawk
Text = Convert.ToString(bval, 2).PadLeft(numBits, '0'); Text = Convert.ToString(bval, 2).PadLeft(numBits, '0');
break; break;
case Watch.DisplayType.Hex: case Watch.DisplayType.Hex:
uint hexVal = (uint)ToRawInt(); uint hexVal = (uint)(ToRawInt() ?? 0);
if (hexVal == MaxUnsignedInt) if (hexVal == MaxUnsignedInt)
{ {
hexVal = 0; hexVal = 0;
@ -282,7 +287,7 @@ namespace BizHawk.Client.EmuHawk
Text = hexVal.ToHexString(MaxLength); Text = hexVal.ToHexString(MaxLength);
break; break;
case Watch.DisplayType.FixedPoint_12_4: case Watch.DisplayType.FixedPoint_12_4:
double f12val = double.Parse(Text); double f12val = double.Parse(text);
if (f12val > Max12_4 - _12_4_Unit) if (f12val > Max12_4 - _12_4_Unit)
{ {
f12val = 0; f12val = 0;
@ -294,7 +299,7 @@ namespace BizHawk.Client.EmuHawk
Text = f12val.ToString(); Text = f12val.ToString();
break; break;
case Watch.DisplayType.FixedPoint_20_12: case Watch.DisplayType.FixedPoint_20_12:
double f24val = double.Parse(Text); double f24val = double.Parse(text);
if (f24val >= Max20_12 - _20_12_Unit) if (f24val >= Max20_12 - _20_12_Unit)
{ {
f24val = 0; f24val = 0;
@ -306,7 +311,7 @@ namespace BizHawk.Client.EmuHawk
Text = f24val.ToString(); Text = f24val.ToString();
break; break;
case Watch.DisplayType.Float: case Watch.DisplayType.Float:
double dval = double.Parse(Text); double dval = double.Parse(text);
if (dval > double.MaxValue - 1) if (dval > double.MaxValue - 1)
{ {
dval = 0; dval = 0;
@ -325,8 +330,12 @@ namespace BizHawk.Client.EmuHawk
{ {
default: default:
case Watch.DisplayType.Signed: case Watch.DisplayType.Signed:
int val = ToRawInt(); int? val = ToRawInt();
if (val == 0) if (!val.HasValue)
{
Text = String.Empty;
}
else if (val == 0)
{ {
val = MaxSignedInt; val = MaxSignedInt;
} }
@ -362,7 +371,7 @@ namespace BizHawk.Client.EmuHawk
Text = Convert.ToString(bval, 2).PadLeft(numBits, '0'); Text = Convert.ToString(bval, 2).PadLeft(numBits, '0');
break; break;
case Watch.DisplayType.Hex: case Watch.DisplayType.Hex:
uint hexVal = (uint)ToRawInt(); uint hexVal = (uint)(ToRawInt() ?? 0);
if (hexVal == 0) if (hexVal == 0)
{ {
hexVal = MaxUnsignedInt; hexVal = MaxUnsignedInt;
@ -374,7 +383,7 @@ namespace BizHawk.Client.EmuHawk
Text = hexVal.ToHexString(MaxLength); Text = hexVal.ToHexString(MaxLength);
break; break;
case Watch.DisplayType.FixedPoint_12_4: case Watch.DisplayType.FixedPoint_12_4:
double f12val = double.Parse(Text); double f12val = double.Parse(text);
if (f12val < 0 + _12_4_Unit) if (f12val < 0 + _12_4_Unit)
{ {
f12val = Max12_4; f12val = Max12_4;
@ -386,7 +395,7 @@ namespace BizHawk.Client.EmuHawk
Text = f12val.ToString(); Text = f12val.ToString();
break; break;
case Watch.DisplayType.FixedPoint_20_12: case Watch.DisplayType.FixedPoint_20_12:
double f24val = double.Parse(Text); double f24val = double.Parse(text);
if (f24val < 0 + _20_12_Unit) if (f24val < 0 + _20_12_Unit)
{ {
f24val = Max20_12; f24val = Max20_12;
@ -398,7 +407,7 @@ namespace BizHawk.Client.EmuHawk
Text = f24val.ToString(); Text = f24val.ToString();
break; break;
case Watch.DisplayType.Float: case Watch.DisplayType.Float:
double dval = double.Parse(Text); double dval = double.Parse(text);
if (dval > double.MaxValue - 1) if (dval > double.MaxValue - 1)
{ {
dval = 0; dval = 0;
@ -478,11 +487,18 @@ namespace BizHawk.Client.EmuHawk
} }
} }
public int ToRawInt() public int? ToRawInt()
{ {
if (String.IsNullOrWhiteSpace(Text)) if (String.IsNullOrWhiteSpace(Text))
{ {
return 0; if (Nullable)
{
return null;
}
else
{
return 0;
}
} }
else else
{ {
@ -509,37 +525,44 @@ namespace BizHawk.Client.EmuHawk
} }
} }
public void SetFromRawInt(int val) public void SetFromRawInt(int? val)
{ {
switch (_type) if (val.HasValue)
{ {
default: switch (_type)
case Watch.DisplayType.Signed: {
Text = val.ToString(); default:
break; case Watch.DisplayType.Signed:
case Watch.DisplayType.Unsigned: Text = val.ToString();
uint uval = (uint)val; break;
Text = uval.ToString(); case Watch.DisplayType.Unsigned:
break; uint uval = (uint)val.Value;
case Watch.DisplayType.Binary: Text = uval.ToString();
uint bval = (uint)val; break;
int numBits = ((int)ByteSize) * 8; case Watch.DisplayType.Binary:
Text = Convert.ToString(bval, 2).PadLeft(numBits, '0'); uint bval = (uint)val.Value;
break; int numBits = ((int)ByteSize) * 8;
case Watch.DisplayType.Hex: Text = Convert.ToString(bval, 2).PadLeft(numBits, '0');
Text = val.ToHexString(MaxLength); break;
break; case Watch.DisplayType.Hex:
case Watch.DisplayType.FixedPoint_12_4: Text = val.Value.ToHexString(MaxLength);
Text = String.Format("{0:F5}", (val / 16.0)); break;
break; case Watch.DisplayType.FixedPoint_12_4:
case Watch.DisplayType.FixedPoint_20_12: Text = String.Format("{0:F5}", (val.Value / 16.0));
Text = String.Format("{0:F5}", (val / 4096.0)); break;
break; case Watch.DisplayType.FixedPoint_20_12:
case Watch.DisplayType.Float: Text = String.Format("{0:F5}", (val.Value / 4096.0));
byte[] bytes = BitConverter.GetBytes(val); break;
float _float = BitConverter.ToSingle(bytes, 0); case Watch.DisplayType.Float:
Text = String.Format("{0:F6}", _float); byte[] bytes = BitConverter.GetBytes(val.Value);
break; float _float = BitConverter.ToSingle(bytes, 0);
Text = String.Format("{0:F6}", _float);
break;
}
}
else
{
Text = String.Empty;
} }
} }
} }