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

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

Defined Under Namespace

Classes: Direction, Quantity

Constant Summary collapse

SOURCES =
["gesture"].freeze
BUFFER_TYPE =
"gesture"
GESTURE_RECORD_TYPE =
"pinch"
FINGERS =
[2, 3, 4].freeze
BASE_THRESHOLD =
1.3

Instance Attribute Summary

Attributes inherited from Detector

#tag, #type

Instance Method Summary collapse

Methods inherited from Detector

#create_event, #first_time?, #initialize, #last_time, #sources, tag, type, #watch?

Methods inherited from Base

#config_index, #config_param_types, #config_params, inherited, plugins, #shutdown

Constructor Details

This class inherits a constructor from Fusuma::Plugin::Detectors::Detector

Instance Method Details

#create_oneshot_index(gesture:, finger:, direction:) ⇒ Config::Index

Parameters:

Returns:



115
116
117
118
119
120
121
122
123
# File 'lib/fusuma/plugin/detectors/pinch_detector.rb', line 115

def create_oneshot_index(gesture:, finger:, direction:)
  Config::Index.new(
    [
      Config::Index::Key.new(gesture), # 'pinch'
      Config::Index::Key.new(finger.to_i, skippable: true), # 2, 3, 4
      Config::Index::Key.new(direction) # 'in', 'out'
    ]
  )
end

#create_repeat_index(gesture:, finger:, direction:, status:) ⇒ Config::Index

Parameters:

Returns:



100
101
102
103
104
105
106
107
108
109
# File 'lib/fusuma/plugin/detectors/pinch_detector.rb', line 100

def create_repeat_index(gesture:, finger:, direction:, status:)
  Config::Index.new(
    [
      Config::Index::Key.new(gesture), # 'pinch'
      Config::Index::Key.new(finger.to_i), # 2, 3, 4
      Config::Index::Key.new(direction, skippable: true), # 'in', 'out'
      Config::Index::Key.new(status) # 'begin', 'update', 'end'
    ]
  )
end

#detect(buffers) ⇒ Events::Event, NilClass

Parameters:

  • buffers (Array<Buffer>)

Returns:

  • (Events::Event)

    if event is detected

  • (NilClass)

    if event is NOT detected



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
60
61
62
63
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
# File 'lib/fusuma/plugin/detectors/pinch_detector.rb', line 19

def detect(buffers)
  gesture_buffer = buffers.find { |b| b.type == BUFFER_TYPE }
    .select_from_last_begin
    .select_by_type(GESTURE_RECORD_TYPE)

  updating_events = gesture_buffer.updating_events
  return if updating_events.empty?

  finger = gesture_buffer.finger

  status = case gesture_buffer.events.last.record.status
  when "end"
    "end"
  when "update"
    if updating_events.length == 1
      "begin"
    else
      "update"
    end
  else
    gesture_buffer.events.last.record.status
  end

  prev_event, event = if status == "end"
    [
      gesture_buffer.events[-3],
      gesture_buffer.events[-2]
    ]
  else
    [
      gesture_buffer.events[-2],
      gesture_buffer.events[-1]
    ]
  end
  delta = event.record.delta
  prev_delta = prev_event.record.delta

  repeat_direction = Direction.new(target: delta.zoom, base: (prev_delta&.zoom || 1.0)).to_s
  # repeat_quantity = Quantity.new(target: delta.zoom, base: (prev_delta&.zoom || 1.0)).to_f

  repeat_index = create_repeat_index(gesture: type, finger: finger,
    direction: repeat_direction,
    status: status)
  if status == "update"
    return unless moved?(prev_event, event)

    first_zoom, avg_zoom = if updating_events.size >= 10
      [updating_events[-10].record.delta.zoom,
        gesture_buffer.class.new(
          updating_events[-10..-1]
        ).avg_attrs(:zoom)]
    else
      [updating_events.first.record.delta.zoom,
        gesture_buffer.avg_attrs(:zoom)]
    end

    oneshot_quantity = Quantity.new(target: avg_zoom, base: first_zoom).to_f
    oneshot_direction = Direction.new(target: avg_zoom, base: first_zoom).to_s
    oneshot_index = create_oneshot_index(gesture: type, finger: finger,
      direction: oneshot_direction)
    if enough_oneshot_threshold?(index: oneshot_index, quantity: oneshot_quantity)
      return [
        create_event(record: Events::Records::IndexRecord.new(
          index: oneshot_index, trigger: :oneshot, args: delta.to_h
        )),
        create_event(record: Events::Records::IndexRecord.new(
          index: repeat_index, trigger: :repeat, args: delta.to_h
        ))
      ]
    end
  end
  create_event(record: Events::Records::IndexRecord.new(
    index: repeat_index, trigger: :repeat, args: delta.to_h
  ))
end