-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmouse.v
142 lines (119 loc) · 3.77 KB
/
mouse.v
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
module vrobot
import math
import time
fn send_mouse_event(x int) {
C.mouse_event(x, 0, 0, 0, 0)
}
fn mouse_down(button string) {
match button {
'left' { send_mouse_event(C.MOUSEEVENTF_LEFTDOWN) }
'right' { send_mouse_event(C.MOUSEEVENTF_RIGHTDOWN) }
'middle' { send_mouse_event(C.MOUSEEVENTF_MIDDLEDOWN) }
else { panic('Invalid mouse button') }
}
}
fn mouse_up(button string) {
match button {
'left' { send_mouse_event(C.MOUSEEVENTF_LEFTUP) }
'right' { send_mouse_event(C.MOUSEEVENTF_RIGHTUP) }
'middle' { send_mouse_event(C.MOUSEEVENTF_MIDDLEUP) }
else { panic('Invalid mouse button') }
}
}
pub fn click(button string) {
mouse_down(button)
mouse_up(button)
}
pub fn double_click(button string) {
click(button)
click(button)
}
pub fn drag(x int, y int) {
mouse_down('left')
move_mouse(x, y)
mouse_up('left')
}
pub fn drag_rel(offset_x int, offset_y int) {
start_pos := mouse_pos()
dest_x := start_pos.x + offset_x
dest_y := start_pos.y + offset_y
mouse_down('left')
move_mouse(dest_x, dest_y)
mouse_up('left')
}
pub fn move_mouse(x int, y int) {
C.SetCursorPos(x, y)
}
pub fn move_mouse_rel(offset_x int, offset_y int) {
start_pos := mouse_pos()
dest_x := start_pos.x + offset_x
dest_y := start_pos.y + offset_y
move_mouse(dest_x, dest_y)
}
pub fn move_mouse_smooth(x int, y int, duration_ms int, tween string) {
start_pos := mouse_pos()
dist_x := f64(x) - start_pos.x
dist_y := f64(y) - start_pos.y
dist := math.sqrt(math.pow(x - start_pos.x, 2) + math.pow(y - start_pos.y, 2))
steps := int(math.max(50.0, duration_ms * 1000 / 5))
dt := int(f64(duration_ms * 1000) / f64(steps))
mut factor := 0.0
mut i := 0
for i < steps {
n := normalize(i, 0, steps)
// TODO there must be better way to do this
factor = match tween {
'linear' { n }
'ease_in_quad' { ease_in_quad(n) }
'ease_out_quad' { ease_out_quad(n) }
'ease_in_out_quad' { ease_in_out_quad(n) }
'ease_in_cubic' { ease_in_cubic(n) }
'ease_out_cubic' { ease_out_cubic(n) }
'ease_in_out_cubic' { ease_in_out_cubic(n) }
'ease_in_quart' { ease_in_quart(n) }
'ease_out_quart' { ease_out_quart(n) }
'ease_in_out_quart' { ease_in_out_quart(n) }
'ease_in_quint' { ease_in_quint(n) }
'ease_out_quint' { ease_out_quint(n) }
'ease_in_out_quint' { ease_in_out_quint(n) }
'ease_in_sine' { ease_in_sine(n) }
'ease_out_sine' { ease_out_sine(n) }
'ease_in_out_sine' { ease_in_out_sine(n) }
'ease_in_expo' { ease_in_expo(n) }
'ease_out_expo' { ease_out_expo(n) }
'ease_in_out_expo' { ease_in_out_expo(n) }
'ease_in_circ' { ease_in_circ(n) }
'ease_out_circ' { ease_out_circ(n) }
'ease_in_out_circ' { ease_in_out_circ(n) }
'ease_in_elastic' { ease_in_elastic(n) }
'ease_out_elastic' { ease_out_elastic(n) }
'ease_in_out_elastic' { ease_in_out_elastic(n) }
'ease_in_back' { ease_in_back(n) }
'ease_out_back' { ease_out_back(n) }
'ease_in_out_back' { ease_in_out_back(n) }
'ease_in_bounce' { ease_in_bounce(n) }
'ease_out_bounce' { ease_out_bounce(n) }
'ease_in_out_bounce' { ease_in_out_bounce(n) }
else { panic('Tween not found') }
}
current_x := int(f64(start_pos.x) + factor * dist_x)
current_y := int(f64(start_pos.y) + factor * dist_y)
move_mouse(current_x, current_y)
time.sleep(dt)
i++
}
}
pub fn move_mouse_smooth_rel(offset_x int, offset_y int, duration_ms int, tween string) {
start_pos := mouse_pos()
dest_x := start_pos.x + offset_x
dest_y := start_pos.y + offset_y
move_mouse_smooth(dest_x, dest_y, duration_ms, tween)
}
pub fn mouse_pos() Point {
p := Point{}
cursor := C.GetCursorPos(&p)
if !cursor {
panic('No cursor found')
}
return p
}