Improve button and keystrokes handling
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2024-08-05 21:29:02 +02:00
parent ab1a0a753c
commit a6c43fd646
4 changed files with 43 additions and 15 deletions

View File

@ -16,13 +16,13 @@ public class Main {
private static boolean running = true; private static boolean running = true;
private static final Logger LOGGER = LogManager.getLogger(Main.class); private static final Logger LOGGER = LogManager.getLogger(Main.class);
private static final MainWindow mainWindow = new MainWindow();
public static void main(String[] args) { public static void main(String[] args) {
LOGGER.info("Hello world!"); LOGGER.info("Hello world!");
registerJNativeHook(); registerJNativeHook();
MainWindow mainWindow = new MainWindow();
mainWindow.init(); mainWindow.init();
} }
@ -41,4 +41,8 @@ public class Main {
} }
} }
public static MainWindow getMainWindow() {
return mainWindow;
}
} }

View File

@ -6,6 +6,7 @@ import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener; import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import wtf.beatrice.autosqueal.Main;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -50,7 +51,7 @@ public class KeyPressListener implements NativeKeyListener
LOGGER.warn("Received shutdown keystroke: {}", keys); LOGGER.warn("Received shutdown keystroke: {}", keys);
System.exit(0); Main.getMainWindow().toggleRunning();
} }
} }
} }

View File

@ -1,5 +1,6 @@
package wtf.beatrice.autosqueal.ui; package wtf.beatrice.autosqueal.ui;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import wtf.beatrice.autosqueal.controls.CursorMover; import wtf.beatrice.autosqueal.controls.CursorMover;
@ -8,7 +9,6 @@ import wtf.beatrice.autosqueal.util.RunnerUtil;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.Timer; import java.util.Timer;
@ -19,22 +19,27 @@ public class MainWindow
private static final int WINDOW_HEIGHT = 600; private static final int WINDOW_HEIGHT = 600;
private static final int WINDOW_WIDTH = 800; private static final int WINDOW_WIDTH = 800;
private final Timer TIMER_RUNNER = new Timer(); private Timer timerRunner = new Timer();
private CursorMover cursorMover = new CursorMover();
private CursorMover cursorMover = null; private Button toggleButton;
public MainWindow() { public MainWindow() {
} }
public void init() { public void init() {
JFrame frame = new JFrame(); JFrame frame = new JFrame();
frame.setSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT)); frame.setSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
frame.setTitle("autosqueal");
frame.setResizable(false);
Button toggleButton = new Button("Start"); toggleButton = new Button();
toggleButton.setBounds(new Rectangle((WINDOW_WIDTH / 2) - 40, WINDOW_HEIGHT - 60, 80, 30)); toggleButton.setBounds(new Rectangle((WINDOW_WIDTH / 2) - 60, WINDOW_HEIGHT - 60, 120, 30));
toggleButton.addActionListener(e -> toggleRunning(toggleButton)); toggleButton.addActionListener(e -> toggleRunning());
frame.add(toggleButton); frame.add(toggleButton);
toggleRunning();
int bordersPx = 10; int bordersPx = 10;
int rescaleRateo = ((WINDOW_WIDTH - (2 * bordersPx)) * 100) / RunnerUtil.SCREEN_WIDTH; int rescaleRateo = ((WINDOW_WIDTH - (2 * bordersPx)) * 100) / RunnerUtil.SCREEN_WIDTH;
@ -61,24 +66,38 @@ public class MainWindow
} }
} }
private void toggleRunning(Button button) { public void toggleRunning() {
String label;
if (cursorMover == null) { if (cursorMover == null) {
timerRunner = new Timer();
CursorMoveListener cursorMoveListener = new CursorMoveListener(); CursorMoveListener cursorMoveListener = new CursorMoveListener();
TIMER_RUNNER.schedule(cursorMoveListener, 0L, 1000L); timerRunner.schedule(cursorMoveListener, 0L, 1000L);
cursorMover = new CursorMover(); cursorMover = new CursorMover();
TIMER_RUNNER.schedule(cursorMover, 1000L, RunnerUtil.SECONDS_BETWEEN_MOVES * 1000L); timerRunner.schedule(cursorMover, 1000L, RunnerUtil.SECONDS_BETWEEN_MOVES * 1000L);
button.setLabel("Stop"); label = "Stop [" +
NativeKeyEvent.getKeyText(NativeKeyEvent.VC_CONTROL) +
"][" +
NativeKeyEvent.getKeyText(NativeKeyEvent.VC_ALT) +
"]";
} }
else { else {
TIMER_RUNNER.cancel(); timerRunner.cancel();
cursorMover = null; cursorMover = null;
button.setLabel("Start"); label = "Start [" +
NativeKeyEvent.getKeyText(NativeKeyEvent.VC_CONTROL) +
"][" +
NativeKeyEvent.getKeyText(NativeKeyEvent.VC_ALT) +
"]";
} }
toggleButton.setLabel(label);
} }
} }

View File

@ -4,6 +4,10 @@ import java.awt.*;
public class RunnerUtil { public class RunnerUtil {
private RunnerUtil() {
throw new AssertionError("The RunnerUtil class is not intended to be instantiated.");
}
public static final int SECONDS_BETWEEN_MOVES = 10; public static final int SECONDS_BETWEEN_MOVES = 10;
public static final int SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; public static final int SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;