Class: CGRect

Inherits:
Object
  • Object
show all
Defined in:
lib/geomotion/cg_rect.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.emptyObject



29
30
31
32
# File 'lib/geomotion/cg_rect.rb', line 29

def self.empty
  # Don't just return CGRectZero; can be mutated
  CGRectZero.dup
end

.infiniteObject



39
40
41
42
43
44
45
46
47
# File 'lib/geomotion/cg_rect.rb', line 39

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])



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/geomotion/cg_rect.rb', line 52

def self.layout(rect1, options)
  if options.empty?
    p "No options provided in #{self.class}.layout"
    return rect1
  end

  rect = self.new
  rect.size = rect1.size

  options[:margins] ||= []
  margins = {}
  [:top, :right, :bottom, :left].each_with_index do |margin, index|
    margins[margin] = options[:margins][index] || 0
  end

  rect.y = options[:above].up(rect.height + margins[:bottom]).y if options[:above]
  rect.y = options[:below].below(margins[:top]).y if options[:below]

  rect.x = options[:left_of].left(rect.width + margins[:right]).x if options[:left_of]
  rect.x = options[:right_of].beside(margins[:left]).x if options[: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)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/geomotion/cg_rect.rb', line 11

def self.make(options = {})
  if options[:origin]
    x = options[:origin][0]
    y = options[:origin][1]
  else
    x = options[:x] || 0
    y = options[:y] || 0
  end
  if options[:size]
    w = options[:size][0]
    h = options[:size][1]
  else
    w = options[:width] || 0
    h = options[:height] || 0
  end
  self.new([x, y], [w, h])
end

.nullObject



34
35
36
37
# File 'lib/geomotion/cg_rect.rb', line 34

def self.null
  # Don't just return CGRectNull; can be mutated
  CGRectNull.dup
end

Instance Method Details

#*(scale) ⇒ Object



387
388
389
390
391
392
393
394
# File 'lib/geomotion/cg_rect.rb', line 387

def *(scale)
  case scale
  when Numeric
    return CGRect.new(self.origin, self.size * scale)
  else
    super
  end
end

#+(other) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/geomotion/cg_rect.rb', line 372

def +(other)
  case other
  when CGRect
    return self.union_with(other)
  when CGSize
    return CGRect.new([self.origin.x, self.origin.y], [self.size.width + other.width, self.size.height + other.height])
  when CGPoint
    return self.offset(other.x, other.y)
  when UIOffset
    return self.offset(other.horizontal, other.vertical)
  when UIEdgeInsets
    return self.inset(other)
  end
end

#-(other) ⇒ Object



537
538
539
# File 'lib/geomotion/cg_rect.rb', line 537

def -(other)
  self.+(-other)
end

#-@Object



533
534
535
# File 'lib/geomotion/cg_rect.rb', line 533

def -@
  CGRect.new(-self.origin, -self.size)
end

#/(scale) ⇒ Object

it is tempting to define this as self * (1.0/scale) but floating point errors result in too many errors



398
399
400
401
402
403
404
405
# File 'lib/geomotion/cg_rect.rb', line 398

def /(scale)
  case scale
  when Numeric
    return CGRect.new(self.origin, self.size / scale)
  else
    super
  end
end

#==(rect) ⇒ Object



529
530
531
# File 'lib/geomotion/cg_rect.rb', line 529

def ==(rect)
  rect.is_a?(CGRect) && CGRectEqualToRect(self, rect)
end

#above(margin = 0, options = {}) ⇒ Object

adjacent rects



286
287
288
289
290
291
292
# File 'lib/geomotion/cg_rect.rb', line 286

def above(margin = 0, options={})
  margin, options = 0, margin if margin.is_a?(NSDictionary)

  height = options[:height] || self.size.height
  options[:up] = height + margin
  self.apply(options)
end

#apply(options) ⇒ Object

most rect modifiers call this method in one way or another



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/geomotion/cg_rect.rb', line 147

def apply(options)
  rect = CGRectStandardize(CGRect.new(self.origin, self.size))
  options.each do |method, value|
    case method
    when :left
      rect.origin.x -= value
    when :right
      rect.origin.x += value
    when :up
      rect.origin.y -= value
    when :down
      rect.origin.y += value
    when :wider, :grow_right
      rect.size.width += value
    when :thinner, :shrink_left
      rect.size.width -= value
    when :taller, :grow_down
      rect.size.height += value
    when :shorter, :shrink_up
      rect.size.height -= value
    when :x
      rect.origin.x = value
    when :y
      rect.origin.y = value
    when :origin
      rect.origin = value
    when :width
      rect.size.width = value
    when :height
      rect.size.height = value
    when :size
      rect.size = value
    when :grow
      rect = rect.grow(value)
    when :grow_up
      rect.size.height += value
      rect.origin.y -= value
    when :shrink_down
      rect.size.height -= value
      rect.origin.y += value
    when :grow_left
      rect.size.width += value
      rect.origin.x -= value
    when :shrink_right
      rect.size.width -= value
      rect.origin.x += value
    when :grow_width
      rect = rect.grow_width(value)
    when :grow_height
      rect = rect.grow_height(value)
    when :shrink
      rect = rect.shrink(value)
    when :shrink_width
      rect = rect.shrink_width(value)
    when :shrink_height
      rect = rect.shrink_height(value)
    when :offset
      rect = rect.offset(value)
    else
      raise "Unknow option #{method}"
    end
  end
  return rect
end

#before(margin = 0, options = {}) ⇒ Object



301
302
303
304
305
306
307
# File 'lib/geomotion/cg_rect.rb', line 301

def before(margin = 0, options={})
  margin, options = 0, margin if margin.is_a?(NSDictionary)

  width = options[:width] || self.size.width
  options[:left] = width + margin
  self.apply(options)
end

#below(margin = 0, options = {}) ⇒ Object



294
295
296
297
298
299
# File 'lib/geomotion/cg_rect.rb', line 294

def below(margin = 0, options={})
  margin, options = 0, margin if margin.is_a?(NSDictionary)

  options[:down] = self.size.height + margin
  self.apply(options)
end

#beside(margin = 0, options = {}) ⇒ Object



309
310
311
312
313
314
# File 'lib/geomotion/cg_rect.rb', line 309

def beside(margin = 0, options={})
  margin, options = 0, margin if margin.is_a?(NSDictionary)

  options[:right] = self.size.width + margin
  self.apply(options)
end

#bottom_center(absolute = false) ⇒ Object



351
352
353
# File 'lib/geomotion/cg_rect.rb', line 351

def bottom_center(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width / 2, self.size.height)
end

#bottom_left(absolute = false) ⇒ Object



355
356
357
# File 'lib/geomotion/cg_rect.rb', line 355

def bottom_left(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(0, self.size.height)
end

#bottom_right(absolute = false) ⇒ Object



347
348
349
# File 'lib/geomotion/cg_rect.rb', line 347

def bottom_right(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width, self.size.height)
end

#center(absolute = false) ⇒ Object



327
328
329
# File 'lib/geomotion/cg_rect.rb', line 327

def center(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width / 2, self.size.height / 2)
end

#center_left(absolute = false) ⇒ Object



359
360
361
# File 'lib/geomotion/cg_rect.rb', line 359

def center_left(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(0, self.size.height / 2)
end

#center_right(absolute = false) ⇒ Object



343
344
345
# File 'lib/geomotion/cg_rect.rb', line 343

def center_right(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width, self.size.height / 2)
end

#centered_in(rect, absolute = false) ⇒ Object



368
369
370
# File 'lib/geomotion/cg_rect.rb', line 368

def centered_in(rect, absolute = false)
  self.size.centered_in(rect, absolute)
end

#contains?(rect_or_point) ⇒ Boolean

Returns:

  • (Boolean)


518
519
520
521
522
523
524
525
526
527
# File 'lib/geomotion/cg_rect.rb', line 518

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 = nil, options = {}) ⇒ Object



246
247
248
249
250
251
252
253
254
255
# File 'lib/geomotion/cg_rect.rb', line 246

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 must specify an amount in `CGRect#down`" unless dist.is_a?(Numeric)

  options[:down] = dist
  self.apply(options)
end

#empty?Boolean

Returns:

  • (Boolean)


497
498
499
# File 'lib/geomotion/cg_rect.rb', line 497

def empty?
  CGRectIsEmpty(self)
end

#grow(size, options = nil) ⇒ Object



427
428
429
430
431
432
433
434
435
436
# File 'lib/geomotion/cg_rect.rb', line 427

def grow(size, options=nil)
  if size.is_a? Numeric
    size = CGSize.new(size, size)
  end
  rect = CGRectInset(self, -size[0], -size[1])
  if options
    return rect.apply(options)
  end
  return rect
end

#grow_height(amount, options = nil) ⇒ Object



458
459
460
# File 'lib/geomotion/cg_rect.rb', line 458

def grow_height(amount, options=nil)
  return self.grow([0, amount], options)
end

#grow_left(amount, options = nil) ⇒ Object



439
440
441
442
443
444
# File 'lib/geomotion/cg_rect.rb', line 439

def grow_left(amount, options=nil)
  raise "You must specify an amount in `CGRect#grow_left`" unless amount.is_a?(Numeric)

  options[:grow_left] = amount
  self.apply(options)
end

#grow_up(amount, options = nil) ⇒ Object



447
448
449
450
451
452
# File 'lib/geomotion/cg_rect.rb', line 447

def grow_up(amount, options=nil)
  raise "You must specify an amount in `CGRect#grow_up`" unless amount.is_a?(Numeric)

  options[:grow_up] = amount
  self.apply(options)
end

#grow_width(amount, options = nil) ⇒ Object



454
455
456
# File 'lib/geomotion/cg_rect.rb', line 454

def grow_width(amount, options=nil)
  return self.grow([amount, 0], options)
end

#height(setter = nil) ⇒ Object



135
136
137
138
139
140
# File 'lib/geomotion/cg_rect.rb', line 135

def height(setter = nil)
  if setter
    return CGRect.new(self.origin, [self.size.width, setter])
  end
  CGRectGetHeight(self)
end

#height=(_height) ⇒ Object



142
143
144
# File 'lib/geomotion/cg_rect.rb', line 142

def height=(_height)
  self.size.height = _height
end

#infinite?Boolean

Returns:

  • (Boolean)


501
502
503
# File 'lib/geomotion/cg_rect.rb', line 501

def infinite?
  self.size.infinite? || CGRectEqualToRect(self, CGRectInfinite)
end

#inset(insets) ⇒ Object



415
416
417
# File 'lib/geomotion/cg_rect.rb', line 415

def inset(insets)
  UIEdgeInsetsInsetRect(self, insets)
end

#inspectObject



541
542
543
# File 'lib/geomotion/cg_rect.rb', line 541

def inspect
  "#{self.class.name}([#{self.origin.x}, #{self.origin.y}], [#{self.size.width}, #{self.size.height}])"
end

#intersection_with(rect) ⇒ Object



407
408
409
# File 'lib/geomotion/cg_rect.rb', line 407

def intersection_with(rect)
  CGRectIntersection(self, rect)
end

#intersects?(rect) ⇒ Boolean

Returns:

  • (Boolean)


509
510
511
512
513
514
515
516
# File 'lib/geomotion/cg_rect.rb', line 509

def intersects?(rect)
  case rect
  when CGRect
    CGRectIntersectsRect(self, rect)
  else
    super  # raises an error
  end
end

#left(dist = nil, options = {}) ⇒ Object

modified rects



213
214
215
216
217
218
219
220
221
222
# File 'lib/geomotion/cg_rect.rb', line 213

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 must specify an amount in `CGRect#left`" unless dist.is_a?(Numeric)

  options[:left] = dist
  self.apply(options)
end

#max_xObject



85
86
87
# File 'lib/geomotion/cg_rect.rb', line 85

def max_x
  CGRectGetMaxX(self)
end

#max_yObject



97
98
99
# File 'lib/geomotion/cg_rect.rb', line 97

def max_y
  CGRectGetMaxY(self)
end

#mid_xObject



81
82
83
# File 'lib/geomotion/cg_rect.rb', line 81

def mid_x
  CGRectGetMidX(self)
end

#mid_yObject



93
94
95
# File 'lib/geomotion/cg_rect.rb', line 93

def mid_y
  CGRectGetMidY(self)
end

#min_xObject

bounds



77
78
79
# File 'lib/geomotion/cg_rect.rb', line 77

def min_x
  CGRectGetMinX(self)
end

#min_yObject



89
90
91
# File 'lib/geomotion/cg_rect.rb', line 89

def min_y
  CGRectGetMinY(self)
end

#null?Boolean

Returns:

  • (Boolean)


505
506
507
# File 'lib/geomotion/cg_rect.rb', line 505

def null?
  CGRectIsNull(self)
end

#offset(point_or_x, y = nil) ⇒ Object



419
420
421
422
423
424
425
# File 'lib/geomotion/cg_rect.rb', line 419

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

#right(dist = nil, options = {}) ⇒ Object



224
225
226
227
228
229
230
231
232
233
# File 'lib/geomotion/cg_rect.rb', line 224

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 must specify an amount in `CGRect#right`" unless dist.is_a?(Numeric)

  options[:right] = dist
  self.apply(options)
end

#roundObject

others



364
365
366
# File 'lib/geomotion/cg_rect.rb', line 364

def round
  CGRect.new([self.origin.x.round, self.origin.y.round], [self.size.width.round, self.size.height.round])
end

#shorter(dist, options = {}) ⇒ Object Also known as: shrink_up



278
279
280
281
282
283
# File 'lib/geomotion/cg_rect.rb', line 278

def shorter(dist, options={})
  raise "You must specify an amount in `CGRect#shorter`" unless dist.is_a?(Numeric)

  options[:shorter] = dist
  self.apply(options)
end

#shrink(size, options = nil) ⇒ Object



462
463
464
465
466
467
468
469
470
471
# File 'lib/geomotion/cg_rect.rb', line 462

def shrink(size, options=nil)
  if size.is_a? Numeric
    size = CGSize.new(size, size)
  end
  rect = CGRectInset(self, size[0], size[1])
  if options
    return rect.apply(options)
  end
  return rect
end

#shrink_down(amount, options = {}) ⇒ Object



482
483
484
485
486
487
# File 'lib/geomotion/cg_rect.rb', line 482

def shrink_down(amount, options={})
  raise "You must specify an amount in `CGRect#shrink_down`" unless amount.is_a?(Numeric)

  options[:shrink_down] = amount
  self.apply(options)
end

#shrink_height(amount, options = {}) ⇒ Object



493
494
495
# File 'lib/geomotion/cg_rect.rb', line 493

def shrink_height(amount, options={})
  return self.shrink([0, amount], options)
end

#shrink_right(amount, options = {}) ⇒ Object



474
475
476
477
478
479
# File 'lib/geomotion/cg_rect.rb', line 474

def shrink_right(amount, options={})
  raise "You must specify an amount in `CGRect#shrink_right`" unless amount.is_a?(Numeric)

  options[:shrink_right] = amount
  self.apply(options)
end

#shrink_width(amount, options = {}) ⇒ Object



489
490
491
# File 'lib/geomotion/cg_rect.rb', line 489

def shrink_width(amount, options={})
  return self.shrink([amount, 0], options)
end

#taller(dist, options = {}) ⇒ Object Also known as: grow_down



271
272
273
274
275
276
# File 'lib/geomotion/cg_rect.rb', line 271

def taller(dist, options={})
  raise "You must specify an amount in `CGRect#taller`" unless dist.is_a?(Numeric)

  options[:taller] = dist
  self.apply(options)
end

#thinner(dist, options = {}) ⇒ Object Also known as: shrink_left



264
265
266
267
268
269
# File 'lib/geomotion/cg_rect.rb', line 264

def thinner(dist, options={})
  raise "You must specify an amount in `CGRect#thinner`" unless dist.is_a?(Numeric)

  options[:thinner] = dist
  self.apply(options)
end

#top_center(absolute = false) ⇒ Object



335
336
337
# File 'lib/geomotion/cg_rect.rb', line 335

def top_center(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width / 2, 0)
end

#top_left(absolute = false) ⇒ Object



331
332
333
# File 'lib/geomotion/cg_rect.rb', line 331

def top_left(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(0, 0)
end

#top_right(absolute = false) ⇒ Object



339
340
341
# File 'lib/geomotion/cg_rect.rb', line 339

def top_right(absolute = false)
  cgrect_offset(absolute) + CGPoint.new(self.size.width, 0)
end

#union_with(rect) ⇒ Object



411
412
413
# File 'lib/geomotion/cg_rect.rb', line 411

def union_with(rect)
  CGRectUnion(self, rect)
end

#up(dist = nil, options = {}) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/geomotion/cg_rect.rb', line 235

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 must specify an amount in `CGRect#up`" unless dist.is_a?(Numeric)

  options[:up] = dist
  self.apply(options)
end

#wider(dist, options = {}) ⇒ Object Also known as: grow_right



257
258
259
260
261
262
# File 'lib/geomotion/cg_rect.rb', line 257

def wider(dist, options={})
  raise "You must specify an amount in `CGRect#wider`" unless dist.is_a?(Numeric)

  options[:wider] = dist
  self.apply(options)
end

#width(setter = nil) ⇒ Object



124
125
126
127
128
129
# File 'lib/geomotion/cg_rect.rb', line 124

def width(setter = nil)
  if setter
    return CGRect.new(self.origin, [setter, self.size.height])
  end
  CGRectGetWidth(self)
end

#width=(_width) ⇒ Object



131
132
133
# File 'lib/geomotion/cg_rect.rb', line 131

def width=(_width)
  self.size.width = _width
end

#x(setter = nil) ⇒ Object

getters/setters



102
103
104
105
106
107
# File 'lib/geomotion/cg_rect.rb', line 102

def x(setter = nil)
  if setter
    return CGRect.new([setter, self.origin.y], self.size)
  end
  min_x
end

#x=(_x) ⇒ Object



109
110
111
# File 'lib/geomotion/cg_rect.rb', line 109

def x=(_x)
  self.origin.x = _x
end

#y(setter = nil) ⇒ Object



113
114
115
116
117
118
# File 'lib/geomotion/cg_rect.rb', line 113

def y(setter = nil)
  if setter
    return CGRect.new([self.origin.x, setter], self.size)
  end
  min_y
end

#y=(_y) ⇒ Object



120
121
122
# File 'lib/geomotion/cg_rect.rb', line 120

def y=(_y)
  self.origin.y = _y
end