1
0
mirror of https://github.com/davpapp/PowerMiner synced 2024-11-15 05:55:05 -05:00
PowerMiner/src/Cursor.java

222 lines
8.1 KiB
Java
Raw Normal View History

2018-01-30 04:14:17 -05:00
/* Reads a file of coordinates
*/
import java.awt.AWTException;
2018-01-30 04:14:17 -05:00
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
2018-01-31 00:45:08 -05:00
import java.awt.event.InputEvent;
2018-01-30 04:14:17 -05:00
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
2018-01-30 04:14:17 -05:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Cursor {
2018-02-15 06:27:12 -05:00
public static final int NUMBER_OF_DISTANCES = 2203; // For 1080p screen
2018-01-31 00:45:08 -05:00
public static final int MINIMUM_CLICK_LENGTH = 120;
public static final int MAXIMUM_CLICK_LENGTH = 240;
private Robot robot;
2018-02-04 15:34:27 -05:00
private Randomizer randomizer;
2018-02-12 19:40:09 -05:00
private Random random;
2018-01-30 04:14:17 -05:00
private ArrayList<ArrayList<CursorPath>> cursorPathsByDistance;
public Cursor() throws AWTException {
System.out.println("Initializing cursor...");
2018-01-31 00:45:08 -05:00
initializeCursorPathsByDistanceFromFile("/home/dpapp/GhostMouse/coordinates.txt");
robot = new Robot();
2018-02-04 15:34:27 -05:00
randomizer = new Randomizer();
2018-02-12 19:40:09 -05:00
random = new Random();
2018-01-31 00:45:08 -05:00
}
private void initializeCursorPathsByDistanceFromFile(String path) {
2018-01-30 04:14:17 -05:00
initializeCursorPathsByDistance();
2018-01-31 00:45:08 -05:00
ArrayList<CursorPath> cursorPaths = getArrayListOfCursorPathsFromFile(path);
2018-01-30 04:14:17 -05:00
assignCursorPathsByDistance(cursorPaths);
}
private void initializeCursorPathsByDistance() {
this.cursorPathsByDistance = new ArrayList<ArrayList<CursorPath>>();
for (int i = 0; i < NUMBER_OF_DISTANCES; i++) {
this.cursorPathsByDistance.add(new ArrayList<CursorPath>());
}
}
private ArrayList<CursorPath> getArrayListOfCursorPathsFromFile(String path) {
CursorDataFileParser cursorDataFileParser = new CursorDataFileParser(path);
return cursorDataFileParser.getArrayListOfCursorPathsFromFile();
}
private void assignCursorPathsByDistance(ArrayList<CursorPath> cursorPaths) {
for (CursorPath cursorPath : cursorPaths) {
if (cursorPath.isCursorPathReasonable()) {
addCursorPathToCursorPathsByDistance(cursorPath);
}
}
}
private void addCursorPathToCursorPathsByDistance(CursorPath cursorPath) {
this.cursorPathsByDistance.get(cursorPath.getCursorPathDistance()).add(cursorPath);
}
2018-01-31 00:45:08 -05:00
// TODO: make sure these are reasonable
2018-01-31 00:45:08 -05:00
private int getRandomClickLength() {
2018-02-04 15:34:27 -05:00
return randomizer.nextGaussianWithinRange(MINIMUM_CLICK_LENGTH, MAXIMUM_CLICK_LENGTH);
2018-01-31 00:45:08 -05:00
}
public void leftClickCursor() throws InterruptedException {
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(getRandomClickLength());
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(getRandomClickLength());
2018-01-31 00:45:08 -05:00
}
public void rightClickCursor() throws InterruptedException {
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
Thread.sleep(20 + getRandomClickLength());
2018-01-31 00:45:08 -05:00
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
Thread.sleep(getRandomClickLength());
2018-01-31 00:45:08 -05:00
}
public void moveAndLeftClickAtCoordinates(Point goalPoint) throws Exception {
2018-01-31 00:45:08 -05:00
moveCursorToCoordinates(goalPoint);
leftClickCursor();
}
public void moveAndRightClickAtCoordinates(Point goalPoint) throws Exception {
2018-02-15 06:27:12 -05:00
moveCursorToCoordinates(goalPoint);
rightClickCursor();
}
public Point moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws Exception {
Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance);
moveCursorToCoordinates(randomizedGoalPoint);
leftClickCursor();
2018-02-15 06:27:12 -05:00
return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards
2018-01-31 00:45:08 -05:00
}
public Point moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws Exception {
Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance);
moveCursorToCoordinates(randomizedGoalPoint);
rightClickCursor();
2018-02-15 06:27:12 -05:00
return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards
}
2018-01-31 00:45:08 -05:00
public void moveCursorToCoordinates(Point goalPoint) throws Exception {
2018-02-04 15:34:27 -05:00
Point startingPoint = getCurrentCursorPoint();
2018-02-12 19:40:09 -05:00
int distanceToMoveCursor = getDistanceBetweenPoints(startingPoint, goalPoint);
2018-02-15 06:27:12 -05:00
double angleToRotateCursorPathTo = getThetaBetweenPoints(startingPoint, goalPoint);
System.out.println("R:" + distanceToMoveCursor + ", theta:" + angleToRotateCursorPathTo);
if (distanceToMoveCursor == 0) {
return;
}
2018-02-15 06:27:12 -05:00
CursorPath cursorPathWithDistanceSet = chooseCursorPathToFollowBasedOnDistance(distanceToMoveCursor);
CursorPath cursorPathWithDistanceAndAngleSet = cursorPathWithDistanceSet.getRotatedCopyOfCursorPath(angleToRotateCursorPathTo);
System.out.println("Rotated the points: ");
//cursorPathWithDistanceAndAngleSet.displayCursorPoints();
followCursorPath(cursorPathWithDistanceAndAngleSet, startingPoint);
}
2018-02-12 19:40:09 -05:00
public int getDistanceBetweenPoints(Point startingPoint, Point goalPoint) {
return (int) (Math.hypot(goalPoint.x - startingPoint.x, goalPoint.y - startingPoint.y));
}
2018-02-04 15:34:27 -05:00
2018-02-12 19:40:09 -05:00
public double getThetaBetweenPoints(Point startingPoint, Point goalPoint) {
2018-02-15 06:27:12 -05:00
return Math.atan2((goalPoint.x - startingPoint.x), (goalPoint.y - startingPoint.y));
2018-02-04 15:34:27 -05:00
}
2018-02-15 06:27:12 -05:00
private void followCursorPath(CursorPath cursorPathToFollow, Point startingPoint) throws InterruptedException {
for (CursorPoint cursorPoint : cursorPathToFollow.getCursorPathPoints()) {
Point translatedPointToClick = new Point(cursorPoint.x + startingPoint.x, cursorPoint.y + startingPoint.y);
2018-01-30 10:19:12 -05:00
robotMouseMove(translatedPointToClick);
2018-02-15 06:27:12 -05:00
Thread.sleep(cursorPoint.delay);
}
}
2018-01-30 10:04:20 -05:00
public void robotMouseMove(Point pointToMoveCursorTo) {
robot.mouseMove(pointToMoveCursorTo.x, pointToMoveCursorTo.y);
}
private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) throws Exception {
int newDistanceToMoveCursor = findNearestPathLengthThatExists(distanceToMoveCursor);
2018-02-12 19:40:09 -05:00
double scaleToFactorBy = getScaleToFactorBy(newDistanceToMoveCursor, distanceToMoveCursor);
System.out.println("new distance to follow cursor is: " + newDistanceToMoveCursor + " from scaling by " + scaleToFactorBy);
ArrayList<CursorPath> cursorPathsWithSameDistance = cursorPathsByDistance.get(newDistanceToMoveCursor);
CursorPath scaledCursorPath = cursorPathsWithSameDistance.get(random.nextInt(cursorPathsWithSameDistance.size())).getScaledCopyOfCursorPath(scaleToFactorBy);
return scaledCursorPath;
//return cursorPathsByDistance.get(newDistanceToMoveCursor).get(indexOfRandomPathToFollow);//scaledCursorPath;
}
public int findNearestPathLengthThatExists(int distanceToMoveCursor) throws Exception {
int offset = 0;
boolean reachedMinimumLimit = false;
boolean reachedMaximumLimit = false;
while (cursorPathsByDistance.get(distanceToMoveCursor + offset).size() == 0) {
// Go up
if (distanceToMoveCursor + Math.abs(offset) + 1 >= NUMBER_OF_DISTANCES) {
reachedMaximumLimit = true;
}
if (distanceToMoveCursor - Math.abs(offset) - 1 < 0) {
reachedMinimumLimit = true;
}
if (reachedMaximumLimit && reachedMinimumLimit) {
throw new Exception("No paths exist.");
}
if (offset < 0) {
if (!reachedMaximumLimit) {
offset = -offset + 1;
}
else {
offset--;
}
}
else {
if (!reachedMinimumLimit) {
offset = -offset - 1;
}
else {
offset++;
}
}
}
return distanceToMoveCursor + offset;
}
2018-02-12 19:40:09 -05:00
private double getScaleToFactorBy(int newDistanceToMoveCursor, int distanceToMoveCursor) {
return (1.0 * distanceToMoveCursor / newDistanceToMoveCursor);
}
2018-01-30 10:04:20 -05:00
public Point getCurrentCursorPoint() {
return MouseInfo.getPointerInfo().getLocation();
}
private Point randomizePoint(Point goalPoint, int xTolerance, int yTolerance) {
2018-02-04 15:34:27 -05:00
Randomizer randomizer = new Randomizer();
return new Point(goalPoint.x + randomizer.nextGaussianWithinRange(-xTolerance, xTolerance), goalPoint.y + randomizer.nextGaussianWithinRange(-yTolerance, yTolerance));
}
public void displayCursorPaths() {
for (int i = 0; i < NUMBER_OF_DISTANCES; i++) {
System.out.println("There are " + cursorPathsByDistance.get(i).size() + " paths of size " + i);
}
System.out.println("--------------");
/*for (int i = 0; i < cursorPathsByDistance.get(1).size(); i++) {
cursorPathsByDistance.get(1).get(i).displayCursorPoints();
}*/
//cursorPathsByDistance.get(0).get(0).displayCursorPoints();
}
2018-01-30 04:14:17 -05:00
}