Class: Fusuma::Plugin::Detectors::TouchDetectors::PinchDetector

Inherits:
Base
  • Object
show all
Defined in:
lib/fusuma/plugin/detectors/touch_detectors/pinch_detector.rb

Instance Method Summary collapse

Instance Method Details

#detect(touch_buffer) ⇒ Object



10
11
12
13
14
15
16
17
18
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fusuma/plugin/detectors/touch_detectors/pinch_detector.rb', line 10

def detect(touch_buffer)
  MultiLogger.debug('> pinch detector')

  MultiLogger.debug('  movement?')
  return unless touch_buffer.moved?

  MultiLogger.debug('  at least 2 fingers?')
  return unless touch_buffer.finger_movements.size >= 2

  MultiLogger.debug('  distance change between first 2 fingers?')
  first_finger, second_finger = touch_buffer.finger_movements.values[0..1]
  begin_distance = Touchscreen::Math.distance(first_finger[:first_position], second_finger[:first_position])
  end_distance = Touchscreen::Math.distance(first_finger[:last_position], second_finger[:last_position])
  distance = end_distance - begin_distance
  return unless distance.abs > jitter_threshold

  direction = distance <=> 0
  MultiLogger.debug("  assuming direction is #{direction}, testing all pairs of fingers")
  is_pinch = catch(:not_a_pinch) do
    pairs = touch_buffer.finger_movements.keys.combination(2).to_a
    pairs.each do |finger1, finger2|
      begin_distance = Touchscreen::Math.distance(
        touch_buffer.finger_movements[finger1][:first_position],
        touch_buffer.finger_movements[finger2][:first_position]
      )
      end_distance = Touchscreen::Math.distance(
        touch_buffer.finger_movements[finger1][:last_position],
        touch_buffer.finger_movements[finger2][:last_position]
      )
      MultiLogger.debug("  fingers #{finger1} and #{finger2} moved from #{begin_distance} to #{end_distance}")
      this_distance = end_distance - begin_distance
      if this_distance.abs < jitter_threshold
        MultiLogger.debug("  !distance between fingers #{finger1} and #{finger2} is not changed enough (#{this_distance.abs}), not a pinch")
        throw(:not_a_pinch)
      end
      this_direction = this_distance <=> 0
      if this_direction != direction
        MultiLogger.debug("  !fingers #{finger1} and #{finger2} moved in direction #{this_direction}, not a pinch")
        throw(:not_a_pinch)
      end
    end
    true
  end
  return unless is_pinch

  direction = direction == -1 ? :in : :out
  MultiLogger.debug("  pinch #{direction} detected!")

  Plugin::Events::Records::TouchRecords::PinchRecord.new(finger: touch_buffer.finger, direction: direction)
end