Module: MotionKit::OriginCalculator

Included in:
ViewCalculator
Defined in:
lib/motion-kit/calculator/origin_calculator.rb

Instance Method Summary collapse

Instance Method Details

#calculate_absolute_from_hash(view, dimension, amount, my_size) ⇒ Object



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
# File 'lib/motion-kit/calculator/origin_calculator.rb', line 64

def calculate_absolute_from_hash(view, dimension, amount, my_size)
  if amount.key?(:right)
    x_offset = -my_size.width
    x = amount[:right]
  elsif amount.key?(:x) || amount.key?(:left)
    x = amount[:x] || amount[:left]
  elsif dimension == :center
    x = view.frame.origin.x + (my_size.width / 2)
  else
    x = view.frame.origin.x
  end

  if amount.key?(:bottom)
    y_offset = -my_size.height
    y = amount[:bottom]
  elsif amount.key?(:y) || amount.key?(:top)
    y = amount[:y] || amount[:top]
  elsif dimension == :center
    y = view.frame.origin.y + (my_size.height / 2)
  else
    y = view.frame.origin.y
  end

  return x, y, x_offset, y_offset
end

#calculate_from_center(view, dimension, amount, my_size, full_view) ⇒ Object



17
18
19
20
21
# File 'lib/motion-kit/calculator/origin_calculator.rb', line 17

def calculate_from_center(view, dimension, amount, my_size, full_view)
  x = calculate(view, :width, '50%', full_view) - my_size.width / 2.0
  y = calculate(view, :height, '50%', full_view) - my_size.height / 2.0
  return CGPoint.new(x, y)
end

#calculate_from_hash(view, dimension, amount, my_size) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/motion-kit/calculator/origin_calculator.rb', line 30

def calculate_from_hash(view, dimension, amount, my_size)
  return amount[0], amount[1], nil, nil if amount.is_a?(Array)
  if amount[:relative]
    calculate_relative_from_hash(view, dimension, amount, my_size)
  else
    calculate_absolute_from_hash(view, dimension, amount, my_size)
  end
end

#calculate_from_hash_array(view, dimension, amount, my_size, full_view) ⇒ Object



23
24
25
26
27
28
# File 'lib/motion-kit/calculator/origin_calculator.rb', line 23

def calculate_from_hash_array(view, dimension, amount, my_size, full_view)
  x, y, x_offset, y_offset = calculate_from_hash(view, dimension, amount, my_size)
  x = calculate(view, :width, x, full_view) + (x_offset || 0)
  y = calculate(view, :height, y, full_view) + (y_offset || 0)
  return CGPoint.new(x, y)
end

#calculate_origin(view, dimension, amount, my_size = nil, full_view) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/motion-kit/calculator/origin_calculator.rb', line 6

def calculate_origin(view, dimension, amount, my_size=nil, full_view)
  my_size ||= view.frame.size
  if amount == :center
    calculate_from_center(view, dimension, amount, my_size, full_view)
  elsif amount.is_a?(Array) || amount.is_a?(Hash)
    calculate_from_hash_array(view, dimension, amount, my_size, full_view)
  else
    return amount
  end
end

#calculate_relative_from_hash(view, dimension, amount, my_size) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/motion-kit/calculator/origin_calculator.rb', line 39

def calculate_relative_from_hash(view, dimension, amount, my_size)
  x = amount[:x] || begin
    x = view.frame.origin.x
    x += my_size.width / 2.0 if dimension == :center
    x
  end

  y = amount[:y] || begin
    y = view.frame.origin.y
    y += my_size.height / 2.0 if dimension == :center
    y
  end

  x_offset = 0
  x_offset = amount[:right] if amount.key?(:right)
  x_offset = -amount[:left] if amount.key?(:left)

  y_offset = 0
  y_offset = amount[:down] if amount.key?(:down)
  y_offset = -amount[:up] if amount.key?(:up)
  y_offset = -y_offset if amount[:flipped]

  return x, y, x_offset, y_offset
end