Files
systemrevivalserver/Services/Admin/Pages/DungeonBuilder/Stamps/Edit.cshtml
T
2026-01-31 18:51:45 -05:00

310 lines
12 KiB
Plaintext

@page "/admin/dungeonbuilder/stamps/edit"
@model DungeonBuilder.Stamps.EditModel
@{
ViewData["Title"] = Model.Stamp.Id == 0 ? "New Tile Stamp" : $"Edit Stamp: {Model.Stamp.Name}";
}
<style>
.stamp-grid {
display: grid;
gap: 1px;
background: #1a1a2e;
border: 2px solid #374151;
border-radius: 4px;
}
.stamp-cell {
background: #0f0f1a;
cursor: crosshair;
position: relative;
image-rendering: pixelated;
}
.stamp-cell:hover {
outline: 2px solid #60a5fa;
outline-offset: -2px;
}
.palette-tile {
cursor: pointer;
border: 2px solid transparent;
border-radius: 2px;
image-rendering: pixelated;
box-sizing: content-box;
}
.palette-tile:hover { border-color: #60a5fa; }
.palette-tile.active { border-color: #fbbf24; }
</style>
<div class="mb-4">
<a href="/admin/dungeonbuilder/stamps" class="text-blue-400 hover:text-blue-300 text-sm">&larr; Back to Stamps</a>
</div>
<h1 class="text-2xl font-bold text-white mb-6">@ViewData["Title"]</h1>
@if (!string.IsNullOrEmpty(Model.Message))
{
<div class="mb-4 p-3 rounded @(Model.IsError ? "bg-red-900/50 text-red-300" : "bg-green-900/50 text-green-300")">
@Model.Message
</div>
}
<form method="post" id="stampForm">
@Html.AntiForgeryToken()
<input type="hidden" asp-for="Stamp.Id" />
<input type="hidden" asp-for="Stamp.TilesJson" id="tilesJson" />
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
<!-- Left: Settings -->
<div class="space-y-4">
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Stamp Settings</h3>
<div class="space-y-3">
<div>
<label class="text-xs text-gray-400">Name *</label>
<input type="text" asp-for="Stamp.Name" class="input w-full text-sm" required placeholder="2x2 Pillar" />
</div>
<div>
<label class="text-xs text-gray-400">Description</label>
<textarea asp-for="Stamp.Description" rows="2" class="input w-full text-sm"></textarea>
</div>
<div class="grid grid-cols-2 gap-2">
<div>
<label class="text-xs text-gray-400">Width</label>
<input type="number" asp-for="Stamp.Width" class="input w-full text-sm" min="1" max="20" id="stampWidth" />
</div>
<div>
<label class="text-xs text-gray-400">Height</label>
<input type="number" asp-for="Stamp.Height" class="input w-full text-sm" min="1" max="20" id="stampHeight" />
</div>
</div>
<button type="button" onclick="resizeStamp()" class="btn btn-secondary w-full text-sm">Resize</button>
<div class="flex items-center gap-2">
<input type="checkbox" asp-for="Stamp.IsEnabled" class="rounded" />
<label class="text-sm text-gray-300">Enabled</label>
</div>
</div>
</div>
</div>
<!-- Center: Stamp Grid -->
<div class="space-y-4">
<div class="card p-4">
<div class="flex items-center justify-between mb-3">
<h3 class="font-semibold text-white">Stamp Grid</h3>
<button type="button" onclick="clearStamp()" class="text-xs text-gray-400 hover:text-white">Clear</button>
</div>
<div class="bg-gray-900 rounded p-4 flex justify-center">
<div id="stampGrid" class="stamp-grid inline-grid"></div>
</div>
<p class="text-xs text-gray-500 mt-2">Click cells to paint with selected tile definition.</p>
</div>
</div>
<!-- Right: Tile Palette -->
<div class="space-y-4">
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Tile Palette</h3>
<div class="mb-3">
<select id="sheetSelect" class="input w-full text-sm" onchange="loadSheet()">
<option value="">Select sprite sheet...</option>
@foreach (var sheet in Model.TileSheets)
{
<option value="@sheet.Id"
data-cdn="@sheet.CdnPath"
data-cols="@sheet.Columns"
data-tw="@sheet.TileWidth"
data-th="@sheet.TileHeight"
data-sw="@sheet.SheetWidth"
data-sh="@sheet.SheetHeight">@sheet.Name</option>
}
</select>
</div>
<div id="palette" class="flex flex-wrap gap-1 max-h-[400px] overflow-auto bg-gray-900 rounded p-2">
<div class="text-gray-500 text-xs p-2">Select a sprite sheet</div>
</div>
</div>
<div class="card p-4">
<h3 class="font-semibold text-white mb-2">Selected Tile</h3>
<div id="selectedInfo" class="text-sm text-gray-400">None</div>
<div id="selectedPreview" class="mt-2 bg-gray-900 rounded p-2 h-12 flex items-center justify-center">
<span class="text-gray-600">-</span>
</div>
</div>
</div>
</div>
<div class="flex justify-end gap-4 mt-6">
<a href="/admin/dungeonbuilder/stamps" class="btn btn-ghost">Cancel</a>
<button type="submit" class="btn btn-primary">
@(Model.Stamp.Id == 0 ? "Create Stamp" : "Save Changes")
</button>
</div>
</form>
<script>
const cdnBase = '@Model.CdnBaseUrl';
let sw = @Model.Stamp.Width, sh = @Model.Stamp.Height;
let currentSheetId = null;
let selected = null;
let stampData = {};
const loadedSheets = {};
const tileDefs = @Html.Raw(Model.TileDefinitionsJson);
function preloadSheets() {
document.querySelectorAll('#sheetSelect option').forEach(opt => {
if (opt.value) {
loadedSheets[+opt.value] = {
id: +opt.value,
cdn: opt.dataset.cdn,
cols: +opt.dataset.cols,
tw: +opt.dataset.tw,
th: +opt.dataset.th,
sw: +opt.dataset.sw,
sh: +opt.dataset.sh
};
}
});
}
document.addEventListener('DOMContentLoaded', () => {
preloadSheets();
loadExisting();
buildStampGrid();
});
function loadExisting() {
const json = document.getElementById('tilesJson').value;
if (json) {
try {
JSON.parse(json).forEach(t => {
stampData[`${t.x},${t.y}`] = { sheetId: t.sheetId, frameIndex: t.frameIndex, tileDefinitionId: t.tileDefinitionId || t.tileDefId || 0 };
});
} catch {}
}
}
function buildStampGrid() {
const grid = document.getElementById('stampGrid');
let cellW = 32, cellH = 32;
if (currentSheetId && loadedSheets[currentSheetId]) {
cellW = loadedSheets[currentSheetId].tw;
cellH = loadedSheets[currentSheetId].th;
} else {
const firstTile = Object.values(stampData)[0];
if (firstTile && loadedSheets[firstTile.sheetId]) {
cellW = loadedSheets[firstTile.sheetId].tw;
cellH = loadedSheets[firstTile.sheetId].th;
}
}
grid.style.gridTemplateColumns = `repeat(${sw}, ${cellW}px)`;
grid.innerHTML = '';
for (let y = 0; y < sh; y++) {
for (let x = 0; x < sw; x++) {
const cell = document.createElement('div');
cell.className = 'stamp-cell';
cell.style.width = cellW + 'px';
cell.style.height = cellH + 'px';
cell.dataset.x = x;
cell.dataset.y = y;
cell.onclick = () => paintCell(x, y);
grid.appendChild(cell);
renderCell(x, y);
}
}
}
function renderCell(x, y) {
const cell = document.querySelector(`.stamp-cell[data-x="${x}"][data-y="${y}"]`);
if (!cell) return;
const data = stampData[`${x},${y}`];
if (data && data.sheetId && loadedSheets[data.sheetId]) {
const sheet = loadedSheets[data.sheetId];
const col = data.frameIndex % sheet.cols;
const row = Math.floor(data.frameIndex / sheet.cols);
cell.style.background = `url('${cdnBase}/${sheet.cdn}') ${-col * sheet.tw}px ${-row * sheet.th}px no-repeat`;
cell.style.backgroundSize = `${sheet.sw}px ${sheet.sh}px`;
} else {
cell.style.background = '#0f0f1a';
}
}
function paintCell(x, y) {
if (!selected) return;
stampData[`${x},${y}`] = { sheetId: selected.sheetId, frameIndex: selected.frameIndex, tileDefinitionId: selected.tileDefinitionId };
renderCell(x, y);
saveData();
}
function loadSheet() {
const opt = document.getElementById('sheetSelect').selectedOptions[0];
if (!opt?.value) {
currentSheetId = null;
document.getElementById('palette').innerHTML = '<div class="text-gray-500 text-xs p-2">Select a sprite sheet</div>';
return;
}
currentSheetId = +opt.value;
const sheet = loadedSheets[currentSheetId];
const tiles = tileDefs.filter(t => t.sheetId === currentSheetId);
let html = '';
if (tiles.length === 0) {
html = '<div class="text-gray-500 text-xs p-2">No tile definitions for this sheet.</div>';
} else {
tiles.forEach(t => {
const col = t.frameIndex % sheet.cols;
const row = Math.floor(t.frameIndex / sheet.cols);
html += `<div class="palette-tile" data-frame="${t.frameIndex}" data-tile-id="${t.id}" onclick="selectTile(${t.frameIndex}, ${t.id})"
style="width: ${sheet.tw}px; height: ${sheet.th}px;
background: url('${cdnBase}/${sheet.cdn}') ${-col * sheet.tw}px ${-row * sheet.th}px no-repeat;
background-size: ${sheet.sw}px ${sheet.sh}px;"></div>`;
});
}
document.getElementById('palette').innerHTML = html;
}
function selectTile(frameIndex, tileId) {
document.querySelectorAll('.palette-tile').forEach(t => t.classList.remove('active'));
document.querySelector(`.palette-tile[data-frame="${frameIndex}"]`)?.classList.add('active');
const sheet = loadedSheets[currentSheetId];
selected = { sheetId: currentSheetId, frameIndex, tileDefinitionId: tileId };
document.getElementById('selectedInfo').textContent = `Frame ${frameIndex}`;
const col = frameIndex % sheet.cols;
const row = Math.floor(frameIndex / sheet.cols);
document.getElementById('selectedPreview').innerHTML = `
<div style="width: ${sheet.tw}px; height: ${sheet.th}px;
background: url('${cdnBase}/${sheet.cdn}') ${-col * sheet.tw}px ${-row * sheet.th}px no-repeat;
background-size: ${sheet.sw}px ${sheet.sh}px;
image-rendering: pixelated;"></div>
`;
}
function resizeStamp() {
sw = +document.getElementById('stampWidth').value || 1;
sh = +document.getElementById('stampHeight').value || 1;
sw = Math.max(1, Math.min(20, sw));
sh = Math.max(1, Math.min(20, sh));
buildStampGrid();
}
function clearStamp() {
if (!confirm('Clear all tiles?')) return;
stampData = {};
buildStampGrid();
saveData();
}
function saveData() {
const tiles = Object.entries(stampData).map(([k, v]) => {
const [x, y] = k.split(',').map(Number);
return { x, y, sheetId: v.sheetId, frameIndex: v.frameIndex, tileDefinitionId: v.tileDefinitionId, layer: 0 };
});
document.getElementById('tilesJson').value = JSON.stringify(tiles);
}
document.getElementById('stampForm').onsubmit = () => saveData();
</script>