-
Notifications
You must be signed in to change notification settings - Fork 1
/
mouse_controller.rb
111 lines (90 loc) · 2.86 KB
/
mouse_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true
module Chromate
module Hardwares
class MouseController
CLICK_DURATION_RANGE = (0.01..0.1)
DOUBLE_CLICK_DURATION_RANGE = (0.1..0.5)
def self.reset_mouse_position
@@mouse_position = { x: 0, y: 0 } # rubocop:disable Style/ClassVars
end
attr_accessor :element, :client
# @param [Chromate::Element] element
# @param [Chromate::Client] client
def initialize(element: nil, client: nil)
@element = element
@client = client
end
# @param [Chromate::Element] element
# @return [self]
def set_element(element) # rubocop:disable Naming/AccessorMethodName
@element = element
self
end
# @return [Hash]
def mouse_position
@@mouse_position ||= { x: 0, y: 0 } # rubocop:disable Style/ClassVars
end
# @return [self]
def hover
raise NotImplementedError
end
# @return [self]
def click
raise NotImplementedError
end
# @return [self]
def double_click
raise NotImplementedError
end
# @return [self]
def right_click
raise NotImplementedError
end
# @params [Chromate::Element] element
# @return [self]
def drag_and_drop_to(element)
raise NotImplementedError
end
# @return [Integer]
def position_x
mouse_position[:x]
end
# @return [Integer]
def position_y
mouse_position[:y]
end
private
# @return [Integer]
def target_x
element.x + (element.width / 2)
end
# @return [Integer]
def target_y
element.y + (element.height / 2)
end
# @param [Integer] steps
# @return [Array<Hash>]
def bezier_curve(steps:, start_x: position_x, start_y: position_y, t_x: target_x, t_y: target_y) # rubocop:disable Metrics/AbcSize
# Points for the Bézier curve
control_x1 = start_x + (rand(50..150) * (t_x > start_x ? 1 : -1))
control_y1 = start_y + (rand(50..150) * (t_y > start_y ? 1 : -1))
control_x2 = t_x + (rand(50..150) * (t_x > start_x ? -1 : 1))
control_y2 = t_y + (rand(50..150) * (t_y > start_y ? -1 : 1))
(0..steps).map do |i|
t = i.to_f / steps
x = (((1 - t)**3) * start_x) + (3 * ((1 - t)**2) * t * control_x1) + (3 * (1 - t) * (t**2) * control_x2) + ((t**3) * t_x)
y = (((1 - t)**3) * start_y) + (3 * ((1 - t)**2) * t * control_y1) + (3 * (1 - t) * (t**2) * control_y2) + ((t**3) * t_y)
{ x: x, y: y }
end
end
# @param [Integer] target_x
# @param [Integer] target_y
# @return [Hash]
def update_mouse_position(target_x, target_y)
@@mouse_position[:x] = target_x
@@mouse_position[:y] = target_y
mouse_position
end
end
end
end