/*************************************************************************
* ModernUO *
* Copyright 2019-2026 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: GridCell.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 . *
*************************************************************************/
using System.Runtime.CompilerServices;
namespace Server.Gumps;
///
/// Represents a pre-calculated cell position within a grid layout.
/// This is a value type with zero heap allocations.
///
public ref struct GridCell
{
public readonly int X;
public readonly int Y;
public readonly int Width;
public readonly int Height;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GridCell(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
///
/// Gets the right edge X coordinate (X + Width).
///
public int Right
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => X + Width;
}
///
/// Gets the bottom edge Y coordinate (Y + Height).
///
public int Bottom
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Y + Height;
}
///
/// Gets the horizontal center X coordinate.
///
public int CenterX
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => X + Width / 2;
}
///
/// Gets the vertical center Y coordinate.
///
public int CenterY
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Y + Height / 2;
}
///
/// Creates a new cell with uniform margin inset on all sides.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GridCell Inset(int margin) =>
new(X + margin, Y + margin, Width - margin * 2, Height - margin * 2);
///
/// Creates a new cell with separate horizontal and vertical margins.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GridCell Inset(int horizontal, int vertical) =>
new(X + horizontal, Y + vertical, Width - horizontal * 2, Height - vertical * 2);
///
/// Creates a new cell with individual margins for each side.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GridCell Inset(int left, int top, int right, int bottom) =>
new(X + left, Y + top, Width - left - right, Height - top - bottom);
///
/// Creates a new cell with an offset applied to the position.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GridCell Offset(int offsetX, int offsetY) => new(X + offsetX, Y + offsetY, Width, Height);
///
/// Creates a new cell with a different width.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GridCell WithWidth(int width) => new(X, Y, width, Height);
///
/// Creates a new cell with a different height.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GridCell WithHeight(int height) => new(X, Y, Width, height);
}