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
@@ -116,42 +116,72 @@ func _build_fields(r: Dictionary) -> void:
var cur_is_hex : bool = get_s.call("boardShape", "Default") == "Hexagon" var cur_is_hex : bool = get_s.call("boardShape", "Default") == "Hexagon"
add_option.call("Grid Type", "_gridType", ["Square", "Hexagon"], add_option.call("Grid Type", "_gridType", ["Square", "Hexagon"],
"Hexagon" if cur_is_hex else "Square") "Hexagon" if cur_is_hex else "Square")
add_int.call("Board Width", "boardWidth", 3, 12, 6, "") var gt_opt := _controls["_gridType"] as OptionButton
add_int.call("Board Height", "boardHeight", 3, 12, 6, "")
add_int.call("Starting Tile Affinity %","startingRandomTilePct",0, 100, 0, "% of tiles with a random basic affinity")
add_option.call("Board Shape", "boardShape", ["Default", "Rectangle", "Cross", "Triangular", "RadialStar", "HexRing"], "Default")
add_int.call("Hex Sides", "hexSides", 3, 10, 5, "Hexagon only: tiles per side (3–10)")
# Board template picker — populate from BoardTemplateCache (GUID values, not labels). # Square-only rows
var tmpl_key := "boardTemplateIdOverride" add_int.call("Board Width", "boardWidth", 3, 12, 6, "")
var cur_tmpl_id : Variant = get_s.call(tmpl_key, "") var sq_width_row := fields_box.get_child(fields_box.get_child_count() - 1)
var custom_tmpls : Array = BoardTemplateCache.get_templates_for_custom() add_int.call("Board Height", "boardHeight", 3, 12, 6, "")
var sq_height_row := fields_box.get_child(fields_box.get_child_count() - 1)
add_int.call("Starting Tile Affinity %", "startingRandomTilePct", 0, 100, 0, "% of tiles with a random basic affinity")
add_option.call("Board Shape", "boardShape", ["Default", "Rectangle", "Cross", "Triangular", "RadialStar", "HexRing"], "Default")
var sq_shape_row := fields_box.get_child(fields_box.get_child_count() - 1)
# Hex-only rows
add_int.call("Hex Sides", "hexSides", 3, 10, 5, "Hexagon only: tiles per side (3–10)")
var hex_sides_row := fields_box.get_child(fields_box.get_child_count() - 1)
# Board template picker — filtered by current grid type.
var tmpl_key := "boardTemplateIdOverride"
var cur_tmpl_id : Variant = get_s.call(tmpl_key, "")
var all_custom_tmpls : Array = BoardTemplateCache.get_templates_for_custom()
var tmpl_hbox := HBoxContainer.new() var tmpl_hbox := HBoxContainer.new()
var tmpl_lbl := Label.new() var tmpl_lbl := Label.new()
tmpl_lbl.text = "Board Template" tmpl_lbl.text = "Board Template"
tmpl_lbl.tooltip_text = "Overrides the board layout with an admin-defined template. Ignores Board Shape when set." tmpl_lbl.tooltip_text = "Overrides the board layout with an admin-defined template. Ignores Board Shape when set."
tmpl_lbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL tmpl_lbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL
tmpl_hbox.add_child(tmpl_lbl) tmpl_hbox.add_child(tmpl_lbl)
var tmpl_opt := OptionButton.new() var tmpl_opt := OptionButton.new()
var tmpl_ids : Array[String] = []
tmpl_opt.add_item("— None —")
tmpl_ids.append("")
var tmpl_sel := 0
for i: int in range(custom_tmpls.size()):
var t := custom_tmpls[i] as Dictionary
var tid := str(t.get("id", ""))
var tname := str(t.get("name", tid.substr(0, 8)))
var tw := int(t.get("width", 0))
var th := int(t.get("height", 0))
tmpl_opt.add_item("%s (%d×%d)" % [tname, tw, th])
tmpl_ids.append(tid)
if tid == cur_tmpl_id:
tmpl_sel = i + 1
tmpl_opt.selected = tmpl_sel
tmpl_hbox.add_child(tmpl_opt) tmpl_hbox.add_child(tmpl_opt)
fields_box.add_child(tmpl_hbox) fields_box.add_child(tmpl_hbox)
_controls[tmpl_key] = tmpl_opt _controls[tmpl_key] = tmpl_opt
_option_ids[tmpl_key] = tmpl_ids _option_ids[tmpl_key] = []
# Rebuilds template picker items to match the given grid type.
var _fill_tmpl := func(is_hex: bool) -> void:
var opt := _controls.get(tmpl_key) as OptionButton
if not is_instance_valid(opt):
return
var ids : Array[String] = []
opt.clear()
opt.add_item("— None —")
ids.append("")
var sel := 0
for t: Dictionary in all_custom_tmpls:
var t_is_hex := str(t.get("gridType", "Square")) == "Hexagon"
if t_is_hex != is_hex:
continue
var tid := str(t.get("id", ""))
var tname := str(t.get("name", tid.substr(0, min(8, tid.length()))))
var tw := int(t.get("width", 0))
var th := int(t.get("height", 0))
opt.add_item("%s (%d×%d)" % [tname, tw, th])
ids.append(tid)
if tid == cur_tmpl_id:
sel = ids.size() - 1
opt.selected = sel
_option_ids[tmpl_key] = ids
# Toggles visibility of grid-type-specific rows and refreshes the template picker.
var _update_board_vis := func(is_hex: bool) -> void:
sq_width_row.visible = not is_hex
sq_height_row.visible = not is_hex
sq_shape_row.visible = not is_hex
hex_sides_row.visible = is_hex
_fill_tmpl.call(is_hex)
_update_board_vis.call(cur_is_hex)
gt_opt.item_selected.connect(func(idx: int): _update_board_vis.call(idx == 1))
# ── Deck ─────────────────────────────────────────────────────────────────── # ── Deck ───────────────────────────────────────────────────────────────────
_section("DECK") _section("DECK")
@@ -89,24 +89,45 @@ else
<div class="col-lg-3"> <div class="col-lg-3">
<div class="card h-100"> <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="card-body">
<div class="mb-3"> <div class="mb-3">
<label class="form-label fw-semibold">Width (columns)</label> <label class="form-label fw-semibold">Type</label>
<input type="number" class="form-control" min="1" max="32" <select class="form-select" @bind="_template.GridType" @bind:after="OnGridTypeChanged">
@bind="_pendingWidth" /> <option value="Square">Square</option>
</div> <option value="Hexagon">Hexagon</option>
<div class="mb-3"> </select>
<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>
@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> </div>
</div> </div>
@@ -237,29 +258,72 @@ else
<div class="col-md-9 col-xl-10"> <div class="col-md-9 col-xl-10">
<div class="card"> <div class="card">
<div class="card-header d-flex justify-content-between align-items-center"> <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"> <span class="text-muted small d-none d-md-inline">
Click / drag to paint &nbsp;·&nbsp; Double-click for tile flags Click / drag to paint &nbsp;·&nbsp; Double-click for tile flags
</span> </span>
</div> </div>
<div class="card-body p-2" style="overflow:auto;max-height:65vh;"> <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;" @if (_template.GridType == "Hexagon")
@onmouseup="() => _mouseDown = false" {
@onmouseleave="() => _mouseDown = false"> const double HexR = 16.0;
@for (int row = 0; row < _template.Height; row++) const double HexPad = 12.0;
{ int hexN = _template.HexSides > 0 ? _template.HexSides : Math.Max(3, _pendingHexSides);
@for (int col = 0; col < _template.Width; col++) 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; @for (int col = 0; col < hexDim; col++)
<div style="width:28px;height:28px;background:@GetCellColor(c, r);border:1px solid rgba(0,0,0,0.12);" {
title="@GetCellTooltip(c, r)" if (!IsHexInBounds(col, row, hexN)) continue;
@onmousedown="@(() => OnCellMouseDown(c, r))" var hr = row; var hc = col;
@onmouseenter="@(() => OnCellMouseEnter(c, r))" double cx = HexCellX(hc, hr, hexN, HexR, HexPad);
@ondblclick="@(() => OpenTileModal(c, r))"> double cy = HexCellY(hr, hexN, HexR, HexPad);
</div> <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>
}
} }
} </svg>
</div> }
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> </div>
</div> </div>
@@ -329,8 +393,9 @@ else
// ── Template + in-memory state ──────────────────────────────────────────── // ── Template + in-memory state ────────────────────────────────────────────
private BoardTemplate _template = new(); private BoardTemplate _template = new();
private int _pendingWidth = 8; private int _pendingWidth = 8;
private int _pendingHeight = 8; private int _pendingHeight = 8;
private int _pendingHexSides = 5;
// Only non-default tiles stored; key = "x,y" // Only non-default tiles stored; key = "x,y"
private Dictionary<string, TileDefinition> _tiles = new(); private Dictionary<string, TileDefinition> _tiles = new();
@@ -374,9 +439,10 @@ else
if (_isNew) if (_isNew)
{ {
_template = new BoardTemplate { CreatedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow }; _template = new BoardTemplate { CreatedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow };
_pendingWidth = _template.Width; _pendingWidth = _template.Width;
_pendingHeight = _template.Height; _pendingHeight = _template.Height;
_pendingHexSides = 5;
} }
else else
{ {
@@ -387,9 +453,10 @@ else
_loading = false; _loading = false;
return; return;
} }
_template = t; _template = t;
_pendingWidth = t.Width; _pendingWidth = t.Width;
_pendingHeight = t.Height; _pendingHeight = t.Height;
_pendingHexSides = t.HexSides > 0 ? t.HexSides : 5;
DeserializeTiles(t.TilesJson); DeserializeTiles(t.TilesJson);
DeserializeZones(t.StartingZonesJson); 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 ───────────────────────────────────────────────────── // ── Cell interactions ─────────────────────────────────────────────────────
private void OnCellMouseDown(int x, int y) private void OnCellMouseDown(int x, int y)
+3 -1
View File
@@ -208,7 +208,9 @@ public sealed record BoardTemplateDto(
bool IsAvailableForRanked, bool IsAvailableForRanked,
bool IsAvailableForCustom, bool IsAvailableForCustom,
string TilesJson, string TilesJson,
string StartingZonesJson); string StartingZonesJson,
string GridType = "Square",
int HexSides = 0);
// ── BGM config ─────────────────────────────────────────────────────────────── // ── BGM config ───────────────────────────────────────────────────────────────
@@ -40,7 +40,9 @@ internal sealed class ContentBoardTemplatesHandler : IMessageHandler
t.IsAvailableForRanked, t.IsAvailableForRanked,
t.IsAvailableForCustom, t.IsAvailableForCustom,
t.TilesJson, t.TilesJson,
t.StartingZonesJson)) t.StartingZonesJson,
t.GridType,
t.HexSides))
.ToListAsync(ct); .ToListAsync(ct);
return ServerMessage.Reply( return ServerMessage.Reply(