class Rabbit::Gesture::Processor

Constants

DEFAULT_SKEW_THRESHOLD_ANGLE
DEFAULT_THRESHOLD
MOTIONS

Attributes

motions[R]
skew_threshold_angle[RW]
threshold[RW]

Public Class Methods

new(threshold=nil, skew_threshold_angle=nil) click to toggle source
# File lib/rabbit/gesture/processor.rb, line 10
def initialize(threshold=nil, skew_threshold_angle=nil)
  @threshold = threshold || DEFAULT_THRESHOLD
  @skew_threshold_angle = skew_threshold_angle
  @skew_threshold_angle ||= DEFAULT_SKEW_THRESHOLD_ANGLE
  reset
end

Public Instance Methods

available_motion?(motion) click to toggle source
# File lib/rabbit/gesture/processor.rb, line 23
def available_motion?(motion)
  MOTIONS.include?(motion)
end
position() click to toggle source
# File lib/rabbit/gesture/processor.rb, line 63
def position
  [@x, @y]
end
reset() click to toggle source
# File lib/rabbit/gesture/processor.rb, line 53
def reset
  @started = false
  @x = @y = -1
  @motions = []
end
start(x, y) click to toggle source
# File lib/rabbit/gesture/processor.rb, line 27
def start(x, y)
  @prev_x = @x = x
  @prev_y = @y = y
  @started = true
  @motions = []
end
started?() click to toggle source
# File lib/rabbit/gesture/processor.rb, line 17
def started?
  @started
end
to_a() click to toggle source
# File lib/rabbit/gesture/processor.rb, line 59
def to_a
  @motions
end
update_position(x, y) click to toggle source
# File lib/rabbit/gesture/processor.rb, line 34
def update_position(x, y)
  mx = x - @prev_x
  my = y - @prev_y

  motion = judge_motion(mx, my)
  if motion
    @prev_x = @x = x
    @prev_y = @y = y
    if @motions.last == motion
      false
    else
      @motions << motion
      true
    end
  else
    false
  end
end

Private Instance Methods

judge_corner_motion(dx, dy) click to toggle source
# File lib/rabbit/gesture/processor.rb, line 85
def judge_corner_motion(dx, dy)
  if dx < 0
    if dy < 0
      "UL"
    else
      "LL"
    end
  else
    if dy < 0
      "UR"
    else
      "LR"
    end
  end
end
judge_cross_motion(dx, dy) click to toggle source
# File lib/rabbit/gesture/processor.rb, line 101
def judge_cross_motion(dx, dy)
  if dx.abs > dy.abs
    if dx < 0
      "L"
    else
      "R"
    end
  else
    if dy < 0
      "U"
    else
      "D"
    end
  end
end
judge_motion(dx, dy) click to toggle source
# File lib/rabbit/gesture/processor.rb, line 68
def judge_motion(dx, dy)
  dxa = dx.abs
  dya = dy.abs
  distance = Math.sqrt(dxa ** 2 + dya ** 2)
  upper_theta = (45 + @skew_threshold_angle) * (Math::PI / 180.0)
  lower_theta = (45 - @skew_threshold_angle) * (Math::PI / 180.0)
  if distance > @threshold and
      dya < Math.tan(upper_theta) * dxa and
      dya > Math.tan(lower_theta) * dxa
    judge_corner_motion(dx, dy)
  elsif dxa > @threshold or dya > @threshold
    judge_cross_motion(dx, dy)
  else
    nil
  end
end