Add support for hexadecimal values in editor value fields

This commit is contained in:
Wojtek Figat
2020-12-31 15:35:31 +01:00
parent 3ab04598da
commit 9b891d0c55
2 changed files with 42 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using FlaxEngine;
namespace FlaxEditor.Utilities
{
@@ -172,24 +173,49 @@ namespace FlaxEditor.Utilities
// Necessary to correctly parse negative numbers
var previous = TokenType.WhiteSpace;
var token = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
var token = new StringBuilder();
var type = DetermineType(text[i]);
if (type == TokenType.WhiteSpace)
continue;
token.Clear();
token.Append(text[i]);
// Handle fractions and negative numbers (dot . is considered a figure)
if (type == TokenType.Number || text[i] == '-' && previous != TokenType.Number)
{
// Continue till the end of the number
while (i + 1 < text.Length && DetermineType(text[i + 1]) == TokenType.Number)
// Parse the whole number till the end
if (i + 1 < text.Length)
{
i++;
token.Append(text[i]);
switch (text[i + 1])
{
case 'x':
case 'X':
// Hexadecimal value
i++;
token.Clear();
if (i + 1 == text.Length || !StringUtils.IsHexDigit(text[i + 1]))
throw new ParsingException("invalid hexadecimal number");
while (i + 1 < text.Length && StringUtils.IsHexDigit(text[i + 1]))
{
i++;
token.Append(text[i]);
}
var value = ulong.Parse(token.ToString(), NumberStyles.HexNumber);
token.Clear();
token.Append(value.ToString());
break;
default:
// Decimal value
while (i + 1 < text.Length && DetermineType(text[i + 1]) == TokenType.Number)
{
i++;
token.Append(text[i]);
}
break;
}
}
// Discard solo '-'

View File

@@ -13,6 +13,16 @@ namespace FlaxEngine
/// </summary>
public static class StringUtils
{
/// <summary>
/// Checks if given character is valid hexadecimal digit.
/// </summary>
/// <param name="c">The hex character.</param>
/// <returns>True if character is valid hexadecimal digit, otherwise false.</returns>
public static bool IsHexDigit(char c)
{
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
/// <summary>
/// Parse hexadecimals digit to value.
/// </summary>