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
|
# File 'lib/fusuma/plugin/detectors/swipe_detector.rb', line 19
def detect(buffers)
gesture_buffer = buffers.find { |b| b.type == BUFFER_TYPE }
.select_from_last_begin
.select_by_events { |e| e.record.gesture == GESTURE_RECORD_TYPE }
updating_events = gesture_buffer.updating_events
return if updating_events.empty?
oneshot_move_x, oneshot_move_y = if updating_events.size >= 10
updating_time = 100 * (updating_events[-1].time - updating_events[-10].time)
last_10 = gesture_buffer.class.new(updating_events[-10..-1])
[last_10.sum_attrs(:move_x) / updating_time,
last_10.sum_attrs(:move_y) / updating_time]
else
updating_time = 100 * (updating_events.last.time - updating_events.first.time)
[gesture_buffer.sum_attrs(:move_x) / updating_time,
gesture_buffer.sum_attrs(:move_y) / updating_time]
end
gesture_buffer.sum_attrs(:move_x) / updating_time
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
delta = if status == 'end'
gesture_buffer.events[-2].record.delta
else
gesture_buffer.events.last.record.delta
end
repeat_direction = Direction.new(move_x: delta.move_x, move_y: delta.move_y).to_s
repeat_quantity = Quantity.new(move_x: delta.move_x, move_y: delta.move_y).to_f
repeat_index = create_repeat_index(gesture: type, finger: finger,
direction: repeat_direction, status: status)
if status == 'update'
return unless moved?(repeat_quantity)
oneshot_direction = Direction.new(move_x: oneshot_move_x, move_y: oneshot_move_y).to_s
oneshot_quantity = Quantity.new(move_x: oneshot_move_x, move_y: oneshot_move_y).to_f
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
|