class Spritely::ImageSet

Each image in the sprite maps to an instance of `ImageSet` that stores the image data, width, height, and outer positioning.

Attributes

data[R]
height[R]
left[R]
options[R]
path[R]
top[RW]
width[R]

Public Class Methods

new(path, options) click to toggle source
# File lib/spritely/image_set.rb, line 10
def initialize(path, options)
  @path = path
  @options = options
  @data = File.read(path)
  @width, @height = data[0x10..0x18].unpack('NN')
  @left = 0
end

Public Instance Methods

images() click to toggle source
# File lib/spritely/image_set.rb, line 22
def images
  @images ||= []
end
name() click to toggle source
# File lib/spritely/image_set.rb, line 18
def name
  File.basename(path, ".png")
end
outer_height() click to toggle source
# File lib/spritely/image_set.rb, line 26
def outer_height
  spacing_above + height + spacing_below
end
position_in!(collection_width) click to toggle source

When positioned in the sprite, we must take into account whether the image is configured to repeat, or is positioned to the right-hand side of the sprite map.

# File lib/spritely/image_set.rb, line 49
def position_in!(collection_width)
  if repeated?
    left_position = 0
    while left_position < collection_width
      add_image!(left_position)
      left_position += width
    end
  elsif right?
    add_image!(@left = collection_width - width)
  else
    add_image!(0)
  end
end
repeated?() click to toggle source
# File lib/spritely/image_set.rb, line 38
def repeated?
  options[:repeat] == 'true'
end
right?() click to toggle source
# File lib/spritely/image_set.rb, line 42
def right?
  options[:position] == 'right'
end
spacing_above() click to toggle source
# File lib/spritely/image_set.rb, line 30
def spacing_above
  options[:spacing_above].to_i
end
spacing_below() click to toggle source
# File lib/spritely/image_set.rb, line 34
def spacing_below
  (options[:spacing_below] || options[:spacing]).to_i
end

Private Instance Methods

add_image!(left_position) click to toggle source
# File lib/spritely/image_set.rb, line 65
def add_image!(left_position)
  images << Image.new(data).tap do |image|
    image.top = top + spacing_above
    image.left = left_position
  end
end