Class: Motion::Xray::XrayDpad

Inherits:
UIView
  • Object
show all
Defined in:
lib/motion-xray/views/xray_dpad.rb

Instance Method Summary collapse

Methods inherited from UIView

build_xray, #xray, #xray_subviews

Instance Method Details

#add_listener(target, action) ⇒ Object



54
55
56
# File 'lib/motion-xray/views/xray_dpad.rb', line 54

def add_listener(target, action)
  targets << [target, action]
end

#fire(delta) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/motion-xray/views/xray_dpad.rb', line 58

def fire(delta)
  if delta.is_a?(Symbol)
    dx = 0
    dy = 0
    case delta
    when :up
      dy = -1
    when :down
      dy = 1
    when :left
      dx = -1
    when :right
      dx = 1
    when :center
      # pass
    else
      raise "huh? #{delta.inspect}"
    end
    delta = CGPoint.new(dx, dy)
  end

  targets.each { |target, action| target.send(action, delta) }
end

#get_pressing(point) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/motion-xray/views/xray_dpad.rb', line 26

def get_pressing(point)
  center = CGRect.new([24.5, 24.5], [23.0, 23.0])
  pressing = nil
  if not self.bounds.contains? point
    return nil
  elsif center.contains? point
    return :center
  else
    if point.x < point.y
      if point.x < self.frame.height - point.y
        return :left
      else
        return :down
      end
    else
      if point.x < self.frame.height - point.y
        return :up
      else
        return :right
      end
    end
  end
end

#initWithFrame(frame) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/motion-xray/views/xray_dpad.rb', line 5

def initWithFrame(frame)
  super.tap do
    @pressed = {
      up: 'xray_dpad_up'.uiimage,
      down: 'xray_dpad_down'.uiimage,
      left: 'xray_dpad_left'.uiimage,
      right: 'xray_dpad_right'.uiimage,
      center: 'xray_dpad_center'.uiimage,
    }
    @default = 'xray_dpad'.uiimage
    @image_view = 'xray_dpad'.uiimageview
    @pressing = nil
    self << @image_view
  end
end

#set_pressing_image(direction) ⇒ Object



21
22
23
24
# File 'lib/motion-xray/views/xray_dpad.rb', line 21

def set_pressing_image(direction)
  image = @pressed[direction] || @default
  @image_view.image = image
end

#targetsObject



50
51
52
# File 'lib/motion-xray/views/xray_dpad.rb', line 50

def targets
  @targets ||= []
end

#touchesBegan(touches, withEvent: event) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/motion-xray/views/xray_dpad.rb', line 82

def touchesBegan(touches, withEvent:event)
  super
  point = touches.anyObject.locationInView(self)

  @pressing = get_pressing(point)
  @was_pressing = true
  @started_location = point
  if @pressing != :center
    @started_time = NSDate.new.to_i
    @started_timer = 0.1.every do
      new_time = NSDate.new.to_i
      delta = new_time - @started_time
      break if delta < 0.5

      if @was_pressing
        fire(@pressing)
      end
    end
    fire(@pressing)
  end
  set_pressing_image(@pressing)
end

#touchesCancelled(touches, withEvent: event) ⇒ Object



133
134
135
136
137
138
# File 'lib/motion-xray/views/xray_dpad.rb', line 133

def touchesCancelled(touches, withEvent:event)
  super
  @started_timer.invalidate if @started_timer
  set_pressing_image(nil)
  @started_timer = nil
end

#touchesEnded(touches, withEvent: event) ⇒ Object



126
127
128
129
130
131
# File 'lib/motion-xray/views/xray_dpad.rb', line 126

def touchesEnded(touches, withEvent:event)
  super
  @started_timer.invalidate if @started_timer
  set_pressing_image(nil)
  @started_timer = nil
end

#touchesMoved(touches, withEvent: event) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/motion-xray/views/xray_dpad.rb', line 105

def touchesMoved(touches, withEvent:event)
  super
  return unless @pressing

  point = touches.anyObject.locationInView(self)
  if @pressing == :center
    dx = point.x - @started_location.x
    dy = point.y - @started_location.y
    fire(CGPoint.new(dx, dy))
    @started_location = point
  else
    if get_pressing(point) != @pressing
      @was_pressing = false
      set_pressing_image(nil)
    else
      @was_pressing = true
      set_pressing_image(@pressing)
    end
  end
end