smart packing implimentation thanks @johnbintz

This commit is contained in:
Scott Davis 2011-08-02 23:36:20 -04:00
parent ef4c897de5
commit 59722ef4ad
2 changed files with 28 additions and 1 deletions

View File

@ -7,7 +7,8 @@ module Compass
end end
end end
end end
require 'compass/sass_extensions/sprites/image_row'
require 'compass/sass_extensions/sprites/row_fitter'
require 'compass/sass_extensions/sprites/image' require 'compass/sass_extensions/sprites/image'
require 'compass/sass_extensions/sprites/layout_methods' require 'compass/sass_extensions/sprites/layout_methods'
require 'compass/sass_extensions/sprites/sprite_methods' require 'compass/sass_extensions/sprites/sprite_methods'

View File

@ -4,6 +4,11 @@ module Compass
module LayoutMethods module LayoutMethods
HORIZONTAL = 'horizontal' HORIZONTAL = 'horizontal'
DIAGONAL = 'diagonal' DIAGONAL = 'diagonal'
SMART = 'smart'
def smart?
@kwargs.get_var('layout').value == SMART
end
def horizontal? def horizontal?
@kwargs.get_var('layout').value == HORIZONTAL @kwargs.get_var('layout').value == HORIZONTAL
@ -17,6 +22,10 @@ module Compass
# collects image sizes and input parameters for each sprite # collects image sizes and input parameters for each sprite
def compute_image_positions! def compute_image_positions!
case @kwargs.get_var('layout').value case @kwargs.get_var('layout').value
when SMART
require 'compass/sass_extensions/sprites/image_row'
require 'compass/sass_extensions/sprites/row_fitter'
calculate_smart_positions
when DIAGONAL when DIAGONAL
calculate_diagonal_dimensions calculate_diagonal_dimensions
calculate_diagonal_positions calculate_diagonal_positions
@ -32,6 +41,23 @@ module Compass
end end
end end
def calculate_smart_positions
fitter = ::Compass::SassExtensions::Sprites::RowFitter.new(@images)
current_y = 0
fitter.fit!.each do |row|
current_x = 0
row.images.each_with_index do |image, index|
image.left = current_x
image.top = current_y
current_x += image.width
end
current_y += row.height
end
@width = fitter.width
@height = fitter.height
end
def calculate_diagonal_dimensions def calculate_diagonal_dimensions
@width = @images.map {|image| image.width}.inject(0) {|width, sum| sum + width} @width = @images.map {|image| image.width}.inject(0) {|width, sum| sum + width}
@height = @images.map {|image| image.height}.inject(0) {|height, sum| sum + height} @height = @images.map {|image| image.height}.inject(0) {|height, sum| sum + height}