Class: Rabbit::Gesture::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/rabbit/gesture/processor.rb

Constant Summary collapse

DEFAULT_THRESHOLD =
48
DEFAULT_SKEW_THRESHOLD_ANGLE =
90 / 4
MOTIONS =
%w(L R U D UL UR LL LR)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threshold = nil, skew_threshold_angle = nil) ⇒ Processor

Returns a new instance of Processor.



10
11
12
13
14
15
# 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

Instance Attribute Details

#motionsObject (readonly)

Returns the value of attribute motions.



8
9
10
# File 'lib/rabbit/gesture/processor.rb', line 8

def motions
  @motions
end

#skew_threshold_angleObject

Returns the value of attribute skew_threshold_angle.



7
8
9
# File 'lib/rabbit/gesture/processor.rb', line 7

def skew_threshold_angle
  @skew_threshold_angle
end

#thresholdObject

Returns the value of attribute threshold.



7
8
9
# File 'lib/rabbit/gesture/processor.rb', line 7

def threshold
  @threshold
end

Instance Method Details

#available_motion?(motion) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rabbit/gesture/processor.rb', line 23

def available_motion?(motion)
  MOTIONS.include?(motion)
end

#positionObject



63
64
65
# File 'lib/rabbit/gesture/processor.rb', line 63

def position
  [@x, @y]
end

#resetObject



53
54
55
56
57
# File 'lib/rabbit/gesture/processor.rb', line 53

def reset
  @started = false
  @x = @y = -1
  @motions = []
end

#start(x, y) ⇒ Object



27
28
29
30
31
32
# 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?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/rabbit/gesture/processor.rb', line 17

def started?
  @started
end

#to_aObject



59
60
61
# File 'lib/rabbit/gesture/processor.rb', line 59

def to_a
  @motions
end

#update_position(x, y) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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