Class: TokenCollection
- Inherits:
-
Artbase::Base
- Object
- Artbase::Base
- TokenCollection
- Defined in:
- lib/artbase-cocos/collection/token.rb,
lib/artbase-cocos/collection/token_meta.rb
Defined Under Namespace
Classes: Meta
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Class Method Summary collapse
Instance Method Summary collapse
-
#_parse_dimension(str) ⇒ Object
e.g.
-
#_range(offset: 0) ⇒ Object
return “default” range - make “private” helper public - why? why not?.
-
#download_images(range = _range, force: false, direct: @image_base ? true : false) ⇒ Object
note: default to direct true if image_base present/availabe otherwise to false todo/check: change/rename force para to overwrite - why? why not?.
- #download_meta(range = _range, force: false) ⇒ Object
- #each_image(range = _range, exclude: true, &blk) ⇒ Object
- #each_meta(range = _range, exclude: true, &blk) ⇒ Object
- #image_url(id:, direct: @image_base ? true : false) ⇒ Object
-
#initialize(slug, count, token_base:, image_base: nil, image_base_id_format: nil, format:, source:, top_x: 0, top_y: 0, center_x: true, center_y: true, excludes: [], offset: 0) ⇒ TokenCollection
constructor
A new instance of TokenCollection.
- #meta_url(id:) ⇒ Object (also: #token_url)
- #pixelate(range = _range, exclude: true, force: false, debug: false, zoom: nil, faster: false) ⇒ Object
Methods inherited from Artbase::Base
#_normalize_trait_type, #_normalize_trait_value, #calc_attribute_counters, #convert_images, #dump_attributes, #export_attributes, #make_composite, #make_strip
Constructor Details
#initialize(slug, count, token_base:, image_base: nil, image_base_id_format: nil, format:, source:, top_x: 0, top_y: 0, center_x: true, center_y: true, excludes: [], offset: 0) ⇒ TokenCollection
Returns a new instance of TokenCollection.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/artbase-cocos/collection/token.rb', line 30 def initialize( slug, count, token_base:, image_base: nil, image_base_id_format: nil, format:, source:, top_x: 0, top_y: 0, center_x: true, center_y: true, excludes: [], offset: 0 ) # check: rename count to items or such - why? why not? @slug = slug @count = count @offset = offset ## starting by default at 0 (NOT 1 or such) @token_base = token_base @image_base = image_base @image_base_id_format = image_base_id_format @width, @height = _parse_dimension( format ) ## note: allow multiple source formats / dimensions ### e.g. convert 512x512 into [ [512,512] ] ## source = [source] unless source.is_a?( Array ) @sources = source.map { |dimension| _parse_dimension( dimension ) } @top_x = top_x ## more (down)sampling / pixelate options @top_y = top_y @center_x = center_x @center_y = center_y @excludes = excludes end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
28 29 30 |
# File 'lib/artbase-cocos/collection/token.rb', line 28 def count @count end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
28 29 30 |
# File 'lib/artbase-cocos/collection/token.rb', line 28 def slug @slug end |
Class Method Details
.read(path) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/artbase-cocos/collection/token.rb', line 5 def self.read( path ) puts "==> reading collection config >#{path}<..." config = read_yaml( path ) ## note: ## allow sources to be empty - use [] quick hack for now - why? why not? ## allow count to be empty - use 999 quick hack for now - why? why not? ## new( config['slug'], config['count'] || 999, token_base: config['token_base'], image_base: config['image_base'], format: config['format'], source: config['source'] || [], offset: config['offset'] || 0 ) end |
Instance Method Details
#_parse_dimension(str) ⇒ Object
e.g. convert dimension (width x height) “24x24” or “24 x 24” to [24,24]
69 70 71 |
# File 'lib/artbase-cocos/collection/token.rb', line 69 def _parse_dimension( str ) str.split( /x/i ).map { |str| str.strip.to_i } end |
#_range(offset: 0) ⇒ Object
return “default” range - make “private” helper public - why? why not?
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/artbase-cocos/collection/token.rb', line 74 def _range( offset: 0 ) ## return "default" range - make "private" helper public - why? why not? ## note: range uses three dots (...) exclusive (NOT inclusive) range ## e.g. 0...100 => [0,..,99] ## 1...101 => [1,..,100] ## ## note: allow offset argument ## (to start with different offset - note: in addition to builtin 0/1 offset) (0+@offset+offset...@count+@offset) end |
#download_images(range = _range, force: false, direct: @image_base ? true : false) ⇒ Object
note: default to direct true if image_base present/availabe
otherwise to false
todo/check: change/rename force para to overwrite - why? why not?
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/artbase-cocos/collection/token.rb', line 306 def download_images( range=_range, force: false, direct: @image_base ? true : false ) start = Time.now delay_in_s = 0.3 range.each do |id| ## note: skip if (downloaded) file already exists skip = false if !force ['png', 'gif', 'jgp', 'svg'].each do |format| if File.exist?( "./#{@slug}/token-i/#{id}.#{format}" ) skip = true break end end end next if skip image_src = image_url( id: id, direct: direct ) ## note: will auto-add format file extension (e.g. .png, .jpg) ## depending on http content type!!!!! start_copy = Time.now copy_image( image_src, "./#{@slug}/token-i/#{id}" ) stop = Time.now diff = stop - start_copy puts " download image in #{diff} sec(s)" diff = stop - start mins = diff / 60 ## todo - use floor or such? secs = diff % 60 puts "up #{mins} mins #{secs} secs (total #{diff} secs)" puts "sleeping #{delay_in_s}s..." sleep( delay_in_s ) end end |
#download_meta(range = _range, force: false) ⇒ Object
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 |
# File 'lib/artbase-cocos/collection/token.rb', line 271 def ( range=_range, force: false ) start = Time.now delay_in_s = 0.3 range.each do |id| outpath = "./#{@slug}/token/#{id}.json" if !force && File.exist?( outpath ) next ## note: skip if file already exists end dirname = File.dirname( outpath ) FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname ) puts "==> #{id} - #{@slug}..." token_src = ( id: id ) copy_json( token_src, outpath ) stop = Time.now diff = stop - start puts " download token metadata in #{diff} sec(s)" mins = diff / 60 ## todo - use floor or such? secs = diff % 60 puts "up #{mins} mins #{secs} secs (total #{diff} secs)" puts "sleeping #{delay_in_s}s..." sleep( delay_in_s ) end end |
#each_image(range = _range, exclude: true, &blk) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/artbase-cocos/collection/token.rb', line 87 def each_image( range=_range, exclude: true, &blk ) range.each do |id| #### # filter out/skip # if exclude && @excludes.include?( id ) # puts " skipping / exclude #{id}..." # next # end puts "==> #{id}" img = Image.read( "./#{@slug}/#{@width}x#{@height}/#{id}.png" ) blk.call( img, id ) end end |
#each_meta(range = _range, exclude: true, &blk) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/artbase-cocos/collection/token.rb', line 104 def ( range=_range, exclude: true, &blk ) range.each do |id| ## check: change/rename id to index - why? why not? = Meta.read( "./#{@slug}/token/#{id}.json" ) #### # filter out/skip # if exclude && @excludes.include?( meta.name ) # puts " skipping / exclude #{id} >#{meta.name}<..." # next # end blk.call( , id ) end end |
#image_url(id:, direct: @image_base ? true : false) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/artbase-cocos/collection/token.rb', line 238 def image_url( id:, direct: @image_base ? true : false ) src = if direct && @image_base ### ## todo/fix: ## change image_base_id_format ## to image_base proc with para id and call proc!!!! if @image_base_id_format @image_base.gsub( '{id}', @image_base_id_format % id ) else @image_base.gsub( '{id}', id.to_s ) end else ## todo/check - change/rename data to meta - why? why not? data = Meta.read( "./#{@slug}/token/#{id}.json" ) = data.name = data.image puts "==> #{id} - #{@slug}..." puts " name: #{}" puts " image: #{}" end src ## quick ipfs (interplanetary file system) hack - make more reusabele!!! src = handle_ipfs( src ) src end |
#meta_url(id:) ⇒ Object Also known as: token_url
228 229 230 231 232 233 234 |
# File 'lib/artbase-cocos/collection/token.rb', line 228 def ( id: ) src = @token_base.gsub( '{id}', id.to_s ) ## quick ipfs (interplanetary file system) hack - make more reusabele!!! src = handle_ipfs( src ) src end |
#pixelate(range = _range, exclude: true, force: false, debug: false, zoom: nil, faster: false) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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 |
# File 'lib/artbase-cocos/collection/token.rb', line 124 def pixelate( range=_range, exclude: true, force: false, debug: false, zoom: nil, faster: false ) range.each do |id| if exclude && @excludes.include?( id ) puts " skipping #{id}; listed in excludes #{@excludes.inspect}" next end outpath = "./#{@slug}/#{@width}x#{@height}/#{id}.png" if !force && File.exist?( outpath ) next ## note: skip if file already exists end center_x = if @center_x.is_a?( Proc ) then @center_x.call( id ); else @center_x; end center_y = if @center_y.is_a?( Proc ) then @center_y.call( id ); else @center_y; end puts "==> #{id} - reading / decoding #{id} ..." if faster ## note: faster for now only supports ## single /one source format ## always will use first source format from array for now cmd = "./pixelator " cmd << "./#{@slug}/token-i/#{id}.png" cmd << " " + @sources[0][0].to_s cmd << " " + @sources[0][1].to_s cmd << " " + outpath cmd << " " + @width.to_s cmd << " " + @height.to_s puts "==> #{cmd}..." ret = system( cmd ) if ret puts "OK" else puts "!! FAIL" if ret.nil? puts " command not found" else puts " exit code: #{$?}" end end else start = Time.now img = Image.read( "./#{@slug}/token-i/#{id}.png" ) stop = Time.now diff = stop - start puts " in #{diff} sec(s)\n" source = nil @sources.each do |source_width, source_height| if img.width == source_width && img.height == source_height source = [source_width, source_height] break end end if source source_width = source[0] source_height = source[1] steps_x = Image.calc_sample_steps( source_width-@top_x, @width, center: center_x ) steps_y = Image.calc_sample_steps( source_height-@top_y, @height, center: center_y ) pix = if debug img.pixelate_debug( steps_x, steps_y, top_x: @top_x, top_y: @top_y ) else img.pixelate( steps_x, steps_y, top_x: @top_x, top_y: @top_y ) end ## todo/check: keep usingu slug e.g. 0001.png or "plain" 1.png - why? why not? ## slug = "%04d" % id pix.save( outpath ) if zoom outpath = "./#{@slug}/#{@width}x#{@height}/#{id}@#{zoom}x.png" pix.zoom( zoom ).save( outpath ) end else puts "!! ERROR - unknown/unsupported dimension - #{img.width}x#{img.height}; sorry - tried:" pp @sources exit 1 end end end end |