Class: Spriter
- Inherits:
-
Object
- Object
- Spriter
- Defined in:
- lib/spriter.rb
Class Attribute Summary collapse
-
.assets_path ⇒ Object
Returns the value of attribute assets_path.
-
.sprite_image_path ⇒ Object
Returns the value of attribute sprite_image_path.
-
.sprite_image_url ⇒ Object
Returns the value of attribute sprite_image_url.
Instance Attribute Summary collapse
-
#images ⇒ Object
readonly
Returns the value of attribute images.
Class Method Summary collapse
Instance Method Summary collapse
- #add_image(image) ⇒ Object
- #generate_sprite_image ⇒ Object
-
#initialize(assets_path = Spriter.assets_path, sprite_image_path = Spriter.sprite_image_path, sprite_image_url = Spriter.sprite_image_url) ⇒ Spriter
constructor
A new instance of Spriter.
- #transform(*inputs) ⇒ Object
- #transform_files(*paths) ⇒ Object
- #y_offset(image) ⇒ Object
Constructor Details
#initialize(assets_path = Spriter.assets_path, sprite_image_path = Spriter.sprite_image_path, sprite_image_url = Spriter.sprite_image_url) ⇒ Spriter
Returns a new instance of Spriter.
36 37 38 39 40 41 |
# File 'lib/spriter.rb', line 36 def initialize(assets_path = Spriter.assets_path, sprite_image_path = Spriter.sprite_image_path, sprite_image_url = Spriter.sprite_image_url) @assets_path, @sprite_image_path, @sprite_image_url = assets_path, sprite_image_path, sprite_image_url @images = [] @y_offsets = {} @current_offset, @max_width = 0, 0 end |
Class Attribute Details
.assets_path ⇒ Object
Returns the value of attribute assets_path.
5 6 7 |
# File 'lib/spriter.rb', line 5 def assets_path @assets_path end |
.sprite_image_path ⇒ Object
Returns the value of attribute sprite_image_path.
6 7 8 |
# File 'lib/spriter.rb', line 6 def sprite_image_path @sprite_image_path end |
.sprite_image_url ⇒ Object
Returns the value of attribute sprite_image_url.
7 8 9 |
# File 'lib/spriter.rb', line 7 def sprite_image_url @sprite_image_url end |
Instance Attribute Details
#images ⇒ Object (readonly)
Returns the value of attribute images.
34 35 36 |
# File 'lib/spriter.rb', line 34 def images @images end |
Class Method Details
.image_dimensions(path) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/spriter.rb', line 17 def image_dimensions(path) dimensions = [0, 0] if path =~ /\.png$/ dimensions = IO.read(path)[0x10..0x18].unpack('NN') elsif path =~ /\.gif$/ dimensions = IO.read(path)[6..10].unpack('SS') end if dimensions.any?{|n| n.zero? } magick_image = MiniMagick::Image.from_file(path) dimensions = [ magick_image[:width], magick_image[:height] ] end dimensions end |
.transform(*args) ⇒ Object
9 10 11 |
# File 'lib/spriter.rb', line 9 def transform(*args) new.transform(*args) end |
.transform_files(*args) ⇒ Object
13 14 15 |
# File 'lib/spriter.rb', line 13 def transform_files(*args) new.transform_files(*args) end |
Instance Method Details
#add_image(image) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/spriter.rb', line 75 def add_image(image) return if @images.include? image width, height = self.class.image_dimensions(File.join(@assets_path, image)) @images << image @y_offsets[image] = @current_offset @current_offset += height @max_width = [@max_width, width].max end |
#generate_sprite_image ⇒ Object
90 91 92 93 94 |
# File 'lib/spriter.rb', line 90 def generate_sprite_image return unless @images.any? source_paths = @images.map{ |file| File.join(@assets_path, file) } system(*['convert', source_paths, '-gravity', 'West', '-background', '#FFFFFF00', '-append', @sprite_image_path].flatten) or raise "Failed to generate sprite" end |
#transform(*inputs) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/spriter.rb', line 43 def transform(*inputs) previous_image_list = inputs.last.is_a?(Array) ? inputs.pop : [] image_matcher = /([^a-z])-spriter-background:\s*([^;\}]+)([;\}])/ new_css = inputs.inject([]) do |new_css, css| css = css.read if css.respond_to? :read new_css << css.gsub(image_matcher) do |matched| indent, image, terminator = $1, $2, $3 image = image.strip.gsub(/(?:^['"]|['"]$)/, '') add_image(image) "#{indent}background: url(#{@sprite_image_url}) no-repeat 0 #{-y_offset(image)}#{y_offset(image) == 0 ? '' : 'px'}#{terminator} /* #{image} */" end end generate_sprite_image unless @images == previous_image_list if new_css.length == 1 new_css.first else new_css end end |
#transform_files(*paths) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/spriter.rb', line 66 def transform_files(*paths) files = paths.map{ |p| File.open(p, 'r') } generated_css = transform(*files) generated_css = [generated_css] unless generated_css.is_a? Array paths.map{ |p| p.sub(/\.spriter$/, '.css') }.each_with_index do |output_path, i| File.open(output_path, 'w'){ |f| f << generated_css[i] } end end |
#y_offset(image) ⇒ Object
86 87 88 |
# File 'lib/spriter.rb', line 86 def y_offset(image) @y_offsets[image] or raise ArgumentError, "Unknown image (#{image})\nKnown images are: #{@images.join(', ')}" end |