sugarcube 0.5 → 0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -173,8 +173,41 @@ self.view.show # => self.hidden = false
173
173
  self.view.hide # => self.hidden = true
174
174
  ```
175
175
 
176
- View factories
177
- ---------
176
+ ###### Animations
177
+
178
+ jQuery-like animation methods.
179
+
180
+ ```ruby
181
+ # default timeout is 0.3
182
+ view.fade_out { |view|
183
+ view.removeFromSuperview
184
+ }
185
+ # options:
186
+ view.fade_out(0.5, delay: 0,
187
+ options: UIViewAnimationOptionCurveLinear,
188
+ opacity: 0.5) { |view|
189
+ view.removeFromSuperview
190
+ }
191
+
192
+ view.move_to([0, 100]) # move to position 0, 100
193
+ view.delta_to([0, 100]) # move over 0, down 100, from current position
194
+
195
+ view.slide :left # slides the entire view one "page" to the left, right, up, or down
196
+
197
+ view.shake # shakes the view.
198
+ # options w/ default values:
199
+ shake offset: 8, # move 8 px left, and 8 px right
200
+ repeat: 3, # three times
201
+ duration: 0.3, # for a total of 0.3 seconds
202
+ keypath: 'transform.translate.x'
203
+
204
+ # vigorous nodding - modifying transform.translation.y:
205
+ view.shake offset: 20, repeat: 10, duration: 5, keypath: 'transform.translation.y'
206
+ # an adorable wiggle - modifying transform.rotation:
207
+ superview.shake offset: 0.1, repeat: 2, duration: 0.5, keypath: 'transform.rotation'
208
+ ```
209
+
210
+ ###### View factories
178
211
 
179
212
  ```ruby
180
213
  UIButton.buttonWithType(:custom.uibuttontype)
@@ -303,61 +336,109 @@ test[:my] = 'new'
303
336
 
304
337
  ###### Is it `CGMakeRect` or `CGRectMake`?
305
338
 
306
- Instead, just use `Rect`, `Size` and `Point`. These are namespaced in
307
- `SugarCube::CoreGraphics` module, but I recommend you `include
308
- SugarCube::CoreGraphics` in app_delegate.rb.
339
+ Instead, just use `Rect`, `Size` and `Point`. They will happily convert most
340
+ sensible arguments into a `Rect/Size/Point`, which can be treated as a `CGRect`
341
+ object OR as an `Array` (woah).
342
+
343
+ These are namespaced in `SugarCube::CoreGraphics` module, but I recommend you
344
+ `include SugarCube::CoreGraphics` in app_delegate.rb.
309
345
 
310
346
  ```ruby
311
- f = Rect(view.frame) # converts a CGRect into a CGRectArray
312
- o = Point(view.frame.origin) # converts a CGPoint into a CGPointArray
313
- s = Size(view.frame.size) # converts a CGSize into a CGSizeArray
347
+ f = Rect(view.frame) # converts a CGRect into a Rect
348
+ o = Point(view.frame.origin) # converts a CGPoint into a Point
349
+ s = Size(view.frame.size) # converts a CGSize into a Size
314
350
 
315
351
  # lots of other conversions are possible.
352
+ # a UIView or CALayer => view.frame
353
+ f = Rect(view)
316
354
  # 4 numbers
317
355
  f = Rect(x, y, w, h)
318
356
  # or two arrays
319
357
  p = Point(x, y) # or just [x, y] works, too
320
358
  s = Size(w, h) # again, [w, h] is fine
321
359
  f = Rect(p, s)
322
- f = Rect([[x, y], [w, h]]) # same as above (the CG*Array objects will get created for you)
323
-
324
- view.frame = f # Rect returns a CGRectArray, which view.frame can accept
360
+ # like I said, a straight-up array of nested arrays is fine, too.
361
+ f = Rect([[x, y], [w, h]])
325
362
  ```
326
363
 
327
- ###### Animations
364
+ ###### {CGRect,CGPoint,CGSize} is a *real boy*!
328
365
 
329
- jQuery-like animation methods.
366
+ These methods get defined in a module (`SugarCube::CG{Rect,Size,Point}Extensions`),
367
+ and included in `CGRect` *and* `Rect`. The idea is that you do not have to
368
+ distinguish between the two objects.
369
+
370
+ These methods all use the methods as described in [CGGeometry Reference][], e.g.
371
+ `CGRectContainsPoint`, `CGRectIntersectsRect`, etc.
330
372
 
331
373
  ```ruby
332
- # default timeout is 0.3
333
- view.fade_out { |view|
334
- view.removeFromSuperview
335
- }
336
- # options:
337
- view.fade_out(0.5, delay: 0,
338
- options: UIViewAnimationOptionCurveLinear,
339
- opacity: 0.5) { |view|
340
- view.removeFromSuperview
341
- }
374
+ # intersection / contains
375
+ Point(0, 0).intersects?(Rect(-1, -1, 2, 2)) # => true
376
+ # if a Point intersects a Rect, the Rect intersects the Point, right?
377
+ Rect(-1, -1, 2, 2).intersects? Point(0, 0) # => true
378
+
379
+ # CGRect and the gang are real Ruby objects. Let's treat 'em that way!
380
+ view.frame.contains? Point(10, 10) # in this case, contains? and intersects? are synonyms
381
+ view.frame.intersects? Rect(0, 0, 10, 10) # <= but this one
382
+ view.frame.contains? Rect(0, 0, 10, 10) # <= and this one are different.
383
+
384
+ # CGRect has factory methods for CGRectEmpty, CGRectNull, and - KINDA - CGRectInfinite
385
+ # BUT, there is a bug (?) right now where CGRectIsInfinite(CGRectInfinite) returns false.
386
+ # so instead, I've built my own infinite? method that checks for the special "Infinite" value
387
+ > CGRect.infinite
388
+ => [[0, 0], [Infinity, Infinity]]
389
+ > CGRect.infinite.infinite?
390
+ => true
391
+ > CGRect.null
392
+ => [[Infinity, Infinity], [0.0, 0.0]]
393
+ > CGRect.null.null?
394
+ => true
395
+ > CGRect.empty
396
+ => [[0.0, 0.0], [0.0, 0.0]]
397
+ > CGRect.empty.empty?
398
+ => true
399
+ ```
342
400
 
343
- view.move_to([0, 100]) # move to position 0, 100
344
- view.delta_to([0, 100]) # move over 0, down 100, from current position
401
+ A lot of the methods in CGGeometry Reference are available as instance methods
345
402
 
346
- view.slide :left # slides the entire view one "page" to the left, right, up, or down
403
+ ```ruby
404
+ view.frame.left # => CGRectGetMinX(view.frame)
405
+ view.frame.right # => CGRectGetMaxX(view.frame)
406
+ view.frame.top # => CGRectGetMinY(view.frame)
407
+ view.frame.bottom # => CGRectGetMaxY(view.frame)
408
+ view.frame.width # => CGRectGetWidth(view.frame)
409
+ view.frame.height # => CGRectGetHeight(view.frame)
410
+ view.frame.center # => Point(CGRectGetMidX(view.frame), CGRectGetMidY(view.frame))
411
+
412
+ view.frame.intersection(another_rect) # => CGRectIntersection(view.frame, another_rect)
413
+ view.frame + another_rect # => CGRectUnion(view.frame, another_rect)
414
+ view.frame + a_point # => CGRectOffset(view.frame, a_point.x, a_point.y)
415
+ view.frame + a_offset # => CGRectOffset(view.frame, a_offset.horizontal, a_offset.vertical)
416
+ view.frame + edgeinsets # => UIEdgeInsetsInsetRect(view.frame, edgeinsets)
417
+ view.frame + a_size # => CGRectInset(view.frame, -a_size.width, -a_size.height)
418
+ # Adding a size to a view keeps the view's CENTER in the same place, but
419
+ # increases its size by `size.width,size.height`. it's the same as using
420
+ # UIEdgeInsets with top == bottom, and left == right
421
+ > Rect(0, 0, 10, 10).center
422
+ => Point(5.0, 5.0) # note the center
423
+ > Rect(0, 0, 10, 10) + Size(10, 10)
424
+ => Rect([-10.0, -10.0],{30.0 × 30.0}) # origin and size changed, but...
425
+ > (Rect(0, 0, 10, 10) + Size(10, 10)).center
426
+ => Point(5.0, 5.0)
427
+ # See? It's bigger, but the center hasn't moved.
428
+ ```
347
429
 
348
- view.shake # shakes the view.
349
- # options w/ default values:
350
- shake offset: 8, # move 8 px left, and 8 px right
351
- repeat: 3, # three times
352
- duration: 0.3, # for a total of 0.3 seconds
353
- keypath: 'transform.translate.x'
430
+ `to_hash/from_hash`, and notice here that I used `puts`, to show that the `to_s`
431
+ method is a little more readable.
354
432
 
355
- # vigorous nodding - modifying transform.translation.y:
356
- view.shake offset: 20, repeat: 10, duration: 5, keypath: 'transform.translation.y'
357
- # an adorable wiggle - modifying transform.rotation:
358
- superview.shake offset: 0.1, repeat: 2, duration: 0.5, keypath: 'transform.rotation'
433
+ ```ruby
434
+ > Rect(0, 0, 10, 10).to_hash
435
+ => {"Width"=>10.0, "Height"=>10.0, "Y"=>0.0, "X"=>0.0}
436
+ > puts CGRect.from_hash Rect(0, 0, 1, 1).to_hash
437
+ CGRect([0.0, 0.0],{1.0 × 1.0})
359
438
  ```
360
439
 
440
+ [CGGeometry Reference]: https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html
441
+
361
442
  REPL View adjustments
362
443
  -----------------------
363
444
 
@@ -413,6 +494,21 @@ Assume I ran `include SugarCube::Adjust` in these examples.
413
494
  # if you forget what view you are adjusting, run `adjust` again
414
495
  > a
415
496
  => {UITextField @ x: 46.0 y:214.0, 280.0×33.0} child of UIView
497
+ ```
498
+
499
+ Pointers
500
+ ----------
501
+
502
+ These are not UIKit-related, so I reverted to Ruby's preferred `to_foo`
503
+ convention.
504
+
505
+ ```ruby
506
+ [0.0, 1.1, 2.2].to_pointer(:float)
507
+
508
+ floats = Pointer.new(:float, 3)
509
+ floats[0] = 0.0
510
+ floats[1] = 1.1
511
+ floats[2] = 2.2
416
512
  ```
417
513
 
418
514
  [BubbleWrap]: https://github.com/rubymotion/BubbleWrap
@@ -198,10 +198,10 @@ module SugarCube
198
198
 
199
199
  if tab
200
200
  print tab
201
- print is_last ? "`--" : "+--"
201
+ print is_last ? "`-- " : "+-- "
202
202
  tab += is_last ? " " : "| "
203
203
  else
204
- print "."
204
+ print ". "
205
205
  tab = ""
206
206
  end
207
207
  puts view.inspect
@@ -0,0 +1,11 @@
1
+ class Array
2
+
3
+ def to_pointer(type)
4
+ ret = Pointer.new(type, self.length)
5
+ self.each_index do |i|
6
+ ret[i] = self[i]
7
+ end
8
+ ret
9
+ end
10
+
11
+ end
@@ -1,190 +1,401 @@
1
+ module SugarCube
2
+ # Extensions to make CGRect a "real class"
3
+ module CGRectExtensions
4
+ module ClassMethods
1
5
 
2
- class CGRectArray < Array
6
+ def empty
7
+ SugarCube::CoreGraphics::Rect(CGRectZero)
8
+ end
3
9
 
4
- def initialize args
5
- if args.length == 4
6
- super [CGPointArray.new([args[0], args[1]]), CGSizeArray.new([args[2], args[3]])]
7
- else
8
- unless CGPointArray === args[0]
9
- args[0] = SugarCube::CoreGraphics::Point(args[0])
10
+ def null
11
+ SugarCube::CoreGraphics::Rect(CGRectNull)
10
12
  end
11
- unless CGSizeArray === args[1]
12
- args[1] = SugarCube::CoreGraphics::Size(args[1])
13
+
14
+ def infinite
15
+ SugarCube::CoreGraphics::Rect([0, 0], CGSize.infinite)
16
+ end
17
+
18
+ def from_hash(hash)
19
+ ret = Pointer.new(CGRect.type)
20
+ if CGRectMakeWithDictionaryRepresentation(hash, ret)
21
+ ret[0]
22
+ else
23
+ nil
24
+ end
13
25
  end
14
- super [args[0], args[1]]
15
26
  end
16
- end
17
27
 
18
- def origin
19
- return self[0]
20
- end
28
+ def self.included(base)
29
+ base.extend(ClassMethods)
30
+ end
21
31
 
22
- def origin= val
23
- self[0] = SugarCube::CoreGraphics::Point(val)
24
- end
32
+ def empty?
33
+ CGRectIsEmpty(self)
34
+ end
25
35
 
26
- def size
27
- return self[1]
28
- end
36
+ def infinite?
37
+ self.size.infinite?
38
+ end
29
39
 
30
- def size= val
31
- self[1] = SugarCube::CoreGraphics::Size(val)
32
- end
40
+ def null?
41
+ CGRectIsNull(self)
42
+ end
33
43
 
34
- # returns an intersection of self and rect, or moves the Rect using Point,
35
- # or increases the size using Size
36
- def +(rect)
37
- case rect
38
- when CGRectArray
39
- x1 = self.origin.x < rect.origin.x ? self.origin.x : rect.origin.x
40
- y1 = self.origin.y < rect.origin.y ? self.origin.y : rect.origin.y
41
- x2 = self.origin.x + self.size.width > rect.origin.x + rect.size.width ? self.origin.x + self.size.width : rect.origin.x + rect.size.width
42
- y2 = self.origin.y + self.size.height > rect.origin.y + rect.size.height ? self.origin.y + self.size.height : rect.origin.y + rect.size.height
43
- SugarCube::CoreGraphics::Rect(x1, y1, x2-x1, y2-y1)
44
- when CGPointArray
45
- x = self.origin.x + rect.x
46
- y = self.origin.y + rect.y
47
- SugarCube::CoreGraphics::Rect([[x, y], self.size])
48
- when CGSizeArray
49
- w = self.size.width + rect.width
50
- h = self.size.height + rect.height
51
- SugarCube::CoreGraphics::Rect([self.origin, [w, h]])
52
- else
53
- super
44
+ def left
45
+ return CGRectGetMinX(self)
54
46
  end
55
- end
56
47
 
57
- end
48
+ def right
49
+ return CGRectGetMaxX(self)
50
+ end
58
51
 
52
+ def width
53
+ return CGRectGetWidth(self)
54
+ end
59
55
 
60
- class CGPointArray < Array
56
+ def top
57
+ return CGRectGetMinY(self)
58
+ end
61
59
 
62
- def x
63
- return self[0]
64
- end
60
+ def bottom
61
+ return CGRectGetMaxY(self)
62
+ end
65
63
 
66
- def x= val
67
- self[0] = val
68
- end
64
+ def height
65
+ return CGRectGetHeight(self)
66
+ end
69
67
 
70
- def y
71
- return self[1]
72
- end
68
+ def center
69
+ return SugarCube::CoreGraphics::Point(CGRectGetMidX(self), CGRectGetMidY(self))
70
+ end
73
71
 
74
- def y= val
75
- self[1] = val
76
- end
72
+ def to_s
73
+ "#{self.class.name}([#{self.origin.x}, #{self.origin.y}],{#{self.size.width} × #{self.size.height}})"
74
+ end
77
75
 
78
- # adds a vector to this point, or creates a Rect by adding a size
79
- def +(point)
80
- case point
81
- when CGPointArray
82
- x = self.x + point.x
83
- y = self.y + point.y
84
- CGPointArray[x, y]
85
- when CGSizeArray
86
- CGRectArray[self, point]
87
- else
88
- super
76
+ def to_hash
77
+ CGRectCreateDictionaryRepresentation(self)
89
78
  end
90
- end
91
79
 
92
- end
80
+ def inspect ; to_s ; end
81
+
82
+ # # returns an intersection of self and rect, or moves the Rect using Point,
83
+ # or increases the size using Size
84
+ def +(rect)
85
+ case rect
86
+ when SugarCube::CoreGraphics::Rect, CGRect
87
+ SugarCube::CoreGraphics::Rect(CGRectUnion(self, rect))
88
+ when SugarCube::CoreGraphics::Point, CGPoint
89
+ SugarCube::CoreGraphics::Rect(CGRectOffset(self, rect.x, rect.y))
90
+ when SugarCube::CoreGraphics::Offset, UIOffset
91
+ SugarCube::CoreGraphics::Rect(CGRectOffset(self, rect.horizontal, rect.vertical))
92
+ when SugarCube::CoreGraphics::Size, CGSize
93
+ SugarCube::CoreGraphics::Rect(CGRectInset(self, - rect.width, - rect.height))
94
+ when SugarCube::CoreGraphics::EdgeInsets, UIEdgeInsets
95
+ SugarCube::CoreGraphics::Rect(UIEdgeInsetsInsetRect(self, rect))
96
+ else
97
+ super
98
+ end
99
+ end
93
100
 
94
- class CGSizeArray < Array
101
+ def intersection(rect)
102
+ SugarCube::CoreGraphics::Rect(CGRectIntersection(self, rect))
103
+ end
95
104
 
96
- def width
97
- return self[0]
98
- end
105
+ def intersects?(rect_or_point)
106
+ case rect_or_point
107
+ when SugarCube::CoreGraphics::Point, CGPoint
108
+ CGRectContainsPoint(self, rect_or_point)
109
+ when Array, CGRect
110
+ CGRectIntersectsRect(self, rect_or_point)
111
+ else
112
+ super
113
+ end
114
+ end
99
115
 
100
- def width= val
101
- self[0] = val
102
- end
116
+ def contains?(rect_or_point)
117
+ case rect_or_point
118
+ when SugarCube::CoreGraphics::Point, CGPoint
119
+ CGRectContainsPoint(self, rect_or_point)
120
+ when Array, CGRect
121
+ CGRectContainsRect(self, rect_or_point)
122
+ else
123
+ super
124
+ end
125
+ end
103
126
 
104
- def height
105
- return self[1]
106
- end
127
+ def ==(rect)
128
+ CGRectEqualToRect(self, rect)
129
+ end
107
130
 
108
- def height= val
109
- self[1] = val
110
131
  end
111
132
 
112
- # adds the sizes
113
- def +(size)
114
- case size
115
- when CGSizeArray
116
- width = self.width + size.width
117
- height = self.height + size.height
118
- CGSizeArray[width, height]
119
- when CGPointArray
120
- CGRectArray[size, self]
121
- else
122
- super
133
+ # Extensions to make CGPoint a "real class"
134
+ module CGPointExtensions
135
+ module ClassMethods
136
+ def from_hash(hash)
137
+ ret = Pointer.new(CGPoint.type)
138
+ if CGPointMakeWithDictionaryRepresentation(hash, ret)
139
+ ret[0]
140
+ else
141
+ nil
142
+ end
143
+ end
123
144
  end
124
- end
125
145
 
126
- end
146
+ def self.included(base)
147
+ base.extend(ClassMethods)
148
+ end
127
149
 
150
+ def intersects?(rect)
151
+ CGRectContainsPoint(rect, self)
152
+ end
128
153
 
129
- class UIEdgeInsetsArray < Array
154
+ def ==(point)
155
+ CGPointEqualToPoint(self, point)
156
+ end
130
157
 
131
- def top
132
- return self[0]
133
- end
158
+ def to_s
159
+ "#{self.class.name}(#{self.x}, #{self.y})"
160
+ end
134
161
 
135
- def top= val
136
- self[0] = val
137
- end
162
+ def to_hash
163
+ CGPointCreateDictionaryRepresentation(self)
164
+ end
138
165
 
139
- def left
140
- return self[1]
141
- end
166
+ def inspect ; to_s ; end
142
167
 
143
- def left= val
144
- self[1] = val
145
168
  end
146
169
 
147
- def bottom
148
- return self[2]
149
- end
170
+ # Extensions to make CGSize a "real class"
171
+ module CGSizeExtensions
172
+ module ClassMethods
173
+ def infinite
174
+ infinity = CGRect.null[0][0]
175
+ SugarCube::CoreGraphics::Size(infinity, infinity)
176
+ end
150
177
 
151
- def bottom= val
152
- self[2] = val
153
- end
178
+ def from_hash(hash)
179
+ ret = Pointer.new(CGSize.type)
180
+ if CGSizeMakeWithDictionaryRepresentation(hash, ret)
181
+ ret[0]
182
+ else
183
+ nil
184
+ end
185
+ end
186
+ end
154
187
 
155
- def right
156
- return self[3]
157
- end
188
+ def self.included(base)
189
+ base.extend(ClassMethods)
190
+ end
158
191
 
159
- def right= val
160
- self[3] = val
161
- end
192
+ def infinite?
193
+ infinity = CGRect.null[0][0]
194
+ self.width == infinity or self.height == infinity
195
+ end
162
196
 
163
- end
197
+ def ==(size)
198
+ CGSizeEqualToSize(self, size)
199
+ end
200
+
201
+ def to_s
202
+ "#{self.class.name}(#{self.width} × #{self.height})"
203
+ end
164
204
 
205
+ def to_hash
206
+ CGSizeCreateDictionaryRepresentation(self)
207
+ end
165
208
 
166
- class UIOffsetArray < Array
209
+ def inspect ; to_s ; end
167
210
 
168
- def horizontal
169
- return self[0]
170
211
  end
212
+ end
171
213
 
172
- def horizontal= val
173
- self[0] = val
174
- end
175
214
 
176
- def vertical
177
- return self[1]
178
- end
215
+ class CGRect
216
+ include SugarCube::CGRectExtensions
217
+ end
218
+
219
+
220
+ class CGPoint
221
+ include SugarCube::CGPointExtensions
222
+ end
179
223
 
180
- def vertical= val
181
- self[1] = val
182
- end
183
224
 
225
+ class CGSize
226
+ include SugarCube::CGSizeExtensions
184
227
  end
185
228
 
229
+
186
230
  module SugarCube
187
231
  module CoreGraphics
232
+ PI = 3.141592654
233
+ PHI = 1.618033989
234
+ E = 2.718281828
235
+
236
+ class Rect < Array
237
+ include SugarCube::CGRectExtensions
238
+
239
+ def initialize args
240
+ if args.length == 4
241
+ super [Point.new([args[0], args[1]]), Size.new([args[2], args[3]])]
242
+ else
243
+ unless args[0].is_a? Point
244
+ args[0] = Point(args[0])
245
+ end
246
+ unless args[1].is_a? Size
247
+ args[1] = Size(args[1])
248
+ end
249
+ super [args[0], args[1]]
250
+ end
251
+ end
252
+
253
+ def origin
254
+ return self[0]
255
+ end
256
+
257
+ def origin= val
258
+ self[0] = Point(val)
259
+ end
260
+
261
+ def size
262
+ return self[1]
263
+ end
264
+
265
+ def size= val
266
+ self[1] = Size(val)
267
+ end
268
+ end
269
+
270
+
271
+ class Point < Array
272
+ include SugarCube::CGPointExtensions
273
+
274
+ def x
275
+ return self[0]
276
+ end
277
+
278
+ def x= val
279
+ self[0] = val
280
+ end
281
+
282
+ def y
283
+ return self[1]
284
+ end
285
+
286
+ def y= val
287
+ self[1] = val
288
+ end
289
+
290
+ # adds a vector to this point, or creates a Rect by adding a size
291
+ def +(point)
292
+ case point
293
+ when Point, CGPoint
294
+ x = self.x + point.x
295
+ y = self.y + point.y
296
+ Point[x, y]
297
+ when Size, CGSize
298
+ Rect[self, point]
299
+ else
300
+ super
301
+ end
302
+ end
303
+ end
304
+
305
+
306
+ class Size < Array
307
+ include SugarCube::CGSizeExtensions
308
+
309
+ def width
310
+ return self[0]
311
+ end
312
+
313
+ def width= val
314
+ self[0] = val
315
+ end
316
+
317
+ def height
318
+ return self[1]
319
+ end
320
+
321
+ def height= val
322
+ self[1] = val
323
+ end
324
+
325
+ # adds the sizes
326
+ def +(size)
327
+ case size
328
+ when Size, CGSize
329
+ width = self.width + size.width
330
+ height = self.height + size.height
331
+ Size[width, height]
332
+ when Point, CGPoint
333
+ Rect[size, self]
334
+ else
335
+ super
336
+ end
337
+ end
338
+
339
+ end
340
+
341
+
342
+ class EdgeInsets < Array
343
+
344
+ def top
345
+ return self[0]
346
+ end
347
+
348
+ def top= val
349
+ self[0] = val
350
+ end
351
+
352
+ def left
353
+ return self[1]
354
+ end
355
+
356
+ def left= val
357
+ self[1] = val
358
+ end
359
+
360
+ def bottom
361
+ return self[2]
362
+ end
363
+
364
+ def bottom= val
365
+ self[2] = val
366
+ end
367
+
368
+ def right
369
+ return self[3]
370
+ end
371
+
372
+ def right= val
373
+ self[3] = val
374
+ end
375
+
376
+ end
377
+
378
+
379
+ class Offset < Array
380
+
381
+ def horizontal
382
+ return self[0]
383
+ end
384
+
385
+ def horizontal= val
386
+ self[0] = val
387
+ end
388
+
389
+ def vertical
390
+ return self[1]
391
+ end
392
+
393
+ def vertical= val
394
+ self[1] = val
395
+ end
396
+
397
+ end
398
+
188
399
  module_function
189
400
 
190
401
  def Size(w_or_size, h=nil)
@@ -202,7 +413,7 @@ module SugarCube
202
413
  else
203
414
  w = w_or_size
204
415
  end
205
- return CGSizeArray.new([w, h])
416
+ return Size.new([w, h])
206
417
  end
207
418
 
208
419
  def Point(x_or_origin, y=nil)
@@ -220,11 +431,11 @@ module SugarCube
220
431
  else
221
432
  x = x_or_origin
222
433
  end
223
- return CGPointArray.new([x, y])
434
+ return Point.new([x, y])
224
435
  end
225
436
 
226
437
  # Accepts 1, 2 or 4 arguments.
227
- # 1 argument should be an Array[4], CGRectArray, or CGRect
438
+ # 1 argument should be an Array[4], Rect, or CGRect
228
439
  # 2 arguments should be a Point/CGPoint and a Size/CGSize,
229
440
  # 4 should be x, y, w, h
230
441
  def Rect(x_or_origin, y_or_size=nil, w=nil, h=nil)
@@ -235,7 +446,12 @@ module SugarCube
235
446
  y = x_or_origin.origin.y
236
447
  w = x_or_origin.size.width
237
448
  h = x_or_origin.size.height
238
- when CGRectArray
449
+ when UIView, CALayer
450
+ x = x_or_origin.frame.origin.x
451
+ y = x_or_origin.frame.origin.y
452
+ w = x_or_origin.frame.size.width
453
+ h = x_or_origin.frame.size.height
454
+ when Rect
239
455
  x = x_or_origin[0][0]
240
456
  y = x_or_origin[0][1]
241
457
  w = x_or_origin[1][0]
@@ -258,17 +474,17 @@ module SugarCube
258
474
  raise RuntimeError.new("Invalid argument sent to Rect(#{x_or_origin.inspect})")
259
475
  end
260
476
  elsif not w and not h
261
- x_or_origin = Point(x_or_origin) unless x_or_origin.is_a? CGPointArray
477
+ x_or_origin = Point(x_or_origin) unless x_or_origin.is_a? Point
262
478
  x = x_or_origin.x
263
479
  y = x_or_origin.y
264
- y_or_size = Size(y_or_size) unless y_or_size.is_a? CGSizeArray
480
+ y_or_size = Size(y_or_size) unless y_or_size.is_a? Size
265
481
  w = y_or_size.width
266
482
  h = y_or_size.height
267
483
  else
268
484
  x = x_or_origin
269
485
  y = y_or_size
270
486
  end
271
- return CGRectArray.new([[x, y], [w, h]])
487
+ return Rect.new([[x, y], [w, h]])
272
488
  end
273
489
 
274
490
  # Accepts 1 or 4 arguments.
@@ -295,7 +511,7 @@ module SugarCube
295
511
  else
296
512
  top = top_or_inset
297
513
  end
298
- return UIEdgeInsetsArray.new([top, left, bottom, right])
514
+ return EdgeInsets.new([top, left, bottom, right])
299
515
  end
300
516
 
301
517
  # Accepts 1 or 2 arguments.
@@ -319,7 +535,7 @@ module SugarCube
319
535
  else
320
536
  horizontal = horizontal_or_offset
321
537
  end
322
- return UIOffsetArray.new([horizontal, vertical])
538
+ return Offset.new([horizontal, vertical])
323
539
  end
324
540
 
325
541
  end
@@ -3,8 +3,8 @@ class Fixnum
3
3
  # 0xffffff.uicolor
4
4
  # 0xffffff.uicolor(0.33)
5
5
  # =>
6
- # UIColor.colorWithRed(1.0, green:1.0, red: 1.0, alpha:1.0)
7
- # UIColor.colorWithRed(1.0, green:1.0, red: 1.0, alpha:0.33)
6
+ # UIColor.colorWithRed(1.0, green:1.0, blue: 1.0, alpha:1.0)
7
+ # UIColor.colorWithRed(1.0, green:1.0, blue: 1.0, alpha:0.33)
8
8
  def uicolor(alpha=nil)
9
9
  alpha = 1.0 if alpha.nil?
10
10
 
@@ -0,0 +1,7 @@
1
+ class NSSet
2
+
3
+ def to_s
4
+ "NSSet(#{self.count}){#{self.allObjects.map{|o| o.inspect}.join(', ')}}"
5
+ end
6
+
7
+ end
@@ -0,0 +1,40 @@
1
+ class UIEvent
2
+
3
+ def to_s
4
+ type = case self.type
5
+ when UIEventTypeTouches
6
+ "touch"
7
+ when UIEventTypeMotion
8
+ if self.subtype == UIEventSubtypeMotionShake
9
+ "shake"
10
+ else
11
+ "motion"
12
+ end
13
+ when UIEventTypeRemoteControl
14
+ case self.subtype
15
+ when UIEventSubtypeRemoteControlPlay
16
+ "remote-control, Play"
17
+ when UIEventSubtypeRemoteControlPause
18
+ "remote-control, Pause"
19
+ when UIEventSubtypeRemoteControlStop
20
+ "remote-control, Stop"
21
+ when UIEventSubtypeRemoteControlTogglePlayPause
22
+ "remote-control, TogglePlayPause"
23
+ when UIEventSubtypeRemoteControlNextTrack
24
+ "remote-control, NextTrack"
25
+ when UIEventSubtypeRemoteControlPreviousTrack
26
+ "remote-control, PreviousTrack"
27
+ when UIEventSubtypeRemoteControlBeginSeekingBackward
28
+ "remote-control, BeginSeekingBackward"
29
+ when UIEventSubtypeRemoteControlEndSeekingBackward
30
+ "remote-control, EndSeekingBackward"
31
+ when UIEventSubtypeRemoteControlBeginSeekingForward
32
+ "remote-control, BeginSeekingForward"
33
+ when UIEventSubtypeRemoteControlEndSeekingForward
34
+ "remote-control, EndSeekingForward"
35
+ end
36
+ end
37
+ "#{self.class.name}(#{type}, touches: #{allTouches.inspect})"
38
+ end
39
+
40
+ end
@@ -0,0 +1,20 @@
1
+ class UITouch
2
+
3
+ def to_s
4
+ phase = case self.phase
5
+ when UITouchPhaseBegan
6
+ "began"
7
+ when UITouchPhaseMoved
8
+ "moved"
9
+ when UITouchPhaseStationary
10
+ "stationary"
11
+ when UITouchPhaseEnded
12
+ "ended"
13
+ when UITouchPhaseCancelled
14
+ "cancelled"
15
+ end
16
+ "#{self.class.name}(#{self.tapCount} #{self.tapCount == 1 ? 'tap' : 'taps'} #{phase},"\
17
+ " at #{self.locationInView(self.view).inspect}, @ #{self.timestamp})"
18
+ end
19
+
20
+ end
@@ -0,0 +1,8 @@
1
+ class UIView
2
+
3
+ def to_s
4
+ "#{self.class.name}(##{self.object_id}, #{self.frame.inspect}, "\
5
+ "#{self.superview ? ' child of ' + self.superview.class.name + ' #' + self.superview.object_id.to_s : ''})"
6
+ end
7
+
8
+ end
@@ -2,7 +2,7 @@ class UIAlertView
2
2
 
3
3
  # UIAlertView.alert("title",
4
4
  # message: "help!",
5
- # buttons: %w"OK Cancel Go Away",
5
+ # buttons: %w"OK Cancel No-way",
6
6
  # cancel: proc{ puts "nevermind" },
7
7
  # success: proc{ |pressed| puts "pressed: #{pressed}" },
8
8
  # )
@@ -4,12 +4,6 @@ class UIView
4
4
  self.addSubview view
5
5
  end
6
6
 
7
- def to_s
8
- "{#{self.class.name} @ x: #{self.frame.origin.x} y:#{self.frame.origin.y}, "\
9
- "#{self.frame.size.width}×#{self.frame.size.height}}"\
10
- "#{self.superview ? ' child of ' + self.superview.class.name : ''}"
11
- end
12
-
13
7
  def show
14
8
  self.hidden = false
15
9
  self
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.5'
2
+ Version = '0.6'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugarcube
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.5'
4
+ version: '0.6'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-26 00:00:00.000000000 Z
13
+ date: 2012-07-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
17
- requirement: &70125432971500 !ruby/object:Gem::Requirement
17
+ requirement: &70171931302100 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70125432971500
25
+ version_requirements: *70171931302100
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &70125432986920 !ruby/object:Gem::Requirement
28
+ requirement: &70171931301160 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70125432986920
36
+ version_requirements: *70171931301160
37
37
  description: ! 'CocoaTouch/iOS is a *verbose* framework. These extensions hope to
38
38
  make
39
39
 
@@ -65,6 +65,7 @@ files:
65
65
  - Rakefile
66
66
  - lib/sugarcube.rb
67
67
  - lib/sugarcube/adjust.rb
68
+ - lib/sugarcube/array.rb
68
69
  - lib/sugarcube/core_graphics.rb
69
70
  - lib/sugarcube/defaults.rb
70
71
  - lib/sugarcube/document.rb
@@ -76,6 +77,10 @@ files:
76
77
  - lib/sugarcube/numeric.rb
77
78
  - lib/sugarcube/symbol.rb
78
79
  - lib/sugarcube/symbol/symbol_uicolor.rb
80
+ - lib/sugarcube/to_s/nsset.rb
81
+ - lib/sugarcube/to_s/uievent.rb
82
+ - lib/sugarcube/to_s/uitouch.rb
83
+ - lib/sugarcube/to_s/uiview.rb
79
84
  - lib/sugarcube/uialertview.rb
80
85
  - lib/sugarcube/uibutton.rb
81
86
  - lib/sugarcube/uicolor.rb