using System; using UnityEngine; namespace DevilLink2.Presentation { public readonly struct BoardLayout { public float TileSize { get; } public Vector2 Size { get; } public float CellGap { get; } public float Padding { get; } public BoardLayout(float tileSize, Vector2 size) { TileSize = tileSize; Size = size; CellGap = BoardLayoutCalculator.Gap; Padding = BoardLayoutCalculator.Pad + BoardLayoutCalculator.Inset; } public BoardLayout(float tileSize, Vector2 size, float gap, float padding) { TileSize = tileSize; Size = size; CellGap = gap; Padding = padding; } public Vector2 TilePosition(int row, int column) => new Vector2( Padding + column * (TileSize + CellGap), -(Padding + row * (TileSize + CellGap))); } public static class BoardLayoutCalculator { public const int Gap = 2, Inset = 4, Pad = 12, AreaWidth = 418, AreaHeight = 385; public static BoardLayout Calculate(int columns, int rows) { if (columns <= 0 || rows <= 0) throw new ArgumentOutOfRangeException(); float tile = Mathf.Max(18, Mathf.Min(44, Mathf.Floor((AreaHeight - 2f * (Pad + Inset) - Gap * (rows - 1f)) / rows), Mathf.Floor((AreaWidth - 2f * (Pad + Inset) - Gap * (columns - 1f)) / columns))); return new BoardLayout(tile, new Vector2(columns * tile + Gap * (columns - 1f) + 2 * (Pad + Inset), rows * tile + Gap * (rows - 1f) + 2 * (Pad + Inset))); } // Presentation-only fit. Reserve enough margin for the OUTSIDE row/column path centers. public static BoardLayout CalculateResponsive(int columns, int rows, Vector2 area) { if (columns <= 0 || rows <= 0 || float.IsNaN(area.x) || float.IsNaN(area.y) || float.IsInfinity(area.x) || float.IsInfinity(area.y) || area.x <= 22 || area.y <= 22) throw new ArgumentOutOfRangeException(); const float gap = 3, pathClearance = 6; float tile = Mathf.Min(60, Mathf.Floor((area.x - 2 * pathClearance) / (columns + 1) - gap), Mathf.Floor((area.y - 2 * pathClearance) / (rows + 1) - gap)); if (tile < 1) throw new ArgumentOutOfRangeException(nameof(area)); float padding = tile * .5f + gap + pathClearance; return new BoardLayout(tile, new Vector2(columns * tile + (columns - 1) * gap + 2 * padding, rows * tile + (rows - 1) * gap + 2 * padding), gap, padding); } public static float FitWidth(BoardLayout layout, float viewportWidth) => Mathf.Min(1, Mathf.Max(0, viewportWidth) / layout.Size.x); } }