Method: SugarCube::CoreGraphics.Rect

Defined in:
lib/ios/sugarcube-coregraphics/core_graphics.rb

.Rect(x_or_origin, y_or_size = nil, w = nil, h = nil) ⇒ Object

Accepts 1, 2 or 4 arguments. 1 argument should be an Array, Array[,[Numeric*2]], CGRect, UIView or CALayar 2 arguments should be a CGPoint and a CGSize, 3 arguments should be either Numeric, Numeric, Size or Point, Numeric, Numeric 4 should be x, y, w, h



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ios/sugarcube-coregraphics/core_graphics.rb', line 64

def Rect(x_or_origin, y_or_size=nil, w=nil, h=nil)
  # one arg
  if y_or_size.nil? and w.nil? and h.nil?
    case x_or_origin
    when CGRect
      x = x_or_origin.origin.x
      y = x_or_origin.origin.y
      w = x_or_origin.size.width
      h = x_or_origin.size.height
    when UIView, CALayer
      x = x_or_origin.frame.origin.x
      y = x_or_origin.frame.origin.y
      w = x_or_origin.frame.size.width
      h = x_or_origin.frame.size.height
    when Array
      if x_or_origin.length == 2
        x = x_or_origin[0][0]
        y = x_or_origin[0][1]
        w = x_or_origin[1][0]
        h = x_or_origin[1][1]
      elsif
        x = x_or_origin[0]
        y = x_or_origin[1]
        w = x_or_origin[2]
        h = x_or_origin[3]
      else
        raise RuntimeError.new("Invalid argument sent to Rect(#{x_or_origin.inspect})")
      end
    else
      raise RuntimeError.new("Invalid argument sent to Rect(#{x_or_origin.inspect})")
    end
  # two args
  elsif w.nil? and h.nil?
    x_or_origin = SugarCube::CoreGraphics::Point(x_or_origin) unless x_or_origin.is_a? CGPoint
    x = x_or_origin.x
    y = x_or_origin.y
    if y_or_size.is_a?(CGPoint)
      w = y_or_size.x - x
      h = y_or_size.y - y
    else
      y_or_size = SugarCube::CoreGraphics::Size(y_or_size)
      w = y_or_size.width
      h = y_or_size.height
    end
  # three args
  elsif h.nil?
    if x_or_origin.is_a? Numeric
      # x_or_origin: x, y_or_size: y, w: size
      point = SugarCube::CoreGraphics::Point(x_or_origin, y_or_size)
      size = w
      return SugarCube::CoreGraphics::Rect(point, size)
    elsif w.is_a? Numeric
      # x_or_origin: point, y_or_size: w, w: h
      point = x_or_origin
      size = SugarCube::CoreGraphics::Size(y_or_size, w)
      return SugarCube::CoreGraphics::Rect(point, size)
    else
      raise RuntimeError.new("Invalid arguments sent to Rect(#{x_or_origin.inspect}, #{y_or_size.inspect}, #{w.inspect})")
    end
  else
    x = x_or_origin
    y = y_or_size
  end
  return CGRect.new([x, y], [w, h])
end