diff --git a/src/main/java/com/macrobyte/macrobyte/HelloController.java b/src/main/java/com/macrobyte/macrobyte/HelloController.java index 5235b4f..05f8d23 100644 --- a/src/main/java/com/macrobyte/macrobyte/HelloController.java +++ b/src/main/java/com/macrobyte/macrobyte/HelloController.java @@ -1,13 +1,11 @@ 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; @@ -15,12 +13,13 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; public class HelloController { @FXML - public ListView selectedActions; + public ListView selectedActions; private final String RED_BUTTON = "-fx-background-color: linear-gradient(to right, #f0f2f0, #000c40);" + @@ -52,16 +51,14 @@ public class HelloController { private final List 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() { + 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); @@ -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;" + @@ -122,30 +119,19 @@ protected void updateItem(String item, boolean empty) { } }); - selectedActions.getItems().addListener(new ListChangeListener() { - @Override - public void onChanged(Change 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())); + } } @@ -169,7 +155,7 @@ protected void setShortcut() { } - public List getActions() { + public List getActions() { return selectedActions.getItems(); } @@ -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"); @@ -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); @@ -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); @@ -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"); diff --git a/src/main/java/com/macrobyte/macrobyte/MacroOperations.java b/src/main/java/com/macrobyte/macrobyte/MacroOperations.java index 9fc1167..3c803f9 100644 --- a/src/main/java/com/macrobyte/macrobyte/MacroOperations.java +++ b/src/main/java/com/macrobyte/macrobyte/MacroOperations.java @@ -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; @@ -9,7 +13,7 @@ public class MacroOperations { - List actionOrder; + List actionOrder; int loopTime; int sleepTime; HashMap coordinates; @@ -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()); } diff --git a/src/main/java/com/macrobyte/macrobyte/actions/Action.java b/src/main/java/com/macrobyte/macrobyte/actions/Action.java new file mode 100644 index 0000000..d30df0a --- /dev/null +++ b/src/main/java/com/macrobyte/macrobyte/actions/Action.java @@ -0,0 +1,6 @@ +package com.macrobyte.macrobyte.actions; + +public interface Action { + String getName(); + String getPrintName(); +} diff --git a/src/main/java/com/macrobyte/macrobyte/actions/LeftClick.java b/src/main/java/com/macrobyte/macrobyte/actions/LeftClick.java new file mode 100644 index 0000000..99977b3 --- /dev/null +++ b/src/main/java/com/macrobyte/macrobyte/actions/LeftClick.java @@ -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"; + } +} diff --git a/src/main/java/com/macrobyte/macrobyte/actions/MoveCursor.java b/src/main/java/com/macrobyte/macrobyte/actions/MoveCursor.java new file mode 100644 index 0000000..b650e4a --- /dev/null +++ b/src/main/java/com/macrobyte/macrobyte/actions/MoveCursor.java @@ -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 + "]"; + } +} diff --git a/src/main/java/com/macrobyte/macrobyte/actions/RightClick.java b/src/main/java/com/macrobyte/macrobyte/actions/RightClick.java new file mode 100644 index 0000000..705388d --- /dev/null +++ b/src/main/java/com/macrobyte/macrobyte/actions/RightClick.java @@ -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"; + } +} diff --git a/src/main/java/com/macrobyte/macrobyte/actions/SimulateKey.java b/src/main/java/com/macrobyte/macrobyte/actions/SimulateKey.java new file mode 100644 index 0000000..15fc69d --- /dev/null +++ b/src/main/java/com/macrobyte/macrobyte/actions/SimulateKey.java @@ -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 + "]"; + } +} diff --git a/src/main/java/com/macrobyte/macrobyte/actions/Sleep.java b/src/main/java/com/macrobyte/macrobyte/actions/Sleep.java new file mode 100644 index 0000000..e02b933 --- /dev/null +++ b/src/main/java/com/macrobyte/macrobyte/actions/Sleep.java @@ -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 + "]"; + } +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 7b88a26..84292ce 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -6,5 +6,6 @@ opens com.macrobyte.macrobyte to javafx.fxml; exports com.macrobyte.macrobyte; + exports com.macrobyte.macrobyte.actions; } \ No newline at end of file