refactoring sprite class

This commit is contained in:
Scott Davis 2011-02-22 21:47:08 -05:00
parent 995a20cd36
commit 153582fe68
4 changed files with 20 additions and 20 deletions

View File

@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
compass (0.11.beta.2.994cf53) compass (0.11.beta.2.995a20c)
chunky_png (~> 0.12.0) chunky_png (~> 0.12.0)
sass (>= 3.1.0.alpha.218) sass (>= 3.1.0.alpha.218)

View File

@ -19,38 +19,32 @@ module Compass::SassExtensions::Functions::Sprites
# collects image sizes and input parameters for each sprite # collects image sizes and input parameters for each sprite
def compute_image_metadata! def compute_image_metadata!
@width = 0 @width = 0
init_images
compute_image_positions!
@height = @images.last.top + @images.last.height
end
def init_images
@images = image_names.collect do |relative_file| @images = image_names.collect do |relative_file|
image = Compass::SassExtensions::Sprites::Image.new(relative_file, options) image = Compass::SassExtensions::Sprites::Image.new(relative_file, options)
@width = [ @width, image.width + image.offset ].max @width = [ @width, image.width + image.offset ].max
image image
end end
end
def compute_image_positions!
@images.each_with_index do |image, index| @images.each_with_index do |image, index|
if index == 0 image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100) : image.position.value
image.top = 0 next if index == 0
else last_image = @images[index-1]
last_image = @images[index-1] image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
end
if image.position.unit_str == "%"
image.left = (@width - image.width) * (image.position.value / 100)
else
image.left = image.position.value
end
end end
@height = @images.last.top + @images.last.height
end end
def image_for(name) def image_for(name)
@images.detect { |img| img.name == name} @images.detect { |img| img.name == name}
end end
# Calculate the size of the sprite
def size
[width, height]
end
def require_png_library! def require_png_library!
begin begin
require 'oily_png' require 'oily_png'

View File

@ -27,6 +27,11 @@ module Compass
compute_image_metadata! compute_image_metadata!
end end
# Calculate the size of the sprite
def size
[width, height]
end
def sprite_names def sprite_names
image_names.map{|f| Compass::Sprites.sprite_name(f) } image_names.map{|f| Compass::Sprites.sprite_name(f) }
end end

View File

@ -9,6 +9,7 @@ module Compass
def initialize(relative_file, options) def initialize(relative_file, options)
@relative_file, @options = relative_file, options @relative_file, @options = relative_file, options
@left = @top = 0
end end
def file def file