refactoring sprite class
This commit is contained in:
parent
995a20cd36
commit
153582fe68
@ -1,7 +1,7 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
compass (0.11.beta.2.994cf53)
|
||||
compass (0.11.beta.2.995a20c)
|
||||
chunky_png (~> 0.12.0)
|
||||
sass (>= 3.1.0.alpha.218)
|
||||
|
||||
|
@ -19,38 +19,32 @@ module Compass::SassExtensions::Functions::Sprites
|
||||
# collects image sizes and input parameters for each sprite
|
||||
def compute_image_metadata!
|
||||
@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|
|
||||
image = Compass::SassExtensions::Sprites::Image.new(relative_file, options)
|
||||
@width = [ @width, image.width + image.offset ].max
|
||||
image
|
||||
end
|
||||
end
|
||||
|
||||
def compute_image_positions!
|
||||
@images.each_with_index do |image, index|
|
||||
if index == 0
|
||||
image.top = 0
|
||||
else
|
||||
last_image = @images[index-1]
|
||||
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
|
||||
image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100) : image.position.value
|
||||
next if index == 0
|
||||
last_image = @images[index-1]
|
||||
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
|
||||
end
|
||||
@height = @images.last.top + @images.last.height
|
||||
end
|
||||
|
||||
def image_for(name)
|
||||
@images.detect { |img| img.name == name}
|
||||
end
|
||||
|
||||
# Calculate the size of the sprite
|
||||
def size
|
||||
[width, height]
|
||||
end
|
||||
|
||||
def require_png_library!
|
||||
begin
|
||||
require 'oily_png'
|
||||
|
@ -27,6 +27,11 @@ module Compass
|
||||
compute_image_metadata!
|
||||
end
|
||||
|
||||
# Calculate the size of the sprite
|
||||
def size
|
||||
[width, height]
|
||||
end
|
||||
|
||||
def sprite_names
|
||||
image_names.map{|f| Compass::Sprites.sprite_name(f) }
|
||||
end
|
||||
|
@ -9,6 +9,7 @@ module Compass
|
||||
|
||||
def initialize(relative_file, options)
|
||||
@relative_file, @options = relative_file, options
|
||||
@left = @top = 0
|
||||
end
|
||||
|
||||
def file
|
||||
|
Loading…
Reference in New Issue
Block a user