ModernUO/Projects/UOContent/Gumps/Base/GridLayout/GridSizeSpec.cs
Kamron Batman bc6735bd23
feat: Adds grid support for the DynamicGump system (#2306)
### Summary 

- Adds a new grid layout system for DynamicGump with zero heap allocations
 - Migrates SpawnerControllerGump from legacy GumpGrid to DynamicGump
 - Migrates CommandListGump from BaseGridGump to DynamicGump
 - Removes legacy GumpGrid (no longer needed)

 ### New Grid Layout Components

 | Component | Purpose |
 |-----------|---------|
 | `GridCell` | Value type for cell bounds (x, y, width, height) |
 | `GridSizeSpec` | Parses CSS-like sizing specs ("10*", "*", "100") |
 | `GridCalculator` | Computes track positions using stackalloc |
 | `ListViewLayout` | Pagination + column layout for list views |
 | `GridBuilderExtensions` | Extension methods accepting `GridCell` |
 | `GridEntryStyle` | Styling properties for BaseGridGump migration |
 | `GridEntryExtensions` | Entry methods matching BaseGridGump patterns |

 ### Memory Impact

 | Component | Legacy | New |
 |-----------|--------|-----|
 | Grid storage | ~200 bytes heap | 0 bytes (stackalloc) |
 | Column/Row lists | ~400 bytes heap | ~128 bytes stack |
 | ListView | ~300 bytes heap | ~64 bytes stack |
 | **Total per render** | **~900 bytes heap** | **0 bytes heap** |

 ### Test plan

 - [x] All 21 gump tests pass
 - [x] Build succeeds with no warnings
 - [ ] In-game verification of SpawnerControllerGump
 - [ ] In-game verification of CommandListGump (HelpInfo command)
2026-01-03 10:12:22 -08:00

162 lines
5.1 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: GridSizeSpec.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;
using System.Runtime.CompilerServices;
namespace Server.Gumps;
/// <summary>
/// Specifies how a grid column or row should be sized.
/// Supports absolute pixels, percentage, and star (proportional) sizing.
/// </summary>
public readonly struct GridSizeSpec
{
/// <summary>
/// The sizing type.
/// </summary>
public readonly GridSizeType Type;
/// <summary>
/// The value - meaning depends on Type:
/// - Absolute: pixel size
/// - Percent: percentage of total size (1-100)
/// - Star: proportional weight (1 = equal share)
/// </summary>
public readonly int Value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private GridSizeSpec(GridSizeType type, int value)
{
Type = type;
Value = value;
}
/// <summary>
/// Creates an absolute pixel size specification.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static GridSizeSpec Absolute(int pixels) => new(GridSizeType.Absolute, pixels);
/// <summary>
/// Creates a star (proportional) size specification.
/// Star-sized tracks share the remaining space equally (after absolute/percent are allocated).
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static GridSizeSpec Star() => new(GridSizeType.Star, 1);
/// <summary>
/// Creates a percentage size specification.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static GridSizeSpec Percent(int percent) => new(GridSizeType.Percent, percent);
/// <summary>
/// Parses a single size specification token.
/// Formats:
/// - "*" = star (equal share of remaining space)
/// - "10*" = 10% of total size
/// - "100" = 100 pixels absolute
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static GridSizeSpec Parse(ReadOnlySpan<char> token)
{
token = token.Trim();
if (token.IsEmpty)
{
return Star();
}
// Check for star notation
var starIndex = token.IndexOf('*');
if (starIndex == -1)
{
// No star - absolute pixel value
return int.TryParse(token, out var pixels) ? Absolute(pixels) : Star();
}
if (starIndex == 0 && token.Length == 1)
{
// Just "*" - equal share star
return Star();
}
// "N*" format - percentage
var percentPart = token[..starIndex];
return int.TryParse(percentPart, out var percent) ? Percent(percent) : Star();
}
/// <summary>
/// Parses a space-separated size specification string into a span of GridSizeSpec values.
/// Returns the number of specs parsed.
/// </summary>
public static int ParseAll(ReadOnlySpan<char> sizeSpec, Span<GridSizeSpec> destination)
{
var count = 0;
var remaining = sizeSpec;
while (!remaining.IsEmpty && count < destination.Length)
{
// Skip leading whitespace
remaining = remaining.TrimStart();
if (remaining.IsEmpty)
{
break;
}
// Find end of current token
var spaceIndex = remaining.IndexOf(' ');
ReadOnlySpan<char> token;
if (spaceIndex < 0)
{
token = remaining;
remaining = default;
}
else
{
token = remaining[..spaceIndex];
remaining = remaining[(spaceIndex + 1)..];
}
destination[count++] = Parse(token);
}
return count;
}
}
/// <summary>
/// Specifies the type of grid size specification.
/// </summary>
public enum GridSizeType : byte
{
/// <summary>
/// Fixed pixel size.
/// </summary>
Absolute,
/// <summary>
/// Star sizing - shares remaining space equally with other star-sized tracks.
/// </summary>
Star,
/// <summary>
/// Percentage of total available size.
/// </summary>
Percent
}