Skip to content

Commit

Permalink
Added swing based visual cue for lower latency
Browse files Browse the repository at this point in the history
  • Loading branch information
stringflow committed Aug 3, 2018
1 parent f8716e3 commit ac563c9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 40 deletions.
13 changes: 2 additions & 11 deletions res/layout/mainFrame.fxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.embed.swing.SwingNode?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
Expand All @@ -8,7 +9,6 @@
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>

<VBox maxHeight="345.0" maxWidth="430.0" minHeight="345.0" minWidth="430.0" prefHeight="345.0" prefWidth="430.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="stringflow.cheatontimer.FixedOffsetTab">
Expand All @@ -24,11 +24,6 @@
<content>
<AnchorPane fx:id="layout" maxWidth="430.0" minWidth="430.0" prefWidth="430.0">
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Rectangle fx:id="visualCueRect" arcHeight="5.0" arcWidth="5.0" fill="#ffffff00" height="55.0" stroke="#ffffff00" strokeType="INSIDE" width="120.0" />
</children>
</AnchorPane>
<Label layoutX="206.0" layoutY="16.0" />
<Label layoutX="145.0" layoutY="13.0" maxWidth="55.0" minHeight="20.0" minWidth="55.0" prefHeight="20.0" prefWidth="55.0" text="Name" />
<Label layoutX="205.0" layoutY="13.0" maxWidth="55.0" minHeight="20.0" minWidth="55.0" prefHeight="20.0" prefWidth="55.0" text="Offset" />
Expand All @@ -45,11 +40,7 @@
</Tab>
</tabs>
</TabPane>
<Label fx:id="timerLabel" layoutX="9.0" layoutY="34.0" prefHeight="44.0" prefWidth="113.0" text="0.000">
<font>
<Font name="Verdana" size="30.0" />
</font>
</Label>
<SwingNode fx:id="swingNode" layoutY="30.0" />
<Button fx:id="startButton" focusTraversable="false" layoutX="10.0" layoutY="85.0" maxHeight="25.0" maxWidth="100.0" minHeight="25.0" minWidth="100.0" mnemonicParsing="false" onAction="#onStartButtonPress" prefHeight="25.0" prefWidth="100.0" text="Start" />
<Button fx:id="resetButton" focusTraversable="false" layoutX="10.0" layoutY="115.0" maxHeight="25.0" maxWidth="100.0" minHeight="25.0" minWidth="100.0" mnemonicParsing="false" onAction="#onResetButtonPress" prefHeight="25.0" prefWidth="100.0" text="Reset" />
<Button fx:id="settingsButton" focusTraversable="false" layoutX="10.0" layoutY="145.0" maxHeight="25.0" maxWidth="100.0" minHeight="25.0" minWidth="100.0" mnemonicParsing="false" onAction="#onSettingsButtonPress" prefHeight="25.0" prefWidth="100.0" text="Settings" />
Expand Down
19 changes: 16 additions & 3 deletions src/stringflow/cheatontimer/FixedOffsetTab.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package stringflow.cheatontimer;

import javafx.embed.swing.SwingNode;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TabPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import stringflow.cheatontimer.timerFile.TimerFileUtil;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -35,12 +38,13 @@ public class FixedOffsetTab {
public Button settingsButton;
public Button importButton;
public Button exportButton;
public Label timerLabel;
public Rectangle visualCueRect;
public SwingNode swingNode;
public ImageView pinIcon;
public ImageView unpinIcon;

public String saveLocationBuffer;
public JLabel timerLabel;
public TimerPanel swingPanel;

private LinkedList<TimerEntry> timers = new LinkedList<>();
private TimerEntry activeTimer;
Expand All @@ -51,6 +55,15 @@ public FixedOffsetTab() {

@FXML
public void initialize() {
swingPanel = new TimerPanel();
swingPanel.setBackground(new Color(0xF4F4F4));
swingPanel.setLayout(null);
swingPanel.setBounds(20, 0, 120, 50);
timerLabel = new JLabel("0.000");
timerLabel.setFont(new Font("Verdana", 0, 30));
timerLabel.setBounds(9, 0, 150, 50);
swingPanel.add(timerLabel);
swingNode.setContent(swingPanel);
addNewTimer("Initial", 2500, 500, 5, false);
getTimer(0).select();
recalcAddButton();
Expand Down
5 changes: 3 additions & 2 deletions src/stringflow/cheatontimer/FlowTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public void start(Stage primaryStage) throws Exception {
String mode = ini.get("Audio", "mode");
audioCue = mode.toLowerCase().contains("audio");
visualCue = mode.toLowerCase().contains("visual");
audioEngine.setVolume(Float.valueOf(String.valueOf(ini.get("Audio", "volume"))));
mainFrame = primaryStage;
setupNativeHook();
GlobalScreen.registerNativeHook();
Expand Down Expand Up @@ -94,7 +93,6 @@ public void start(Stage primaryStage) throws Exception {
settingsWindow.setScene(new Scene(FXMLLoader.load(getClass().getResource("/layout/settingsWindow.fxml")), 265, 160));
settingsWindow.initModality(Modality.APPLICATION_MODAL);


// Check for 1.4.1 settings
if(ini.get("General") == null) {
ini.add("General");
Expand Down Expand Up @@ -123,6 +121,8 @@ public void start(Stage primaryStage) throws Exception {
} else {
SettingsWindow.instance.popAudioFile.setSelected(true);
}
audioEngine.setVolume(0);
currentBeep.play();
SettingsWindow.instance.startInputField1.set(ini.get("Input", "start1Name"), Integer.valueOf(String.valueOf(ini.get("Input", "start1"))));
SettingsWindow.instance.startInputField2.set(ini.get("Input", "start2Name"), Integer.valueOf(String.valueOf(ini.get("Input", "start2"))));
SettingsWindow.instance.resetInputField1.set(ini.get("Input", "reset1Name"), Integer.valueOf(String.valueOf(ini.get("Input", "reset1"))));
Expand All @@ -136,6 +136,7 @@ public void start(Stage primaryStage) throws Exception {
SettingsWindow.instance.volumeSlider.setValue(Double.valueOf(String.valueOf(ini.get("Audio", "volume"))) * 100.0);
SettingsWindow.instance.choiceBox.setValue(mode);
SettingsWindow.instance.setUpListeners();
audioEngine.setVolume(Float.valueOf(String.valueOf(ini.get("Audio", "volume"))));
}

private static Wini loadIniAndSetAudioEngine() throws Exception {
Expand Down
29 changes: 5 additions & 24 deletions src/stringflow/cheatontimer/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,6 @@ public class Timer {
public static long maxOffset;
public static long cutoff;
public static long elapsedTime;
public static boolean visualCue;
public static boolean lastVisualCue;

private static Timeline animation;

static {
animation = new Timeline(new KeyFrame(Duration.millis(1), (ActionEvent e) -> {
if(visualCue != lastVisualCue) {
FixedOffsetTab.instance.visualCueRect.setFill(visualCue ? Color.BLACK : Color.TRANSPARENT);
}
long time = (maxOffset - elapsedTime) / 1000000L;
if(time < 0) {
time = 0;
}
FixedOffsetTab.instance.setTimerLabel(time);
lastVisualCue = visualCue;
}));
animation.setCycleCount(Timeline.INDEFINITE);
}

public static void calcCurrentTime(TimerEntry entry) {
int numBeeps = entry.getOffsets().length * entry.getNumBeeps();
Expand Down Expand Up @@ -71,13 +52,11 @@ public static void start() {
currentTimerThread.stop();
}
new Thread(currentTimerThread = new TimerThread()).start();
animation.play();
FixedOffsetTab.instance.setElements(true);
}

public static void reset() {
if(isTimerRunning) {
animation.stop();
currentTimerThread.stop();
currentTimerThread.finish();
}
Expand All @@ -101,9 +80,11 @@ public void run() {
beepIndex++;
}
if(elapsedTime >= visualCues[visualCueIndex]) {
visualCue = !visualCue;
FixedOffsetTab.instance.swingPanel.toggleVisualCue();
FixedOffsetTab.instance.swingPanel.repaint();
visualCueIndex++;
}
FixedOffsetTab.instance.setTimerLabel((maxOffset - elapsedTime) / 1000000);
if(elapsedTime >= maxOffset) {
finish();
return;
Expand Down Expand Up @@ -137,10 +118,10 @@ public void run() {
try {
if(FlowTimer.visualCue) {
Thread.sleep(visualDuration);
visualCue = !visualCue;
FixedOffsetTab.instance.swingPanel.setVisualCue(false);
FixedOffsetTab.instance.swingPanel.repaint();
}
Thread.sleep(34);
animation.stop();
Platform.runLater(() -> {
FixedOffsetTab.instance.setTimerLabel(0);
FixedOffsetTab.instance.setActiveTimer(FixedOffsetTab.instance.getSelectedTimer());
Expand Down
31 changes: 31 additions & 0 deletions src/stringflow/cheatontimer/TimerPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package stringflow.cheatontimer;

import javax.swing.*;
import java.awt.*;

public class TimerPanel extends JPanel {

private static final Color BLACK = new Color(0, false);
private static final Color BLANK = new Color(0, true);

private boolean visualCue;

public TimerPanel() {
visualCue = false;
repaint();
}

public void paint(Graphics g) {
super.paint(g);
g.setColor(visualCue ? BLACK : BLANK);
g.fillRect(0, 0, 150, 150);
}

public void setVisualCue(boolean value) {
visualCue = value;
}

public void toggleVisualCue() {
visualCue = !visualCue;
}
}

0 comments on commit ac563c9

Please sign in to comment.