lib/geomotion/cg_rect.rb in geomotion-0.14.0 vs lib/geomotion/cg_rect.rb in geomotion-0.15.0

- old
+ new

@@ -232,122 +232,130 @@ def left(dist=nil, options={}) if dist.nil? NSLog("Using the default value of `0` in `CGRect#left` is deprecated.") dist = 0 end + raise "You cannot specify `:left` in `CGRect#left`" if options.key?(:left) raise "You must specify an amount in `CGRect#left`" unless dist.is_a?(Numeric) self.apply({ left: dist - }.merge(options)) + }).apply(options) end def right(dist=nil, options={}) if dist.nil? NSLog("Using the default value of `0` in `CGRect#right` is deprecated.") dist = 0 end + raise "You cannot specify `:right` in `CGRect#right`" if options.key?(:right) raise "You must specify an amount in `CGRect#right`" unless dist.is_a?(Numeric) self.apply({ right: dist - }.merge(options)) + }).apply(options) end def up(dist=nil, options={}) if dist.nil? NSLog("Using the default value of `0` in `CGRect#up` is deprecated.") dist = 0 end + raise "You cannot specify `:up` in `CGRect#up`" if options.key?(:up) raise "You must specify an amount in `CGRect#up`" unless dist.is_a?(Numeric) self.apply({ up: dist - }.merge(options)) + }).apply(options) end def down(dist=nil, options={}) if dist.nil? NSLog("Using the default value of `0` in `CGRect#down` is deprecated.") dist = 0 end + raise "You cannot specify `:down` in `CGRect#down`" if options.key?(:down) raise "You must specify an amount in `CGRect#down`" unless dist.is_a?(Numeric) self.apply({ down: dist - }.merge(options)) + }).apply(options) end def wider(dist, options={}) + raise "You cannot specify `:width` in `CGRect#width`" if options.key?(:width) raise "You must specify an amount in `CGRect#wider`" unless dist.is_a?(Numeric) self.apply({ wider: dist - }.merge(options)) + }).apply(options) end def thinner(dist, options={}) + raise "You cannot specify `:thinner` in `CGRect#thinner`" if options.key?(:thinner) raise "You must specify an amount in `CGRect#thinner`" unless dist.is_a?(Numeric) self.apply({ thinner: dist - }.merge(options)) + }).apply(options) end def taller(dist, options={}) + raise "You cannot specify `:taller` in `CGRect#taller`" if options.key?(:taller) raise "You must specify an amount in `CGRect#taller`" unless dist.is_a?(Numeric) self.apply({ taller: dist - }.merge(options)) + }).apply(options) end def shorter(dist, options={}) + raise "You cannot specify `:shorter` in `CGRect#shorter`" if options.key?(:shorter) raise "You must specify an amount in `CGRect#shorter`" unless dist.is_a?(Numeric) self.apply({ shorter: dist - }.merge(options)) + }).apply(options) end # adjacent rects def above(margin = 0, options={}) margin, options = 0, margin if margin.is_a?(NSDictionary) margin = options.delete(:margin) if options.key?(:margin) height = options[:height] || self.size.height self.apply({ up: height + margin - }.merge(options)) + }).apply(options) end def below(margin = 0, options={}) margin, options = 0, margin if margin.is_a?(NSDictionary) margin = options.delete(:margin) if options.key?(:margin) self.apply({ down: self.size.height + margin - }.merge(options)) + }).apply(options) end def before(margin = 0, options={}) margin, options = 0, margin if margin.is_a?(NSDictionary) margin = options.delete(:margin) if options.key?(:margin) width = options[:width] || self.size.width self.apply({ left: width + margin - }.merge(options)) + }).apply(options) end def beside(margin = 0, options={}) margin, options = 0, margin if margin.is_a?(NSDictionary) margin = options.delete(:margin) if options.key?(:margin) self.apply({ right: self.size.width + margin - }.merge(options)) + }).apply(options) end alias after beside # these methods create a rect INSIDE the receiver @@ -359,13 +367,13 @@ raise "You must specify a width in `CGRect#from_left`" unless width offset = cgrect_offset(options.delete(:absolute)) self.apply({ x: offset.x + margin, y: offset.y, + width: width, height: self.size.height, - width: width - }.merge(options)) + }).apply(options) end # Create a rect inside the receiver, on the right side. If `margin` is # supplied, the rect will be moved that number of points to the left. def from_right(options={}) @@ -374,13 +382,13 @@ raise "You must specify a width in `CGRect#from_right`" unless width offset = cgrect_offset(options.delete(:absolute)) self.apply({ x: offset.x + self.size.width - width - margin, y: offset.y, + width: width, height: self.size.height, - width: width - }.merge(options)) + }).apply(options) end # Create a rect inside the receiver, on the top side. If `margin` is # supplied, the rect will be moved that number of points down. def from_top(options={}) @@ -390,12 +398,12 @@ offset = cgrect_offset(options.delete(:absolute)) self.apply({ x: offset.x, y: offset.y + margin, width: self.size.width, - height: height - }.merge(options)) + height: height, + }).apply(options) end # Create a rect inside the receiver, on the bottom side. If `margin` is # supplied, the rect will be moved that number of points up. def from_bottom(options={}) @@ -405,12 +413,12 @@ offset = cgrect_offset(options.delete(:absolute)) self.apply({ x: offset.x, y: offset.y + self.size.height - height - margin, width: self.size.width, - height: height - }.merge(options)) + height: height, + }).apply(options) end # positions private def cgrect_offset(absolute) @@ -419,14 +427,10 @@ else CGPoint.new(0, 0) end end - def to_ary - [self.origin, self.size] - end - public def center(absolute = false) cgrect_offset(absolute) + CGPoint.new(self.size.width / 2, self.size.height / 2) end @@ -465,10 +469,14 @@ # others def round CGRect.new([self.origin.x.round, self.origin.y.round], [self.size.width.round, self.size.height.round]) end + def integral + CGRectIntegral(self) + end + def centered_in(rect, absolute = false) self.size.centered_in(rect, absolute) end def +(other) @@ -538,25 +546,27 @@ end alias grow_right wider def grow_left(amount, options={}) + raise "You cannot specify `:grow_left` in `CGRect#grow_left`" if options.key?(:grow_left) raise "You must specify an amount in `CGRect#grow_left`" unless amount.is_a?(Numeric) self.apply({ grow_left: amount - }.merge(options)) + }).apply(options) end alias grow_down taller def grow_up(amount, options={}) + raise "You cannot specify `:grow_up` in `CGRect#grow_up`" if options.key?(:grow_up) raise "You must specify an amount in `CGRect#grow_up`" unless amount.is_a?(Numeric) self.apply({ grow_up: amount - }.merge(options)) + }).apply(options) end def grow_width(amount, options={}) return self.grow([amount, 0], options) end @@ -577,25 +587,27 @@ end alias shrink_left thinner def shrink_right(amount, options={}) + raise "You cannot specify `:shrink_right` in `CGRect#shrink_right`" if options.key?(:shrink_right) raise "You must specify an amount in `CGRect#shrink_right`" unless amount.is_a?(Numeric) self.apply({ shrink_right: amount - }.merge(options)) + }).apply(options) end alias shrink_up shorter def shrink_down(amount, options={}) + raise "You cannot specify `:shrink_down` in `CGRect#shrink_down`" if options.key?(:shrink_down) raise "You must specify an amount in `CGRect#shrink_down`" unless amount.is_a?(Numeric) self.apply({ shrink_down: amount - }.merge(options)) + }).apply(options) end def shrink_width(amount, options={}) return self.shrink([amount, 0], options) end @@ -648,8 +660,18 @@ self.+(-other) end def inspect "#{self.class.name}([#{self.origin.x}, #{self.origin.y}], [#{self.size.width}, #{self.size.height}])" + end + + def to_ns_value + NSValue.valueWithCGRect(self) + end + +private + + def to_ary + [self.origin, self.size] end end