-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathspritesheet.rb
91 lines (58 loc) · 2.04 KB
/
spritesheet.rb
1
2
3
4
5
6
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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
####################
# assemble spritesheet (all-in-one composite image with attributes)
#
# to run use:
# $ ruby spritesheet/spritesheet.rb
require 'pixelart'
attributes = CsvHash.read( "./spritesheet/attributes.csv" )
puts " #{attributes.size} record(s)" #=> 82 record(s)
cols = 20
rows = (attributes.size/cols)
rows +=1 if attributes.size % cols != 0
spritesheet = ImageComposite.new( cols, rows,
width: 42,
height: 42 )
meta = [] ## output meta(data) records
####
# add attributes
attributes.each do |rec|
parts = rec['path'].split( '/')
## auto-add /attributes directory (after first part)
## e.g. moonbirds/headwear/beanie.png =>
## moonbirds/attributes/beanie.png
path = './' + parts[0] + '/attributes/' + parts[1..-1].join('/')
spritesheet << Image.read( path )
name = rec['name']
more_names = (rec['more_names'] || '').split( '|' )
if name.nil? || name.empty?
## try to auto-fill name for path
filename = parts[-1]
basename = File.basename(filename, File.extname(filename))
dirname = parts[-2]
collection = parts[0]
basename = basename.sub( 'body-I-', '' )
basename = basename.sub( 'beak-I-', '' )
basename = basename.sub( 'eyes-I-', '' )
name = basename
end
names = [name] + more_names
## normalize spaces in more names
names = names.map {|str| str.strip.gsub(/[ ]{2,}/, ' ' )}
meta << [meta.size,
names[0],
'?', ## note: type to done (later)
names[1..-1].join( ' | '),
]
end
spritesheet.save( "./tmp/spritesheet.png" )
spritesheet.zoom(2).save( "./tmp/[email protected]" )
headers = ['id', 'name', 'type', 'more_names']
File.open( "./tmp/spritesheet.csv", "w:utf-8" ) do |f|
f.write( headers.join( ', '))
f.write( "\n")
meta.each do |values|
f.write( values.join( ', ' ))
f.write( "\n" )
end
end
puts "bye"