Class: RecursiveGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ordinals/recursive/generator.rb

Constant Summary collapse

SEP_RX =
%r{[ \t/,;*+_-]+
}x

Instance Method Summary collapse

Constructor Details

#initialize(width, height, pixelate: true, inscribes:) ⇒ RecursiveGenerator

Returns a new instance of RecursiveGenerator.



4
5
6
7
8
9
10
11
# File 'lib/ordinals/recursive/generator.rb', line 4

def initialize( width, height,
                pixelate: true,
                inscribes:  )
    @width  = width
    @height = height
    @recursions  = _prepare( inscribes )
    @pixelate   = pixelate
end

Instance Method Details

#[](index) ⇒ Object

return recursion (config) as array



13
14
15
# File 'lib/ordinals/recursive/generator.rb', line 13

def [](index)  ## return recursion (config) as array
  @recursions[index]
end

#_parse(str) ⇒ Object



51
52
53
# File 'lib/ordinals/recursive/generator.rb', line 51

def _parse( str )
    str.strip.split( SEP_RX ).map { |str| str.to_i(10) }
end

#_prepare(inscribes) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ordinals/recursive/generator.rb', line 24

def _prepare( inscribes)
     ## prepare (flatten) recursion lookup table indexed 0,1,2,3, etc.
     index = []
     inscribes.each do |rec|
        if rec.is_a?( Array )  ## assume spritesheet 
           id = rec[0]
           opts = rec[1]
           cols = opts[:width] / width
           rows = opts[:height] / height
           count = cols*rows
           count.times do |num| 
             index << [id, {  spritesheet: [opts[:width],opts[:height]],
                              num: num }
                      ]
           end
        else ## assume "standalone / one-by-one" inscribe
           id = rec
           index << [id, {}]  
        end
     end
    index
end

#countObject Also known as: size



17
# File 'lib/ordinals/recursive/generator.rb', line 17

def count() @recursions.size; end

#generate(*args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ordinals/recursive/generator.rb', line 55

def generate( *args )
   g = if args.size==1 && args.is_a?( String )
          _parse( args[0] )
       else  ## assume integer numbers
          args
       end
   img = RecursiveImage.new( @width, @height )
   g.each do |num|
      id, opts = @recursions[num]
      img << [id, opts.merge( {pixelate: @pixelate} )]
   end
   img 
end

#heightObject



21
# File 'lib/ordinals/recursive/generator.rb', line 21

def height() @height; end

#widthObject



20
# File 'lib/ordinals/recursive/generator.rb', line 20

def width() @width; end