369 lines
16 KiB
Plaintext
369 lines
16 KiB
Plaintext
@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">← 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 Definitions -->
|
||
@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">Define Tile Properties</h3>
|
||
<p class="text-gray-400 text-sm mt-1">
|
||
Use Tile Definitions below to set walkability, buckets, and other gameplay properties per frame.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
}
|
||
|
||
@if (Model.Sheet.Id > 0)
|
||
{
|
||
<div class="card p-4 md:p-6 mt-4 md:mt-6">
|
||
<div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
|
||
<div>
|
||
<h3 class="text-lg font-semibold text-white">Define Tiles (Full Sheet)</h3>
|
||
<p class="text-gray-400 text-sm mt-1">Click any frame to create or edit its Tile Definition.</p>
|
||
</div>
|
||
<div class="flex gap-2">
|
||
<input type="number" id="frameJumpGrid" class="input text-sm w-28" min="0" placeholder="Frame #" />
|
||
<button type="button" class="btn btn-secondary text-sm" onclick="openTileEditorFromGrid()">Edit Frame</button>
|
||
</div>
|
||
</div>
|
||
<div class="mt-4 overflow-auto bg-gray-900 rounded p-2" style="max-height: 50vh;">
|
||
<div id="tileDefGrid" class="grid gap-1"></div>
|
||
</div>
|
||
<div class="text-xs text-gray-500 mt-2">Green border = defined. Red border = not defined.</div>
|
||
</div>
|
||
|
||
<div class="card p-4 md:p-6 mt-4 md:mt-6">
|
||
<div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
|
||
<div>
|
||
<h3 class="text-lg font-semibold text-white">Tile Definitions</h3>
|
||
<p class="text-gray-400 text-sm mt-1">Define gameplay properties and buckets per frame.</p>
|
||
</div>
|
||
<div class="flex gap-2">
|
||
<input type="number" id="frameJump" class="input text-sm w-28" min="0" placeholder="Frame #" />
|
||
<button type="button" class="btn btn-secondary text-sm" onclick="openTileEditor()">Edit Frame</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="overflow-x-auto mt-4">
|
||
<table class="w-full text-sm">
|
||
<thead>
|
||
<tr class="text-left text-gray-400 border-b border-gray-700">
|
||
<th class="pb-2 pr-4">Frame</th>
|
||
<th class="pb-2 pr-4">Name</th>
|
||
<th class="pb-2 pr-4">Type</th>
|
||
<th class="pb-2 pr-4">Buckets</th>
|
||
<th class="pb-2">Actions</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody class="divide-y divide-gray-700/50">
|
||
@foreach (var row in Model.TileDefinitionRows)
|
||
{
|
||
<tr class="hover:bg-gray-800/50">
|
||
<td class="py-2 pr-4 text-gray-300">@row.FrameIndex</td>
|
||
<td class="py-2 pr-4 text-white">@(!string.IsNullOrWhiteSpace(row.Name) ? row.Name : "-")</td>
|
||
<td class="py-2 pr-4 text-gray-400">@row.Type</td>
|
||
<td class="py-2 pr-4 text-gray-400">@row.BucketCount</td>
|
||
<td class="py-2">
|
||
<a href="/admin/tilemaps/tiles/edit?id=@row.Id" class="text-blue-400 hover:text-blue-300">Edit</a>
|
||
</td>
|
||
</tr>
|
||
}
|
||
@if (!Model.TileDefinitionRows.Any())
|
||
{
|
||
<tr>
|
||
<td colspan="5" class="py-6 text-center text-gray-500">No tile definitions yet. Use "Edit Frame" to create one.</td>
|
||
</tr>
|
||
}
|
||
</tbody>
|
||
</table>
|
||
</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();
|
||
}
|
||
});
|
||
|
||
function openTileEditor() {
|
||
const frame = document.getElementById('frameJump').value;
|
||
if (frame === '') {
|
||
alert('Enter a frame index.');
|
||
return;
|
||
}
|
||
window.location.href = `/admin/tilemaps/tiles/edit?sheetId=@Model.Sheet.Id&frameIndex=${frame}`;
|
||
}
|
||
|
||
function openTileEditorFromGrid() {
|
||
const frame = document.getElementById('frameJumpGrid').value;
|
||
if (frame === '') {
|
||
alert('Enter a frame index.');
|
||
return;
|
||
}
|
||
window.location.href = `/admin/tilemaps/tiles/edit?sheetId=@Model.Sheet.Id&frameIndex=${frame}`;
|
||
}
|
||
|
||
function buildTileDefGrid() {
|
||
const grid = document.getElementById('tileDefGrid');
|
||
if (!grid) return;
|
||
|
||
const cols = Math.floor(sheetWidth / tileWidth);
|
||
const rows = Math.floor(sheetHeight / tileHeight);
|
||
const total = cols * rows;
|
||
const cellSize = Math.min(48, Math.floor(600 / cols));
|
||
const defs = @Html.Raw(Model.TileDefinitionsJson);
|
||
const defMap = {};
|
||
defs.forEach(d => defMap[d.frameIndex] = d.id);
|
||
|
||
grid.style.gridTemplateColumns = `repeat(${cols}, ${cellSize}px)`;
|
||
grid.innerHTML = '';
|
||
|
||
for (let i = 0; i < total; i++) {
|
||
const canvas = document.createElement('canvas');
|
||
canvas.width = cellSize;
|
||
canvas.height = cellSize;
|
||
canvas.className = 'tile-cell';
|
||
canvas.style.borderColor = defMap[i] ? '#22c55e' : '#ef4444';
|
||
canvas.dataset.frame = i;
|
||
canvas.onclick = () => {
|
||
window.location.href = `/admin/tilemaps/tiles/edit?sheetId=@Model.Sheet.Id&frameIndex=${i}`;
|
||
};
|
||
grid.appendChild(canvas);
|
||
}
|
||
|
||
const img = document.getElementById('sheetPreview');
|
||
if (!img) return;
|
||
const imgEl = new Image();
|
||
imgEl.src = img.src;
|
||
imgEl.onload = () => {
|
||
const ctxs = grid.querySelectorAll('canvas');
|
||
ctxs.forEach(c => {
|
||
const frame = parseInt(c.dataset.frame);
|
||
const col = frame % cols;
|
||
const row = Math.floor(frame / cols);
|
||
const ctx = c.getContext('2d');
|
||
ctx.imageSmoothingEnabled = false;
|
||
ctx.clearRect(0, 0, cellSize, cellSize);
|
||
ctx.drawImage(imgEl, col * tileWidth, row * tileHeight, tileWidth, tileHeight, 0, 0, cellSize, cellSize);
|
||
});
|
||
};
|
||
}
|
||
|
||
if (@Model.Sheet.Id > 0) {
|
||
buildTileDefGrid();
|
||
}
|
||
</script>
|
||
}
|