Class: Sprite
- Inherits:
-
Object
- Object
- Sprite
- Defined in:
- lib/spittle/sprite.rb
Defined Under Namespace
Classes: ImageFormatException
Instance Attribute Summary collapse
-
#images ⇒ Object
readonly
Returns the value of attribute images.
-
#max_height ⇒ Object
readonly
Returns the value of attribute max_height.
Instance Method Summary collapse
- #append(image) ⇒ Object
-
#initialize ⇒ Sprite
constructor
A new instance of Sprite.
- #locations ⇒ Object
- #write(output_filename) ⇒ Object
Constructor Details
#initialize ⇒ Sprite
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
#images ⇒ Object (readonly)
Returns the value of attribute images.
3 4 5 |
# File 'lib/spittle/sprite.rb', line 3 def images @images end |
#max_height ⇒ Object (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 |
#locations ⇒ Object
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 |