From 18c79f51a24a60e69bfce94b258cfaa723fb90b7 Mon Sep 17 00:00:00 2001 From: Scott Davis Date: Sat, 25 Jun 2011 01:23:50 -0400 Subject: [PATCH] horizontal sprite layout --- lib/compass/sass_extensions/sprites.rb | 1 + .../sass_extensions/sprites/layout_methods.rb | 85 +++++++++++++++++++ .../sass_extensions/sprites/sprite_map.rb | 2 + .../sass_extensions/sprites/sprite_methods.rb | 13 --- 4 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 lib/compass/sass_extensions/sprites/layout_methods.rb diff --git a/lib/compass/sass_extensions/sprites.rb b/lib/compass/sass_extensions/sprites.rb index ab35d84e..246836a7 100644 --- a/lib/compass/sass_extensions/sprites.rb +++ b/lib/compass/sass_extensions/sprites.rb @@ -9,6 +9,7 @@ module Compass end require 'compass/sass_extensions/sprites/image' +require 'compass/sass_extensions/sprites/layout_methods' require 'compass/sass_extensions/sprites/sprite_methods' require 'compass/sass_extensions/sprites/image_methods' require 'compass/sass_extensions/sprites/sprite_map' diff --git a/lib/compass/sass_extensions/sprites/layout_methods.rb b/lib/compass/sass_extensions/sprites/layout_methods.rb new file mode 100644 index 00000000..d8787671 --- /dev/null +++ b/lib/compass/sass_extensions/sprites/layout_methods.rb @@ -0,0 +1,85 @@ +module Compass + module SassExtensions + module Sprites + module LayoutMethods + HORIZONTAL = 'horizontal' + + def horizontal? + @kwargs.get_var('layout').value == HORIZONTAL + end + + # Calculates the overal image dimensions + # collects image sizes and input parameters for each sprite + def compute_image_positions! + if horizontal? + calculate_height + calculate_horizontal_positions + calculate_width + else + calculate_width + calulate_vertical_postions + calculate_height + end + end + + def calculate_horizontal_positions + @images.each_with_index do |image, index| + image.top = image.position.unit_str == '%' ? (@height - image.height) * (image.position.value / 100.0) : image.position.value + next if index == 0 + last_image = @images[index-1] + image.left = last_image.left + last_image.width + [image.offset, last_image.offset].max + end + end + + def calulate_vertical_postions + @images.each_with_index do |image, index| + image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100.0) : 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 + end + + + def calculate_dimensions! + calculate_width + calculate_height + end + + def calculate_height + @height = if horizontal? + height_for_horizontal_layout + else + height_for_vertical_layout + end + end + + def height_for_vertical_layout + last = @images.last + last.top + last.height + end + + def height_for_horizontal_layout + @height = @images.map {|image| image.height + image.spacing}.max + end + + def calculate_width + @width = if horizontal? + width_for_horizontal_layout + else + width_for_vertical_layout + end + end + + def width_for_horizontal_layout + @images.inject(0) { |sum, image| sum += (image.width + image.offset) } + end + + def width_for_vertical_layout + @images.map { |image| image.width + image.offset }.max + end + end + end + end +end + \ No newline at end of file diff --git a/lib/compass/sass_extensions/sprites/sprite_map.rb b/lib/compass/sass_extensions/sprites/sprite_map.rb index 0ca769d0..aa9179a1 100644 --- a/lib/compass/sass_extensions/sprites/sprite_map.rb +++ b/lib/compass/sass_extensions/sprites/sprite_map.rb @@ -7,6 +7,7 @@ module Compass include SpriteMethods include ImageMethods + include LayoutMethods # Initialize a new sprite object from a relative file path @@ -27,6 +28,7 @@ module Compass @name = name @kwargs = kwargs @kwargs['cleanup'] ||= Sass::Script::Bool.new(true) + @kwargs['layout'] ||= Sass::Script::String.new('vertical') @images = nil @width = nil @height = nil diff --git a/lib/compass/sass_extensions/sprites/sprite_methods.rb b/lib/compass/sass_extensions/sprites/sprite_methods.rb index 539d1d22..7fdec149 100644 --- a/lib/compass/sass_extensions/sprites/sprite_methods.rb +++ b/lib/compass/sass_extensions/sprites/sprite_methods.rb @@ -14,7 +14,6 @@ module Compass @width = 0 init_images compute_image_positions! - @height = @images.last.top + @images.last.height init_engine end @@ -29,22 +28,10 @@ module Compass def init_images @images = image_names.collect do |relative_file| image = Compass::SassExtensions::Sprites::Image.new(self, relative_file, kwargs) - @width = [ @width, image.width + image.offset ].max image end end - # Calculates the overal image dimensions - # collects image sizes and input parameters for each sprite - def compute_image_positions! - @images.each_with_index do |image, index| - image.left = image.position.unit_str == "%" ? (@width - image.width) * (image.position.value / 100.0) : 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 - end - # Validates that the sprite_names are valid sass def validate! for sprite_name in sprite_names