RuneFactory/src/main/kotlin/controllers/DesktopController.kt

81 lines
2.3 KiB
Kotlin

package controllers
import params.WiggleParams
import java.awt.MouseInfo
import java.awt.Point
import kotlin.random.Random
/**
* Interface for controllers that interact with the desktop.
*
* This defines methods for getting desktop state like the mouse pointer
* location.
*
* Classes that implement this can serve as desktop automation controllers.
*/
interface DesktopController {
/**
* Gets the current pointer/mouse location on the desktop.
*
* This returns a [Point] representing the x, y coordinates of the mouse pointer on the screen.
*
* For example:
*
* ```
* val mouseLocation = getPointerLocation()
*
* println(mouseLocation) // Might print "Point[x=1920,y=1080]"
* ```
*
* @return The current [Point] location of the mouse pointer on the screen.
*/
fun getPointerLocation(): Point {
return MouseInfo.getPointerInfo().location
}
/**
* Gets a point near the given [point], with some random wiggle.
*
* This generates a new point that is randomly offset from the given [point]
* by an amount controlled by the given [WiggleParams].
*
* Usage example:
*
* ```
* val base = Point(10, 20)
* val params = WiggleParams(xWiggle = 5, yWiggle = 5)
* val randomPoint = getAlmostPoint(base, params)
*
* // randomPoint might be (8, 22)
* ```
*
* Alternatively, params may be omitted to use the default WiggleParams values
* ```
* val base = Point(10, 20)
* val randomPoint = getAlmostPoint(base)
*
* // randomPoint might be (8, 22)
* ```
*
* @param point The base point to start from
* @param params The wiggle parameters that control the random offset amount
* @return A new [Point] near the given point with some random wiggle applied
*/
fun getAlmostPoint(point: Point, params: WiggleParams = WiggleParams()): Point {
val xDel = Random.nextInt(0, params.xWiggle)
val yDel = Random.nextInt(0, params.yWiggle)
val xDir = if (Random.nextDouble() > 0.5) {
1
} else {
-1
}
val yDir = if (Random.nextDouble() > 0.5) {
1
} else {
-1
}
return Point(point.x + (xDel * xDir), point.y + (yDel * yDir))
}
}