All 26 pages had @attribute [Authorize(Roles = "Admin,SuperAdmin")] which
conflicts with the Keycloak role sor-admin. The fallback policy in Program.cs
already enforces RequireRole("sor-admin") globally, making these attributes
redundant. Removing them fixes Access Denied for all admin pages.
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>
843 lines
35 KiB
Plaintext
843 lines
35 KiB
Plaintext
@page "/content/tile-models"
|
||
@using Amazon.S3
|
||
@using Amazon.S3.Model
|
||
@using Microsoft.EntityFrameworkCore
|
||
@using Microsoft.Extensions.Options
|
||
@using SOR.Cdn
|
||
@using SOR.Database.Contexts
|
||
@using SOR.Database.Entities.Catalog
|
||
@using SOR.Database.Enums
|
||
@inject CatalogDbContext Db
|
||
@inject IAmazonS3 S3
|
||
@inject IOptions<CdnOptions> CdnOptionsAccessor
|
||
|
||
<PageTitle>Tile Models – SOR Admin</PageTitle>
|
||
|
||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||
<div>
|
||
<h1 class="mb-0">Tile Model Manager</h1>
|
||
<div class="text-muted small">Assign 3D models to tile affinities for Square and Hex grids</div>
|
||
</div>
|
||
<button class="btn btn-outline-secondary btn-sm" @onclick="LoadAsync" disabled="@_loading">
|
||
<i class="bi bi-arrow-clockwise me-1"></i>Refresh
|
||
</button>
|
||
</div>
|
||
|
||
@if (_message is not null)
|
||
{
|
||
<div class="alert @(_isError ? "alert-danger" : "alert-success") alert-dismissible">
|
||
@_message
|
||
<button type="button" class="btn-close" @onclick='() => _message = null'></button>
|
||
</div>
|
||
}
|
||
|
||
@if (_loading)
|
||
{
|
||
<p class="text-muted">Loading…</p>
|
||
}
|
||
else
|
||
{
|
||
<!-- Category sections -->
|
||
@foreach (var group in _affinityGroups)
|
||
{
|
||
<div class="card shadow-sm mb-4">
|
||
<div class="card-header fw-medium">@group.Label</div>
|
||
<div class="card-body p-0">
|
||
<table class="table table-sm table-hover mb-0">
|
||
<thead class="table-light">
|
||
<tr>
|
||
<th style="width:160px">Affinity</th>
|
||
<th>Square Model</th>
|
||
<th>Square Texture</th>
|
||
<th>Hex Model</th>
|
||
<th>Hex Texture</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
@foreach (var affinity in group.Affinities)
|
||
{
|
||
var squareBinding = _bindings.GetValueOrDefault((affinity, TileShape.Square));
|
||
var hexBinding = _bindings.GetValueOrDefault((affinity, TileShape.Hex));
|
||
|
||
<tr>
|
||
<td class="align-middle fw-semibold small">@affinity</td>
|
||
<td class="align-middle">
|
||
@ModelCell(squareBinding, affinity, TileShape.Square)
|
||
</td>
|
||
<td class="align-middle">
|
||
@TextureCell(squareBinding, affinity, TileShape.Square)
|
||
</td>
|
||
<td class="align-middle">
|
||
@ModelCell(hexBinding, affinity, TileShape.Hex)
|
||
</td>
|
||
<td class="align-middle">
|
||
@TextureCell(hexBinding, affinity, TileShape.Hex)
|
||
</td>
|
||
</tr>
|
||
}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
}
|
||
}
|
||
|
||
<!-- ── Tile Bottoms (stalagmite undersides) ── -->
|
||
<div class="card shadow-sm mb-4">
|
||
<div class="card-header fw-medium d-flex justify-content-between align-items-center">
|
||
<span>Tile Bottom Models <span class="text-muted small fw-normal">(stalagmites, randomly selected per tile)</span></span>
|
||
<button class="btn btn-sm btn-outline-primary" @onclick="OpenBottomPickerAsync">
|
||
<i class="bi bi-plus me-1"></i>Add
|
||
</button>
|
||
</div>
|
||
<div class="card-body p-0">
|
||
@if (_bottomModels.Count == 0)
|
||
{
|
||
<div class="text-muted text-center py-3 small">No bottom models yet — upload GLBs and add them here.</div>
|
||
}
|
||
else
|
||
{
|
||
<table class="table table-sm table-hover mb-0">
|
||
<thead class="table-light">
|
||
<tr>
|
||
<th>Model</th>
|
||
<th style="width:80px">Active</th>
|
||
<th style="width:100px"></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
@foreach (var bottom in _bottomModels)
|
||
{
|
||
var b = bottom;
|
||
<tr>
|
||
<td class="align-middle font-monospace small text-break">
|
||
@(b.CdnKey.Contains('/') ? b.CdnKey[(b.CdnKey.LastIndexOf('/') + 1)..] : b.CdnKey)
|
||
</td>
|
||
<td class="align-middle">
|
||
<input type="checkbox" class="form-check-input" checked="@b.IsActive"
|
||
@onchange="e => ToggleBottomActiveAsync(b, (bool)e.Value!)" />
|
||
</td>
|
||
<td class="align-middle text-end pe-2">
|
||
<button class="btn btn-sm btn-outline-info py-0 me-1"
|
||
@onclick="() => _previewUrl = b.PublicUrl" title="Preview">
|
||
<i class="bi bi-box"></i>
|
||
</button>
|
||
<button class="btn btn-sm btn-outline-danger py-0"
|
||
@onclick="() => DeleteBottomAsync(b)" title="Delete">
|
||
<i class="bi bi-trash"></i>
|
||
</button>
|
||
</td>
|
||
</tr>
|
||
}
|
||
</tbody>
|
||
</table>
|
||
}
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Assign / Picker Modal -->
|
||
@if (_pickerOpen)
|
||
{
|
||
<div class="modal d-block" tabindex="-1" style="background:rgba(0,0,0,0.55)">
|
||
<div class="modal-dialog modal-xl modal-dialog-scrollable">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h5 class="modal-title">
|
||
Assign Model — <span class="text-primary">@_pickerAffinity</span>
|
||
<span class="badge bg-secondary ms-2">@_pickerShape</span>
|
||
</h5>
|
||
<button type="button" class="btn-close" @onclick="ClosePicker"></button>
|
||
</div>
|
||
<div class="modal-body">
|
||
<input @bind="_pickerSearch" @bind:event="oninput"
|
||
class="form-control form-control-sm font-monospace mb-3"
|
||
placeholder="Filter models…" />
|
||
|
||
@if (_pickerLoading)
|
||
{
|
||
<div class="text-muted text-center py-4">
|
||
<span class="spinner-border spinner-border-sm me-2"></span>Loading CDN models…
|
||
</div>
|
||
}
|
||
else if (!_pickerModels.Any())
|
||
{
|
||
<div class="text-muted text-center py-4">
|
||
No .glb or .gltf files found in the <code>models/</code> prefix.
|
||
Upload models via the CDN Browser first.
|
||
</div>
|
||
}
|
||
else
|
||
{
|
||
<div class="row g-3">
|
||
@foreach (var model in PickerFiltered())
|
||
{
|
||
var m = model;
|
||
var isSelected = _pickerSelected == m.Key;
|
||
<div class="col-md-4">
|
||
<div class="card h-100 @(isSelected ? "border-primary" : "")"
|
||
style="cursor:pointer" @onclick="() => _pickerSelected = m.Key">
|
||
@if (_pickerPreview == m.Key)
|
||
{
|
||
<div style="background:#f0f0f0;height:200px">
|
||
<model-viewer src="@m.PublicUrl"
|
||
camera-controls
|
||
auto-rotate
|
||
environment-image="neutral"
|
||
style="width:100%;height:200px;background:#f5f5f5">
|
||
</model-viewer>
|
||
</div>
|
||
}
|
||
else
|
||
{
|
||
<div class="d-flex justify-content-center align-items-center"
|
||
style="height:80px;background:#f8f9fa">
|
||
<button class="btn btn-sm btn-outline-info"
|
||
@onclick:stopPropagation="true"
|
||
@onclick="() => _pickerPreview = m.Key">
|
||
<i class="bi bi-box me-1"></i>Preview 3D
|
||
</button>
|
||
</div>
|
||
}
|
||
<div class="card-body py-2 px-3">
|
||
<div class="font-monospace small text-break">@m.ShortName</div>
|
||
<div class="text-muted" style="font-size:0.7rem">@m.Key</div>
|
||
</div>
|
||
@if (isSelected)
|
||
{
|
||
<div class="card-footer py-1 bg-primary text-white text-center small">
|
||
✓ Selected
|
||
</div>
|
||
}
|
||
</div>
|
||
</div>
|
||
}
|
||
</div>
|
||
}
|
||
</div>
|
||
<div class="modal-footer">
|
||
<button class="btn btn-secondary" @onclick="ClosePicker">Cancel</button>
|
||
<button class="btn btn-primary"
|
||
disabled="@(_pickerSelected is null || _saving)"
|
||
@onclick="SaveBindingAsync">
|
||
@if (_saving)
|
||
{
|
||
<span class="spinner-border spinner-border-sm me-1"></span>
|
||
}
|
||
Assign Selected
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
}
|
||
|
||
<!-- 3D Preview Modal (standalone) -->
|
||
@if (_previewUrl is not null)
|
||
{
|
||
<div class="modal d-block" tabindex="-1" style="background:rgba(0,0,0,0.55)">
|
||
<div class="modal-dialog modal-lg modal-dialog-centered">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h5 class="modal-title"><i class="bi bi-box me-2"></i>3D Preview</h5>
|
||
<button type="button" class="btn-close" @onclick="() => _previewUrl = null"></button>
|
||
</div>
|
||
<div class="modal-body p-0" style="background:#f0f0f0">
|
||
<model-viewer src="@_previewUrl"
|
||
camera-controls
|
||
auto-rotate
|
||
environment-image="neutral"
|
||
shadow-intensity="0.8"
|
||
style="width:100%;height:520px;background:#f5f5f5">
|
||
</model-viewer>
|
||
</div>
|
||
<div class="modal-footer py-2">
|
||
<button class="btn btn-sm btn-secondary" @onclick="() => _previewUrl = null">Close</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
}
|
||
|
||
<!-- Bottom Model Picker Modal -->
|
||
@if (_bottomPickerOpen)
|
||
{
|
||
<div class="modal d-block" tabindex="-1" style="background:rgba(0,0,0,0.55)">
|
||
<div class="modal-dialog modal-xl modal-dialog-scrollable">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h5 class="modal-title">Add Tile Bottom Model</h5>
|
||
<button type="button" class="btn-close" @onclick="() => _bottomPickerOpen = false"></button>
|
||
</div>
|
||
<div class="modal-body">
|
||
<input @bind="_bottomPickerSearch" @bind:event="oninput"
|
||
class="form-control form-control-sm font-monospace mb-3"
|
||
placeholder="Filter models…" />
|
||
@if (_pickerLoading)
|
||
{
|
||
<div class="text-muted text-center py-4">
|
||
<span class="spinner-border spinner-border-sm me-2"></span>Loading CDN models…
|
||
</div>
|
||
}
|
||
else
|
||
{
|
||
<div class="row g-3">
|
||
@foreach (var model in BottomPickerFiltered())
|
||
{
|
||
var m = model;
|
||
var isSel = _bottomPickerSelected == m.Key;
|
||
<div class="col-md-4">
|
||
<div class="card h-100 @(isSel ? "border-primary" : "")"
|
||
style="cursor:pointer" @onclick="() => _bottomPickerSelected = m.Key">
|
||
<div class="card-body py-2 px-3">
|
||
<div class="font-monospace small text-break">@m.ShortName</div>
|
||
</div>
|
||
@if (isSel)
|
||
{
|
||
<div class="card-footer py-1 bg-primary text-white text-center small">✓ Selected</div>
|
||
}
|
||
</div>
|
||
</div>
|
||
}
|
||
</div>
|
||
}
|
||
</div>
|
||
<div class="modal-footer">
|
||
<button class="btn btn-secondary" @onclick="() => _bottomPickerOpen = false">Cancel</button>
|
||
<button class="btn btn-primary"
|
||
disabled="@(_bottomPickerSelected is null || _saving)"
|
||
@onclick="SaveBottomAsync">
|
||
@if (_saving) { <span class="spinner-border spinner-border-sm me-1"></span> }
|
||
Add
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
}
|
||
|
||
<!-- Texture Picker Modal -->
|
||
@if (_texPickerOpen)
|
||
{
|
||
<div class="modal d-block" tabindex="-1" style="background:rgba(0,0,0,0.55)">
|
||
<div class="modal-dialog modal-xl modal-dialog-scrollable">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h5 class="modal-title">
|
||
Assign Texture — <span class="text-primary">@_texPickerAffinity</span>
|
||
<span class="badge bg-secondary ms-2">@_texPickerShape</span>
|
||
</h5>
|
||
<button type="button" class="btn-close" @onclick="CloseTexPicker"></button>
|
||
</div>
|
||
<div class="modal-body">
|
||
<input @bind="_texPickerSearch" @bind:event="oninput"
|
||
class="form-control form-control-sm font-monospace mb-3"
|
||
placeholder="Filter textures…" />
|
||
@if (_texPickerLoading)
|
||
{
|
||
<div class="text-muted text-center py-4">
|
||
<span class="spinner-border spinner-border-sm me-2"></span>Loading CDN textures…
|
||
</div>
|
||
}
|
||
else if (!_texPickerItems.Any())
|
||
{
|
||
<div class="text-muted text-center py-4">
|
||
No image files found in the <code>textures/</code> prefix.
|
||
Upload textures via the CDN Browser first.
|
||
</div>
|
||
}
|
||
else
|
||
{
|
||
<div class="row g-3">
|
||
@foreach (var tex in TexPickerFiltered())
|
||
{
|
||
var t = tex;
|
||
var isSel = _texPickerSelected == t.PublicUrl;
|
||
<div class="col-md-3">
|
||
<div class="card h-100 @(isSel ? "border-primary" : "")"
|
||
style="cursor:pointer" @onclick="() => _texPickerSelected = t.PublicUrl">
|
||
<img src="@t.PublicUrl" alt="@t.ShortName"
|
||
style="height:120px;object-fit:cover;border-radius:4px 4px 0 0" />
|
||
<div class="card-body py-1 px-2">
|
||
<div class="font-monospace small text-break">@t.ShortName</div>
|
||
</div>
|
||
@if (isSel)
|
||
{
|
||
<div class="card-footer py-1 bg-primary text-white text-center small">✓ Selected</div>
|
||
}
|
||
</div>
|
||
</div>
|
||
}
|
||
</div>
|
||
}
|
||
</div>
|
||
<div class="modal-footer">
|
||
<button class="btn btn-secondary" @onclick="CloseTexPicker">Cancel</button>
|
||
<button class="btn btn-primary"
|
||
disabled="@(_texPickerSelected is null || _saving)"
|
||
@onclick="SaveTextureAsync">
|
||
@if (_saving) { <span class="spinner-border spinner-border-sm me-1"></span> }
|
||
Assign Selected
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
}
|
||
|
||
<script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.5.0/model-viewer.min.js"></script>
|
||
|
||
@code {
|
||
// ── State ────────────────────────────────────────────────────────────────
|
||
|
||
private bool _loading = true;
|
||
private string? _message;
|
||
private bool _isError;
|
||
|
||
// Loaded bindings: (Affinity, Shape) → entity
|
||
private Dictionary<(Affinity, TileShape), TileModelBinding> _bindings = new();
|
||
|
||
// Picker state
|
||
private bool _pickerOpen;
|
||
private bool _pickerLoading;
|
||
private bool _saving;
|
||
private Affinity _pickerAffinity;
|
||
private TileShape _pickerShape;
|
||
private string _pickerSearch = "";
|
||
private string? _pickerSelected;
|
||
private string? _pickerPreview;
|
||
private List<ModelItem> _pickerModels = new();
|
||
|
||
// Standalone 3D preview
|
||
private string? _previewUrl;
|
||
|
||
// ── Affinity groupings ────────────────────────────────────────────────────
|
||
|
||
private static readonly (string Label, Affinity[] Affinities)[] _affinityGroups =
|
||
{
|
||
("Basic", new[] { Affinity.Void, Affinity.Aether, Affinity.Lake, Affinity.Hill, Affinity.Forest, Affinity.Field }),
|
||
("Non-Basic", new[] { Affinity.Ocean, Affinity.Island, Affinity.Marsh, Affinity.Swamp,
|
||
Affinity.Mountain, Affinity.Steppe, Affinity.Fort, Affinity.Plains,
|
||
Affinity.Grove, Affinity.Jungle }),
|
||
("Aether / Void", new[] { Affinity.UnstableAether, Affinity.PerfectAether, Affinity.PureVoid }),
|
||
("Gateways", new[] { Affinity.GatewayLake, Affinity.GatewayHill, Affinity.GatewayForest, Affinity.GatewayField }),
|
||
};
|
||
|
||
// Texture picker state
|
||
private bool _texPickerOpen;
|
||
private bool _texPickerLoading;
|
||
private Affinity _texPickerAffinity;
|
||
private TileShape _texPickerShape;
|
||
private string _texPickerSearch = "";
|
||
private string? _texPickerSelected;
|
||
private List<TexItem> _texPickerItems = new();
|
||
|
||
// Tile bottom models
|
||
private List<TileBottomModel> _bottomModels = new();
|
||
private bool _bottomPickerOpen;
|
||
private string _bottomPickerSearch = "";
|
||
private string? _bottomPickerSelected;
|
||
|
||
// ── Lifecycle ─────────────────────────────────────────────────────────────
|
||
|
||
protected override async Task OnInitializedAsync() => await LoadAsync();
|
||
|
||
private async Task LoadAsync()
|
||
{
|
||
_loading = true;
|
||
var all = await Db.TileModelBindings.ToListAsync();
|
||
_bindings = all.ToDictionary(b => (b.Affinity, b.Shape));
|
||
_bottomModels = await Db.TileBottomModels.OrderBy(m => m.CdnKey).ToListAsync();
|
||
_loading = false;
|
||
}
|
||
|
||
// ── Picker ────────────────────────────────────────────────────────────────
|
||
|
||
private async Task OpenPickerAsync(Affinity affinity, TileShape shape)
|
||
{
|
||
_pickerAffinity = affinity;
|
||
_pickerShape = shape;
|
||
_pickerSelected = null;
|
||
_pickerPreview = null;
|
||
_pickerSearch = "";
|
||
_pickerOpen = true;
|
||
_pickerLoading = true;
|
||
|
||
// Pre-select existing binding if any
|
||
if (_bindings.TryGetValue((affinity, shape), out var existing))
|
||
_pickerSelected = existing.ModelCdnKey;
|
||
|
||
await LoadPickerModelsAsync();
|
||
}
|
||
|
||
private async Task LoadPickerModelsAsync()
|
||
{
|
||
try
|
||
{
|
||
var options = CdnOptionsAccessor.Value;
|
||
var request = new ListObjectsV2Request
|
||
{
|
||
BucketName = options.BucketName,
|
||
Prefix = "models/",
|
||
MaxKeys = 1000,
|
||
};
|
||
|
||
var response = await S3.ListObjectsV2Async(request);
|
||
|
||
_pickerModels = response.S3Objects
|
||
.Where(o => !o.Key.EndsWith('/') &&
|
||
(o.Key.EndsWith(".glb", StringComparison.OrdinalIgnoreCase) ||
|
||
o.Key.EndsWith(".gltf", StringComparison.OrdinalIgnoreCase)))
|
||
.Select(o => new ModelItem
|
||
{
|
||
Key = o.Key,
|
||
ShortName = o.Key.Contains('/') ? o.Key[(o.Key.LastIndexOf('/') + 1)..] : o.Key,
|
||
PublicUrl = BuildPublicUrl(options, o.Key),
|
||
})
|
||
.OrderBy(m => m.Key)
|
||
.ToList();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_message = $"Failed to load CDN models: {ex.Message}";
|
||
_isError = true;
|
||
}
|
||
finally
|
||
{
|
||
_pickerLoading = false;
|
||
}
|
||
}
|
||
|
||
private IEnumerable<ModelItem> PickerFiltered()
|
||
{
|
||
if (string.IsNullOrWhiteSpace(_pickerSearch))
|
||
return _pickerModels;
|
||
return _pickerModels.Where(m =>
|
||
m.Key.Contains(_pickerSearch, StringComparison.OrdinalIgnoreCase));
|
||
}
|
||
|
||
private void ClosePicker()
|
||
{
|
||
_pickerOpen = false;
|
||
_pickerModels = new();
|
||
_pickerSelected = null;
|
||
_pickerPreview = null;
|
||
}
|
||
|
||
private async Task SaveBindingAsync()
|
||
{
|
||
if (_pickerSelected is null) return;
|
||
|
||
_saving = true;
|
||
try
|
||
{
|
||
var options = CdnOptionsAccessor.Value;
|
||
var publicUrl = BuildPublicUrl(options, _pickerSelected);
|
||
|
||
var binding = await Db.TileModelBindings
|
||
.FirstOrDefaultAsync(b => b.Affinity == _pickerAffinity && b.Shape == _pickerShape);
|
||
|
||
if (binding is null)
|
||
{
|
||
binding = new TileModelBinding { Affinity = _pickerAffinity, Shape = _pickerShape };
|
||
Db.TileModelBindings.Add(binding);
|
||
}
|
||
|
||
binding.ModelCdnKey = _pickerSelected;
|
||
binding.ModelPublicUrl = publicUrl;
|
||
binding.UpdatedAt = DateTimeOffset.UtcNow;
|
||
|
||
await Db.SaveChangesAsync();
|
||
|
||
_bindings[(_pickerAffinity, _pickerShape)] = binding;
|
||
_message = $"Assigned {_pickerSelected} → {_pickerAffinity} / {_pickerShape}.";
|
||
_isError = false;
|
||
ClosePicker();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_message = $"Save failed: {ex.Message}";
|
||
_isError = true;
|
||
}
|
||
finally
|
||
{
|
||
_saving = false;
|
||
}
|
||
}
|
||
|
||
private async Task ClearBindingAsync(Affinity affinity, TileShape shape)
|
||
{
|
||
var binding = await Db.TileModelBindings
|
||
.FirstOrDefaultAsync(b => b.Affinity == affinity && b.Shape == shape);
|
||
|
||
if (binding is not null)
|
||
{
|
||
Db.TileModelBindings.Remove(binding);
|
||
await Db.SaveChangesAsync();
|
||
}
|
||
|
||
_bindings.Remove((affinity, shape));
|
||
_message = $"Cleared {affinity} / {shape}.";
|
||
_isError = false;
|
||
}
|
||
|
||
// ── Texture Picker ────────────────────────────────────────────────────────
|
||
|
||
private async Task OpenTexPickerAsync(Affinity affinity, TileShape shape)
|
||
{
|
||
_texPickerAffinity = affinity;
|
||
_texPickerShape = shape;
|
||
_texPickerSelected = _bindings.TryGetValue((affinity, shape), out var b) ? b.TexturePublicUrl : null;
|
||
_texPickerSearch = "";
|
||
_texPickerOpen = true;
|
||
_texPickerLoading = true;
|
||
await LoadTexPickerItemsAsync();
|
||
}
|
||
|
||
private async Task LoadTexPickerItemsAsync()
|
||
{
|
||
try
|
||
{
|
||
var options = CdnOptionsAccessor.Value;
|
||
var request = new ListObjectsV2Request
|
||
{
|
||
BucketName = options.BucketName,
|
||
Prefix = "textures/",
|
||
MaxKeys = 1000,
|
||
};
|
||
var response = await S3.ListObjectsV2Async(request);
|
||
_texPickerItems = response.S3Objects
|
||
.Where(o => !o.Key.EndsWith('/') && IsImageKey(o.Key))
|
||
.Select(o => new TexItem
|
||
{
|
||
Key = o.Key,
|
||
ShortName = o.Key.Contains('/') ? o.Key[(o.Key.LastIndexOf('/') + 1)..] : o.Key,
|
||
PublicUrl = BuildPublicUrl(options, o.Key),
|
||
})
|
||
.OrderBy(t => t.Key)
|
||
.ToList();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_message = $"Failed to load CDN textures: {ex.Message}";
|
||
_isError = true;
|
||
}
|
||
finally { _texPickerLoading = false; }
|
||
}
|
||
|
||
private IEnumerable<TexItem> TexPickerFiltered() =>
|
||
string.IsNullOrWhiteSpace(_texPickerSearch)
|
||
? _texPickerItems
|
||
: _texPickerItems.Where(t => t.Key.Contains(_texPickerSearch, StringComparison.OrdinalIgnoreCase));
|
||
|
||
private void CloseTexPicker()
|
||
{
|
||
_texPickerOpen = false;
|
||
_texPickerItems = new();
|
||
_texPickerSelected = null;
|
||
}
|
||
|
||
private async Task SaveTextureAsync()
|
||
{
|
||
if (_texPickerSelected is null) return;
|
||
_saving = true;
|
||
try
|
||
{
|
||
var binding = await Db.TileModelBindings
|
||
.FirstOrDefaultAsync(b => b.Affinity == _texPickerAffinity && b.Shape == _texPickerShape);
|
||
|
||
if (binding is null)
|
||
{
|
||
// Create a texture-only binding (no 3D model yet).
|
||
binding = new TileModelBinding
|
||
{
|
||
Affinity = _texPickerAffinity,
|
||
Shape = _texPickerShape,
|
||
ModelCdnKey = "",
|
||
ModelPublicUrl = "",
|
||
};
|
||
Db.TileModelBindings.Add(binding);
|
||
}
|
||
|
||
binding.TexturePublicUrl = _texPickerSelected;
|
||
binding.UpdatedAt = DateTimeOffset.UtcNow;
|
||
await Db.SaveChangesAsync();
|
||
|
||
_bindings[(_texPickerAffinity, _texPickerShape)] = binding;
|
||
_message = $"Texture assigned to {_texPickerAffinity} / {_texPickerShape}.";
|
||
_isError = false;
|
||
CloseTexPicker();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_message = $"Save failed: {ex.Message}";
|
||
_isError = true;
|
||
}
|
||
finally { _saving = false; }
|
||
}
|
||
|
||
private async Task ClearTextureAsync(Affinity affinity, TileShape shape)
|
||
{
|
||
var binding = await Db.TileModelBindings
|
||
.FirstOrDefaultAsync(b => b.Affinity == affinity && b.Shape == shape);
|
||
if (binding is null) return;
|
||
|
||
binding.TexturePublicUrl = null;
|
||
binding.UpdatedAt = DateTimeOffset.UtcNow;
|
||
await Db.SaveChangesAsync();
|
||
_bindings[(affinity, shape)] = binding;
|
||
_message = $"Texture cleared for {affinity} / {shape}.";
|
||
_isError = false;
|
||
}
|
||
|
||
private RenderFragment TextureCell(TileModelBinding? binding, Affinity affinity, TileShape shape) => __builder =>
|
||
{
|
||
var url = binding?.TexturePublicUrl;
|
||
if (url is not null)
|
||
{
|
||
<div class="d-flex align-items-center gap-2">
|
||
<img src="@url" alt="texture" style="height:40px;width:40px;object-fit:cover;border-radius:4px" />
|
||
<button class="btn btn-sm btn-outline-warning py-0"
|
||
@onclick="() => OpenTexPickerAsync(affinity, shape)"
|
||
title="Change texture"><i class="bi bi-arrow-repeat"></i></button>
|
||
<button class="btn btn-sm btn-outline-danger py-0"
|
||
@onclick="() => ClearTextureAsync(affinity, shape)"
|
||
title="Remove texture"><i class="bi bi-x"></i></button>
|
||
</div>
|
||
}
|
||
else
|
||
{
|
||
<button class="btn btn-sm btn-outline-secondary"
|
||
@onclick="() => OpenTexPickerAsync(affinity, shape)">
|
||
<i class="bi bi-image me-1"></i>Assign
|
||
</button>
|
||
}
|
||
};
|
||
|
||
private static bool IsImageKey(string key) =>
|
||
key.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
|
||
key.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) ||
|
||
key.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
|
||
key.EndsWith(".webp", StringComparison.OrdinalIgnoreCase);
|
||
|
||
// ── Tile Bottom Models ────────────────────────────────────────────────────
|
||
|
||
private async Task OpenBottomPickerAsync()
|
||
{
|
||
_bottomPickerSelected = null;
|
||
_bottomPickerSearch = "";
|
||
_bottomPickerOpen = true;
|
||
_pickerLoading = true;
|
||
await LoadPickerModelsAsync();
|
||
}
|
||
|
||
private IEnumerable<ModelItem> BottomPickerFiltered()
|
||
{
|
||
if (string.IsNullOrWhiteSpace(_bottomPickerSearch))
|
||
return _pickerModels;
|
||
return _pickerModels.Where(m =>
|
||
m.Key.Contains(_bottomPickerSearch, StringComparison.OrdinalIgnoreCase));
|
||
}
|
||
|
||
private async Task SaveBottomAsync()
|
||
{
|
||
if (_bottomPickerSelected is null) return;
|
||
_saving = true;
|
||
try
|
||
{
|
||
var options = CdnOptionsAccessor.Value;
|
||
var publicUrl = BuildPublicUrl(options, _bottomPickerSelected);
|
||
var model = new TileBottomModel
|
||
{
|
||
CdnKey = _bottomPickerSelected,
|
||
PublicUrl = publicUrl,
|
||
IsActive = true,
|
||
UpdatedAt = DateTimeOffset.UtcNow,
|
||
};
|
||
Db.TileBottomModels.Add(model);
|
||
await Db.SaveChangesAsync();
|
||
_bottomModels.Add(model);
|
||
_bottomModels = _bottomModels.OrderBy(m => m.CdnKey).ToList();
|
||
_bottomPickerOpen = false;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_message = $"Save failed: {ex.Message}";
|
||
_isError = true;
|
||
}
|
||
finally { _saving = false; }
|
||
}
|
||
|
||
private async Task ToggleBottomActiveAsync(TileBottomModel model, bool active)
|
||
{
|
||
model.IsActive = active;
|
||
model.UpdatedAt = DateTimeOffset.UtcNow;
|
||
await Db.SaveChangesAsync();
|
||
}
|
||
|
||
private async Task DeleteBottomAsync(TileBottomModel model)
|
||
{
|
||
Db.TileBottomModels.Remove(model);
|
||
await Db.SaveChangesAsync();
|
||
_bottomModels.Remove(model);
|
||
}
|
||
|
||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||
|
||
private RenderFragment ModelCell(TileModelBinding? binding, Affinity affinity, TileShape shape) => __builder =>
|
||
{
|
||
if (binding is not null)
|
||
{
|
||
<div class="d-flex align-items-center gap-2">
|
||
<span class="font-monospace small text-break flex-grow-1">
|
||
@(binding.ModelCdnKey.Contains('/') ? binding.ModelCdnKey[(binding.ModelCdnKey.LastIndexOf('/') + 1)..] : binding.ModelCdnKey)
|
||
</span>
|
||
<button class="btn btn-sm btn-outline-info py-0"
|
||
@onclick="() => _previewUrl = binding.ModelPublicUrl"
|
||
title="Preview 3D model">
|
||
<i class="bi bi-box"></i>
|
||
</button>
|
||
<button class="btn btn-sm btn-outline-warning py-0"
|
||
@onclick="() => OpenPickerAsync(affinity, shape)"
|
||
title="Change model">
|
||
<i class="bi bi-arrow-repeat"></i>
|
||
</button>
|
||
<button class="btn btn-sm btn-outline-danger py-0"
|
||
@onclick="() => ClearBindingAsync(affinity, shape)"
|
||
title="Remove binding">
|
||
<i class="bi bi-x"></i>
|
||
</button>
|
||
</div>
|
||
}
|
||
else
|
||
{
|
||
<button class="btn btn-sm btn-outline-secondary"
|
||
@onclick="() => OpenPickerAsync(affinity, shape)">
|
||
<i class="bi bi-plus me-1"></i>Assign
|
||
</button>
|
||
}
|
||
};
|
||
|
||
private static string BuildPublicUrl(CdnOptions options, string key)
|
||
{
|
||
var baseUrl = options.PublicBaseUrl.TrimEnd('/');
|
||
var bucketSegment = options.BucketName.Trim('/');
|
||
return baseUrl.EndsWith("/" + bucketSegment, StringComparison.OrdinalIgnoreCase)
|
||
? $"{baseUrl}/{key}"
|
||
: $"{baseUrl}/{bucketSegment}/{key}";
|
||
}
|
||
|
||
private sealed class ModelItem
|
||
{
|
||
public string Key { get; init; } = "";
|
||
public string ShortName { get; init; } = "";
|
||
public string PublicUrl { get; init; } = "";
|
||
}
|
||
|
||
private sealed class TexItem
|
||
{
|
||
public string Key { get; init; } = "";
|
||
public string ShortName { get; init; } = "";
|
||
public string PublicUrl { get; init; } = "";
|
||
}
|
||
}
|