Merge branch 'master' of https://eng-git.canterbury.ac.nz/seng302-2017/team-7 into story27
This commit is contained in:
@@ -4,6 +4,8 @@ package seng302.Controllers;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.layout.GridPane;
|
||||
@@ -24,6 +26,9 @@ public class RaceController extends Controller {
|
||||
@FXML
|
||||
GridPane canvasBase;
|
||||
|
||||
//user saved data for annotation display
|
||||
private ArrayList<Boolean> presetAnno;
|
||||
|
||||
ResizableRaceCanvas raceMap;
|
||||
@FXML SplitPane race;
|
||||
@FXML
|
||||
@@ -34,6 +39,16 @@ public class RaceController extends Controller {
|
||||
Label timer;
|
||||
@FXML
|
||||
Label FPS;
|
||||
@FXML
|
||||
CheckBox showName;
|
||||
@FXML
|
||||
CheckBox showAbbrev;
|
||||
@FXML
|
||||
CheckBox showSpeed;
|
||||
@FXML
|
||||
Button saveAnno;
|
||||
@FXML
|
||||
Button showSetAnno;
|
||||
|
||||
@FXML
|
||||
TableView<BoatInRace> boatInfoTable;
|
||||
@@ -139,7 +154,7 @@ public class RaceController extends Controller {
|
||||
canvasBase.getChildren().add(raceMap);
|
||||
race.setVisible(true);
|
||||
|
||||
|
||||
//Initialize save annotation array, fps listener, and annotation listeners
|
||||
initializeFPS();
|
||||
initializeAnnotations();
|
||||
|
||||
@@ -186,6 +201,7 @@ public class RaceController extends Controller {
|
||||
* Set up boat annotations
|
||||
*/
|
||||
private void initializeAnnotations() {
|
||||
presetAnno = new ArrayList<>();
|
||||
//listener for annotation
|
||||
showAnnotations.selectedProperty().addListener(new ChangeListener<Boolean>() {
|
||||
public void changed(ObservableValue<? extends Boolean> ov,
|
||||
@@ -194,6 +210,51 @@ public class RaceController extends Controller {
|
||||
raceMap.update();
|
||||
}
|
||||
});
|
||||
//listener for show name in annotation
|
||||
showName.selectedProperty().addListener(new ChangeListener<Boolean>() {
|
||||
public void changed(ObservableValue<? extends Boolean> ov,
|
||||
Boolean old_val, Boolean new_val) {
|
||||
raceMap.toggleAnnoName();
|
||||
raceMap.update();
|
||||
}
|
||||
});
|
||||
//listener for show abbreviation for annotation
|
||||
showAbbrev.selectedProperty().addListener(new ChangeListener<Boolean>() {
|
||||
public void changed(ObservableValue<? extends Boolean> ov,
|
||||
Boolean old_val, Boolean new_val) {
|
||||
raceMap.toggleAnnoAbbrev();
|
||||
raceMap.update();
|
||||
}
|
||||
});
|
||||
//listener to show speed for annotation
|
||||
showSpeed.selectedProperty().addListener(new ChangeListener<Boolean>() {
|
||||
public void changed(ObservableValue<? extends Boolean> ov,
|
||||
Boolean old_val, Boolean new_val) {
|
||||
raceMap.toggleAnnoSpeed();
|
||||
raceMap.update();
|
||||
}
|
||||
});
|
||||
//listener to save currently selected annotation
|
||||
saveAnno.setOnAction(new EventHandler<ActionEvent>() {
|
||||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
presetAnno.clear();
|
||||
presetAnno.add(showName.isSelected());
|
||||
presetAnno.add(showAbbrev.isSelected());
|
||||
presetAnno.add(showSpeed.isSelected());
|
||||
}
|
||||
});
|
||||
//listener to show saved annotation
|
||||
showSetAnno.setOnAction(new EventHandler<ActionEvent>() {
|
||||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
if (presetAnno.size() > 0) {
|
||||
showName.setSelected(presetAnno.get(0));
|
||||
showAbbrev.setSelected(presetAnno.get(1));
|
||||
showSpeed.setSelected(presetAnno.get(2));
|
||||
raceMap.update();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ public class BoatInRace extends Boat {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if boat has finished, fals eif not
|
||||
* @return true if boat has finished, false if not
|
||||
*/
|
||||
public boolean isFinished() {
|
||||
return this.finished;
|
||||
@@ -248,7 +248,7 @@ public class BoatInRace extends Boat {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(int position) {
|
||||
this.position.set(Integer.toString(position + 1));
|
||||
public void setPosition(String position) {
|
||||
this.position.set(position);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public abstract class Race implements Runnable {
|
||||
protected int scaleFactor;
|
||||
|
||||
private int SLEEP_TIME = 100; //time in milliseconds to pause in a paced loop
|
||||
protected int PRERACE_TIME = 5000; //time in milliseconds to pause during pre-race
|
||||
protected int PRERACE_TIME = 15000; //time in milliseconds to pause during pre-race
|
||||
private boolean timerEnabled = true; //boolean to determine if timer is ran
|
||||
|
||||
/**
|
||||
@@ -85,7 +85,7 @@ public abstract class Race implements Runnable {
|
||||
setControllerListeners();
|
||||
initialiseBoats();
|
||||
if (timerEnabled) countdownTimer();
|
||||
simulateRace();
|
||||
//simulateRace();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,37 +100,35 @@ public abstract class Race implements Runnable {
|
||||
* Countdown timer until race starts. Use PRERACE_TIME to set countdown duration.
|
||||
*/
|
||||
protected void countdownTimer() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long startTime = currentTime + PRERACE_TIME;
|
||||
long minutes;
|
||||
long currentTimeInSeconds;
|
||||
long remainingSeconds;
|
||||
long hours;
|
||||
long timeLeft;
|
||||
long timeLoopEnded;
|
||||
new AnimationTimer() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long startTime = currentTime + (PRERACE_TIME/scaleFactor);
|
||||
long minutes;
|
||||
long currentTimeInSeconds;
|
||||
long remainingSeconds;
|
||||
long hours;
|
||||
long timeLeft;
|
||||
|
||||
while (currentTime <= startTime) {
|
||||
timeLeft = startTime - currentTime;
|
||||
if (timeLeft == 0 && controller != null) {
|
||||
updateTime("Race is starting...");
|
||||
} else {
|
||||
currentTimeInSeconds = timeLeft / 1000;
|
||||
minutes = currentTimeInSeconds / 60;
|
||||
remainingSeconds = currentTimeInSeconds % 60;
|
||||
hours = minutes / 60;
|
||||
minutes = minutes % 60;
|
||||
if (controller != null) {
|
||||
updateTime(String.format("Race clock: -%02d:%02d:%02d", hours, minutes, remainingSeconds));
|
||||
}
|
||||
@Override
|
||||
public void handle(long arg0) {
|
||||
timeLeft = startTime - currentTime;
|
||||
if (timeLeft <= 0 && controller != null) {
|
||||
updateTime("Race is starting...");
|
||||
stop();
|
||||
simulateRace();
|
||||
} else {
|
||||
currentTimeInSeconds = (timeLeft*scaleFactor) / 1000;
|
||||
minutes = currentTimeInSeconds / 60;
|
||||
remainingSeconds = currentTimeInSeconds % 60;
|
||||
hours = minutes / 60;
|
||||
minutes = minutes % 60;
|
||||
if (controller != null) {
|
||||
updateTime(String.format("Race clock: -%02d:%02d:%02d", hours, minutes, remainingSeconds));
|
||||
}
|
||||
}
|
||||
currentTime = System.currentTimeMillis();
|
||||
}
|
||||
try {
|
||||
timeLoopEnded = System.currentTimeMillis();
|
||||
Thread.sleep(SLEEP_TIME - (timeLoopEnded - currentTime));
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
currentTime = System.currentTimeMillis();
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,7 +174,7 @@ public abstract class Race implements Runnable {
|
||||
|
||||
private boolean doNotFinish() {
|
||||
Random rand = new Random();
|
||||
return rand.nextInt(100) < 1;
|
||||
return rand.nextInt(4) < 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -264,8 +262,21 @@ public abstract class Race implements Runnable {
|
||||
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
|
||||
}
|
||||
//Update the boat display table in the GUI to reflect the leg change
|
||||
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
|
||||
boat.setPosition(startingBoats.indexOf(boat));
|
||||
updatePositions();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update position of boats in race, no position if on starting leg or DNF.
|
||||
*/
|
||||
private void updatePositions() {
|
||||
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
|
||||
for(BoatInRace boat: startingBoats) {
|
||||
if(boat != null) {
|
||||
boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1));
|
||||
if (boat.getCurrentLeg().getName().equals("DNF") || boat.getCurrentLeg().getLegNumber() == 0)
|
||||
boat.setPosition("-");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import seng302.GraphCoordinate;
|
||||
import seng302.RaceMap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* This creates a JavaFX Canvas that is fills it's parent.
|
||||
@@ -25,6 +26,9 @@ public class ResizableRaceCanvas extends Canvas {
|
||||
private BoatInRace[] boats;
|
||||
private RaceController controller;
|
||||
private boolean raceAnno = true;
|
||||
private boolean annoName = true;
|
||||
private boolean annoAbbrev = true;
|
||||
private boolean annoSpeed = true;
|
||||
private ArrayList<GPSCoordinate> raceBoundaries;
|
||||
double[] xpoints = {}, ypoints = {};
|
||||
|
||||
@@ -163,11 +167,25 @@ public class ResizableRaceCanvas extends Canvas {
|
||||
* Display given name and speed of boat at a graph coordinate
|
||||
*
|
||||
* @param name name of the boat
|
||||
* @param abbrev abbreviation of the boat name
|
||||
* @param speed speed of the boat
|
||||
* @param coordinate coordinate the text appears
|
||||
*/
|
||||
private void displayText(String name, double speed, GraphCoordinate coordinate) {
|
||||
String text = String.format("%s, %2$.2fkn", name, speed);
|
||||
private void displayText(String name, String abbrev, double speed, GraphCoordinate coordinate) {
|
||||
String text = "";
|
||||
//Check name toggle value
|
||||
if (annoName){
|
||||
text += String.format("%s ", name);
|
||||
}
|
||||
//Check abbreviation toggle value
|
||||
if (annoAbbrev){
|
||||
text += String.format("%s ", abbrev);
|
||||
}
|
||||
//Check speed toggle value
|
||||
if (annoSpeed){
|
||||
text += String.format("%.2fkn", speed);
|
||||
}
|
||||
//String text = String.format("%s, %2$.2fkn", name, speed);
|
||||
long xCoord = coordinate.getX() + 20;
|
||||
long yCoord = coordinate.getY();
|
||||
if (xCoord + (text.length() * 7) >= getWidth()) {
|
||||
@@ -187,6 +205,9 @@ public class ResizableRaceCanvas extends Canvas {
|
||||
this.updateBoats();
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw boundary of the race.
|
||||
*/
|
||||
public void drawBoundaries() {
|
||||
if (this.raceBoundaries == null) {
|
||||
return;
|
||||
@@ -263,6 +284,39 @@ public class ResizableRaceCanvas extends Canvas {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle name display in annotation
|
||||
*/
|
||||
public void toggleAnnoName() {
|
||||
if (annoName) {
|
||||
annoName = false;
|
||||
} else {
|
||||
annoName = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle abbreviation display in annotation
|
||||
*/
|
||||
public void toggleAnnoAbbrev() {
|
||||
if (annoAbbrev) {
|
||||
annoAbbrev = false;
|
||||
} else {
|
||||
annoAbbrev = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle speed display in annotation
|
||||
*/
|
||||
public void toggleAnnoSpeed() {
|
||||
if (annoSpeed) {
|
||||
annoSpeed = false;
|
||||
} else {
|
||||
annoSpeed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws boats while race in progress, when leg heading is set.
|
||||
*/
|
||||
@@ -283,7 +337,7 @@ public class ResizableRaceCanvas extends Canvas {
|
||||
}
|
||||
|
||||
if (raceAnno)
|
||||
displayText(boat.getAbbrev(), boat.getVelocity(), this.map.convertGPS(boat.getCurrentPosition()));
|
||||
displayText(boat.toString(), boat.getAbbrev(), boat.getVelocity(), this.map.convertGPS(boat.getCurrentPosition()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +96,10 @@ public class RaceTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
/*@Test
|
||||
|
||||
//Test temporarily removed as countdown timer now uses animation timer
|
||||
|
||||
public void timerDelaysByHalfSecond() {
|
||||
|
||||
ArrayList<Leg> legs = new ArrayList<>();
|
||||
@@ -108,9 +111,10 @@ public class RaceTest {
|
||||
long timeStarted = System.currentTimeMillis();
|
||||
race.countdownTimer();
|
||||
|
||||
assertTrue(System.currentTimeMillis() - timeStarted > 500);
|
||||
//assertTrue(System.currentTimeMillis() - timeStarted > 500);
|
||||
System.out.println(System.currentTimeMillis() - timeStarted);
|
||||
|
||||
}
|
||||
}*/
|
||||
|
||||
@Test
|
||||
public void scalerScalesVelocityCorrectly() {
|
||||
|
||||
Reference in New Issue
Block a user