Skip to content

Conversation

@DatMoshu
Copy link

Animation Packing & Unpacking System

Overview

This PR introduces a robust system for exporting (packing) and importing (unpacking) animations in UOFiddler. It allows users to easily transfer animation data between UOFiddler and external tools by converting complex UO frame structures into standard PNG sprite sheets with accompanying JSON metadata.

Key Features

1. Pack Frames (Export)

  • Functionality: Converts the selected animation (or all actions for a mob) into a single PNG sprite sheet and a JSON descriptor file.
  • Customization: Users can configure the export via a new PackOptionsForm:
    • Directions: Choose which facing directions to export.
    • Layout: Define maximum sprite sheet width or force "One Row Per Direction" for easier manual editing.
    • Spacing: Adjust padding between frames.
  • Output: Generates anim_{body}_{action}.png (sprite sheet) and anim_{body}_{action}.json (metadata).
  • Metadata: The JSON file preserves critical data for every frame, including:
    • Direction & Index (Frame order)
    • Frame bounds (X, Y, Width, Height on the sheet)
    • Center point (The "pivot" or "anchor" of the frame, essential for correct alignment in-game)

2. Unpack Frames (Import)

  • Functionality: Reconstructs animations from the exported JSON and PNG files.
  • Process:
    • Reads the JSON to locate frames on the sprite sheet.
    • Extracts pixel data and rebuilds the UO-specific palette (16-bit A1R5G5B5).
    • Restores the exact Center points for every frame.
  • Conflict Resolution: Prompts the user to either Overwrite existing frames or Append to them.

3. Bulk Unpack (Batch Import)

  • Functionality: Automates the importing process for an entire directory of animations.
  • Workflow:
    • Scans a selected folder for *.json files.
    • Parses filenames (expected format: anim_{body}_{action}.json) to automatically map data to the correct Body and Action IDs.
    • Global Overwrite: Asks the user once for their overwrite preference (Yes/No/Cancel) and applies it to the entire batch, preventing popup spam.
    • Error Handling: Skips invalid files or mismatches gracefully, providing a summary report (Imported count vs. Error count) at the end.

Technical Implementation

  • AnimationListControl.cs:
    • Added OnPackFramesClick: Handles the export logic, sprite sheet generation using GDI+, and JSON serialization.
    • Added OnUnpackFramesClick: Handles single-file import.
    • Added OnBulkUnpackFramesClick: Implements the directory scanning and batch processing loop.
    • Refactored UnpackAnimation: A core helper method that performs the actual file reading, bitmap extraction, and memory insertion for both single and bulk modes.
  • Data Structures:
    • Introduced PackedOutput, PackedMeta, and PackedFrameEntry classes to strictly type the JSON data.
  • Palette Management:
    • Implemented BuildPaletteFromFrames to dynamically generate an optimal 16-bit palette from the 32-bit source PNGs during import, ensuring visual fidelity is maintained when converting back to UO's format.

How to Test

  1. Pack: Right-click an animation in the list -> "Pack Frames". Select options and save. Verify the PNG and JSON are created.
  2. Unpack: Right-click an animation -> "Unpack Frames". Select a generated JSON file. Verify the animation loads correctly in the viewer.
  3. Bulk: Place multiple exported animation files in a folder. Right-click -> "Bulk Unpack Frames". Select the folder and confirm the batch processes correctly.

Introduces functionality to export animation frames as packed JSON/PNG sprite sheets and import them back, including new context menu items and dialogs for options. Adds supporting classes for JSON structure in PackerClasses.cs and integrates handlers in AnimationListControl for packing and unpacking frames.
Refactored the animation frame import process to prompt the user once for overwrite/append behavior across all directions, improving usability. Added palette building and region extraction helpers for more efficient and accurate color mapping. Enhanced error handling and memory management during large imports.
Introduces a 'One row per direction' checkbox to the PackOptionsForm, allowing packed sprite sheets to organize frames with each direction on a separate row. Updates packing logic to support this option and outputs a text file with row mapping when enabled.
Introduces a 'FrameSpacing' option in the animation packing dialog, allowing users to specify spacing between frames when exporting packed animations. Updates layout logic to respect the spacing value, and adds a UI slider for user control.
Introduces an 'Export all animations' checkbox to the pack options form, allowing users to export all actions for the selected body. Refactors export logic to support batch exporting and updates UI layout to accommodate the new option.
Introduces a 'Bulk Unpack Frames' menu item and handler to allow importing multiple animation JSON files at once. Refactors unpack logic into a reusable method and adds overwrite/append options for bulk imports, improving workflow for batch animation imports.
Added a check to skip processing animation directions greater than 4 in AnimationListControl. This prevents handling unsupported or invalid direction values.
Moved PackedOutput, PackedMeta, PackedFrameEntry, Rect, PointStruct, and SizeStruct classes from AnimationListControl.cs to UoFiddler.Controls/Classes for reuse and better organization. Deleted PackerClasses.cs, consolidating all packed animation-related data structures into the new shared classes. Adjusted button layout in AnimationListControl for improved UI.
Introduces an 'Import Animation' menu item to the context menu in AnimationListControl, allowing users to import animations via unpacking frames. Also refactors the export animation submenu to remove unpack options, consolidating import-related actions under the new menu item.
The PackOptionsForm class was relocated from AnimationListControl.cs to its own file, PackOptionsForm.cs, to improve code organization and maintain compilation visibility.
Introduced AnimationDebugHelper to create a debug image with frame bounds and center crosshairs. Updated AnimationListControl to generate and save this debug image alongside the packed sprite and JSON output.
Introduces export and import functionality for packed item ranges in ItemsControl, including new classes for packed item metadata and output, a range input form, and UI integration. Users can export selected item ranges to a sprite sheet and JSON, and import them back, facilitating easier sharing and management of item graphics.
Copy link
Collaborator

@AsYlum- AsYlum- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. Minor changes needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be reverted. Formatting changes in unrelated file.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this to Forms directory?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants