Class: ICuke::Simulate::Events::Touch

Inherits:
Event
  • Object
show all
Defined in:
lib/icuke/simulate.rb

Constant Summary collapse

GSEventHand =
3001
GSTouchDown =
1
GSTouchUp =
6
GSTouchMoved =
2
TYPE =
{
  :down => GSTouchDown,
  :up => GSTouchUp,
  :moved => GSTouchMoved
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Event

#event_time, #hold_for, #options

Instance Method Summary collapse

Methods inherited from Event

event_time, hold_for

Constructor Details

#initialize(type, paths, options = {}) ⇒ Touch

Returns a new instance of Touch.



41
42
43
44
45
46
# File 'lib/icuke/simulate.rb', line 41

def initialize(type, paths, options = {})
  super(options)
  
  @type = type
  @paths = paths
end

Instance Attribute Details

#pathsObject

Returns the value of attribute paths.



39
40
41
# File 'lib/icuke/simulate.rb', line 39

def paths
  @paths
end

#typeObject

Returns the value of attribute type.



39
40
41
# File 'lib/icuke/simulate.rb', line 39

def type
  @type
end

Instance Method Details

#averageXObject



48
49
50
# File 'lib/icuke/simulate.rb', line 48

def averageX
  paths.inject(0) { |s,p| s + p[0] } / paths.size
end

#averageYObject



52
53
54
# File 'lib/icuke/simulate.rb', line 52

def averageY
  paths.inject(0) { |s,p| s + p[1] } / paths.size
end

#to_json(*a) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/icuke/simulate.rb', line 56

def to_json(*a)
  {
    'Type' => GSEventHand,
    'Time' => event_time,
    'WindowLocation' => { 'X' => averageX, 'Y' => averageY },
    'Data' => {
      'Delta' => { 'X' => paths.size, 'Y' => paths.size },
      'Type' => TYPE[type],
      'Paths' => paths.map { |p|
        {
          'Location' => { 'X' => p[0], 'Y' => p[1] },
          'Size' => { 'X' => 1.0, 'Y' => 1.0 }
        }
      }
    }
  }.to_json(*a)
end