horizontal sprite layout

This commit is contained in:
Scott Davis 2011-06-25 01:23:50 -04:00
parent 8d2ad57e18
commit 18c79f51a2
4 changed files with 88 additions and 13 deletions

View File

@ -9,6 +9,7 @@ module Compass
end end
require 'compass/sass_extensions/sprites/image' 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/sprite_methods'
require 'compass/sass_extensions/sprites/image_methods' require 'compass/sass_extensions/sprites/image_methods'
require 'compass/sass_extensions/sprites/sprite_map' require 'compass/sass_extensions/sprites/sprite_map'

View File

@ -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

View File

@ -7,6 +7,7 @@ module Compass
include SpriteMethods include SpriteMethods
include ImageMethods include ImageMethods
include LayoutMethods
# Initialize a new sprite object from a relative file path # Initialize a new sprite object from a relative file path
@ -27,6 +28,7 @@ module Compass
@name = name @name = name
@kwargs = kwargs @kwargs = kwargs
@kwargs['cleanup'] ||= Sass::Script::Bool.new(true) @kwargs['cleanup'] ||= Sass::Script::Bool.new(true)
@kwargs['layout'] ||= Sass::Script::String.new('vertical')
@images = nil @images = nil
@width = nil @width = nil
@height = nil @height = nil

View File

@ -14,7 +14,6 @@ module Compass
@width = 0 @width = 0
init_images init_images
compute_image_positions! compute_image_positions!
@height = @images.last.top + @images.last.height
init_engine init_engine
end end
@ -29,22 +28,10 @@ module Compass
def init_images def init_images
@images = image_names.collect do |relative_file| @images = image_names.collect do |relative_file|
image = Compass::SassExtensions::Sprites::Image.new(self, relative_file, kwargs) image = Compass::SassExtensions::Sprites::Image.new(self, relative_file, kwargs)
@width = [ @width, image.width + image.offset ].max
image image
end end
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 # Validates that the sprite_names are valid sass
def validate! def validate!
for sprite_name in sprite_names for sprite_name in sprite_names