feat: Adds size/style support to gump builders, optimizes EscapeHtml (#2257)

> [!IMPORTANT]
> **Developer Note**
> THIS IS A BREAKING CHANGE TO THE NEW API GUMP.
> Please give us feedback in [discord ](https://muo.gg/discord) if you have issues, need help, or have ideas for a better API change!

### Summary

* Adds support for size/style to dynamic/static builder.
* Drastically simplifies the dynamic/static builder api for AddHtml.
* Cleans up some legacy gump files.
This commit is contained in:
Kamron Batman 2025-11-23 11:35:31 -08:00 • committed by GitHub
parent ea86d5b2a6
commit 7bd5a853a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 398 additions and 635 deletions

View file

@ -78,114 +78,33 @@ public ref struct DynamicGumpBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtml(
int x,
int y,
int width,
int height,
ReadOnlySpan<char> text,
bool background = false,
bool scrollbar = false
int x, int y, int width, int height, ReadOnlySpan<char> text, ReadOnlySpan<char> color = default, int size = -1, byte fontStyle = 0,
TextAlignment align = TextAlignment.Left, bool background = false, bool scrollbar = false
)
{
WriteInternalizedString(text);
if (color.Length > 0 || size != -1 || fontStyle != 0 || align != TextAlignment.Left)
{
var arr = STArrayPool<char>.Shared.Rent(Html.BuildCharCount(text, color));
var bytesWritten = Html.Build(text, arr.AsSpan(), color, size, fontStyle, align);
WriteInternalizedString(arr.AsSpan(0, bytesWritten));
STArrayPool<char>.Shared.Return(arr);
}
else
{
WriteInternalizedString(text);
}
_gumpBuilder.AddHtml(x, y, width, height, _stringsCount++, background, scrollbar);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtml(
int x, int y, int width, int height, ref RawInterpolatedStringHandler handler,
bool background = false, bool scrollbar = false
int x, int y, int width, int height, ref RawInterpolatedStringHandler handler, ReadOnlySpan<char> color = default, int size = -1,
byte fontStyle = 0, TextAlignment align = TextAlignment.Left, bool background = false, bool scrollbar = false
)
{
AddHtml(x, y, width, height, handler.Text, background, scrollbar);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtml(
int x,
int y,
int width,
int height,
int color,
ReadOnlySpan<char> text,
bool background = false,
bool scrollbar = false
)
{
var handler = text.Color(color);
AddHtml(x, y, width, height, ref handler, background, scrollbar);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtml(
int x,
int y,
int width,
int height,
int color,
ref RawInterpolatedStringHandler handler,
bool background = false,
bool scrollbar = false
)
{
AddHtml(x, y, width, height, color, handler.Text, background, scrollbar);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlCentered(
int x, int y, int width, int height, ReadOnlySpan<char> text, bool background = false, bool scrollbar = false
)
{
var handler = text.Center();
AddHtml(x, y, width, height, ref handler, background, scrollbar);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlCentered(
int x,
int y,
int width,
int height,
ref RawInterpolatedStringHandler handler,
bool background = false,
bool scrollbar = false
)
{
AddHtmlCentered(x, y, width, height, handler.Text, background, scrollbar);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlCentered(
int x,
int y,
int width,
int height,
int color,
ReadOnlySpan<char> text,
bool background = false,
bool scrollbar = false
)
{
var handler = text.Center(color);
AddHtml(x, y, width, height, ref handler, background, scrollbar);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlCentered(
int x,
int y,
int width,
int height,
int color,
ref RawInterpolatedStringHandler handler,
bool background = false,
bool scrollbar = false
)
{
AddHtmlCentered(x, y, width, height, color, handler.Text, background, scrollbar);
AddHtml(x, y, width, height, handler.Text, color, size, fontStyle, align, background, scrollbar);
handler.Clear();
}
@ -201,14 +120,7 @@ public ref struct DynamicGumpBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlLocalized(
int x,
int y,
int width,
int height,
int number,
ReadOnlySpan<char> args,
int color,
bool background = false,
int x, int y, int width, int height, int number, ReadOnlySpan<char> args, int color, bool background = false,
bool scrollbar = false
) => _gumpBuilder.AddHtmlLocalized(x, y, width, height, number, args, color, background, scrollbar);
@ -232,18 +144,8 @@ public ref struct DynamicGumpBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddImageTiledButton(
int x,
int y,
int normalId,
int pressedId,
int buttonId,
GumpButtonType type,
int param,
int itemId,
int hue,
int width,
int height,
int localizedTooltip = -1
int x, int y, int normalId, int pressedId, int buttonId, GumpButtonType type, int param, int itemId, int hue,
int width, int height, int localizedTooltip = -1
) => _gumpBuilder.AddImageTiledButton(
x, y, normalId, pressedId, buttonId, type, param, itemId, hue, width, height, localizedTooltip
);

View file

@ -1,77 +0,0 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Grid.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System.Collections.Generic;
namespace Server.Gumps;
public enum GridHues
{
White = 2049, // Change to 0x480 if you have old hues.mul
Black = 0,
}
public static class GridColors
{
public const string Gold = "#ffc959";
public const string White = "#ffffff";
public const string Blue = "#1D63DC";
public const string LightBlue = "#7799EE";
public const string Green = "#80E732";
public const string Orange = "#F28B00";
public const string Purple = "#F3ACF6";
public const string Red = "#B52B2D";
public const string LightGreen = "#E6FFC0";
public const string Yellow = "#FFFFBB";
}
public class ListItem
{
public int X { get; set; }
public int Y { get; set; }
public int Index { get; set; }
public Col[] Cols { get; set; }
}
public class Col
{
public int X { get; set; }
public int Width { get; set; }
public int HCenter => X + Width / 2;
public int HEnd => X + Width;
}
public class Row
{
public int Y { get; set; }
public int Height { get; set; }
public int VCenter => Y + Height / 2;
public int VEnd => Y + Height;
}
public class Grid
{
public string Name { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public List<Col> Columns { get; set; } = new();
public List<Row> Rows { get; set; } = new();
}
public class Swap
{
public int Index { get; set; }
public int Size { get; set; }
}

View file

@ -66,78 +66,46 @@ public ref struct GumpStringsBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlText(
ref RawInterpolatedStringHandler slotKeyHandler, ref RawInterpolatedStringHandler handler, int color, int size = -1,
byte fontStyle = 0
ref RawInterpolatedStringHandler slotKeyHandler, ref RawInterpolatedStringHandler handler, ReadOnlySpan<char> color = default, int size = -1,
byte fontStyle = 0, TextAlignment align = TextAlignment.Left
)
{
SetHtmlText(ref slotKeyHandler, handler.Text, color, size, fontStyle);
SetHtmlText(slotKeyHandler.Text, handler.Text, color, size, fontStyle, align);
slotKeyHandler.Clear();
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlText(
ref RawInterpolatedStringHandler slotKeyHandler, ReadOnlySpan<char> value, int color, int size = -1, byte fontStyle = 0
ref RawInterpolatedStringHandler slotKeyHandler, ReadOnlySpan<char> value, ReadOnlySpan<char> color = default, int size = -1, byte fontStyle = 0,
TextAlignment align = TextAlignment.Left
)
{
SetHtmlText(slotKeyHandler.Text, value, color, size, fontStyle);
SetHtmlText(slotKeyHandler.Text, value, color, size, fontStyle, align);
slotKeyHandler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlText(
ReadOnlySpan<char> slotKey, ref RawInterpolatedStringHandler handler, int color, int size = -1, byte fontStyle = 0
ReadOnlySpan<char> slotKey, ref RawInterpolatedStringHandler handler, ReadOnlySpan<char> color = default, int size = -1, byte fontStyle = 0,
TextAlignment align = TextAlignment.Left
)
{
SetHtmlText(slotKey, handler.Text, color, size, fontStyle);
SetHtmlText(slotKey, handler.Text, color, size, fontStyle, align);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetHtmlText(
ReadOnlySpan<char> slotKey, ReadOnlySpan<char> value, int color, int size = -1, byte fontStyle = 0
ReadOnlySpan<char> slotKey, ReadOnlySpan<char> value, ReadOnlySpan<char> color = default, int size = -1, byte fontStyle = 0,
TextAlignment align = TextAlignment.Left
)
{
var coloredTextHandler = value.Color(color, size, fontStyle);
SetStringSlot(slotKey, ref coloredTextHandler);
}
var arr = STArrayPool<char>.Shared.Rent(Html.BuildCharCount(value, color));
var bytesWritten = Html.Build(value, arr.AsSpan(), color, size, fontStyle, align);
[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);
SetStringSlot(slotKey, arr.AsSpan(0, bytesWritten));
STArrayPool<char>.Shared.Return(arr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View file

@ -169,7 +169,7 @@ public class Gump : BaseGump
public void AddLabelHtml(int x, int y, int width, int height, string text, string hue, int size = 4, bool center = true)
{
AddHtml(x, y, width, height, center ? text.Center(hue, size) : text.Color(hue, size));
AddHtml(x, y, width, height, Html.Build(text, hue, size, align: center ? TextAlignment.Center : TextAlignment.Left));
}
public void AddLabelCropped(int x, int y, int width, int height, int hue, string text)

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: GumpGrid.cs *
* *
@ -523,3 +523,63 @@ public class GumpGrid : Gump
return null;
}
}
public enum GridHues
{
White = 2049, // Change to 0x480 if you have old hues.mul
Black = 0,
}
public static class GridColors
{
public const string Gold = "#ffc959";
public const string White = "#ffffff";
public const string Blue = "#1D63DC";
public const string LightBlue = "#7799EE";
public const string Green = "#80E732";
public const string Orange = "#F28B00";
public const string Purple = "#F3ACF6";
public const string Red = "#B52B2D";
public const string LightGreen = "#E6FFC0";
public const string Yellow = "#FFFFBB";
}
public class ListItem
{
public int X { get; set; }
public int Y { get; set; }
public int Index { get; set; }
public Col[] Cols { get; set; }
}
public class Col
{
public int X { get; set; }
public int Width { get; set; }
public int HCenter => X + Width / 2;
public int HEnd => X + Width;
}
public class Row
{
public int Y { get; set; }
public int Height { get; set; }
public int VCenter => Y + Height / 2;
public int VEnd => Y + Height;
}
public class Grid
{
public string Name { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public List<Col> Columns { get; set; } = new();
public List<Row> Rows { get; set; } = new();
}
public class Swap
{
public int Index { get; set; }
public int Size { get; set; }
}

View file

@ -91,13 +91,7 @@ public ref struct StaticGumpBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlPlaceholder(
int x,
int y,
int width,
int height,
ReadOnlySpan<char> slotKey,
bool background = false,
bool scrollbar = false
int x, int y, int width, int height, ReadOnlySpan<char> slotKey, bool background = false, bool scrollbar = false
)
{
var index = _gumpBuilder.AddHtmlPlaceholder(x, y, width, height, background, scrollbar);
@ -106,12 +100,7 @@ public ref struct StaticGumpBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlPlaceholder(
int x,
int y,
int width,
int height,
ref RawInterpolatedStringHandler handler,
bool background = false,
int x, int y, int width, int height, ref RawInterpolatedStringHandler handler, bool background = false,
bool scrollbar = false
)
{
@ -121,115 +110,33 @@ public ref struct StaticGumpBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtml(
int x,
int y,
int width,
int height,
ReadOnlySpan<char> text,
bool background = false,
bool scrollbar = false
int x, int y, int width, int height, ReadOnlySpan<char> text, ReadOnlySpan<char> color = default, int size = -1, byte fontStyle = 0,
TextAlignment align = TextAlignment.Left, bool background = false, bool scrollbar = false
)
{
WriteInternalizedString(text);
if (color.Length > 0 || size != -1 || fontStyle != 0 || align != TextAlignment.Left)
{
var arr = STArrayPool<char>.Shared.Rent(Html.BuildCharCount(text, color));
var bytesWritten = Html.Build(text, arr.AsSpan(), color, size, fontStyle, align);
WriteInternalizedString(arr.AsSpan(0, bytesWritten));
STArrayPool<char>.Shared.Return(arr);
}
else
{
WriteInternalizedString(text);
}
_gumpBuilder.AddHtml(x, y, width, height, _stringsCount++, background, scrollbar);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtml(
int x, int y, int width, int height, ref RawInterpolatedStringHandler handler,
bool background = false, bool scrollbar = false
int x, int y, int width, int height, ref RawInterpolatedStringHandler handler, ReadOnlySpan<char> color = default, int size = -1,
byte fontStyle = 0, TextAlignment align = TextAlignment.Left, bool background = false, bool scrollbar = false
)
{
AddHtml(x, y, width, height, handler.Text, background, scrollbar);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtml(
int x,
int y,
int width,
int height,
int color,
ReadOnlySpan<char> text,
bool background = false,
bool scrollbar = false
)
{
var handler = text.Color(color);
AddHtml(x, y, width, height, ref handler, background, scrollbar);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtml(
int x,
int y,
int width,
int height,
int color,
ref RawInterpolatedStringHandler handler,
bool background = false,
bool scrollbar = false
)
{
AddHtml(x, y, width, height, color, handler.Text, background, scrollbar);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlCentered(
int x, int y, int width, int height, ReadOnlySpan<char> text, bool background = false, bool scrollbar = false
)
{
var handler = text.Center();
AddHtml(x, y, width, height, ref handler, background, scrollbar);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlCentered(
int x,
int y,
int width,
int height,
ref RawInterpolatedStringHandler handler,
bool background = false,
bool scrollbar = false
)
{
var centerHandler = handler.Text.Center();
AddHtml(x, y, width, height, ref centerHandler, background, scrollbar);
handler.Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlCentered(
int x,
int y,
int width,
int height,
int color,
ReadOnlySpan<char> text,
bool background = false,
bool scrollbar = false
)
{
var handler = text.Center(color);
AddHtml(x, y, width, height, ref handler, background, scrollbar);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHtmlCentered(
int x,
int y,
int width,
int height,
int color,
ref RawInterpolatedStringHandler handler,
bool background = false,
bool scrollbar = false
)
{
AddHtmlCentered(x, y, width, height, color, handler.Text, background, scrollbar);
AddHtml(x, y, width, height, handler.Text, color, size, fontStyle, align, background, scrollbar);
handler.Clear();
}