Class: Pixelart::Generator
- Inherits:
-
Object
- Object
- Pixelart::Generator
- Defined in:
- lib/pixelart/generator.rb
Class Method Summary collapse
-
.normalize_key(str) ⇒ Object
static helpers - (turn into “true” static self.class methods - why? why not?).
- .normalize_name(str) ⇒ Object
Instance Method Summary collapse
- #build_attributes_by_name(recs) ⇒ Object
-
#build_recs(recs) ⇒ Object
build and normalize (meta data) records.
- #find(q) ⇒ Object
- #find_meta(q) ⇒ Object
- #generate_image(*values, background: nil, before: nil) ⇒ Object (also: #generate)
-
#initialize(image_path = "./spritesheet.png", meta_path = "./spritesheet.csv", width: 24, height: 24) ⇒ Generator
constructor
A new instance of Generator.
- #normalize_key(str) ⇒ Object
- #normalize_name(str) ⇒ Object
- #records ⇒ Object (also: #meta)
- #spritesheet ⇒ Object (also: #sheet)
- #to_recs(*values) ⇒ Object
Constructor Details
#initialize(image_path = "./spritesheet.png", meta_path = "./spritesheet.csv", width: 24, height: 24) ⇒ Generator
Returns a new instance of Generator.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/pixelart/generator.rb', line 106 def initialize( image_path="./spritesheet.png", ="./spritesheet.csv", width: 24, height: 24 ) @width = width @height = height @sheet = ImageComposite.read( image_path, width: @width, height: @height ) recs = CsvHash.read( ) @recs = build_recs( recs ) ## lookup by "case/space-insensitive" name / key @attributes_by_name = build_attributes_by_name( @recs ) end |
Class Method Details
.normalize_key(str) ⇒ Object
static helpers - (turn into “true” static self.class methods - why? why not?)
32 33 34 35 36 |
# File 'lib/pixelart/generator.rb', line 32 def self.normalize_key( str ) ## add & e.g. B&W ## add ' e.g. McDonald's Red str.downcase.gsub(/[ ()&°'_-]/, '').strip end |
.normalize_name(str) ⇒ Object
38 39 40 41 |
# File 'lib/pixelart/generator.rb', line 38 def self.normalize_name( str ) ## normalize spaces in more names str.strip.gsub( /[ ]{2,}/, ' ' ) end |
Instance Method Details
#build_attributes_by_name(recs) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pixelart/generator.rb', line 48 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 ) 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/pixelart/generator.rb', line 71 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'] ) 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, more_names: more_names ) end recs end |
#find(q) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/pixelart/generator.rb', line 146 def find( q ) rec = ( q ) ## return image if record found rec ? @sheet[ rec.id ] : nil end |
#find_meta(q) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/pixelart/generator.rb', line 133 def ( q ) key = normalize_key( q ) ## normalize q(uery) string/symbol rec = @attributes_by_name[ key ] if rec puts " lookup >#{key}< => #{rec.id}: #{rec.name} / #{rec.type}" else puts "!! WARN - no lookup found for key >#{key}<" end rec end |
#generate_image(*values, background: nil, before: nil) ⇒ Object Also known as: generate
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 |
# File 'lib/pixelart/generator.rb', line 173 def generate_image( *values, background: nil, before: nil ) ## note: generate_image NO longer supports ## - generate by integer number (indexes), sorry recs = to_recs( *values ) ## note: first construct/generate image on transparent background ## add background if present as LAST step img = Image.new( @width, @height ) recs.each do |rec| ## note: before call(back) MUST change image INPLACE!!!! before.call( img, rec ) if before img.compose!( @sheet[ rec.id ] ) end if background ## for now assume background is (simply) color img2 = Image.new( @width, @height ) img2.compose!( Image.new( @width, @height, background ) ) img2.compose!( img ) img = img2 end img end |
#normalize_key(str) ⇒ Object
43 |
# File 'lib/pixelart/generator.rb', line 43 def normalize_key( str ) self.class.normalize_key( str ); end |
#normalize_name(str) ⇒ Object
44 |
# File 'lib/pixelart/generator.rb', line 44 def normalize_name( str ) self.class.normalize_name( str ); end |
#records ⇒ Object Also known as: meta
127 |
# File 'lib/pixelart/generator.rb', line 127 def records() @recs; end |
#spritesheet ⇒ Object Also known as: sheet
123 |
# File 'lib/pixelart/generator.rb', line 123 def spritesheet() @sheet; end |
#to_recs(*values) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/pixelart/generator.rb', line 154 def to_recs( *values ) recs = [] attribute_names = values attribute_names.each do |attribute_name| attribute = ( attribute_name ) if attribute.nil? puts "!! ERROR - attribute >#{attribute_name}< not found; sorry" exit 1 end recs << attribute end recs end |