Skip to content

Commit 4ed892e

Browse files
committed
Refactor the mouse movement generator script
1 parent e310ecc commit 4ed892e

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

scripts/mouse.main.kts

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,53 @@ import kotlin.math.roundToInt
88

99
typealias Point = Pair<Int, Int>
1010

11+
val resultFile = Path("mouse-movements.arf")
12+
1113
// Target cursor points
1214
val center = 640 to 450
1315
val watch = 948 to 222
1416
val custom = 958 to 480
1517
val release = 456 to 312
1618
val apply = 818 to 456
1719

18-
val waitAtCenter = interpolate(center, center, 60).map(::moveTo)
19-
val moveFromCenterToWatch = interpolate(center, watch, 30).map(::moveTo)
20-
val waitAtWatch = interpolate(watch, watch, 15).map(::moveTo)
21-
val moveFromWatchToCustom = interpolate(watch, custom, 30).map(::moveTo)
22-
val waitAtCustom = interpolate(custom, custom, 15).map(::moveTo)
23-
val moveFromCustomToRelease = interpolate(custom, release, 30).map(::moveTo)
24-
val waitAtRelease = interpolate(release, release, 15).map(::moveTo)
25-
val moveFromReleaseToApply = interpolate(release, apply, 30).map(::moveTo)
26-
val waitAtApply = interpolate(apply, apply, 15).map(::moveTo)
27-
val moveFromApplyToCenter = interpolate(apply, center, 30).map(::moveTo)
28-
2920
(
30-
waitAtCenter +
31-
moveFromCenterToWatch + waitAtWatch + press(watch) + release(watch) + waitAtWatch +
32-
moveFromWatchToCustom + waitAtCustom + press(custom) + release(custom) + waitAtCustom +
33-
moveFromCustomToRelease + waitAtRelease + press(release) + release(release) + waitAtRelease +
34-
moveFromReleaseToApply + waitAtApply + press(apply) + release(apply) + waitAtApply + waitAtApply +
35-
moveFromApplyToCenter + waitAtCenter
21+
waitAt(center, duration = 60) +
22+
move(from = center, to = watch) + waitAt(watch) + press(watch) + letGo(watch) + waitAt(watch) +
23+
move(from = watch, to = custom) + waitAt(custom) + press(custom) + letGo(custom) + waitAt(custom) +
24+
move(from = custom, to = release) + waitAt(release) + press(release) + letGo(release) + waitAt(release) +
25+
move(from = release, to = apply) + waitAt(apply) + press(apply) + letGo(apply) + waitAt(apply, duration = 30) +
26+
move(from = apply, to = center) + waitAt(center, duration = 60)
3627
)
3728
.joinToString(separator = "\n")
38-
.let { Path("mouse-movements.arf").writeText(it) }
29+
.let(resultFile::writeText)
30+
31+
fun waitAt(point: Point, duration: Int = 15) = interpolate(point, point, duration).map(::goTo)
32+
33+
fun move(from: Point, to: Point) = interpolate(from, to, 30).map(::goTo)
34+
35+
fun press(point: Point) = "${point.first}|${point.second}|ltd"
36+
37+
fun letGo(point: Point) = "${point.first}|${point.second}|ltu"
38+
39+
fun goTo(point: Point) = "${point.first}|${point.second}|mov"
3940

4041
fun interpolate(
4142
a: Point,
4243
b: Point,
43-
count: Int
44+
steps: Int
4445
): List<Point> {
4546
val (x1, y1) = a
4647
val (x2, y2) = b
4748
val deltaX = x2 - x1
4849
val deltaY = y2 - y1
49-
val gradient = if (deltaX == 0) 0f else deltaY.toFloat() / deltaX
50-
val xIncrement = deltaX.toFloat() / count
51-
val yIncrement = xIncrement * gradient
50+
val slope = if (deltaX == 0) 0f else deltaY / deltaX.toFloat()
51+
val xIncrement = deltaX.toFloat() / steps
52+
val yIncrement = xIncrement * slope
5253
return buildList {
53-
for (i in 0..count) {
54-
val x = x1 + i * xIncrement
55-
val y = y1 + i * yIncrement
54+
for (i in 0..steps) {
55+
val x = x1 + (i * xIncrement)
56+
val y = y1 + (i * yIncrement)
5657
add(x.roundToInt() to y.roundToInt())
5758
}
5859
}
5960
}
60-
61-
fun moveTo(point: Point) = "${point.first}|${point.second}|mov"
62-
63-
fun press(point: Point) = "${point.first}|${point.second}|ltd"
64-
65-
fun release(point: Point) = "${point.first}|${point.second}|ltu"

0 commit comments

Comments
 (0)