Files
systemrevivalserver/Services/Admin/Pages/TileMaps/Edit.cshtml
T
2025-12-28 00:08:33 -05:00

494 lines
25 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}";
var tileTypes = Enum.GetValues<TileType>();
// 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>
@if (Model.Sheet.Id > 0)
{
<!-- Tile Editor Section -->
<div class="card p-4 md:p-6 mt-4 md:mt-6">
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-4">
<h2 class="text-lg md:text-xl font-semibold text-white">Tile Definitions</h2>
<div class="flex gap-2">
<form method="post" asp-page-handler="AutoGenerate" class="flex gap-2">
<input type="hidden" name="sheetId" value="@Model.Sheet.Id" />
<select name="defaultType" class="input text-sm py-1">
<option value="0">Floor (walkable)</option>
<option value="1">Wall (blocked)</option>
</select>
<button type="submit" class="btn btn-secondary text-sm py-1">Auto-Generate</button>
</form>
</div>
</div>
<p class="text-gray-400 text-sm mb-4">Click on a tile to define its properties. Defined: @Model.TileDefinitions.Count / @Model.Sheet.TotalTiles</p>
<!-- Interactive Tile Grid -->
<div class="bg-gray-900 rounded-lg p-2 md:p-4 overflow-auto" style="max-height: 60vh;">
<div class="relative inline-block" id="tileGridContainer" style="image-rendering: pixelated;">
@if (!string.IsNullOrEmpty(Model.Sheet.CdnPath))
{
<img src="@imageUrl"
alt="@Model.Sheet.Name"
id="tileGridImage"
class="max-w-none"
onerror="this.style.display='none';" />
<!-- Clickable tile overlay -->
<div id="tileClickOverlay" class="absolute top-0 left-0"
style="width: @(Model.Sheet.SheetWidth)px; height: @(Model.Sheet.SheetHeight)px;">
@for (int row = 0; row < Model.Sheet.Rows; row++)
{
@for (int col = 0; col < Model.Sheet.Columns; col++)
{
var frameIndex = row * Model.Sheet.Columns + col;
var tileDef = Model.TileDefinitions.FirstOrDefault(t => t.FrameIndex == frameIndex);
var hasDefinition = tileDef != null;
var tileColor = hasDefinition ? GetTileTypeColor(tileDef!.Type) : "transparent";
<div class="absolute cursor-pointer border border-gray-600/30 hover:border-yellow-400 hover:bg-yellow-400/30 transition-all"
style="left: @(col * Model.Sheet.TileWidth)px; top: @(row * Model.Sheet.TileHeight)px; width: @(Model.Sheet.TileWidth)px; height: @(Model.Sheet.TileHeight)px; background-color: @tileColor;"
onclick="openTileEditor(@frameIndex, @(tileDef?.Id ?? 0))"
title="Frame @frameIndex @(hasDefinition ? $"({tileDef!.Type})" : "")">
</div>
}
}
</div>
}
</div>
</div>
<!-- Tile Type Legend -->
<div class="mt-3 flex flex-wrap gap-1 md:gap-2 text-xs">
<span class="text-gray-400">Legend:</span>
<span class="px-1.5 py-0.5 rounded" style="background-color: rgba(34, 197, 94, 0.3);">Floor</span>
<span class="px-1.5 py-0.5 rounded" style="background-color: rgba(239, 68, 68, 0.3);">Wall</span>
<span class="px-1.5 py-0.5 rounded" style="background-color: rgba(168, 85, 247, 0.3);">Door</span>
<span class="px-1.5 py-0.5 rounded" style="background-color: rgba(59, 130, 246, 0.3);">Water</span>
<span class="px-1.5 py-0.5 rounded" style="background-color: rgba(249, 115, 22, 0.3);">Lava</span>
<span class="px-1.5 py-0.5 rounded" style="background-color: rgba(234, 179, 8, 0.3);">Spawn</span>
</div>
<!-- Defined Tiles Table -->
@if (Model.TileDefinitions.Count > 0)
{
<div class="mt-6">
<h3 class="text-base md:text-lg font-semibold text-white mb-3">Defined Tiles (@Model.TileDefinitions.Count)</h3>
<div class="overflow-x-auto -mx-4 md:mx-0">
<table class="w-full text-xs md:text-sm min-w-[600px]">
<thead>
<tr class="text-left text-gray-400 border-b border-gray-700">
<th class="p-2">#</th>
<th class="p-2">Preview</th>
<th class="p-2">Name</th>
<th class="p-2">Type</th>
<th class="p-2">Tags</th>
<th class="p-2">Props</th>
<th class="p-2">Actions</th>
</tr>
</thead>
<tbody>
@foreach (var tile in Model.TileDefinitions.Take(50))
{
var col = tile.FrameIndex % Model.Sheet.Columns;
var row = tile.FrameIndex / Model.Sheet.Columns;
<tr class="border-b border-gray-800 hover:bg-gray-800/50">
<td class="p-2 text-gray-300">@tile.FrameIndex</td>
<td class="p-2">
<div class="w-6 h-6 md:w-8 md:h-8 bg-gray-900 overflow-hidden" style="image-rendering: pixelated;">
<div style="width: @(Model.Sheet.TileWidth)px; height: @(Model.Sheet.TileHeight)px; background-image: url('@imageUrl'); background-position: -@(col * Model.Sheet.TileWidth)px -@(row * Model.Sheet.TileHeight)px; transform: scale(@(24.0 / Model.Sheet.TileWidth)); transform-origin: top left;"></div>
</div>
</td>
<td class="p-2 text-gray-300 max-w-[100px] truncate">@(tile.Name ?? "-")</td>
<td class="p-2">
<span class="px-1.5 py-0.5 text-xs rounded" style="background-color: @GetTileTypeColor(tile.Type);">@tile.Type</span>
</td>
<td class="p-2 text-gray-400 text-xs max-w-[80px] truncate">@(tile.Tags ?? "-")</td>
<td class="p-2 text-xs">
@if (tile.IsWalkable) { <span class="text-green-400">W</span> }
@if (tile.BlocksProjectiles) { <span class="text-red-400">P</span> }
@if (tile.BlocksLineOfSight) { <span class="text-yellow-400">L</span> }
</td>
<td class="p-2">
<button onclick="openTileEditor(@tile.FrameIndex, @tile.Id)" class="text-blue-400 hover:text-blue-300 mr-1">Edit</button>
<form method="post" asp-page-handler="DeleteTile" class="inline">
<input type="hidden" name="tileId" value="@tile.Id" />
<input type="hidden" name="sheetId" value="@Model.Sheet.Id" />
<button type="submit" class="text-red-400 hover:text-red-300" onclick="return confirm('Delete?')">×</button>
</form>
</td>
</tr>
}
</tbody>
</table>
@if (Model.TileDefinitions.Count > 50)
{
<p class="text-gray-500 text-xs p-2">Showing first 50 of @Model.TileDefinitions.Count tiles</p>
}
</div>
</div>
}
</div>
}
<!-- Tile Editor Modal -->
<div id="tileEditorModal" 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-md border border-gray-700 max-h-[90vh] overflow-y-auto">
<h3 class="text-lg font-semibold text-white mb-4">Edit Tile <span id="modalFrameIndex"></span></h3>
<form method="post" asp-page-handler="SaveTile">
@Html.AntiForgeryToken()
<input type="hidden" name="sheetId" value="@Model.Sheet.Id" />
<input type="hidden" name="frameIndex" id="editFrameIndex" />
<div class="space-y-3 md:space-y-4">
<!-- Tile Preview -->
<div class="flex justify-center">
<div id="modalTilePreview" class="bg-gray-800 border border-gray-600" style="image-rendering: pixelated; width: 64px; height: 64px;"></div>
</div>
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">Name (optional)</label>
<input type="text" name="name" id="editName" class="input w-full text-sm" placeholder="Stone Floor A" />
</div>
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">Type</label>
<select name="type" id="editType" class="input w-full text-sm">
@foreach (var t in tileTypes)
{
<option value="@((int)t)">@t</option>
}
</select>
</div>
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">Tags</label>
<input type="text" name="tags" id="editTags" class="input w-full text-sm" placeholder="stone, cracked" />
</div>
<div>
<label class="block text-sm font-medium text-gray-300 mb-1">Render Layer</label>
<select name="renderLayer" id="editRenderLayer" class="input w-full text-sm">
<option value="0">0 - Ground</option>
<option value="1">1 - Floor Decor</option>
<option value="2">2 - Walls</option>
<option value="3">3 - Ceiling</option>
</select>
</div>
<div class="grid grid-cols-3 gap-2 text-sm">
<label class="flex items-center gap-1.5 text-gray-300">
<input type="checkbox" name="isWalkable" id="editWalkable" value="true" class="rounded" />
Walk
</label>
<label class="flex items-center gap-1.5 text-gray-300">
<input type="checkbox" name="blocksProjectiles" id="editBlocksProj" value="true" class="rounded" />
Block Proj
</label>
<label class="flex items-center gap-1.5 text-gray-300">
<input type="checkbox" name="blocksLineOfSight" id="editBlocksLOS" value="true" class="rounded" />
Block LOS
</label>
</div>
</div>
<div class="flex gap-2 mt-5">
<button type="submit" class="btn btn-primary flex-1 text-sm">Save</button>
<button type="button" onclick="closeTileEditor()" class="btn btn-secondary flex-1 text-sm">Cancel</button>
</div>
</form>
</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 tileDefinitions = @Html.Raw(System.Text.Json.JsonSerializer.Serialize(Model.TileDefinitions.ToDictionary(t => t.FrameIndex, t => new { t.Id, t.FrameIndex, t.Name, t.Type, t.Tags, t.IsWalkable, t.BlocksProjectiles, t.BlocksLineOfSight, t.RenderLayer })));
const sheetWidth = @Model.Sheet.SheetWidth;
const sheetHeight = @Model.Sheet.SheetHeight;
const tileWidth = @Model.Sheet.TileWidth;
const tileHeight = @Model.Sheet.TileHeight;
const columns = @Model.Sheet.Columns;
const imageUrl = '@imageUrl';
function openTileEditor(frameIndex, existingId) {
document.getElementById('editFrameIndex').value = frameIndex;
document.getElementById('modalFrameIndex').textContent = '#' + frameIndex;
const col = frameIndex % columns;
const row = Math.floor(frameIndex / columns);
// Set preview with proper scaling
const preview = document.getElementById('modalTilePreview');
const scale = 64 / Math.max(tileWidth, tileHeight);
preview.style.width = '64px';
preview.style.height = '64px';
preview.style.backgroundImage = `url('${imageUrl}')`;
preview.style.backgroundPosition = `-${col * tileWidth * scale}px -${row * tileHeight * scale}px`;
preview.style.backgroundSize = `${sheetWidth * scale}px ${sheetHeight * scale}px`;
// Load existing data
const existing = tileDefinitions[frameIndex];
if (existing) {
document.getElementById('editName').value = existing.Name || '';
document.getElementById('editType').value = existing.Type;
document.getElementById('editTags').value = existing.Tags || '';
document.getElementById('editRenderLayer').value = existing.RenderLayer;
document.getElementById('editWalkable').checked = existing.IsWalkable;
document.getElementById('editBlocksProj').checked = existing.BlocksProjectiles;
document.getElementById('editBlocksLOS').checked = existing.BlocksLineOfSight;
} else {
document.getElementById('editName').value = '';
document.getElementById('editType').value = '0';
document.getElementById('editTags').value = '';
document.getElementById('editRenderLayer').value = '0';
document.getElementById('editWalkable').checked = true;
document.getElementById('editBlocksProj').checked = false;
document.getElementById('editBlocksLOS').checked = false;
}
document.getElementById('tileEditorModal').classList.remove('hidden');
document.getElementById('tileEditorModal').classList.add('flex');
}
function closeTileEditor() {
document.getElementById('tileEditorModal').classList.add('hidden');
document.getElementById('tileEditorModal').classList.remove('flex');
}
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') {
closeTileEditor();
closeCdnBrowser();
}
});
</script>
}
@functions {
string GetTileTypeColor(TileType type)
{
return type switch
{
TileType.Floor => "rgba(34, 197, 94, 0.3)",
TileType.Wall => "rgba(239, 68, 68, 0.3)",
TileType.Door => "rgba(168, 85, 247, 0.3)",
TileType.Water => "rgba(59, 130, 246, 0.3)",
TileType.Lava => "rgba(249, 115, 22, 0.3)",
TileType.Pit => "rgba(31, 41, 55, 0.5)",
TileType.Stairs => "rgba(236, 72, 153, 0.3)",
TileType.PlayerSpawn => "rgba(234, 179, 8, 0.4)",
TileType.MobSpawn => "rgba(234, 179, 8, 0.3)",
TileType.BossSpawn => "rgba(220, 38, 38, 0.4)",
TileType.ChestSpawn => "rgba(251, 191, 36, 0.4)",
TileType.Trigger => "rgba(139, 92, 246, 0.3)",
TileType.Transition => "rgba(14, 165, 233, 0.3)",
TileType.Secret => "rgba(163, 163, 163, 0.3)",
TileType.Trap => "rgba(239, 68, 68, 0.4)",
TileType.FloorDecor => "rgba(34, 197, 94, 0.2)",
TileType.WallDecor => "rgba(239, 68, 68, 0.2)",
_ => "transparent"
};
}
}