Files
systemrevivalserver/Services/Admin/Pages/DungeonBuilder/TileEdit.cshtml
T
2026-01-03 22:30:32 -05:00

499 lines
20 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/dungeonbuilder/tiles/edit"
@model DungeonBuilder.TileEditModel
@using DataServer.Data.Entities
@{
ViewData["Title"] = Model.Tile.Id == 0 ? "New Tile Template" : $"Edit: {Model.Tile.Name}";
var categories = Enum.GetValues<TileTemplateCategory>();
var socketTypes = Enum.GetValues<SocketType>();
}
<style>
.tile-grid {
display: grid;
gap: 1px;
background: #1a1a2e;
border: 2px solid #374151;
border-radius: 4px;
}
.tile-cell {
width: 32px;
height: 32px;
background: #0f0f1a;
cursor: crosshair;
position: relative;
image-rendering: pixelated;
}
.tile-cell:hover {
outline: 2px solid #60a5fa;
outline-offset: -2px;
}
.palette-tile {
width: 34px;
height: 34px;
cursor: pointer;
border: 2px solid transparent;
border-radius: 2px;
image-rendering: pixelated;
}
.palette-tile:hover { border-color: #60a5fa; }
.palette-tile.active { border-color: #fbbf24; }
.socket-indicator {
position: absolute;
background: rgba(0,0,0,0.7);
font-size: 8px;
padding: 1px 3px;
border-radius: 2px;
}
.socket-n { top: 0; left: 50%; transform: translateX(-50%); }
.socket-e { right: 0; top: 50%; transform: translateY(-50%); }
.socket-s { bottom: 0; left: 50%; transform: translateX(-50%); }
.socket-w { left: 0; top: 50%; transform: translateY(-50%); }
.socket-open { color: #4ade80; }
.socket-closed { color: #f87171; }
.socket-custom { color: #facc15; }
</style>
<div class="mb-4">
<a href="/admin/dungeonbuilder/tiles" class="text-blue-400 hover:text-blue-300 text-sm">&larr; Back to Tiles</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="tileForm">
@Html.AntiForgeryToken()
<input type="hidden" asp-for="Tile.Id" />
<input type="hidden" asp-for="Tile.TilesJson" id="tilesJson" />
<input type="hidden" asp-for="Tile.PrimaryTileDefinitionId" id="primaryTileId" />
<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">Basic Settings</h3>
<div class="space-y-3">
<div>
<label class="text-xs text-gray-400">Name *</label>
<input type="text" asp-for="Tile.Name" class="input w-full text-sm" required placeholder="Stone Floor Clean" />
</div>
<div>
<label class="text-xs text-gray-400">Category</label>
<select asp-for="Tile.Category" class="input w-full text-sm" onchange="onCategoryChange()">
@foreach (var c in categories)
{
<option value="@c">@c</option>
}
</select>
</div>
<div class="grid grid-cols-2 gap-2">
<div>
<label class="text-xs text-gray-400">Biome</label>
<input type="text" asp-for="Tile.Biome" class="input w-full text-sm" placeholder="dungeon" />
</div>
<div>
<label class="text-xs text-gray-400">Tags</label>
<input type="text" asp-for="Tile.Tags" class="input w-full text-sm" placeholder="stone,clean" />
</div>
</div>
</div>
</div>
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Size</h3>
<div class="grid grid-cols-2 gap-2 mb-3">
<div>
<label class="text-xs text-gray-400">Width</label>
<input type="number" asp-for="Tile.Width" class="input w-full text-sm" min="1" max="10" id="tileWidth" />
</div>
<div>
<label class="text-xs text-gray-400">Height</label>
<input type="number" asp-for="Tile.Height" class="input w-full text-sm" min="1" max="10" id="tileHeight" />
</div>
</div>
<button type="button" onclick="resizeTemplate()" class="btn btn-secondary w-full text-sm">Resize</button>
<p class="text-xs text-gray-500 mt-2">Use 1×1 for simple tiles, larger for patterns like pillars or wall corners.</p>
</div>
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Properties</h3>
<div class="space-y-2">
<div class="flex items-center gap-2">
<input type="checkbox" asp-for="Tile.IsWalkable" class="rounded" id="isWalkable" />
<label class="text-sm text-gray-300">Walkable</label>
</div>
<div class="flex items-center gap-2">
<input type="checkbox" asp-for="Tile.BlocksMovement" class="rounded" />
<label class="text-sm text-gray-300">Blocks Movement</label>
</div>
<div class="flex items-center gap-2">
<input type="checkbox" asp-for="Tile.BlocksProjectiles" class="rounded" />
<label class="text-sm text-gray-300">Blocks Projectiles</label>
</div>
<div class="flex items-center gap-2">
<input type="checkbox" asp-for="Tile.BlocksLineOfSight" class="rounded" />
<label class="text-sm text-gray-300">Blocks Line of Sight</label>
</div>
</div>
</div>
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Generation</h3>
<div class="space-y-3">
<div>
<label class="text-xs text-gray-400">Weight</label>
<input type="number" asp-for="Tile.Weight" class="input w-full text-sm" min="1" />
<p class="text-xs text-gray-500 mt-1">Higher = more common</p>
</div>
<div class="grid grid-cols-2 gap-2">
<div>
<label class="text-xs text-gray-400">Min Tier</label>
<input type="number" asp-for="Tile.MinTier" class="input w-full text-sm" min="1" />
</div>
<div>
<label class="text-xs text-gray-400">Max Tier</label>
<input type="number" asp-for="Tile.MaxTier" class="input w-full text-sm" min="0" />
</div>
</div>
<div class="flex items-center gap-2">
<input type="checkbox" asp-for="Tile.IsEnabled" class="rounded" />
<label class="text-sm text-gray-300">Enabled</label>
</div>
</div>
</div>
</div>
<!-- Center: Template 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">Template Pattern</h3>
<button type="button" onclick="clearTemplate()" 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="templateGrid" class="tile-grid inline-grid"></div>
</div>
<p class="text-xs text-gray-500 mt-2">Click cells to paint with selected tile. First tile placed becomes the primary.</p>
</div>
<!-- Socket Configuration -->
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Edge Sockets</h3>
<p class="text-xs text-gray-500 mb-3">Sockets control how this template connects to neighbors.</p>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="text-xs text-gray-400">North</label>
<select asp-for="Tile.SocketN" class="input w-full text-sm">
@foreach (var s in socketTypes) { <option value="@s">@s</option> }
</select>
</div>
<div>
<label class="text-xs text-gray-400">East</label>
<select asp-for="Tile.SocketE" class="input w-full text-sm">
@foreach (var s in socketTypes) { <option value="@s">@s</option> }
</select>
</div>
<div>
<label class="text-xs text-gray-400">South</label>
<select asp-for="Tile.SocketS" class="input w-full text-sm">
@foreach (var s in socketTypes) { <option value="@s">@s</option> }
</select>
</div>
<div>
<label class="text-xs text-gray-400">West</label>
<select asp-for="Tile.SocketW" class="input w-full text-sm">
@foreach (var s in socketTypes) { <option value="@s">@s</option> }
</select>
</div>
</div>
<div class="mt-3">
<label class="text-xs text-gray-400">Custom Socket Names (comma-separated: N,E,S,W)</label>
<input type="text" asp-for="Tile.SocketCustom" class="input w-full text-sm" placeholder="wall-edge,wall-edge,open,open" />
</div>
<div class="flex gap-2 mt-3">
<button type="button" onclick="setAllSockets('Open')" class="btn btn-ghost text-xs flex-1">All Open</button>
<button type="button" onclick="setAllSockets('Closed')" class="btn btn-ghost text-xs flex-1">All Closed</button>
<button type="button" onclick="autoSockets()" class="btn btn-ghost text-xs flex-1">Auto</button>
</div>
</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>
<!-- Quick Templates -->
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Quick Fill</h3>
<div class="grid grid-cols-2 gap-2">
<button type="button" onclick="fillAll()" class="btn btn-secondary text-xs">Fill All</button>
<button type="button" onclick="fillBorder()" class="btn btn-secondary text-xs">Fill Border</button>
</div>
</div>
</div>
</div>
<div class="flex justify-end gap-4 mt-6">
<a href="/admin/dungeonbuilder/tiles" class="btn btn-ghost">Cancel</a>
<button type="submit" class="btn btn-primary">
@(Model.Tile.Id == 0 ? "Create Template" : "Save Changes")
</button>
</div>
</form>
<script>
const cdnBase = '@Model.CdnBaseUrl';
let tw = @Model.Tile.Width, th = @Model.Tile.Height;
let sheet = null;
let selected = null;
let templateData = {}; // { "x,y": { sheetId, frameIndex, tileDefId } }
let primarySet = false;
document.addEventListener('DOMContentLoaded', () => {
loadExisting();
buildTemplateGrid();
});
function loadExisting() {
const json = document.getElementById('tilesJson').value;
if (json) {
try {
JSON.parse(json).forEach(t => {
templateData[`${t.x},${t.y}`] = { sheetId: t.sheetId, frameIndex: t.frameIndex, tileDefId: t.tileDefId };
});
primarySet = Object.keys(templateData).length > 0;
} catch {}
}
}
function buildTemplateGrid() {
const grid = document.getElementById('templateGrid');
grid.style.gridTemplateColumns = `repeat(${tw}, 32px)`;
grid.innerHTML = '';
for (let y = 0; y < th; y++) {
for (let x = 0; x < tw; x++) {
const cell = document.createElement('div');
cell.className = 'tile-cell';
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(`.tile-cell[data-x="${x}"][data-y="${y}"]`);
if (!cell) return;
const data = templateData[`${x},${y}`];
if (data && sheet && data.sheetId == sheet.id) {
const col = data.frameIndex % sheet.cols;
const row = Math.floor(data.frameIndex / sheet.cols);
cell.style.background = `url('${cdnBase}/downloads/${sheet.cdn}') ${-col * sheet.tw}px ${-row * sheet.th}px no-repeat`;
cell.style.backgroundSize = `${sheet.sw}px ${sheet.sh}px`;
} else if (data) {
cell.style.background = '#2d3748';
} else {
cell.style.background = '#0f0f1a';
}
}
function paintCell(x, y) {
if (!selected) return;
const key = `${x},${y}`;
templateData[key] = { sheetId: selected.sheetId, frameIndex: selected.frameIndex };
// First tile becomes primary
if (!primarySet) {
document.getElementById('primaryTileId').value = selected.tileDefId || '';
primarySet = true;
}
renderCell(x, y);
saveData();
}
function loadSheet() {
const opt = document.getElementById('sheetSelect').selectedOptions[0];
if (!opt?.value) {
sheet = null;
document.getElementById('palette').innerHTML = '<div class="text-gray-500 text-xs p-2">Select a sprite sheet</div>';
return;
}
sheet = {
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
};
const total = sheet.cols * Math.ceil(sheet.sh / sheet.th);
let html = '';
for (let i = 0; i < total; i++) {
const col = i % sheet.cols;
const row = Math.floor(i / sheet.cols);
html += `<div class="palette-tile" data-frame="${i}" onclick="selectTile(${i})"
style="background: url('${cdnBase}/downloads/${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;
// Re-render template with new sheet
for (let y = 0; y < th; y++) {
for (let x = 0; x < tw; x++) {
renderCell(x, y);
}
}
}
function selectTile(frameIndex) {
document.querySelectorAll('.palette-tile').forEach(t => t.classList.remove('active'));
document.querySelector(`.palette-tile[data-frame="${frameIndex}"]`)?.classList.add('active');
selected = { sheetId: sheet.id, frameIndex };
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}/downloads/${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 resizeTemplate() {
tw = +document.getElementById('tileWidth').value || 1;
th = +document.getElementById('tileHeight').value || 1;
tw = Math.max(1, Math.min(10, tw));
th = Math.max(1, Math.min(10, th));
buildTemplateGrid();
}
function clearTemplate() {
if (!confirm('Clear all tiles?')) return;
templateData = {};
primarySet = false;
document.getElementById('primaryTileId').value = '';
buildTemplateGrid();
saveData();
}
function fillAll() {
if (!selected) { alert('Select a tile first'); return; }
for (let y = 0; y < th; y++) {
for (let x = 0; x < tw; x++) {
templateData[`${x},${y}`] = { ...selected };
}
}
if (!primarySet) {
document.getElementById('primaryTileId').value = '';
primarySet = true;
}
buildTemplateGrid();
saveData();
}
function fillBorder() {
if (!selected) { alert('Select a tile first'); return; }
for (let x = 0; x < tw; x++) {
templateData[`${x},0`] = { ...selected };
templateData[`${x},${th-1}`] = { ...selected };
}
for (let y = 0; y < th; y++) {
templateData[`0,${y}`] = { ...selected };
templateData[`${tw-1},${y}`] = { ...selected };
}
buildTemplateGrid();
saveData();
}
function setAllSockets(type) {
document.querySelector('[name="Tile.SocketN"]').value = type;
document.querySelector('[name="Tile.SocketE"]').value = type;
document.querySelector('[name="Tile.SocketS"]').value = type;
document.querySelector('[name="Tile.SocketW"]').value = type;
}
function autoSockets() {
const cat = document.querySelector('[name="Tile.Category"]').value;
if (cat === 'Floor' || cat === 'Decoration') {
setAllSockets('Open');
document.getElementById('isWalkable').checked = true;
} else if (cat === 'Wall') {
setAllSockets('Closed');
document.getElementById('isWalkable').checked = false;
} else if (cat === 'Door') {
// Vertical door default
document.querySelector('[name="Tile.SocketN"]').value = 'Open';
document.querySelector('[name="Tile.SocketS"]').value = 'Open';
document.querySelector('[name="Tile.SocketE"]').value = 'Closed';
document.querySelector('[name="Tile.SocketW"]').value = 'Closed';
}
}
function onCategoryChange() {
autoSockets();
}
function saveData() {
const tiles = Object.entries(templateData).map(([k, v]) => {
const [x, y] = k.split(',').map(Number);
return { x, y, sheetId: v.sheetId, frameIndex: v.frameIndex, tileDefId: v.tileDefId };
});
document.getElementById('tilesJson').value = JSON.stringify(tiles);
}
document.getElementById('tileForm').onsubmit = () => saveData();
</script>