Parser And Formatter
See the ParserAndFormatterDemo.cs.
using Gridrand.Contracts;
using Gridrand.RimGui.Manual;
using System;
using System.Globalization;
namespace Gridrand.RimGui.Extensions.Manual
{
/// <summary>
/// <see cref="ExtensionsStyle.Parsers"/> and <see cref="ExtensionsStyle.Formatters"/> demo.
/// </summary>
class ParserAndFormatterDemo : ManualBase, IManual
{
float percentFloatValue = 0.12f;
float frFloatValue = 123.45f;
int intValue = 100000000;
public ParserAndFormatterDemo(ManualBaseResource p) : base(p)
{
}
public void Draw()
{
Gui.Heading("bool");
using (Gui.BeginBox())
{
// Applies a BitBoolFormatter for boolean values
using (Formatters.Begin(BitBoolFormatter.Instance))
{
Gui.LabelNext("true").Text(true);
Gui.LabelNext("false").Text(false);
}
}
Gui.Heading("float");
using (Gui.BeginBox())
{
// Applies PercentageFloatParser and PercentageFloatFormatter
using (Parsers.Begin(PercentageFloatParser.Instance))
using (Formatters.Begin(PercentageFloatFormatter.Instance))
Gui.LabelNext("P2").InputNumeric(ref percentFloatValue);
// Applies FRFloatParser and FRFloatFormatter
using (Parsers.Begin(FRFloatParser.Instance))
using (Formatters.Begin(FRFloatFormatter.InstanceWithTwoDecimals))
Gui.LabelNext("fr-FR").InputNumeric(ref frFloatValue);
}
Gui.Heading("int");
using (Gui.BeginBox())
{
using (Style.Text.Colors.Begin(Color32.White))
Gui.Text("Int Format");
Gui.InputNumeric(ref intValue);
// General
using (Formatters.Begin(IntFormatter.GInstance))
Gui.LabelNext("G").TextNumeric(intValue);
// Uppercase hexadecimal
using (Formatters.Begin(IntFormatter.XInstance))
Gui.LabelNext("X").TextNumeric(intValue);
// Lowercase hexadecimal
using (Formatters.Begin(IntFormatter.xInstance))
Gui.LabelNext("x").TextNumeric(intValue);
// Decimal format. Pads with leading zeros if the specified number of digits is not met
using (Formatters.Begin(IntFormatter.D5Instance))
Gui.LabelNext("D5").TextNumeric(intValue);
// Number format with thousand separators
using (Formatters.Begin(IntFormatter.NInstance))
Gui.LabelNext("N").TextNumeric(intValue);
using (Formatters.Begin(IntFormatter.N0Instance))
Gui.LabelNext("N0").TextNumeric(intValue);
}
}
class BitBoolFormatter : IFormatter<bool>
{
public static readonly BitBoolFormatter Instance = new();
BitBoolFormatter() { }
public bool TryFormat(bool value, Span<char> destination, out int writtenCount)
{
if (destination.Length == 0)
{
writtenCount = 0;
return false;
}
if (value)
destination[0] = '1';
else
destination[0] = '0';
writtenCount = 1;
return true;
}
}
public class IntFormatter : IFormatter<int>
{
public static readonly IntFormatter GInstance = new("G");
public static readonly IntFormatter XInstance = new("X");
public static readonly IntFormatter D5Instance = new("D5");
public static readonly IntFormatter xInstance = new("x");
public static readonly IntFormatter NInstance = new("N");
public static readonly IntFormatter N0Instance = new("N0");
readonly string format;
public IntFormatter(string format)
{
this.format = format;
}
public bool TryFormat(int value, Span<char> destination, out int writtenCount)
{
return value.TryFormat(destination, out writtenCount, format);
}
}
class FRFloatFormatter : IFormatter<float>
{
public static FRFloatFormatter InstanceWithTwoDecimals { get; } = new();
static readonly CultureInfo FrCulture = CultureInfo.GetCultureInfo("fr-FR");
public bool TryFormat(float value, Span<char> destination, out int writtenCount)
{
return value.TryFormat(destination, out writtenCount, "F2", FrCulture);
}
}
class FRFloatParser : IParser<float>
{
public static FRFloatParser Instance { get; } = new();
static readonly CultureInfo FrCulture = CultureInfo.GetCultureInfo("fr-FR");
public bool TryParse(ReadOnlySpan<char> text, out float value)
{
return float.TryParse(text, NumberStyles.Float, FrCulture, out value);
}
}
class PercentageFloatFormatter : IFormatter<float>
{
public static PercentageFloatFormatter Instance { get; } = new();
static readonly CultureInfo InvariantCulture = CultureInfo.InvariantCulture;
public bool TryFormat(float value, Span<char> destination, out int writtenCount)
{
// 0.123 is 12.30%
return value.TryFormat(destination, out writtenCount, "P2", InvariantCulture);
}
}
class PercentageFloatParser : IParser<float>
{
public static PercentageFloatParser Instance { get; } = new();
static readonly CultureInfo InvariantCulture = CultureInfo.InvariantCulture;
public bool TryParse(ReadOnlySpan<char> text, out float value)
{
// Remove "%" and trim whitespace
var cleanText = text.ToString().Replace("%", "").Trim();
// Try parsing the cleaned text as a float
if (float.TryParse(cleanText, NumberStyles.Float | NumberStyles.AllowTrailingSign | NumberStyles.AllowParentheses, InvariantCulture, out float raw))
{
value = raw / 100f;
return true;
}
value = 0;
return false;
}
}
}
}