Module: SugarCube::Adjust
- Defined in:
- lib/sugarcube/adjust.rb
Class Method Summary collapse
- .adjust(view = nil, format = nil) ⇒ Object (also: a)
- .build_tree(item, selector) ⇒ Object
- .center(*args) ⇒ Object (also: c)
- .down(val = 1) ⇒ Object (also: d)
- .draw_tree(item, selector, tab = nil, is_last = true, items_index = 0) ⇒ Object
- .format_frame(frame) ⇒ Object
-
.frame(f = nil) ⇒ Object
(also: f)
| FRAME.
-
.left(val = 1) ⇒ Object
(also: l)
| ORIGIN.
- .origin(x = nil, y = nil) ⇒ Object (also: o)
- .repl_format(format = nil) ⇒ Object
-
.restore ⇒ Object
| RESTORE.
- .right(val = 1) ⇒ Object (also: r)
- .root ⇒ Object
-
.shadow(shadow = nil) ⇒ Object
(also: h)
| SHADOW.
- .shorter(val = 1) ⇒ Object (also: s)
- .size(w = nil, h = nil) ⇒ Object (also: z)
- .taller(val = 1) ⇒ Object (also: t)
-
.thinner(val = 1) ⇒ Object
(also: n)
| SIZE.
-
.tree(item = nil, selector = nil, &sel_blk) ⇒ Object
| TREE.
- .up(val = 1) ⇒ Object (also: u)
- .wider(val = 1) ⇒ Object (also: w)
Class Method Details
.adjust(view = nil, format = nil) ⇒ Object Also known as: a
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sugarcube/adjust.rb', line 19 def adjust(view=nil, format=nil) SugarCube::Adjust::repl_format(format.to_sym) if format return $sugarcube_view if not view if view.is_a? Fixnum $sugarcube_items ||= SugarCube::Adjust::build_tree(UIApplication.sharedApplication.keyWindow, :subviews) view = $sugarcube_items[view] end # a/adjust will return this object $sugarcube_view = view if view.is_a?(UIView) $sugarcube_restore = { frame: SugarCube::Adjust.frame, shadow: SugarCube::Adjust.shadow, } if format puts format_frame view.frame end end view end |
.build_tree(item, selector) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/sugarcube/adjust.rb', line 350 def build_tree(item, selector) if selector.is_a? Proc items = selector.call(item) else items = item.send(selector) end items ||= [] ret = [item] items.each_with_index { |subview, index| ret.concat SugarCube::Adjust::build_tree(subview, selector) } ret end |
.center(*args) ⇒ Object Also known as: c
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/sugarcube/adjust.rb', line 168 def center(*args) raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view element = nil total = nil direction = 'h' args.each do |option| case option when String, Symbol # accept string or symbol direction = option.to_s when Numeric if not total total = option elsif not element element = option else raise "I don't know what to do with #{option.inspect}" end else raise "I don't know what to do with #{option.inspect}" end end element = 1 unless element total = 1 unless total view = $sugarcube_view left = view.origin.x top = view.origin.y if /h|x/.match(direction.downcase) swidth = view.frame.width pwidth = view.superview.frame.width / total left = (pwidth - swidth) / 2 + pwidth * (element - 1) end if /v|y/.match(direction.downcase) sheight = view.frame.height pheight = view.superview.frame.height / total top = (pheight - sheight) / 2 + pheight * (element - 1) end self.origin left, top end |
.down(val = 1) ⇒ Object Also known as: d
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/sugarcube/adjust.rb', line 83 def down val=1 raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view f = $sugarcube_view.frame f.origin.y += val $sugarcube_view.frame = f puts format_frame(f) $sugarcube_view end |
.draw_tree(item, selector, tab = nil, is_last = true, items_index = 0) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/sugarcube/adjust.rb', line 294 def draw_tree(item, selector, tab=nil, is_last=true, items_index=0) space = ' ' if items_index < 10 print ' ' elsif items_index < 100 print ' ' elsif items_index > 999 # good god, man! space = '' end print items_index.to_s + ":" + space if tab print tab if is_last print '`-- ' tab += ' ' else print '+-- ' tab += '| ' end else print '. ' tab = '' end if self == item || $sugarcube_view == item print "\033[1m" end if item.is_a? UIView puts item.to_s superview: false else puts item.to_s end if self == item || $sugarcube_view == item print "\033[0m" end if selector.is_a? Proc items = selector.call(item) else items = item.send(selector) end items ||= [] items.each_with_index { |subview, index| items_index += 1 if self.respond_to? :draw_tree items_index = draw_tree(subview, selector, tab, index == items.length - 1, items_index) else items_index = SugarCube::Adjust::draw_tree(subview, selector, tab, index == items.length - 1, items_index) end } return items_index end |
.format_frame(frame) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/sugarcube/adjust.rb', line 378 def format_frame(frame) case SugarCube::Adjust::repl_format when :json then "{x: #{frame.origin.x}, y: #{frame.origin.y}, width: #{frame.size.width}, height: #{frame.size.height}}" when :ruby then "[[#{frame.origin.x}, #{frame.origin.y}], [#{frame.size.width}, #{frame.size.height}]]" when :objc frame.to_s else raise "Unknown repl_format #{SugarCube::Adjust::repl_format.inspect}" end end |
.frame(f = nil) ⇒ Object Also known as: f
| FRAME
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/sugarcube/adjust.rb', line 47 def frame f=nil raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view return $sugarcube_view.frame if not f f = SugarCube::CoreGraphics::Rect(f) $sugarcube_view.frame = f puts format_frame(f) $sugarcube_view end |
.left(val = 1) ⇒ Object Also known as: l
| ORIGIN
61 62 63 |
# File 'lib/sugarcube/adjust.rb', line 61 def left val=1 SugarCube::Adjust::right -val end |
.origin(x = nil, y = nil) ⇒ Object Also known as: o
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sugarcube/adjust.rb', line 95 def origin x=nil, y=nil raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view f = $sugarcube_view.frame return f.origin if not x if y f.origin.x = x f.origin.y = y else f.origin = SugarCube::CoreGraphics::Point(x) end $sugarcube_view.frame = f puts format_frame(f) $sugarcube_view end |
.repl_format(format = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/sugarcube/adjust.rb', line 9 def repl_format(format=nil) if format @@repl_format = format else # if adjust has not been called, the instance variable has not yet been set @@repl_format ||= :ruby end @@repl_format end |
.restore ⇒ Object
| RESTORE
370 371 372 373 374 375 376 |
# File 'lib/sugarcube/adjust.rb', line 370 def restore raise 'no view has been assigned to SugarCube::Adjust::adjust' unless $sugarcube_view $sugarcube_restore.each do |msg, value| SugarCube::Adjust.send(msg, value) end end |
.right(val = 1) ⇒ Object Also known as: r
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sugarcube/adjust.rb', line 66 def right val=1 raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view f = $sugarcube_view.frame f.origin.x += val $sugarcube_view.frame = f puts format_frame(f) $sugarcube_view end |
.root ⇒ Object
365 366 367 |
# File 'lib/sugarcube/adjust.rb', line 365 def root (UIApplication.sharedApplication.keyWindow || UIApplication.sharedApplication.windows[0]).rootViewController end |
.shadow(shadow = nil) ⇒ Object Also known as: h
| SHADOW
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/sugarcube/adjust.rb', line 214 def shadow shadow=nil raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view if shadow { opacity: :'shadowOpacity=', radius: :'shadowRadius=', offset: :'shadowOffset=', color: :'shadowColor=', path: :'shadowPath=', }.each { |key, msg| if value = shadow[key] if key == :color and [Symbol, Fixnum, NSString, UIImage, UIColor].any?{|klass| value.is_a? klass} value = value.uicolor.CGColor end $sugarcube_view.layer.send(msg, value) $sugarcube_view.layer.masksToBounds = false $sugarcube_view.layer.shouldRasterize = true end } $sugarcube_view else { opacity: $sugarcube_view.layer.shadowOpacity, radius: $sugarcube_view.layer.shadowRadius, offset: $sugarcube_view.layer.shadowOffset, color: $sugarcube_view.layer.shadowColor, path: $sugarcube_view.layer.shadowPath, } end end |
.shorter(val = 1) ⇒ Object Also known as: s
132 133 134 |
# File 'lib/sugarcube/adjust.rb', line 132 def shorter val=1 SugarCube::Adjust::taller -val end |
.size(w = nil, h = nil) ⇒ Object Also known as: z
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/sugarcube/adjust.rb', line 149 def size w=nil, h=nil raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view f = $sugarcube_view.frame return f.size if not w if h f.size.width = w f.size.height = h else f.size = SugarCube::CoreGraphics::Size(w) end $sugarcube_view.frame = f puts format_frame(f) $sugarcube_view end |
.taller(val = 1) ⇒ Object Also known as: t
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/sugarcube/adjust.rb', line 137 def taller val=1 raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view f = $sugarcube_view.frame f.size.height += val $sugarcube_view.frame = f puts format_frame(f) $sugarcube_view end |
.thinner(val = 1) ⇒ Object Also known as: n
| SIZE
115 116 117 |
# File 'lib/sugarcube/adjust.rb', line 115 def thinner val=1 SugarCube::Adjust::wider -val end |
.tree(item = nil, selector = nil, &sel_blk) ⇒ Object
| TREE
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/sugarcube/adjust.rb', line 248 def tree(item=nil, selector=nil, &sel_blk) unless item item = UIApplication.sharedApplication.keyWindow end if not item puts 'View is nil (no window, view, or controller to display)' return end if sel_blk if selector raise "You can't hand me a block AND a selector. I don't know what to do with them!" end if sel_blk.arity != 1 raise "The block you passed should accept one and only one object" end selector = sel_blk elsif ! selector if item.is_a? UIView selector = :subviews elsif item.is_a? UIViewController selector = lambda { |ctlr| ret = Array.new ctlr.childViewControllers if ctlr.presentedViewController && ctlr.presentedViewController.presentingViewController == ctlr ret << ctlr.presentedViewController end ret } elsif item.is_a? CALayer selector = :sublayers else raise "Unable to determine a SugarCube::Adjust::tree selector for #{item.class.name}" end end $sugarcube_items = SugarCube::Adjust::build_tree(item, selector) if self.respond_to? :draw_tree draw_tree(item, selector) else SugarCube::Adjust::draw_tree(item, selector) end puts '' return item end |
.up(val = 1) ⇒ Object Also known as: u
78 79 80 |
# File 'lib/sugarcube/adjust.rb', line 78 def up val=1 SugarCube::Adjust::down -val end |
.wider(val = 1) ⇒ Object Also known as: w
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sugarcube/adjust.rb', line 120 def wider val=1 raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view f = $sugarcube_view.frame f.size.width += val $sugarcube_view.frame = f puts format_frame(f) $sugarcube_view end |