Class: Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/spittle/sprite.rb

Defined Under Namespace

Classes: ImageFormatException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSprite

Returns a new instance of Sprite.



5
6
7
8
# File 'lib/spittle/sprite.rb', line 5

def initialize
  @images = []
  @locations = {}
end

Instance Attribute Details

#imagesObject (readonly)

Returns the value of attribute images.



3
4
5
# File 'lib/spittle/sprite.rb', line 3

def images
  @images
end

#max_heightObject (readonly)

Returns the value of attribute max_height.



3
4
5
# File 'lib/spittle/sprite.rb', line 3

def max_height
  @max_height
end

Instance Method Details

#append(image) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/spittle/sprite.rb', line 10

def append( image )
  @images.each do |i|
   unless i.compatible? image
     raise ImageFormatException.new("Image #{i} not compatible with #{image}")
   end
  end

  @images << image
  @max_height = @images.map{ |i| i.height }.max
end

#locationsObject



21
22
23
24
25
26
27
28
29
# File 'lib/spittle/sprite.rb', line 21

def locations
  @images.inject(0) do |x, image|
    @locations[image.name.to_sym] = { :x => -(x),
      :width => image.width,
      :height => image.height}
    image.width + x
  end
  @locations
end

#write(output_filename) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/spittle/sprite.rb', line 31

def write( output_filename )
  return if @images.empty?
  right_sized = @images.map{|i| i.fill_to_height(@max_height)}
  # head is the last image, then we merge left
  head, *tail = right_sized.reverse
  result = tail.inject( head ){ |head, image| head.merge_left( image ) }
  PNG::Image.write( output_filename, result )
end