Class: Bitgen::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/bitgen.rb

Overview

change/rename to Spritesheet / Atlas / Deploy or such - why? why not?

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta: {}, attributes: {}) ⇒ Catalog

Returns a new instance of Catalog.



42
43
44
45
46
# File 'lib/bitgen.rb', line 42

def initialize( meta: {},
                attributes: {} )
  @meta = meta
  @attributes = attributes
end

Class Method Details

.read(path) ⇒ Object



7
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
# File 'lib/bitgen.rb', line 7

def self.read( path )
  data = read_json( path  )
  # pp data

  meta = {
     'slug' => data['slug'].downcase,  ## note: always downcase slug for now (e.g. OrdinalsEgg => ordinalsegg)
     'name' => data['name'],
     'dim'  => data['dim'] 
  }
 
  attributes = {}

  ## note: use/access via trait_types array
  ##         for (restoring  insertion) order!!!
  data['trait_types'].each do |category_name|
    h = data['traits'][category_name]
    puts "==> #{category_name} - #{h.size} record(s)..."

    h.each_with_index do |(name, h), i|
       puts "   #{i} - #{name} (#{h['name']}):"

       img = Image.parse_base64(  h['base64'] )

       ## note: always downcase category_name for now 
       ##    (e.g. Background => background  in OrdinalsEgg for example)
       cat = attributes[ category_name.downcase ] ||= {}
       cat[ name ] = img
    end
  end


  new( meta: meta,
       attributes: attributes )
end

Instance Method Details

#[](name) ⇒ Object



57
# File 'lib/bitgen.rb', line 57

def [](name) @attributes[name]; end

#categoriesObject



56
# File 'lib/bitgen.rb', line 56

def categories()  @attributes.keys; end

#cheatObject

print cheat sheet



59
60
61
62
63
64
65
66
67
68
# File 'lib/bitgen.rb', line 59

def cheat  ## print cheat sheet   
  buf = '' 
  @attributes.each_with_index do |(category_name, cat),i|
      buf << "#{i} - #{category_name} (#{cat.size})\n"
      cat.each_with_index do |(attribute_name, _),j|
          buf << "    #{j} - #{attribute_name}\n"
      end
  end
  buf
end

#dimObject



51
# File 'lib/bitgen.rb', line 51

def dim()  @meta['dim']; end

#exportObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bitgen.rb', line 70

def export
  @attributes.each_with_index do |(category_name, cat),i|
    puts "==> #{category_name} - #{cat.size} record(s)..."
    cat.each_with_index do |(attribute_name, img), j|
      puts "   #{j} - #{attribute_name} (#{img.width}x#{img.height})"
  
      path = "./tmp/#{slug}/#{i}_#{category_name}/#{j}_#{attribute_name}"
      puts path

      img.save( path+".png" )
      ## note - only auto-add zoom  if  width < 100px
      ##  e.g. sathoshi's pet is 630px
      img.zoom(8).save( path+"@8x.png" )  if img.width < 100
    end
  end
end

#heightObject



53
# File 'lib/bitgen.rb', line 53

def height()  @meta['dim'] ? @meta['dim'][1] : nil; end

#nameObject



48
# File 'lib/bitgen.rb', line 48

def name() @meta['name']; end

#slugObject



49
# File 'lib/bitgen.rb', line 49

def slug() @meta['slug']; end

#widthObject



52
# File 'lib/bitgen.rb', line 52

def width()  @meta['dim'] ? @meta['dim'][0] : nil; end