Upgrade NPC admin editor selectors and add dialog tree admin UI

This commit is contained in:
dingo
2026-02-14 01:31:05 -05:00
parent 0e71d64baa
commit 4ba5a1040b
8 changed files with 1157 additions and 29 deletions
+287 -21
View File
@@ -1,8 +1,12 @@
@page "/admin/npcs/edit"
@model Npcs.EditModel
@using System.Text.Json
@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 resistanceOptions = string.Join("", Model.ResistanceTypes.Select(rt => $"<option value=\"{rt.Key}\">{rt.DisplayName}</option>"));
}
<div class="space-y-6 max-w-6xl">
@@ -142,21 +146,150 @@
</div>
<div class="card">
<div class="p-4 border-b border-gray-700">
<h2 class="text-lg font-semibold text-white">Visual & Linked Systems</h2>
<div class="p-4 border-b border-gray-700 flex items-center justify-between">
<h2 class="text-lg font-semibold text-white">Damage Resistances</h2>
</div>
<div class="p-6 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
<div>
<label class="label">Tint Color</label>
<div class="flex gap-2">
<input type="color" value="@Model.Npc.TintColor" class="w-10 h-10 rounded border border-gray-700 bg-gray-800 cursor-pointer" oninput="document.getElementById('tintColor').value=this.value" />
<input id="tintColor" type="text" name="Npc.TintColor" value="@Model.Npc.TintColor" class="input" maxlength="16" />
<div class="p-6">
<div id="resistancesList" class="space-y-2 mb-4">
@{
Dictionary<string, float> resistances = new();
try
{
resistances = JsonSerializer.Deserialize<Dictionary<string, float>>(Model.Npc.ResistancesJson ?? "{}") ?? new();
}
catch { }
}
@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">
<select name="resist_name[]" class="input w-40">
@foreach (var rt in Model.ResistanceTypes)
{
<option value="@rt.Key" selected="@(resist.Key == rt.Key)">@rt.DisplayName</option>
}
</select>
<div class="flex items-center gap-2 flex-1">
<input type="range" name="resist_value[]" value="@resist.Value" min="-1" max="1" step="0.05" class="flex-1" oninput="this.nextElementSibling.value = Math.round(this.value * 100)" />
<div class="flex items-center gap-1">
<input type="number" value="@((resist.Value * 100).ToString("0"))" min="-100" max="100" step="5" class="input w-20 text-center text-sm" oninput="this.parentElement.previousElementSibling.value = this.value / 100" />
<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>
</div>
}
</div>
<div class="flex items-center gap-4 pt-4 border-t border-gray-700">
<select id="newResistType" class="input w-48">
<option value="">Select type...</option>
@foreach (var rt in Model.ResistanceTypes)
{
<option value="@rt.Key">@rt.DisplayName</option>
}
</select>
<input type="number" id="newResistValue" class="input w-24" value="50" min="-100" max="100" step="5" />
<span class="text-gray-400">%</span>
<button type="button" onclick="addResistance()" class="btn btn-secondary">Add</button>
</div>
</div>
</div>
<div class="card">
<div class="p-4 border-b border-gray-700">
<h2 class="text-lg font-semibold text-white">Abilities</h2>
</div>
<div class="p-6 space-y-4">
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="md:col-span-2">
<label class="label">Filter Abilities</label>
<input id="abilitySearch" type="text" class="input" placeholder="Type to filter abilities..." oninput="filterAbilities()" />
</div>
<div>
<label class="label">Ability List</label>
<select id="abilityPicker" class="input" size="8" style="height:auto;">
@foreach (var a in Model.AllAbilities)
{
<option value="@a.Id" data-search="@($"{a.Name} {a.Key} {a.AbilityType}".ToLowerInvariant())">@a.Name (@a.AbilityType)</option>
}
</select>
</div>
</div>
<button type="button" class="btn btn-secondary" onclick="addSelectedAbility()">Add Selected Ability</button>
<div>
<label class="label">Scale</label>
<input type="number" name="Npc.Scale" value="@Model.Npc.Scale" class="input" min="0.1" step="0.1" />
<label class="label">Selected Abilities</label>
<div id="selectedAbilities" class="space-y-2"></div>
</div>
</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>
<div class="p-6 space-y-4">
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label class="label">Frame Width</label>
<input type="number" name="sprite_frameWidth" value="@Model.SpriteConfig.FrameWidth" class="input" min="8" step="8" />
</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" />
</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>
<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>
<div class="card">
<div class="p-4 border-b border-gray-700 flex items-center justify-between">
<h2 class="text-lg font-semibold text-white">Linked Systems</h2>
<a href="/admin/npcs/dialogtrees" class="text-sm text-indigo-400 hover:underline">Manage Dialog Trees</a>
</div>
<div class="p-6 grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label class="label">Loot Table</label>
<select name="Npc.LootTableId" class="input">
@@ -177,7 +310,7 @@
}
</select>
</div>
<div class="lg:col-span-2">
<div>
<label class="label">Dialog Tree</label>
<select name="Npc.DialogTreeId" class="input">
<option value="">None</option>
@@ -187,17 +320,16 @@
}
</select>
</div>
<div class="lg:col-span-4">
<label class="label">Resistances JSON</label>
<textarea name="Npc.ResistancesJson" class="input font-mono text-xs" rows="3">@Model.Npc.ResistancesJson</textarea>
<div>
<label class="label">Tint Color</label>
<div class="flex gap-2">
<input type="color" value="@Model.Npc.TintColor" class="w-10 h-10 rounded border border-gray-700 bg-gray-800 cursor-pointer" oninput="document.getElementById('tintColor').value=this.value" />
<input id="tintColor" type="text" name="Npc.TintColor" value="@Model.Npc.TintColor" class="input" maxlength="16" />
</div>
</div>
<div class="lg:col-span-4">
<label class="label">Ability Definition IDs JSON</label>
<textarea name="Npc.AbilityDefinitionIdsJson" class="input font-mono text-xs" rows="3">@Model.Npc.AbilityDefinitionIdsJson</textarea>
</div>
<div class="lg:col-span-4">
<label class="label">Sprite Config JSON</label>
<textarea name="Npc.SpriteConfigJson" class="input font-mono text-xs" rows="5">@Model.Npc.SpriteConfigJson</textarea>
<div>
<label class="label">Scale</label>
<input type="number" name="Npc.Scale" value="@Model.Npc.Scale" class="input" min="0.1" step="0.1" />
</div>
</div>
</div>
@@ -208,3 +340,137 @@
</div>
</form>
</div>
@section Scripts {
<script>
const selectedAbilitiesSeed = @Html.Raw(selectedAbilitiesJson);
const spriteSheetCatalog = @Html.Raw(spriteSheetOptions);
const resistanceOptionsHtml = `@Html.Raw(resistanceOptions)`;
const abilityById = {
@foreach (var a in Model.AllAbilities)
{
@:@a.Id: "@a.Name (@a.AbilityType)",
}
};
function renderSelectedAbilities() {
const container = document.getElementById('selectedAbilities');
container.innerHTML = '';
selectedAbilitiesSeed.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);
row.innerHTML = `<span class="text-gray-200">${label}</span><div class="flex items-center gap-2"><input type="hidden" name="ability_ids[]" value="${id}"/><button type="button" class="btn btn-danger btn-sm" onclick="removeAbility(${id})">Remove</button></div>`;
container.appendChild(row);
});
}
function removeAbility(id) {
const idx = selectedAbilitiesSeed.indexOf(id);
if (idx >= 0) {
selectedAbilitiesSeed.splice(idx, 1);
renderSelectedAbilities();
}
}
function addSelectedAbility() {
const select = document.getElementById('abilityPicker');
const id = parseInt(select.value || '0', 10);
if (!id) return;
if (!selectedAbilitiesSeed.includes(id)) {
selectedAbilitiesSeed.push(id);
renderSelectedAbilities();
}
}
function filterAbilities() {
const term = (document.getElementById('abilitySearch').value || '').toLowerCase();
const select = document.getElementById('abilityPicker');
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 addResistance() {
const typeSelect = document.getElementById('newResistType');
const valueInput = document.getElementById('newResistValue');
const key = typeSelect.value;
if (!key) return;
const percent = parseInt(valueInput.value || '0', 10);
const value = percent / 100;
const row = document.createElement('div');
row.className = 'bg-gray-800 border border-gray-700 rounded-lg p-3 flex items-center gap-4 resistance-row';
row.innerHTML = `
<select name="resist_name[]" class="input w-40">${resistanceOptionsHtml.replace(`value="${key}"`, `value="${key}" selected`)}</select>
<div class="flex items-center gap-2 flex-1">
<input type="range" name="resist_value[]" value="${value}" min="-1" max="1" step="0.05" class="flex-1" oninput="this.nextElementSibling.value = Math.round(this.value * 100)" />
<div class="flex items-center gap-1">
<input type="number" value="${percent}" min="-100" max="100" step="5" class="input w-20 text-center text-sm" oninput="this.parentElement.previousElementSibling.value = this.value / 100" />
<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>
`;
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 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();
}
renderSelectedAbilities();
</script>
}