Module: RubySprites::Packer::HorizontalSplit

Defined in:
lib/lash-sprites/packer/horizontal_split.rb

Defined Under Namespace

Classes: Heap

Class Method Summary collapse

Class Method Details

.pack(images, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lash-sprites/packer/horizontal_split.rb', line 8

def self.pack(images, options = {})
  width = 0
  height = 0
  
  images = images.dup

  images.sort! do |a, b|
    a.height <=> b.height
  end

  i = images.pop
  width = i.width
  height = i.height
  i.x = 0
  i.y = 0

  images.sort! do |a, b|
    a.area <=> b.area
  end

  block_heap = Heap.new {|a,b| b.area <=> a.area}

  while !images.empty?
    if block_heap.empty?
      img = images.pop
      if width == 0 && height == 0
        img.x = 0
        img.y = 0
        width = img.width
        height = img.height
      else
        if img.height > height
          block_heap.insert(Block.new(0, height, width, img.height - height))
          height = img.height
        elsif img.height < height
          block_heap.insert(Block.new(width, img.height, img.width, height - img.height))
        end
        img.x = width
        img.y = 0
        width += img.width
      end
    else
      while !block_heap.empty? && !images.empty?
        block = block_heap.remove
        cur_img = nil
        cur_exact = nil
        images.each_index do |i|
          cur_img = i if block.fits?(images[i])
          cur_exact = i if block.fits?(images[i]) && (block.width == images[i].width || block.height == images[i].height)
        end
        if !cur_exact.nil?
          img = images.delete_at(cur_exact)
          img.x = block.x
          img.y = block.y
          split_block(block, img).each do |b|
            block_heap.insert(b)
          end
        elsif !cur_img.nil?
          img = images.delete_at(cur_img)
          img.x = block.x
          img.y = block.y
          split_block(block, img).each do |b|
            block_heap.insert(b)
          end
        else
          # Nothing will fit in this block, we are throwing it out
        end
      end
    end
  end
  
  return {:width => width, :height => height}
end

.split_block(block, img) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lash-sprites/packer/horizontal_split.rb', line 82

def self.split_block(block, img)
  blocks = []
  img.x = block.x
  img.y = block.y
  if (block.width - img.width) * img.height > (block.height - img.height) * img.width
    blocks.push Block.new(block.x + img.width, block.y, block.width - img.width, block.height) if block.width != img.width
    blocks.push Block.new(block.x, block.y + img.height, img.width, block.height - img.height) if block.height != img.height
  else
    blocks.push Block.new(block.x + img.width, block.y, block.width - img.width, img.height) if block.width != img.width
    blocks.push Block.new(block.x, block.y + img.height, block.width, block.height - img.height) if block.height != img.height
  end
  return blocks
end