lib/fusuma/action_stack.rb in fusuma-0.2.0 vs lib/fusuma/action_stack.rb in fusuma-0.2.2

- old
+ new

@@ -3,11 +3,10 @@ class ActionStack < Array def initialize(*args) super(*args) end - # return { finger:, direction:, action: } or nil def gesture_info return unless enough_actions? && enough_time_passed? action_type = detect_action_type direction = detect_direction(action_type) return if direction.nil? @@ -23,53 +22,50 @@ end alias << push private - GestureInfo = Struct.new(:finger, :direction, :action_type) - def elapsed_time return 0 if length.zero? last.time - first.time end def detect_direction(action_type) case action_type when 'swipe' - detect_move + detect_swipe when 'pinch' - detect_zoom + detect_pinch end end - def detect_move - move = avg_moves - MultiLogger.debug(move: move) - return unless enough_distance?(move) - return move[:x] > 0 ? 'right' : 'left' if move[:x].abs > move[:y].abs - move[:y] > 0 ? 'down' : 'up' + def detect_swipe + swipe = avg_swipe + return unless swipe.enough_distance? + swipe.direction end - def detect_zoom - diameter = avg_attrs(:zoom) - MultiLogger.debug(diameter: diameter) - # TODO: change threshold from config files - return unless enough_diameter?(diameter) - return 'in' if diameter > 1 - 'out' + def detect_pinch + pinch = avg_pinch + return unless pinch.enough_diameter? + pinch.direction end def detect_finger last.finger end - Distance = Struct.new(:x, :y) + def avg_swipe + move_x = avg_attrs(:move_x) + move_y = avg_attrs(:move_y) + Swipe.new(move_x, move_y) + end - def avg_moves - move_x = sum_attrs(:move_x) / length - move_y = sum_attrs(:move_y) / length - Distance.new(move_x, move_y) + def avg_pinch + diameter = avg_attrs(:zoom) + delta_diameter = diameter - first.zoom + Pinch.new(delta_diameter) end def sum_attrs(attr) send('map') do |gesture_action| gesture_action.send(attr.to_sym.to_s) @@ -87,32 +83,19 @@ def last_action_name return false if last.class != GestureAction last.action end - def enough_distance?(move) - (move[:x].abs > 20) || (move[:y].abs > 20) - end - - def enough_diameter?(avg_diameter) - delta_diameter = if avg_diameter > 1 - avg_diameter - first.zoom - else - first.zoom - avg_diameter - end - delta_diameter > 0.3 - end - def enough_actions? - (length > 1) && (elapsed_time > 0.1) + (length > 1) && (elapsed_time > 0.05) end def enough_time_passed? (last.time - last_triggerd_time) > 0.5 end def last_triggerd_time - @last_triggered_time || 0 + @last_triggered_time ||= 0 end def detect_action_type first.action =~ /GESTURE_(.*?)_/ Regexp.last_match(1).downcase