Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Action interface and classes #6

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 23 additions & 38 deletions src/main/java/com/macrobyte/macrobyte/HelloController.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package com.macrobyte.macrobyte;


import com.macrobyte.macrobyte.actions.*;
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

public class HelloController {


@FXML
public ListView<String> selectedActions;
public ListView<Action> selectedActions;


private final String RED_BUTTON = "-fx-background-color: linear-gradient(to right, #f0f2f0, #000c40);" +
Expand Down Expand Up @@ -52,16 +51,14 @@ public class HelloController {

private final List<String> keys = new ArrayList<>();

private int tracker = 0;


@FXML
private void initialize() {

actions.getItems().addAll("Right Click", "Left Click", "Simulate Key", "Sleep", "Move Cursor");
selectedActions.setCellFactory(lv -> new ListCell<String>() {
selectedActions.setCellFactory(lv -> new ListCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
protected void updateItem(Action item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
Expand All @@ -76,7 +73,7 @@ protected void updateItem(String item, boolean empty) {
"-fx-background-insets: 18;"
);
} else {
setText(item);
setText(item.getPrintName());
setStyle(
"-fx-background-color:linear-gradient(to bottom, #1d4a59, #7a3734);" +
"-fx-text-fill: #FFFFFF;" +
Expand Down Expand Up @@ -122,30 +119,19 @@ protected void updateItem(String item, boolean empty) {
}
});

selectedActions.getItems().addListener(new ListChangeListener<String>() {
@Override
public void onChanged(Change<? extends String> change) {
while (change.next()) {
for (String s : change.getAddedSubList()) {

if (s.strip().equals("Move Cursor")) {
getCoordinatesFromUser();
}
if (s.strip().equals("Simulate Key")) {
getKeyFromUser();
}
}
}
}
});
}


@FXML
protected void addItem() {

selectedActions.getItems().add(actions.getSelectionModel().getSelectedItem());

String name = actions.getSelectionModel().getSelectedItem();
switch (name) {
case "Move Cursor" -> getCoordinatesFromUser();
case "Simulate Key" -> getKeyFromUser();
case "Left Click" -> selectedActions.getItems().add(new LeftClick());
case "Right Click" -> selectedActions.getItems().add(new RightClick());
case "Sleep" -> selectedActions.getItems().add(new Sleep(getSleep()));
}

}

Expand All @@ -169,7 +155,7 @@ protected void setShortcut() {

}

public List<String> getActions() {
public List<Action> getActions() {
return selectedActions.getItems();

}
Expand Down Expand Up @@ -217,7 +203,10 @@ public int getSleep() {

}

// directly adds the MoveCursor action to selectedActions
private void getCoordinatesFromUser() {
final int[] coordinates = new int[2];

Stage second = new Stage();
second.initModality(Modality.APPLICATION_MODAL);
second.setTitle("Coordinates");
Expand All @@ -231,13 +220,10 @@ private void getCoordinatesFromUser() {

Button confirm = new Button("Confirm");
confirm.setOnAction(e -> {
coordinates.put("xCoordinate" + tracker, Integer.parseInt(xCoordinate.getText()));
coordinates.put("yCoordinate" + tracker, Integer.parseInt(yCoordinate.getText()));
tracker++;

coordinates[0] = Integer.parseInt(xCoordinate.getText());
coordinates[1] = Integer.parseInt(yCoordinate.getText());
selectedActions.getItems().add(new MoveCursor(coordinates));
second.close();


});
pane.add(xLabel, 0, 0);
pane.add(xCoordinate, 0, 1);
Expand All @@ -247,10 +233,9 @@ private void getCoordinatesFromUser() {
Scene scene = new Scene(pane, 400, 150);
second.setScene(scene);
second.show();


}

// directly adds the SimulateKey action to selectedActions
private void getKeyFromUser() {
Stage second = new Stage();
second.initModality(Modality.APPLICATION_MODAL);
Expand All @@ -263,7 +248,7 @@ private void getKeyFromUser() {

Button confirm = new Button("Confirm");
confirm.setOnAction(e -> {
keys.add(key.getText());
selectedActions.getItems().add((new SimulateKey(key.getText().toUpperCase())));
second.close();
});
Button capture = new Button("Capture");
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/com/macrobyte/macrobyte/MacroOperations.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.macrobyte.macrobyte;


import com.macrobyte.macrobyte.actions.Action;
import com.macrobyte.macrobyte.actions.MoveCursor;
import com.macrobyte.macrobyte.actions.Sleep;

import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.InputEvent;
Expand All @@ -9,7 +13,7 @@

public class MacroOperations {

List<String> actionOrder;
List<Action> actionOrder;
int loopTime;
int sleepTime;
HashMap<String, Integer> coordinates;
Expand All @@ -36,25 +40,24 @@ public void runMacro() {

HelloApplication.controller.notifyUser();
for (int i = 0; i < loopTime; i++) {
int track = 0;
for (String s : actionOrder) {
if (s.strip().equals("Left Click")) {
for (Action s : actionOrder) {
if(s == null){continue;}
if (s.getName().strip().equals("LeftClick")) {
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

} else if (s.strip().equals("Right Click")) {
} else if (s.getName().strip().equals("RightClick")) {
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
} else if (s.strip().equals("Sleep")) {
sleepTime = HelloApplication.controller.getSleep();
} else if (s.getName().strip().equals("Sleep")) {
sleepTime = ((Sleep)s).seconds();
try {
Thread.sleep(sleepTime * 1000);
Thread.sleep(sleepTime * 1000L);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
} else if (s.strip().equals("Move Cursor")) {
robot.mouseMove(coordinates.get("xCoordinate" + track), coordinates.get("yCoordinate" + track));
track++;
} else if (s.getName().strip().equals("MoveCursor")) {
robot.mouseMove(((MoveCursor)s).x(), ((MoveCursor)s).y());

}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/macrobyte/macrobyte/actions/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.macrobyte.macrobyte.actions;

public interface Action {
String getName();
String getPrintName();
}
13 changes: 13 additions & 0 deletions src/main/java/com/macrobyte/macrobyte/actions/LeftClick.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.macrobyte.macrobyte.actions;

public record LeftClick() implements Action{

@Override
public String getName() {
return getClass().getSimpleName();
}

public String getPrintName(){
return "Left Click";
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/macrobyte/macrobyte/actions/MoveCursor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.macrobyte.macrobyte.actions;

public record MoveCursor(Integer x, Integer y) implements Action {
@Override
public java.lang.String getName() {
return getClass().getSimpleName();
}

public MoveCursor(int[] coordinates){
this(coordinates[0], coordinates[1]);
}

public String getPrintName(){
return "Move Cursor\n [x:" + x + ", y:" + y + "]";
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/macrobyte/macrobyte/actions/RightClick.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.macrobyte.macrobyte.actions;

public record RightClick() implements Action {
@Override
public java.lang.String getName() {
return getClass().getSimpleName();
}

public String getPrintName(){
return "Right Click";
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/macrobyte/macrobyte/actions/SimulateKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.macrobyte.macrobyte.actions;

public record SimulateKey(String key) implements Action {
@Override
public java.lang.String getName() {
return getClass().getSimpleName();
}

public String getPrintName(){
return "Simulate Key[" + key + "]";
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/macrobyte/macrobyte/actions/Sleep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.macrobyte.macrobyte.actions;

public record Sleep(int seconds) implements Action {
@Override
public java.lang.String getName() {
return getClass().getSimpleName();
}

public String getPrintName(){
return "Sleep[" + seconds + "]";
}
}
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

opens com.macrobyte.macrobyte to javafx.fxml;
exports com.macrobyte.macrobyte;
exports com.macrobyte.macrobyte.actions;

}