Extend board template builder with hex SVG canvas + filter client by grid type

- Admin builder: SVG hex canvas renders when GridType=Hexagon (pointy-top hexagons, correct axial layout); square CSS grid otherwise
- Admin builder: ApplyHexSides / OnGridTypeChanged handle dimension/tile pruning; canvas header shows N/diameter for hex
- BoardTemplateDto now includes GridType and HexSides so clients can filter
- rules_editor_dialog: Board Width/Height/Shape hidden when hex selected; Hex Sides hidden when square; template picker filtered to matching grid type with live toggle

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-04-01 21:22:07 -04:00
co-authored by Claude Happy
parent a4badc276b
commit 0572aea4d2
4 changed files with 248 additions and 68 deletions
@@ -89,24 +89,45 @@ else
<div class="col-lg-3">
<div class="card h-100">
<div class="card-header">Board Size</div>
<div class="card-header">Grid Type</div>
<div class="card-body">
<div class="mb-3">
<label class="form-label fw-semibold">Width (columns)</label>
<input type="number" class="form-control" min="1" max="32"
@bind="_pendingWidth" />
</div>
<div class="mb-3">
<label class="form-label fw-semibold">Height (rows)</label>
<input type="number" class="form-control" min="1" max="32"
@bind="_pendingHeight" />
</div>
<button class="btn btn-outline-secondary btn-sm w-100" @onclick="ApplySize">
Apply Size
</button>
<div class="form-text mt-1">
Tiles inside the new bounds are preserved.
<label class="form-label fw-semibold">Type</label>
<select class="form-select" @bind="_template.GridType" @bind:after="OnGridTypeChanged">
<option value="Square">Square</option>
<option value="Hexagon">Hexagon</option>
</select>
</div>
@if (_template.GridType == "Hexagon")
{
<div class="mb-3">
<label class="form-label fw-semibold">Hex Sides (N)</label>
<input type="number" class="form-control" min="3" max="10"
@bind="_pendingHexSides" />
<div class="form-text">Diameter = 2N−1. Tiles: 3N²−3N+1.</div>
</div>
<button class="btn btn-outline-secondary btn-sm w-100" @onclick="ApplyHexSides">
Apply Hex Sides
</button>
}
else
{
<div class="mb-3">
<label class="form-label fw-semibold">Width (columns)</label>
<input type="number" class="form-control" min="1" max="32"
@bind="_pendingWidth" />
</div>
<div class="mb-3">
<label class="form-label fw-semibold">Height (rows)</label>
<input type="number" class="form-control" min="1" max="32"
@bind="_pendingHeight" />
</div>
<button class="btn btn-outline-secondary btn-sm w-100" @onclick="ApplySize">
Apply Size
</button>
<div class="form-text mt-1">Tiles inside the new bounds are preserved.</div>
}
</div>
</div>
</div>
@@ -237,29 +258,72 @@ else
<div class="col-md-9 col-xl-10">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<span>Canvas <span class="text-muted small">@_template.Width × @_template.Height</span></span>
@if (_template.GridType == "Hexagon")
{
int _hn = _template.HexSides > 0 ? _template.HexSides : _pendingHexSides;
<span>Canvas <span class="text-muted small">N=@_hn hex (@(2*_hn-1)×@(2*_hn-1))</span></span>
}
else
{
<span>Canvas <span class="text-muted small">@_template.Width × @_template.Height</span></span>
}
<span class="text-muted small d-none d-md-inline">
Click / drag to paint &nbsp;·&nbsp; Double-click for tile flags
</span>
</div>
<div class="card-body p-2" style="overflow:auto;max-height:65vh;">
<div style="display:grid;grid-template-columns:repeat(@_template.Width, 28px);gap:1px;width:fit-content;user-select:none;cursor:crosshair;"
@onmouseup="() => _mouseDown = false"
@onmouseleave="() => _mouseDown = false">
@for (int row = 0; row < _template.Height; row++)
{
@for (int col = 0; col < _template.Width; col++)
@if (_template.GridType == "Hexagon")
{
const double HexR = 16.0;
const double HexPad = 12.0;
int hexN = _template.HexSides > 0 ? _template.HexSides : Math.Max(3, _pendingHexSides);
int hexDim = 2 * hexN - 1;
double svgW = HexR * Math.Sqrt(3) * (hexDim - 1) + 2 * HexR + 2 * HexPad;
double svgH = HexR * 1.5 * (hexDim - 1) + 2 * HexR + 2 * HexPad;
<svg width="@((int)Math.Ceiling(svgW))" height="@((int)Math.Ceiling(svgH))"
style="user-select:none;cursor:crosshair;display:block;"
@onmouseup="() => _mouseDown = false"
@onmouseleave="() => _mouseDown = false">
@for (int row = 0; row < hexDim; row++)
{
var r = row; var c = col;
<div style="width:28px;height:28px;background:@GetCellColor(c, r);border:1px solid rgba(0,0,0,0.12);"
title="@GetCellTooltip(c, r)"
@onmousedown="@(() => OnCellMouseDown(c, r))"
@onmouseenter="@(() => OnCellMouseEnter(c, r))"
@ondblclick="@(() => OpenTileModal(c, r))">
</div>
@for (int col = 0; col < hexDim; col++)
{
if (!IsHexInBounds(col, row, hexN)) continue;
var hr = row; var hc = col;
double cx = HexCellX(hc, hr, hexN, HexR, HexPad);
double cy = HexCellY(hr, hexN, HexR, HexPad);
<polygon points="@HexPolygonPoints(cx, cy, HexR - 1.0)"
fill="@GetCellColor(hc, hr)"
stroke="rgba(0,0,0,0.25)" stroke-width="1"
title="@GetCellTooltip(hc, hr)"
@onmousedown="@(() => OnCellMouseDown(hc, hr))"
@onmouseenter="@(() => OnCellMouseEnter(hc, hr))"
@ondblclick="@(() => OpenTileModal(hc, hr))">
</polygon>
}
}
}
</div>
</svg>
}
else
{
<div style="display:grid;grid-template-columns:repeat(@_template.Width, 28px);gap:1px;width:fit-content;user-select:none;cursor:crosshair;"
@onmouseup="() => _mouseDown = false"
@onmouseleave="() => _mouseDown = false">
@for (int row = 0; row < _template.Height; row++)
{
@for (int col = 0; col < _template.Width; col++)
{
var r = row; var c = col;
<div style="width:28px;height:28px;background:@GetCellColor(c, r);border:1px solid rgba(0,0,0,0.12);"
title="@GetCellTooltip(c, r)"
@onmousedown="@(() => OnCellMouseDown(c, r))"
@onmouseenter="@(() => OnCellMouseEnter(c, r))"
@ondblclick="@(() => OpenTileModal(c, r))">
</div>
}
}
</div>
}
</div>
</div>
</div>
@@ -329,8 +393,9 @@ else
// ── Template + in-memory state ────────────────────────────────────────────
private BoardTemplate _template = new();
private int _pendingWidth = 8;
private int _pendingHeight = 8;
private int _pendingWidth = 8;
private int _pendingHeight = 8;
private int _pendingHexSides = 5;
// Only non-default tiles stored; key = "x,y"
private Dictionary<string, TileDefinition> _tiles = new();
@@ -374,9 +439,10 @@ else
if (_isNew)
{
_template = new BoardTemplate { CreatedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow };
_pendingWidth = _template.Width;
_pendingHeight = _template.Height;
_template = new BoardTemplate { CreatedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow };
_pendingWidth = _template.Width;
_pendingHeight = _template.Height;
_pendingHexSides = 5;
}
else
{
@@ -387,9 +453,10 @@ else
_loading = false;
return;
}
_template = t;
_pendingWidth = t.Width;
_pendingHeight = t.Height;
_template = t;
_pendingWidth = t.Width;
_pendingHeight = t.Height;
_pendingHexSides = t.HexSides > 0 ? t.HexSides : 5;
DeserializeTiles(t.TilesJson);
DeserializeZones(t.StartingZonesJson);
}
@@ -458,6 +525,85 @@ else
});
}
private void ApplyHexSides()
{
var n = Math.Clamp(_pendingHexSides, 3, 10);
_pendingHexSides = n;
_template.HexSides = n;
int dim = 2 * n - 1;
_template.Width = dim;
_template.Height = dim;
var toRemoveTiles = _tiles.Keys
.Where(k => { var p = k.Split(','); return !IsHexInBounds(int.Parse(p[0]), int.Parse(p[1]), n); })
.ToList();
foreach (var k in toRemoveTiles) _tiles.Remove(k);
foreach (var seatMap in _zones.Values)
foreach (var tileSet in seatMap.Values)
tileSet.RemoveWhere(k =>
{
var p = k.Split(',');
return !IsHexInBounds(int.Parse(p[0]), int.Parse(p[1]), n);
});
}
private void OnGridTypeChanged()
{
_tiles.Clear();
_zones.Clear();
if (_template.GridType == "Hexagon")
{
_template.HexSides = _pendingHexSides;
int dim = 2 * _pendingHexSides - 1;
_template.Width = dim;
_template.Height = dim;
}
else
{
_template.HexSides = 0;
_template.Width = _pendingWidth;
_template.Height = _pendingHeight;
}
}
// ── Hex geometry helpers ──────────────────────────────────────────────────
private static bool IsHexInBounds(int x, int y, int N)
{
int q = x - (N - 1);
int r = y - (N - 1);
int s = -q - r;
return Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(s))) <= N - 1;
}
private static double HexCellX(int x, int y, int N, double R, double pad)
{
int q = x - (N - 1);
int r = y - (N - 1);
double offsetX = pad + R * Math.Sqrt(3) * (N - 1);
return offsetX + R * Math.Sqrt(3) * q + R * Math.Sqrt(3) / 2.0 * r;
}
private static double HexCellY(int y, int N, double R, double pad)
{
int r = y - (N - 1);
return pad + R * 1.5 * (N - 1) + R * 1.5 * r;
}
private static string HexPolygonPoints(double cx, double cy, double R)
{
var sb = new System.Text.StringBuilder();
for (int i = 0; i < 6; i++)
{
double angle = Math.PI / 180.0 * (60.0 * i - 30.0); // pointy-top
if (i > 0) sb.Append(' ');
sb.Append(FormattableString.Invariant(
$"{cx + R * Math.Cos(angle):F1},{cy + R * Math.Sin(angle):F1}"));
}
return sb.ToString();
}
// ── Cell interactions ─────────────────────────────────────────────────────
private void OnCellMouseDown(int x, int y)