lib/sprite_factory/layout/packed.rb in sprite-factory-1.4.2 vs lib/sprite_factory/layout/packed.rb in sprite-factory-1.5.0

- old
+ new

@@ -4,43 +4,54 @@ #------------------------------------------------------------------------ def self.layout(images, options = {}) - raise NotImplementedError, ":packed layout does not support the :padding option" if (options[:padding].to_i > 0) || (options[:hpadding].to_i > 0) || (options[:vpadding].to_i > 0) raise NotImplementedError, ":packed layout does not support fixed :width/:height option" if options[:width] || options[:height] return { :width => 0, :height => 0 } if images.empty? + hpadding = options[:hpadding] || 0 + vpadding = options[:vpadding] || 0 + hmargin = options[:hmargin] || 0 + vmargin = options[:vmargin] || 0 + + images.each do |i| + i[:w] = i[:width] + (2*hpadding) + (2*hmargin) + i[:h] = i[:height] + (2*vpadding) + (2*vmargin) + end + images.sort! do |a,b| - diff = [b[:width], b[:height]].max <=> [a[:width], a[:height]].max - diff = [b[:width], b[:height]].min <=> [a[:width], a[:height]].min if diff.zero? - diff = b[:height] <=> a[:height] if diff.zero? - diff = b[:width] <=> a[:width] if diff.zero? + diff = [b[:w], b[:h]].max <=> [a[:w], a[:h]].max + diff = [b[:w], b[:h]].min <=> [a[:w], a[:h]].min if diff.zero? + diff = b[:h] <=> a[:h] if diff.zero? + diff = b[:w] <=> a[:w] if diff.zero? diff end - root = { :x => 0, :y => 0, :w => images[0][:width], :h => images[0][:height] } + root = { :x => 0, :y => 0, :w => images[0][:w], :h => images[0][:h] } images.each do |i| - if (node = findNode(root, i[:width], i[:height])) - placeImage(i, node) - splitNode(node, i[:width], i[:height]) + if (node = findNode(root, i[:w], i[:h])) + placeImage(i, node, hpadding, vpadding, hmargin, vmargin) + splitNode(node, i[:w], i[:h]) else - root = grow(root, i[:width], i[:height]) + root = grow(root, i[:w], i[:h]) redo end end { :width => root[:w], :height => root[:h] } end - def self.placeImage(image, node) - image[:cssx] = image[:x] = node[:x] # TODO - image[:cssy] = image[:y] = node[:y] # * support for :padding option - image[:cssw] = image[:width] # * support for fixed :width/:height option - image[:cssh] = image[:height] + def self.placeImage(image, node, hpadding, vpadding, hmargin, vmargin) + image[:cssx] = node[:x] + hmargin + image[:cssy] = node[:y] + vmargin + image[:cssw] = image[:width] + (2*hpadding) + image[:cssh] = image[:height] + (2*vpadding) + image[:x] = image[:cssx] + hpadding + image[:y] = image[:cssy] + vpadding end def self.findNode(root, w, h) if root[:used] findNode(root[:right], w, h) || findNode(root[:down], w, h)