sugarcube 0.11.3 → 0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ $sugarcube_view = nil
2
+ $sugarcube_items = nil
3
+ $sugarcube_restore = nil
4
+
1
5
  module SugarCube
2
6
  module Adjust
3
7
  module_function
@@ -14,17 +18,18 @@ module SugarCube
14
18
 
15
19
  def adjust(view=nil, format=nil)
16
20
  SugarCube::Adjust::repl_format(format.to_sym) if format
17
- @@sugarcube_view ||= nil # this looks like a NOOP, but ||= also checks whether the variable exists. aka: errors happen if we don't do this.
18
- return @@sugarcube_view if not view
21
+ return $sugarcube_view if not view
19
22
 
20
23
  if view.is_a? Fixnum
21
- @@sugarcube_items ||= SugarCube::Adjust::build_tree(UIApplication.sharedApplication.keyWindow, :subviews)
22
- view = @@sugarcube_items[view]
24
+ $sugarcube_items ||= SugarCube::Adjust::build_tree(UIApplication.sharedApplication.keyWindow, :subviews)
25
+ view = $sugarcube_items[view]
23
26
  end
24
27
 
28
+ # a/adjust will return this object
29
+ $sugarcube_view = view
30
+
25
31
  if view.is_a?(UIView)
26
- @@sugarcube_view = view
27
- @@sugarcube_restore = {
32
+ $sugarcube_restore = {
28
33
  frame: SugarCube::Adjust.frame,
29
34
  shadow: SugarCube::Adjust.shadow,
30
35
  }
@@ -40,15 +45,14 @@ module SugarCube
40
45
 
41
46
  ##| FRAME
42
47
  def frame f=nil
43
- @@sugarcube_view ||= nil
44
- raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
48
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
45
49
 
46
- return SugarCube::CoreGraphics::Rect(@@sugarcube_view.frame) if not f
50
+ return SugarCube::CoreGraphics::Rect($sugarcube_view.frame) if not f
47
51
 
48
- @@sugarcube_view.frame = f
52
+ $sugarcube_view.frame = f
49
53
  puts format_frame(f)
50
54
 
51
- @@sugarcube_view
55
+ $sugarcube_view
52
56
  end
53
57
  alias f frame
54
58
 
@@ -59,15 +63,14 @@ module SugarCube
59
63
  alias l left
60
64
 
61
65
  def right val=1
62
- @@sugarcube_view ||= nil
63
- raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
66
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
64
67
 
65
- f = @@sugarcube_view.frame
68
+ f = $sugarcube_view.frame
66
69
  f.origin.x += val
67
- @@sugarcube_view.frame = f
70
+ $sugarcube_view.frame = f
68
71
  puts format_frame(f)
69
72
 
70
- @@sugarcube_view
73
+ $sugarcube_view
71
74
  end
72
75
  alias r right
73
76
 
@@ -77,23 +80,21 @@ module SugarCube
77
80
  alias u up
78
81
 
79
82
  def down val=1
80
- @@sugarcube_view ||= nil
81
- raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
83
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
82
84
 
83
- f = @@sugarcube_view.frame
85
+ f = $sugarcube_view.frame
84
86
  f.origin.y += val
85
- @@sugarcube_view.frame = f
87
+ $sugarcube_view.frame = f
86
88
  puts format_frame(f)
87
89
 
88
- @@sugarcube_view
90
+ $sugarcube_view
89
91
  end
90
92
  alias d down
91
93
 
92
94
  def origin x=nil, y=nil
93
- @@sugarcube_view ||= nil
94
- raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
95
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
95
96
 
96
- f = @@sugarcube_view.frame
97
+ f = $sugarcube_view.frame
97
98
  return SugarCube::CoreGraphics::Point(f.origin) if not x
98
99
 
99
100
  if y
@@ -102,10 +103,10 @@ module SugarCube
102
103
  else
103
104
  f.origin = x
104
105
  end
105
- @@sugarcube_view.frame = f
106
+ $sugarcube_view.frame = f
106
107
  puts format_frame(f)
107
108
 
108
- @@sugarcube_view
109
+ $sugarcube_view
109
110
  end
110
111
  alias o origin
111
112
 
@@ -116,15 +117,14 @@ module SugarCube
116
117
  alias n thinner
117
118
 
118
119
  def wider val=1
119
- @@sugarcube_view ||= nil
120
- raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
120
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
121
121
 
122
- f = @@sugarcube_view.frame
122
+ f = $sugarcube_view.frame
123
123
  f.size.width += val
124
- @@sugarcube_view.frame = f
124
+ $sugarcube_view.frame = f
125
125
  puts format_frame(f)
126
126
 
127
- @@sugarcube_view
127
+ $sugarcube_view
128
128
  end
129
129
  alias w wider
130
130
 
@@ -134,23 +134,21 @@ module SugarCube
134
134
  alias s shorter
135
135
 
136
136
  def taller val=1
137
- @@sugarcube_view ||= nil
138
- raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
137
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
139
138
 
140
- f = @@sugarcube_view.frame
139
+ f = $sugarcube_view.frame
141
140
  f.size.height += val
142
- @@sugarcube_view.frame = f
141
+ $sugarcube_view.frame = f
143
142
  puts format_frame(f)
144
143
 
145
- @@sugarcube_view
144
+ $sugarcube_view
146
145
  end
147
146
  alias t taller
148
147
 
149
148
  def size w=nil, h=nil
150
- @@sugarcube_view ||= nil
151
- raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
149
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
152
150
 
153
- f = @@sugarcube_view.frame
151
+ f = $sugarcube_view.frame
154
152
  return SugarCube::CoreGraphics::Size(f.size) if not w
155
153
 
156
154
  if h
@@ -159,16 +157,15 @@ module SugarCube
159
157
  else
160
158
  f.size = w
161
159
  end
162
- @@sugarcube_view.frame = f
160
+ $sugarcube_view.frame = f
163
161
  puts format_frame(f)
164
162
 
165
- @@sugarcube_view
163
+ $sugarcube_view
166
164
  end
167
165
  alias z size
168
166
 
169
167
  def center(*args)
170
- @@sugarcube_view ||= nil
171
- raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
168
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
172
169
 
173
170
  element = nil
174
171
  total = nil
@@ -192,7 +189,7 @@ module SugarCube
192
189
  element = 1 unless element
193
190
  total = 1 unless total
194
191
 
195
- view = @@sugarcube_view
192
+ view = $sugarcube_view
196
193
 
197
194
  left = view.origin.x
198
195
  top = view.origin.y
@@ -214,8 +211,7 @@ module SugarCube
214
211
 
215
212
  ##| SHADOW
216
213
  def shadow shadow=nil
217
- @@sugarcube_view ||= nil
218
- raise "no view has been assigned to SugarCube::Adjust::adjust" unless @@sugarcube_view
214
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
219
215
 
220
216
  if shadow
221
217
  {
@@ -229,19 +225,19 @@ module SugarCube
229
225
  if key == :color and [Symbol, Fixnum, NSString, UIImage, UIColor].any?{|klass| value.is_a? klass}
230
226
  value = value.uicolor.CGColor
231
227
  end
232
- @@sugarcube_view.layer.send(msg, value)
233
- @@sugarcube_view.layer.masksToBounds = false
234
- @@sugarcube_view.layer.shouldRasterize = true
228
+ $sugarcube_view.layer.send(msg, value)
229
+ $sugarcube_view.layer.masksToBounds = false
230
+ $sugarcube_view.layer.shouldRasterize = true
235
231
  end
236
232
  }
237
- @@sugarcube_view
233
+ $sugarcube_view
238
234
  else
239
235
  {
240
- opacity: @@sugarcube_view.layer.shadowOpacity,
241
- radius: @@sugarcube_view.layer.shadowRadius,
242
- offset: @@sugarcube_view.layer.shadowOffset,
243
- color: @@sugarcube_view.layer.shadowColor,
244
- path: @@sugarcube_view.layer.shadowPath,
236
+ opacity: $sugarcube_view.layer.shadowOpacity,
237
+ radius: $sugarcube_view.layer.shadowRadius,
238
+ offset: $sugarcube_view.layer.shadowOffset,
239
+ color: $sugarcube_view.layer.shadowColor,
240
+ path: $sugarcube_view.layer.shadowPath,
245
241
  }
246
242
  end
247
243
  end
@@ -276,16 +272,18 @@ module SugarCube
276
272
  end
277
273
  ret
278
274
  }
275
+ elsif item.is_a? CALayer
276
+ selector = :sublayers
279
277
  else
280
278
  raise "Unable to determine a SugarCube::Adjust::tree selector for #{item.class.name}"
281
279
  end
282
280
  end
283
281
 
284
- @@sugarcube_items = SugarCube::Adjust::build_tree(item, selector)
282
+ $sugarcube_items = SugarCube::Adjust::build_tree(item, selector)
285
283
  if self.respond_to? :draw_tree
286
- total = draw_tree(item, selector)
284
+ draw_tree(item, selector)
287
285
  else
288
- total = SugarCube::Adjust::draw_tree(item, selector)
286
+ SugarCube::Adjust::draw_tree(item, selector)
289
287
  end
290
288
  puts ''
291
289
 
@@ -317,7 +315,7 @@ module SugarCube
317
315
  tab = ''
318
316
  end
319
317
 
320
- if self == item
318
+ if self == item || $sugarcube_view == item
321
319
  print "\033[1m"
322
320
  end
323
321
  if item.is_a? UIView
@@ -325,7 +323,7 @@ module SugarCube
325
323
  else
326
324
  puts item.to_s
327
325
  end
328
- if self == item
326
+ if self == item || $sugarcube_view == item
329
327
  print "\033[0m"
330
328
  end
331
329
 
@@ -334,9 +332,15 @@ module SugarCube
334
332
  else
335
333
  items = item.send(selector)
336
334
  end
335
+ items ||= []
336
+
337
337
  items.each_with_index { |subview, index|
338
338
  items_index += 1
339
- items_index = SugarCube::Adjust::draw_tree(subview, selector, tab, index == items.length - 1, items_index)
339
+ if self.respond_to? :draw_tree
340
+ items_index = draw_tree(subview, selector, tab, index == items.length - 1, items_index)
341
+ else
342
+ items_index = SugarCube::Adjust::draw_tree(subview, selector, tab, index == items.length - 1, items_index)
343
+ end
340
344
  }
341
345
 
342
346
  return items_index
@@ -348,6 +352,8 @@ module SugarCube
348
352
  else
349
353
  items = item.send(selector)
350
354
  end
355
+ items ||= []
356
+
351
357
  ret = [item]
352
358
  items.each_with_index { |subview, index|
353
359
  ret.concat SugarCube::Adjust::build_tree(subview, selector)
@@ -361,10 +367,9 @@ module SugarCube
361
367
 
362
368
  ##| RESTORE
363
369
  def restore
364
- @@sugarcube_view ||= nil
365
- raise 'no view has been assigned to SugarCube::Adjust::adjust' unless @@sugarcube_view
370
+ raise 'no view has been assigned to SugarCube::Adjust::adjust' unless $sugarcube_view
366
371
 
367
- @@sugarcube_restore.each do |msg, value|
372
+ $sugarcube_restore.each do |msg, value|
368
373
  SugarCube::Adjust.send(msg, value)
369
374
  end
370
375
  end
@@ -0,0 +1,8 @@
1
+ class CALayer
2
+
3
+ def << (layer)
4
+ self.addSublayer layer
5
+ return self
6
+ end
7
+
8
+ end
@@ -1,426 +1,49 @@
1
- module SugarCube
2
- # Extensions to make CGRect a "real class"
3
- module CGRectExtensions
4
- module ClassMethods
5
- def empty
6
- SugarCube::CoreGraphics::Rect(CGRectZero)
7
- end
8
-
9
- def null
10
- SugarCube::CoreGraphics::Rect(CGRectNull)
11
- end
12
-
13
- def infinite
14
- SugarCube::CoreGraphics::Rect([0, 0], CGSize.infinite)
15
- end
16
-
17
- def from_hash(hash)
18
- ret = Pointer.new(CGRect.type)
19
- if CGRectMakeWithDictionaryRepresentation(hash, ret)
20
- ret[0]
21
- else
22
- nil
23
- end
24
- end
25
-
26
- def from_s(s)
27
- CGRectFromString(s)
28
- end
29
- end
30
-
31
- def self.included(base)
32
- base.extend(ClassMethods)
33
- end
34
-
35
- def empty?
36
- CGRectIsEmpty(self)
37
- end
38
-
39
- def infinite?
40
- self.size.infinite?
41
- end
42
-
43
- def null?
44
- CGRectIsNull(self)
45
- end
46
-
47
- def left
48
- return CGRectGetMinX(self)
49
- end
50
-
51
- def right
52
- return CGRectGetMaxX(self)
53
- end
54
-
55
- def width
56
- return CGRectGetWidth(self)
57
- end
58
-
59
- def top
60
- return CGRectGetMinY(self)
61
- end
62
-
63
- def bottom
64
- return CGRectGetMaxY(self)
65
- end
66
-
67
- def height
68
- return CGRectGetHeight(self)
69
- end
70
-
71
- def center
72
- return SugarCube::CoreGraphics::Point(CGRectGetMidX(self), CGRectGetMidY(self))
73
- end
74
-
75
- def to_s
76
- NSStringFromCGRect(self)
77
- end
78
-
79
- def inspect
80
- "#{self.class.name}([#{self.origin.x}, #{self.origin.y}],{#{self.size.width} × #{self.size.height}})"
81
- end
82
-
83
- def to_hash
84
- CGRectCreateDictionaryRepresentation(self)
85
- end
86
-
87
- # # returns an intersection of self and rect, or moves the Rect using Point,
88
- # or increases the size using Size
89
- def +(rect)
90
- case rect
91
- when SugarCube::CoreGraphics::Rect, CGRect
92
- SugarCube::CoreGraphics::Rect(CGRectUnion(self, rect))
93
- when SugarCube::CoreGraphics::Point, CGPoint
94
- SugarCube::CoreGraphics::Rect(CGRectOffset(self, rect.x, rect.y))
95
- when SugarCube::CoreGraphics::Offset, UIOffset
96
- SugarCube::CoreGraphics::Rect(CGRectOffset(self, rect.horizontal, rect.vertical))
97
- when SugarCube::CoreGraphics::Size, CGSize
98
- SugarCube::CoreGraphics::Rect(CGRectInset(self, - rect.width, - rect.height))
99
- when SugarCube::CoreGraphics::EdgeInsets, UIEdgeInsets
100
- SugarCube::CoreGraphics::Rect(UIEdgeInsetsInsetRect(self, rect))
101
- else
102
- super
103
- end
104
- end
105
-
106
- def intersection(rect)
107
- SugarCube::CoreGraphics::Rect(CGRectIntersection(self, rect))
108
- end
109
-
110
- def intersects?(rect_or_point)
111
- case rect_or_point
112
- when SugarCube::CoreGraphics::Point, CGPoint
113
- CGRectContainsPoint(self, rect_or_point)
114
- when Array, CGRect
115
- CGRectIntersectsRect(self, rect_or_point)
116
- else
117
- super
118
- end
119
- end
120
-
121
- def contains?(rect_or_point)
122
- case rect_or_point
123
- when SugarCube::CoreGraphics::Point, CGPoint
124
- CGRectContainsPoint(self, rect_or_point)
125
- when Array, CGRect
126
- CGRectContainsRect(self, rect_or_point)
127
- else
128
- super
129
- end
130
- end
131
-
132
- def ==(rect)
133
- CGRectEqualToRect(self, rect)
134
- end
135
-
136
- end
137
-
138
- # Extensions to make CGPoint a "real class"
139
- module CGPointExtensions
140
- module ClassMethods
141
- def from_hash(hash)
142
- ret = Pointer.new(CGPoint.type)
143
- if CGPointMakeWithDictionaryRepresentation(hash, ret)
144
- ret[0]
145
- else
146
- nil
147
- end
148
- end
149
-
150
- def from_s(s)
151
- CGPointFromString(s)
152
- end
153
- end
154
-
155
- def self.included(base)
156
- base.extend(ClassMethods)
157
- end
158
-
159
- def intersects?(rect)
160
- CGRectContainsPoint(rect, self)
161
- end
162
-
163
- def ==(point)
164
- CGPointEqualToPoint(self, point)
165
- end
166
-
167
- def to_s
168
- NSStringFromCGPoint(self)
169
- end
170
-
171
- def inspect
172
- "#{self.class.name}(#{self.x}, #{self.y})"
173
- end
174
-
175
- def to_hash
176
- CGPointCreateDictionaryRepresentation(self)
177
- end
178
-
179
- end
180
-
181
- # Extensions to make CGSize a "real class"
182
- module CGSizeExtensions
183
- module ClassMethods
184
- def infinite
185
- infinity = CGRect.null[0][0]
186
- SugarCube::CoreGraphics::Size(infinity, infinity)
187
- end
188
-
189
- def from_hash(hash)
190
- ret = Pointer.new(CGSize.type)
191
- if CGSizeMakeWithDictionaryRepresentation(hash, ret)
192
- ret[0]
193
- else
194
- nil
195
- end
196
- end
197
-
198
- def from_s(s)
199
- CGSizeFromString(s)
200
- end
201
- end
202
-
203
- def self.included(base)
204
- base.extend(ClassMethods)
205
- end
206
-
207
- def infinite?
208
- infinity = CGRect.null[0][0]
209
- self.width == infinity or self.height == infinity
210
- end
211
-
212
- def ==(size)
213
- CGSizeEqualToSize(self, size)
214
- end
215
-
216
- def to_s
217
- NSStringFromCGSize(self)
218
- end
219
-
220
- def inspect
221
- "#{self.class.name}(#{self.width} × #{self.height})"
222
- end
223
-
224
- def to_hash
225
- CGSizeCreateDictionaryRepresentation(self)
226
- end
227
-
228
- end
229
- end
230
-
231
-
232
- class CGRect
233
- include SugarCube::CGRectExtensions
234
- end
235
-
236
-
237
- class CGPoint
238
- include SugarCube::CGPointExtensions
239
- end
240
-
241
-
242
- class CGSize
243
- include SugarCube::CGSizeExtensions
244
- end
245
-
246
-
247
1
  module SugarCube
248
2
  module CoreGraphics
249
- PI = 3.141592654
250
- PHI = 1.618033989
251
- E = 2.718281828
252
-
253
- class Rect < Array
254
- include SugarCube::CGRectExtensions
255
-
256
- def initialize args
257
- if args.length == 4
258
- super [Point.new([args[0], args[1]]), Size.new([args[2], args[3]])]
259
- else
260
- unless args[0].is_a? Point
261
- args[0] = SugarCube::CoreGraphics::Point(args[0])
262
- end
263
- unless args[1].is_a? Size
264
- args[1] = SugarCube::CoreGraphics::Size(args[1])
265
- end
266
- super [args[0], args[1]]
267
- end
268
- end
269
-
270
- def origin
271
- return self[0]
272
- end
273
-
274
- def origin= val
275
- self[0] = SugarCube::CoreGraphics::Point(val)
276
- end
277
-
278
- def size
279
- return self[1]
280
- end
281
-
282
- def size= val
283
- self[1] = SugarCube::CoreGraphics::Size(val)
284
- end
285
- end
286
-
287
-
288
- class Point < Array
289
- include SugarCube::CGPointExtensions
290
-
291
- def x
292
- return self[0]
293
- end
294
-
295
- def x= val
296
- self[0] = val
297
- end
298
-
299
- def y
300
- return self[1]
301
- end
302
-
303
- def y= val
304
- self[1] = val
305
- end
306
-
307
- # adds a vector to this point, or creates a Rect by adding a size
308
- def +(point)
309
- case point
310
- when SugarCube::CoreGraphics::Point, CGPoint
311
- x = self.x + point.x
312
- y = self.y + point.y
313
- Point[x, y]
314
- when SugarCube::CoreGraphics::Size, CGSize
315
- Rect[self, point]
316
- else
317
- super
318
- end
319
- end
320
- end
321
-
322
-
323
- class Size < Array
324
- include SugarCube::CGSizeExtensions
325
-
326
- def width
327
- return self[0]
328
- end
329
-
330
- def width= val
331
- self[0] = val
332
- end
333
-
334
- def height
335
- return self[1]
336
- end
337
-
338
- def height= val
339
- self[1] = val
340
- end
3
+ module_function
341
4
 
342
- # adds the sizes
343
- def +(size)
344
- case size
345
- when Size, CGSize
346
- width = self.width + size.width
347
- height = self.height + size.height
348
- Size[width, height]
349
- when Point, CGPoint
350
- Rect[size, self]
5
+ # Accepts 1 or 2 arguments
6
+ # 1 argument should be a CGPoint, CGSize, Array[Numeric, Numeric] or UIOffset
7
+ # 2 arguments should be Numeric, Numeric
8
+ def Point(x_or_origin, y=nil)
9
+ unless y
10
+ case x_or_origin
11
+ when CGPoint
12
+ x = x_or_origin.x
13
+ y = x_or_origin.y
14
+ when CGSize
15
+ x = x_or_origin.width
16
+ y = x_or_origin.height
17
+ when UIOffset
18
+ x = x_or_origin.horizontal
19
+ y = x_or_origin.vertical
20
+ when Array
21
+ x = x_or_origin[0]
22
+ y = x_or_origin[1]
351
23
  else
352
- super
24
+ raise RuntimeError.new("Invalid argument sent to Point(#{x_or_origin.inspect})")
353
25
  end
26
+ else
27
+ x = x_or_origin
354
28
  end
355
-
356
- end
357
-
358
-
359
- class EdgeInsets < Array
360
-
361
- def top
362
- return self[0]
363
- end
364
-
365
- def top= val
366
- self[0] = val
367
- end
368
-
369
- def left
370
- return self[1]
371
- end
372
-
373
- def left= val
374
- self[1] = val
375
- end
376
-
377
- def bottom
378
- return self[2]
379
- end
380
-
381
- def bottom= val
382
- self[2] = val
383
- end
384
-
385
- def right
386
- return self[3]
387
- end
388
-
389
- def right= val
390
- self[3] = val
391
- end
392
-
393
- end
394
-
395
-
396
- class Offset < Array
397
-
398
- def horizontal
399
- return self[0]
400
- end
401
-
402
- def horizontal= val
403
- self[0] = val
404
- end
405
-
406
- def vertical
407
- return self[1]
408
- end
409
-
410
- def vertical= val
411
- self[1] = val
412
- end
413
-
29
+ return CGPoint.new(x, y)
414
30
  end
415
31
 
416
- module_function
417
-
32
+ # Accepts 1 or 2 arguments
33
+ # 1 argument should be a CGPoint, CGSize, Array[Numeric, Numeric] or UIOffset
34
+ # 2 arguments should be Numeric, Numeric
418
35
  def Size(w_or_size, h=nil)
419
- if not h
36
+ unless h
420
37
  case w_or_size
421
38
  when CGSize
422
39
  w = w_or_size.width
423
40
  h = w_or_size.height
41
+ when CGPoint
42
+ w = w_or_size.x
43
+ h = w_or_size.y
44
+ when UIOffset
45
+ w = w_or_size.horizontal
46
+ h = w_or_size.vertical
424
47
  when Array
425
48
  w = w_or_size[0]
426
49
  h = w_or_size[1]
@@ -430,33 +53,17 @@ module SugarCube
430
53
  else
431
54
  w = w_or_size
432
55
  end
433
- return Size.new([w, h])
434
- end
435
-
436
- def Point(x_or_origin, y=nil)
437
- if not y
438
- case x_or_origin
439
- when CGPoint
440
- x = x_or_origin.x
441
- y = x_or_origin.y
442
- when Array
443
- x = x_or_origin[0]
444
- y = x_or_origin[1]
445
- else
446
- raise RuntimeError.new("Invalid argument sent to Point(#{x_or_origin.inspect})")
447
- end
448
- else
449
- x = x_or_origin
450
- end
451
- return Point.new([x, y])
56
+ return CGSize.new(w, h)
452
57
  end
453
58
 
454
59
  # Accepts 1, 2 or 4 arguments.
455
- # 1 argument should be an Array[4], Rect, or CGRect
456
- # 2 arguments should be a Point/CGPoint and a Size/CGSize,
60
+ # 1 argument should be an Array[Numeric*4], Array[[Numeric*2],[Numeric*2]], CGRect, UIView or CALayar
61
+ # 2 arguments should be a CGPoint and a CGSize,
62
+ # 3 arguments should be either Numeric, Numeric, Size or Point, Numeric, Numeric
457
63
  # 4 should be x, y, w, h
458
64
  def Rect(x_or_origin, y_or_size=nil, w=nil, h=nil)
459
- if not y_or_size
65
+ # one arg
66
+ if y_or_size.nil? and w.nil? and h.nil?
460
67
  case x_or_origin
461
68
  when CGRect
462
69
  x = x_or_origin.origin.x
@@ -468,11 +75,6 @@ module SugarCube
468
75
  y = x_or_origin.frame.origin.y
469
76
  w = x_or_origin.frame.size.width
470
77
  h = x_or_origin.frame.size.height
471
- when Rect
472
- x = x_or_origin[0][0]
473
- y = x_or_origin[0][1]
474
- w = x_or_origin[1][0]
475
- h = x_or_origin[1][1]
476
78
  when Array
477
79
  if x_or_origin.length == 2
478
80
  x = x_or_origin[0][0]
@@ -490,69 +92,115 @@ module SugarCube
490
92
  else
491
93
  raise RuntimeError.new("Invalid argument sent to Rect(#{x_or_origin.inspect})")
492
94
  end
493
- elsif not w and not h
494
- x_or_origin = SugarCube::CoreGraphics::Point(x_or_origin) unless x_or_origin.is_a? SugarCube::CoreGraphics::Point
95
+ # two args
96
+ elsif w.nil? and h.nil?
97
+ x_or_origin = SugarCube::CoreGraphics::Point(x_or_origin) unless x_or_origin.is_a? CGPoint
495
98
  x = x_or_origin.x
496
99
  y = x_or_origin.y
497
- y_or_size = SugarCube::CoreGraphics::Size(y_or_size) unless y_or_size.is_a? SugarCube::CoreGraphics::Size
100
+ y_or_size = SugarCube::CoreGraphics::Size(y_or_size) unless y_or_size.is_a? CGSize
498
101
  w = y_or_size.width
499
102
  h = y_or_size.height
103
+ # three args
104
+ elsif h.nil?
105
+ if x_or_origin.is_a? Numeric
106
+ # x_or_origin: x, y_or_size: y, w: size
107
+ point = SugarCube::CoreGraphics::Point(x_or_origin, y_or_size)
108
+ size = w
109
+ return SugarCube::CoreGraphics::Rect(point, size)
110
+ elsif w.is_a? Numeric
111
+ # x_or_origin: point, y_or_size: w, w: h
112
+ point = x_or_origin
113
+ size = SugarCube::CoreGraphics::Size(y_or_size, w)
114
+ return SugarCube::CoreGraphics::Rect(point, size)
115
+ else
116
+ raise RuntimeError.new("Invalid arguments sent to Rect(#{x_or_origin.inspect}, #{y_or_size.inspect}, #{w.inspect})")
117
+ end
500
118
  else
501
119
  x = x_or_origin
502
120
  y = y_or_size
503
121
  end
504
- return Rect.new([[x, y], [w, h]])
122
+ return CGRect.new([x, y], [w, h])
505
123
  end
506
124
 
507
- # Accepts 1 or 4 arguments.
508
- # 1 argument should be an Array[4] or UIEdgeInset,
125
+ # Accepts 1..4 arguments.
126
+ # 1 argument should be an Array[Numeric * 1..4], UIEdgeInset, or Numeric
127
+ # 2 arguments should be top/bottom, left/right
128
+ # 3 arguments should be top, left/right, bottom
509
129
  # 4 should be top, left, bottom, right
510
130
  def EdgeInsets(top_or_inset, left=nil, bottom=nil, right=nil)
511
- unless left or bottom or right
131
+ if left.nil? and bottom.nil? and right.nil?
512
132
  case top_or_inset
513
133
  when UIEdgeInsets
514
- top = top.top
515
- left = top.left
516
- bottom = top.bottom
517
- right = top.right
134
+ top = top_or_inset.top
135
+ left = top_or_inset.left
136
+ bottom = top_or_inset.bottom
137
+ right = top_or_inset.right
138
+ when CGRect
139
+ # converting a rect to insets is hard to visualize,
140
+ # but it goes something like this:
141
+ # +-----------.....----------+
142
+ # | origin |y | top inset |
143
+ # |--------+ V |
144
+ # | x .......... |
145
+ # . . . .
146
+ # .-------->. . right .
147
+ # . left . . inset .
148
+ # . inset . .<------.
149
+ # . . . .
150
+ # | .......... w |
151
+ # | ^ +------|
152
+ # | bottom inset| h| size |
153
+ # +-----------.....----------+
154
+ left = top_or_inset.origin.x
155
+ top = top_or_inset.origin.y
156
+ right = top_or_inset.size.width
157
+ bottom = top_or_inset.size.height
518
158
  when Array
519
- top = top_or_inset[0]
520
- left = top_or_inset[1]
521
- bottom = top_or_inset[2]
522
- right = top_or_inset[3]
159
+ # okay, in this ONE case, I use the splat operator.
160
+ return SugarCube::CoreGraphics::EdgeInsets(*top_or_inset)
523
161
  when Numeric
524
162
  top = left = bottom = right = top_or_inset
525
163
  else
526
164
  raise RuntimeError.new("Invalid argument sent to EdgeInsets(#{top_or_inset.inspect})")
527
165
  end
166
+ elsif bottom.nil? and right.nil?
167
+ return SugarCube::CoreGraphics::EdgeInsets(top_or_inset, left, top_or_inset, left)
168
+ elsif right.nil?
169
+ return SugarCube::CoreGraphics::EdgeInsets(top_or_inset, left, bottom, left)
528
170
  else
529
171
  top = top_or_inset
530
172
  end
531
- return EdgeInsets.new([top, left, bottom, right])
173
+ return UIEdgeInsets.new(top, left, bottom, right)
532
174
  end
533
175
 
534
176
  # Accepts 1 or 2 arguments.
535
- # 1 argument should be an Array[2] or UIOffset,
177
+ # 1 argument should be an Array[2], UIOffset, CGPoint, CGSize, or Numeric
536
178
  # 2 should be horizontal, vertical
537
179
  def Offset(horizontal_or_offset, vertical=nil)
538
- if not vertical
180
+ unless vertical
539
181
  case horizontal_or_offset
540
182
  when UIOffset
541
183
  horizontal = horizontal_or_offset.horizontal
542
184
  vertical = horizontal_or_offset.vertical
185
+ when CGPoint
186
+ horizontal = horizontal_or_offset.x
187
+ vertical = horizontal_or_offset.y
188
+ when CGSize
189
+ horizontal = horizontal_or_offset.width
190
+ vertical = horizontal_or_offset.height
543
191
  when Array
544
192
  horizontal = horizontal_or_offset[0]
545
193
  vertical = horizontal_or_offset[1]
546
- when Fixnum
547
- horizontal_or_offset =
548
- vertical = horizontal_or_offset[0]
194
+ when Numeric
195
+ horizontal = horizontal_or_offset
196
+ vertical = horizontal_or_offset
549
197
  else
550
198
  raise RuntimeError.new("Invalid argument sent to Offset(#{horizontal_or_offset.inspect})")
551
199
  end
552
200
  else
553
201
  horizontal = horizontal_or_offset
554
202
  end
555
- return Offset.new([horizontal, vertical])
203
+ return UIOffset.new(horizontal, vertical)
556
204
  end
557
205
 
558
206
  end