class Spritely::Collection

A `SpriteMap` has a `Collection` that knows how to calculate the size of the sprite, based on image repetition and spacing.

Public Class Methods

create(*args) click to toggle source
# File lib/spritely/collection.rb, line 7
def self.create(*args)
  new(*args).tap do |collection|
    collection.position!
  end
end

Public Instance Methods

cache_key() click to toggle source
# File lib/spritely/collection.rb, line 21
def cache_key
  files.flat_map { |file_path| [Digest::MD5.hexdigest(file_path), Digest::MD5.file(file_path)] }.join
end
Also aliased as: to_s
find(name) click to toggle source
# File lib/spritely/collection.rb, line 17
def find(name)
  image_sets.find { |image_set| image_set.name == name }
end
height() click to toggle source
# File lib/spritely/collection.rb, line 42
def height
  heights.reduce(:+)
end
images() click to toggle source
# File lib/spritely/collection.rb, line 13
def images
  image_sets.flat_map(&:images)
end
position!() click to toggle source

Upon creation, the collection is then positioned appropriately by positioning each image within the sprite.

# File lib/spritely/collection.rb, line 48
def position!
  image_sets.each_with_index do |image_set, index|
    image_set.top = heights[0..index].reduce(:+) - image_set.outer_height
    image_set.position_in!(width)
  end
end
to_s()
Alias for: cache_key
width() click to toggle source

Returns the width of the to-be-generated sprite image. When none of the images repeat, it is simply the max width of all images in the sprite. When an image in the sprite is repeated, a calculation is performed based on the least common multiple of all repeated images. That least common multiple is then multiplied by the minimum multiple that will result in a value greater than or equal to the max width of all images in the sprite.

# File lib/spritely/collection.rb, line 32
def width
  @width ||= if image_sets.none?(&:repeated?)
    max_width
  else
    lcm = image_sets.select(&:repeated?).collect(&:width).reduce(:lcm)

    lcm * (max_width / lcm.to_f).ceil
  end
end

Private Instance Methods

heights() click to toggle source
# File lib/spritely/collection.rb, line 65
def heights
  @heights ||= image_sets.collect(&:outer_height)
end
image_sets() click to toggle source
# File lib/spritely/collection.rb, line 57
def image_sets
  @image_sets ||= files.collect { |file| ImageSet.new(file, options[:images][File.basename(file, ".png")] || options[:global]) }
end
max_width() click to toggle source
# File lib/spritely/collection.rb, line 61
def max_width
  @max_width ||= image_sets.collect(&:width).max
end