Method: SugarCube::Repl.center

Defined in:
lib/cocoa/sugarcube-repl/repl.rb

.center(*args) ⇒ Object Also known as: c



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
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/cocoa/sugarcube-repl/repl.rb', line 181

def center(*args)
  return unless check_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
      unless total
        total = option
      elsunless 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.size.width
    pwidth = view.superview.frame.size.width / total
    left = (pwidth - swidth) / 2 + pwidth * (element - 1)
  end
  if /v|y/.match(direction.downcase)
    sheight = view.frame.size.height
    pheight = view.superview.frame.size.height / total
    top = (pheight - sheight) / 2 + pheight * (element - 1)
  end

  self.origin left, top
end