diff --git a/src/application/spriteOperations.ts b/src/application/spriteOperations.ts index e1c5cc9..6552d6f 100644 --- a/src/application/spriteOperations.ts +++ b/src/application/spriteOperations.ts @@ -28,12 +28,23 @@ export function addSprites(newSprites: Sprite[]) { return; } + // Log the number of valid sprites being added + logger.info(`Adding ${validSprites.length} valid sprites`); + sprites.value.push(...validSprites); sprites.value.sort((a, b) => a.uploadOrder - b.uploadOrder); // Update cell size before arranging sprites + logger.info('Updating cell size after adding sprites'); updateCellSize(); - autoArrangeSprites(); + + // Only auto-arrange if cell size is valid + if (cellSize && typeof cellSize.width === 'number' && typeof cellSize.height === 'number' && cellSize.width > 0 && cellSize.height > 0) { + logger.info('Auto-arranging sprites'); + autoArrangeSprites(); + } else { + logger.warn('Skipping auto-arrange due to invalid cell size'); + } } catch (error) { logger.error('Error adding sprites:', error); } @@ -44,12 +55,14 @@ export function addSprites(newSprites: Sprite[]) { */ export function updateCellSize() { if (sprites.value.length === 0) { + logger.warn('Cannot update cell size: no sprites available'); return; } try { let maxWidth = 0; let maxHeight = 0; + let validSpriteCount = 0; // Find the maximum dimensions across all sprites sprites.value.forEach(sprite => { @@ -59,17 +72,29 @@ export function updateCellSize() { } maxWidth = Math.max(maxWidth, sprite.width); maxHeight = Math.max(maxHeight, sprite.height); + validSpriteCount++; }); - if (maxWidth === 0 || maxHeight === 0) { - logger.error('Failed to calculate valid cell size'); + if (maxWidth === 0 || maxHeight === 0 || validSpriteCount === 0) { + logger.error('Failed to calculate valid cell size - no valid sprites found'); return; } // Add a small buffer to ensure sprites fit completely (optional) const buffer = 0; // Increase if you want padding between sprites - cellSize.width = maxWidth + buffer; - cellSize.height = maxHeight + buffer; + + // Set cell size with validation + const newWidth = maxWidth + buffer; + const newHeight = maxHeight + buffer; + + if (newWidth <= 0 || newHeight <= 0) { + logger.error(`Invalid calculated cell dimensions: ${newWidth}x${newHeight}`); + return; + } + + logger.info(`Updating cell size to ${newWidth}x${newHeight}`); + cellSize.width = newWidth; + cellSize.height = newHeight; // Ensure all sprites are within their cell bounds after resize sprites.value.forEach((sprite, index) => { @@ -100,13 +125,25 @@ export function updateCellSize() { */ export function autoArrangeSprites() { if (sprites.value.length === 0) { + logger.warn('No sprites to arrange'); return; } try { - if (cellSize.width <= 0 || cellSize.height <= 0) { + // Ensure cell size is valid before proceeding + if (!cellSize || typeof cellSize.width !== 'number' || typeof cellSize.height !== 'number' || cellSize.width <= 0 || cellSize.height <= 0) { logger.error('Invalid cell size for auto-arranging', cellSize); - return; + + // Try to update cell size first + updateCellSize(); + + // Check again after update attempt + if (!cellSize || typeof cellSize.width !== 'number' || typeof cellSize.height !== 'number' || cellSize.width <= 0 || cellSize.height <= 0) { + logger.error('Still invalid cell size after update attempt', cellSize); + return; + } + + logger.warn('Cell size was updated and is now valid'); } // First update the canvas size to ensure it's large enough diff --git a/src/application/utilities.ts b/src/application/utilities.ts index 63ef55c..543108c 100644 --- a/src/application/utilities.ts +++ b/src/application/utilities.ts @@ -5,6 +5,9 @@ import { type Sprite } from '@/application/types'; * Logger utility with consistent error format */ export const logger = { + info: (message: string, details?: any) => { + console.log(`Spritesheet: ${message}`, details || ''); + }, warn: (message: string, details?: any) => { console.warn(`Spritesheet: ${message}`, details || ''); }, diff --git a/src/components/DropZone.vue b/src/components/DropZone.vue index 112ea5b..43a84b9 100644 --- a/src/components/DropZone.vue +++ b/src/components/DropZone.vue @@ -1,5 +1,12 @@