module DragonSkeleton::FileFormats::AsepriteJson
Contains methods for reading Aseprite JSON Data files as produced by the “Export Sprite Sheet” command.
The Data file must have been exported as Array with Tags and Slices enabled.
Tag names will be converted to symbols during reading.
Frame durations will be rounded down to the nearest 3 ticks (50ms).
Public Class Methods
flip_animation_horizontally(animation)
click to toggle source
Returns a new animation with all frames (and associated slices) flipped horizontally.
# File lib/dragon_skeleton/file_formats/aseprite_json.rb, line 69 def flip_animation_horizontally(animation) { frames: animation[:frames].map { |frame| values = frame[:values] frame.merge( values: values.merge(flip_horizontally: !values[:flip_horizontally]), metadata: { slices: frame[:metadata][:slices].transform_values { |bounds| bounds.merge(x: values[:w] - bounds[:x] - bounds[:w]) } } ) } } end
read_as_animations(json_path)
click to toggle source
Reads an Aseprite Spritesheet JSON data file and returns a hash of animations which can be used with the DragonSkeleton::Animations
or DragonSkeleton::AnimatedSprite modules.
# File lib/dragon_skeleton/file_formats/aseprite_json.rb, line 17 def read_as_animations(json_path) sprite_sheet_data = deep_symbolize_keys! $gtk.parse_json_file(json_path) path = sprite_path(sprite_sheet_data, json_path) {}.tap { |result| frames = sprite_sheet_data.fetch :frames slices_data = sprite_sheet_data.fetch(:meta).fetch :slices sprite_sheet_data.fetch(:meta).fetch(:frameTags).each do |frame_tag_data| tag = frame_tag_data.fetch(:name).to_sym frame_range = frame_tag_data.fetch(:from)..frame_tag_data.fetch(:to) tag_frames = frame_range.map { |frame_index| frame_data = frames[frame_index] frame = frame_data.fetch(:frame) { path: path, w: frame[:w], h: frame[:h], tile_x: frame[:x], tile_y: frame[:y], tile_w: frame[:w], tile_h: frame[:h], flip_horizontally: false, duration: frame_data.fetch(:duration).idiv(50) * 3, # 50ms = 3 ticks metadata: { slices: slice_bounds_for_frame(slices_data, frame_index, frame.slice(:w, :h)) } } } apply_animation_direction! tag_frames, frame_tag_data.fetch(:direction) result[tag.to_sym] = Animations.build(frames: tag_frames) end } end
read_as_sprites(json_path)
click to toggle source
Reads an Aseprite Spritesheet JSON data file and returns a hash of sprites.
If a tag has only one frame, the sprite will be returned directly, otherwise an array of sprites will be returned.
# File lib/dragon_skeleton/file_formats/aseprite_json.rb, line 57 def read_as_sprites(json_path) animations = read_as_animations json_path animations.transform_values { |animation| sprites = animation[:frames].map { |frame| frame[:values].to_sprite(frame.slice(:duration, :metadata)) } sprites.length == 1 ? sprites.first : sprites } end