Class: Spritely::Collection
- Inherits:
-
Struct
- Object
- Struct
- Spritely::Collection
- Defined in:
- lib/spritely/collection.rb
Overview
A ‘SpriteMap` has a `Collection` that knows how to calculate the size of the sprite, based on image repetition and spacing.
Instance Attribute Summary collapse
-
#files ⇒ Object
Returns the value of attribute files.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #cache_key ⇒ Object (also: #to_s)
- #find(name) ⇒ Object
- #height ⇒ Object
- #images ⇒ Object
-
#position! ⇒ Object
Upon creation, the collection is then positioned appropriately by positioning each image within the sprite.
-
#width ⇒ Object
Returns the width of the to-be-generated sprite image.
Instance Attribute Details
#files ⇒ Object
Returns the value of attribute files
6 7 8 |
# File 'lib/spritely/collection.rb', line 6 def files @files end |
#options ⇒ Object
Returns the value of attribute options
6 7 8 |
# File 'lib/spritely/collection.rb', line 6 def @options end |
Class Method Details
.create(*args) ⇒ Object
7 8 9 10 11 |
# File 'lib/spritely/collection.rb', line 7 def self.create(*args) new(*args).tap do |collection| collection.position! end end |
Instance Method Details
#cache_key ⇒ Object Also known as: to_s
21 22 23 |
# File 'lib/spritely/collection.rb', line 21 def cache_key files.flat_map { |file_path| [Digest::MD5.hexdigest(file_path), Digest::MD5.file(file_path)] }.join end |
#find(name) ⇒ Object
17 18 19 |
# File 'lib/spritely/collection.rb', line 17 def find(name) image_sets.find { |image_set| image_set.name == name } end |
#height ⇒ Object
42 43 44 |
# File 'lib/spritely/collection.rb', line 42 def height heights.reduce(:+) end |
#images ⇒ Object
13 14 15 |
# File 'lib/spritely/collection.rb', line 13 def images image_sets.flat_map(&:images) end |
#position! ⇒ Object
Upon creation, the collection is then positioned appropriately by positioning each image within the sprite.
48 49 50 51 52 53 |
# File 'lib/spritely/collection.rb', line 48 def position! image_sets.each_with_index do |image_set, index| image_set.top = heights[0..index].reduce(:+) - image_set.outer_height image_set.position_in!(width) end end |
#width ⇒ Object
Returns the width of the to-be-generated sprite image. When none of the images repeat, it is simply the max width of all images in the sprite. When an image in the sprite is repeated, a calculation is performed based on the least common multiple of all repeated images. That least common multiple is then multiplied by the minimum multiple that will result in a value greater than or equal to the max width of all images in the sprite.
32 33 34 35 36 37 38 39 40 |
# File 'lib/spritely/collection.rb', line 32 def width @width ||= if image_sets.none?(&:repeated?) max_width else lcm = image_sets.select(&:repeated?).collect(&:width).reduce(:lcm) lcm * (max_width / lcm.to_f).ceil end end |