Boats not moving working on fix

Merge remote-tracking branch 'remotes/origin/sprint4_master' into story36

# Conflicts:
#	mock/src/main/java/seng302/DataInput/XMLReader.java
#	visualiser/src/main/java/seng302/Controllers/RaceController.java
#	visualiser/src/main/java/seng302/Mock/StreamedRace.java
#	visualiser/src/main/java/seng302/Model/ResizableRaceCanvas.java
#	visualiser/src/main/resources/scenes/race.fxml

#story[878]
This commit is contained in:
hba56
2017-05-22 17:45:45 +12:00
46 changed files with 3347 additions and 529 deletions
@@ -1,17 +1,21 @@
package seng302.Controllers;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import seng302.Mock.StreamedRace;
import seng302.Model.*;
import seng302.VisualiserInput;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.*;
/**
* Created by fwy13 on 15/03/2017.
@@ -21,9 +25,13 @@ public class RaceController extends Controller {
//user saved data for annotation display
private ArrayList<Boolean> presetAnno;
private ArrayList<Boat> startBoats;
private Integer sparkLineNumber = 0;
private ResizableRaceCanvas raceMap;
private ResizableRaceMap raceBoundaries;
private ArrayList<String> colours;
private Map<Integer, String> boatColours = new HashMap<>();
private int legNum;
private RaceClock raceClock;
@FXML SplitPane race;
@FXML CheckBox showFPS;
@@ -43,6 +51,9 @@ public class RaceController extends Controller {
@FXML TableColumn<Boat, String> boatTeamColumn;
@FXML TableColumn<Boat, String> boatMarkColumn;
@FXML TableColumn<Boat, String> boatSpeedColumn;
@FXML LineChart<Number, Number> sparklineChart;
@FXML NumberAxis xAxis;
@FXML NumberAxis yAxis;
/**
* Updates the ResizableRaceCanvas (raceMap) with most recent data
@@ -78,6 +89,7 @@ public class RaceController extends Controller {
@Override
public void initialize(URL location, ResourceBundle resources) {
//listener for fps
startBoats = new ArrayList<>();
showFPS.selectedProperty().addListener((ov, old_val, new_val) -> {
if (showFPS.isSelected()) {
FPS.setVisible(true);
@@ -87,6 +99,64 @@ public class RaceController extends Controller {
});
}
/**
* Creates and sets initial display for Sparkline for race positions.
* A data series for each boat in the race is added.
* Position numbers are displayed.
*
* @param boats boats to display on the sparkline
*/
public void createSparkLine(ObservableList<Boat> boats){
// NOTE: Y axis is in negatives to display correct positions
makeColours();
startBoats.addAll(boats);
mapBoatColours();
// all boats start in 'last' place
for (int i=0; i<startBoats.size(); i++){
XYChart.Series<Number, Number> series = new XYChart.Series();
series.getData().add(new XYChart.Data(0, -startBoats.size()));
series.getData().add(new XYChart.Data(0, -startBoats.size()));
sparklineChart.getData().add(series);
sparklineChart.getData().get(i).getNode().setStyle("-fx-stroke: " +
""+boatColours.get(startBoats.get(i).getSourceID())+";");
}
sparklineChart.setCreateSymbols(false);
// set x axis details
xAxis.setAutoRanging(false);
xAxis.setTickMarkVisible(false);
xAxis.setTickLabelsVisible(false);
xAxis.setMinorTickVisible(false);
xAxis.setUpperBound(startBoats.size()*legNum);
xAxis.setTickUnit(startBoats.size()*legNum);
// set y axis details
yAxis.setLowerBound(-(startBoats.size()+1));
yAxis.setUpperBound(0);
yAxis.setAutoRanging(false);
yAxis.setLabel("Position in Race");
yAxis.setTickUnit(1);
yAxis.setTickMarkVisible(false);
yAxis.setMinorTickVisible(false);
// hide minus number from displaying on axis
yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) {
@Override
public String toString(Number value) {
if ((Double)value == 0.0
|| (Double)value < -startBoats.size()){
return "";
}
else {
return String.format("%7.0f", -value.doubleValue());
}
}
});
}
/**
* Initializes and runs the race, based on the user's chosen scale factor
* Currently uses an example racecourse
@@ -97,7 +167,7 @@ public class RaceController extends Controller {
public void startRace(VisualiserInput visualiserInput, RaceClock raceClock) {
//newRace.initialiseBoats();
legNum = visualiserInput.getCourse().getLegs().size()-1;
raceMap = new ResizableRaceCanvas(visualiserInput.getCourse());
raceMap.setMouseTransparent(true);
raceMap.widthProperty().bind(canvasBase.widthProperty());
@@ -120,7 +190,13 @@ public class RaceController extends Controller {
//Initialize save annotation array, fps listener, and annotation listeners
timeZone.setText(raceClock.getTimeZone());
timer.textProperty().bind(raceClock.durationProperty());
//RaceClock.duration isn't necessarily being changed in the javaFX thread, so we need to runlater the update.
raceClock.durationProperty().addListener((observable, oldValue, newValue) -> {
Platform.runLater(() -> {
timer.setText(newValue);
});
});
this.raceClock = raceClock;
raceMap.setRaceClock(raceClock);
@@ -170,6 +246,77 @@ public class RaceController extends Controller {
});
}
/**
* Updates the sparkline to display current boat positions.
* New points are plotted to represent each boat when required.
*
* @param boatsInRace used for current boat positions.
*/
public void updateSparkline(ObservableList<Boat> boatsInRace){
int placingVal = boatsInRace.size();
sparkLineNumber++;
for (int i = boatsInRace.size() - 1; i >= 0; i--){
for (int j = startBoats.size() - 1; j >= 0; j--){
if (boatsInRace.get(i)==startBoats.get(j)){
// when a boat is on its first leg
if (boatsInRace.get(i).getCurrentLeg().getLegNumber()==0){
// adjust boats latest point on X axis
sparklineChart.getData().get(j).getData().get(1)
.setXValue(sparkLineNumber);
}
// when a boat first enters its second leg
else if (boatsInRace.get(i).getCurrentLeg().getLegNumber
()==1 && sparklineChart.getData().get(j).getData
().size()==2){
// adjust boats position from start mark
sparklineChart.getData().get(j).getData().get(1)
.setYValue(-placingVal);
sparklineChart.getData().get(j).getData().get(1)
.setXValue(sparkLineNumber);
sparklineChart.getData().get(j).getData().add(new XYChart.Data<>
(sparkLineNumber, -placingVal));
}
// plot new point for boats current position
else {
sparklineChart.getData().get(j).getData().add
(new XYChart.Data<>(sparkLineNumber, -placingVal));
}
placingVal-=1;
}
}
}
// xAxis.setUpperBound(sparkLineNumber);
// xAxis.setTickUnit(sparkLineNumber);
}
private void makeColours() {
colours = new ArrayList<>(Arrays.asList(
colourToHex(Color.BLUEVIOLET),
colourToHex(Color.BLACK),
colourToHex(Color.RED),
colourToHex(Color.ORANGE),
colourToHex(Color.DARKOLIVEGREEN),
colourToHex(Color.LIMEGREEN),
colourToHex(Color.PURPLE),
colourToHex(Color.DARKGRAY),
colourToHex(Color.YELLOW)
));
}
private void mapBoatColours() {
int currentColour = 0;
for (Boat boat : startBoats) {
if (!boatColours.containsKey(boat.getSourceID())) {
boatColours.put(boat.getSourceID(), colours.get(currentColour));
}
currentColour = (currentColour + 1) % colours.size();
}
}
/**
* Set up boat annotations
*/
@@ -225,6 +372,13 @@ public class RaceController extends Controller {
});
}
private String colourToHex(Color color) {
return String.format( "#%02X%02X%02X",
(int)( color.getRed() * 255 ),
(int)( color.getGreen() * 255 ),
(int)( color.getBlue() * 255 ) );
}
public RaceClock getRaceClock() {
return raceClock;
}
@@ -54,11 +54,14 @@ public class StartController extends Controller implements Observer {
///Tracks whether the race has been started (that is, has startRaceNoScaling() be called).
private boolean hasRaceStarted = false;
//Tracks whether or not a clock has been created and setup, which occurs after receiving enough information.
private boolean hasCreatedClock = false;
/**
* Begins the race with a scale factor of 1
*/
private void startRaceNoScaling() {
while(visualiserInput.getRaceStatus() == null);//TODO probably remove this.
//while(visualiserInput.getRaceStatus() == null);//TODO probably remove this.
countdownTimer();
}
@@ -106,7 +109,14 @@ public class StartController extends Controller implements Observer {
private void setRaceClock() {
raceClock = new RaceClock(raceData.getZonedDateTime());
timeZoneTime.textProperty().bind(raceClock.timeStringProperty());
raceClock.timeStringProperty().addListener((observable, oldValue, newValue) -> {
Platform.runLater(() -> {
timeZoneTime.setText(newValue);
});
});
//TEMP REMOVE
//timeZoneTime.textProperty().bind(raceClock.timeStringProperty());
raceClock.run();
}
@@ -116,7 +126,12 @@ public class StartController extends Controller implements Observer {
long utcTime = visualiserInput.getRaceStatus().getExpectedStartTime();
raceClock.setStartingTime(raceClock.getLocalTime(utcTime));
raceStartLabel.setText(DateTimeFormatter.ofPattern(dateFormat).format(raceClock.getStartingTime()));
timer.textProperty().bind(raceClock.durationProperty());
raceClock.durationProperty().addListener((observable, oldValue, newValue) -> {
Platform.runLater(() -> {
timer.setText(newValue);
});
});
});
}
@@ -138,12 +153,15 @@ public class StartController extends Controller implements Observer {
}
if (streamedCourse.hasReadCourse()) {
Platform.runLater(() -> {
setRaceClock();
if(visualiserInput.getRaceStatus() == null){
return;//TEMP BUG FIX if the race isn't sending race status messages (our mock currently doesn't), then it would block the javafx thread with the previous while loop.
}// TODO - replace with observer on VisualiserInput
setStartingTime();
setCurrentTime();
if (!this.hasCreatedClock) {
this.hasCreatedClock = true;
setRaceClock();
if (visualiserInput.getRaceStatus() == null) {
return;//TEMP BUG FIX if the race isn't sending race status messages (our mock currently doesn't), then it would block the javafx thread with the previous while loop.
}// TODO - replace with observer on VisualiserInput
setStartingTime();
setCurrentTime();
}
});
}
@@ -97,6 +97,7 @@ public class BoatXMLReader extends XMLReader {
/**
* Reads the information about one boat
* Ignored values: ShapeID, StoweName, HullNum, Skipper, Type
* @param boat The node to read boat data from.
*/
private void readSingleBoat(Node boat) {
StreamedBoat streamedBoat;
@@ -127,9 +128,12 @@ public class BoatXMLReader extends XMLReader {
}
}
/**
* Reads the positional information about a boat
* Ignored values: FlagPosition, MastTop, Z value of GPSposition
* @param sourceID The source ID of the boat.
* @param GPSPosition The relative GPS position of the boat.
*/
private void readBoatPositionInformation(int sourceID, Node GPSPosition) {
// TODO Get relative point before implementing. (GPSposition is based off a relative point).
@@ -122,6 +122,7 @@ public class StreamedCourseXMLReader extends XMLReader {
/**
* Indexes CompoundMark elements by their ID for use in generating the course, and populates list of Markers.
* @throws StreamedCourseXMLException if a CompoundMark element contains an unhandled number of compoundMarks.
* @see seng302.Model.Marker
*/
private void readCompoundMarks() throws StreamedCourseXMLException {
@@ -77,6 +77,7 @@ public class StreamedRace implements Runnable {
* @param timeElapsed Time that has elapse since the start of the the race.
*/
private void checkPosition(Boat boat, long timeElapsed) {
boolean legChanged = false;
StreamedCourse raceData = visualiserInput.getCourse();
BoatStatus boatStatusMessage = visualiserInput.getBoatStatusMap().get(boat.getSourceID());
if (boatStatusMessage != null) {
@@ -84,8 +85,12 @@ public class StreamedRace implements Runnable {
int legNumber = boatStatusMessage.getLegNumber();
if (legNumber >= 1 && legNumber < legs.size() && boat.getCurrentLeg().getLegNumber() != legNumber) {
boat.setCurrentLeg(legs.get(legNumber), controller.getRaceClock());
if (legNumber >= 1 && legNumber < legs.size()) {
if (boat.getCurrentLeg() != legs.get(legNumber)){
boat.setCurrentLeg(legs.get(legNumber), controller.getRaceClock());
legChanged = true;
}
}
if (boatStatusEnum == BoatStatusEnum.RACING) {
@@ -96,11 +101,13 @@ public class StreamedRace implements Runnable {
boatsFinished++;
boat.setTimeFinished(timeElapsed);
boat.setFinished(true);
//System.out.println("Boat finished");
}
}
//Update the boat display table in the GUI to reflect the leg change
updatePositions();
if (legChanged) {
//Update the boat display table in the GUI to reflect the leg change
updatePositions();
controller.updateSparkline(startingBoats);
}
}
/**
@@ -154,6 +161,7 @@ public class StreamedRace implements Runnable {
*/
public void run() {
setControllerListeners();
Platform.runLater(() -> controller.createSparkLine(startingBoats));
initialiseBoats();
startRaceStream();
}
@@ -190,6 +198,7 @@ public class StreamedRace implements Runnable {
public void handle(long arg0) {
if (boatsFinished < startingBoats.size()) {
boatsFinished = 0;
//controller.updateSparkline(startingBoats);
totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted;
for (Boat boat : startingBoats) {
@@ -207,6 +216,7 @@ public class StreamedRace implements Runnable {
}
}
//System.out.println(boatsFinished + ":" + startingBoats.size());
} else {
controller.finishRace(startingBoats);
stop();
@@ -59,6 +59,8 @@ public class RaceClock implements Runnable {
this.time = time;
this.timeString.set(DateTimeFormatter.ofPattern("HH:mm:ss dd/MM/YYYY Z").format(time));
this.lastTime = System.currentTimeMillis();
if(startingTime != null) {
long seconds = Duration.between(startingTime.toLocalDateTime(), time.toLocalDateTime()).getSeconds();
if(seconds < 0)
@@ -66,6 +68,7 @@ public class RaceClock implements Runnable {
else
duration.set(String.format("Time: %02d:%02d:%02d", seconds/3600, (seconds%3600)/60, seconds%60));
}
}
/**
@@ -13,9 +13,8 @@ import seng302.RaceMap;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
/**
* This creates a JavaFX Canvas that is fills it's parent.
@@ -35,6 +34,7 @@ public class ResizableRaceCanvas extends ResizableCanvas {
private List<Color> colours;
private final List<Marker> markers;
private final RaceDataSource raceData;
private Map<Integer, Color> boatColours = new HashMap<>();
private RaceClock raceClock;
@@ -60,6 +60,7 @@ public class ResizableRaceCanvas extends ResizableCanvas {
*/
public void setBoats(List<Boat> boats) {
this.boats = boats;
mapBoatColours();
}
@@ -296,20 +297,21 @@ public class ResizableRaceCanvas extends ResizableCanvas {
* Draws boats while race in progress, when leg heading is set.
*/
private void updateBoats() {
int currentColour = 0;
if (boats != null) {
if (boatColours.size() < boats.size()) mapBoatColours();
for (Boat boat : boats) {
boolean finished = boat.getCurrentLeg().getName().equals("Finish") || boat.getCurrentLeg().getName().equals("DNF");
boolean isStart = boat.isStarted();
int sourceID = boat.getSourceID();
if (!finished && isStart) {
displayBoat(boat, boat.getHeading(), colours.get(currentColour));
displayBoat(boat, boat.getHeading(), boatColours.get(sourceID));
GraphCoordinate wakeFrom = this.map.convertGPS(boat.getCurrentPosition());
GraphCoordinate wakeTo = this.map.convertGPS(boat.getWake());
displayLine(wakeFrom, wakeTo, colours.get(currentColour));
displayLine(wakeFrom, wakeTo, boatColours.get(sourceID));
} else if (!isStart) {
displayBoat(boat, boat.getHeading(), colours.get(currentColour));
displayBoat(boat, boat.getHeading(), boatColours.get(sourceID));
} else {
displayBoat(boat, 0, colours.get(currentColour));
displayBoat(boat, 0, boatColours.get(sourceID));
}
if (raceAnno)
@@ -318,8 +320,7 @@ public class ResizableRaceCanvas extends ResizableCanvas {
}
displayText(boat.toString(), boat.getAbbrev(), boat.getVelocity(), this.map.convertGPS(boat.getCurrentPosition()), boat.getTimeSinceLastMark());
//TODO this needs to be fixed.
drawTrack(boat, colours.get(currentColour));
currentColour = (currentColour + 1) % colours.size();
drawTrack(boat, boatColours.get(sourceID));
}
}
}
@@ -327,6 +328,7 @@ public class ResizableRaceCanvas extends ResizableCanvas {
/**
* Draws all track points for a given boat. Colour is set by boat, opacity by track point.
* @param boat whose track is displayed
* @param colour The color to use for the track.
* @see seng302.Model.TrackPoint
*/
private void drawTrack(Boat boat, Color colour) {
@@ -356,4 +358,15 @@ public class ResizableRaceCanvas extends ResizableCanvas {
public void setRaceClock(RaceClock raceClock) {
this.raceClock = raceClock;
}
}
private void mapBoatColours() {
int currentColour = 0;
for (Boat boat : boats) {
if (!boatColours.containsKey(boat.getSourceID())) {
boatColours.put(boat.getSourceID(), colours.get(currentColour));
}
currentColour = (currentColour + 1) % colours.size();
}
}
}
@@ -148,6 +148,7 @@ public class VisualiserInput implements Runnable {
/**
* Sets the wind direction for the current course.
* @param direction The new wind direction for the course.
*/
private void setCourseWindDirection(double direction) {
this.course.setWindDirection(direction);
+17 -5
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.*?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
@@ -8,7 +9,7 @@
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<SplitPane fx:id="race" dividerPositions="0.7" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.Controllers.RaceController">
<SplitPane fx:id="race" dividerPositions="0.7" prefHeight="431.0" prefWidth="610.0" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.Controllers.RaceController">
<items>
<GridPane fx:id="canvasBase">
<columnConstraints>
@@ -31,9 +32,8 @@
<CheckBox fx:id="showName" layoutY="39.0" mnemonicParsing="false" selected="true" text="Show Boat Name" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="26.0" />
<CheckBox fx:id="showAbbrev" layoutY="61.0" mnemonicParsing="false" selected="true" text="Show Boat Abbreviation" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="52.0" />
<CheckBox fx:id="showSpeed" layoutY="90.0" mnemonicParsing="false" selected="true" text="Show Boat Speed" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="78.0" />
<CheckBox fx:id="showTime" mnemonicParsing="false" selected="true" text="Show Boat Time" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="130.0" />
<Button fx:id="saveAnno" layoutX="11.0" layoutY="106.0" maxWidth="154.0" mnemonicParsing="false" prefWidth="154.0" text="Save Annotation" AnchorPane.topAnchor="160.0" />
<Button fx:id="showSetAnno" layoutX="11.0" layoutY="139.0" mnemonicParsing="false" text="Show Set Annotation" AnchorPane.topAnchor="190.0" />
<Button fx:id="saveAnno" layoutX="11.0" layoutY="106.0" maxWidth="154.0" mnemonicParsing="false" prefWidth="154.0" text="Save Annotation" AnchorPane.topAnchor="130.0" />
<Button fx:id="showSetAnno" layoutX="11.0" layoutY="139.0" mnemonicParsing="false" text="Show Set Annotation" AnchorPane.topAnchor="160.0" />
</children>
</AnchorPane>
</content>
@@ -73,7 +73,7 @@
</GridPane>
<AnchorPane layoutX="450.0" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="200.0" GridPane.columnIndex="1">
<children>
<TableView fx:id="boatInfoTable" layoutX="-2.0" prefHeight="600.0" prefWidth="264.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="-2.0" AnchorPane.rightAnchor="-62.0" AnchorPane.topAnchor="0.0">
<TableView fx:id="boatInfoTable" layoutX="-2.0" prefHeight="265.0" prefWidth="242.0" AnchorPane.bottomAnchor="164.0" AnchorPane.leftAnchor="-2.0" AnchorPane.rightAnchor="-62.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="boatPlacingColumn" prefWidth="50.0" text="Place" />
<TableColumn fx:id="boatTeamColumn" prefWidth="100.0" text="Team" />
@@ -81,6 +81,18 @@
<TableColumn fx:id="boatSpeedColumn" prefWidth="75.0" text="Speed" />
</columns>
</TableView>
<AnchorPane layoutY="265.0" prefHeight="167.0" prefWidth="178.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<LineChart fx:id="sparklineChart" layoutX="-211.0" layoutY="-186.0" mouseTransparent="true" prefHeight="167.0" prefWidth="178.0" titleSide="LEFT" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<xAxis>
<NumberAxis side="BOTTOM" fx:id="xAxis"/>
</xAxis>
<yAxis>
<NumberAxis side="LEFT" fx:id="yAxis"/>
</yAxis>
</LineChart>
</children>
</AnchorPane>
</children>
</AnchorPane>
</items>