From 40d8570550a16c3705f7fa7b1f8985b700ce3a28 Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 14 Feb 2026 02:06:51 -0500 Subject: [PATCH] Switch NPC editor to mob-style animation assignment selection --- Services/Admin/Pages/Npcs/Edit.cshtml | 290 ++++++++++++++--------- Services/Admin/Pages/Npcs/Edit.cshtml.cs | 261 ++++++++++++-------- 2 files changed, 347 insertions(+), 204 deletions(-) diff --git a/Services/Admin/Pages/Npcs/Edit.cshtml b/Services/Admin/Pages/Npcs/Edit.cshtml index 56c91c9..f066be9 100644 --- a/Services/Admin/Pages/Npcs/Edit.cshtml +++ b/Services/Admin/Pages/Npcs/Edit.cshtml @@ -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 => $"")); } @@ -146,7 +161,7 @@
-
+

Damage Resistances

@@ -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"; -
+
+ + +
- - -
-
- - + +
- -
-
- - -
+
+ +
- -
- @for (var i = 0; i < Model.SpriteRows.Count; i++) - { - var row = Model.SpriteRows[i]; -
- - - - - -
- } -
- -
@@ -343,8 +349,9 @@ @section Scripts { } diff --git a/Services/Admin/Pages/Npcs/Edit.cshtml.cs b/Services/Admin/Pages/Npcs/Edit.cshtml.cs index 2b3cf0b..c5c51f6 100644 --- a/Services/Admin/Pages/Npcs/Edit.cshtml.cs +++ b/Services/Admin/Pages/Npcs/Edit.cshtml.cs @@ -24,36 +24,19 @@ public class EditModel : AdminPageModel public List DialogTrees { get; set; } = new(); public List ResistanceTypes { get; set; } = new(); public List AllAbilities { get; set; } = new(); - public List AllSpriteSheets { get; set; } = new(); + public List AllAnimations { get; set; } = new(); public List SelectedAbilityIds { get; set; } = new(); - public List SpriteRows { get; set; } = new(); - public NpcSpriteConfig SpriteConfig { get; set; } = new(); + public List SelectedAnimations { get; set; } = new(); public bool IsNew => Npc.Id == 0; public string? Message { get; set; } public bool IsError { get; set; } - public class NpcSpriteRow + public class NpcAnimationSelection { - public string Key { get; set; } = ""; - public int? SheetId { get; set; } - public int Frames { get; set; } = 8; - public bool Loop { get; set; } = true; - } - - public class NpcSpriteConfig - { - public Dictionary Sprites { get; set; } = new(); - public Dictionary Animations { get; set; } = new(); - public int FrameWidth { get; set; } = 64; - public int FrameHeight { get; set; } = 64; - public int Fps { get; set; } = 8; - - public class SpriteAnimation - { - public int Frames { get; set; } = 8; - public bool Loop { get; set; } = true; - } + public int AnimationDefinitionId { get; set; } + public string? OverrideKey { get; set; } + public bool IsEnabled { get; set; } = true; } public async Task OnGetAsync(int? id) @@ -116,9 +99,10 @@ public class EditModel : AdminPageModel Npc.Key = (Npc.Key ?? "").Trim(); Npc.Description ??= ""; Npc.TintColor = string.IsNullOrWhiteSpace(Npc.TintColor) ? "#ffffff" : Npc.TintColor.Trim(); + ParseAndSetResistancesFromForm(); ParseAndSetAbilityIdsFromForm(); - ParseAndSetSpriteConfigFromForm(); + ParseAndSetAnimationsFromForm(); if (string.IsNullOrWhiteSpace(Npc.Name)) { @@ -130,9 +114,7 @@ public class EditModel : AdminPageModel if (string.IsNullOrWhiteSpace(Npc.Key)) { - Npc.Key = Npc.Name - .ToLowerInvariant() - .Replace(" ", "_"); + Npc.Key = Npc.Name.ToLowerInvariant().Replace(" ", "_"); } try @@ -228,9 +210,12 @@ public class EditModel : AdminPageModel .OrderBy(a => a.Name) .ToListAsync(); - AllSpriteSheets = await _db.MobSpriteSheets - .Where(s => s.IsEnabled) - .OrderBy(s => s.Name) + AllAnimations = await _db.MobAnimationDefinitions + .Include(a => a.SpriteSheet) + .Where(a => a.IsEnabled) + .Where(a => a.SpriteSheet != null && a.SpriteSheet.IsEnabled) + .OrderBy(a => a.SpriteSheet!.Name) + .ThenBy(a => a.Key) .ToListAsync(); } @@ -256,42 +241,81 @@ public class EditModel : AdminPageModel Npc.AbilityDefinitionIdsJson = "[]"; } + SelectedAnimations = ParseAnimationsFromSpriteConfig(Npc.SpriteConfigJson); + } + + private List ParseAnimationsFromSpriteConfig(string? spriteConfigJson) + { + var selected = new List(); + if (string.IsNullOrWhiteSpace(spriteConfigJson)) return selected; + try { - SpriteConfig = string.IsNullOrWhiteSpace(Npc.SpriteConfigJson) - ? new NpcSpriteConfig() - : (JsonSerializer.Deserialize(Npc.SpriteConfigJson!, new JsonSerializerOptions + using var doc = JsonDocument.Parse(spriteConfigJson); + var root = doc.RootElement; + + if (!root.TryGetProperty("sheets", out var sheetsElem) || !root.TryGetProperty("animations", out var animationsElem)) + return selected; + + foreach (var animProp in animationsElem.EnumerateObject()) + { + var animKey = animProp.Name; + var animObj = animProp.Value; + if (!animObj.TryGetProperty("sheet", out var sheetRefElem)) continue; + var sheetRef = sheetRefElem.GetString(); + if (string.IsNullOrWhiteSpace(sheetRef)) continue; + if (!sheetsElem.TryGetProperty(sheetRef, out var sheetObj)) continue; + if (!sheetObj.TryGetProperty("path", out var pathElem)) continue; + var path = pathElem.GetString(); + if (string.IsNullOrWhiteSpace(path)) continue; + + var frames = animObj.TryGetProperty("frames", out var framesElem) + ? framesElem.EnumerateArray().Select(x => x.GetInt32()).ToArray() + : Array.Empty(); + var fps = animObj.TryGetProperty("fps", out var fpsElem) ? fpsElem.GetSingle() : 8f; + var loop = animObj.TryGetProperty("loop", out var loopElem) && loopElem.GetBoolean(); + var flipX = animObj.TryGetProperty("flipX", out var flipXElem) && flipXElem.GetBoolean(); + var flipY = animObj.TryGetProperty("flipY", out var flipYElem) && flipYElem.GetBoolean(); + + var match = AllAnimations.FirstOrDefault(a => { - PropertyNameCaseInsensitive = true - }) ?? new NpcSpriteConfig()); + var sheet = a.SpriteSheet; + if (sheet == null) return false; + if (!string.Equals(sheet.CdnPath, path, StringComparison.OrdinalIgnoreCase)) return false; + if (Math.Abs(a.Fps - fps) > 0.01f) return false; + if (a.Loop != loop || a.FlipX != flipX || a.FlipY != flipY) return false; + var aFrames = ParseFrameSequence(a.FrameSequence); + return aFrames.SequenceEqual(frames); + }); + + if (match == null) + { + match = AllAnimations.FirstOrDefault(a => + a.SpriteSheet != null && + string.Equals(a.Key, animKey, StringComparison.OrdinalIgnoreCase) && + string.Equals(a.SpriteSheet.CdnPath, path, StringComparison.OrdinalIgnoreCase)); + } + + if (match != null) + { + selected.Add(new NpcAnimationSelection + { + AnimationDefinitionId = match.Id, + OverrideKey = string.Equals(animKey, match.Key, StringComparison.OrdinalIgnoreCase) ? null : animKey, + IsEnabled = true + }); + } + } } catch { - SpriteConfig = new NpcSpriteConfig(); + // Keep empty if invalid JSON } - SpriteRows = new List(); - foreach (var kv in SpriteConfig.Sprites) - { - var frames = 8; - var loop = true; - if (SpriteConfig.Animations.TryGetValue(kv.Key, out var anim)) - { - frames = anim.Frames; - loop = anim.Loop; - } - - var matchingSheet = AllSpriteSheets.FirstOrDefault(s => - string.Equals(s.CdnPath, kv.Value, StringComparison.OrdinalIgnoreCase)); - - SpriteRows.Add(new NpcSpriteRow - { - Key = kv.Key, - SheetId = matchingSheet?.Id, - Frames = frames, - Loop = loop - }); - } + return selected + .GroupBy(x => new { x.AnimationDefinitionId, Key = (x.OverrideKey ?? "").Trim().ToLowerInvariant() }) + .Select(g => g.First()) + .ToList(); } private void ParseAndSetResistancesFromForm() @@ -323,56 +347,101 @@ public class EditModel : AdminPageModel Npc.AbilityDefinitionIdsJson = JsonSerializer.Serialize(list); } - private void ParseAndSetSpriteConfigFromForm() + private void ParseAndSetAnimationsFromForm() { - var spriteKeys = Request.Form["sprite_key[]"].ToArray(); - var spriteSheetIds = Request.Form["sprite_sheet_id[]"].ToArray(); - var spriteFrames = Request.Form["sprite_frames[]"].ToArray(); - var spriteLoops = Request.Form["sprite_loop[]"].ToArray(); + var ids = Request.Form["npc_anim_id[]"].ToArray(); + var overrides = Request.Form["npc_anim_override_key[]"].ToArray(); + var enabledValues = Request.Form["npc_anim_enabled[]"].ToHashSet(StringComparer.OrdinalIgnoreCase); - var config = new NpcSpriteConfig(); - if (int.TryParse(Request.Form["sprite_frameWidth"], out var fw)) config.FrameWidth = fw; - if (int.TryParse(Request.Form["sprite_frameHeight"], out var fh)) config.FrameHeight = fh; - if (int.TryParse(Request.Form["sprite_fps"], out var fps)) config.Fps = fps; - - for (var i = 0; i < spriteKeys.Length && i < spriteSheetIds.Length; i++) + var selected = new List(); + for (var i = 0; i < ids.Length; i++) { - var key = (spriteKeys[i] ?? "").Trim(); - if (string.IsNullOrWhiteSpace(key)) continue; - if (!int.TryParse(spriteSheetIds[i], out var sheetId)) continue; - var sheet = AllSpriteSheets.FirstOrDefault(s => s.Id == sheetId); - if (sheet == null) continue; + if (!int.TryParse(ids[i], out var animId) || animId <= 0) continue; + var isEnabled = enabledValues.Contains(i.ToString()); + var ov = i < overrides.Length ? overrides[i]?.Trim() : null; + if (string.IsNullOrWhiteSpace(ov)) ov = null; - var frames = 8; - if (i < spriteFrames.Length && int.TryParse(spriteFrames[i], out var rowFrames)) + selected.Add(new NpcAnimationSelection { - frames = Math.Max(1, rowFrames); - } - - var loop = spriteLoops.Contains(i.ToString()); - config.Sprites[key] = sheet.CdnPath; - config.Animations[key] = new NpcSpriteConfig.SpriteAnimation - { - Frames = frames, - Loop = loop - }; + AnimationDefinitionId = animId, + OverrideKey = ov, + IsEnabled = isEnabled + }); } - if (config.Sprites.Count == 0) + selected = selected + .GroupBy(x => new { x.AnimationDefinitionId, Key = (x.OverrideKey ?? "").Trim().ToLowerInvariant() }) + .Select(g => g.First()) + .ToList(); + + SelectedAnimations = selected; + + var enabled = selected.Where(s => s.IsEnabled).ToList(); + if (enabled.Count == 0) { - SpriteConfig = new NpcSpriteConfig(); - SpriteRows = new List(); Npc.SpriteConfigJson = null; return; } - SpriteConfig = config; - Npc.SpriteConfigJson = JsonSerializer.Serialize(config, new JsonSerializerOptions - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }); + var byId = AllAnimations.Where(a => a.SpriteSheet != null).ToDictionary(a => a.Id, a => a); - // rebuild display rows for round-trip on validation failure - ParseNpcJsonFields(); + var sheets = new Dictionary(); + var sheetKeyById = new Dictionary(); + var animations = new Dictionary(); + + foreach (var sel in enabled) + { + if (!byId.TryGetValue(sel.AnimationDefinitionId, out var anim)) continue; + var sheet = anim.SpriteSheet; + if (sheet == null) continue; + + if (!sheetKeyById.TryGetValue(sheet.Id, out var sheetKey)) + { + sheetKey = $"sheet_{sheet.Id}"; + sheetKeyById[sheet.Id] = sheetKey; + sheets[sheetKey] = new + { + path = sheet.CdnPath, + width = sheet.SheetWidth, + height = sheet.SheetHeight, + frameWidth = sheet.FrameWidth, + frameHeight = sheet.FrameHeight + }; + } + + var key = !string.IsNullOrWhiteSpace(sel.OverrideKey) ? sel.OverrideKey! : anim.Key; + animations[key] = new + { + sheet = sheetKey, + frames = ParseFrameSequence(anim.FrameSequence), + fps = anim.Fps, + loop = anim.Loop, + flipX = anim.FlipX, + flipY = anim.FlipY + }; + } + + if (animations.Count == 0) + { + Npc.SpriteConfigJson = null; + return; + } + + Npc.SpriteConfigJson = JsonSerializer.Serialize(new + { + sheets, + animations + }); + } + + private static int[] ParseFrameSequence(string frameSequence) + { + if (string.IsNullOrWhiteSpace(frameSequence)) + return new[] { 0 }; + + return frameSequence + .Split(',') + .Select(s => int.TryParse(s.Trim(), out var n) ? n : 0) + .ToArray(); } }