Hex and Watch Boxes - prevent user from pasting invalid input, also if they do manage to get the textbox into an invalid state, interpret the value as 0 rather than throwing an exception
This commit is contained in:
parent
1964f3754a
commit
6bbd3ca80d
|
@ -213,7 +213,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
|
|
||||||
protected override void OnTextChanged(EventArgs e)
|
protected override void OnTextChanged(EventArgs e)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(Text))
|
if (string.IsNullOrWhiteSpace(Text) || !Text.IsHex())
|
||||||
{
|
{
|
||||||
ResetText();
|
ResetText();
|
||||||
}
|
}
|
||||||
|
@ -223,7 +223,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
|
|
||||||
public int? ToRawInt()
|
public int? ToRawInt()
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(Text))
|
if (string.IsNullOrWhiteSpace(Text) || !Text.IsHex())
|
||||||
{
|
{
|
||||||
if (Nullable)
|
if (Nullable)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1377,7 +1377,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
if (data != null && data.GetDataPresent(DataFormats.Text))
|
if (data != null && data.GetDataPresent(DataFormats.Text))
|
||||||
{
|
{
|
||||||
var clipboardRaw = (string)data.GetData(DataFormats.Text);
|
var clipboardRaw = (string)data.GetData(DataFormats.Text);
|
||||||
var hex = clipboardRaw.DoHexString();
|
var hex = clipboardRaw.OnlyHex();
|
||||||
|
|
||||||
var numBytes = hex.Length / 2;
|
var numBytes = hex.Length / 2;
|
||||||
for (var i = 0; i < numBytes; i++)
|
for (var i = 0; i < numBytes; i++)
|
||||||
|
|
|
@ -533,6 +533,31 @@ namespace BizHawk.Client.EmuHawk
|
||||||
{
|
{
|
||||||
ResetText();
|
ResetText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch (_type)
|
||||||
|
{
|
||||||
|
case Watch.DisplayType.Signed:
|
||||||
|
Text = Text.OnlySigned();
|
||||||
|
break;
|
||||||
|
case Watch.DisplayType.Unsigned:
|
||||||
|
Text = Text.OnlyUnsigned();
|
||||||
|
break;
|
||||||
|
case Watch.DisplayType.Binary:
|
||||||
|
Text = Text.OnlyBinary();
|
||||||
|
break;
|
||||||
|
case Watch.DisplayType.Hex:
|
||||||
|
Text = Text.OnlyHex();
|
||||||
|
break;
|
||||||
|
case Watch.DisplayType.FixedPoint_12_4:
|
||||||
|
case Watch.DisplayType.FixedPoint_20_12:
|
||||||
|
Text = Text.OnlyFixedPoint();
|
||||||
|
break;
|
||||||
|
case Watch.DisplayType.Float:
|
||||||
|
Text = Text.OnlyFloat();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
base.OnTextChanged(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int? ToRawInt()
|
public int? ToRawInt()
|
||||||
|
@ -549,24 +574,60 @@ namespace BizHawk.Client.EmuHawk
|
||||||
|
|
||||||
switch (_type)
|
switch (_type)
|
||||||
{
|
{
|
||||||
default:
|
|
||||||
case Watch.DisplayType.Signed:
|
case Watch.DisplayType.Signed:
|
||||||
|
if (Text.IsSigned())
|
||||||
|
{
|
||||||
return int.Parse(Text);
|
return int.Parse(Text);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
case Watch.DisplayType.Unsigned:
|
case Watch.DisplayType.Unsigned:
|
||||||
|
if (Text.IsUnsigned())
|
||||||
|
{
|
||||||
return (int)uint.Parse(Text);
|
return (int)uint.Parse(Text);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
case Watch.DisplayType.Binary:
|
case Watch.DisplayType.Binary:
|
||||||
|
if (Text.IsBinary())
|
||||||
|
{
|
||||||
return Convert.ToInt32(Text, 2);
|
return Convert.ToInt32(Text, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
case Watch.DisplayType.Hex:
|
case Watch.DisplayType.Hex:
|
||||||
|
if (Text.IsHex())
|
||||||
|
{
|
||||||
return int.Parse(Text, NumberStyles.HexNumber);
|
return int.Parse(Text, NumberStyles.HexNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
case Watch.DisplayType.FixedPoint_12_4:
|
case Watch.DisplayType.FixedPoint_12_4:
|
||||||
|
if (Text.IsFixedPoint())
|
||||||
|
{
|
||||||
return (int)(double.Parse(Text) * 16.0);
|
return (int)(double.Parse(Text) * 16.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
case Watch.DisplayType.FixedPoint_20_12:
|
case Watch.DisplayType.FixedPoint_20_12:
|
||||||
|
if (Text.IsFixedPoint())
|
||||||
|
{
|
||||||
return (int)(double.Parse(Text) * 4096.0);
|
return (int)(double.Parse(Text) * 4096.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
case Watch.DisplayType.Float:
|
case Watch.DisplayType.Float:
|
||||||
|
if (Text.IsFloat())
|
||||||
|
{
|
||||||
float val = float.Parse(Text);
|
float val = float.Parse(Text);
|
||||||
var bytes = BitConverter.GetBytes(val);
|
var bytes = BitConverter.GetBytes(val);
|
||||||
return BitConverter.ToInt32(bytes, 0);
|
return BitConverter.ToInt32(bytes, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetFromRawInt(int? val)
|
public void SetFromRawInt(int? val)
|
||||||
|
|
|
@ -149,29 +149,6 @@ namespace BizHawk.Common.StringExtensions
|
||||||
return char.ToUpper(c) >= 'A' && char.ToUpper(c) <= 'F';
|
return char.ToUpper(c) >= 'A' && char.ToUpper(c) <= 'F';
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Takes any string and removes any value that is not a valid hex value (0-9, a-f, A-F), returns the remaining characters in uppercase
|
|
||||||
/// </summary>
|
|
||||||
public static string DoHexString(this string raw)
|
|
||||||
{
|
|
||||||
if (raw == null)
|
|
||||||
{
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
var output = new StringBuilder();
|
|
||||||
|
|
||||||
foreach (var chr in raw)
|
|
||||||
{
|
|
||||||
if (IsHex(chr))
|
|
||||||
{
|
|
||||||
output.Append(char.ToUpper(chr));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return output.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validates all chars are 0 or 1
|
/// Validates all chars are 0 or 1
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -238,6 +215,190 @@ namespace BizHawk.Common.StringExtensions
|
||||||
return c.IsFixedPoint() || c == '-';
|
return c.IsFixedPoint() || c == '-';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Takes any string and removes any value that is not a valid binary value (0 or 1)
|
||||||
|
/// </summary>
|
||||||
|
public static string OnlyBinary(this string raw)
|
||||||
|
{
|
||||||
|
if (raw == null)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var output = new StringBuilder();
|
||||||
|
|
||||||
|
foreach (var chr in raw)
|
||||||
|
{
|
||||||
|
if (IsBinary(chr))
|
||||||
|
{
|
||||||
|
output.Append(chr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Takes any string and removes any value that is not a valid unsigned integer value (0-9)
|
||||||
|
/// </summary>
|
||||||
|
public static string OnlyUnsigned(this string raw)
|
||||||
|
{
|
||||||
|
if (raw == null)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var output = new StringBuilder();
|
||||||
|
|
||||||
|
foreach (var chr in raw)
|
||||||
|
{
|
||||||
|
if (IsUnsigned(chr))
|
||||||
|
{
|
||||||
|
output.Append(chr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Takes any string and removes any value that is not a valid unsigned integer value (0-9 or -)
|
||||||
|
/// Note: a "-" will only be kept if it is the first digit
|
||||||
|
/// </summary>
|
||||||
|
public static string OnlySigned(this string raw)
|
||||||
|
{
|
||||||
|
if (raw == null)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var output = new StringBuilder();
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
foreach (var chr in raw)
|
||||||
|
{
|
||||||
|
if (count == 0 && chr == '-')
|
||||||
|
{
|
||||||
|
output.Append(chr);
|
||||||
|
}
|
||||||
|
else if (IsUnsigned(chr))
|
||||||
|
{
|
||||||
|
|
||||||
|
output.Append(chr);
|
||||||
|
}
|
||||||
|
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Takes any string and removes any value that is not a valid hex value (0-9, a-f, A-F), returns the remaining characters in uppercase
|
||||||
|
/// </summary>
|
||||||
|
public static string OnlyHex(this string raw)
|
||||||
|
{
|
||||||
|
if (raw == null)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var output = new StringBuilder();
|
||||||
|
|
||||||
|
foreach (var chr in raw)
|
||||||
|
{
|
||||||
|
if (IsHex(chr))
|
||||||
|
{
|
||||||
|
output.Append(char.ToUpper(chr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Takes any string and removes any value that is not a fixed point value (0-9 or .)
|
||||||
|
/// Note: only the first occurance of a . will be kept
|
||||||
|
/// </summary>
|
||||||
|
public static string OnlyFixedPoint(this string raw)
|
||||||
|
{
|
||||||
|
if (raw == null)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var output = new StringBuilder();
|
||||||
|
|
||||||
|
bool usedDot = false;
|
||||||
|
foreach (var chr in raw)
|
||||||
|
{
|
||||||
|
if (chr == '.')
|
||||||
|
{
|
||||||
|
if (usedDot)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
usedDot = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsFixedPoint(chr))
|
||||||
|
{
|
||||||
|
output.Append(chr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Takes any string and removes any value that is not a float point value (0-9, -, or .)
|
||||||
|
/// Note: - is only valid as the first character, and only the first occurance of a . will be kept
|
||||||
|
/// </summary>
|
||||||
|
public static string OnlyFloat(this string raw)
|
||||||
|
{
|
||||||
|
if (raw == null)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var output = new StringBuilder();
|
||||||
|
|
||||||
|
bool usedDot = false;
|
||||||
|
int count = 0;
|
||||||
|
foreach (var chr in raw)
|
||||||
|
{
|
||||||
|
if (count == 0 && chr == '-')
|
||||||
|
{
|
||||||
|
output.Append(chr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (chr == '.')
|
||||||
|
{
|
||||||
|
if (usedDot)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
usedDot = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsFixedPoint(chr))
|
||||||
|
{
|
||||||
|
output.Append(chr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
count++;
|
||||||
|
return output.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue