feat: Adds convenience methods to GumpStringsBuilder (#2124)

This commit is contained in:
Kamron Batman 2025-02-13 19:58:46 -08:00 committed by GitHub
parent 6d7b1b8bee
commit 90059c5e74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 269 additions and 36 deletions

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Html.cs *
* *
@ -23,65 +23,219 @@ namespace Server;
public static class Html
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Color(this string text, int color) => $"<BASEFONT COLOR=#{color:X6}>{text}</BASEFONT>";
public static RawInterpolatedStringHandler Color(
scoped ref RawInterpolatedStringHandler textHandler,
ReadOnlySpan<char> color,
int size = -1, byte fontStyle = 0
)
{
var handler = textHandler.Text.Color(color, size, fontStyle);
textHandler.Clear();
return handler;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RawInterpolatedStringHandler Color(this ReadOnlySpan<char> text, int color) =>
$"<BASEFONT COLOR=#{color:X6}>{text}</BASEFONT>";
public static RawInterpolatedStringHandler Color(
this ReadOnlySpan<char> text,
ReadOnlySpan<char> color,
int size = -1,
byte fontStyle = 0
)
{
if (color != Span<char>.Empty)
{
if (size > -1)
{
if (fontStyle > 0)
{
return $"<BASEFONT COLOR={color} SIZE={size} STYLE={fontStyle}>{text}</BASEFONT>";
}
return $"<BASEFONT COLOR={color} SIZE={size}>{text}</BASEFONT>";
}
if (fontStyle > 0)
{
return $"<BASEFONT COLOR={color} STYLE={fontStyle}>{text}</BASEFONT>";
}
return $"<BASEFONT COLOR={color}>{text}</BASEFONT>";
}
if (size > -1)
{
if (fontStyle > 0)
{
return $"<BASEFONT SIZE={size} STYLE={fontStyle}>{text}</BASEFONT>";
}
return $"<BASEFONT SIZE={size}>{text}</BASEFONT>";
}
if (fontStyle > 0)
{
return $"<BASEFONT STYLE={fontStyle}>{text}</BASEFONT>";
}
return $"{text}";
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Color(this string text, int color, int size) => $"<BASEFONT COLOR=#{color:X6} SIZE={size}>{text}</BASEFONT>";
public static RawInterpolatedStringHandler Color(
this ReadOnlySpan<char> text,
int color,
int size = -1,
byte fontStyle = 0
)
{
if (color > -1)
{
return text.Color($"#{color:X6}", size, fontStyle);
}
return text.Color((ReadOnlySpan<char>)default, size, fontStyle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Color(this string text, string color) => $"<BASEFONT COLOR={color}>{text}</BASEFONT>";
public static RawInterpolatedStringHandler Color(
scoped ref RawInterpolatedStringHandler textHandler,
int color,
int size = -1,
byte fontStyle = 0
)
{
var handler = textHandler.Text.Color(color, size, fontStyle);
textHandler.Clear();
return handler;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Color(this string text, string color, int size) =>
$"<BASEFONT COLOR={color} SIZE={size}>{text}</BASEFONT>";
public static string Color(
this string text,
ReadOnlySpan<char> color,
int size = -1,
byte fontStyle = 0
)
{
var textHandler = ((ReadOnlySpan<char>)text).Color(color, size, fontStyle);
var str = textHandler.Text.ToString();
textHandler.Clear();
return str;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Center(this string text) => $"<CENTER>{text}</CENTER>";
public static string Color(
this string text,
int color,
int size = -1,
byte fontStyle = 0
)
{
var textHandler = ((ReadOnlySpan<char>)text).Color(color, size, fontStyle);
var str = textHandler.Text.ToString();
textHandler.Clear();
return str;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RawInterpolatedStringHandler Center(this ReadOnlySpan<char> text) => $"<CENTER>{text}</CENTER>";
public static string Center(
this string text, int color, int size = -1, byte fontStyle = 0
)
{
var handler = Center((ReadOnlySpan<char>)text, color, size, fontStyle);
var str = handler.Text.ToString();
handler.Clear();
return str;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Center(this string text, int color) =>
$"<BASEFONT COLOR=#{color:X6}><CENTER>{text}</CENTER></BASEFONT>";
public static string Center(
this string text, ReadOnlySpan<char> color, int size = -1, byte fontStyle = 0
)
{
var handler = Center((ReadOnlySpan<char>)text, color, size, fontStyle);
var str = handler.Text.ToString();
handler.Clear();
return str;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RawInterpolatedStringHandler Center(this ReadOnlySpan<char> text, int color) =>
$"<BASEFONT COLOR=#{color:X6}><CENTER>{text}</CENTER></BASEFONT>";
public static string Center(this string text) => text.Center(-1);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Center(this string text, int color, int size) =>
$"<BASEFONT COLOR=#{color:X6} SIZE={size}><CENTER>{text}</CENTER></BASEFONT>";
public static RawInterpolatedStringHandler Center(this ReadOnlySpan<char> text) => Center(text, -1);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Center(this string text, string color) =>
$"<BASEFONT COLOR={color}><CENTER>{text}</CENTER></BASEFONT>";
public static RawInterpolatedStringHandler Center(
this ReadOnlySpan<char> text, int color, int size = -1, byte fontStyle = 0
) => Color($"<CENTER>{text}</CENTER>", color, size, fontStyle);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Center(this string text, string color, int size) =>
$"<BASEFONT COLOR={color} SIZE={size}><CENTER>{text}</CENTER></BASEFONT>";
public static RawInterpolatedStringHandler Center(
this ReadOnlySpan<char> text, ReadOnlySpan<char> color, int size = -1, byte fontStyle = 0
) => Color($"<CENTER>{text}</CENTER>", color, size, fontStyle);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Right(this string text) => $"<RIGHT>{text}</RIGHT>";
public static RawInterpolatedStringHandler Center(
scoped ref RawInterpolatedStringHandler textHandler, int color = -1, int size = -1, byte fontStyle = 0
)
{
var handler = textHandler.Text.Center(color, size, fontStyle);
textHandler.Clear();
return handler;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Right(this string text, int color) =>
$"<BASEFONT COLOR=#{color:X6}><RIGHT>{text}</RIGHT></BASEFONT>";
public static string Right(
this string text, int color, int size = -1, byte fontStyle = 0
)
{
var handler = Right((ReadOnlySpan<char>)text, color, size, fontStyle);
var str = handler.Text.ToString();
handler.Clear();
return str;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Right(this string text, int color, int size) =>
$"<BASEFONT COLOR=#{color:X6} SIZE={size}><RIGHT>{text}</RIGHT></BASEFONT>";
public static string Right(
this string text, ReadOnlySpan<char> color, int size = -1, byte fontStyle = 0
)
{
var handler = Right((ReadOnlySpan<char>)text, color, size, fontStyle);
var str = handler.Text.ToString();
handler.Clear();
return str;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Right(this string text, string color) => $"<BASEFONT COLOR={color}><RIGHT>{text}</RIGHT></BASEFONT>";
public static string Right(this string text) => text.Right(-1);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Right(this string text, string color, int size) =>
$"<BASEFONT COLOR={color} SIZE={size}><RIGHT>{text}</RIGHT></BASEFONT>";
public static RawInterpolatedStringHandler Right(
scoped ref RawInterpolatedStringHandler textHandler, int color = -1, int size = -1, byte fontStyle = 0
)
{
var handler = textHandler.Text.Right(color, size, fontStyle);
textHandler.Clear();
return handler;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RawInterpolatedStringHandler Right(
this ReadOnlySpan<char> text, int color, int size = -1, byte fontStyle = 0
) => Color($"<RIGHT>{text}</RIGHT>", color, size, fontStyle);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RawInterpolatedStringHandler Right(
this ReadOnlySpan<char> text, ReadOnlySpan<char> color, int size = -1, byte fontStyle = 0
) => Color($"<RIGHT>{text}</RIGHT>", color, size, fontStyle);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RawInterpolatedStringHandler Right(this ReadOnlySpan<char> text) => text.Right(-1);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string EscapeHtml(this string input) =>

View file

@ -37,7 +37,6 @@ public class StaticLayoutTestGump : StaticGump<StaticLayoutTestGump>
protected override void BuildStrings(ref GumpStringsBuilder builder)
{
var petNameText = _petName.AsSpan().Center();
builder.SetStringSlot("petName", ref petNameText);
builder.SetHtmlTextCentered("petName", _petName);
}
}

View file

@ -64,6 +64,82 @@ public ref struct GumpStringsBuilder
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlText(
ref RawInterpolatedStringHandler slotKeyHandler, ref RawInterpolatedStringHandler handler, int color, int size = -1,
byte fontStyle = 0
)
{
SetHtmlText(ref slotKeyHandler, handler.Text, color, size, fontStyle);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlText(
ref RawInterpolatedStringHandler slotKeyHandler, ReadOnlySpan<char> value, int color, int size = -1, byte fontStyle = 0
)
{
SetHtmlText(slotKeyHandler.Text, value, color, size, fontStyle);
slotKeyHandler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlText(
ReadOnlySpan<char> slotKey, ref RawInterpolatedStringHandler handler, int color, int size = -1, byte fontStyle = 0
)
{
SetHtmlText(slotKey, handler.Text, color, size, fontStyle);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlText(
ReadOnlySpan<char> slotKey, ReadOnlySpan<char> value, int color, int size = -1, byte fontStyle = 0
)
{
var coloredTextHandler = value.Color(color, size, fontStyle);
SetStringSlot(slotKey, ref coloredTextHandler);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlTextCentered(
ref RawInterpolatedStringHandler slotKeyHandler, ref RawInterpolatedStringHandler handler, int color = -1,
int size = -1, byte fontStyle = 0
)
{
SetHtmlTextCentered(ref slotKeyHandler, handler.Text, color, size, fontStyle);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlTextCentered(
ref RawInterpolatedStringHandler slotKeyHandler, ReadOnlySpan<char> value, int color = -1, int size = -1,
byte fontStyle = 0
)
{
SetHtmlTextCentered(slotKeyHandler.Text, value, color, size, fontStyle);
slotKeyHandler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlTextCentered(
ReadOnlySpan<char> slotKey, ref RawInterpolatedStringHandler handler, int color = -1, int size = -1,
byte fontStyle = 0
)
{
SetHtmlTextCentered(slotKey, handler.Text, color, size, fontStyle);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlTextCentered(
ReadOnlySpan<char> slotKey, ReadOnlySpan<char> value, int color = -1, int size = -1, byte fontStyle = 0
)
{
var coloredTextHandler = value.Center(color, size, fontStyle);
SetStringSlot(slotKey, ref coloredTextHandler);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetStringSlot(ReadOnlySpan<char> slotKey, ref RawInterpolatedStringHandler handler)
{
@ -74,11 +150,17 @@ public ref struct GumpStringsBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetStringSlot(ref RawInterpolatedStringHandler slotKeyHandler, ref RawInterpolatedStringHandler handler)
{
SetStringSlot(slotKeyHandler.Text, handler.Text);
slotKeyHandler.Clear();
SetStringSlot(ref slotKeyHandler, handler.Text);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetStringSlot(ref RawInterpolatedStringHandler slotKeyHandler, ReadOnlySpan<char> text)
{
SetStringSlot(slotKeyHandler.Text, text);
slotKeyHandler.Clear();
}
public void SetStringSlot(ReadOnlySpan<char> slotKey, ReadOnlySpan<char> text)
{
var hash = HashUtility.ComputeHash64(slotKey);

View file

@ -40,8 +40,7 @@ public class PetResurrectGump : StaticGump<PetResurrectGump>
protected override void BuildStrings(ref GumpStringsBuilder builder)
{
var petNameText = _pet.Name.AsSpan().Center();
builder.SetStringSlot("petName", ref petNameText);
builder.SetHtmlTextCentered("petName", _pet.Name);
}
public override void OnResponse(NetState state, in RelayInfo info)

View file

@ -88,8 +88,7 @@ public abstract class StaticWarningGump<T> : StaticGump<T> where T : StaticWarni
protected sealed override void BuildStrings(ref GumpStringsBuilder builder)
{
var contentText = Content.AsSpan().Color(ContentColor);
builder.SetStringSlot("content", ref contentText);
builder.SetHtmlText("content", Content, ContentColor);
}
public override void OnResponse(NetState sender, in RelayInfo info) => _callback?.Invoke(info.ButtonID == 1);