Lumi/companion/plugins/Lumi.Companion.Overlay/AnchorLayout.cs
2026-07-26 22:06:34 +02:00

41 lines
2.2 KiB
C#

namespace Lumi.Companion.Overlay;
public readonly record struct PhysicalRect(int X, int Y, int Width, int Height);
public static class AnchorLayout
{
public static void ClampPadding(OverlayContainerSettings settings)
{
var top = settings.Anchor is OverlayAnchor.TopLeft or OverlayAnchor.Top or OverlayAnchor.TopRight or OverlayAnchor.Left or OverlayAnchor.Right;
var bottom = settings.Anchor is OverlayAnchor.BottomLeft or OverlayAnchor.Bottom or OverlayAnchor.BottomRight or OverlayAnchor.Left or OverlayAnchor.Right;
var left = settings.Anchor is OverlayAnchor.TopLeft or OverlayAnchor.Left or OverlayAnchor.BottomLeft or OverlayAnchor.Top or OverlayAnchor.Bottom;
var right = settings.Anchor is OverlayAnchor.TopRight or OverlayAnchor.Right or OverlayAnchor.BottomRight or OverlayAnchor.Top or OverlayAnchor.Bottom;
if (!top) settings.PaddingTop = 0;
if (!right) settings.PaddingRight = 0;
if (!bottom) settings.PaddingBottom = 0;
if (!left) settings.PaddingLeft = 0;
}
public static PhysicalRect Calculate(OverlayContainerSettings value, int monitorWidth, int monitorHeight)
{
ClampPadding(value);
var availableWidth = Math.Max(1, monitorWidth - value.PaddingLeft - value.PaddingRight);
var availableHeight = Math.Max(1, monitorHeight - value.PaddingTop - value.PaddingBottom);
var width = Math.Clamp(value.Width, 1, availableWidth);
var height = Math.Clamp(value.Height, 1, availableHeight);
var x = value.Anchor switch
{
OverlayAnchor.TopRight or OverlayAnchor.Right or OverlayAnchor.BottomRight => monitorWidth - value.PaddingRight - width,
OverlayAnchor.Top or OverlayAnchor.Bottom => value.PaddingLeft + (availableWidth - width) / 2,
_ => value.PaddingLeft
};
var y = value.Anchor switch
{
OverlayAnchor.BottomLeft or OverlayAnchor.Bottom or OverlayAnchor.BottomRight => monitorHeight - value.PaddingBottom - height,
OverlayAnchor.Left or OverlayAnchor.Right => value.PaddingTop + (availableHeight - height) / 2,
_ => value.PaddingTop
};
return new(x, y, width, height);
}
}