Create a new TexBox control called WatchValueBox which manages all the pre and post validation logic for typing a value based on all the watch display types, refactor Ram Poke to use this box instead of having its own logic

This commit is contained in:
adelikat 2013-09-21 17:34:24 +00:00
parent 52918e9609
commit 4415fa9fd5
7 changed files with 255 additions and 177 deletions

View File

@ -692,6 +692,7 @@
</Compile>
<Compile Include="tools\Watch\WatchLegacy.cs" />
<Compile Include="tools\Watch\WatchList.cs" />
<Compile Include="tools\Watch\WatchValueBox.cs" />
<Compile Include="XmlGame.cs" />
<EmbeddedResource Include="AVOut\FFmpegWriterForm.resx">
<DependentUpon>FFmpegWriterForm.cs</DependentUpon>

View File

@ -694,6 +694,9 @@
</Compile>
<Compile Include="tools\Watch\WatchLegacy.cs" />
<Compile Include="tools\Watch\WatchList.cs" />
<Compile Include="tools\Watch\WatchValueBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="XmlGame.cs" />
<EmbeddedResource Include="AVOut\FFmpegWriterForm.resx">
<DependentUpon>FFmpegWriterForm.cs</DependentUpon>

View File

@ -34,7 +34,7 @@
this.Cancel = new System.Windows.Forms.Button();
this.OutputLabel = new System.Windows.Forms.Label();
this.ValeLabel = new System.Windows.Forms.Label();
this.ValueBox = new System.Windows.Forms.TextBox();
this.ValueBox = new WatchValueBox();
this.ValueHexLabel = new System.Windows.Forms.Label();
this.DisplayTypeLabel = new System.Windows.Forms.Label();
this.SizeLabel = new System.Windows.Forms.Label();
@ -106,7 +106,6 @@
this.ValueBox.Size = new System.Drawing.Size(116, 20);
this.ValueBox.TabIndex = 10;
this.ValueBox.Text = "0000";
this.ValueBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.ValueBox_KeyPress);
//
// ValueHexLabel
//
@ -243,7 +242,7 @@
private System.Windows.Forms.Button Cancel;
private System.Windows.Forms.Label OutputLabel;
private System.Windows.Forms.Label ValeLabel;
private System.Windows.Forms.TextBox ValueBox;
private WatchValueBox ValueBox;
private System.Windows.Forms.Label ValueHexLabel;
private System.Windows.Forms.Label DisplayTypeLabel;
private System.Windows.Forms.Label SizeLabel;

View File

@ -65,79 +65,9 @@ namespace BizHawk.MultiClient
DisplayTypeLabel.Text = Watch.DisplayTypeToString(_watchList[0].Type);
BigEndianLabel.Text = _watchList[0].BigEndian ? "Big Endian" : "Little Endian";
SetTitle();
SetValueBoxProperties();
}
private void SetValueBoxProperties()
{
switch(_watchList[0].Type)
{
default:
ValueBox.MaxLength = 8;
break;
case Watch.DisplayType.Binary:
switch (_watchList[0].Size)
{
default:
case Watch.WatchSize.Byte:
ValueBox.MaxLength = 8;
break;
case Watch.WatchSize.Word:
ValueBox.MaxLength = 16;
break;
}
break;
case Watch.DisplayType.Hex:
switch (_watchList[0].Size)
{
default:
case Watch.WatchSize.Byte:
ValueBox.MaxLength = 2;
break;
case Watch.WatchSize.Word:
ValueBox.MaxLength = 4;
break;
case Watch.WatchSize.DWord:
ValueBox.MaxLength = 8;
break;
}
break;
case Watch.DisplayType.Signed:
switch (_watchList[0].Size)
{
default:
case Watch.WatchSize.Byte:
ValueBox.MaxLength = 4;
break;
case Watch.WatchSize.Word:
ValueBox.MaxLength = 6;
break;
case Watch.WatchSize.DWord:
ValueBox.MaxLength = 11;
break;
}
break;
case Watch.DisplayType.Unsigned:
switch (_watchList[0].Size)
{
default:
case Watch.WatchSize.Byte:
ValueBox.MaxLength = 3;
break;
case Watch.WatchSize.Word:
ValueBox.MaxLength = 5;
break;
case Watch.WatchSize.DWord:
ValueBox.MaxLength = 10;
break;
}
break;
case Watch.DisplayType.Float:
case Watch.DisplayType.FixedPoint_12_4:
case Watch.DisplayType.FixedPoint_20_12:
ValueBox.MaxLength = 32;
break;
}
ValueBox.ByteSize = _watchList[0].Size;
ValueBox.Type = _watchList[0].Type;
}
private void SetTitle()
@ -145,8 +75,6 @@ namespace BizHawk.MultiClient
Text = "Ram Poke - " + _watchList[0].Domain.Name;
}
#region Events
private void Cancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
@ -173,72 +101,5 @@ namespace BizHawk.MultiClient
OutputLabel.Text = "An error occured when writing Value.";
}
}
private void ValueBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\b' || e.KeyChar == 22 || e.KeyChar == 1 || e.KeyChar == 3)
{
return;
}
if (e.KeyChar == '.')
{
if (ValueBox.Text.Contains('.'))
{
e.Handled = true;
}
}
else if (e.KeyChar == '-')
{
if (ValueBox.Text.Contains('-'))
{
e.Handled = true;
}
}
switch(_watchList[0].Type)
{
case Watch.DisplayType.Signed:
if (!InputValidate.IsValidSignedNumber(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Unsigned:
if (!InputValidate.IsValidUnsignedNumber(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Hex:
if (!InputValidate.IsValidHexNumber(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Binary:
if (!InputValidate.IsValidBinaryNumber(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.FixedPoint_12_4:
case Watch.DisplayType.FixedPoint_20_12:
if (!InputValidate.IsValidFixedPointNumber(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Float:
if (!InputValidate.IsValidDecimalNumber(e.KeyChar))
{
e.Handled = true;
}
break;
}
}
#endregion
}
}

View File

@ -713,13 +713,12 @@ namespace BizHawk.MultiClient
case DisplayType.FixedPoint_12_4:
if (InputValidate.IsValidFixedPointNumber(value))
{
//TODO
throw new NotImplementedException();
}
else
{
return false;
}
break;
}
PokeWord(val);
return true;
@ -913,23 +912,21 @@ namespace BizHawk.MultiClient
case DisplayType.FixedPoint_20_12:
if (InputValidate.IsValidFixedPointNumber(value))
{
//TODO
throw new NotImplementedException();
}
else
{
return false;
}
break;
case DisplayType.Float:
if (InputValidate.IsValidDecimalNumber(value))
{
//TODO
throw new NotImplementedException();
}
else
{
return false;
}
break;
}
PokeDWord(val);
return true;

View File

@ -0,0 +1,217 @@
using System;
using System.Globalization;
using System.Windows.Forms;
namespace BizHawk.MultiClient
{
class WatchValueBox : TextBox
{
private Watch.WatchSize _size = Watch.WatchSize.Byte;
private Watch.DisplayType _type = Watch.DisplayType.Hex;
public WatchValueBox()
{
CharacterCasing = CharacterCasing.Upper;
}
public Watch.WatchSize ByteSize
{
get { return _size; }
set
{
if (_size != value)
{
if (!Watch.AvailableTypes(value).Contains(_type))
{
Type = Watch.AvailableTypes(value)[0];
}
}
_size = value;
}
}
public Watch.DisplayType Type
{
get { return _type; }
set
{
_type = value;
switch(_type)
{
default:
MaxLength = 8;
break;
case Watch.DisplayType.Binary:
switch (_size)
{
default:
case Watch.WatchSize.Byte:
MaxLength = 8;
break;
case Watch.WatchSize.Word:
MaxLength = 16;
break;
}
break;
case Watch.DisplayType.Hex:
switch (_size)
{
default:
case Watch.WatchSize.Byte:
MaxLength = 2;
break;
case Watch.WatchSize.Word:
MaxLength = 4;
break;
case Watch.WatchSize.DWord:
MaxLength = 8;
break;
}
break;
case Watch.DisplayType.Signed:
switch (_size)
{
default:
case Watch.WatchSize.Byte:
MaxLength = 4;
break;
case Watch.WatchSize.Word:
MaxLength = 6;
break;
case Watch.WatchSize.DWord:
MaxLength = 11;
break;
}
break;
case Watch.DisplayType.Unsigned:
switch (_size)
{
default:
case Watch.WatchSize.Byte:
MaxLength = 3;
break;
case Watch.WatchSize.Word:
MaxLength = 5;
break;
case Watch.WatchSize.DWord:
MaxLength = 10;
break;
}
break;
case Watch.DisplayType.Float:
case Watch.DisplayType.FixedPoint_12_4:
case Watch.DisplayType.FixedPoint_20_12:
MaxLength = 32;
break;
}
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == '\b' || e.KeyChar == 22 || e.KeyChar == 1 || e.KeyChar == 3)
{
return;
}
if (e.KeyChar == '.')
{
if (Text.Contains("."))
{
e.Handled = true;
return;
}
}
else if (e.KeyChar == '-')
{
if (Text.Contains("-"))
{
e.Handled = true;
return;
}
}
switch(_type)
{
default:
case Watch.DisplayType.Binary:
if (!InputValidate.IsValidBinaryNumber(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.FixedPoint_12_4:
case Watch.DisplayType.FixedPoint_20_12:
if (!InputValidate.IsValidFixedPointNumber(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Float:
if (!InputValidate.IsValidDecimalNumber(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Hex:
if (!InputValidate.IsValidHexNumber(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Signed:
if (!InputValidate.IsValidSignedNumber(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Unsigned:
if (!InputValidate.IsValidUnsignedNumber(e.KeyChar))
{
e.Handled = true;
}
break;
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
//if (InputValidate.IsValidHexNumber(Text))
//{
// int val = ToInt();
// val++;
// string formatstr = "{0:X" + MaxLength.ToString() + "}";
// Text = String.Format(formatstr, val);
//}
}
else if (e.KeyCode == Keys.Down)
{
//if (InputValidate.IsValidHexNumber(Text))
//{
// int val = ToInt();
// val--;
// string formatstr = "{0:X" + MaxLength.ToString() + "}";
// Text = String.Format(formatstr, val);
//}
}
else
{
base.OnKeyDown(e);
}
}
public int ToInt()
{
switch (_type)
{
default:
return int.Parse(Text);
case Watch.DisplayType.Binary:
return Convert.ToInt32(Text, 2);
case Watch.DisplayType.Hex:
return int.Parse(Text, NumberStyles.HexNumber);
}
}
}
}

View File

@ -23,33 +23,33 @@ namespace BizHawk
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
if (InputValidate.IsValidHexNumber(Text))
{
int val = ToInt();
val++;
string formatstr = "{0:X" + MaxLength.ToString() + "}";
Text = String.Format(formatstr, val);
}
}
else if (e.KeyCode == Keys.Down)
{
if (InputValidate.IsValidHexNumber(Text))
{
int val = ToInt();
val--;
string formatstr = "{0:X" + MaxLength.ToString() + "}";
Text = String.Format(formatstr, val);
}
}
else
{
base.OnKeyDown(e);
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
if (InputValidate.IsValidHexNumber(Text))
{
int val = ToInt();
val++;
string formatstr = "{0:X" + MaxLength.ToString() + "}";
Text = String.Format(formatstr, val);
}
}
else if (e.KeyCode == Keys.Down)
{
if (InputValidate.IsValidHexNumber(Text))
{
int val = ToInt();
val--;
string formatstr = "{0:X" + MaxLength.ToString() + "}";
Text = String.Format(formatstr, val);
}
}
else
{
base.OnKeyDown(e);
}
}
public int ToInt()
{