Files
systemrevivalserver/Services/Admin/Pages/TileMaps/Edit.cshtml
T
2026-01-10 13:43:59 -05:00

235 lines
10 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
@page "/admin/tilemaps/edit"
@model TileMaps.EditModel
@using DataServer.Data.Entities
@{
ViewData["Title"] = Model.Sheet.Id == 0 ? "New TileMap Sheet" : $"Edit: {Model.Sheet.Name}";
// Build image URL - handle both with and without /downloads/ path
var imageUrl = Model.Sheet.CdnPath;
if (!string.IsNullOrEmpty(imageUrl))
{
if (!imageUrl.StartsWith("http"))
{
// Ensure proper CDN URL construction
var baseUrl = Model.CdnBaseUrl.TrimEnd('/');
if (!baseUrl.EndsWith("/downloads"))
{
baseUrl += "/downloads";
}
imageUrl = $"{baseUrl}/{imageUrl.TrimStart('/')}";
}
}
}
<div class="mb-4 md:mb-6">
<a href="/admin/tilemaps" class="text-blue-400 hover:text-blue-300 text-sm md:text-base">&larr; Back to TileMap Sheets</a>
</div>
@if (!string.IsNullOrEmpty(Model.Message))
{
<div class="mb-4 p-3 rounded text-sm @(Model.IsError ? "bg-red-900/50 text-red-300" : "bg-green-900/50 text-green-300")">
@Model.Message
</div>
}
<div class="grid grid-cols-1 lg:grid-cols-2 gap-4 md:gap-6">
<!-- Sheet Settings -->
<div class="card p-4 md:p-6">
<h2 class="text-lg md:text-xl font-semibold text-white mb-4">TileMap Sheet Settings</h2>
<form method="post">
@Html.AntiForgeryToken()
<input type="hidden" asp-for="Sheet.Id" />
<div class="space-y-3 md:space-y-4">
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">Name</label>
<input type="text" asp-for="Sheet.Name" class="input w-full text-sm md:text-base" placeholder="Dungeon Stone Tiles" />
</div>
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">CDN Path</label>
<div class="flex gap-2">
<input type="text" asp-for="Sheet.CdnPath" class="input flex-1 text-sm md:text-base" placeholder="textures/tilemaps/dungeon.png" />
<button type="button" onclick="showCdnBrowser()" class="btn btn-secondary text-sm">Browse</button>
</div>
</div>
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">Tags (comma-separated)</label>
<input type="text" asp-for="Sheet.Tags" class="input w-full text-sm md:text-base" placeholder="dungeon, stone, dark" />
</div>
<div class="grid grid-cols-2 gap-2 md:gap-4">
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">Sheet Width (px)</label>
<input type="number" asp-for="Sheet.SheetWidth" class="input w-full text-sm" />
</div>
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">Sheet Height (px)</label>
<input type="number" asp-for="Sheet.SheetHeight" class="input w-full text-sm" />
</div>
</div>
<div class="grid grid-cols-2 gap-2 md:gap-4">
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">Tile Width (px)</label>
<input type="number" asp-for="Sheet.TileWidth" class="input w-full text-sm" />
</div>
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">Tile Height (px)</label>
<input type="number" asp-for="Sheet.TileHeight" class="input w-full text-sm" />
</div>
</div>
<div class="flex items-center gap-2">
<input type="checkbox" asp-for="Sheet.IsEnabled" class="rounded" />
<label asp-for="Sheet.IsEnabled" class="text-gray-300 text-sm">Enabled</label>
</div>
<button type="submit" class="btn btn-primary w-full">
@(Model.Sheet.Id == 0 ? "Create TileMap Sheet" : "Save Changes")
</button>
</div>
</form>
</div>
<!-- Preview -->
<div class="card p-4 md:p-6">
<h2 class="text-lg md:text-xl font-semibold text-white mb-4">Preview</h2>
@if (!string.IsNullOrEmpty(Model.Sheet.CdnPath))
{
<div class="bg-gray-900 rounded-lg p-2 md:p-4 overflow-auto" style="max-height: 50vh;">
<div class="relative inline-block" style="image-rendering: pixelated;">
<img src="@imageUrl"
alt="@Model.Sheet.Name"
id="sheetPreview"
class="max-w-none"
onerror="this.onerror=null; this.parentElement.innerHTML='<div class=\'p-4 text-red-400 text-sm\'>Failed to load image. Check CDN path.</div>';" />
<!-- Grid overlay will be drawn by JavaScript -->
<canvas id="gridOverlay" class="absolute top-0 left-0 pointer-events-none"></canvas>
</div>
</div>
<p class="text-xs md:text-sm text-gray-400 mt-2">
Grid: @Model.Sheet.Columns × @Model.Sheet.Rows = @Model.Sheet.TotalTiles tiles
</p>
<p class="text-xs text-gray-500 mt-1 break-all">URL: @imageUrl</p>
}
else
{
<div class="bg-gray-900 rounded-lg p-8 text-center text-gray-500 text-sm">
Select a CDN path to preview the tilemap
</div>
}
</div>
</div>
<!-- Note about Tile Templates -->
@if (Model.Sheet.Id > 0)
{
<div class="card p-4 md:p-6 mt-4 md:mt-6">
<div class="flex items-center gap-3">
<div class="text-2xl">💡</div>
<div>
<h3 class="font-semibold text-white">Create Tile Templates</h3>
<p class="text-gray-400 text-sm mt-1">
To use tiles from this sheet, create <a href="/admin/dungeonbuilder/tiles" class="text-blue-400 hover:underline">Tile Templates</a>
which define walkability, sockets, and other properties.
</p>
</div>
</div>
</div>
}
<!-- CDN Browser Modal -->
<div id="cdnBrowserModal" class="fixed inset-0 bg-black/50 z-50 hidden items-center justify-center p-4">
<div class="bg-gray-900 rounded-xl shadow-xl p-4 md:p-6 w-full max-w-2xl border border-gray-700 max-h-[90vh] overflow-hidden flex flex-col">
<h3 class="text-lg font-semibold text-white mb-4">Select TileMap from CDN</h3>
<div class="flex-1 overflow-y-auto grid grid-cols-2 md:grid-cols-3 gap-2 md:gap-3">
@foreach (var asset in Model.CdnImages)
{
var assetUrl = Model.CdnBaseUrl.TrimEnd('/');
if (!assetUrl.EndsWith("/downloads")) { assetUrl += "/downloads"; }
assetUrl += "/" + asset.DownloadPath.TrimStart('/');
<button type="button" onclick="selectCdnImage('@asset.DownloadPath')" class="p-2 bg-gray-800 rounded-lg hover:bg-gray-700 text-left">
<img src="@assetUrl" alt="@asset.FilePath" class="w-full h-16 md:h-20 object-contain mb-1" style="image-rendering: pixelated;" onerror="this.src='data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>❌</text></svg>';" />
<p class="text-xs text-gray-400 truncate">@asset.FilePath</p>
</button>
}
@if (Model.CdnImages.Count == 0)
{
<p class="col-span-full text-gray-500 text-center py-8">No tilemap images found. Upload images to textures/tilemaps/ in CDN Assets.</p>
}
</div>
<div class="flex justify-end pt-4">
<button type="button" onclick="closeCdnBrowser()" class="btn btn-secondary text-sm">Cancel</button>
</div>
</div>
</div>
@section Scripts {
<script>
const sheetWidth = @Model.Sheet.SheetWidth;
const sheetHeight = @Model.Sheet.SheetHeight;
const tileWidth = @Model.Sheet.TileWidth;
const tileHeight = @Model.Sheet.TileHeight;
function showCdnBrowser() {
document.getElementById('cdnBrowserModal').classList.remove('hidden');
document.getElementById('cdnBrowserModal').classList.add('flex');
}
function closeCdnBrowser() {
document.getElementById('cdnBrowserModal').classList.add('hidden');
document.getElementById('cdnBrowserModal').classList.remove('flex');
}
function selectCdnImage(path) {
document.getElementById('Sheet_CdnPath').value = path;
closeCdnBrowser();
// Submit form to refresh preview
document.querySelector('form').submit();
}
function drawGridOverlay() {
const img = document.getElementById('sheetPreview');
const canvas = document.getElementById('gridOverlay');
if (!img || !canvas || !img.naturalWidth) return;
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
const ctx = canvas.getContext('2d');
ctx.strokeStyle = 'rgba(255, 255, 255, 0.4)';
ctx.lineWidth = 1;
for (let x = tileWidth; x < canvas.width; x += tileWidth) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
ctx.stroke();
}
for (let y = tileHeight; y < canvas.height; y += tileHeight) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
}
}
const previewImg = document.getElementById('sheetPreview');
if (previewImg) {
previewImg.onload = drawGridOverlay;
if (previewImg.complete) drawGridOverlay();
}
document.addEventListener('keydown', e => {
if (e.key === 'Escape') {
closeCdnBrowser();
}
});
</script>
}