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



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

.infiniteObject



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



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(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



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

#*(scale) ⇒ Object



282
283
284
285
286
287
288
289
# File 'lib/geomotion/cg_rect.rb', line 282

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

#+(other) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/geomotion/cg_rect.rb', line 267

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



376
377
378
# File 'lib/geomotion/cg_rect.rb', line 376

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

#-@Object



372
373
374
# File 'lib/geomotion/cg_rect.rb', line 372

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



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

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

#==(rect) ⇒ Object



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

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

#above(margin, height: height) ⇒ Object

adjacent rects



179
180
181
# File 'lib/geomotion/cg_rect.rb', line 179

def above(margin = 0)
  self.above(margin, height:self.height)
end

#before(margin, width: width) ⇒ Object



195
196
197
# File 'lib/geomotion/cg_rect.rb', line 195

def before(margin = 0)
  self.before(margin, width:self.width)
end

#below(margin, height: height) ⇒ Object



187
188
189
# File 'lib/geomotion/cg_rect.rb', line 187

def below(margin = 0)
  self.below(margin, height:self.height)
end

#beside(margin, width: width) ⇒ Object



203
204
205
# File 'lib/geomotion/cg_rect.rb', line 203

def beside(margin = 0)
  self.beside(margin, width: self.width)
end

#bottom_center(absolute = false) ⇒ Object



246
247
248
# File 'lib/geomotion/cg_rect.rb', line 246

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

#bottom_left(absolute = false) ⇒ Object



250
251
252
# File 'lib/geomotion/cg_rect.rb', line 250

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

#bottom_right(absolute = false) ⇒ Object



242
243
244
# File 'lib/geomotion/cg_rect.rb', line 242

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

#center(absolute = false) ⇒ Object



222
223
224
# File 'lib/geomotion/cg_rect.rb', line 222

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

#center_left(absolute = false) ⇒ Object



254
255
256
# File 'lib/geomotion/cg_rect.rb', line 254

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

#center_right(absolute = false) ⇒ Object



238
239
240
# File 'lib/geomotion/cg_rect.rb', line 238

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

#centered_in(rect, absolute = false) ⇒ Object



263
264
265
# File 'lib/geomotion/cg_rect.rb', line 263

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

#contains?(rect_or_point) ⇒ Boolean

Returns:

  • (Boolean)


357
358
359
360
361
362
363
364
365
366
# File 'lib/geomotion/cg_rect.rb', line 357

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



158
159
160
# File 'lib/geomotion/cg_rect.rb', line 158

def down(dist = 0)
  CGRect.new([self.x, self.y + dist], self.size)
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  CGRectIsEmpty(self)
end

#grow(size) ⇒ Object



322
323
324
325
326
327
# File 'lib/geomotion/cg_rect.rb', line 322

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

#height(setter = nil) ⇒ Object



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

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

#height=(_height) ⇒ Object



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

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

#infinite?Boolean

Returns:

  • (Boolean)


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

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

#inset(insets) ⇒ Object



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

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

#inspectObject



380
381
382
# File 'lib/geomotion/cg_rect.rb', line 380

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

#intersection_with(rect) ⇒ Object



302
303
304
# File 'lib/geomotion/cg_rect.rb', line 302

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

#intersects?(rect) ⇒ Boolean

Returns:

  • (Boolean)


348
349
350
351
352
353
354
355
# File 'lib/geomotion/cg_rect.rb', line 348

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

#left(dist = 0) ⇒ Object

modified rects



146
147
148
# File 'lib/geomotion/cg_rect.rb', line 146

def left(dist = 0)
  CGRect.new([self.x - dist, self.y], self.size)
end

#max_xObject



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

def max_x
  CGRectGetMaxX(self)
end

#max_yObject



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

def max_y
  CGRectGetMaxY(self)
end

#mid_xObject



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

def mid_x
  CGRectGetMidX(self)
end

#mid_yObject



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

def mid_y
  CGRectGetMidY(self)
end

#min_xObject

bounds



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

def min_x
  CGRectGetMinX(self)
end

#min_yObject



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

def min_y
  CGRectGetMinY(self)
end

#null?Boolean

Returns:

  • (Boolean)


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

def null?
  CGRectIsNull(self)
end

#offset(point_or_x, y = nil) ⇒ Object



314
315
316
317
318
319
320
# File 'lib/geomotion/cg_rect.rb', line 314

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 = 0) ⇒ Object



150
151
152
# File 'lib/geomotion/cg_rect.rb', line 150

def right(dist = 0)
  CGRect.new([self.x + dist, self.y], self.size)
end

#roundObject

others



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

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

#shorter(dist) ⇒ Object



174
175
176
# File 'lib/geomotion/cg_rect.rb', line 174

def shorter(dist)
  CGRect.new(self.origin, [self.width, self.height - dist])
end

#shrink(size) ⇒ Object



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

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

#taller(dist) ⇒ Object



170
171
172
# File 'lib/geomotion/cg_rect.rb', line 170

def taller(dist)
  CGRect.new(self.origin, [self.width, self.height + dist])
end

#thinner(dist) ⇒ Object



166
167
168
# File 'lib/geomotion/cg_rect.rb', line 166

def thinner(dist)
  CGRect.new(self.origin, [self.width - dist, self.height])
end

#top_center(absolute = false) ⇒ Object



230
231
232
# File 'lib/geomotion/cg_rect.rb', line 230

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

#top_left(absolute = false) ⇒ Object



226
227
228
# File 'lib/geomotion/cg_rect.rb', line 226

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

#top_right(absolute = false) ⇒ Object



234
235
236
# File 'lib/geomotion/cg_rect.rb', line 234

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

#union_with(rect) ⇒ Object



306
307
308
# File 'lib/geomotion/cg_rect.rb', line 306

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

#up(dist = 0) ⇒ Object



154
155
156
# File 'lib/geomotion/cg_rect.rb', line 154

def up(dist = 0)
  CGRect.new([self.x, self.y - dist], self.size)
end

#wider(dist) ⇒ Object



162
163
164
# File 'lib/geomotion/cg_rect.rb', line 162

def wider(dist)
  CGRect.new(self.origin, [self.width + dist, self.height])
end

#width(setter = nil) ⇒ Object



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

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

#width=(_width) ⇒ Object



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

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

#x(setter = nil) ⇒ Object

getters/setters



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

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

#x=(_x) ⇒ Object



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

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

#y(setter = nil) ⇒ Object



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

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

#y=(_y) ⇒ Object



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

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