From 59722ef4ad48a1a52f73ac3828d0ded1d3fb0002 Mon Sep 17 00:00:00 2001 From: Scott Davis Date: Tue, 2 Aug 2011 23:36:20 -0400 Subject: [PATCH] smart packing implimentation thanks @johnbintz --- lib/compass/sass_extensions/sprites.rb | 3 ++- .../sass_extensions/sprites/layout_methods.rb | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/compass/sass_extensions/sprites.rb b/lib/compass/sass_extensions/sprites.rb index 246836a7..b5f08a94 100644 --- a/lib/compass/sass_extensions/sprites.rb +++ b/lib/compass/sass_extensions/sprites.rb @@ -7,7 +7,8 @@ module Compass 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/layout_methods' require 'compass/sass_extensions/sprites/sprite_methods' diff --git a/lib/compass/sass_extensions/sprites/layout_methods.rb b/lib/compass/sass_extensions/sprites/layout_methods.rb index f194e742..5a030231 100644 --- a/lib/compass/sass_extensions/sprites/layout_methods.rb +++ b/lib/compass/sass_extensions/sprites/layout_methods.rb @@ -4,6 +4,11 @@ module Compass module LayoutMethods HORIZONTAL = 'horizontal' DIAGONAL = 'diagonal' + SMART = 'smart' + + def smart? + @kwargs.get_var('layout').value == SMART + end def horizontal? @kwargs.get_var('layout').value == HORIZONTAL @@ -17,6 +22,10 @@ module Compass # collects image sizes and input parameters for each sprite def compute_image_positions! 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 calculate_diagonal_dimensions calculate_diagonal_positions @@ -32,6 +41,23 @@ module Compass 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 @width = @images.map {|image| image.width}.inject(0) {|width, sum| sum + width} @height = @images.map {|image| image.height}.inject(0) {|height, sum| sum + height}