class Tily::Tily

Attributes

background_color[RW]
output_path[RW]
raw_image[RW]
ts[RW]

Public Class Methods

new(opts) click to toggle source
   # File lib/tily.rb
16 def initialize opts
17         img_path = opts[:img_path]
18         output_path = opts[:output_path]
19         unit_size = opts[:unit_size] || 256
20         background_color = opts[:bg_color] || "grey"
21 
22         @raw_image = ImageList.new img_path
23         @output_path = output_path
24         @ts = TileSystem.new unit_size
25         @ts.read_raw_dimension @raw_image.columns, @raw_image.rows
26         @background_color = background_color
27 
28         FileUtils.mkdir_p @output_path unless Dir.exists? @output_path
29 end

Public Instance Methods

gen_tiles() click to toggle source
   # File lib/tily.rb
31 def gen_tiles
32         norm_size = @ts.normalized_size
33         base_img = Image.new(norm_size, norm_size) { self.background_color = "grey" }
34         base_img = base_img.composite(@raw_image, GravityType::CenterGravity, CompositeOperator::CopyCompositeOp)
35 
36         puts "Generating meta-data..."
37         File.open("#{@output_path}/meta.json", "w") {|f| f.write(@ts.meta.to_json) }
38         puts "Done."
39 
40         puts "Generating images..."
41 
42         @ts.each_level do |level|
43                 puts "Level #{level}:"
44 
45                 level_folder = "#{@output_path}/#{level}"
46                 FileUtils.mkdir_p level_folder unless Dir.exists? level_folder
47 
48                 level_size = @ts.level_size level
49                 level_img  = base_img.resize level_size, level_size
50 
51                 @ts.each_tile_with_index(level) do |x, y, index|
52                         tile_img = level_img.crop(@ts.tile_offset(x), @ts.tile_offset(y), @ts.unit_size, @ts.unit_size)
53                         tile_img.write("#{level_folder}/#{@ts.quadkey(level, x, y)}.png")
54                 end
55 
56                 puts "Done."
57         end
58 
59         puts "Tiles generated to folder '#{@output_path}'..."
60 end

Private Instance Methods

processing_hint_str(number, level) click to toggle source

Format a beautiful hint string

   # File lib/tily.rb
64 def processing_hint_str number, level
65         total = (@ts.tile_size(level) ** 2).to_s
66         now   = (number + 1).to_s.rjust(total.length, " ")
67         "#{now}/#{total}"
68 end