Class: Spritely::ImageSet
- Inherits:
-
Object
- Object
- Spritely::ImageSet
- Defined in:
- lib/spritely/image_set.rb
Overview
Each image in the sprite maps to an instance of ‘ImageSet` that stores the image data, width, height, and outer positioning.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#top ⇒ Object
Returns the value of attribute top.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #images ⇒ Object
-
#initialize(path, options) ⇒ ImageSet
constructor
A new instance of ImageSet.
- #name ⇒ Object
- #outer_height ⇒ Object
-
#position_in!(collection_width) ⇒ Object
When positioned in the sprite, we must take into account whether the image is configured to repeat, or is positioned to the right-hand side of the sprite map.
- #repeated? ⇒ Boolean
- #right? ⇒ Boolean
- #spacing_above ⇒ Object
- #spacing_below ⇒ Object
Constructor Details
#initialize(path, options) ⇒ ImageSet
Returns a new instance of ImageSet.
10 11 12 13 14 15 16 |
# File 'lib/spritely/image_set.rb', line 10 def initialize(path, ) @path = path @options = @data = File.read(path) @width, @height = data[0x10..0x18].unpack('NN') @left = 0 end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/spritely/image_set.rb', line 8 def data @data end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
8 9 10 |
# File 'lib/spritely/image_set.rb', line 8 def height @height end |
#left ⇒ Object (readonly)
Returns the value of attribute left.
8 9 10 |
# File 'lib/spritely/image_set.rb', line 8 def left @left end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/spritely/image_set.rb', line 8 def @options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/spritely/image_set.rb', line 8 def path @path end |
#top ⇒ Object
Returns the value of attribute top.
7 8 9 |
# File 'lib/spritely/image_set.rb', line 7 def top @top end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
8 9 10 |
# File 'lib/spritely/image_set.rb', line 8 def width @width end |
Instance Method Details
#images ⇒ Object
22 23 24 |
# File 'lib/spritely/image_set.rb', line 22 def images @images ||= [] end |
#name ⇒ Object
18 19 20 |
# File 'lib/spritely/image_set.rb', line 18 def name File.basename(path, ".png") end |
#outer_height ⇒ Object
26 27 28 |
# File 'lib/spritely/image_set.rb', line 26 def outer_height spacing_above + height + spacing_below end |
#position_in!(collection_width) ⇒ Object
When positioned in the sprite, we must take into account whether the image is configured to repeat, or is positioned to the right-hand side of the sprite map.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/spritely/image_set.rb', line 49 def position_in!(collection_width) if repeated? left_position = 0 while left_position < collection_width add_image!(left_position) left_position += width end elsif right? add_image!(@left = collection_width - width) else add_image!(0) end end |
#repeated? ⇒ Boolean
38 39 40 |
# File 'lib/spritely/image_set.rb', line 38 def repeated? [:repeat] == 'true' end |
#right? ⇒ Boolean
42 43 44 |
# File 'lib/spritely/image_set.rb', line 42 def right? [:position] == 'right' end |
#spacing_above ⇒ Object
30 31 32 |
# File 'lib/spritely/image_set.rb', line 30 def spacing_above [:spacing_above].to_i end |
#spacing_below ⇒ Object
34 35 36 |
# File 'lib/spritely/image_set.rb', line 34 def spacing_below ([:spacing_below] || [:spacing]).to_i end |