Class: Punk::Generator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_path = "./spritesheet.png", meta_path = "./spritesheet.csv", image_class:) ⇒ Generator

Returns a new instance of Generator.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/punks/generator.rb', line 147

def initialize( image_path="./spritesheet.png",
                meta_path="./spritesheet.csv",
                image_class: )
   @image_class  = image_class

   @sheet = Pixelart::ImageComposite.read( image_path,
                                             width: 24,
                                             height: 24 )
   recs  = CsvHash.read( meta_path )

   @recs = build_recs( recs )

   ## lookup by "case/space-insensitive" name / key
   @attributes_by_name = build_attributes_by_name( @recs )
end

Class Method Details

.normalize_gender(str) ⇒ Object



54
55
56
57
58
59
# File 'lib/punks/generator.rb', line 54

def self.normalize_gender( str )
   ## e.g. Female => f
   ##      F => f
   ##  always return f/m
   str.downcase[0]
end

.normalize_key(str) ⇒ Object

static helpers - (turn into “true” static self.class methods - why? why not?)



49
50
51
52
# File 'lib/punks/generator.rb', line 49

def self.normalize_key( str )
  ## add & e.g. B&W
   str.downcase.gsub(/[ ()&°_-]/, '').strip
end

.normalize_name(str) ⇒ Object



69
70
71
72
# File 'lib/punks/generator.rb', line 69

def self.normalize_name( str )
  ## normalize spaces in more names
  str.strip.gsub( /[ ]{2,}/, ' ' )
end

.normalize_size(str) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/punks/generator.rb', line 61

def self.normalize_size( str )
   ## e.g. U or Unisize or Univeral => u
   ##      S or Small               => s
   ##      L or Large               => l
   ##   always return u/l/s
   str.downcase[0]
end

Instance Method Details

#build_attributes_by_name(recs) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/punks/generator.rb', line 82

def build_attributes_by_name( recs )
   h = {}
   recs.each_with_index do |rec|
     names = [rec.name] + rec.more_names

     names.each do |name|
       key = normalize_key( name )
       key << "_(#{rec.gender}+#{rec.size})"  if rec.attribute?

       if h[ key ]
         puts "!!! ERROR - attribute name is not unique:"
         pp rec
         puts "duplicate:"
         pp h[key]
         exit 1
       end
       h[ key ] = rec
     end
  end
   ## pp h
   h
end

#build_recs(recs) ⇒ Object

build and normalize (meta data) records



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/punks/generator.rb', line 106

def build_recs( recs )   ## build and normalize (meta data) records

   ## sort by id
   recs = recs.sort do |l,r|
                      l['id'].to_i( 10 ) <=> r['id'].to_i( 10 )   # use base10 (decimal)
                    end

   ## assert all recs are in order by id (0 to size)
   recs.each_with_index do |rec, exp_id|
      id = rec['id'].to_i(10)
      if id != exp_id
         puts "!! ERROR -  meta data record ids out-of-order - expected id #{exp_id}; got #{id}"
         exit 1
      end
   end

   ## convert to "wrapped / immutable" kind-of struct
   recs = recs.map do |rec|
            id         = rec['id'].to_i( 10 )
            name       = normalize_name( rec['name'] )
            gender     = normalize_gender( rec['gender'] )
            size       = normalize_size( rec['size'] )
            type       = rec['type']

            more_names = (rec['more_names'] || '').split( '|' )
            more_names = more_names.map {|str| normalize_name( str ) }

            Metadata::Sprite.new(
              id:         id,
              name:       name,
              type:       type,
              gender:     gender,
              size:       size,
              more_names: more_names )
          end
   recs
end

#find(q, gender: nil, size: nil, style: nil) ⇒ Object

gender (m/f) required for attributes!!!



238
239
240
241
242
243
# File 'lib/punks/generator.rb', line 238

def find( q, gender: nil, size: nil, style: nil )  ## gender (m/f) required for attributes!!!
   rec = find_meta( q, gender: gender, size: size, style: style )

   ## return image if record found
   rec ? @sheet[ rec.id ] : nil
end

#find_meta(q, gender: nil, size: nil, style: nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/punks/generator.rb', line 172

def find_meta( q, gender: nil,
                  size: nil,
                  style: nil )   ## note: gender (m/f) required for attributes!!!

   key = normalize_key( q )  ## normalize q(uery) string/symbol

   keys = []    ## note allow lookup by more than one keys
                   ##  e.g. if gender set try   f/m first and than try unisex as fallback
   if gender
      gender = normalize_gender( gender )
      ## auto-fill size if not passed in
      ##    for f(emale)  =>   s(mall)
      ##        m(ale)    =>   l(arge)
      size =  if size.nil?
                gender == 'f' ? 's' : 'l'
              else
                normalize_size( size )
              end

      ###
      #  try (auto-add) style-specific version first (fallback to "regular" if not found)
      if style
        ## for now only support natural series
        style_key =  if style.downcase.start_with?( 'natural' )
                         'natural'
                     else
                       puts "!! ERROR - unknown attribute style #{style}; sorry"
                       exit 1
                     end

        keys << "#{key}#{style_key}_(#{gender}+#{size})"
        ## auto-add (u)niversal size as fallback
        keys << "#{key}#{style_key}_(#{gender}+u)"  if size == 's' || size == 'l'
        ## auto-add u(nisex) as fallback
        keys << "#{key}#{style_key}_(u+#{size})"    if gender == 'f' || gender == 'm'
      end


      keys << "#{key}_(#{gender}+#{size})"
      ## auto-add (u)niversal size as fallback
      keys << "#{key}_(#{gender}+u)"  if size == 's' || size == 'l'
      ## auto-add u(nisex) as fallback
      keys << "#{key}_(u+#{size})"    if gender == 'f' || gender == 'm'
   else
      keys << key
   end


   rec = nil
   keys.each do |key|
      rec = @attributes_by_name[ key ]
      if rec
        puts " lookup >#{key}< => #{rec.id}: #{rec.name} / #{rec.type} (#{rec.gender}+#{rec.size})"
        # pp rec
        break
      end
   end

   if rec.nil?
      puts "!! WARN - no lookup found for #{keys.size} key(s) >#{keys.inspect}<"
   end

   rec
end

#generate_image(*values, style: nil, patch: nil) ⇒ Object Also known as: generate



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/punks/generator.rb', line 306

def generate_image( *values, style: nil, patch: nil )
  ## check: rename patch to more/extras/foreign or ... - why? why not?

   recs = to_recs( *values, style: style, patch: patch )

   punk = @image_class.new( 24, 24 )

   recs.each do |rec|
     ## note: quick hack - allow "inline" raw images for now - why? why not?
     ##         pass through as-is
     img = if rec.is_a?( Pixelart::Image )
             rec
           else
             @sheet[ rec.id ]
           end
     punk.compose!( img )
   end

   punk
end

#normalize_gender(str) ⇒ Object



75
# File 'lib/punks/generator.rb', line 75

def normalize_gender( str )  self.class.normalize_gender( str ); end

#normalize_key(str) ⇒ Object



74
# File 'lib/punks/generator.rb', line 74

def normalize_key( str )     self.class.normalize_key( str ); end

#normalize_name(str) ⇒ Object



77
# File 'lib/punks/generator.rb', line 77

def normalize_name( str )    self.class.normalize_name( str ); end

#normalize_size(str) ⇒ Object



76
# File 'lib/punks/generator.rb', line 76

def normalize_size( str )    self.class.normalize_size( str ); end

#recordsObject Also known as: meta



168
# File 'lib/punks/generator.rb', line 168

def records() @recs; end

#spritesheetObject Also known as: sheet



164
# File 'lib/punks/generator.rb', line 164

def spritesheet() @sheet; end

#to_recs(*values, style: nil, patch: nil) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/punks/generator.rb', line 248

def to_recs( *values, style: nil, patch: nil )
     archetype_name  = values[0]

     if archetype_name.is_a?( Pixelart::Image )
        archetype = archetype_name
     elsif patch && img=patch[ normalize_key(archetype_name) ]
        archetype = img
     else ## assume it's a string
       ### todo/fix:  check for nil/not found!!!!
       ## todo/check/fix:  assert meta record returned is archetype NOT attribute!!!!
       archetype  = find_meta( archetype_name )
       if archetype.nil?
         puts "!! ERROR -  archetype >#{archetype}< not found; sorry"
         exit 1
       end
     end

     recs       = [archetype]

     ## note: attribute lookup requires gender from archetype!!!!
     if archetype.is_a?( Pixelart::Image )
        ### for now assume (support only)
        ##    large & male (l/m) for "inline/patch" archetypes - why? why not?
        ##    change male to unisex - why? why not?  (note: for now unisex is not doing a backup lookup using male/female)
        attribute_gender = 'm'
        attribute_size   = 'l'
     else
        attribute_gender = archetype.gender
        attribute_size   = archetype.size
     end

     attribute_names  = values[1..-1]
     attribute_names.each do |attribute_name|
        ## note: quick hack - allow "inline" raw images for now - why? why not?
        ##         pass through as-is
       if attribute_name.is_a?( Pixelart::Image )
         recs << attribute_name
       elsif patch && img=patch[ normalize_key(attribute_name) ]
         recs << img
       else
         rec = find_meta( attribute_name,
                          gender: attribute_gender,
                          size:   attribute_size,
                          style:  style )
         if rec.nil?
           puts "!! ERROR - attribute >#{attribute_name}< for (#{attribute_gender}+#{attribute_size}) not found; sorry"
           exit 1
         end
         recs << rec
        end
     end

     recs
end