class TouchscreenTaps::GestureBuffer

Buffer for all events related to a single gesture Gesture is considered started when first finger touches the screen Gesture is considired ended when last finger leaves the screen Between start and end points at least one finger should be touching the screen

Constants

DOWN_TYPE
MOTION_TYPE
TIME_TO_KEEP
UP_TYPE

Attributes

events[R]

Public Class Methods

new() click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 16
def initialize
  @events = []
end

Public Instance Methods

down_touches() click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 36
def down_touches
  events_by_type(DOWN_TYPE)
end
empty?() click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 28
def empty?
  @events.empty?
end
gesture_in_progress?() click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 32
def gesture_in_progress?
  down_touches.count > up_touches.count
end
motion_touches() click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 44
def motion_touches
  events_by_type(MOTION_TYPE)
end
push(event) click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 20
def push(event)
  delete_old_events
  clear unless gesture_in_progress?
  remove_previous_events(event) if repeating_touch?(event)

  @events.push(event)
end
up_touches() click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 40
def up_touches
  events_by_type(UP_TYPE)
end

Private Instance Methods

clear() click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 58
def clear
  @events.clear
end
current_time() click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 62
def current_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
delete_old_events() click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 50
def delete_old_events
  @events.each do |event|
    next if (current_time - event.timestamp) < TIME_TO_KEEP

    @events.delete(event)
  end
end
events_by_type(type) click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 79
def events_by_type(type)
  @events.select { |e| e.type == type }
end
remove_previous_events(event) click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 72
def remove_previous_events(event)
  @events.select { |e| e.finger == event.finger }.each do |e|
    @events.delete(e)
  end
  @events.delete(@events.find { |e| e.type == UP_TYPE })
end
repeating_touch?(event) click to toggle source
# File lib/touchscreen_taps/gesture_buffer.rb, line 66
def repeating_touch?(event)
  return false unless event.type == DOWN_TYPE

  !@events.find { |e| e.finger == event.finger }.nil?
end