Class: CGRect
- Inherits:
-
Object
- Object
- CGRect
- Defined in:
- lib/geomotion/cg_rect.rb
Class Method Summary collapse
- .empty ⇒ Object
- .infinite ⇒ Object
-
.layout(rect1, options) ⇒ Object
OPTIONS: [:above, :below, :left_of, :right_of, :margins] :margins is array of [top, right, bottom, left] EX CGRect.layout(rect1, above: rect2, left_of: rect3, margins: [0, 10, 20, 0]).
-
.make(options = {}) ⇒ Object
CGRect.make # default rect: {x: 0, y: 0, size: height:0} # aka CGRectZero CGRect.make(x: 10, y: 30) # default size: [0, 0] CGRect.make(x: 10, y: 30, width:100, height: 20).
- .null ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #==(rect) ⇒ Object
-
#above(margin, height: height) ⇒ Object
adjacent rects.
- #before(margin, width: width) ⇒ Object
- #below(margin = 0) ⇒ Object
- #beside(margin, width: width) ⇒ Object
- #bottom_left ⇒ Object
- #bottom_right ⇒ Object
-
#center(absolute = false) ⇒ Object
locations.
- #centered_in(rect, absolute = false) ⇒ Object
- #contains?(rect_or_point) ⇒ Boolean
- #down(dist = 0) ⇒ Object
- #empty? ⇒ Boolean
- #grow(size) ⇒ Object
- #height(setter = nil) ⇒ Object
- #height=(_height) ⇒ Object
- #infinite? ⇒ Boolean
- #inspect ⇒ Object
- #intersection_with(rect) ⇒ Object
- #intersects?(rect) ⇒ Boolean
-
#left(dist = 0) ⇒ Object
modified rects.
- #max_x ⇒ Object
- #max_y ⇒ Object
-
#min_x ⇒ Object
bounds.
- #min_y ⇒ Object
- #null? ⇒ Boolean
- #right(dist = 0) ⇒ Object
-
#round ⇒ Object
others.
- #shorter(dist) ⇒ Object
- #shrink(size) ⇒ Object
- #taller(dist) ⇒ Object
- #thinner(dist) ⇒ Object
- #top_left ⇒ Object
- #top_right ⇒ Object
- #union_with(rect) ⇒ Object
- #up(dist = 0) ⇒ Object
- #wider(dist) ⇒ Object
- #width(setter = nil) ⇒ Object
- #width=(_width) ⇒ Object
-
#x(setter = nil) ⇒ Object
getters/setters.
- #x=(_x) ⇒ Object
- #y(setter = nil) ⇒ Object
- #y=(_y) ⇒ Object
Class Method Details
.empty ⇒ Object
28 29 30 31 |
# File 'lib/geomotion/cg_rect.rb', line 28 def self.empty # Don't just return CGRectZero; can be mutated CGRectZero.dup end |
.infinite ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/geomotion/cg_rect.rb', line 38 def self.infinite # This actually returns the not-very-infinite value of: # [[-1.7014114289565e+38, -1.7014114289565e+38], [3.402822857913e+38, 3.402822857913e+38]] # originally this method returned [[-Infinity, -Infinity], [Infinity, Infinity]], # but that rect ended up returning `false` for any point in the method # CGRect.infinite.contains?(point). CGRectInfinite returns `true` for any # (sensible) point, so we'll go with that instead CGRectInfinite.dup end |
.layout(rect1, options) ⇒ Object
OPTIONS: [:above, :below, :left_of, :right_of, :margins]
:margins is array of [top, right, bottom, left]
EX CGRect.layout(rect1, above: rect2, left_of: rect3, margins: [0, 10, 20, 0])
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/geomotion/cg_rect.rb', line 51 def self.layout(rect1, ) if .empty? p "No options provided in #{self.class}.layout" return rect1 end rect = self.new rect.size = rect1.size [:margins] ||= [] margins = {} [:top, :right, :bottom, :left].each_with_index do |margin, index| margins[margin] = [:margins][index] || 0 end rect.y = [:above].up(rect.height + margins[:bottom]).y if [:above] rect.y = [:below].below(margins[:top]).y if [:below] rect.x = [:left_of].left(rect.width + margins[:right]).x if [:left_of] rect.x = [:right_of].beside(margins[:left]).x if [:right_of] rect end |
.make(options = {}) ⇒ Object
CGRect.make # default rect: {x: 0, y: 0, size: height:0}
# aka CGRectZero
CGRect.make(x: 10, y: 30) # default size: [0, 0] CGRect.make(x: 10, y: 30, width:100, height: 20)
point = CGPoint.make(x: 10, y: 30) size = CGSize.make(width: 100, height: 20) CGRect.make(origin: point, size: size)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/geomotion/cg_rect.rb', line 10 def self.make( = {}) if [:origin] x = [:origin].x y = [:origin].y else x = [:x] || 0 y = [:y] || 0 end if [:size] w = [:size].width h = [:size].height else w = [:width] || 0 h = [:height] || 0 end self.new([x, y], [w, h]) end |
.null ⇒ Object
33 34 35 36 |
# File 'lib/geomotion/cg_rect.rb', line 33 def self.null # Don't just return CGRectNull; can be mutated CGRectNull.dup end |
Instance Method Details
#+(other) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/geomotion/cg_rect.rb', line 231 def +(other) case other 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) when UIOffset CGRectOffset(self, other.horizontal, other.vertical) when UIEdgeInsets UIEdgeInsetsInsetRect(self, other) end end |
#-(other) ⇒ Object
308 309 310 |
# File 'lib/geomotion/cg_rect.rb', line 308 def -(other) self.+(-other) end |
#-@ ⇒ Object
304 305 306 |
# File 'lib/geomotion/cg_rect.rb', line 304 def -@ CGRect.new(-self.origin, -self.size) end |
#==(rect) ⇒ Object
300 301 302 |
# File 'lib/geomotion/cg_rect.rb', line 300 def ==(rect) rect.is_a?(CGRect) && CGRectEqualToRect(self, rect) end |
#above(margin, height: height) ⇒ Object
adjacent rects
171 172 173 |
# File 'lib/geomotion/cg_rect.rb', line 171 def above(margin = 0) self.above(margin, height:self.height) end |
#before(margin, width: width) ⇒ Object
183 184 185 |
# File 'lib/geomotion/cg_rect.rb', line 183 def before(margin = 0) self.before(margin, width:self.width) end |
#below(margin = 0) ⇒ Object
179 180 181 |
# File 'lib/geomotion/cg_rect.rb', line 179 def below(margin = 0) CGRect.new([self.x, self.y + self.height + margin], self.size) end |
#beside(margin, width: width) ⇒ Object
191 192 193 |
# File 'lib/geomotion/cg_rect.rb', line 191 def beside(margin = 0) self.beside(margin, width: self.width) end |
#bottom_left ⇒ Object
214 215 216 |
# File 'lib/geomotion/cg_rect.rb', line 214 def bottom_left CGPoint.new(CGRectGetMinX(self), CGRectGetMaxY(self)) end |
#bottom_right ⇒ Object
218 219 220 |
# File 'lib/geomotion/cg_rect.rb', line 218 def bottom_right CGPoint.new(CGRectGetMaxX(self), CGRectGetMaxY(self)) end |
#center(absolute = false) ⇒ Object
locations
200 201 202 203 204 |
# File 'lib/geomotion/cg_rect.rb', line 200 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) end |
#centered_in(rect, absolute = false) ⇒ Object
227 228 229 |
# File 'lib/geomotion/cg_rect.rb', line 227 def centered_in(rect, absolute = false) self.size.centered_in(rect, absolute) end |
#contains?(rect_or_point) ⇒ Boolean
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/geomotion/cg_rect.rb', line 289 def contains?(rect_or_point) case rect_or_point when CGPoint CGRectContainsPoint(self, rect_or_point) when CGRect CGRectContainsRect(self, rect_or_point) else super # raises an error end end |
#down(dist = 0) ⇒ Object
150 151 152 |
# File 'lib/geomotion/cg_rect.rb', line 150 def down(dist = 0) CGRect.new([self.x, self.y + dist], self.size) end |
#empty? ⇒ Boolean
268 269 270 |
# File 'lib/geomotion/cg_rect.rb', line 268 def empty? CGRectIsEmpty(self) end |
#grow(size) ⇒ Object
254 255 256 257 258 259 |
# File 'lib/geomotion/cg_rect.rb', line 254 def grow(size) if size.is_a? Numeric size = CGSize.new(size, size) end CGRectInset(self, -size.width, -size.height) end |
#height(setter = nil) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/geomotion/cg_rect.rb', line 126 def height(setter = nil) if setter return CGRect.new(self.origin, [self.width, setter]) end CGRectGetHeight(self) end |
#height=(_height) ⇒ Object
133 134 135 |
# File 'lib/geomotion/cg_rect.rb', line 133 def height=(_height) self.size.height = _height end |
#infinite? ⇒ Boolean
272 273 274 |
# File 'lib/geomotion/cg_rect.rb', line 272 def infinite? self.size.infinite? || CGRectEqualToRect(self, CGRectInfinite) end |
#inspect ⇒ Object
312 313 314 |
# File 'lib/geomotion/cg_rect.rb', line 312 def inspect "#{self.class.name}([#{self.origin.x}, #{self.origin.y}], [#{self.size.width}, #{self.size.height}])" end |
#intersection_with(rect) ⇒ Object
246 247 248 |
# File 'lib/geomotion/cg_rect.rb', line 246 def intersection_with(rect) CGRectIntersection(self, rect) end |
#intersects?(rect) ⇒ Boolean
280 281 282 283 284 285 286 287 |
# File 'lib/geomotion/cg_rect.rb', line 280 def intersects?(rect) case rect when CGRect CGRectIntersectsRect(self, rect) else super # raises an error end end |
#left(dist = 0) ⇒ Object
modified rects
138 139 140 |
# File 'lib/geomotion/cg_rect.rb', line 138 def left(dist = 0) CGRect.new([self.x - dist, self.y], self.size) end |
#max_x ⇒ Object
80 81 82 |
# File 'lib/geomotion/cg_rect.rb', line 80 def max_x CGRectGetMaxX(self) end |
#max_y ⇒ Object
88 89 90 |
# File 'lib/geomotion/cg_rect.rb', line 88 def max_y CGRectGetMaxY(self) end |
#min_x ⇒ Object
bounds
76 77 78 |
# File 'lib/geomotion/cg_rect.rb', line 76 def min_x CGRectGetMinX(self) end |
#min_y ⇒ Object
84 85 86 |
# File 'lib/geomotion/cg_rect.rb', line 84 def min_y CGRectGetMinY(self) end |
#null? ⇒ Boolean
276 277 278 |
# File 'lib/geomotion/cg_rect.rb', line 276 def null? CGRectIsNull(self) end |
#right(dist = 0) ⇒ Object
142 143 144 |
# File 'lib/geomotion/cg_rect.rb', line 142 def right(dist = 0) CGRect.new([self.x + dist, self.y], self.size) end |
#round ⇒ Object
others
223 224 225 |
# File 'lib/geomotion/cg_rect.rb', line 223 def round CGRect.new([self.x.round, self.y.round], [self.width.round, self.height.round]) end |
#shorter(dist) ⇒ Object
166 167 168 |
# File 'lib/geomotion/cg_rect.rb', line 166 def shorter(dist) CGRect.new(self.origin, [self.width, self.height - dist]) end |
#shrink(size) ⇒ Object
261 262 263 264 265 266 |
# File 'lib/geomotion/cg_rect.rb', line 261 def shrink(size) if size.is_a? Numeric size = CGSize.new(size, size) end CGRectInset(self, size.width, size.height) end |
#taller(dist) ⇒ Object
162 163 164 |
# File 'lib/geomotion/cg_rect.rb', line 162 def taller(dist) CGRect.new(self.origin, [self.width, self.height + dist]) end |
#thinner(dist) ⇒ Object
158 159 160 |
# File 'lib/geomotion/cg_rect.rb', line 158 def thinner(dist) CGRect.new(self.origin, [self.width - dist, self.height]) end |
#top_left ⇒ Object
206 207 208 |
# File 'lib/geomotion/cg_rect.rb', line 206 def top_left CGPoint.new(CGRectGetMinX(self), CGRectGetMinY(self)) end |
#top_right ⇒ Object
210 211 212 |
# File 'lib/geomotion/cg_rect.rb', line 210 def top_right CGPoint.new(CGRectGetMaxX(self), CGRectGetMinY(self)) end |
#union_with(rect) ⇒ Object
250 251 252 |
# File 'lib/geomotion/cg_rect.rb', line 250 def union_with(rect) CGRectUnion(self, rect) end |
#up(dist = 0) ⇒ Object
146 147 148 |
# File 'lib/geomotion/cg_rect.rb', line 146 def up(dist = 0) CGRect.new([self.x, self.y - dist], self.size) end |
#wider(dist) ⇒ Object
154 155 156 |
# File 'lib/geomotion/cg_rect.rb', line 154 def wider(dist) CGRect.new(self.origin, [self.width + dist, self.height]) end |
#width(setter = nil) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/geomotion/cg_rect.rb', line 115 def width(setter = nil) if setter return CGRect.new(self.origin, [setter, self.height]) end CGRectGetWidth(self) end |
#width=(_width) ⇒ Object
122 123 124 |
# File 'lib/geomotion/cg_rect.rb', line 122 def width=(_width) self.size.width = _width end |
#x(setter = nil) ⇒ Object
getters/setters
93 94 95 96 97 98 |
# File 'lib/geomotion/cg_rect.rb', line 93 def x(setter = nil) if setter return CGRect.new([setter, self.y], self.size) end min_x end |
#x=(_x) ⇒ Object
100 101 102 |
# File 'lib/geomotion/cg_rect.rb', line 100 def x=(_x) self.origin.x = _x end |
#y(setter = nil) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/geomotion/cg_rect.rb', line 104 def y(setter = nil) if setter return CGRect.new([self.x, setter], self.size) end min_y end |
#y=(_y) ⇒ Object
111 112 113 |
# File 'lib/geomotion/cg_rect.rb', line 111 def y=(_y) self.origin.y = _y end |