Improve button and keystrokes handling
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ab1a0a753c
commit
a6c43fd646
@ -16,13 +16,13 @@ public class Main {
|
||||
private static boolean running = true;
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(Main.class);
|
||||
private static final MainWindow mainWindow = new MainWindow();
|
||||
|
||||
public static void main(String[] args) {
|
||||
LOGGER.info("Hello world!");
|
||||
|
||||
registerJNativeHook();
|
||||
|
||||
MainWindow mainWindow = new MainWindow();
|
||||
mainWindow.init();
|
||||
}
|
||||
|
||||
@ -41,4 +41,8 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
public static MainWindow getMainWindow() {
|
||||
return mainWindow;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
|
||||
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import wtf.beatrice.autosqueal.Main;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -50,7 +51,7 @@ public class KeyPressListener implements NativeKeyListener
|
||||
|
||||
LOGGER.warn("Received shutdown keystroke: {}", keys);
|
||||
|
||||
System.exit(0);
|
||||
Main.getMainWindow().toggleRunning();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package wtf.beatrice.autosqueal.ui;
|
||||
|
||||
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import wtf.beatrice.autosqueal.controls.CursorMover;
|
||||
@ -8,7 +9,6 @@ import wtf.beatrice.autosqueal.util.RunnerUtil;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Timer;
|
||||
|
||||
@ -19,22 +19,27 @@ public class MainWindow
|
||||
private static final int WINDOW_HEIGHT = 600;
|
||||
private static final int WINDOW_WIDTH = 800;
|
||||
|
||||
private final Timer TIMER_RUNNER = new Timer();
|
||||
|
||||
private CursorMover cursorMover = null;
|
||||
private Timer timerRunner = new Timer();
|
||||
private CursorMover cursorMover = new CursorMover();
|
||||
private Button toggleButton;
|
||||
|
||||
|
||||
public MainWindow() {
|
||||
|
||||
}
|
||||
|
||||
public void init() {
|
||||
|
||||
JFrame frame = new JFrame();
|
||||
frame.setSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
|
||||
frame.setTitle("autosqueal");
|
||||
frame.setResizable(false);
|
||||
|
||||
Button toggleButton = new Button("Start");
|
||||
toggleButton.setBounds(new Rectangle((WINDOW_WIDTH / 2) - 40, WINDOW_HEIGHT - 60, 80, 30));
|
||||
toggleButton.addActionListener(e -> toggleRunning(toggleButton));
|
||||
toggleButton = new Button();
|
||||
toggleButton.setBounds(new Rectangle((WINDOW_WIDTH / 2) - 60, WINDOW_HEIGHT - 60, 120, 30));
|
||||
toggleButton.addActionListener(e -> toggleRunning());
|
||||
frame.add(toggleButton);
|
||||
toggleRunning();
|
||||
|
||||
int bordersPx = 10;
|
||||
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) {
|
||||
timerRunner = new Timer();
|
||||
CursorMoveListener cursorMoveListener = new CursorMoveListener();
|
||||
TIMER_RUNNER.schedule(cursorMoveListener, 0L, 1000L);
|
||||
timerRunner.schedule(cursorMoveListener, 0L, 1000L);
|
||||
|
||||
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 {
|
||||
TIMER_RUNNER.cancel();
|
||||
timerRunner.cancel();
|
||||
|
||||
cursorMover = null;
|
||||
|
||||
button.setLabel("Start");
|
||||
label = "Start [" +
|
||||
NativeKeyEvent.getKeyText(NativeKeyEvent.VC_CONTROL) +
|
||||
"][" +
|
||||
NativeKeyEvent.getKeyText(NativeKeyEvent.VC_ALT) +
|
||||
"]";
|
||||
|
||||
}
|
||||
|
||||
toggleButton.setLabel(label);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,10 @@ import java.awt.*;
|
||||
|
||||
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 SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
|
||||
|
Loading…
Reference in New Issue
Block a user