lib/geomotion/cg_rect.rb in geomotion-0.5 vs lib/geomotion/cg_rect.rb in geomotion-0.7.0

- old
+ new

@@ -75,18 +75,26 @@ # bounds def min_x CGRectGetMinX(self) end + def mid_x + CGRectGetMidX(self) + end + def max_x CGRectGetMaxX(self) end def min_y CGRectGetMinY(self) end + def mid_y + CGRectGetMidY(self) + end + def max_y CGRectGetMaxY(self) end # getters/setters @@ -194,33 +202,57 @@ def beside(margin, width:width) CGRect.new([self.x + self.width + margin, self.y], [width, self.height]) end - # locations + # positions +private + def cgrect_offset(absolute) + if absolute + CGPoint.new(self.min_x, self.min_y) + else + CGPoint.new(0, 0) + end + end + +public def center(absolute = false) - offset_x = absolute ? self.x : 0 - offset_y = absolute ? self.y : 0 - CGPoint.new(offset_x + self.width / 2, offset_y + self.height / 2) + cgrect_offset(absolute) + CGPoint.new(self.width / 2, self.height / 2) end - def top_left - CGPoint.new(CGRectGetMinX(self), CGRectGetMinY(self)) + def top_left(absolute = false) + cgrect_offset(absolute) + CGPoint.new(0, 0) end - def top_right - CGPoint.new(CGRectGetMaxX(self), CGRectGetMinY(self)) + def top_center(absolute = false) + cgrect_offset(absolute) + CGPoint.new(self.width / 2, 0) end - def bottom_left - CGPoint.new(CGRectGetMinX(self), CGRectGetMaxY(self)) + def top_right(absolute = false) + cgrect_offset(absolute) + CGPoint.new(self.width, 0) end - def bottom_right - CGPoint.new(CGRectGetMaxX(self), CGRectGetMaxY(self)) + def center_right(absolute = false) + cgrect_offset(absolute) + CGPoint.new(self.width, self.height / 2) end + def bottom_right(absolute = false) + cgrect_offset(absolute) + CGPoint.new(self.width, self.height) + end + + def bottom_center(absolute = false) + cgrect_offset(absolute) + CGPoint.new(self.width / 2, self.height) + end + + def bottom_left(absolute = false) + cgrect_offset(absolute) + CGPoint.new(0, self.height) + end + + def center_left(absolute = false) + cgrect_offset(absolute) + CGPoint.new(0, self.height / 2) + end + # others def round CGRect.new([self.x.round, self.y.round], [self.width.round, self.height.round]) end @@ -233,37 +265,69 @@ when CGRect return self.union_with(other) when CGSize return CGRect.new([self.x, self.y], [self.width + other.width, self.height + other.height]) when CGPoint - return CGRectOffset(self, other.x, other.y) + return self.offset(other.x, other.y) when UIOffset - CGRectOffset(self, other.horizontal, other.vertical) + return self.offset(other.horizontal, other.vertical) when UIEdgeInsets - UIEdgeInsetsInsetRect(self, other) + return self.inset(other) end end + def *(scale) + case scale + when Numeric + return CGRect.new(self.origin, self.size * scale) + else + super + end + end + + # it is tempting to define this as self * (1.0/scale) but floating point + # errors result in too many errors + def /(scale) + case scale + when Numeric + return CGRect.new(self.origin, self.size / scale) + else + super + end + end + def intersection_with(rect) CGRectIntersection(self, rect) end def union_with(rect) CGRectUnion(self, rect) end + def inset(insets) + UIEdgeInsetsInsetRect(self, insets) + end + + def offset(point_or_x, y=nil) + if y + CGRectOffset(self, point_or_x, y) + else + CGRectOffset(self, point_or_x[0], point_or_x[1]) + end + end + def grow(size) if size.is_a? Numeric size = CGSize.new(size, size) end - CGRectInset(self, -size.width, -size.height) + CGRectInset(self, -size[0], -size[1]) end def shrink(size) if size.is_a? Numeric size = CGSize.new(size, size) end - CGRectInset(self, size.width, size.height) + CGRectInset(self, size[0], size[1]) end def empty? CGRectIsEmpty(self) end