class Mooncats::Design

Public Class Methods

find( num ) click to toggle source
# File lib/mooncats/design.rb, line 6
def self.find( num )   ## pass in design index number (0 to 127)
  ## todo: add cache (memoize) - why? why not?
  design = parse( DESIGNS[ num ] )

  puts "    design ##{num} (#{design.width}x#{design.height})"
  ## pp design.data
  ## puts

  design
end
new( data ) click to toggle source
# File lib/mooncats/design.rb, line 64
def initialize( data )
 @data = data
end
parse( str ) click to toggle source
# File lib/mooncats/design.rb, line 23
def self.parse( str )
  ## support original "classic" compact single-line format
  ##   e.g. 00011111100000000.01113333310000000.13533333331110000....
  ##  note: this format needs to get rotated by 90 degree!!!
  if str.start_with?( /[0-5]+\.[0-5]+\.[0-5]+/ )  ## quick (and dirty) heuristic check
    data = str.split('.')

    ## note: map colors encoded as a string to an array of integers - why? why not?
    ##  e.g. "00011111133344411"
    ##          =>
    ##       [0,0,0,1,1,1,1,1,1,3,3,3,4,4,4,1,1]
    data = data.map do |row|
             row.chars.map do |color|
                color.to_i
             end
           end
    data = data.transpose   ## note: rotate by 90 degree!!!!!
  else  ## assume "modern" pixelart format
     ## todo/check: delegate to pixelart parse or such - why? why not?

     data = []
     str.each_line do |line|
       line = line.strip
       next if line.empty?               ## skipping empty line in pixel art source
       next if line.start_with?( '#' )   ## skipping comment line in pixel art source

       ## note: allow multiple spaces or tabs to separate pixel codes
       data << line.split( /[ \t]+/)
     end
     ## todo/check: change to use strings (instead of nummbers) in the future?
     ##                 why? why not?  stay "compatible" to pixel art format/machinery?
     data = data.map do |row|
       row.map do |color|
          color.to_i
       end
     end
  end
  new( data )
end
read( path ) click to toggle source
# File lib/mooncats/design.rb, line 18
def self.read( path )
  text = File.open( path, 'r:utf-8') { |f| f.read }
  parse( text )
end

Public Instance Methods

each( &blk ) click to toggle source
# File lib/mooncats/design.rb, line 78
def each( &blk )
  @data.each { |row| blk.call( row ) }
end
each_with_index( &blk ) click to toggle source
# File lib/mooncats/design.rb, line 75
def each_with_index( &blk )
  @data.each_with_index { |row, y| blk.call( row, y ) }
end
height() click to toggle source
# File lib/mooncats/design.rb, line 72
def height() @data.size; end
to_txt() click to toggle source
# File lib/mooncats/design.rb, line 84
def to_txt
  buf = String.new('')

  @data.each do |row|
    buf << row.join( ' ' )
    buf << "\n"
  end
  buf
end
width() click to toggle source
# File lib/mooncats/design.rb, line 68
def width
  ## todo/check: use/find max - why? why not? lets you you used "unbalanced" / shortcut lines too
  @data[0].size
end