Skip to content
This repository
Browse code

chunky sprite can now tell you the locations of the images

  • Loading branch information...
commit 1ebfa0616617a30b810f8117efb9c77b72e231d3 1 parent 8ee5770
colin harris authored March 22, 2011
11  lib/css-spriter/chunky_sprite.rb
@@ -10,12 +10,23 @@ def initialize
10 10
 
11 11
   def append( image )
12 12
     @images << image
  13
+    @max_height = @images.map{ |i| i.height }.max
13 14
   end
14 15
 
15 16
   def append_file( filename )
16 17
     append( ChunkyPNG::Image.from_file( filename ))
17 18
   end
18 19
 
  20
+  def locations
  21
+    @images.inject(0) do |x, image|
  22
+      @locations[image.name.to_sym] = { :x => -(x),
  23
+        :width => image.width,
  24
+        :height => image.height}
  25
+      image.width + x
  26
+    end
  27
+    @locations
  28
+  end
  29
+
19 30
   def write( output_filename )
20 31
     return if @images.empty?
21 32
 
31  spec/lib/chunky_sprite_spec.rb
... ...
@@ -0,0 +1,31 @@
  1
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
  2
+
  3
+describe ChunkySprite do
  4
+  before :each do
  5
+     @sprite = ChunkySprite.new
  6
+     @builder = ImageBuilder.new
  7
+
  8
+     #TODO - We should just create ImageData objects here
  9
+     @image1 = @builder.build( :width => 50, :height => 50, :name => "image1").to_image
  10
+     @image2 = @builder.build( :width => 50, :height => 50, :name => "image2").to_image
  11
+   end
  12
+
  13
+   it "knows the location of each image in the sprite" do
  14
+     @sprite.append( @image1 )
  15
+     @sprite.append( @image2 )
  16
+
  17
+     @sprite.locations[@image1.name.to_sym].should == {:x => -( 0 ), :width=> @image1.width, :height => @image1.height }
  18
+     @sprite.locations[@image2.name.to_sym].should == {:x => -( @image2.width ),  :width=> @image2.width, :height => @image2.height }
  19
+   end
  20
+
  21
+   it "knows the height of the tallest image" do
  22
+     max_height = 70
  23
+
  24
+     @image3 = @builder.build( :width => 50, :height => max_height, :name => "image3")
  25
+
  26
+     @sprite.append( @image1 )
  27
+     @sprite.append( @image3 )
  28
+
  29
+     @sprite.max_height.should == max_height
  30
+   end
  31
+end

0 notes on commit 1ebfa06

Please sign in to comment.
Something went wrong with that request. Please try again.