From 13d29f811297a0b472bd0cdbf9de98efb2429b2b Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 14 Feb 2026 16:57:49 -0500 Subject: [PATCH] Add world editor chunk clear/delete, zoom, sprite tile rendering, and tile-definition-driven paint --- Services/Admin/Pages/WorldEditor/Index.cshtml | 160 ++++++++++++++++-- .../Admin/Pages/WorldEditor/Index.cshtml.cs | 79 ++++++++- 2 files changed, 226 insertions(+), 13 deletions(-) diff --git a/Services/Admin/Pages/WorldEditor/Index.cshtml b/Services/Admin/Pages/WorldEditor/Index.cshtml index acf2b05..ed8a731 100644 --- a/Services/Admin/Pages/WorldEditor/Index.cshtml +++ b/Services/Admin/Pages/WorldEditor/Index.cshtml @@ -30,6 +30,15 @@ var objectsJson = JsonSerializer.Serialize(Model.ObjectDefinitions.Select(o => new { id = o.Id, name = o.Name })); var npcsJson = JsonSerializer.Serialize(Model.NpcDefinitions.Select(n => new { id = n.Id, name = n.Name })); + var tileDefsJson = JsonSerializer.Serialize(Model.TileDefinitions.Select(t => new + { + id = t.Id, + sheetId = t.TileMapSheetId, + frameIndex = t.FrameIndex, + name = t.Name, + type = t.Type.ToString(), + isWalkable = t.IsWalkable + })); }
@@ -74,6 +83,10 @@
+
+ + +
@@ -155,7 +168,13 @@

Chunk Canvas

-
No chunk loaded
+
+ + + + +
No chunk loaded
+
@@ -169,6 +188,7 @@ const sheets = @Html.Raw(sheetsJson); const objectDefs = @Html.Raw(objectsJson); const npcDefs = @Html.Raw(npcsJson); + const tileDefs = @Html.Raw(tileDefsJson); const cdnBaseUrl = '@Model.CdnBaseUrl'; let currentRegion = null; @@ -178,6 +198,9 @@ let currentObjects = []; let currentNpcs = []; let eventSource = null; + let chunkZoom = 1; + const tileDefBySprite = {}; + const sheetImageCache = new Map(); const regionCanvas = document.getElementById('regionCanvas'); const regionCtx = regionCanvas.getContext('2d'); @@ -198,6 +221,10 @@ function key(x, y) { return `${x},${y}`; } function initSelectors() { + for (const def of tileDefs) { + tileDefBySprite[`${def.sheetId}:${def.frameIndex}`] = def; + } + const regionSel = document.getElementById('regionId'); regionSel.innerHTML = regions.map(r => ``).join(''); regionSel.value = '@Model.SelectedRegionId'; @@ -272,6 +299,7 @@ currentChunk = data.chunk; currentTiles = new Map(); for (const t of data.tiles) currentTiles.set(key(t.x, t.y), t); + for (const t of data.tiles) preloadSheetImage(t.sheetId); currentObjects = data.objects || []; currentNpcs = data.npcs || []; document.getElementById('chunkMeta').textContent = `Chunk ${s.zoneX}:${s.zoneY}:${s.chunkX}:${s.chunkY} v${currentChunk.version} (${currentChunk.width}x${currentChunk.height})`; @@ -333,6 +361,19 @@ else currentNpcs.push({ ...op }); } else if (op.type === 'npc_remove') { currentNpcs = currentNpcs.filter(n => n.id !== op.id); + } else if (op.type === 'chunk_clear') { + currentTiles.clear(); + currentObjects = []; + currentNpcs = []; + } else if (op.type === 'chunk_deleted') { + currentChunk = null; + currentTiles.clear(); + currentObjects = []; + currentNpcs = []; + chunkCtx.clearRect(0, 0, chunkCanvas.width, chunkCanvas.height); + document.getElementById('chunkMeta').textContent = 'Chunk deleted'; + loadZone(); + return; } else { loadChunk(); return; @@ -400,7 +441,8 @@ chunkCtx.clearRect(0, 0, chunkCanvas.width, chunkCanvas.height); if (!currentChunk) return; - const tileSize = Math.max(6, Math.floor(Math.min(chunkCanvas.width / currentChunk.width, chunkCanvas.height / currentChunk.height))); + const baseTileSize = Math.max(6, Math.floor(Math.min(chunkCanvas.width / currentChunk.width, chunkCanvas.height / currentChunk.height))); + const tileSize = Math.max(2, Math.floor(baseTileSize * chunkZoom)); const drawW = currentChunk.width * tileSize; const drawH = currentChunk.height * tileSize; chunkCtx.fillStyle = '#111827'; @@ -409,14 +451,32 @@ for (const t of currentTiles.values()) { const px = t.x * tileSize; const py = t.y * tileSize; - if (t.isInvisiblePaywall) { - chunkCtx.fillStyle = '#7c2d12'; - } else if (t.isWalkable) { - chunkCtx.fillStyle = '#1d4ed8'; + const sheet = sheets.find(s => s.id === t.sheetId); + const img = sheet ? sheetImageCache.get(sheet.id) : null; + const canDrawSprite = !!sheet && !!img && img.complete && sheet.tileW > 0 && sheet.tileH > 0 && sheet.cols > 0; + + if (canDrawSprite) { + const col = t.frameIndex % sheet.cols; + const row = Math.floor(t.frameIndex / sheet.cols); + const sx = col * sheet.tileW; + const sy = row * sheet.tileH; + chunkCtx.imageSmoothingEnabled = false; + chunkCtx.drawImage(img, sx, sy, sheet.tileW, sheet.tileH, px, py, tileSize, tileSize); } else { - chunkCtx.fillStyle = '#334155'; + if (t.isInvisiblePaywall) { + chunkCtx.fillStyle = '#7c2d12'; + } else if (t.isWalkable) { + chunkCtx.fillStyle = '#1d4ed8'; + } else { + chunkCtx.fillStyle = '#334155'; + } + chunkCtx.fillRect(px, py, tileSize, tileSize); + } + + if (t.isInvisiblePaywall) { + chunkCtx.fillStyle = 'rgba(127,29,29,0.45)'; + chunkCtx.fillRect(px, py, tileSize, tileSize); } - chunkCtx.fillRect(px, py, tileSize, tileSize); } chunkCtx.strokeStyle = 'rgba(148,163,184,0.16)'; @@ -462,7 +522,8 @@ const scaleY = chunkCanvas.height / rect.height; const cx = (e.clientX - rect.left) * scaleX; const cy = (e.clientY - rect.top) * scaleY; - const tileSize = Math.max(6, Math.floor(Math.min(chunkCanvas.width / currentChunk.width, chunkCanvas.height / currentChunk.height))); + const baseTileSize = Math.max(6, Math.floor(Math.min(chunkCanvas.width / currentChunk.width, chunkCanvas.height / currentChunk.height))); + const tileSize = Math.max(2, Math.floor(baseTileSize * chunkZoom)); const x = Math.floor(cx / tileSize); const y = Math.floor(cy / tileSize); if (x < 0 || y < 0 || x >= currentChunk.width || y >= currentChunk.height) return null; @@ -478,7 +539,8 @@ if (mode === 'tile') { const sheetId = parseInt(document.getElementById('sheetId').value, 10) || 0; const frameIndex = parseInt(document.getElementById('frameIndex').value, 10) || 0; - const isWalkable = document.getElementById('isWalkable').checked; + const def = tileDefBySprite[`${sheetId}:${frameIndex}`]; + const isWalkable = def ? !!def.isWalkable : document.getElementById('isWalkable').checked; const isInvisiblePaywall = document.getElementById('isInvisiblePaywall').checked; const data = await postJson('/admin/worldeditor?handler=UpsertTile', { ...s, @@ -492,7 +554,12 @@ if (data.success) { currentChunk.version = data.chunkVersion; currentTiles.set(key(pos.x, pos.y), { - x: pos.x, y: pos.y, sheetId, frameIndex, isWalkable, isInvisiblePaywall + x: pos.x, + y: pos.y, + sheetId, + frameIndex, + isWalkable: data.op?.isWalkable ?? isWalkable, + isInvisiblePaywall }); } } else if (mode === 'object') { @@ -566,9 +633,21 @@ frameIndex = Math.max(0, Math.min(totalFrames - 1, frameIndex)); document.getElementById('frameIndex').value = frameIndex; document.getElementById('tileDefLink').href = `/admin/tilemaps/tiles/edit?sheetId=${sheetId}&frameIndex=${frameIndex}`; + applySelectedTileDefinition(); + preloadSheetImage(sheetId); renderSheetPicker(); } + function applySelectedTileDefinition() { + const sheetId = parseInt(document.getElementById('sheetId').value, 10) || 0; + const frameIndex = parseInt(document.getElementById('frameIndex').value, 10) || 0; + const def = tileDefBySprite[`${sheetId}:${frameIndex}`]; + const walkableBox = document.getElementById('isWalkable'); + if (def) { + walkableBox.checked = !!def.isWalkable; + } + } + function resolveSheetImageUrl(sheet) { if (!sheet || !sheet.path) return ''; if (sheet.path.startsWith('http://') || sheet.path.startsWith('https://')) return sheet.path; @@ -644,6 +723,65 @@ sheetPickerImage.src = imageUrl; } + function preloadSheetImage(sheetId) { + if (!sheetId || sheetImageCache.has(sheetId)) return; + const sheet = sheets.find(s => s.id === sheetId); + if (!sheet) return; + const url = resolveSheetImageUrl(sheet); + if (!url) return; + const img = new Image(); + img.onload = () => renderChunk(); + img.src = url; + sheetImageCache.set(sheetId, img); + } + + function onZoomChanged() { + const value = parseFloat(document.getElementById('zoomLevel').value); + chunkZoom = Number.isFinite(value) ? Math.max(0.5, Math.min(4, value)) : 1; + renderChunk(); + } + + function adjustZoom(delta) { + const slider = document.getElementById('zoomLevel'); + const current = parseFloat(slider.value) || 1; + const next = Math.max(0.5, Math.min(4, Math.round((current + delta) * 4) / 4)); + slider.value = String(next); + onZoomChanged(); + } + + function resetZoom() { + document.getElementById('zoomLevel').value = '1'; + onZoomChanged(); + } + + async function clearChunk() { + if (!currentChunk) return; + const s = selection(); + const data = await postJson('/admin/worldeditor?handler=ClearChunk', s); + if (!data.success) return; + currentChunk.version = data.chunkVersion; + currentTiles.clear(); + currentObjects = []; + currentNpcs = []; + renderChunk(); + loadZone(); + } + + async function deleteChunk() { + if (!currentChunk) return; + if (!confirm('Delete this chunk and all contained tiles/objects/NPC spawns?')) return; + const s = selection(); + const data = await postJson('/admin/worldeditor?handler=DeleteChunk', s); + if (!data.success) return; + currentChunk = null; + currentTiles.clear(); + currentObjects = []; + currentNpcs = []; + chunkCtx.clearRect(0, 0, chunkCanvas.width, chunkCanvas.height); + document.getElementById('chunkMeta').textContent = 'Chunk deleted'; + loadZone(); + } + document.getElementById('frameIndex').addEventListener('input', onSheetChanged); sheetPickerCanvas.addEventListener('click', (e) => { const sheetId = parseInt(document.getElementById('sheetId').value, 10) || 0; diff --git a/Services/Admin/Pages/WorldEditor/Index.cshtml.cs b/Services/Admin/Pages/WorldEditor/Index.cshtml.cs index be0404d..b0f2182 100644 --- a/Services/Admin/Pages/WorldEditor/Index.cshtml.cs +++ b/Services/Admin/Pages/WorldEditor/Index.cshtml.cs @@ -23,6 +23,7 @@ public class IndexModel : AdminPageModel public List Regions { get; set; } = new(); public List TileSheets { get; set; } = new(); + public List TileDefinitions { get; set; } = new(); public List ObjectDefinitions { get; set; } = new(); public List NpcDefinitions { get; set; } = new(); @@ -58,6 +59,20 @@ public class IndexModel : AdminPageModel .OrderBy(s => s.Name) .ToListAsync(); + var enabledSheetIds = TileSheets.Select(s => s.Id).ToList(); + TileDefinitions = await _db.TileDefinitions + .Where(t => t.IsEnabled && enabledSheetIds.Contains(t.TileMapSheetId)) + .Select(t => new TileDefinition + { + Id = t.Id, + TileMapSheetId = t.TileMapSheetId, + FrameIndex = t.FrameIndex, + Name = t.Name, + Type = t.Type, + IsWalkable = t.IsWalkable + }) + .ToListAsync(); + ObjectDefinitions = await _db.DungeonObjects .Where(o => o.IsEnabled) .OrderBy(o => o.Name) @@ -325,7 +340,11 @@ public class IndexModel : AdminPageModel tile.SheetId = sheetId; tile.FrameIndex = frameIndex; - tile.IsWalkable = isWalkable; + var tileDef = await _db.TileDefinitions + .AsNoTracking() + .FirstOrDefaultAsync(t => t.IsEnabled && t.TileMapSheetId == sheetId && t.FrameIndex == frameIndex); + var effectiveWalkable = tileDef?.IsWalkable ?? isWalkable; + tile.IsWalkable = effectiveWalkable; tile.IsInvisiblePaywall = isInvisiblePaywall; var fromVersion = locate.Chunk.Version; @@ -340,7 +359,7 @@ public class IndexModel : AdminPageModel y, sheetId, frameIndex, - isWalkable, + isWalkable = effectiveWalkable, isInvisiblePaywall }; PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op); @@ -355,6 +374,62 @@ public class IndexModel : AdminPageModel }); } + public async Task OnPostClearChunkAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY) + { + var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY); + if (!locate.Success || locate.Chunk == null) + { + return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 }; + } + + var fromVersion = locate.Chunk.Version; + + var tiles = _db.WorldChunkTiles.Where(t => t.WorldChunkId == locate.Chunk.Id); + var objects = _db.WorldChunkObjects.Where(o => o.WorldChunkId == locate.Chunk.Id); + var npcs = _db.WorldChunkNpcSpawns.Where(n => n.WorldChunkId == locate.Chunk.Id); + + _db.WorldChunkTiles.RemoveRange(tiles); + _db.WorldChunkObjects.RemoveRange(objects); + _db.WorldChunkNpcSpawns.RemoveRange(npcs); + + locate.Chunk.Version++; + locate.Chunk.UpdatedAt = DateTime.UtcNow; + await _db.SaveChangesAsync(); + + var op = new { type = "chunk_clear" }; + PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op); + + return new JsonResult(new + { + success = true, + chunkVersion = locate.Chunk.Version, + fromVersion, + toVersion = locate.Chunk.Version, + op + }); + } + + public async Task OnPostDeleteChunkAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY) + { + var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY); + if (!locate.Success || locate.Chunk == null) + { + return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 }; + } + + var fromVersion = locate.Chunk.Version; + _db.WorldChunks.Remove(locate.Chunk); + await _db.SaveChangesAsync(); + + PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, fromVersion + 1, new { type = "chunk_deleted" }); + + return new JsonResult(new + { + success = true, + op = new { type = "chunk_deleted" } + }); + } + public async Task OnPostRemoveTileAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, int x, int y) { var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);