819 lines
32 KiB
Plaintext
819 lines
32 KiB
Plaintext
@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 {
|
||
background: #0f0f1a;
|
||
cursor: crosshair;
|
||
position: relative;
|
||
image-rendering: pixelated;
|
||
}
|
||
.tile-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; }
|
||
.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">← 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 (NEW) -->
|
||
<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">
|
||
Configure sockets for each edge. Multi-tile templates have one socket per edge cell.
|
||
<br/>Tiles connect when sockets match: Open↔Open, Closed↔Closed, Custom:name↔Custom:name
|
||
</p>
|
||
|
||
<input type="hidden" name="SocketConfigJson" id="socketConfigJson" value="@Model.SocketConfigJson" />
|
||
|
||
<!-- Socket Visual Editor -->
|
||
<div id="socketEditor" class="bg-gray-900 rounded p-4 mb-3">
|
||
<!-- Will be populated by JavaScript -->
|
||
</div>
|
||
|
||
<!-- Quick Actions -->
|
||
<div class="flex gap-2 mb-3">
|
||
<button type="button" onclick="setAllSocketsNew('Open')" class="btn btn-ghost text-xs flex-1">All Open</button>
|
||
<button type="button" onclick="setAllSocketsNew('Closed')" class="btn btn-ghost text-xs flex-1">All Closed</button>
|
||
<button type="button" onclick="autoSocketsNew()" class="btn btn-ghost text-xs flex-1">Auto</button>
|
||
</div>
|
||
|
||
<!-- Custom Socket Name Input with Autocomplete -->
|
||
<div class="relative" id="customNameContainer" style="display: none;">
|
||
<label class="text-xs text-gray-400">Custom Socket Name</label>
|
||
<input type="text" id="customNameInput" class="input w-full text-sm"
|
||
placeholder="Type to search or create..."
|
||
oninput="filterSocketNames(this.value)"
|
||
onfocus="showSocketDropdown()"
|
||
autocomplete="off" />
|
||
<div id="socketNameDropdown" class="absolute z-50 w-full bg-gray-800 border border-gray-600 rounded-b max-h-48 overflow-auto hidden">
|
||
<!-- Autocomplete options populated by JS -->
|
||
</div>
|
||
<p class="text-xs text-gray-500 mt-1">Names are stored lowercase. Click a suggestion or press Enter to create new.</p>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Legacy Socket Fields (hidden, for backward compatibility) -->
|
||
<input type="hidden" asp-for="Tile.SocketN" id="legacySocketN" />
|
||
<input type="hidden" asp-for="Tile.SocketE" id="legacySocketE" />
|
||
<input type="hidden" asp-for="Tile.SocketS" id="legacySocketS" />
|
||
<input type="hidden" asp-for="Tile.SocketW" id="legacySocketW" />
|
||
<input type="hidden" asp-for="Tile.SocketCustom" id="legacySocketCustom" />
|
||
</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 currentSheetId = null; // Currently selected sheet for painting
|
||
let selected = null;
|
||
let templateData = {}; // { "x,y": { sheetId, frameIndex, tileDefId } }
|
||
let primarySet = false;
|
||
|
||
// All loaded sheets (by ID) for rendering tiles from any sheet
|
||
const loadedSheets = {};
|
||
|
||
// Pre-load all sheet data from the dropdown
|
||
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();
|
||
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');
|
||
|
||
// Use current sheet dimensions, or try to get from existing tile data, or fallback to 32px
|
||
let cellW = 32, cellH = 32;
|
||
if (currentSheetId && loadedSheets[currentSheetId]) {
|
||
cellW = loadedSheets[currentSheetId].tw;
|
||
cellH = loadedSheets[currentSheetId].th;
|
||
} else {
|
||
// Try to get dimensions from existing template data
|
||
const firstTile = Object.values(templateData)[0];
|
||
if (firstTile && loadedSheets[firstTile.sheetId]) {
|
||
cellW = loadedSheets[firstTile.sheetId].tw;
|
||
cellH = loadedSheets[firstTile.sheetId].th;
|
||
}
|
||
}
|
||
|
||
grid.style.gridTemplateColumns = `repeat(${tw}, ${cellW}px)`;
|
||
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.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(`.tile-cell[data-x="${x}"][data-y="${y}"]`);
|
||
if (!cell) return;
|
||
|
||
const data = templateData[`${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 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) {
|
||
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];
|
||
|
||
// Don't rebuild template grid - just re-render to keep existing tiles visible
|
||
// Only rebuild if dimensions might need to change
|
||
for (let y = 0; y < th; y++) {
|
||
for (let x = 0; x < tw; x++) {
|
||
renderCell(x, y);
|
||
}
|
||
}
|
||
|
||
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="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) {
|
||
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 };
|
||
|
||
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 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();
|
||
}
|
||
|
||
// ============================================================
|
||
// NEW SOCKET EDITOR
|
||
// ============================================================
|
||
|
||
// All existing socket names for autocomplete (from server)
|
||
const allSocketNames = @Html.Raw(System.Text.Json.JsonSerializer.Serialize(Model.AllSocketNames));
|
||
|
||
// Current socket configuration
|
||
let socketConfig = { north: [], east: [], south: [], west: [] };
|
||
|
||
// Currently editing socket (for custom name input)
|
||
let editingSocket = null;
|
||
|
||
// Initialize socket config from hidden field
|
||
function initSocketConfig() {
|
||
const json = document.getElementById('socketConfigJson').value;
|
||
if (json) {
|
||
try {
|
||
socketConfig = JSON.parse(json);
|
||
} catch (e) {
|
||
console.error('Failed to parse socket config:', e);
|
||
}
|
||
}
|
||
|
||
// Ensure arrays exist and have correct length for current dimensions
|
||
resizeSocketConfig();
|
||
renderSocketEditor();
|
||
}
|
||
|
||
// Resize socket arrays when template dimensions change
|
||
function resizeSocketConfig() {
|
||
const dirs = ['north', 'south', 'east', 'west'];
|
||
dirs.forEach(dir => {
|
||
if (!socketConfig[dir]) socketConfig[dir] = [];
|
||
});
|
||
|
||
// North and South have 'tw' sockets (width)
|
||
while (socketConfig.north.length < tw) socketConfig.north.push({ type: 'Open', customName: null });
|
||
while (socketConfig.north.length > tw) socketConfig.north.pop();
|
||
while (socketConfig.south.length < tw) socketConfig.south.push({ type: 'Open', customName: null });
|
||
while (socketConfig.south.length > tw) socketConfig.south.pop();
|
||
|
||
// East and West have 'th' sockets (height)
|
||
while (socketConfig.east.length < th) socketConfig.east.push({ type: 'Open', customName: null });
|
||
while (socketConfig.east.length > th) socketConfig.east.pop();
|
||
while (socketConfig.west.length < th) socketConfig.west.push({ type: 'Open', customName: null });
|
||
while (socketConfig.west.length > th) socketConfig.west.pop();
|
||
}
|
||
|
||
// Render the visual socket editor
|
||
function renderSocketEditor() {
|
||
const container = document.getElementById('socketEditor');
|
||
const cellSize = 40;
|
||
const socketSize = 12;
|
||
|
||
// Calculate grid dimensions
|
||
const gridW = tw * cellSize;
|
||
const gridH = th * cellSize;
|
||
const padding = 30; // Space for socket indicators
|
||
|
||
let html = `<div style="position: relative; width: ${gridW + padding*2}px; height: ${gridH + padding*2}px; margin: 0 auto;">`;
|
||
|
||
// Draw the template grid representation
|
||
html += `<div style="position: absolute; left: ${padding}px; top: ${padding}px; width: ${gridW}px; height: ${gridH}px;
|
||
background: #1a1a2e; border: 2px solid #374151; display: grid;
|
||
grid-template-columns: repeat(${tw}, 1fr); gap: 1px;">`;
|
||
for (let i = 0; i < tw * th; i++) {
|
||
html += `<div style="background: #0f0f1a;"></div>`;
|
||
}
|
||
html += `</div>`;
|
||
|
||
// North sockets (top edge)
|
||
for (let i = 0; i < tw; i++) {
|
||
const s = socketConfig.north[i];
|
||
const x = padding + (i + 0.5) * cellSize - socketSize/2;
|
||
const y = padding - socketSize - 4;
|
||
html += renderSocketButton('north', i, s, x, y);
|
||
}
|
||
|
||
// South sockets (bottom edge)
|
||
for (let i = 0; i < tw; i++) {
|
||
const s = socketConfig.south[i];
|
||
const x = padding + (i + 0.5) * cellSize - socketSize/2;
|
||
const y = padding + gridH + 4;
|
||
html += renderSocketButton('south', i, s, x, y);
|
||
}
|
||
|
||
// East sockets (right edge)
|
||
for (let i = 0; i < th; i++) {
|
||
const s = socketConfig.east[i];
|
||
const x = padding + gridW + 4;
|
||
const y = padding + (i + 0.5) * cellSize - socketSize/2;
|
||
html += renderSocketButton('east', i, s, x, y);
|
||
}
|
||
|
||
// West sockets (left edge)
|
||
for (let i = 0; i < th; i++) {
|
||
const s = socketConfig.west[i];
|
||
const x = padding - socketSize - 4;
|
||
const y = padding + (i + 0.5) * cellSize - socketSize/2;
|
||
html += renderSocketButton('west', i, s, x, y);
|
||
}
|
||
|
||
html += `</div>`;
|
||
|
||
// Legend
|
||
html += `<div class="flex gap-4 justify-center mt-3 text-xs">
|
||
<span class="flex items-center gap-1"><span class="w-3 h-3 rounded bg-green-500"></span> Open</span>
|
||
<span class="flex items-center gap-1"><span class="w-3 h-3 rounded bg-red-500"></span> Closed</span>
|
||
<span class="flex items-center gap-1"><span class="w-3 h-3 rounded bg-yellow-500"></span> Custom</span>
|
||
</div>`;
|
||
|
||
container.innerHTML = html;
|
||
}
|
||
|
||
function renderSocketButton(direction, index, socket, x, y) {
|
||
const colors = { 'Open': '#4ade80', 'Closed': '#f87171', 'Custom': '#facc15' };
|
||
const color = colors[socket?.type] || colors['Open'];
|
||
const title = socket?.type === 'Custom' ? `Custom: ${socket.customName || '(none)'}` : socket?.type || 'Open';
|
||
|
||
return `<button type="button"
|
||
onclick="cycleSocket('${direction}', ${index})"
|
||
oncontextmenu="editCustomSocket('${direction}', ${index}); return false;"
|
||
style="position: absolute; left: ${x}px; top: ${y}px; width: 12px; height: 12px;
|
||
background: ${color}; border: 1px solid rgba(255,255,255,0.3); border-radius: 2px;
|
||
cursor: pointer; font-size: 8px; line-height: 10px; text-align: center;"
|
||
title="${title} (click to cycle, right-click for custom)">
|
||
${socket?.type === 'Custom' ? 'C' : ''}
|
||
</button>`;
|
||
}
|
||
|
||
// Cycle through socket types: Open -> Closed -> Custom -> Open
|
||
function cycleSocket(direction, index) {
|
||
const socket = socketConfig[direction][index];
|
||
const types = ['Open', 'Closed', 'Custom'];
|
||
const currentIdx = types.indexOf(socket?.type || 'Open');
|
||
const nextType = types[(currentIdx + 1) % types.length];
|
||
|
||
socketConfig[direction][index] = {
|
||
type: nextType,
|
||
customName: nextType === 'Custom' ? (socket?.customName || '') : null
|
||
};
|
||
|
||
if (nextType === 'Custom') {
|
||
editCustomSocket(direction, index);
|
||
}
|
||
|
||
renderSocketEditor();
|
||
saveSocketConfig();
|
||
}
|
||
|
||
// Open custom name editor for a socket
|
||
function editCustomSocket(direction, index) {
|
||
editingSocket = { direction, index };
|
||
const socket = socketConfig[direction][index];
|
||
|
||
document.getElementById('customNameContainer').style.display = 'block';
|
||
const input = document.getElementById('customNameInput');
|
||
input.value = socket?.customName || '';
|
||
input.focus();
|
||
input.select();
|
||
|
||
// Update socket to Custom type if not already
|
||
if (socket?.type !== 'Custom') {
|
||
socketConfig[direction][index] = { type: 'Custom', customName: socket?.customName || '' };
|
||
renderSocketEditor();
|
||
}
|
||
}
|
||
|
||
// Filter socket names dropdown based on input
|
||
function filterSocketNames(query) {
|
||
const dropdown = document.getElementById('socketNameDropdown');
|
||
query = query.toLowerCase().trim();
|
||
|
||
let matches = allSocketNames.filter(name => name.includes(query));
|
||
|
||
// If query doesn't match anything exactly, allow creating new
|
||
const exactMatch = allSocketNames.includes(query);
|
||
|
||
let html = '';
|
||
if (query && !exactMatch) {
|
||
html += `<div class="px-3 py-2 hover:bg-gray-700 cursor-pointer text-yellow-400"
|
||
onclick="selectSocketName('${query.replace(/'/g, "\\'")}')">
|
||
Create: "${query}"
|
||
</div>`;
|
||
}
|
||
|
||
matches.slice(0, 15).forEach(name => {
|
||
html += `<div class="px-3 py-2 hover:bg-gray-700 cursor-pointer"
|
||
onclick="selectSocketName('${name.replace(/'/g, "\\'")}')">
|
||
${name}
|
||
</div>`;
|
||
});
|
||
|
||
if (!html) {
|
||
html = '<div class="px-3 py-2 text-gray-500">No matches</div>';
|
||
}
|
||
|
||
dropdown.innerHTML = html;
|
||
dropdown.classList.remove('hidden');
|
||
}
|
||
|
||
function showSocketDropdown() {
|
||
filterSocketNames(document.getElementById('customNameInput').value);
|
||
}
|
||
|
||
function hideSocketDropdown() {
|
||
setTimeout(() => {
|
||
document.getElementById('socketNameDropdown').classList.add('hidden');
|
||
}, 200);
|
||
}
|
||
|
||
function selectSocketName(name) {
|
||
name = name.toLowerCase().trim();
|
||
|
||
if (editingSocket) {
|
||
socketConfig[editingSocket.direction][editingSocket.index].customName = name;
|
||
renderSocketEditor();
|
||
saveSocketConfig();
|
||
}
|
||
|
||
document.getElementById('customNameInput').value = name;
|
||
document.getElementById('socketNameDropdown').classList.add('hidden');
|
||
|
||
// Add to local list if new
|
||
if (!allSocketNames.includes(name)) {
|
||
allSocketNames.push(name);
|
||
allSocketNames.sort();
|
||
}
|
||
}
|
||
|
||
// Handle Enter key in custom name input
|
||
document.addEventListener('DOMContentLoaded', () => {
|
||
const input = document.getElementById('customNameInput');
|
||
if (input) {
|
||
input.addEventListener('keydown', (e) => {
|
||
if (e.key === 'Enter') {
|
||
e.preventDefault();
|
||
selectSocketName(input.value);
|
||
} else if (e.key === 'Escape') {
|
||
document.getElementById('customNameContainer').style.display = 'none';
|
||
editingSocket = null;
|
||
}
|
||
});
|
||
input.addEventListener('blur', hideSocketDropdown);
|
||
}
|
||
});
|
||
|
||
// Set all sockets to a type
|
||
function setAllSocketsNew(type) {
|
||
['north', 'south', 'east', 'west'].forEach(dir => {
|
||
socketConfig[dir].forEach((s, i) => {
|
||
socketConfig[dir][i] = { type: type, customName: null };
|
||
});
|
||
});
|
||
renderSocketEditor();
|
||
saveSocketConfig();
|
||
}
|
||
|
||
// Auto-configure sockets based on category
|
||
function autoSocketsNew() {
|
||
const cat = document.querySelector('[name="Tile.Category"]').value;
|
||
if (cat === 'Floor' || cat === 'Decoration') {
|
||
setAllSocketsNew('Open');
|
||
document.getElementById('isWalkable').checked = true;
|
||
} else if (cat === 'Wall') {
|
||
setAllSocketsNew('Closed');
|
||
document.getElementById('isWalkable').checked = false;
|
||
} else if (cat === 'Door') {
|
||
// Doors: Open on N/S, Closed on E/W
|
||
socketConfig.north.forEach((s, i) => socketConfig.north[i] = { type: 'Open', customName: null });
|
||
socketConfig.south.forEach((s, i) => socketConfig.south[i] = { type: 'Open', customName: null });
|
||
socketConfig.east.forEach((s, i) => socketConfig.east[i] = { type: 'Closed', customName: null });
|
||
socketConfig.west.forEach((s, i) => socketConfig.west[i] = { type: 'Closed', customName: null });
|
||
renderSocketEditor();
|
||
saveSocketConfig();
|
||
}
|
||
}
|
||
|
||
// Save socket config to hidden field
|
||
function saveSocketConfig() {
|
||
document.getElementById('socketConfigJson').value = JSON.stringify(socketConfig);
|
||
}
|
||
|
||
// Override resize to also resize sockets
|
||
const originalResizeTemplate = resizeTemplate;
|
||
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();
|
||
resizeSocketConfig();
|
||
renderSocketEditor();
|
||
saveSocketConfig();
|
||
}
|
||
|
||
// Legacy functions (keep for backward compatibility)
|
||
function setAllSockets(type) {
|
||
setAllSocketsNew(type);
|
||
}
|
||
|
||
function autoSockets() {
|
||
autoSocketsNew();
|
||
}
|
||
|
||
function onCategoryChange() {
|
||
autoSocketsNew();
|
||
}
|
||
|
||
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);
|
||
saveSocketConfig();
|
||
}
|
||
|
||
// Initialize socket editor on page load
|
||
document.addEventListener('DOMContentLoaded', () => {
|
||
initSocketConfig();
|
||
});
|
||
|
||
document.getElementById('tileForm').onsubmit = () => { saveData(); saveSocketConfig(); };
|
||
</script>
|