rdoc updates for sprite classes
This commit is contained in:
parent
6d91ecc0ea
commit
e47fccd2ed
@ -2,6 +2,10 @@ module Compass
|
|||||||
module SassExtensions
|
module SassExtensions
|
||||||
module Sprites
|
module Sprites
|
||||||
class Base < Sass::Script::Literal
|
class Base < Sass::Script::Literal
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize a new aprite object from a relative file path
|
||||||
|
# the path is relative to the <tt>images_path</tt> confguration option
|
||||||
def self.from_uri(uri, context, kwargs)
|
def self.from_uri(uri, context, kwargs)
|
||||||
sprite_map = ::Compass::SpriteMap.new(uri.value, {})
|
sprite_map = ::Compass::SpriteMap.new(uri.value, {})
|
||||||
|
|
||||||
@ -10,7 +14,8 @@ module Compass
|
|||||||
end
|
end
|
||||||
new(sprites, sprite_map.path, sprite_map.name, context, kwargs)
|
new(sprites, sprite_map.path, sprite_map.name, context, kwargs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Loads the sprite engine
|
||||||
def require_engine!
|
def require_engine!
|
||||||
self.class.send(:include, eval("::Compass::SassExtensions::Sprites::#{modulize}Engine"))
|
self.class.send(:include, eval("::Compass::SassExtensions::Sprites::#{modulize}Engine"))
|
||||||
end
|
end
|
||||||
@ -41,13 +46,15 @@ module Compass
|
|||||||
|
|
||||||
# Calculates the overal image dimensions
|
# Calculates the overal image dimensions
|
||||||
# collects image sizes and input parameters for each sprite
|
# collects image sizes and input parameters for each sprite
|
||||||
|
# Calculates the height
|
||||||
def compute_image_metadata!
|
def compute_image_metadata!
|
||||||
@width = 0
|
@width = 0
|
||||||
init_images
|
init_images
|
||||||
compute_image_positions!
|
compute_image_positions!
|
||||||
@height = @images.last.top + @images.last.height
|
@height = @images.last.top + @images.last.height
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Creates the Sprite::Image objects for each image and calculates the width
|
||||||
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, options)
|
image = Compass::SassExtensions::Sprites::Image.new(self, relative_file, options)
|
||||||
@ -66,27 +73,34 @@ module Compass
|
|||||||
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
|
image.top = last_image.top + last_image.height + [image.spacing, last_image.spacing].max
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Fetches the Sprite::Image object for the supplied name
|
||||||
def image_for(name)
|
def image_for(name)
|
||||||
@images.detect { |img| img.name == name}
|
@images.detect { |img| img.name == name}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns true if the image name has a hover selector image
|
||||||
def has_hover?(name)
|
def has_hover?(name)
|
||||||
!image_for("#{name}_hover").nil?
|
!image_for("#{name}_hover").nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns true if the image name has a target selector image
|
||||||
def has_target?(name)
|
def has_target?(name)
|
||||||
!image_for("#{name}_target").nil?
|
!image_for("#{name}_target").nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns true if the image name has an active selector image
|
||||||
def has_active?(name)
|
def has_active?(name)
|
||||||
!image_for("#{name}_active").nil?
|
!image_for("#{name}_active").nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return and array of image names that make up this sprite
|
||||||
def sprite_names
|
def sprite_names
|
||||||
image_names.map { |f| File.basename(f, '.png') }
|
image_names.map { |f| File.basename(f, '.png') }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# Validates that the sprite_names are valid sass
|
||||||
def validate!
|
def validate!
|
||||||
for sprite_name in sprite_names
|
for sprite_name in sprite_names
|
||||||
unless sprite_name =~ /\A#{Sass::SCSS::RX::IDENT}\Z/
|
unless sprite_name =~ /\A#{Sass::SCSS::RX::IDENT}\Z/
|
||||||
@ -108,11 +122,13 @@ module Compass
|
|||||||
Compass.configuration.run_callback(:sprite_generated, sprite_data)
|
Compass.configuration.run_callback(:sprite_generated, sprite_data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Does this sprite need to be generated
|
||||||
def generation_required?
|
def generation_required?
|
||||||
!File.exists?(filename) || outdated?
|
!File.exists?(filename) || outdated?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the uniqueness hash for this sprite object
|
||||||
def uniqueness_hash
|
def uniqueness_hash
|
||||||
@uniqueness_hash ||= begin
|
@uniqueness_hash ||= begin
|
||||||
sum = Digest::MD5.new
|
sum = Digest::MD5.new
|
||||||
@ -128,6 +144,7 @@ module Compass
|
|||||||
@uniqueness_hash
|
@uniqueness_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Saves the sprite engine
|
||||||
def save!(output_png)
|
def save!(output_png)
|
||||||
saved = output_png.save filename
|
saved = output_png.save filename
|
||||||
Compass.configuration.run_callback(:sprite_saved, filename)
|
Compass.configuration.run_callback(:sprite_saved, filename)
|
||||||
@ -147,6 +164,7 @@ module Compass
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Mtime of the sprite file
|
||||||
def mtime
|
def mtime
|
||||||
File.mtime(filename)
|
File.mtime(filename)
|
||||||
end
|
end
|
||||||
|
@ -11,7 +11,6 @@ module Compass
|
|||||||
|
|
||||||
# Returns a PNG object
|
# Returns a PNG object
|
||||||
def construct_sprite
|
def construct_sprite
|
||||||
#require_png_library!
|
|
||||||
output_png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
|
output_png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
|
||||||
images.each do |image|
|
images.each do |image|
|
||||||
input_png = ChunkyPNG::Image.from_file(image.file)
|
input_png = ChunkyPNG::Image.from_file(image.file)
|
||||||
|
@ -9,23 +9,28 @@ module Compass
|
|||||||
@base, @relative_file, @options = base, relative_file, options
|
@base, @relative_file, @options = base, relative_file, options
|
||||||
@left = @top = 0
|
@left = @top = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The Full path to the image
|
||||||
def file
|
def file
|
||||||
File.join(Compass.configuration.images_path, relative_file)
|
File.join(Compass.configuration.images_path, relative_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Width of the image
|
||||||
def width
|
def width
|
||||||
dimensions.first
|
dimensions.first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Height of the image
|
||||||
def height
|
def height
|
||||||
dimensions.last
|
dimensions.last
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Basename of the image
|
||||||
def name
|
def name
|
||||||
File.basename(relative_file, '.png')
|
File.basename(relative_file, '.png')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Value of <tt> $#{name}-repeat </tt> or <tt> $repeat </tt>
|
||||||
def repeat
|
def repeat
|
||||||
[ "#{name}-repeat", "repeat" ].each { |which|
|
[ "#{name}-repeat", "repeat" ].each { |which|
|
||||||
if var = options.get_var(which)
|
if var = options.get_var(which)
|
||||||
@ -35,46 +40,57 @@ module Compass
|
|||||||
"no-repeat"
|
"no-repeat"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Value of <tt> $#{name}-position </tt> or <tt> $position </tt> defaults o <tt>0px</tt>
|
||||||
def position
|
def position
|
||||||
options.get_var("#{name}-position") || options.get_var("position") || Sass::Script::Number.new(0, ["px"])
|
options.get_var("#{name}-position") || options.get_var("position") || Sass::Script::Number.new(0, ["px"])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Offset within the sprite
|
||||||
def offset
|
def offset
|
||||||
(position.unitless? || position.unit_str == "px") ? position.value : 0
|
(position.unitless? || position.unit_str == "px") ? position.value : 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Spacing between this image and the next
|
||||||
def spacing
|
def spacing
|
||||||
(options.get_var("#{name}-spacing") || options.get_var("spacing") || Sass::Script::Number.new(0)).value
|
(options.get_var("#{name}-spacing") || options.get_var("spacing") || Sass::Script::Number.new(0)).value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# MD5 hash of this file
|
||||||
def digest
|
def digest
|
||||||
Digest::MD5.file(file).hexdigest
|
Digest::MD5.file(file).hexdigest
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# mtime of this file
|
||||||
def mtime
|
def mtime
|
||||||
File.mtime(file)
|
File.mtime(file)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Has hover selector
|
||||||
def hover?
|
def hover?
|
||||||
base.has_hover?(name)
|
base.has_hover?(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Hover selector Image object if exsists
|
||||||
def hover
|
def hover
|
||||||
base.image_for("#{name}_hover")
|
base.image_for("#{name}_hover")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Has target selector
|
||||||
def target?
|
def target?
|
||||||
base.has_target?(name)
|
base.has_target?(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Target selector Image object if exsists
|
||||||
def target
|
def target
|
||||||
base.image_for("#{name}_target")
|
base.image_for("#{name}_target")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Has active selector
|
||||||
def active?
|
def active?
|
||||||
base.has_active?(name)
|
base.has_active?(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Active selector Image object if exsists
|
||||||
def active
|
def active
|
||||||
base.image_for("#{name}_active")
|
base.image_for("#{name}_active")
|
||||||
end
|
end
|
||||||
|
@ -6,34 +6,41 @@ module Compass
|
|||||||
@uri, @options = uri, options
|
@uri, @options = uri, options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Name of this spite
|
||||||
def name
|
def name
|
||||||
ensure_path_and_name!
|
ensure_path_and_name!
|
||||||
@name
|
@name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The on-disk location of this sprite
|
||||||
def path
|
def path
|
||||||
ensure_path_and_name!
|
ensure_path_and_name!
|
||||||
@path
|
@path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the Glob of image files for this sprite
|
||||||
def files
|
def files
|
||||||
@files ||= Dir[File.join(Compass.configuration.images_path, uri)].sort
|
@files ||= Dir[File.join(Compass.configuration.images_path, uri)].sort
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns an Array of image names without the file extension
|
||||||
def sprite_names
|
def sprite_names
|
||||||
@sprite_names ||= files.collect { |file| File.basename(file, '.png') }
|
@sprite_names ||= files.collect { |file| File.basename(file, '.png') }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the sass options for this sprite
|
||||||
def sass_options
|
def sass_options
|
||||||
@sass_options ||= options.merge(:filename => name, :syntax => :scss, :importer => self)
|
@sass_options ||= options.merge(:filename => name, :syntax => :scss, :importer => self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the mtime of all image files combined
|
||||||
def mtime
|
def mtime
|
||||||
Compass.quick_cache("mtime:#{uri}") do
|
Compass.quick_cache("mtime:#{uri}") do
|
||||||
files.collect { |file| File.mtime(file) }.max
|
files.collect { |file| File.mtime(file) }.max
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns a Sass::Engine for this sprite object
|
||||||
def sass_engine
|
def sass_engine
|
||||||
Sass::Engine.new(content_for_images, options)
|
Sass::Engine.new(content_for_images, options)
|
||||||
end
|
end
|
||||||
@ -45,6 +52,7 @@ module Compass
|
|||||||
@path, @name = $1, $3
|
@path, @name = $1, $3
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Generates the Sass for this sprite file
|
||||||
def content_for_images(skip_overrides = false)
|
def content_for_images(skip_overrides = false)
|
||||||
<<-SCSS
|
<<-SCSS
|
||||||
@import "compass/utilities/sprites/base";
|
@import "compass/utilities/sprites/base";
|
||||||
@ -94,7 +102,11 @@ $#{name}-prefix: '' !default;
|
|||||||
}
|
}
|
||||||
SCSS
|
SCSS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Generates the override defaults for this Sprite
|
||||||
|
# <tt>$#{name}-#{sprite_name}-position </tt>
|
||||||
|
# <tt> $#{name}-#{sprite_name}-spacing </tt>
|
||||||
|
# <tt> #{name}-#{sprite_name}-repeat: </tt>
|
||||||
def generate_overrides
|
def generate_overrides
|
||||||
content = <<-TXT
|
content = <<-TXT
|
||||||
// These variables control the generated sprite output
|
// These variables control the generated sprite output
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
module Compass
|
module Compass
|
||||||
class Sprites < Sass::Importers::Base
|
class Sprites < Sass::Importers::Base
|
||||||
|
|
||||||
def find_relative(*args)
|
def find_relative(*args)
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Called by the sass engine to build a new SpriteMap
|
||||||
def find(uri, options)
|
def find(uri, options)
|
||||||
if uri =~ /\.png$/
|
if uri =~ /\.png$/
|
||||||
SpriteMap.new(uri, options).sass_engine
|
SpriteMap.new(uri, options).sass_engine
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Called by the sass engine to identift the SpriteMap
|
||||||
def key(uri, options)
|
def key(uri, options)
|
||||||
[self.class.name + ":" + File.dirname(File.expand_path(uri)),
|
[self.class.name + ":" + File.dirname(File.expand_path(uri)),
|
||||||
File.basename(uri)]
|
File.basename(uri)]
|
||||||
|
Loading…
Reference in New Issue
Block a user