Skip to content

Commit

Permalink
Better threading. Added play/pause functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
isalin committed Oct 7, 2018
1 parent c2a5f6b commit baaf0b5
Showing 1 changed file with 63 additions and 16 deletions.
79 changes: 63 additions & 16 deletions TBbard/src/bard/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@

public class GUI {

Notes n;
// Notes n;
Thread playingThread;
Thread countdownThread;
MidiParser midi;

String filePath = "";
boolean fileLoaded = false;

/** Field to hold the keybind that should stop the playback
* TODO Make this configurable by the user
/**
* Field to hold the keybind that should stop the playback TODO Make this
* configurable by the user
*/
int stopPlayback = KeyEvent.VK_ESCAPE;

Expand Down Expand Up @@ -300,7 +303,7 @@ public void itemStateChanged(ItemEvent e) {
}
});
loopCheckBox.setSelected(Settings.LoadBool("loop"));

trueTimingsCheckBox = new JCheckBox( "True Timings" );
gbcPanel1.gridx = 20;
gbcPanel1.gridy = 1;
Expand Down Expand Up @@ -532,10 +535,10 @@ public synchronized void drop(DropTargetDropEvent evt) {

taText.setText(midi.getSheet((String)cmbSelectedInstrument.getItemAt(0), cmbOctaveTargetCombo.getSelectedIndex()));


DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(midi.getOctaveQuality((String)cmbSelectedInstrument.getSelectedItem()));
cmbOctaveTargetCombo.setModel(model);

cmbOctaveTargetCombo.setSelectedIndex(midi.getHighestQualityOctave((String)cmbSelectedInstrument.getSelectedItem()));
taText.setText(midi.getSheet((String)cmbSelectedInstrument.getSelectedItem(), cmbOctaveTargetCombo.getSelectedIndex()));

Expand Down Expand Up @@ -573,20 +576,55 @@ public void itemStateChanged(ItemEvent arg0) {
{
@Override
public void actionPerformed(ActionEvent e) {
new Thread() { // Making a new thread instead of sleeping the old one
if(countdownThread != null && countdownThread.isAlive()) {
return;
}

if(btPlayButton.getText().equals("Playing")) {
playingThread.suspend();
btPlayButton.setText("Paused");
return;
} else
if(btPlayButton.getText().equals("Paused")) {
countdownThread = new Thread() { // Making a new thread instead of sleeping the old one
@Override
public void run() {
try {
double countdown = Math.ceil(((int)spnDelaySpinner.getValue()));
while(countdown > 0){
System.out.println("Countdown (ms): " + countdown);
btPlayButton.setText((int)countdown + "...");
Thread.sleep(1000);
countdown--;
}
} catch(Exception e) {

}
playingThread.resume();
btPlayButton.setText("Playing");
}
};
countdownThread.start();


return;
}

playingThread = new Thread() { // Making a new thread instead of sleeping the old one
@Override
public void run() {
try {
Settings.SaveInt("fps", (int)spnFpsSpinner.getValue());
Settings.SaveInt("delay", (int)spnDelaySpinner.getValue());
Settings.SaveDouble("waitmult", (double)spnCd.getValue());
n = new Notes((int) spnFpsSpinner.getValue());
Notes n = new Notes((int) spnFpsSpinner.getValue());
n.running = true;
n.holdNotes = holdCheckBox.isSelected();
n.fullKeyboard = keyboardCheckBox.isSelected();
n.waitMultiplier = (double)spnCd.getValue();
n.slowdownConstant = (int) Math.ceil((double) 1000/(int)spnFpsSpinner.getValue());
double countdown = Math.ceil(((int)spnDelaySpinner.getValue()));
countdownThread = this;
while(countdown > 0){
if(n.running == false){
System.out.println("Stopping countdown.");
Expand All @@ -598,8 +636,9 @@ public void run() {
Thread.sleep(1000);
countdown--;
}
countdownThread = null;
btPlayButton.setText("Playing");



do{
Expand All @@ -625,14 +664,15 @@ public void run() {

}while(loopCheckBox.isSelected() && n.running);
btPlayButton.setText("Play");

n.releaseHeldKey();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
n.releaseHeldKey();
//n.releaseHeldKey();
}
}.start(); // We're using a new thread to be able to access Stop still.
};
playingThread.start();// We're using a new thread to be able to access Stop still.
}
});

Expand All @@ -643,7 +683,14 @@ public void run() {
{
@Override
public void actionPerformed(ActionEvent e) {
n.running = false;
//n.running = false;
if (playingThread != null){
playingThread.stop();
}
if (countdownThread != null){
countdownThread.stop();
}

btPlayButton.setText("Play");
//n.releaseHeldKey();
}
Expand All @@ -665,14 +712,14 @@ public void actionPerformed(ActionEvent e) {
frame.requestFocusInWindow();
}

public void setOpenFile(JFrame frame, String fileName){
public void setOpenFile(JFrame frame, String fileName) {
System.out.println("Setting title to: " + fileName);
if(fileName.equals("")) frame.setTitle("TBbard");
if (fileName.equals(""))
frame.setTitle("TBbard");
else {
fileLoaded = true;
frame.setTitle("TBbard - " + fileName);
}
}


}

0 comments on commit baaf0b5

Please sign in to comment.