Admin tools for mob sprites

This commit is contained in:
dingo
2025-12-27 20:12:40 -05:00
parent 99472f226a
commit c16dc1259d
12 changed files with 3536 additions and 2 deletions
@@ -0,0 +1,292 @@
@page "/admin/spritesheets/edit"
@model SpriteSheets.EditModel
@{
ViewData["Title"] = Model.Sheet.Id > 0 ? $"Edit: {Model.Sheet.Name}" : "New Sprite Sheet";
var isNew = Model.Sheet.Id == 0;
}
<div class="space-y-6">
<div class="flex items-center justify-between">
<div>
<a href="/admin/spritesheets" class="text-gray-400 hover:text-white text-sm">&larr; Back to Sprite Sheets</a>
<h1 class="text-3xl font-bold text-white mt-2">@ViewData["Title"]</h1>
</div>
@if (!isNew)
{
<form method="post" asp-page-handler="Duplicate">
@Html.AntiForgeryToken()
<input type="hidden" name="id" value="@Model.Sheet.Id" />
<button type="submit" class="btn btn-secondary">Duplicate</button>
</form>
}
</div>
@if (!string.IsNullOrEmpty(Model.Message))
{
<div class="p-4 rounded-lg @(Model.IsError ? "bg-red-900/50 border border-red-700 text-red-300" : "bg-green-900/50 border border-green-700 text-green-300")">
@Model.Message
</div>
}
@if (TempData["Error"] != null)
{
<div class="p-4 rounded-lg bg-red-900/50 border border-red-700 text-red-300">
@TempData["Error"]
</div>
}
<form method="post" class="space-y-6">
@Html.AntiForgeryToken()
<input type="hidden" asp-for="Sheet.Id" />
<input type="hidden" asp-for="Sheet.CreatedAt" />
<!-- Basic Info -->
<div class="card p-6">
<h2 class="text-lg font-semibold text-white mb-4">Sprite Sheet Info</h2>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
<div class="lg:col-span-2">
<label class="label">Name</label>
<input type="text" asp-for="Sheet.Name" class="input" required placeholder="e.g., Skeleton Walk" />
</div>
<div class="lg:col-span-2">
<label class="label">CDN Path</label>
<div class="flex gap-2">
<input type="text" asp-for="Sheet.CdnPath" class="input flex-1" required placeholder="textures/mobs/skeleton.png" />
<button type="button" onclick="showCdnBrowser()" class="btn btn-secondary">Browse</button>
</div>
</div>
<div>
<label class="label">Sheet Width (px)</label>
<input type="number" asp-for="Sheet.SheetWidth" class="input" min="1" />
</div>
<div>
<label class="label">Sheet Height (px)</label>
<input type="number" asp-for="Sheet.SheetHeight" class="input" min="1" />
</div>
<div>
<label class="label">Frame Width (px)</label>
<input type="number" asp-for="Sheet.FrameWidth" class="input" min="1" />
</div>
<div>
<label class="label">Frame Height (px)</label>
<input type="number" asp-for="Sheet.FrameHeight" class="input" min="1" />
</div>
<div class="flex items-center gap-2 pt-6">
<input type="checkbox" asp-for="Sheet.IsEnabled" class="w-4 h-4" />
<label asp-for="Sheet.IsEnabled" class="text-gray-300">Enabled</label>
</div>
</div>
@if (!string.IsNullOrEmpty(Model.Sheet.CdnPath))
{
<div class="mt-4 p-4 bg-gray-800 rounded-lg">
<p class="text-sm text-gray-400 mb-2">Preview (Sheet: @Model.Sheet.SheetWidth x @Model.Sheet.SheetHeight, Frame: @Model.Sheet.FrameWidth x @Model.Sheet.FrameHeight = @Model.Sheet.TotalFrames frames)</p>
<img src="@Model.CdnBaseUrl/@Model.Sheet.CdnPath" alt="Sprite sheet preview" class="max-h-64 object-contain" style="image-rendering: pixelated;" />
</div>
}
</div>
<div class="flex gap-4">
<button type="submit" class="btn btn-primary">Save Sprite Sheet</button>
<a href="/admin/spritesheets" class="btn btn-secondary">Cancel</a>
</div>
</form>
<!-- Animations Section (only for existing sheets) -->
@if (!isNew)
{
<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">Animations (@Model.Animations.Count)</h2>
<button type="button" onclick="showAddAnimModal()" class="btn btn-sm btn-primary">+ Add Animation</button>
</div>
<div class="p-4">
@if (Model.Animations.Count > 0)
{
<div class="space-y-3">
@foreach (var anim in Model.Animations)
{
<div class="bg-gray-800 rounded-lg p-4">
<form method="post" asp-page-handler="UpdateAnimation" class="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-8 gap-3 items-end">
@Html.AntiForgeryToken()
<input type="hidden" name="animId" value="@anim.Id" />
<input type="hidden" name="sheetId" value="@Model.Sheet.Id" />
<div>
<label class="label text-xs">Key</label>
<input type="text" name="key" value="@anim.Key" class="input input-sm" required />
</div>
<div>
<label class="label text-xs">Name</label>
<input type="text" name="name" value="@anim.Name" class="input input-sm" />
</div>
<div class="lg:col-span-2">
<label class="label text-xs">Frames (comma-separated)</label>
<input type="text" name="frameSequence" value="@anim.FrameSequence" class="input input-sm font-mono" required />
</div>
<div>
<label class="label text-xs">FPS</label>
<input type="number" name="fps" value="@anim.Fps" class="input input-sm" step="0.1" min="0.1" />
</div>
<div class="flex items-center gap-4 pt-5">
<label class="flex items-center gap-1 text-xs text-gray-300">
<input type="checkbox" name="loop" value="true" checked="@anim.Loop" class="w-3 h-3" />
Loop
</label>
<label class="flex items-center gap-1 text-xs text-gray-300">
<input type="checkbox" name="flipX" value="true" checked="@anim.FlipX" class="w-3 h-3" />
FlipX
</label>
<label class="flex items-center gap-1 text-xs text-gray-300">
<input type="checkbox" name="flipY" value="true" checked="@anim.FlipY" class="w-3 h-3" />
FlipY
</label>
<label class="flex items-center gap-1 text-xs text-gray-300">
<input type="checkbox" name="isEnabled" value="true" checked="@anim.IsEnabled" class="w-3 h-3" />
On
</label>
</div>
<div class="flex gap-1">
<button type="submit" class="btn btn-sm btn-secondary flex-1">Save</button>
<button type="button" onclick="deleteAnim(@anim.Id)" class="btn btn-sm btn-danger">X</button>
</div>
</form>
</div>
}
</div>
}
else
{
<p class="text-gray-400 text-center py-4">No animations defined. Add animations to use this sprite sheet.</p>
}
<!-- Frame Reference -->
<div class="mt-4 p-4 bg-gray-900 rounded-lg">
<p class="text-sm text-gray-400 mb-2">Frame Reference (frames are numbered 0 to @(Model.Sheet.TotalFrames - 1), left-to-right, top-to-bottom)</p>
<div class="grid gap-1" style="grid-template-columns: repeat(@Model.Sheet.Columns, minmax(0, 1fr)); max-width: fit-content;">
@for (int i = 0; i < Model.Sheet.TotalFrames; i++)
{
<div class="w-8 h-8 bg-gray-700 rounded text-xs flex items-center justify-center text-gray-300">@i</div>
}
</div>
</div>
</div>
</div>
<!-- Add Animation Modal -->
<div id="addAnimModal" 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-lg mx-4 border border-gray-700">
<h3 class="text-lg font-semibold text-white mb-4">Add Animation</h3>
<form method="post" asp-page-handler="AddAnimation">
@Html.AntiForgeryToken()
<input type="hidden" name="sheetId" value="@Model.Sheet.Id" />
<div class="space-y-4">
<div class="grid grid-cols-2 gap-4">
<div>
<label class="label">Key (e.g., walk_down)</label>
<input type="text" name="key" class="input" required placeholder="idle_down" />
</div>
<div>
<label class="label">Name (optional)</label>
<input type="text" name="name" class="input" placeholder="Idle Down" />
</div>
</div>
<div>
<label class="label">Frame Sequence (comma-separated)</label>
<input type="text" name="frameSequence" class="input font-mono" required placeholder="0,1,2,3" />
<p class="text-xs text-gray-500 mt-1">e.g., "0,1,2,3" or "4,5,6,7,6,5" for ping-pong</p>
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="label">FPS</label>
<input type="number" name="fps" value="8" class="input" step="0.1" min="0.1" />
</div>
<div class="flex items-center gap-4 pt-6">
<label class="flex items-center gap-2 text-gray-300">
<input type="checkbox" name="loop" value="true" checked class="w-4 h-4" />
Loop
</label>
<label class="flex items-center gap-2 text-gray-300">
<input type="checkbox" name="flipX" value="true" class="w-4 h-4" />
Flip X
</label>
<label class="flex items-center gap-2 text-gray-300">
<input type="checkbox" name="flipY" value="true" class="w-4 h-4" />
Flip Y
</label>
</div>
</div>
</div>
<div class="flex justify-end gap-3 pt-4">
<button type="button" onclick="closeAddAnimModal()" class="btn btn-secondary">Cancel</button>
<button type="submit" class="btn btn-primary">Add Animation</button>
</div>
</form>
</div>
</div>
<!-- Delete Animation Form (hidden) -->
<form id="deleteAnimForm" method="post" asp-page-handler="DeleteAnimation" class="hidden">
@Html.AntiForgeryToken()
<input type="hidden" name="animId" id="deleteAnimId" value="" />
<input type="hidden" name="sheetId" value="@Model.Sheet.Id" />
</form>
}
<!-- CDN Browser Modal -->
<div id="cdnBrowserModal" 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-2xl mx-4 border border-gray-700 max-h-[80vh] overflow-hidden flex flex-col">
<h3 class="text-lg font-semibold text-white mb-4">Select Image from CDN</h3>
<div class="flex-1 overflow-y-auto grid grid-cols-2 md:grid-cols-3 gap-3">
@foreach (var asset in Model.CdnImages)
{
<button type="button" onclick="selectCdnImage('@asset.DownloadPath')" class="p-2 bg-gray-800 rounded-lg hover:bg-gray-700 text-left">
<img src="@Model.CdnBaseUrl/@asset.DownloadPath" alt="@asset.FilePath" class="w-full h-20 object-contain mb-1" style="image-rendering: pixelated;" />
<p class="text-xs text-gray-400 truncate">@asset.FilePath</p>
</button>
}
</div>
<div class="flex justify-end pt-4">
<button type="button" onclick="closeCdnBrowser()" class="btn btn-secondary">Cancel</button>
</div>
</div>
</div>
</div>
@section Scripts {
<script>
function showAddAnimModal() {
document.getElementById('addAnimModal').classList.remove('hidden');
document.getElementById('addAnimModal').classList.add('flex');
}
function closeAddAnimModal() {
document.getElementById('addAnimModal').classList.add('hidden');
document.getElementById('addAnimModal').classList.remove('flex');
}
function deleteAnim(id) {
if (confirm('Delete this animation?')) {
document.getElementById('deleteAnimId').value = id;
document.getElementById('deleteAnimForm').submit();
}
}
function showCdnBrowser() {
document.getElementById('cdnBrowserModal').classList.remove('hidden');
document.getElementById('cdnBrowserModal').classList.add('flex');
}
function closeCdnBrowser() {
document.getElementById('cdnBrowserModal').classList.add('hidden');
document.getElementById('cdnBrowserModal').classList.remove('flex');
}
function selectCdnImage(path) {
document.getElementById('Sheet_CdnPath').value = path;
closeCdnBrowser();
}
document.addEventListener('keydown', e => {
if (e.key === 'Escape') {
closeAddAnimModal();
closeCdnBrowser();
}
});
</script>
}
@@ -0,0 +1,226 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DataServer.Data;
using DataServer.Data.Entities;
namespace Server.Services.Admin.Pages.SpriteSheets;
public class EditModel : AdminPageModel
{
private readonly GameDbContext _db;
private readonly IConfiguration _config;
public EditModel(GameDbContext db, IConfiguration config)
{
_db = db;
_config = config;
}
[BindProperty]
public MobSpriteSheet Sheet { get; set; } = new();
public List<MobAnimationDefinition> Animations { get; set; } = new();
public List<ClientAsset> CdnImages { get; set; } = new();
public string CdnBaseUrl { get; set; } = "";
public string? Message { get; set; }
public bool IsError { get; set; }
public async Task OnGetAsync(int? id)
{
CdnBaseUrl = _config["Cdn:BaseUrl"] ?? "https://assets.aeternalisgames.com";
CdnImages = await _db.ClientAssets
.Where(a => a.AssetType == ClientAssetType.Texture && a.IsEnabled)
.OrderBy(a => a.FilePath)
.ToListAsync();
if (id.HasValue)
{
var sheet = await _db.MobSpriteSheets
.Include(s => s.Animations)
.FirstOrDefaultAsync(s => s.Id == id.Value);
if (sheet != null)
{
Sheet = sheet;
Animations = sheet.Animations?.OrderBy(a => a.Key).ToList() ?? new();
}
}
else
{
Sheet = new MobSpriteSheet
{
IsEnabled = true
};
}
}
public async Task<IActionResult> OnPostAsync()
{
CdnBaseUrl = _config["Cdn:BaseUrl"] ?? "https://assets.aeternalisgames.com";
try
{
if (Sheet.Id == 0)
{
Sheet.CreatedAt = DateTime.UtcNow;
Sheet.UpdatedAt = DateTime.UtcNow;
_db.MobSpriteSheets.Add(Sheet);
await _db.SaveChangesAsync();
Message = $"Sprite sheet '{Sheet.Name}' created.";
return RedirectToPage(new { id = Sheet.Id });
}
else
{
var existing = await _db.MobSpriteSheets.FindAsync(Sheet.Id);
if (existing == null)
{
Message = "Sprite sheet not found.";
IsError = true;
return Page();
}
existing.Name = Sheet.Name;
existing.CdnPath = Sheet.CdnPath;
existing.SheetWidth = Sheet.SheetWidth;
existing.SheetHeight = Sheet.SheetHeight;
existing.FrameWidth = Sheet.FrameWidth;
existing.FrameHeight = Sheet.FrameHeight;
existing.IsEnabled = Sheet.IsEnabled;
existing.UpdatedAt = DateTime.UtcNow;
await _db.SaveChangesAsync();
Sheet = existing;
Message = "Sprite sheet updated.";
Animations = await _db.MobAnimationDefinitions
.Where(a => a.SpriteSheetId == Sheet.Id)
.OrderBy(a => a.Key)
.ToListAsync();
}
}
catch (Exception ex)
{
Message = $"Error: {ex.Message}";
IsError = true;
}
CdnImages = await _db.ClientAssets
.Where(a => a.AssetType == ClientAssetType.Texture && a.IsEnabled)
.OrderBy(a => a.FilePath)
.ToListAsync();
return Page();
}
public async Task<IActionResult> OnPostAddAnimationAsync(int sheetId, string key, string? name, string frameSequence, float fps, bool loop, bool flipX, bool flipY)
{
var animation = new MobAnimationDefinition
{
SpriteSheetId = sheetId,
Key = key,
Name = name,
FrameSequence = frameSequence,
Fps = fps,
Loop = loop,
FlipX = flipX,
FlipY = flipY,
IsEnabled = true
};
_db.MobAnimationDefinitions.Add(animation);
await _db.SaveChangesAsync();
return RedirectToPage(new { id = sheetId });
}
public async Task<IActionResult> OnPostUpdateAnimationAsync(int animId, int sheetId, string key, string? name, string frameSequence, float fps, bool loop, bool flipX, bool flipY, bool isEnabled)
{
var animation = await _db.MobAnimationDefinitions.FindAsync(animId);
if (animation != null)
{
animation.Key = key;
animation.Name = name;
animation.FrameSequence = frameSequence;
animation.Fps = fps;
animation.Loop = loop;
animation.FlipX = flipX;
animation.FlipY = flipY;
animation.IsEnabled = isEnabled;
await _db.SaveChangesAsync();
}
return RedirectToPage(new { id = sheetId });
}
public async Task<IActionResult> OnPostDeleteAnimationAsync(int animId, int sheetId)
{
var animation = await _db.MobAnimationDefinitions.FindAsync(animId);
if (animation != null)
{
// Check if assigned to any mobs
var assignmentCount = await _db.MobAnimationAssignments
.Where(a => a.AnimationDefinitionId == animId)
.CountAsync();
if (assignmentCount > 0)
{
TempData["Error"] = $"Cannot delete: animation is assigned to {assignmentCount} mob(s).";
return RedirectToPage(new { id = sheetId });
}
_db.MobAnimationDefinitions.Remove(animation);
await _db.SaveChangesAsync();
}
return RedirectToPage(new { id = sheetId });
}
public async Task<IActionResult> OnPostDuplicateAsync(int id)
{
var original = await _db.MobSpriteSheets
.Include(s => s.Animations)
.FirstOrDefaultAsync(s => s.Id == id);
if (original == null) return RedirectToPage();
var copy = new MobSpriteSheet
{
Name = original.Name + " (Copy)",
CdnPath = original.CdnPath,
SheetWidth = original.SheetWidth,
SheetHeight = original.SheetHeight,
FrameWidth = original.FrameWidth,
FrameHeight = original.FrameHeight,
IsEnabled = true,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
_db.MobSpriteSheets.Add(copy);
await _db.SaveChangesAsync();
// Copy animations
if (original.Animations != null)
{
foreach (var anim in original.Animations)
{
_db.MobAnimationDefinitions.Add(new MobAnimationDefinition
{
SpriteSheetId = copy.Id,
Key = anim.Key,
Name = anim.Name,
FrameSequence = anim.FrameSequence,
Fps = anim.Fps,
Loop = anim.Loop,
FlipX = anim.FlipX,
FlipY = anim.FlipY,
IsEnabled = anim.IsEnabled
});
}
await _db.SaveChangesAsync();
}
return RedirectToPage(new { id = copy.Id });
}
}
@@ -0,0 +1,77 @@
@page "/admin/spritesheets"
@model SpriteSheets.IndexModel
@{
ViewData["Title"] = "Sprite Sheets";
}
<div class="space-y-6">
<div class="flex items-center justify-between">
<div>
<h1 class="text-3xl font-bold text-white">Sprite Sheets</h1>
<p class="text-gray-400 mt-1">Manage sprite sheets and animations for mobs</p>
</div>
<a href="/admin/spritesheets/edit" class="btn btn-primary">+ New Sprite Sheet</a>
</div>
@if (TempData["Message"] != null)
{
<div class="p-4 rounded-lg bg-green-900/50 border border-green-700 text-green-300">
@TempData["Message"]
</div>
}
@if (TempData["Error"] != null)
{
<div class="p-4 rounded-lg bg-red-900/50 border border-red-700 text-red-300">
@TempData["Error"]
</div>
}
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
@foreach (var sheet in Model.SpriteSheets)
{
<div class="card p-4">
<div class="flex items-start justify-between mb-3">
<div>
<h3 class="text-lg font-semibold text-white">@sheet.Name</h3>
<p class="text-sm text-gray-400 font-mono">@sheet.CdnPath</p>
</div>
@if (sheet.IsEnabled)
{
<span class="badge bg-green-600">Enabled</span>
}
else
{
<span class="badge bg-gray-600">Disabled</span>
}
</div>
<div class="grid grid-cols-2 gap-2 text-sm mb-3">
<div class="text-gray-400">Sheet Size:</div>
<div class="text-white">@sheet.SheetWidth x @sheet.SheetHeight</div>
<div class="text-gray-400">Frame Size:</div>
<div class="text-white">@sheet.FrameWidth x @sheet.FrameHeight</div>
<div class="text-gray-400">Grid:</div>
<div class="text-white">@sheet.Columns x @sheet.Rows (@sheet.TotalFrames frames)</div>
<div class="text-gray-400">Animations:</div>
<div class="text-white">@(sheet.Animations?.Count ?? 0)</div>
</div>
<div class="flex gap-2">
<a href="/admin/spritesheets/edit?id=@sheet.Id" class="btn btn-sm btn-secondary flex-1">Edit</a>
<form method="post" asp-page-handler="Delete" class="inline" onsubmit="return confirm('Delete this sprite sheet and all its animations?')">
@Html.AntiForgeryToken()
<input type="hidden" name="id" value="@sheet.Id" />
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</div>
</div>
}
</div>
@if (Model.SpriteSheets.Count == 0)
{
<div class="card p-8 text-center text-gray-400">
No sprite sheets found. <a href="/admin/spritesheets/edit" class="text-indigo-400 hover:underline">Create one</a>
</div>
}
</div>
@@ -0,0 +1,54 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DataServer.Data;
using DataServer.Data.Entities;
namespace Server.Services.Admin.Pages.SpriteSheets;
public class IndexModel : AdminPageModel
{
private readonly GameDbContext _db;
public IndexModel(GameDbContext db)
{
_db = db;
}
public List<MobSpriteSheet> SpriteSheets { get; set; } = new();
public async Task OnGetAsync()
{
SpriteSheets = await _db.MobSpriteSheets
.Include(s => s.Animations)
.OrderBy(s => s.Name)
.ToListAsync();
}
public async Task<IActionResult> OnPostDeleteAsync(int id)
{
var sheet = await _db.MobSpriteSheets.FindAsync(id);
if (sheet != null)
{
// Check for animation assignments
var animationIds = await _db.MobAnimationDefinitions
.Where(a => a.SpriteSheetId == id)
.Select(a => a.Id)
.ToListAsync();
var assignmentCount = await _db.MobAnimationAssignments
.Where(a => animationIds.Contains(a.AnimationDefinitionId))
.CountAsync();
if (assignmentCount > 0)
{
TempData["Error"] = $"Cannot delete: animations from this sheet are assigned to {assignmentCount} mob(s).";
return RedirectToPage();
}
_db.MobSpriteSheets.Remove(sheet);
await _db.SaveChangesAsync();
TempData["Message"] = $"Sprite sheet '{sheet.Name}' deleted.";
}
return RedirectToPage();
}
}