Permalink
Browse files

chunky sprite can now tell you the locations of the images

  • Loading branch information...
1 parent 8ee5770 commit 1ebfa0616617a30b810f8117efb9c77b72e231d3 @aberant committed Mar 22, 2011
Showing with 42 additions and 0 deletions.
  1. +11 −0 lib/css-spriter/chunky_sprite.rb
  2. +31 −0 spec/lib/chunky_sprite_spec.rb
@@ -10,12 +10,23 @@ def initialize
def append( image )
@images << image
+ @max_height = @images.map{ |i| i.height }.max
end
def append_file( filename )
append( ChunkyPNG::Image.from_file( filename ))
end
+ 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
+
def write( output_filename )
return if @images.empty?
@@ -0,0 +1,31 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe ChunkySprite do
+ before :each do
+ @sprite = ChunkySprite.new
+ @builder = ImageBuilder.new
+
+ #TODO - We should just create ImageData objects here
+ @image1 = @builder.build( :width => 50, :height => 50, :name => "image1").to_image
+ @image2 = @builder.build( :width => 50, :height => 50, :name => "image2").to_image
+ end
+
+ it "knows the location of each image in the sprite" do
+ @sprite.append( @image1 )
+ @sprite.append( @image2 )
+
+ @sprite.locations[@image1.name.to_sym].should == {:x => -( 0 ), :width=> @image1.width, :height => @image1.height }
+ @sprite.locations[@image2.name.to_sym].should == {:x => -( @image2.width ), :width=> @image2.width, :height => @image2.height }
+ end
+
+ it "knows the height of the tallest image" do
+ max_height = 70
+
+ @image3 = @builder.build( :width => 50, :height => max_height, :name => "image3")
+
+ @sprite.append( @image1 )
+ @sprite.append( @image3 )
+
+ @sprite.max_height.should == max_height
+ end
+end

0 comments on commit 1ebfa06

Please sign in to comment.