From 4415fa9fd538df7d49ab94e12a8b0d7a6dcc48be Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 21 Sep 2013 17:34:24 +0000 Subject: [PATCH] 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 --- .../BizHawk.MultiClient.csproj | 1 + .../BizHawk.MultiClient_v4.5.csproj | 3 + .../tools/Watch/RamPoke.Designer.cs | 5 +- BizHawk.MultiClient/tools/Watch/RamPoke.cs | 143 +----------- BizHawk.MultiClient/tools/Watch/Watch.cs | 9 +- .../tools/Watch/WatchValueBox.cs | 217 ++++++++++++++++++ BizHawk.Util/HexTextBox.cs | 54 ++--- 7 files changed, 255 insertions(+), 177 deletions(-) create mode 100644 BizHawk.MultiClient/tools/Watch/WatchValueBox.cs diff --git a/BizHawk.MultiClient/BizHawk.MultiClient.csproj b/BizHawk.MultiClient/BizHawk.MultiClient.csproj index 180ab83756..8c8af41efa 100644 --- a/BizHawk.MultiClient/BizHawk.MultiClient.csproj +++ b/BizHawk.MultiClient/BizHawk.MultiClient.csproj @@ -692,6 +692,7 @@ + FFmpegWriterForm.cs diff --git a/BizHawk.MultiClient/BizHawk.MultiClient_v4.5.csproj b/BizHawk.MultiClient/BizHawk.MultiClient_v4.5.csproj index a411e2c41f..4f45a4c13b 100644 --- a/BizHawk.MultiClient/BizHawk.MultiClient_v4.5.csproj +++ b/BizHawk.MultiClient/BizHawk.MultiClient_v4.5.csproj @@ -694,6 +694,9 @@ + + Component + FFmpegWriterForm.cs diff --git a/BizHawk.MultiClient/tools/Watch/RamPoke.Designer.cs b/BizHawk.MultiClient/tools/Watch/RamPoke.Designer.cs index ca58d7960d..be9ff46618 100644 --- a/BizHawk.MultiClient/tools/Watch/RamPoke.Designer.cs +++ b/BizHawk.MultiClient/tools/Watch/RamPoke.Designer.cs @@ -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; diff --git a/BizHawk.MultiClient/tools/Watch/RamPoke.cs b/BizHawk.MultiClient/tools/Watch/RamPoke.cs index 7c3ce00615..2059fe0f98 100644 --- a/BizHawk.MultiClient/tools/Watch/RamPoke.cs +++ b/BizHawk.MultiClient/tools/Watch/RamPoke.cs @@ -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 } } diff --git a/BizHawk.MultiClient/tools/Watch/Watch.cs b/BizHawk.MultiClient/tools/Watch/Watch.cs index 21c6f3e421..23e53171dc 100644 --- a/BizHawk.MultiClient/tools/Watch/Watch.cs +++ b/BizHawk.MultiClient/tools/Watch/Watch.cs @@ -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; diff --git a/BizHawk.MultiClient/tools/Watch/WatchValueBox.cs b/BizHawk.MultiClient/tools/Watch/WatchValueBox.cs new file mode 100644 index 0000000000..e85faa0842 --- /dev/null +++ b/BizHawk.MultiClient/tools/Watch/WatchValueBox.cs @@ -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); + } + } + } +} diff --git a/BizHawk.Util/HexTextBox.cs b/BizHawk.Util/HexTextBox.cs index 059ec91a9f..a789c12a25 100644 --- a/BizHawk.Util/HexTextBox.cs +++ b/BizHawk.Util/HexTextBox.cs @@ -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() {