spritesheet 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8511728b85f60e17547179b2642b1b98789b778d454e714d29911fbabb07f456
4
- data.tar.gz: d7330fd8a0ddc18fcfe5a04a0c914e5c1b50548058e83e6fb23e8cb6924a7370
3
+ metadata.gz: 7b39cad35371c607d82e460f957ae49e6465bba2617b5c193b617dd0d1146fa6
4
+ data.tar.gz: dc94f8002443ab0c82620b4e3792832255511b27b91e5808483629b139ab5427
5
5
  SHA512:
6
- metadata.gz: 6aa47190ec3a45db1c3c0ce7b51a935124dfae0eb90a991d6dcbb0583748ec188b2c6fa7d43a2ad9f9fb37cce81302e61482ac975023c99b530f9e8508d1776d
7
- data.tar.gz: 6a0c4033a797fe33a2fa1e7cf050abfd0d33089424bb1bb93b95a2c5bbbfcc69b618a25545b10527577ed259bbbae28abbe231bc67d62f203131fa3e392a7c2c
6
+ metadata.gz: 9fa1624258eca260dfe601da139a11ce36ce21b832018c0c43fd1852b0daad087766f1b3bf48c3598481303ebac083e85a4fdbb7d85b3c745cd8ba7d8705e103
7
+ data.tar.gz: f4e08a4d6cfbc419d92345151a444d06ad21183941fde4537bccf1cc3340ce0dfe2040c7a638c3fb1bf661c8841fd36ab8bdc32a17c74573ef2b768f5a3d8560
data/Manifest.txt CHANGED
@@ -3,4 +3,5 @@ Manifest.txt
3
3
  README.md
4
4
  Rakefile
5
5
  lib/spritesheet.rb
6
+ lib/spritesheet/spritesheet.rb
6
7
  lib/spritesheet/version.rb
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ end
11
11
 
12
12
  Hoe.spec 'spritesheet' do
13
13
 
14
- self.version = Spritesheet::VERSION
14
+ self.version = Pixelart::Module::Spritesheet::VERSION
15
15
 
16
16
  self.summary = "spritesheet"
17
17
  self.description = summary
@@ -0,0 +1,181 @@
1
+
2
+ module Pixelart
3
+
4
+ class Metadata
5
+ class Sprite ## todo/check: rename/change type to category - why? why not?
6
+ attr_reader :id, :name, :type, :more_names
7
+
8
+ def initialize( id:,
9
+ name:,
10
+ type:,
11
+ more_names: [] )
12
+ @id = id # zero-based index eg. 0,1,2,3, etc.
13
+ @name = name
14
+ @type = type
15
+ @more_names = more_names
16
+ end
17
+ end # class Metadata::Sprite
18
+ end # class Metadata
19
+
20
+
21
+ class Spritesheet
22
+
23
+ ######
24
+ # static helpers - (turn into "true" static self.class methods - why? why not?)
25
+ #
26
+ def self.normalize_key( str )
27
+ ## add & e.g. B&W
28
+ ## add ' e.g. McDonald's Red
29
+ str.downcase.gsub(/[ ()&°'_-]/, '').strip
30
+ end
31
+
32
+ def self.normalize_name( str )
33
+ ## normalize spaces in more names
34
+ str.strip.gsub( /[ ]{2,}/, ' ' )
35
+ end
36
+
37
+ def normalize_key( str ) self.class.normalize_key( str ); end
38
+ def normalize_name( str ) self.class.normalize_name( str ); end
39
+
40
+
41
+
42
+ def self._build_recs( recs ) ## build and normalize (meta data) records
43
+ ## sort by id
44
+ recs = recs.sort do |l,r|
45
+ l['id'].to_i( 10 ) <=> r['id'].to_i( 10 ) # use base10 (decimal)
46
+ end
47
+
48
+ ## assert all recs are in order by id (0 to size)
49
+ recs.each_with_index do |rec, exp_id|
50
+ id = rec['id'].to_i(10)
51
+ if id != exp_id
52
+ puts "!! ERROR - meta data record ids out-of-order - expected id #{exp_id}; got #{id}"
53
+ exit 1
54
+ end
55
+ end
56
+
57
+ ## convert to "wrapped / immutable" kind-of struct
58
+ recs = recs.map do |rec|
59
+ id = rec['id'].to_i( 10 )
60
+ name = normalize_name( rec['name'] )
61
+
62
+ ## todo: double check if rec['type'] returns nil or empty string if column/field not present?
63
+ type = rec['type'] || rec['category']
64
+
65
+ more_names = (rec['more_names'] || '').split( '|' )
66
+ more_names = more_names.map {|str| normalize_name( str ) }
67
+
68
+ Metadata::Sprite.new(
69
+ id: id,
70
+ name: name,
71
+ type: type,
72
+ more_names: more_names )
73
+ end
74
+ recs
75
+ end # method build_recs
76
+
77
+
78
+ def self.read_records( path )
79
+ recs = CsvHash.read( path )
80
+ _build_recs( recs )
81
+ end
82
+ class << self
83
+ alias_method :read_meta, :read_records
84
+ end
85
+
86
+
87
+ def self.read( image_path="./spritesheet.png",
88
+ meta_path="./spritesheet.csv",
89
+ width: 24,
90
+ height: 24)
91
+ img = ImageComposite.read( image_path, width: width, height: height )
92
+ recs = read_records( meta_path )
93
+
94
+ new( img, recs, width: width, height: height )
95
+ end
96
+
97
+
98
+
99
+ def initialize( img,
100
+ recs,
101
+ width: 24,
102
+ height: 24 )
103
+ @width = width
104
+ @height = height
105
+
106
+ ## todo: check if img is a ImageComposite or plain Image?
107
+ ## if plain Image "auto-wrap" into ImageComposite - why? why not?
108
+ @image = img
109
+ @recs = recs
110
+
111
+ ## lookup by "case/space-insensitive" name / key
112
+ @attributes_by_name = _build_attributes_by_name( @recs )
113
+ end
114
+
115
+
116
+ def image() @image; end
117
+ alias_method :composite, :image # add some more aliases/alt names - why? why not?
118
+
119
+ def records() @recs; end
120
+ alias_method :meta, :records
121
+
122
+
123
+ def find_meta_by( name: )
124
+ key = normalize_key( name ) ## normalize q(uery) string/symbol
125
+
126
+ rec = @attributes_by_name[ key ]
127
+ if rec
128
+ puts " lookup >#{key}< => #{rec.id}: #{rec.name} / #{rec.type}"
129
+ else
130
+ puts "!! WARN - no lookup found for key >#{key}<"
131
+ end
132
+
133
+ rec
134
+ end
135
+
136
+ def find_by( name: )
137
+ rec = find_meta_by( name: name )
138
+
139
+ ## return (cropped/sliced) image from image composite if record found
140
+ rec ? @image[ rec.id ] : nil
141
+ end
142
+
143
+ def _find( name ) find_by( name: name ); end
144
+ alias_method :[], :_find
145
+
146
+
147
+ ######
148
+ ## helpers
149
+ def _build_attributes_by_name( recs )
150
+ h = {}
151
+ recs.each_with_index do |rec|
152
+ names = [rec.name] + rec.more_names
153
+
154
+ names.each do |name|
155
+ key = normalize_key( name )
156
+
157
+ if h[ key ]
158
+ puts "!!! ERROR - attribute name is not unique:"
159
+ pp rec
160
+ puts "duplicate:"
161
+ pp h[key]
162
+ exit 1
163
+ end
164
+ h[ key ] = rec
165
+ end
166
+ end
167
+ ## pp h
168
+ h
169
+ end
170
+
171
+ end # class Spritesheet
172
+
173
+
174
+
175
+
176
+ ## add convenience (alternate spelling) alias - why? why not?
177
+ SpriteSheet = Spritesheet
178
+ Sheet = Spritesheet
179
+ Sprite = Spritesheet
180
+
181
+ end # module Pixelart
@@ -1,10 +1,11 @@
1
1
 
2
-
2
+ module Pixelart
3
+ module Module
3
4
  module Spritesheet
4
5
 
5
6
  MAJOR = 1
6
7
  MINOR = 0
7
- PATCH = 0
8
+ PATCH = 1
8
9
  VERSION = [MAJOR,MINOR,PATCH].join('.')
9
10
 
10
11
  def self.version
@@ -20,4 +21,5 @@ module Spritesheet
20
21
  end
21
22
 
22
23
  end # module Spritesheet
23
-
24
+ end # module Module
25
+ end # module Pixelart
data/lib/spritesheet.rb CHANGED
@@ -5,8 +5,11 @@ require 'pixelart/base'
5
5
 
6
6
  ## our own code
7
7
  require 'spritesheet/version' # note: let version always go first
8
+ require 'spritesheet/spritesheet'
8
9
 
9
10
 
10
11
 
11
12
 
12
- puts Spritesheet.banner # say hello
13
+
14
+ puts Pixelart::Module::Spritesheet.banner # say hello
15
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spritesheet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
@@ -72,6 +72,7 @@ files:
72
72
  - README.md
73
73
  - Rakefile
74
74
  - lib/spritesheet.rb
75
+ - lib/spritesheet/spritesheet.rb
75
76
  - lib/spritesheet/version.rb
76
77
  homepage: https://github.com/pixelartexchange/pixelart
77
78
  licenses: