Switch NPC editor to mob-style animation assignment selection

This commit is contained in:
dingo
2026-02-14 02:06:51 -05:00
parent 4ba5a1040b
commit 40d8570550
2 changed files with 347 additions and 204 deletions
+182 -108
View File
@@ -4,8 +4,23 @@
@using DataServer.Data.Enums
@{
ViewData["Title"] = Model.IsNew ? "Create NPC" : $"Edit: {Model.Npc.Name}";
var selectedAbilitiesJson = JsonSerializer.Serialize(Model.SelectedAbilityIds);
var spriteSheetOptions = JsonSerializer.Serialize(Model.AllSpriteSheets.Select(s => new { id = s.Id, name = s.Name, cdnPath = s.CdnPath }));
var selectedAnimationsJson = JsonSerializer.Serialize(Model.SelectedAnimations);
var animationCatalogJson = JsonSerializer.Serialize(Model.AllAnimations.Select(a => new
{
id = a.Id,
key = a.Key,
sheetName = a.SpriteSheet != null ? a.SpriteSheet.Name : "",
sheetPath = a.SpriteSheet != null ? a.SpriteSheet.CdnPath : "",
frameSequence = a.FrameSequence,
fps = a.Fps,
loop = a.Loop,
flipX = a.FlipX,
flipY = a.FlipY,
search = ($"{a.Key} {(a.Name ?? "")} {(a.SpriteSheet != null ? a.SpriteSheet.Name : "")} {(a.SpriteSheet != null ? a.SpriteSheet.CdnPath : "")}").ToLowerInvariant()
}));
var resistanceOptions = string.Join("", Model.ResistanceTypes.Select(rt => $"<option value=\"{rt.Key}\">{rt.DisplayName}</option>"));
}
@@ -146,7 +161,7 @@
</div>
<div class="card">
<div class="p-4 border-b border-gray-700 flex items-center justify-between">
<div class="p-4 border-b border-gray-700">
<h2 class="text-lg font-semibold text-white">Damage Resistances</h2>
</div>
<div class="p-6">
@@ -161,8 +176,7 @@
}
@foreach (var resist in resistances)
{
var bgColor = resist.Value < 0 ? "bg-red-900/30 border-red-700" : resist.Value > 0 ? "bg-green-900/30 border-green-700" : "bg-gray-800 border-gray-700";
<div class="@bgColor border rounded-lg p-3 flex items-center gap-4 resistance-row">
<div class="bg-gray-800 border border-gray-700 rounded-lg p-3 flex items-center gap-4 resistance-row">
<select name="resist_name[]" class="input w-40">
@foreach (var rt in Model.ResistanceTypes)
{
@@ -176,7 +190,7 @@
<span class="text-gray-400">%</span>
</div>
</div>
<button type="button" onclick="this.closest('.resistance-row').remove()" class="p-2 text-red-400 hover:text-red-300">Remove</button>
<button type="button" onclick="this.closest('.resistance-row').remove()" class="btn btn-danger btn-sm">Remove</button>
</div>
}
</div>
@@ -225,62 +239,54 @@
</div>
</div>
<div class="card">
<div class="p-4 border-b border-gray-700">
<h2 class="text-lg font-semibold text-white">Sprite Config</h2>
<div class="card" id="animations-section">
<div class="p-4 border-b border-gray-700 flex items-center justify-between">
<h2 class="text-lg font-semibold text-white">Animations</h2>
<button type="button" class="btn btn-primary btn-sm" onclick="showAddAnimationModal()">+ Add Animation</button>
</div>
<div class="p-6 space-y-4">
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="p-6 overflow-x-auto">
<div id="animationHiddenInputs"></div>
<table class="w-full">
<thead class="bg-gray-900/50">
<tr>
<th class="table-header">Key</th>
<th class="table-header">Sheet</th>
<th class="table-header">Frames</th>
<th class="table-header">FPS</th>
<th class="table-header">Options</th>
<th class="table-header">Override Key</th>
<th class="table-header">Status</th>
<th class="table-header">Actions</th>
</tr>
</thead>
<tbody id="npcAnimationTableBody" class="divide-y divide-gray-700"></tbody>
</table>
</div>
</div>
<div id="addAnimationModal" class="fixed inset-0 bg-black/50 z-50 hidden items-center justify-center">
<div class="bg-gray-900 rounded-xl shadow-xl p-6 w-full max-w-md mx-4 border border-gray-700">
<h3 class="text-lg font-semibold text-white mb-4">Add Animation</h3>
<div class="space-y-4">
<div>
<label class="label">Frame Width</label>
<input type="number" name="sprite_frameWidth" value="@Model.SpriteConfig.FrameWidth" class="input" min="8" step="8" />
<label class="label">Search</label>
<input id="animationSearch" type="text" class="input mb-2" placeholder="Type to search animations..." oninput="filterAnimations()" autocomplete="off" />
<select id="animationSelect" class="input" size="10" style="height: auto;">
@foreach (var anim in Model.AllAnimations)
{
<option value="@anim.Id" data-search="@($"[{(anim.SpriteSheet?.Name ?? "?")}] {anim.Key} {anim.FrameSequence} {anim.Fps}".ToLowerInvariant())">[@(anim.SpriteSheet?.Name ?? "?")] @anim.Key - @anim.FrameSequence (@anim.Fps fps)</option>
}
</select>
</div>
<div>
<label class="label">Frame Height</label>
<input type="number" name="sprite_frameHeight" value="@Model.SpriteConfig.FrameHeight" class="input" min="8" step="8" />
</div>
<div>
<label class="label">Default FPS</label>
<input type="number" name="sprite_fps" value="@Model.SpriteConfig.Fps" class="input" min="1" max="60" />
<label class="label">Override Key (optional)</label>
<input id="animationOverrideKey" type="text" class="input" placeholder="e.g. idle_down" />
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="label">Filter Sprite Sheets</label>
<input id="spriteSheetSearch" type="text" class="input" placeholder="Type to filter sprite sheets..." oninput="filterSpriteSheets()" />
</div>
<div class="flex justify-end gap-3 pt-4">
<button type="button" onclick="closeAddAnimationModal()" class="btn btn-secondary">Cancel</button>
<button type="button" onclick="addAnimationFromModal()" class="btn btn-primary">Assign</button>
</div>
<div id="spriteRows" class="space-y-2">
@for (var i = 0; i < Model.SpriteRows.Count; i++)
{
var row = Model.SpriteRows[i];
<div class="grid grid-cols-1 md:grid-cols-12 gap-2 sprite-row">
<select name="sprite_key[]" class="input md:col-span-2">
@foreach (var key in new[] { "idle_front", "idle_back", "idle_left", "idle_right", "run_front", "run_back", "run_left", "run_right", "attack_front", "attack_back", "death" })
{
<option value="@key" selected="@(row.Key == key)">@key</option>
}
</select>
<select name="sprite_sheet_id[]" class="input md:col-span-5 sprite-sheet-select">
<option value="">Select sheet...</option>
@foreach (var s in Model.AllSpriteSheets)
{
<option value="@s.Id" selected="@(row.SheetId == s.Id)" data-search="@($"{s.Name} {s.CdnPath}".ToLowerInvariant())">@s.Name (@s.CdnPath)</option>
}
</select>
<input type="number" name="sprite_frames[]" value="@row.Frames" class="input md:col-span-2" min="1" placeholder="Frames" />
<label class="md:col-span-2 flex items-center gap-2 text-sm text-gray-300">
<input type="checkbox" name="sprite_loop[]" value="@i" checked="@row.Loop" class="rounded border-gray-600 bg-gray-800" />
Loop
</label>
<button type="button" class="btn btn-danger md:col-span-1" onclick="this.closest('.sprite-row').remove()">X</button>
</div>
}
</div>
<button type="button" class="btn btn-secondary" onclick="addSpriteRow()">Add Sprite Row</button>
</div>
</div>
@@ -343,8 +349,9 @@
@section Scripts {
<script>
const selectedAbilitiesSeed = @Html.Raw(selectedAbilitiesJson);
const spriteSheetCatalog = @Html.Raw(spriteSheetOptions);
const selectedAbilityIds = @Html.Raw(selectedAbilitiesJson);
const selectedAnimations = @Html.Raw(selectedAnimationsJson);
const animationCatalog = @Html.Raw(animationCatalogJson);
const resistanceOptionsHtml = `@Html.Raw(resistanceOptions)`;
const abilityById = {
@@ -357,7 +364,7 @@
function renderSelectedAbilities() {
const container = document.getElementById('selectedAbilities');
container.innerHTML = '';
selectedAbilitiesSeed.forEach(id => {
selectedAbilityIds.forEach(id => {
const row = document.createElement('div');
row.className = 'flex items-center justify-between bg-gray-800 border border-gray-700 rounded p-2';
const label = abilityById[id] || ('Ability #' + id);
@@ -367,9 +374,9 @@
}
function removeAbility(id) {
const idx = selectedAbilitiesSeed.indexOf(id);
const idx = selectedAbilityIds.indexOf(id);
if (idx >= 0) {
selectedAbilitiesSeed.splice(idx, 1);
selectedAbilityIds.splice(idx, 1);
renderSelectedAbilities();
}
}
@@ -378,8 +385,8 @@
const select = document.getElementById('abilityPicker');
const id = parseInt(select.value || '0', 10);
if (!id) return;
if (!selectedAbilitiesSeed.includes(id)) {
selectedAbilitiesSeed.push(id);
if (!selectedAbilityIds.includes(id)) {
selectedAbilityIds.push(id);
renderSelectedAbilities();
}
}
@@ -412,65 +419,132 @@
<span class="text-gray-400">%</span>
</div>
</div>
<button type="button" onclick="this.closest('.resistance-row').remove()" class="p-2 text-red-400 hover:text-red-300">Remove</button>
<button type="button" onclick="this.closest('.resistance-row').remove()" class="btn btn-danger btn-sm">Remove</button>
`;
document.getElementById('resistancesList').appendChild(row);
typeSelect.value = '';
valueInput.value = '50';
}
function filterSpriteSheets() {
const term = (document.getElementById('spriteSheetSearch').value || '').toLowerCase();
const selects = document.querySelectorAll('.sprite-sheet-select');
selects.forEach(select => {
for (let i = 0; i < select.options.length; i++) {
const opt = select.options[i];
const text = (opt.dataset.search || opt.text || '').toLowerCase();
if (opt.value === '') {
opt.style.display = '';
} else {
opt.style.display = term === '' || text.includes(term) ? '' : 'none';
}
function getAnimationById(id) {
return animationCatalog.find(a => a.id === id) || null;
}
function showAddAnimationModal() {
document.getElementById('addAnimationModal').classList.remove('hidden');
document.getElementById('addAnimationModal').classList.add('flex');
}
function closeAddAnimationModal() {
document.getElementById('addAnimationModal').classList.add('hidden');
document.getElementById('addAnimationModal').classList.remove('flex');
document.getElementById('animationSearch').value = '';
filterAnimations();
}
function filterAnimations() {
const term = (document.getElementById('animationSearch').value || '').toLowerCase();
const select = document.getElementById('animationSelect');
for (let i = 0; i < select.options.length; i++) {
const opt = select.options[i];
const text = (opt.dataset.search || opt.text || '').toLowerCase();
opt.style.display = term === '' || text.includes(term) ? '' : 'none';
}
}
function addAnimationFromModal() {
const select = document.getElementById('animationSelect');
const id = parseInt(select.value || '0', 10);
if (!id) return;
const override = (document.getElementById('animationOverrideKey').value || '').trim();
const exists = selectedAnimations.some(a => a.animationDefinitionId === id && (a.overrideKey || '') === override);
if (exists) return;
selectedAnimations.push({
animationDefinitionId: id,
overrideKey: override || null,
isEnabled: true
});
document.getElementById('animationOverrideKey').value = '';
closeAddAnimationModal();
renderAnimationTable();
}
function removeAnimation(idx) {
selectedAnimations.splice(idx, 1);
renderAnimationTable();
}
function toggleAnimation(idx) {
selectedAnimations[idx].isEnabled = !selectedAnimations[idx].isEnabled;
renderAnimationTable();
}
function renderAnimationTable() {
const body = document.getElementById('npcAnimationTableBody');
const hiddenInputs = document.getElementById('animationHiddenInputs');
body.innerHTML = '';
hiddenInputs.innerHTML = '';
if (selectedAnimations.length === 0) {
const tr = document.createElement('tr');
tr.innerHTML = '<td class="table-cell text-center text-gray-500 py-8" colspan="8">No animations assigned. Add animations from the selector.</td>';
body.appendChild(tr);
return;
}
selectedAnimations.forEach((row, idx) => {
const anim = getAnimationById(row.animationDefinitionId);
if (!anim) return;
const key = row.overrideKey && row.overrideKey.length ? row.overrideKey : anim.key;
const options = [anim.loop ? 'Loop' : null, anim.flipX ? 'FlipX' : null, anim.flipY ? 'FlipY' : null].filter(Boolean).join(', ');
const tr = document.createElement('tr');
tr.className = 'hover:bg-gray-700/30';
tr.innerHTML = `
<td class="table-cell text-white font-mono">${anim.key}</td>
<td class="table-cell text-gray-300 text-xs">${anim.sheetName}</td>
<td class="table-cell text-gray-300 font-mono text-xs">${anim.frameSequence}</td>
<td class="table-cell text-gray-300">${anim.fps}</td>
<td class="table-cell text-gray-400 text-xs">${options || '-'}</td>
<td class="table-cell text-gray-300 font-mono text-xs">
<input type="text" class="input" value="${row.overrideKey || ''}" placeholder="-" onchange="updateOverride(${idx}, this.value)" />
</td>
<td class="table-cell">
<span class="${row.isEnabled ? 'text-green-400' : 'text-gray-500'} text-xs">${row.isEnabled ? 'Active' : 'Disabled'}</span>
</td>
<td class="table-cell">
<div class="flex gap-2">
<button type="button" class="btn btn-secondary btn-sm" onclick="toggleAnimation(${idx})">${row.isEnabled ? 'Disable' : 'Enable'}</button>
<button type="button" class="btn btn-danger btn-sm" onclick="removeAnimation(${idx})">Remove</button>
</div>
</td>
`;
body.appendChild(tr);
hiddenInputs.insertAdjacentHTML('beforeend', `<input type="hidden" name="npc_anim_id[]" value="${row.animationDefinitionId}">`);
hiddenInputs.insertAdjacentHTML('beforeend', `<input type="hidden" name="npc_anim_override_key[]" value="${(row.overrideKey || '').replace(/"/g, '&quot;')}">`);
if (row.isEnabled) {
hiddenInputs.insertAdjacentHTML('beforeend', `<input type="hidden" name="npc_anim_enabled[]" value="${idx}">`);
}
});
}
function addSpriteRow() {
const idx = document.querySelectorAll('#spriteRows .sprite-row').length;
const row = document.createElement('div');
row.className = 'grid grid-cols-1 md:grid-cols-12 gap-2 sprite-row';
const sheetOptions = spriteSheetCatalog
.map(s => `<option value="${s.id}" data-search="${(s.name + ' ' + s.cdnPath).toLowerCase()}">${s.name} (${s.cdnPath})</option>`)
.join('');
row.innerHTML = `
<select name="sprite_key[]" class="input md:col-span-2">
<option value="idle_front">idle_front</option>
<option value="idle_back">idle_back</option>
<option value="idle_left">idle_left</option>
<option value="idle_right">idle_right</option>
<option value="run_front">run_front</option>
<option value="run_back">run_back</option>
<option value="run_left">run_left</option>
<option value="run_right">run_right</option>
<option value="attack_front">attack_front</option>
<option value="attack_back">attack_back</option>
<option value="death">death</option>
</select>
<select name="sprite_sheet_id[]" class="input md:col-span-5 sprite-sheet-select"><option value="">Select sheet...</option>${sheetOptions}</select>
<input type="number" name="sprite_frames[]" value="8" class="input md:col-span-2" min="1" placeholder="Frames" />
<label class="md:col-span-2 flex items-center gap-2 text-sm text-gray-300">
<input type="checkbox" name="sprite_loop[]" value="${idx}" checked class="rounded border-gray-600 bg-gray-800" />
Loop
</label>
<button type="button" class="btn btn-danger md:col-span-1" onclick="this.closest('.sprite-row').remove()">X</button>
`;
document.getElementById('spriteRows').appendChild(row);
filterSpriteSheets();
function updateOverride(idx, value) {
selectedAnimations[idx].overrideKey = (value || '').trim() || null;
renderAnimationTable();
}
document.addEventListener('keydown', e => {
if (e.key === 'Escape') {
closeAddAnimationModal();
}
});
renderSelectedAbilities();
renderAnimationTable();
</script>
}