Class: Rubyshop::LayerSet
- Inherits:
-
Object
- Object
- Rubyshop::LayerSet
- Defined in:
- lib/rubyshop/layerset.rb
Instance Attribute Summary collapse
-
#bounding_height ⇒ Object
readonly
Returns the value of attribute bounding_height.
-
#bounding_width ⇒ Object
readonly
Returns the value of attribute bounding_width.
-
#layers ⇒ Object
Returns the value of attribute layers.
-
#max_x ⇒ Object
readonly
Returns the value of attribute max_x.
-
#max_y ⇒ Object
readonly
Returns the value of attribute max_y.
-
#min_x ⇒ Object
readonly
Returns the value of attribute min_x.
-
#min_y ⇒ Object
readonly
Returns the value of attribute min_y.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_layer(layer) ⇒ Object
- #bounding_box ⇒ Object
-
#initialize(name) {|_self| ... } ⇒ LayerSet
constructor
A new instance of LayerSet.
- #layer(filename, &block) ⇒ Object
- #offset(x, y) ⇒ Object
- #remove_layer(layer) ⇒ Object
- #rotate(degrees) ⇒ Object
- #scale(width, height) ⇒ Object
Constructor Details
#initialize(name) {|_self| ... } ⇒ LayerSet
Returns a new instance of LayerSet.
6 7 8 9 10 11 |
# File 'lib/rubyshop/layerset.rb', line 6 def initialize(name) @layers = [] bounding_box @name = name yield self if block_given? end |
Instance Attribute Details
#bounding_height ⇒ Object (readonly)
Returns the value of attribute bounding_height.
4 5 6 |
# File 'lib/rubyshop/layerset.rb', line 4 def bounding_height @bounding_height end |
#bounding_width ⇒ Object (readonly)
Returns the value of attribute bounding_width.
4 5 6 |
# File 'lib/rubyshop/layerset.rb', line 4 def bounding_width @bounding_width end |
#layers ⇒ Object
Returns the value of attribute layers.
3 4 5 |
# File 'lib/rubyshop/layerset.rb', line 3 def layers @layers end |
#max_x ⇒ Object (readonly)
Returns the value of attribute max_x.
4 5 6 |
# File 'lib/rubyshop/layerset.rb', line 4 def max_x @max_x end |
#max_y ⇒ Object (readonly)
Returns the value of attribute max_y.
4 5 6 |
# File 'lib/rubyshop/layerset.rb', line 4 def max_y @max_y end |
#min_x ⇒ Object (readonly)
Returns the value of attribute min_x.
4 5 6 |
# File 'lib/rubyshop/layerset.rb', line 4 def min_x @min_x end |
#min_y ⇒ Object (readonly)
Returns the value of attribute min_y.
4 5 6 |
# File 'lib/rubyshop/layerset.rb', line 4 def min_y @min_y end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/rubyshop/layerset.rb', line 4 def name @name end |
Instance Method Details
#add_layer(layer) ⇒ Object
22 23 24 25 |
# File 'lib/rubyshop/layerset.rb', line 22 def add_layer(layer) @layers << layer bounding_box end |
#bounding_box ⇒ Object
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 |
# File 'lib/rubyshop/layerset.rb', line 32 def bounding_box if @layers.size > 0 x1 = [] y1 = [] x2 = [] y2 = [] @layers.each do |layer| x1 << layer.offset_x y1 << layer.offset_y x2 << layer.width + layer.offset_x y2 << layer.height + layer.offset_y end @min_x = x1.min @min_y = y1.min @max_x = x2.max @max_y = y2.max @bounding_width = @max_x - @min_x @bounding_height = @max_y - @min_y else @min_x = 0 @min_y = 0 @max_x = 0 @max_y = 0 @bounding_width = 0 @boudning_height = 0 end end |
#layer(filename, &block) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/rubyshop/layerset.rb', line 13 def layer(filename, &block) layer = Layer.new(filename) @layers << layer layer.instance_eval &block return layer end |
#offset(x, y) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/rubyshop/layerset.rb', line 61 def offset(x, y) bounding_box # recalculate bounding box shift_x = x - @min_x shift_y = y - @min_y @layers.each do |layer| layer.offset(layer.offset_x + shift_x, layer.offset_y + shift_y) end bounding_box end |
#remove_layer(layer) ⇒ Object
27 28 29 30 |
# File 'lib/rubyshop/layerset.rb', line 27 def remove_layer(layer) @layers.delete(layer) bounding_box end |
#rotate(degrees) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rubyshop/layerset.rb', line 82 def rotate(degrees) bounding_box # recalculate bounding box bounding_mid_x = @bounding_width / 2 bounding_mid_y = @bounding_height / 2 @layers.each do |layer| layer_mid_x = (layer.width/2.0).round + layer.offset_x - @min_x - bounding_mid_x layer_mid_y = ((layer.height/2.0).round + layer.offset_y - @min_y - bounding_mid_y) * -1 radians = (degrees * -1) / (180 / Math::PI) cos = Math.cos(radians) sin = Math.sin(radians) new_mid_x = ((layer_mid_x * cos) - (layer_mid_y * sin)).round new_mid_y = ((layer_mid_x * sin) + (layer_mid_y * cos)).round layer.image.rotate!(degrees) new_offset_x = new_mid_x - (layer.width/2.0).round + @min_x + bounding_mid_x new_offset_y = new_mid_y * -1 - (layer.height/2.0).round + @min_y + bounding_mid_y layer.offset(new_offset_x, new_offset_y) end bounding_box end |
#scale(width, height) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rubyshop/layerset.rb', line 71 def scale(width, height) bounding_box # recalculate bounding box width_scale = width.to_f / bounding_width.to_f height_scale = height.to_f / bounding_height.to_f @layers.each do |layer| layer.image.scale!((layer.width * width_scale).round, (layer.height * height_scale).round) layer.offset(((layer.offset_x - @min_x) * width_scale).round + @min_x, ((layer.offset_y - @min_y) * height_scale).round + @min_y) end bounding_box end |